-
Notifications
You must be signed in to change notification settings - Fork 1
/
conv.py
130 lines (98 loc) · 4.52 KB
/
conv.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/freecadcmd
# !/Applications/FreeCAD.app/Contents/MacOS/FreeCADCmd
'''
Original:
http://jordan.husney.com/archives/2013/07/converting_cad_files_to_stl_us.php
Repaired:
J. R.I.B.-Wein, Open Source Ecology Germany
'''
# $ $(PYTHON) step_stl.py input.step output.stl #<-- yet another problem as the PYTHON was missing! now repaired, $(PYTHON) usually is simply 'python' or '/usr/bin/python' if not on the SYSTEM PATH.
# You can even use it in a more general way, freecad is very powerful:
# $ python freecad_convert.py path/to/infile.<source_ending> path/to/outfile.<target_ending>
'''
#Original instructions stated to overwrite this Variable:
$ export DYLD_FALLBACK_LIBRARY_PATH=\
/Applications/FreeCAD.app/Contents/Frameworks/lib:\
$DYLD_FALLBACK_LIBRARY_PATH
But it failed, because the script did not use the Variable. Now it's repaired. See below:
'''
# Rewritten by OSE Germany, J.R.I.B.-Wein:
FREECAD_LIB_PATH = 'F:/FreeCAD/bin' # path to your FreeCAD.so or FreeCAD.dll file
# Can be overwritten or added to from the command line via:
# ($ stands for 'bash', eff. command line, and has to be omitted!)
# ADD ANOTHER PATH:
# $ export FREECAD_LIB_PATH = '<path_to_lib_FreeCAD.so_or_.dll>':$FREECAD_LIB_PATH
# OVERWRITE:
# $ export FREECAD_LIB_PATH = '<path_to_lib_FreeCAD.so_or_.dll>'
# CAN ALSO BE GIVEN WHILE EXECUTING THIS SCRIPT:
# $ python <path/to/script_for_bash_conversion>.py
import sys
sys.path.append(FREECAD_LIB_PATH) # <-- added, otherwise FreeCAD is not found
import os
import FreeCAD
import Part
import Mesh
# import Blender #<-- kept as a reminder for how well those two open source gems interact
# The original author wrote this script. The syntax was figured out by recording and evaluating some macros in
# FreeCAD. Thanks open source movement.
in_f, out_f = sys.argv[1], sys.argv[2] # <-- repaired, out of bounds
in_fn, in_ext = os.path.splitext(in_f)
out_fn, out_ext = os.path.splitext(out_f)
print(in_ext, " -> ", out_ext)
mesh_formats = ['.dae', '.stl', '.obj']
def main():
shape = Part.Shape()
# shape_formats = ['.brp', '.igs', '.stp']
if in_ext in mesh_formats:
print("Opening mesh file: ", in_f)
Mesh.open(in_f)
o = FreeCAD.getDocument("Unnamed").findObjects()[0]
# print("dir: ", dir(o))
if out_ext in mesh_formats:
print("Exporting to mesh file: ", out_f)
Mesh.export([o], out_f)
else:
# TODO This is not optimizing the resulting amount of faces!
# see http://www.freecadweb.org/wiki/index.php?title=Mesh_to_Part
shape.makeShapeFromMesh(o.Mesh.Topology, 0.05) # tolerance for sewing
exportParametric(shape, out_f, out_ext)
elif out_ext in mesh_formats:
print("Opening parametric file: ", in_f)
Part.open(in_f)
o = FreeCAD.getDocument("Unnamed").findObjects()[0]
print("Exporting to mesh file: ", out_f)
Mesh.export([o], out_f)
else:
# Parametric -> Parametric
print("Opening parametric file: ", in_f)
shape.read(in_f)
exportParametric(shape, out_f, out_ext)
def exportParametric(shape, out_f, out_ext):
print("Exporting to parametric file: ", out_f)
if out_ext == '.brp':
shape.exportBrep(out_f)
elif out_ext == '.igs':
shape.exportIges(out_f)
elif out_ext == '.stl':
shape.exportStl(out_f)
elif out_ext == '.stp':
shape.exportStep(out_f)
else:
print("Export to '%s' not supported." % (out_ext))
main()
'''def import_fcstd(filename): try: import FreeCAD except ValueError: Blender.Draw.PupMenu('Error%t|FreeCAD library
not found. Please check the FREECADPATH variable in the import script is correct') else: scene =
Blender.Scene.GetCurrent() import Part doc = FreeCAD.open(filename) objects = doc.Objects for ob in objects: if
ob.Type[:4] == 'Part': shape = ob.Shape if shape.Faces: mesh = Blender.Mesh.New() rawdata = shape.tessellate(1) for v
in rawdata[0]: mesh.verts.append((v.x,v.y,v.z)) for f in rawdata[1]: mesh.faces.append.append(f) scene.objects.new(
mesh,ob.Name) Blender.Redraw() '''
def main():
pass
# Blender.Window.FileSelector(import_fcstd, 'IMPORT FCSTD',
# Blender.sys.makename(ext='.fcstd'))
# This lets you import the script without running it
if __name__ == '__main__':
main()
# It's used like this:
# $(PYTHON) step_stl.py input.step output.stl #<-- yet another problem as the PYTHON was missing! now repaired,
# $(PYTHON) usually is simply 'python' or '/usr/bin/python' if not on the SYSTEM PATH.