TemplatePyMod/Commands.py

Go to the documentation of this file.
00001 # FreeCAD TemplatePyMod module  
00002 # (c) 2007 Juergen Riegel LGPL
00003 
00004 #
00005 
00006 
00007 # import FreeCAD modules
00008 import FreeCAD, FreeCADGui,inspect
00009 
00010 # helper -------------------------------------------------------------------
00011 
00012 def addCommand(name,cmdObject):
00013         (list,num) = inspect.getsourcelines(cmdObject.Activated)
00014         pos = 0
00015         # check for indentation
00016         while(list[1][pos] == ' ' or list[1][pos] == '\t'):
00017                 pos += 1
00018         source = ""
00019         for i in range(len(list)-1):
00020                 source += list[i+1][pos:]
00021         FreeCADGui.addCommand(name,cmdObject,source)
00022         
00023 
00024 #---------------------------------------------------------------------------
00025 # The command classes
00026 #---------------------------------------------------------------------------
00027 
00028 class TemplatePyMod_Cmd1:
00029     "Example command class"
00030     def Activated(self):
00031         print "TemplatePyMod_Cmd1 activated ;-) "
00032 
00033     def GetResources(self):
00034         return {'Pixmap'  : 'Std_Tool1', 'MenuText': 'Example command', 'ToolTip': 'Very unimportand example command'}
00035 
00036 
00037 class TemplatePyMod_Cmd2:
00038     "Example command class"
00039     def Activated(self):
00040         d = FreeCAD.ActiveDocument
00041         v = FreeCADGui.ActiveDocument.ActiveView
00042         class PolygonCreator:
00043             def __init__(self, doc, view, max):
00044                 self.doc = doc
00045                 self.view = view
00046                 self.call = view.addEventCallback("SoMouseButtonEvent",self.addVertex)
00047                 self.max = max
00048                 self.node=[]
00049                 self.count=0
00050                 self.poly=None
00051 
00052             def addVertex(self, d):
00053                 if (d["State"] == "DOWN"):
00054                     pos = d["Position"]
00055                     self.node.append(self.view.getPoint(pos[0],pos[1]))
00056                     self.count = self.count+1
00057                     if (self.count == 1):
00058                         import Part,PartGui
00059                         self.poly=self.doc.addObject("Part::Polygon","Polygon")
00060                         self.poly.Nodes = self.node
00061                         self.poly.Close=True
00062                     else:
00063                         self.poly.Nodes = self.node
00064                         self.doc.recompute()
00065                     if (self.count == self.max):
00066                         self.node=[]
00067                         self.view.removeEventCallback("SoMouseButtonEvent",self.call)
00068 
00069         p=PolygonCreator(d,v,10)
00070 
00071     def IsActive(self):
00072         if FreeCAD.ActiveDocument == None:
00073             return False
00074         else:
00075             return True
00076 
00077     def GetResources(self):
00078         return {'Pixmap'  : 'Std_Tool2', 'MenuText': 'Create polygon...', 'ToolTip': 'Create a polygon by clicking inside the viewer'}
00079 
00080 
00081 class TemplatePyMod_Cmd3:
00082     "Import PyQt4"
00083     def Activated(self):
00084         import PythonQt
00085         from PyQt4 import QtGui
00086         mw=QtGui.qApp.activeWindow()
00087         QtGui.QMessageBox.information(mw,"PyQt","""PyQt was loaded successfully.\n
00088         Load the PyQt sandbox now...""")
00089         FreeCADGui.activateWorkbench("PythonQtWorkbench")
00090 
00091     def GetResources(self):
00092         return {'Pixmap'  : 'python', 'MenuText': 'Import PyQt4', 'ToolTip': 'Add a workbench for PyQt4 samples'}
00093 
00094 class SphereCreator:
00095         def __init__(self):
00096                 import Part
00097                 self.pt = Part
00098                 self.mode = False
00099                 FreeCAD.Console.PrintMessage("Create instance of SphereCreator\n")
00100 
00101         def __del__(self):
00102                 FreeCAD.Console.PrintMessage("Delete instance of SphereCreator\n")
00103 
00104         def enter(self):
00105                 if (self.mode):
00106                         return
00107                 FreeCAD.Console.PrintMessage("Enter sphere creation mode\n")
00108                 self.av = FreeCADGui.ActiveDocument.ActiveView
00109                 self.cb = self.av.addEventCallback("SoMouseButtonEvent",self.create)
00110                 self.ex = self.av.addEventCallback("SoKeyboardEvent",self.exit)
00111                 self.mode = True
00112 
00113         def leave(self):
00114                 if (not self.mode):
00115                         return
00116                 FreeCAD.Console.PrintMessage("Leave sphere creation mode\n")
00117                 self.av.removeEventCallback("SoMouseButtonEvent",self.cb)
00118                 self.av.removeEventCallback("SoKeyboardEvent",self.ex)
00119                 self.mode = False
00120 
00121         def create(self, info):
00122                 down = (info["State"] == "DOWN")
00123                 pos = info["Position"]
00124                 if (down):
00125                         pnt = self.av.getPoint(pos[0],pos[1])
00126                         FreeCAD.Console.PrintMessage("Clicked on position: ("+str(pos[0])+", "+str(pos[0])+")")
00127                         msg = " -> (%f,%f,%f)\n" % (pnt.x, pnt.y, pnt.z)
00128                         FreeCAD.Console.PrintMessage(msg)
00129                         sph=self.pt.makeSphere(1.0, pnt)
00130                         self.pt.show(sph)
00131 
00132         def exit(self, info):
00133                 esc = (info["Key"] == "ESCAPE")
00134                 if (esc):
00135                         self.leave()
00136 
00137 
00138 class TemplatePyMod_Cmd4:
00139         def __init__(self):
00140                 self.sc = SphereCreator()
00141 
00142         def __del__(self):
00143                 FreeCAD.Console.PrintError('TemplatePyMod_Cmd4 was destroyed\n')
00144 
00145         def Activated(self):
00146                 if FreeCADGui.ActiveDocument != None:
00147                         self.sc.enter()
00148                 else:
00149                         FreeCAD.Console.PrintWarning('A 3d view must be created\n')
00150 
00151         def GetResources(self):
00152                 return {'Pixmap'  : 'python', 'MenuText': 'Create spheres...', 'ToolTip': 'Click on the screen to create a sphere'}
00153 
00154 
00155 myRenderArea = None
00156 class TemplatePyMod_Cmd5:
00157         "Example command class"
00158         def Activated(self):
00159                 from pivy.sogui import *
00160                 from pivy.coin import *
00161 
00162                 global myRenderArea
00163                 if myRenderArea == None:
00164                         root = SoSeparator()
00165                         myCamera = SoPerspectiveCamera()
00166                         myMaterial = SoMaterial()
00167                         root.addChild(myCamera)
00168                         root.addChild(SoDirectionalLight())
00169                         #myMaterial.diffuseColor = (1.0, 0.0, 0.0)   # Red
00170                         root.addChild(myMaterial)
00171                         root.addChild(SoCone())
00172 
00173                         # Create a renderArea in which to see our scene graph.
00174                         # The render area will appear within the main window.
00175                         myRenderArea = SoGuiRenderArea()
00176 
00177                         # Make myCamera see everything.
00178                         myCamera.viewAll(root, myRenderArea.getViewportRegion())
00179 
00180                         # Put our scene in myRenderArea, change the title
00181                         myRenderArea.setSceneGraph(root)
00182                         myRenderArea.setTitle("Hello Cone")
00183                 myRenderArea.show()
00184 
00185         def GetResources(self):
00186                 return {'Pixmap'  : 'Std_Tool1', 'MenuText': 'Render area', 'ToolTip': 'Show render area'}
00187 
00188 
00189 class TemplatePyMod_Cmd6:
00190         def Activated(self):
00191                 import FeaturePython
00192                 FeaturePython.makeBox()
00193 
00194         def GetResources(self):
00195                 return {'Pixmap'  : 'python', 'MenuText': 'Create a box', 'ToolTip': 'Use Box feature class which is completely written in Python'}
00196 
00197 #---------------------------------------------------------------------------
00198 # Adds the commands to the FreeCAD command manager
00199 #---------------------------------------------------------------------------
00200 addCommand('TemplatePyMod_Cmd1',TemplatePyMod_Cmd1())
00201 addCommand('TemplatePyMod_Cmd2',TemplatePyMod_Cmd2())
00202 addCommand('TemplatePyMod_Cmd3',TemplatePyMod_Cmd3())
00203 FreeCADGui.addCommand('TemplatePyMod_Cmd4',TemplatePyMod_Cmd4())
00204 FreeCADGui.addCommand('TemplatePyMod_Cmd5',TemplatePyMod_Cmd5())
00205 FreeCADGui.addCommand('TemplatePyMod_Cmd6',TemplatePyMod_Cmd6())
00206 

Generated on Wed Nov 23 19:00:03 2011 for FreeCAD by  doxygen 1.6.1