Converting a STEP file into an STL

北慕城南 提交于 2020-01-23 12:57:29

问题


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

Resulting STL file with def=0.9 - 16K

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!

来源:https://stackoverflow.com/questions/45185998/converting-a-step-file-into-an-stl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!