cad step格式转换为threejs支持的json格式

step格式的文件在网页端3D显示的时候是不能直接加载的。因为threejs没有提供step格式的加载器。所以如果需要在网页端显示step模型就需要先将step格式的文件进行转换。有的技术方案是通过先转化为stl然后转换为别的gltf,这样比较麻烦,而且多次转换不知道精度会不会丢失太多。

这里我通过搜索各种资料找到了一种转换的方法。直接将stp转换为json格式。关键代码如下:

def step2json(inputFile, outputFile):  # STEP Files
    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(inputFile)

    if status == IFSelect_RetDone:  # check status
        failsonly = False
        step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
        step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)

        step_reader.TransferRoot(1)
        _nbs = step_reader.NbShapes()
        _shape = step_reader.Shape(1)
        _tess = ShapeTesselator(_shape)
        _tess.Compute(compute_edges=False, mesh_quality=50)

        with open(outputFile, "w") as text_file:
            json = _tess.ExportShapeToThreejsJSONString(inputFile)
            json = json.replace("\\", "/")
            text_file.write(json)
    else:
        raise Exception("Error: can't read file - Method: _load_STEP_file")

输入是stp格式的文件,输出为three.js支持的json文件

完整源码获取 http://mutou888.com/pay/twenty.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章