-
Notifications
You must be signed in to change notification settings - Fork 4
/
step_to_stl.py
63 lines (49 loc) · 1.38 KB
/
step_to_stl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import getopt
import os
import sys
from OCC.StlAPI import StlAPI_Writer
from OCC.STEPControl import STEPControl_Reader
from OCC.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity
def usage():
print('step_to_stl.py -i source -o dest')
sys.exit(2)
def convert(source, dest):
step_reader = STEPControl_Reader()
status = step_reader.ReadFile(source)
if status == IFSelect_RetDone:
i = 1
ok = False
number_of_roots = step_reader.NbRootsForTransfer()
while i <= number_of_roots and not ok:
ok = step_reader.TransferRoot(i)
i += 1
if (not ok):
return { 'error': 'Failed to find a suitable root for the STEP file' }
shape = step_reader.Shape(1)
output = os.path.abspath(dest)
stl_ascii = False
stl_writer = StlAPI_Writer()
stl_writer.SetASCIIMode(stl_ascii)
stl_writer.Write(shape, output)
print "STL FILE: %s" % output
else:
print "Error, can't read file: %s" % './demo.stp'
def main(argv):
try:
opts, args = getopt.getopt(argv, "hi:o:", ["infile=", "outfile="])
except getopt.GetoptError:
usage()
source = None
dest = None
for opt, arg in opts:
if opt in ("-i", "--infile"):
source = arg
if opt in ("-o", "--outfile"):
dest = arg
if source != None and dest != None:
convert(source, dest)
else:
usage()
sys.exit(0)
if __name__ == '__main__':
main(sys.argv[1:])