-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathexportProject.FCMacro
105 lines (84 loc) · 2.88 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
# Copyright 2016, Jon Nordby <[email protected]>. Licensed LGPLv2+ & MIT
"""
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
partTypeRegex = re.compile(r'^(.*)\[([,\w]+)\].*$')
sys.path.insert(0, '/usr/lib/freecad/')
import FreeCAD
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)
# TODO: support CAM and post-processors like: rml, spb
supportedTypes = {
'stl': exportMesh,
'obj': exportMesh,
'step': exportStep,
'dxf': exportDxf,
}
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:
exporter = supportedTypes[t]
filename = "%s-%s.%s" % (docname, partname, t)
filepath = os.path.join(exportdir, 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 %d files to '%s'\n%s" % (len(files), exportdir, 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())