3

I am looking for suggestions on how to tackle the following problem:

To convert a STEP (ISO 10303, AP 203/214) into a triangular mesh, i.e. into STL

Obviously, STEP supports various exact representations of some smooth curves, for instance NURBS, so the two are not isomorphic, hence there is no 'translating' one into another.

The research I have done so far suggested that the way to go would be to use some kind of CAD software, which I did - I wrote the following simple script using PythonOCC:

def read_step(filename):
    from OCC.STEPControl import STEPControl_Reader
    from OCC.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity

    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(filename)
    if status == IFSelect_RetDone:
        failsonly = False
        step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
        step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) 

        ok = step_reader.TransferRoot(1)
        _nbs = step_reader.NbShapes()
        return step_reader.Shape(1)
    else:
        raise ValueError('Cannot read the file')

def write_stl(shape, filename, def=0.1):
    from OCC.StlAPI import StlAPI_Writer
    import os

    directory = os.path.split(__name__)[0]
    stl_output_dir = os.path.abspath(directory)
    assert os.path.isdir(stl_output_dir)

    stl_file = os.path.join(stl_output_dir, filename)

    stl_writer = StlAPI_Writer()
    stl_writer.SetASCIIMode(False)

    from OCC.BRepMesh import BRepMesh_IncrementalMesh
    mesh = BRepMesh_IncrementalMesh(shape, def)
    mesh.Perform()
    assert mesh.IsDone()

    stl_writer.Write(shape, stl_file)
    assert os.path.isfile(stl_file)

...

Those functions are capable of doing the job. However, the result isn't always very satisfactory, for instance:

The original STEP file - 76K The original STEP file - 76K

Resulting STL file with def=0.9 - 16K Resulting STL file with def=0.9 - 16K

Resulting STL file with def=0.1 - 116K Resulting STL file with def=0.1 - 116K

Obviously, there is little reason to believe a generic converter is achievable, but I'm here to ask for some more insight. I would appreciate tips on resources that might be useful to help me take on this problem.

I would like to try and achieve the following results:

  • Hopefully make it work for as many models as possible
  • Not gain too much weight on the files

I would appreciate any hints and suggestions on where to look!

Jytug
  • 1,072
  • 2
  • 11
  • 29
  • What's wrong with the 116K resulting STL? – abenci Jul 20 '17 at 06:38
  • There's nothing wrong, it's just that the file is alread larger than the original, while the surface is not nearly as smooth – Jytug Jul 20 '17 at 07:13
  • You'll never had a surface as smooth as the analytical one, of course. If you try OBJ file format, you can benefit from smoothed normals that will make even 16K result look good. – abenci Jul 20 '17 at 07:34
  • Thanks for the tip, would you recommend some software to export OBJ files with bindings to Python / C++? – Jytug Jul 21 '17 at 08:11
  • No, sorry. I work in a different ecosystem. – abenci Jul 23 '17 at 08:42
  • @Jytug did you find solution to this? Did you stick with this format or you transitioned to OBJ? – user1584421 Aug 20 '20 at 10:04

0 Answers0