Automation.py
Go to the documentation of this file.00001
00002
00003
00004 """
00005 The module can be executed with:
00006 FreeCAD -P <path_to_file> Automation.py
00007 FreeCADCmd -P <path_to_file> Automation.py
00008 """
00009
00010 import FreeCAD, Part
00011
00012 def makeSnapshotWithGui():
00013 from PyQt4 import QtGui
00014 import FreeCADGui
00015
00016 def getMainWindow():
00017 toplevel = QtGui.qApp.topLevelWidgets()
00018 for i in toplevel:
00019 if i.metaObject().className() == "Gui::MainWindow":
00020 return i
00021 raise Exception("No main window found")
00022
00023 mw=getMainWindow()
00024 mw.hide()
00025
00026
00027
00028 obj=Part.makeCone(10,8,10)
00029 doc = FreeCAD.newDocument()
00030 Part.show(obj)
00031
00032
00033 view = FreeCADGui.getDocument(doc.Name).activeView()
00034 view.setAnimationEnabled(False)
00035 view.viewAxometric()
00036 view.fitAll()
00037 view.saveImage('crystal.png',800,600,'Current')
00038 FreeCAD.closeDocument(doc.Name)
00039
00040 QtGui.qApp.quit()
00041
00042 def makeSnapshotWithoutGui():
00043 from pivy import coin
00044
00045
00046 box=Part.makeCone(10,8,10)
00047 iv=box.writeInventor()
00048
00049
00050 inp=coin.SoInput()
00051 inp.setBuffer(iv)
00052
00053
00054 data = coin.SoDB.readAll(inp)
00055 base = coin.SoBaseColor()
00056 base.rgb.setValue(0.6,0.7,1.0)
00057 data.insertChild(base,0)
00058
00059
00060 root = coin.SoSeparator()
00061 light = coin.SoDirectionalLight()
00062 cam = coin.SoOrthographicCamera()
00063 root.addChild(cam)
00064 root.addChild(light)
00065 root.addChild(data)
00066
00067
00068 axo = coin.SbRotation(-0.353553, -0.146447, -0.353553, -0.853553)
00069 viewport=coin.SbViewportRegion(400,400)
00070 cam.orientation.setValue(axo)
00071 cam.viewAll(root,viewport)
00072 off=coin.SoOffscreenRenderer(viewport)
00073 root.ref()
00074 off.render(root)
00075 root.unref()
00076
00077
00078 off.writeToPostScript("crystal.ps")
00079
00080
00081 if off.isWriteSupported("PNG"):
00082 print "Save as PNG"
00083 off.writeToFile("crystal.png","PNG")
00084
00085 if FreeCAD.GuiUp:
00086 makeSnapshotWithGui()
00087 else:
00088 makeSnapshotWithoutGui()