FreeCAD
258 строк · 8.6 Кб
1# FreeCAD TemplatePyMod module
2# (c) 2007 Juergen Riegel LGPL
3
4#
5
6
7# import FreeCAD modules
8import FreeCAD, FreeCADGui,inspect
9
10# helper -------------------------------------------------------------------
11
12def addCommand(name,cmdObject):
13(list,num) = inspect.getsourcelines(cmdObject.Activated)
14pos = 0
15# check for indentation
16while(list[1][pos] == ' ' or list[1][pos] == '\t'):
17pos += 1
18source = ""
19for i in range(len(list)-1):
20source += list[i+1][pos:]
21FreeCADGui.addCommand(name,cmdObject,source)
22
23
24#---------------------------------------------------------------------------
25# The command classes
26#---------------------------------------------------------------------------
27
28class TemplatePyMod_Cmd1:
29"Example command class"
30def Activated(self):
31print("TemplatePyMod_Cmd1 activated ;-) ")
32
33def GetResources(self):
34return {'Pixmap' : 'Std_Tool1', 'MenuText': 'Example command', 'ToolTip': 'Very unimportand example command'}
35
36
37class TemplatePyMod_Cmd2:
38"Example command class"
39def Activated(self):
40d = FreeCAD.ActiveDocument
41v = FreeCADGui.ActiveDocument.ActiveView
42class PolygonCreator:
43def __init__(self, doc, view, max):
44self.doc = doc
45self.view = view
46self.call = view.addEventCallback("SoMouseButtonEvent",self.addVertex)
47self.max = max
48self.node=[]
49self.count=0
50self.poly=None
51
52def addVertex(self, d):
53if (d["State"] == "DOWN"):
54pos = d["Position"]
55self.node.append(self.view.getPoint(pos[0],pos[1]))
56self.count = self.count+1
57if (self.count == 1):
58import Part,PartGui
59self.poly=self.doc.addObject("Part::Polygon","Polygon")
60self.poly.Nodes = self.node
61self.poly.Close=True
62else:
63self.poly.Nodes = self.node
64self.doc.recompute()
65if (self.count == self.max):
66self.node=[]
67self.view.removeEventCallback("SoMouseButtonEvent",self.call)
68
69self.polycreator = PolygonCreator(d,v,10)
70
71def IsActive(self):
72if FreeCAD.ActiveDocument is None:
73return False
74else:
75return True
76
77def GetResources(self):
78return {'Pixmap' : 'Std_Tool2', 'MenuText': 'Create polygon...', 'ToolTip': 'Create a polygon by clicking inside the viewer'}
79
80
81class TemplatePyMod_Cmd3:
82"Import PySide"
83def Activated(self):
84import PythonQt
85from PySide import QtGui
86mw=FreeCADGui.getMainWindow()
87QtGui.QMessageBox.information(mw,"PySide","""PySide was loaded successfully.""")
88FreeCADGui.activateWorkbench("PythonQtWorkbench")
89
90def GetResources(self):
91return {'Pixmap' : 'python', 'MenuText': 'Import PySide', 'ToolTip': 'Add a workbench for PySide samples'}
92
93class SphereCreator:
94def __init__(self):
95import Part
96self.pt = Part
97self.mode = False
98FreeCAD.Console.PrintMessage("Create instance of SphereCreator\n")
99
100def __del__(self):
101FreeCAD.Console.PrintMessage("Delete instance of SphereCreator\n")
102
103def enter(self):
104if (self.mode):
105return
106FreeCAD.Console.PrintMessage("Enter sphere creation mode\n")
107self.av = FreeCADGui.ActiveDocument.ActiveView
108self.cb = self.av.addEventCallback("SoMouseButtonEvent",self.create)
109self.ex = self.av.addEventCallback("SoKeyboardEvent",self.exit)
110self.mode = True
111
112def leave(self):
113if (not self.mode):
114return
115FreeCAD.Console.PrintMessage("Leave sphere creation mode\n")
116self.av.removeEventCallback("SoMouseButtonEvent",self.cb)
117self.av.removeEventCallback("SoKeyboardEvent",self.ex)
118self.mode = False
119
120def create(self, info):
121down = (info["State"] == "DOWN")
122pos = info["Position"]
123if (down):
124pnt = self.av.getPoint(pos[0],pos[1])
125FreeCAD.Console.PrintMessage("Clicked on position: ("+str(pos[0])+", "+str(pos[0])+")")
126msg = " -> (%f,%f,%f)\n" % (pnt.x, pnt.y, pnt.z)
127FreeCAD.Console.PrintMessage(msg)
128sph=self.pt.makeSphere(1.0, pnt)
129self.pt.show(sph)
130
131def exit(self, info):
132esc = (info["Key"] == "ESCAPE")
133if (esc):
134self.leave()
135
136
137class TemplatePyMod_Cmd4:
138def __init__(self):
139self.sc = SphereCreator()
140
141def __del__(self):
142FreeCAD.Console.PrintError('TemplatePyMod_Cmd4 was destroyed\n')
143
144def Activated(self):
145if FreeCADGui.ActiveDocument is not None:
146self.sc.enter()
147else:
148FreeCAD.Console.PrintWarning('A 3d view must be created\n')
149
150def GetResources(self):
151return {'Pixmap' : 'python', 'MenuText': 'Create spheres...', 'ToolTip': 'Click on the screen to create a sphere'}
152
153
154myRenderArea = None
155class TemplatePyMod_Cmd5:
156"Example command class"
157def Activated(self):
158from pivy import sogui
159from pivy import coin
160
161global myRenderArea
162if myRenderArea is None:
163root = coin.SoSeparator()
164myCamera = coin.SoPerspectiveCamera()
165myMaterial = coin.SoMaterial()
166root.addChild(myCamera)
167root.addChild(coin.SoDirectionalLight())
168#myMaterial.diffuseColor = (1.0, 0.0, 0.0) # Red
169root.addChild(myMaterial)
170root.addChild(coin.SoCone())
171
172# Create a renderArea in which to see our scene graph.
173# The render area will appear within the main window.
174myRenderArea = sogui.SoGuiRenderArea(FreeCADGui.getMainWindow())
175
176# Make myCamera see everything.
177myCamera.viewAll(root, myRenderArea.getViewportRegion())
178
179# Put our scene in myRenderArea, change the title
180myRenderArea.setSceneGraph(root)
181myRenderArea.setTitle("Hello Cone")
182myRenderArea.show()
183
184def GetResources(self):
185return {'Pixmap' : 'Std_Tool1', 'MenuText': 'Render area', 'ToolTip': 'Show render area'}
186
187
188class TemplatePyMod_Cmd6:
189def Activated(self):
190import FeaturePython
191FeaturePython.makeBox()
192
193def GetResources(self):
194return {'Pixmap' : 'python', 'MenuText': 'Create a box', 'ToolTip': 'Use Box feature class which is completely written in Python'}
195
196class TemplatePyGrp_1:
197def Activated(self):
198import FreeCAD
199FreeCAD.Console.PrintMessage("TemplatePyGrp_1\n")
200
201def GetResources(self):
202return {'Pixmap' : 'Part_JoinConnect', 'MenuText': 'TemplatePyGrp_1', 'ToolTip': 'Print a message'}
203
204class TemplatePyGrp_2:
205def Activated(self):
206import FreeCAD
207FreeCAD.Console.PrintMessage("TemplatePyGrp_2\n")
208
209def GetResources(self):
210return {'Pixmap' : 'Part_JoinEmbed', 'MenuText': 'TemplatePyGrp_2', 'ToolTip': 'Print a message'}
211
212class TemplatePyGrp_3:
213def Activated(self):
214import FreeCAD
215FreeCAD.Console.PrintMessage("TemplatePyGrp_3\n")
216
217def GetResources(self):
218return {'Pixmap' : 'Part_JoinCutout', 'MenuText': 'TemplatePyGrp_3', 'ToolTip': 'Print a message'}
219
220class TemplatePyGroup:
221"Example group command class"
222#def Activated(self, index):
223# print "TemplatePyGroup activated ;-) "
224
225def GetCommands(self):
226return ("TemplatePyGrp_1", "TemplatePyGrp_2", "TemplatePyGrp_3", "Std_New")
227
228def GetDefaultCommand(self):
229return 2
230
231def GetResources(self):
232return {'Pixmap' : 'python', 'MenuText': 'Group command', 'ToolTip': 'Example group command'}
233
234class TemplatePyCheckable:
235"Example toggle command class"
236def Activated(self, index):
237if index == 0:
238print("Toggle is off")
239else:
240print("Toggle is on")
241
242def GetResources(self):
243return {'Pixmap' : 'python', 'MenuText': 'Toggle command', 'ToolTip': 'Example toggle command', 'Checkable': True}
244
245#---------------------------------------------------------------------------
246# Adds the commands to the FreeCAD command manager
247#---------------------------------------------------------------------------
248addCommand('TemplatePyMod_Cmd1',TemplatePyMod_Cmd1())
249addCommand('TemplatePyMod_Cmd2',TemplatePyMod_Cmd2())
250addCommand('TemplatePyMod_Cmd3',TemplatePyMod_Cmd3())
251FreeCADGui.addCommand('TemplatePyMod_Cmd4',TemplatePyMod_Cmd4())
252FreeCADGui.addCommand('TemplatePyMod_Cmd5',TemplatePyMod_Cmd5())
253FreeCADGui.addCommand('TemplatePyMod_Cmd6',TemplatePyMod_Cmd6())
254FreeCADGui.addCommand('TemplatePyGrp_1',TemplatePyGrp_1())
255FreeCADGui.addCommand('TemplatePyGrp_2',TemplatePyGrp_2())
256FreeCADGui.addCommand('TemplatePyGrp_3',TemplatePyGrp_3())
257FreeCADGui.addCommand('TemplatePyGroup',TemplatePyGroup())
258FreeCADGui.addCommand('TemplatePyCheckable',TemplatePyCheckable())
259