tpaviot / pythonocc-core

Python package for 3D geometry CAD/BIM/CAM
GNU Lesser General Public License v3.0
1.35k stars 376 forks source link

A question of converting IGES format models using OCC and pythonocc-core command line tools #1312

Closed sigurHeBin closed 6 months ago

sigurHeBin commented 6 months ago

Dear community: I've successfully deployed OCC and pythonocc-core on Linux, and I've written a script to convert an IGES format model to STL format. Although the script executed successfully, the original 15MB IGES model was reduced to a 4KB STL file. When I import the STL model into 3ds Max, the appearance of the model is completely different from the original. this is my python script:

import sys
from OCC.Core.IGESControl import IGESControl_Reader
from OCC.Core.StlAPI import StlAPI_Writer
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh

if len(sys.argv) != 3:
    print("Usage: python iges_to_stl.py source.iges destination.stl")
    sys.exit(1)

iges_file_path = sys.argv[1]
stl_file_path = sys.argv[2]

iges_reader = IGESControl_Reader()
iges_reader.ReadFile(iges_file_path)
iges_reader.TransferRoots()
shape = iges_reader.Shape()

mesh = BRepMesh_IncrementalMesh(shape, 0.001)  

stl_writer = StlAPI_Writer()
stl_writer.SetASCIIMode(False)  
stl_writer.Write(shape, stl_file_path)

print(f"Converted IGES file {iges_file_path} to STL file {stl_file_path}")

this is my command: python3 /app/software/blender-python/export_iges_to_stl.py /app/software/blender-python/f16.IGS /app/software/blender-python/f16.stl

this is my test model: f16.zip

Could you help me figure out what could be causing this? I look forward to your reply, thank you!

tpaviot commented 6 months ago

I suggest you use the builtin addon for dataexchange:

import sys
from OCC.Core.IGESControl import IGESControl_Reader
from OCC.Core.StlAPI import StlAPI_Writer
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
from OCC.Extend.DataExchange import read_iges_file, write_stl_file

if len(sys.argv) != 3:
    print("Usage: python iges_to_stl.py source.iges destination.stl")
    sys.exit(1)

iges_file_path = sys.argv[1]
stl_file_path = sys.argv[2]

shape = read_iges_file(iges_file_path)

write_stl_file(shape, stl_file_path, mode="binary", linear_deflection=0.9, angular_deflection=0.5)

print(f"Converted IGES file {iges_file_path} to STL file {stl_file_path}")

Be careful with the deflection, if it's too low, you'll get a huge stl. Default values are a good start, the resulting stl file is 8.6Mb on my machine.

sigurHeBin commented 6 months ago

thanks for your relpy! I used your code to test and find some question. image

image I found ListIteratorOfListOfShape not in OCC.Core.TopTools, but in the OCC.Core.TopoDS

Maybe something went wrong when I was cmake the code. I modified the code and successfully converted the model after execution. My pythonocc-core version is 7.7.2 and My OCCT Version is 7.7.2 Thank you for your help!