STEP file parser

This commit is contained in:
Igor Brylyov 2024-03-22 16:31:17 +03:00
parent 0e599a886e
commit 27738cd215

33
parse_step_file.py Normal file
View file

@ -0,0 +1,33 @@
import sys
import SimpleITK
from OCC.Exchange import STEPControl_Reader
from OCC.IFSelect import IFSelect_RetDone
from OCC.TopoDS import TopoDS_Shape
def save_shape(shape, filename):
from OCC.STEPControl import STEPControl_Writer
writer = STEPControl_Writer()
writer.Transfer(shape, STEPControl_Reader().StepModel(), False)
writer.Write(filename)
def parse_step_file(filepath):
reader = STEPControl_Reader()
status = reader.ReadFile(filepath.encode('utf-8'))
if status == IFSelect_RetDone:
nbr = reader.NbRootsForTransfer()
reader.TransferRoots()
shapes = [reader.Shape(i) for i in range(1, nbr + 1)]
solids = []
for i, shape in enumerate(shapes, start=1):
filename = f"solid_{i}.step"
save_shape(shape, filename)
solids.append({"name": f"solid_{i}", "file": filename})
return solids
if __name__ == "__main__":
step_file_path = sys.argv[1]
solids = parse_step_file(step_file_path)
print(solids) # This print is simplistic; in practice, you might write this to a file or handle differently.