FreeCAD

Форк
0
258 строк · 8.6 Кб
1
# FreeCAD TemplatePyMod module  
2
# (c) 2007 Juergen Riegel LGPL
3

4
#
5

6

7
# import FreeCAD modules
8
import FreeCAD, FreeCADGui,inspect
9

10
# helper -------------------------------------------------------------------
11

12
def addCommand(name,cmdObject):
13
	(list,num) = inspect.getsourcelines(cmdObject.Activated)
14
	pos = 0
15
	# check for indentation
16
	while(list[1][pos] == ' ' or list[1][pos] == '\t'):
17
		pos += 1
18
	source = ""
19
	for i in range(len(list)-1):
20
		source += list[i+1][pos:]
21
	FreeCADGui.addCommand(name,cmdObject,source)
22
	
23

24
#---------------------------------------------------------------------------
25
# The command classes
26
#---------------------------------------------------------------------------
27

28
class TemplatePyMod_Cmd1:
29
    "Example command class"
30
    def Activated(self):
31
        print("TemplatePyMod_Cmd1 activated ;-) ")
32

33
    def GetResources(self):
34
        return {'Pixmap'  : 'Std_Tool1', 'MenuText': 'Example command', 'ToolTip': 'Very unimportand example command'}
35

36

37
class TemplatePyMod_Cmd2:
38
    "Example command class"
39
    def Activated(self):
40
        d = FreeCAD.ActiveDocument
41
        v = FreeCADGui.ActiveDocument.ActiveView
42
        class PolygonCreator:
43
            def __init__(self, doc, view, max):
44
                self.doc = doc
45
                self.view = view
46
                self.call = view.addEventCallback("SoMouseButtonEvent",self.addVertex)
47
                self.max = max
48
                self.node=[]
49
                self.count=0
50
                self.poly=None
51

52
            def addVertex(self, d):
53
                if (d["State"] == "DOWN"):
54
                    pos = d["Position"]
55
                    self.node.append(self.view.getPoint(pos[0],pos[1]))
56
                    self.count = self.count+1
57
                    if (self.count == 1):
58
                        import Part,PartGui
59
                        self.poly=self.doc.addObject("Part::Polygon","Polygon")
60
                        self.poly.Nodes = self.node
61
                        self.poly.Close=True
62
                    else:
63
                        self.poly.Nodes = self.node
64
                        self.doc.recompute()
65
                    if (self.count == self.max):
66
                        self.node=[]
67
                        self.view.removeEventCallback("SoMouseButtonEvent",self.call)
68

69
        self.polycreator = PolygonCreator(d,v,10)
70

71
    def IsActive(self):
72
        if FreeCAD.ActiveDocument is None:
73
            return False
74
        else:
75
            return True
76

77
    def GetResources(self):
78
        return {'Pixmap'  : 'Std_Tool2', 'MenuText': 'Create polygon...', 'ToolTip': 'Create a polygon by clicking inside the viewer'}
79

80

81
class TemplatePyMod_Cmd3:
82
    "Import PySide"
83
    def Activated(self):
84
        import PythonQt
85
        from PySide import QtGui
86
        mw=FreeCADGui.getMainWindow()
87
        QtGui.QMessageBox.information(mw,"PySide","""PySide was loaded successfully.""")
88
        FreeCADGui.activateWorkbench("PythonQtWorkbench")
89

90
    def GetResources(self):
91
        return {'Pixmap'  : 'python', 'MenuText': 'Import PySide', 'ToolTip': 'Add a workbench for PySide samples'}
92

93
class SphereCreator:
94
	def __init__(self):
95
		import Part
96
		self.pt = Part
97
		self.mode = False
98
		FreeCAD.Console.PrintMessage("Create instance of SphereCreator\n")
99

100
	def __del__(self):
101
		FreeCAD.Console.PrintMessage("Delete instance of SphereCreator\n")
102

103
	def enter(self):
104
		if (self.mode):
105
			return
106
		FreeCAD.Console.PrintMessage("Enter sphere creation mode\n")
107
		self.av = FreeCADGui.ActiveDocument.ActiveView
108
		self.cb = self.av.addEventCallback("SoMouseButtonEvent",self.create)
109
		self.ex = self.av.addEventCallback("SoKeyboardEvent",self.exit)
110
		self.mode = True
111

112
	def leave(self):
113
		if (not self.mode):
114
			return
115
		FreeCAD.Console.PrintMessage("Leave sphere creation mode\n")
116
		self.av.removeEventCallback("SoMouseButtonEvent",self.cb)
117
		self.av.removeEventCallback("SoKeyboardEvent",self.ex)
118
		self.mode = False
119

120
	def create(self, info):
121
		down = (info["State"] == "DOWN")
122
		pos = info["Position"]
123
		if (down):
124
			pnt = self.av.getPoint(pos[0],pos[1])
125
			FreeCAD.Console.PrintMessage("Clicked on position: ("+str(pos[0])+", "+str(pos[0])+")")
126
			msg = " -> (%f,%f,%f)\n" % (pnt.x, pnt.y, pnt.z)
127
			FreeCAD.Console.PrintMessage(msg)
128
			sph=self.pt.makeSphere(1.0, pnt)
129
			self.pt.show(sph)
130

131
	def exit(self, info):
132
		esc = (info["Key"] == "ESCAPE")
133
		if (esc):
134
			self.leave()
135

136

137
class TemplatePyMod_Cmd4:
138
	def __init__(self):
139
		self.sc = SphereCreator()
140

