FreeCAD
98 строк · 2.5 Кб
1# FreeCAD TemplatePyMod module
2# (c) 2010 Werner Mayer LGPL
3
4"""
5The module can be executed with:
6FreeCAD -P <path_to_file> Automation.py
7FreeCADCmd -P <path_to_file> Automation.py
8"""
9
10import FreeCAD, Part
11import os
12import tempfile
13
14def makeSnapshotWithGui():
15from PySide import QtGui
16import FreeCADGui
17
18def getMainWindow():
19toplevel = QtGui.QApplication.topLevelWidgets()
20for i in toplevel:
21if i.metaObject().className() == "Gui::MainWindow":
22return i
23raise RuntimeError("No main window found")
24
25mw=getMainWindow()
26mw.hide()
27#mw.showMinimized()
28
29# Create a test geometry and add it to the document
30obj=Part.makeCone(10,8,10)
31doc = FreeCAD.newDocument()
32Part.show(obj)
33
34# switch off animation so that the camera is moved to the final position immediately
35view = FreeCADGui.getDocument(doc.Name).activeView()
36view.setAnimationEnabled(False)
37view.viewAxometric()
38view.fitAll()
39view.saveImage('crystal.png',800,600,'Current')
40FreeCAD.closeDocument(doc.Name)
41# close the application
42QtGui.QApplication.quit()
43
44def makeSnapshotWithoutGui():
45from pivy import coin
46
47# create a test geometry and create an IV representation as string
48box=Part.makeCone(10,8,10)
49iv=box.writeInventor()
50
51# load it into a buffer
52inp=coin.SoInput()
53try:
54inp.setBuffer(iv)
55except Exception:
56tempPath = tempfile.gettempdir()
57fileName = tempPath + os.sep + "cone.iv"
58file = open(fileName, "w")
59file.write(iv)
60file.close()
61inp.openFile(fileName)
62
63# and create a scenegraph
64data = coin.SoDB.readAll(inp)
65base = coin.SoBaseColor()
66base.rgb.setValue(0.6,0.7,1.0)
67data.insertChild(base,0)
68
69# add light and camera so that the rendered geometry is visible
70root = coin.SoSeparator()
71light = coin.SoDirectionalLight()
72cam = coin.SoOrthographicCamera()
73root.addChild(cam)
74root.addChild(light)
75root.addChild(data)
76
77# do the rendering now
78axo = coin.SbRotation(-0.353553, -0.146447, -0.353553, -0.853553)
79viewport=coin.SbViewportRegion(400,400)
80cam.orientation.setValue(axo)
81cam.viewAll(root,viewport)
82off=coin.SoOffscreenRenderer(viewport)
83root.ref()
84off.render(root)
85root.unref()
86
87# export the image, PS is always available
88off.writeToPostScript("crystal.ps")
89
90# Other formats are only available if simage package is installed
91if off.isWriteSupported("PNG"):
92print("Save as PNG")
93off.writeToFile("crystal.png","PNG")
94
95if FreeCAD.GuiUp:
96makeSnapshotWithGui()
97else:
98makeSnapshotWithoutGui()
99