STEP file parser
This commit is contained in:
parent
0e599a886e
commit
27738cd215
1 changed files with 33 additions and 0 deletions
33
parse_step_file.py
Normal file
33
parse_step_file.py
Normal 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.
|
Loading…
Add table
Add a link
Reference in a new issue