3

我正在寻找有关如何解决以下问题的建议:

将 STEP (ISO 10303, AP 203/214) 转换为三角形网格,即转换为 STL

显然,STEP 支持某些平滑曲线的各种精确表示,例如 NURBS,因此两者不是同构的,因此没有将一个“转换”为另一个。

到目前为止我所做的研究表明,要走的路是使用某种 CAD 软件,我这样做了 - 我使用以下简单脚本编写了以下简单脚本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)

...

这些功能能够胜任这项工作。但是,结果并不总是很令人满意,例如:

原始 STEP 文件 - 76K 原始 STEP 文件 - 76K

生成的 STL 文件 def=0.9 - 16K 生成的 STL 文件 def=0.9 - 16K

生成的 STL 文件 def=0.1 - 116K 生成的 STL 文件 def=0.1 - 116K

显然,几乎没有理由相信通用转换器是可以实现的,但我在这里寻求更多见解。我会很感激有关资源的提示,这些提示可能有助于我解决这个问题。

我想尝试并实现以下结果:

  • 希望让它适用于尽可能多的模型
  • 不要在文件上增加太多重量

我将不胜感激有关在哪里寻找的任何提示和建议!

4

0 回答 0