141
	def __del__(self):
142
		FreeCAD.Console.PrintError('TemplatePyMod_Cmd4 was destroyed\n')
143

144
	def Activated(self):
145
		if FreeCADGui.ActiveDocument is not None:
146
			self.sc.enter()
147
		else:
148
			FreeCAD.Console.PrintWarning('A 3d view must be created\n')
149

150
	def GetResources(self):
151
		return {'Pixmap'  : 'python', 'MenuText': 'Create spheres...', 'ToolTip': 'Click on the screen to create a sphere'}
152

153

154
myRenderArea = None
155
class TemplatePyMod_Cmd5:
156
	"Example command class"
157
	def Activated(self):
158
		from pivy import sogui
159
		from pivy import coin
160

161
		global myRenderArea
162
		if myRenderArea is None:
163
			root = coin.SoSeparator()
164
			myCamera = coin.SoPerspectiveCamera()
165
			myMaterial = coin.SoMaterial()
166
			root.addChild(myCamera)
167
			root.addChild(coin.SoDirectionalLight())
168
			#myMaterial.diffuseColor = (1.0, 0.0, 0.0)   # Red
169
			root.addChild(myMaterial)
170
			root.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.
174
			myRenderArea = sogui.SoGuiRenderArea(FreeCADGui.getMainWindow())
175

176
			# Make myCamera see everything.
177
			myCamera.viewAll(root, myRenderArea.getViewportRegion())
178

179
			# Put our scene in myRenderArea, change the title
180
			myRenderArea.setSceneGraph(root)
181
			myRenderArea.setTitle("Hello Cone")
182
		myRenderArea.show()
183

184
	def GetResources(self):
185
		return {'Pixmap'  : 'Std_Tool1', 'MenuText': 'Render area', 'ToolTip': 'Show render area'}
186

187

188
class TemplatePyMod_Cmd6:
189
	def Activated(self):
190
		import FeaturePython
191
		FeaturePython.makeBox()
192

193
	def GetResources(self):
194
		return {'Pixmap'  : 'python', 'MenuText': 'Create a box', 'ToolTip': 'Use Box feature class which is completely written in Python'}
195

196
class TemplatePyGrp_1:
197
    def Activated(self):
198
        import FreeCAD
199
        FreeCAD.Console.PrintMessage("TemplatePyGrp_1\n")
200

201
    def GetResources(self):
202
        return {'Pixmap'  : 'Part_JoinConnect', 'MenuText': 'TemplatePyGrp_1', 'ToolTip': 'Print a message'}
203

204
class TemplatePyGrp_2:
205
    def Activated(self):
206
        import FreeCAD
207
        FreeCAD.Console.PrintMessage("TemplatePyGrp_2\n")
208

209
    def GetResources(self):
210
        return {'Pixmap'  : 'Part_JoinEmbed', 'MenuText': 'TemplatePyGrp_2', 'ToolTip': 'Print a message'}
211

212
class TemplatePyGrp_3:
213
    def Activated(self):
214
        import FreeCAD
215
        FreeCAD.Console.PrintMessage("TemplatePyGrp_3\n")
216

217
    def GetResources(self):
218
        return {'Pixmap'  : 'Part_JoinCutout', 'MenuText': 'TemplatePyGrp_3', 'ToolTip': 'Print a message'}
219

220
class TemplatePyGroup:
221
    "Example group command class"
222
    #def Activated(self, index):
223
    #    print "TemplatePyGroup activated ;-) "
224

225
    def GetCommands(self):
226
        return ("TemplatePyGrp_1", "TemplatePyGrp_2", "TemplatePyGrp_3", "Std_New")
227

228
    def GetDefaultCommand(self):
229
        return 2
230

231
    def GetResources(self):
232
        return {'Pixmap'  : 'python', 'MenuText': 'Group command', 'ToolTip': 'Example group command'}
233

234
class TemplatePyCheckable:
235
    "Example toggle command class"
236
    def Activated(self, index):
237
        if index == 0:
238
            print("Toggle is off")
239
        else:
240
            print("Toggle is on")
241

242
    def GetResources(self):
243
        return {'Pixmap'  : 'python', 'MenuText': 'Toggle command', 'ToolTip': 'Example toggle command', 'Checkable': True}
244

245
#---------------------------------------------------------------------------
246
# Adds the commands to the FreeCAD command manager
247
#---------------------------------------------------------------------------
248
addCommand('TemplatePyMod_Cmd1',TemplatePyMod_Cmd1())
249
addCommand('TemplatePyMod_Cmd2',TemplatePyMod_Cmd2())
250
addCommand('TemplatePyMod_Cmd3',TemplatePyMod_Cmd3())
251
FreeCADGui.addCommand('TemplatePyMod_Cmd4',TemplatePyMod_Cmd4())
252
FreeCADGui.addCommand('TemplatePyMod_Cmd5',TemplatePyMod_Cmd5())
253
FreeCADGui.addCommand('TemplatePyMod_Cmd6',TemplatePyMod_Cmd6())
254
FreeCADGui.addCommand('TemplatePyGrp_1',TemplatePyGrp_1())
255
FreeCADGui.addCommand('TemplatePyGrp_2',TemplatePyGrp_2())
256
FreeCADGui.addCommand('TemplatePyGrp_3',TemplatePyGrp_3())
257
FreeCADGui.addCommand('TemplatePyGroup',TemplatePyGroup())
258
FreeCADGui.addCommand('TemplatePyCheckable',TemplatePyCheckable())
259

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.