Automation.py

Go to the documentation of this file.
00001 # FreeCAD TemplatePyMod module  
00002 # (c) 2010 Werner Mayer LGPL
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         #mw.showMinimized()
00026 
00027         # Create a test geometry and add it to the document
00028         obj=Part.makeCone(10,8,10)
00029         doc = FreeCAD.newDocument()
00030         Part.show(obj)
00031 
00032         # switch off animation so that the camera is moved to the final position immediately
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         # close the application
00040         QtGui.qApp.quit()
00041 
00042 def makeSnapshotWithoutGui():
00043         from pivy import coin
00044 
00045         # create a test geometry and create an IV representation as string
00046         box=Part.makeCone(10,8,10)
00047         iv=box.writeInventor()
00048 
00049         # load it into a buffer
00050         inp=coin.SoInput()
00051         inp.setBuffer(iv)
00052 
00053         # and create a scenegraph
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         # add light and camera so that the rendered geometry is visible
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         # do the rendering now
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         # export the image, PS is always available
00078         off.writeToPostScript("crystal.ps")
00079 
00080         # Other formats are only available if simage package is installed
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()

Generated on Wed Nov 23 18:59:57 2011 for FreeCAD by  doxygen 1.6.1