forked from Zolko-123/FreeCAD_Assembly4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaceDatumCmd.py
474 lines (399 loc) · 18.1 KB
/
placeDatumCmd.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/env python3
# coding: utf-8
#
# placeDatumCmd.py
import os
from PySide import QtGui, QtCore
import FreeCADGui as Gui
import FreeCAD as App
import libAsm4 as Asm4
"""
+-----------------------------------------------+
| main class |
+-----------------------------------------------+
"""
class placeDatum( QtGui.QDialog ):
"My tool object"
def __init__(self):
super(placeDatum,self).__init__()
self.drawUI()
def GetResources(self):
return {"MenuText": "Edit Attachment of a Datum object",
"ToolTip": "Attach a Datum object in the assembly to a Datum in a linked Part",
"Pixmap" : os.path.join( Asm4.iconPath , 'Place_Datum.svg')
}
def IsActive(self):
# is there an active document ?
if App.ActiveDocument:
# is something selected ?
selObj = self.checkSelectionDatum()
if selObj != None:
return True
return False
def checkSelectionDatum(self):
selectedObj = None
# check that there is an App::Part called 'Model'
# a standard App::Part would also do, but then more error checks are necessary
if App.ActiveDocument.getObject('Model') and App.ActiveDocument.getObject('Model').TypeId=='App::Part' :
# check that something is selected
if Gui.Selection.getSelection():
# set the (first) selected object as global variable
selection = Gui.Selection.getSelection()[0]
selectedType = selection.TypeId
# check that the selected object is a Datum CS or Point type
if selectedType=='PartDesign::CoordinateSystem' or selectedType=='PartDesign::Plane' or selectedType=='PartDesign::Line' or selectedType=='PartDesign::Point' :
selectedObj = selection
# now we should be safe
return selectedObj
"""
+-----------------------------------------------+
| the real stuff |
+-----------------------------------------------+
"""
def Activated(self):
# get the current active document to avoid errors if user changes tab
self.activeDoc = App.activeDocument()
# the parent (top-level) assembly is the App::Part called Model (hard-coded)
self.parentAssembly = self.activeDoc.Model
# check that we have selected a PartDesign::CoordinateSystem
self.selectedDatum = []
selection = self.checkSelectionDatum()
if not selection:
self.close()
else:
self.selectedDatum = selection
# check if the datum object is already mapped to something
if self.selectedDatum.MapMode != 'Deactivated':
message = 'This datum object \"'+self.selectedDatum.Label+'\" is mapped to some geometry. Attaching-it with Assembly4 will loose this mapping.'
if Asm4.confirmBox(message):
# unset MappingMode
self.selectedDatum.MapMode = 'Deactivated'
else:
# don't do anything and ...
return
# Now we can draw the UI
self.initUI()
# now self.parentList and self.parentTable are available
# find all the linked parts in the assembly
for objName in self.parentAssembly.getSubObjects():
# remove the trailing .
obj = self.activeDoc.getObject(objName[0:-1])
if obj.TypeId=='App::Link':
# add it to our list if it's a link to an App::Part ...
if hasattr(obj.LinkedObject,'isDerivedFrom') and obj.LinkedObject.isDerivedFrom('App::Part'):
self.parentTable.append( obj )
# add to the drop-down combo box with the assembly tree's parts
objIcon = obj.LinkedObject.ViewObject.Icon
objText = Asm4.nameLabel(obj)
self.parentList.addItem( objIcon, objText, obj)
# get and store the Placement's current ExpressionEngine:
self.old_EE = Asm4.placementEE(self.selectedDatum.ExpressionEngine)
#self.expression.setText(self.old_EE)
# decode the old ExpressionEngine
# if the decode is unsuccessful, old_Expression is set to False
# and old_attPart and old_attLCS are set to 'None'
old_Parent = ''
old_ParentPart = ''
old_attLCS = ''
( old_Parent, old_ParentPart, old_attLCS ) = Asm4.splitExpressionDatum( self.old_EE )
self.expression.setText( self.old_EE + ' => old_Parent = '+ old_Parent )
# find the oldPart in the current part list...
"""
oldPart = self.parentList.findText( old_Parent )
# if not found
if oldPart == -1:
# set to initial "Select linked Part" entry
self.parentList.setCurrentIndex( 0 )
else:
self.parentList.setCurrentIndex( oldPart )
"""
parent_found = False
parent_index = 1
for item in self.parentTable[1:]:
if item.Name == old_Parent:
parent_found = True
break
else:
parent_index += 1
if not parent_found:
parent_index = 0
self.parentList.setCurrentIndex( parent_index )
# this should have triggered self.getPartLCS() to fill the LCS list
# find the old attachment Datum in the list of the Datums in the linked part...
# lcs_found = []
lcs_found = self.attLCSlist.findItems( old_attLCS, QtCore.Qt.MatchExactly )
if not lcs_found:
lcs_found = self.attLCSlist.findItems( old_attLCS+' (', QtCore.Qt.MatchContains )
if lcs_found:
# ... and select it
self.attLCSlist.setCurrentItem( lcs_found[0] )
"""
+-----------------------------------------------+
| check that all necessary things are selected, |
| populate the expression with the selected |
| elements, put them into the constraint |
| and trigger the recomputation of the part |
+-----------------------------------------------+
"""
def onApply(self):
# get the name of the part to attach to:
# it's either the top level part name ('Model')
# or the provided link's name.
if self.parentList.currentIndex() > 0:
parent = self.parentTable[ self.parentList.currentIndex() ]
a_Link = parent.Name
a_Part = parent.LinkedObject.Document.Name
else:
a_Link = None
a_Part = None
# the attachment LCS's name in the parent
# check that something is selected in the QlistWidget
if self.attLCSlist.selectedItems():
a_LCS = self.attLCStable[ self.attLCSlist.currentRow() ].Name
else:
a_LCS = None
# check that all of them have something in
# constrName has been checked at the beginning
if not a_Part :
self.expression.setText( 'Problem in selections (no a_Part)' )
elif not a_LCS :
self.expression.setText( 'Problem in selections (no a_LCS)' )
else:
# don't forget the last '.' !!!
# <<LinkName>>.Placement.multiply( <<LinkName>>.<<LCS.>>.Placement )
# expr = '<<'+ a_Part +'>>.Placement.multiply( <<'+ a_Part +'>>.<<'+ a_LCS +'.>>.Placement )'
expr = Asm4.makeExpressionDatum( a_Link, a_Part, a_LCS )
# this can be skipped when this method becomes stable
self.expression.setText( expr )
# load the built expression into the Expression field of the constraint
self.activeDoc.getObject( self.selectedDatum.Name ).setExpression( 'Placement', expr )
# recompute the object to apply the placement:
self.selectedDatum.ViewObject.ShowLabel = True
self.selectedDatum.ViewObject.FontSize = 20
self.selectedDatum.recompute()
# highlight the selected LCS in its new position
Gui.Selection.clearSelection()
Gui.Selection.addSelection( self.activeDoc.Name, 'Model', self.selectedDatum.Name +'.')
return
"""
+-----------------------------------------------+
| get all the LCS in a part |
+-----------------------------------------------+
"""
def getPartLCS( self, part ):
partLCS = [ ]
# parse all objects in the part (they return strings)
for objName in part.getSubObjects():
# get the proper objects
# all object names end with a "." , this needs to be removed
obj = part.getObject( objName[0:-1] )
if obj.TypeId == 'PartDesign::CoordinateSystem' or obj.TypeId == 'PartDesign::Point':
partLCS.append( obj )
return partLCS
"""
+------------------------------------------------+
| fill the LCS list when changing the parent |
+------------------------------------------------+
"""
def onParentSelected(self):
# clear the LCS list
self.attLCSlist.clear()
self.attLCStable = []
# clear the selection in the GUI window
Gui.Selection.clearSelection()
# the current selection in the combo-box list gives the index
# of the currently selected link, whose name is in the table
#parentName = self.parentList.currentText()
#parentLink = self.activeDoc.getObject( parentName )
# if something is selected
if self.parentList.currentIndex() > 0:
parentName = self.parentTable[ self.parentList.currentIndex() ].Name
parentLink = self.activeDoc.getObject( parentName )
if parentLink:
# we get the LCS from the linked part
self.attLCStable = self.getPartLCS( parentLink.LinkedObject )
# linked part & doc
dText = ''
if parentLink.LinkedObject.Document != self.activeDoc :
dText = parentLink.LinkedObject.Document.Name +'#'
# if the linked part has been renamed by the user, keep the label and add (.Name)
#pText = parentLink.LinkedObject.Label
pText = Asm4.nameLabel( parentLink.LinkedObject )
self.parentDoc.setText( dText + pText )
# highlight the selected part:
Gui.Selection.addSelection( parentLink.Document.Name, 'Model', parentLink.Name+'.' )
# something wrong
else:
return
# build the list
for lcs in self.attLCStable:
newItem = QtGui.QListWidgetItem()
# if the LCS has been renamed, we show both the label and the (name)
newItem.setText( Asm4.nameLabel(lcs) )
newItem.setIcon( lcs.ViewObject.Icon )
self.attLCSlist.addItem( newItem )
self.onApply()
return
"""
+-----------------------------------------------+
| A target Datum has been clicked in the list |
+-----------------------------------------------+
"""
def onDatumSelected( self ):
# clear the selection in the GUI window
Gui.Selection.clearSelection()
# check that something is selected
if self.attLCSlist.selectedItems():
# get the linked part where the selected LCS is
a_Link = self.parentTable[ self.parentList.currentIndex() ].Name
# LCS in the linked part
a_LCS = self.attLCStable[ self.attLCSlist.currentRow() ].Name
Gui.Selection.addSelection( self.activeDoc.Name, 'Model', a_Link+'.'+a_LCS+'.')
self.onApply()
return
"""
+-----------------------------------------------+
| Rotations |
+-----------------------------------------------+
"""
def rotAxis( self, addRotation ):
self.selectedDatum.AttachmentOffset = addRotation.multiply( self.selectedDatum.AttachmentOffset )
self.selectedDatum.recompute()
def onRotX(self):
self.rotAxis(Asm4.rotX)
def onRotY(self):
self.rotAxis(Asm4.rotY)
def onRotZ(self):
self.rotAxis(Asm4.rotZ)
"""
+-----------------------------------------------+
| Cancel |
| restores the previous values |
+-----------------------------------------------+
"""
def onCancel(self):
# restore previous expression if it existed
if self.old_EE:
self.selectedDatum.setExpression('Placement', self.old_EE )
self.selectedDatum.ViewObject.ShowLabel = False
self.selectedDatum.recompute()
# highlight the selected LCS in its new position
Gui.Selection.clearSelection()
Gui.Selection.addSelection( self.activeDoc.Name, 'Model', self.selectedDatum.Name +'.')
self.close()
"""
+-----------------------------------------------+
| OK |
| accept and close |
+-----------------------------------------------+
"""
def onOK(self):
self.onApply()
self.selectedDatum.ViewObject.ShowLabel = False
self.close()
"""
+-----------------------------------------------+
| nitialises the UI once the widget has stared |
+-----------------------------------------------+
"""
def initUI(self):
self.lscName.setText( Asm4.nameLabel(self.selectedDatum) )
self.parentDoc.clear()
self.attLCSlist.clear()
self.expression.clear()
self.show()
# list and table containing the available parents in the assembly to choose from
self.parentList.clear()
self.parentList.addItem( 'Please choose' )
self.parentTable = []
self.parentTable.append( [] )
"""
+-----------------------------------------------+
| defines the UI, only static elements |
+-----------------------------------------------+
"""
def drawUI(self):
# Our main window will be a QDialog
self.setWindowTitle('Attach a Coordinate System')
self.setWindowIcon( QtGui.QIcon( os.path.join( Asm4.iconPath , 'FreeCad.svg' ) ) )
self.setWindowFlags( QtCore.Qt.WindowStaysOnTopHint )
self.setMinimumSize(370, 570)
self.setModal(False)
# the layout for the main window is vertical (top to down)
self.mainLayout = QtGui.QVBoxLayout(self)
# Selected Datum
self.formLayout = QtGui.QFormLayout(self)
self.lscName = QtGui.QLineEdit(self)
self.lscName.setReadOnly(True)
self.formLayout.addRow(QtGui.QLabel('Selected Datum :'),self.lscName)
self.mainLayout.addLayout(self.formLayout)
# combobox showing all available App::Link
self.mainLayout.addWidget(QtGui.QLabel('Selected Parent :'))
self.parentList = QtGui.QComboBox(self)
self.mainLayout.addWidget(self.parentList)
# the document containing the linked object
self.mainLayout.addWidget(QtGui.QLabel('Parent Part (Doc#Part) :'))
self.parentDoc = QtGui.QLineEdit(self)
self.parentDoc.setReadOnly(True)
self.mainLayout.addWidget(self.parentDoc)
# The list of all attachment LCS in the assembly is a QListWidget
# it is populated only when the parent combo-box is activated
self.mainLayout.addWidget(QtGui.QLabel('Select LCS in Parent :'))
self.attLCSlist = QtGui.QListWidget(self)
self.mainLayout.addWidget(self.attLCSlist)
# Expression
# Create a line that will contain full expression for the expression engine
self.mainLayout.addWidget(QtGui.QLabel('Expression Engine :'))
self.expression = QtGui.QLineEdit(self)
self.mainLayout.addWidget(self.expression)
# Rot Buttons
self.rotButtonsLayout = QtGui.QHBoxLayout(self)
# RotX button
self.RotXButton = QtGui.QPushButton('Rot X', self)
self.RotXButton.setToolTip("Rotate the Datum around the X axis by 90deg")
# RotY button
self.RotYButton = QtGui.QPushButton('Rot Y', self)
self.RotYButton.setToolTip("Rotate the Datum around the Y axis by 90deg")
# RotZ button
self.RotZButton = QtGui.QPushButton('Rot Z', self)
self.RotZButton.setToolTip("Rotate the Datum around the Z axis by 90deg")
# add the buttons
self.rotButtonsLayout.addStretch()
self.rotButtonsLayout.addWidget(self.RotXButton)
self.rotButtonsLayout.addWidget(self.RotYButton)
self.rotButtonsLayout.addWidget(self.RotZButton)
self.rotButtonsLayout.addStretch()
self.mainLayout.addLayout(self.rotButtonsLayout)
# OK Buttons
self.OkButtonsLayout = QtGui.QHBoxLayout(self)
#
# Cancel button
self.CancelButton = QtGui.QPushButton('Cancel', self)
self.CancelButton.setToolTip("Restore initial parameters and close dialog")
# OK button
self.OkButton = QtGui.QPushButton('OK', self)
self.OkButton.setToolTip("Apply current parameters and close dialog")
self.OkButton.setDefault(True)
# position the buttons
self.OkButtonsLayout.addWidget(self.CancelButton)
self.OkButtonsLayout.addStretch()
self.OkButtonsLayout.addWidget(self.OkButton)
self.mainLayout.addWidget(QtGui.QLabel(' '))
self.mainLayout.addLayout(self.OkButtonsLayout)
# Actions
self.parentList.currentIndexChanged.connect( self.onParentSelected )
#self.attLCSlist.currentItemChanged.connect( self.onDatumSelected )
self.attLCSlist.itemClicked.connect( self.onDatumSelected )
self.CancelButton.clicked.connect(self.onCancel)
self.OkButton.clicked.connect(self.onOK)
self.RotXButton.clicked.connect( self.onRotX )
self.RotYButton.clicked.connect( self.onRotY )
self.RotZButton.clicked.connect( self.onRotZ)
# apply the layout to the main window
self.setLayout(self.mainLayout)
"""
+-----------------------------------------------+
| add the command to the workbench |
+-----------------------------------------------+
"""
Gui.addCommand( 'Asm4_placeDatum', placeDatum() )