-
Notifications
You must be signed in to change notification settings - Fork 75
/
InitGui.py
384 lines (337 loc) · 14.6 KB
/
InitGui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# -*- coding: utf-8 -*-
###################################################################################
#
# InitGui.py
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
###################################################################################
import os
import Asm4_locator
global Asm4_icon, Asm4_path, Asm4_trans
Asm4_path = os.path.dirname( Asm4_locator.__file__ )
Asm4_icon = os.path.join( Asm4_path , 'Resources/icons/Assembly4.svg' )
Asm4_trans = os.path.join(Asm4_path, "Resources/translations")
# I don't like this being here
import selectionFilter
"""
+-----------------------------------------------+
| Initialize the workbench |
+-----------------------------------------------+
"""
class Assembly4Workbench(Workbench):
global Asm4_icon
global selectionFilter
MenuText = "Assembly 4"
ToolTip = "Assembly 4 workbench"
Icon = Asm4_icon
def __init__(self):
"This function is executed when FreeCAD starts"
def Activated(self):
"This function is executed when the workbench is activated"
# make buttons of the selection toolbar checkable
from PySide import QtGui
mainwin = Gui.getMainWindow()
sf_tb = None
for tb in mainwin.findChildren(QtGui.QToolBar):
if tb.objectName()=='Selection Filter':
sf_tb = tb
# make all buttons except last one (clear selection filter) checkable
if sf_tb is not None:
for button in sf_tb.actions()[0:-1]:
button.setCheckable(True)
return
def Deactivated(self):
"This function is executed when the workbench is deactivated"
selectionFilter.observerDisable()
return
def GetClassName(self):
# this function is mandatory if this is a full python workbench
return "Gui::PythonWorkbench"
"""
+-----------------------------------------------+
| This is where all is defined |
+-----------------------------------------------+
"""
def Initialize(self):
# check for FreeCAD version
FCver = FreeCAD.Version()
# print("This is FreeCAD version "+FCver[0]+"."+FCver[1]+"."+FCver[2]+"-"+FCver[3])
if FCver[0]=='0' and FCver[1]=='22':
git = int(FCver[3][0:5])
if isinstance(git, int) and git>35594 :
FreeCAD.Console.PrintWarning("This version of FreeCAD ("+FCver[0]+"."+FCver[1]+"."+FCver[2]+"-"+str(git)+") ")
FreeCAD.Console.PrintWarning("is not backward compatible with FreeCAD v0.21 and earlier\n")
FreeCAD.Console.PrintWarning("It is rather suggested to use the stable FreeCAD v0.21 branch\n")
# Translations
# from Asm4_Translate import Qtranslate
FreeCADGui.addLanguagePath(Asm4_trans)
FreeCADGui.updateLocale()
# Assembly4 version info
# with file package.xml (FreeCAD ≥0.21)
packageFile = os.path.join( Asm4_path, 'package.xml' )
try:
metadata = FreeCAD.Metadata(packageFile)
Asm4_date = metadata.Date
Asm4_version = metadata.Version
# with file VERSION (FreeCAD ≤0.20)
except:
versionPath = os.path.join( Asm4_path, 'VERSION' )
versionFile = open(versionPath,"r")
# read second line
version = versionFile.readlines()[1]
versionFile.close()
# remove trailing newline
Asm4_version = version[:-1]
FreeCAD.Console.PrintMessage("Initializing Assembly4 workbench"+ ' ('+Asm4_version+') .')
FreeCADGui.updateGui()
# import all stuff
import newAssemblyCmd # created an App::Part container called 'Assembly'
self.dot()
import newDatumCmd # creates a new LCS in 'Model'
self.dot()
import newPartCmd # creates a new App::Part container called 'Model'
self.dot()
import infoPartCmd # edits part information for BoM
self.dot()
import insertLinkCmd # inserts an App::Link to a 'Model' in another file
self.dot()
import placeLinkCmd # places a linked part by snapping LCS (in the Part and in the Assembly)
self.dot()
import importDatumCmd # creates an LCS in assembly and attaches it to an LCS relative to an external file
self.dot()
import releaseAttachmentCmd# creates an LCS in assembly and attaches it to an LCS relative to an external file
self.dot()
import makeBinderCmd # creates an LCS in assembly and attaches it to an LCS relative to an external file
self.dot()
import VariablesLib # creates an LCS in assembly and attaches it to an LCS relative to an external file
self.dot()
import AnimationLib # creates an LCS in assembly and attaches it to an LCS relative to an external file
self.dot()
import updateAssemblyCmd # updates all parts and constraints in the assembly
self.dot()
import makeArrayCmd # creates a new array of App::Link
self.dot()
import variantLinkCmd # creates a variant link
self.dot()
import gotoDocumentCmd # opens the documentof the selected App::Link
self.dot()
import Asm4_Measure # Measure tool in the Task panel
self.dot()
import makeBomCmd # creates the parts list
self.dot()
import checkInterference # check interferences btween parts inside the Assembly
self.dot()
import exportFiles # creates a hierarchical tree listing of files in an assembly
self.dot()
import HelpCmd # shows a basic help window
self.dot()
import showHideLcsCmd # shows/hides all the LCSs
self.dot()
import configurationEngine # save/restore configuration
self.dot()
# Fasteners
if self.checkWorkbench('FastenersWorkbench'):
# a library to handle fasteners from the FastenersWorkbench
import FastenersLib
self.FastenersCmd = 'Asm4_Fasteners'
else:
# a dummy library if the FastenersWorkbench is not installed
import FastenersDummy
self.FastenersCmd = 'Asm4_insertScrew'
self.dot()
# Define Menus
# commands to appear in the Assembly4 menu 'Assembly'
self.appendMenu("&Assembly", self.assemblyMenuItems())
self.dot()
# put all constraints related commands in a separate menu
self.appendMenu("&Constraints", self.constraintsMenuItems())
self.dot()
# self.appendMenu("&Geometry",["Asm4_newPart"])
# additional entry in the Help menu
# self.appendMenu(Qtranslate("Workbench", "&Help"), ["Asm4_Help"])
self.appendMenu( "&Help", ["Asm4_Help"])
self.dot()
# Define Toolbars
# commands to appear in the Assembly4 toolbar
self.appendToolbar("Assembly", self.assemblyToolbarItems())
self.dot()
# build the selection toolbar
self.appendToolbar("Selection Filter", self.selectionToolbarItems())
self.dot()
# self.appendToolbar("Geometry",["Asm4_newPart"])
FreeCAD.Console.PrintMessage(" " + "done" + ".\n")
"""
+-----------------------------------------------+
| Initialisation finished |
+-----------------------------------------------+
"""
"""
+-----------------------------------------------+
| Assembly Menu & Toolbar |
+-----------------------------------------------+
"""
def assemblyMenuItems(self):
commandList = [ "Asm4_newAssembly",
"Asm4_newPart",
"Asm4_newBody",
"Asm4_newGroup",
"Asm4_newSketch",
'Asm4_createDatum',
self.FastenersCmd,
"Separator",
"Asm4_insertLink",
"Asm4_mirrorArray",
"Asm4_linearArray",
"Asm4_circularArray",
"Asm4_expressionArray",
"Asm4_variantLink",
"Separator",
"Asm4_cloneFastenersToAxes",
"Asm4_importDatum",
"Asm4_shapeBinder",
"Separator",
"Asm4_infoPart",
"Asm4_makeLocalBOM",
"Asm4_makeBOM",
"Asm4_listLinkedFiles",
"Asm4_checkInterference",
"Asm4_Measure",
'Asm4_showLcs',
'Asm4_hideLcs',
"Asm4_addVariable",
"Asm4_delVariable",
"Asm4_Animate",
"Asm4_openConfigurations"
]
return commandList
def constraintsMenuItems(self):
commandList = [ "Asm4_placeLink",
"Asm4_releaseAttachment",
"Separator",
"Asm4_updateAssembly",
"Separator",
]
return commandList
def assemblyToolbarItems(self):
commandList = [ "Asm4_newAssembly",
"Asm4_newPart",
"Asm4_newBody",
"Asm4_newGroup",
"Asm4_infoPart",
"Asm4_insertLink",
"Asm4_variantLink",
self.FastenersCmd,
"Separator",
"Asm4_newSketch",
'Asm4_createDatum',
"Asm4_importDatum",
"Asm4_shapeBinder",
"Separator",
"Asm4_placeLink",
"Asm4_releaseAttachment",
"Asm4_updateAssembly",
"Separator",
"Asm4_mirrorArray",
"Asm4_linearArray",
"Asm4_circularArray",
"Asm4_expressionArray",
"Asm4_variablesCmd",
"Separator",
"Asm4_Animate",
"Asm4_Measure",
"Asm4_makeBOM",
"Asm4_listLinkedFiles",
'Asm4_showLcs',
'Asm4_hideLcs',
"Asm4_checkInterference",
"Asm4_openConfigurations"
]
return commandList
"""
+-----------------------------------------------+
| Selection Toolbar |
+-----------------------------------------------+
"""
def selectionToolbarItems(self):
# commands to appear in the Selection toolbar
commandList = ["Asm4_SelectionFilterVertexCmd",
"Asm4_SelectionFilterEdgeCmd",
"Asm4_SelectionFilterFaceCmd",
"Asm4_selObserver3DViewCmd" ,
"Asm4_SelectionFilterClearCmd"]
return commandList
"""
+-----------------------------------------------+
| Contextual Menus |
+-----------------------------------------------+
"""
def ContextMenu(self, recipient):
# This is executed whenever the user right-clicks on screen"
# "recipient" will be either "view" or "tree"
contextMenu = ['Asm4_gotoDocument' ,
'Asm4_showLcs' ,
'Asm4_hideLcs' ]
# commands to appear in the 'Assembly' sub-menu in the contextual menu (right-click)
assemblySubMenu =[ "Asm4_insertLink" ,
"Asm4_placeLink" ,
"Asm4_importDatum" ,
'Asm4_FSparameters' ,
'Separator' ,
'Asm4_applyConfiguration']
# commands to appear in the 'Create' sub-menu in the contextual menu (right-click)
createSubMenu =["Asm4_newSketch",
"Asm4_newBody",
"Asm4_newLCS",
"Asm4_newAxis",
"Asm4_newPlane",
"Asm4_newPoint",
"Asm4_newHole",
"Asm4_insertScrew",
"Asm4_insertNut",
"Asm4_insertWasher",
'Separator',
'Asm4_newConfiguration']
self.appendContextMenu("", "Separator")
self.appendContextMenu("", contextMenu) # add commands to the context menu
self.appendContextMenu("Assembly", assemblySubMenu) # add commands to the context menu
self.appendContextMenu("Create", createSubMenu) # add commands to the context menu
self.appendContextMenu("", "Separator")
"""
+-----------------------------------------------+
| helper functions |
+-----------------------------------------------+
"""
def checkWorkbench( self, workbench ):
# checks whether the specified workbench (a 'string') is installed
listWB = Gui.listWorkbenches()
hasWB = False
for wb in listWB.keys():
if wb == workbench:
hasWB = True
return hasWB
# prints a dot in the consome window to show progress
def dot(self):
FreeCAD.Console.PrintMessage(".")
FreeCADGui.updateGui()
"""
+-----------------------------------------------+
| actually make the workbench |
+-----------------------------------------------+
"""
wb = Assembly4Workbench()
Gui.addWorkbench(wb)