Conversion from STL to STEP is slow

I am trying to convert an STL file into and STEP file. The file size id around 4MB. This is the following code that I use to perform the operation:

Standard_Boolean StlAPI_Reader::Read(TopoDS_Shape&          theShape,
	const Standard_CString theFileName)
{
	Handle(Poly_Triangulation) aMesh = RWStl::ReadFile(theFileName);
	if (aMesh.IsNull())
	{
		return Standard_False;
	}

	TopoDS_Vertex aTriVertexes[3];
	TopoDS_Face aFace;
	TopoDS_Wire aWire;
	BRepBuilderAPI_Sewing aSewingTool;
	aSewingTool.Init(1e-6, Standard_True);

	TopoDS_Compound aComp;
	BRep_Builder BuildTool;
	BuildTool.MakeCompound(aComp);

	const TColgp_Array1OfPnt& aNodes = aMesh->Nodes();
	const Poly_Array1OfTriangle& aTriangles = aMesh->Triangles();
	for (Standard_Integer aTriIdx = aTriangles.Lower();
		aTriIdx <= aTriangles.Upper();
		++aTriIdx)
	{
		const Poly_Triangle& aTriangle = aTriangles(aTriIdx);

		Standard_Integer anId[3];
		aTriangle.Get(anId[0], anId[1], anId[2]);

		const gp_Pnt& aPnt1 = aNodes(anId[0]);
		const gp_Pnt& aPnt2 = aNodes(anId[1]);
		const gp_Pnt& aPnt3 = aNodes(anId[2]);
		if ((!(aPnt1.IsEqual(aPnt2, 0.0))) && (!(aPnt1.IsEqual(aPnt3, 0.0))))
		{
			aTriVertexes[0] = BRepBuilderAPI_MakeVertex(aPnt1);
			aTriVertexes[1] = BRepBuilderAPI_MakeVertex(aPnt2);
			aTriVertexes[2] = BRepBuilderAPI_MakeVertex(aPnt3);

			aWire = BRepBuilderAPI_MakePolygon(aTriVertexes[0], aTriVertexes[1], aTriVertexes[2], Standard_True);
			if (!aWire.IsNull())
			{
				aFace = BRepBuilderAPI_MakeFace(aWire);
				if (!aFace.IsNull())
				{
					BuildTool.Add(aComp, aFace);
				}
			}
		}
	}

	aSewingTool.Load(aComp);
	aSewingTool.Perform();
	theShape = aSewingTool.SewedShape();
	if (theShape.IsNull())
	{
		theShape = aComp;
	}
	return Standard_True;
}

It took me around 13 hours to complete the operation. I have tried changing the parameter aSewingTool.Init(), but there is no effect. 

On debugging I found out that most of the time is spent on the command aSewingTool.Perform().  

Is there any way to make it slower or any other way to convert it. 

 

Thanks.

Kirill Gavrilov's picture

What is your context that facetted STEP file generated from STL triangulation in this way have any use at all?
I wouldn't expect any BRep tool being able to do something useful with such kind of structure - considering that STL files coming from modern scanners can be of enormous size.

Anyway, concerning your specific question - sewing tool is not designed for such kind of input, and it might be indeed quite slow when used in this way.
You're better creating BRep sharing Vertices and Edges from the beginning, rather than emitting a tremendous number of unique shapes and relying on BRepBuilderAPI_Sewing for reconstructing sharing for you.

Vishesh Chanana's picture

Could you give me an example code to convert STl into STEP file format. I am very new to this.

Kirill Gavrilov's picture

I still don't get for which applications you are trying to convert an STL mesh into STEP file.
Usually, surface needs to be reconstructed from triangulation (using SSP or GeomPlate),
so that you will have a BSpline surface instead of facets:
https://www.opencascade.com/content/surfaces-scattered-points

But reconstructing surface is a complex task, involving user interaction (e.g. difficult to automate) and various algorithms.

Vishesh Chanana's picture

I am trying to convert a point cloud into a STEP file. Since, there is no direct way to convert a point cloud into a step, I am converting the PCD into an STL and then trying to convert this stl into the final STEP file. 

If you can advise me some other way to do it, it would be a great help.

Thanks

prabhuraju06_151460's picture

Hi  Kirill Gavrilov 

How to convert the Stl files to STEP and IGES format ,Can you please share me sample Code and how to import the STL files into the C++ program using OpensCad lib