-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportProject.FCMacro
170 lines (134 loc) · 5.14 KB
/
exportProject.FCMacro
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Copyright 2016, Jon Nordby <[email protected]>. Licensed LGPLv2+ & MIT
# 2020, Benjamin Richter <[email protected]>
"""
exportProject:
Exports all files in document which has names like "mypart [dxf]" or "otherpart[stl,step]"
to individual files in a export directory. Default prefix: 'export/$project-'
Can be ran both as a macro from the FreeCAD UI and as a script on commandline.
"""
# TODO: support some kind of preview. Maybe using the recent camera macros?
import sys, os
import re
import time
macropath = "/home/user/.FreeCAD/Macro/FreeCAD-macros"
partTypeRegex = re.compile(r'^(.*)\[([,\w]+)\].*$')
sys.path.insert(0, '/usr/lib/freecad/lib/')
sys.path.insert(0, '/usr/lib/freecad/Mod/OpenSCAD/')
import FreeCAD
import exportCSG
def exportViewToPNG(objs, p):
# Exports the current view to a PNG suitable for printing on A4 or Letter.
# Reference: http://www.freecadweb.org/wiki/index.php?title=Std_ViewScreenShot
# Create an image which fits on an A4 or Letter page at 300dpi, landscape
width = 11 * 300 # inside letter
height = int(8.26 * 300) # inside A4
doc = FreeCAD.ActiveDocument
if not doc.FileName:
FreeCAD.Console.PrintError('Must save project first\n')
return
docDir, docFilename = os.path.split(doc.FileName)
filePrefix = os.path.splitext(docFilename)[0]
for o in doc.RootObjects: # hide all root objects
FreeCADGui.ActiveDocument.getObject(o.Name).Visibility = False
for o in objs:
print(o.Name)
FreeCADGui.ActiveDocument.getObject(o.Name).Visibility = True
FreeCADGui.ActiveDocument.ActiveView.fitAll()
Gui.updateGui()
time.sleep(0.5)
FreeCAD.Console.PrintMessage("Saving view to %s\n" % p)
FreeCADGui.ActiveDocument.ActiveView.saveImage(p.encode("utf-8"), width, height, 'White')
FreeCADGui.ActiveDocument.getObject(o.Name).Visibility = False
def exportMesh(objs, p):
import Mesh
Mesh.export(objs, p)
def exportDxf(objs, p):
import importDXF
importDXF.export(objs, p)
def exportStep(objs, p):
# XXX: will overwrite file if called with multiple args.. Last write wins
for o in objs:
shape = o.Shape
shape.exportStep(p)
def exportGif(objs, p):
# Dummy for gif export, exports visible part to generic file
doc = FreeCAD.ActiveDocument
if not doc.FileName:
FreeCAD.Console.PrintError('Must save project first\n')
return
for o in doc.RootObjects: # hide all root objects
FreeCADGui.ActiveDocument.getObject(o.Name).Visibility = False
for o in objs:
print(o.Name)
FreeCADGui.ActiveDocument.getObject(o.Name).Visibility = True
FreeCADGui.ActiveDocument.ActiveView.fitAll()
Gui.updateGui()
time.sleep(0.5)
FreeCAD.Console.PrintMessage("Saving gif")
exec(open(macropath + "/exportGif.FCMacro").read())
FreeCADGui.ActiveDocument.getObject(o.Name).Visibility = False
# TODO: support CAM and post-processors like: rml, spb
supportedTypes = {
'stl': exportMesh,
'obj': exportMesh,
'step': exportStep,
'dxf': exportDxf,
'scad': exportCSG.export,
'png': exportViewToPNG,
'gif': exportGif,
}
def extractTypes(label):
match = re.search(partTypeRegex, label)
if match:
types = match.group(2).split(',')
part = match.group(1)
else:
part = label
types = []
return [part, types]
def exportDocument(doc, exportdir):
exported = []
# if not os.path.exists(exportdir):
# os.makedirs(exportdir)
docname = os.path.splitext(os.path.basename(doc.FileName))[0]
for obj in doc.Objects:
partname, types = extractTypes(obj.Label)
#print "obj %s %s" % (obj.Label, types)
for t in types:
if not os.path.exists(t):
os.makedirs(t)
exporter = supportedTypes[t]
filename = "%s-%s.%s" % (docname, partname, t)
filepath = os.path.join(t, filename)
exporter([obj], filepath)
exported.append(filename)
return exported
def fileListing(files):
return '\n'.join("\t"+f for f in files)
def scriptmain():
projectfile = sys.argv[1]
doc = FreeCAD.openDocument(projectfile)
if len(sys.argv) >= 3:
exportdir = sys.argv[2]
else:
docdir = os.path.dirname(doc.FileName)
exportdir = os.path.join(docdir, 'export/')
print('Opening project', projectfile)
files = exportDocument(doc, exportdir)
print("Exported ", len(files), " files to '", exportdir, "'\n",fileListing(files))
def showText(title, text):
from PySide import QtGui
QtGui.QMessageBox.information(None, title, text)
# TODO: allow specifying export directory in UI
def macromain():
doc = FreeCAD.ActiveDocument
docdir = os.path.dirname(doc.FileName)
exportdir = os.path.join(docdir, 'export/')
files = exportDocument(doc, exportdir)
#showText('Project export', "Exported %d files to %s\n%s" % ( len(files), exportdir, fileListing(files)))
print(files)
if __name__ == '__main__':
if FreeCAD.GuiUp:
macromain()
else:
sys.exit(scriptmain())