why opencascade' speed to read stl file not as fast as freecad

In my application now I use freecadcmd to read the stl 's width、height、volume and area, and the code is bellow:

import sys, Mesh, string, json,os
from FreeCAD import Base
filename = sys.argv[2]
name_arr = filename.split('.')
mesh = Mesh.Mesh(filename)
obj["area"] = mesh.Area
obj["volume"] = mesh.Volume

as it as ap.py  and call it by : freecadcmd ab.py  xxxx.stl

But now I want to use occ , and the c++ code is bellow:

int main(int argc, char *argv[]){
    if(argc <= 1){
        std::cout<<"occstl wrong arguments count!"<<std::endl;
        return -1;
    }
    Standard_CString aFileName = argv[1];
    TopoDS_Shape shape;
    StlAPI_Reader reader;
    reader.Read(shape,aFileName);
    GProp_GProps system;
    BRepGProp::VolumeProperties(shape, system);
    Standard_Real volume = system.Mass();
    std::cout<<"volume:"<<std::to_string(volume)<<std::endl;
    BRepGProp::SurfaceProperties(shape, system);
    Standard_Real area = system.Mass();
    std::cout<<"Area:"<<std::to_string(area)<<std::endl;
}

Then  I use the same stl file , the app use freecadcmd  cost  three second, but the occ c++ code cost about 2 minutes. Is there any problemes in my c++ code ?

 

Forgive me but I don't use c++ very well , can anyone explain why the difference of speed is so big?

 

Attachments: 
Patrik Mueller's picture

Hi,

I assume your Python code works with the mesh data without topology creation. OCCT reads the STL file and creates a  TopoDS_Shape from it. Most of the time and memory will be used by building the topology from the mesh.

haizhao li's picture

void StlAPI_Reader::Read(TopoDS_Shape& aShape, const Standard_CString aFileName) 
{
  OSD_Path aFile(aFileName);
  
  Handle(StlMesh_Mesh) aSTLMesh = RWStl::ReadFile(aFile);
    .........................There is a lot of code ...
  StlMesh_MeshExplorer aMExp (aSTLMesh);
  
  for (iND=1;iND<=NumberDomains;iND++) 
  {
    for (aMExp.InitTriangle (iND); aMExp.MoreTriangle (); aMExp.NextTriangle ()) 
    {
        .........................There is a lot of code ...
    }
  }
  aSTLMesh->Clear();

  aSewingTool.Load( aComp );
  aSewingTool.Perform();
  aShape = aSewingTool.SewedShape();
  if ( aShape.IsNull() )
    aShape = aComp;
}

You are right, The method RWStl::ReadFile(aFile); is read a stl file to a StlMesh_Mesh, and the other code conver  StlMesh_Mesh to TopoDS_Shape so spend a lot of time.  MayBe  I don't need to convert  StlMesh_Mesh to TopoDS_Shape, because all I need to do is calculate the volume and the area and width、height and length, but I can't find api to calculate  a StlMesh_Mesh. just like  BRepGProp::SurfaceProperties(shape, system);​. 

qa qa's picture

Hello, there is no such API. You have to use RWStl::ReadFile(aFile) to get mesh.

qa qa