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

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