forked from Zolko-123/FreeCAD_Assembly4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportDatumCmd.py
280 lines (234 loc) · 10.3 KB
/
importDatumCmd.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
#!/usr/bin/env python3
# coding: utf-8
#
# placeDatumCmd.py
from PySide import QtGui, QtCore
import FreeCADGui as Gui
import FreeCAD as App
import Part, math, re
from libAsm4 import *
"""
+-----------------------------------------------+
| main class |
+-----------------------------------------------+
"""
class importDatum( QtGui.QDialog ):
def __init__(self):
super(importDatum,self).__init__()
self.datumTypes = ['PartDesign::CoordinateSystem',
'PartDesign::Plane',
'PartDesign::Line',
'PartDesign::Point']
def GetResources(self):
return {"MenuText": "Import Datum object",
"ToolTip": "Imports the selected Datum object from a linked Part into the assembly.\nOnly datum objects at the root of the linked part can be imported",
"Pixmap" : os.path.join( iconPath , 'Import_Datum.svg')
}
def IsActive(self):
if App.ActiveDocument:
if self.getSelection():
return True
return False
def getSelection(self):
# check that there is an App::Part called 'Model'
if not App.ActiveDocument.getObject('Model'):
return None
# if something is selected ...
if len(Gui.Selection.getSelection())==1:
selectedObj = Gui.Selection.getSelection()[0]
# if it's a datum object we return it
if selectedObj.TypeId in self.datumTypes:
return selectedObj
return None
def labelName( self, obj ):
if obj.Name==obj.Label:
return(obj.Label)
else:
return(obj.Label+' ('+obj.Name+')')
"""
+-----------------------------------------------+
| the real stuff |
+-----------------------------------------------+
"""
def Activated(self):
# get the current active document to avoid errors if user changes tab
self.activeDoc = App.ActiveDocument
self.parentAssembly = self.activeDoc.Model
# initialize
self.datumTable = [ ]
self.selectedChild = None
# Now we can draw the UI
self.drawUI()
# We get all the App::Link parts in the assembly
self.childrenTable = []
# find all the child linked parts in the assembly
for objStr in self.parentAssembly.getSubObjects():
# the string ends with a . that must be removed
obj = self.activeDoc.getObject( objStr[0:-1] )
if obj.TypeId == 'App::Link' and obj.LinkedObject.isDerivedFrom('App::Part'):
# add it to our tree table if it's a link to an App::Part ...
self.childrenTable.append( obj )
# check whether a Datum is already selected:
self.targetDatum = self.getSelection()
# this returns the selection hierarchy in the form 'linkName.datumName.'
selectionTree = Gui.Selection.getSelectionEx("", 0)[0].SubElementNames[0]
(targetLinkName, sel, dot) = selectionTree.partition('.'+self.targetDatum.Name)
self.targetLink = self.activeDoc.getObject( targetLinkName )
# If the selected datum is at the root of the link. Else we don't consider it
if dot =='.' and self.targetLink in self.childrenTable:
self.datumList.setText( self.labelName(self.targetDatum) )
self.datumType.setText( self.targetDatum.TypeId )
self.linkName.setText( self.labelName(self.targetLink) )
docName = self.targetLink.LinkedObject.Document.Name+'#'
self.partName.setText( docName + self.labelName(self.targetLink.LinkedObject))
self.datumName.setText( self.targetDatum.Label )
else:
# something fishy, abort
msgBox = QtGui.QMessageBox()
msgBox.setWindowTitle('FreeCAD Warning')
msgBox.setIcon(QtGui.QMessageBox.Warning)
msgBox.setText("The selected datum object cannot be imported into this assembly")
msgBox.exec_()
# Cancel = 4194304
# Ok = 1024
return
# Now we can show the UI
self.show()
"""
+-----------------------------------------------+
| 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 where the datum to be copied is:
#linkedPartName = self.childrenList.currentText()
linkName = self.targetLink.Name
linkedPart = self.targetLink.LinkedObject.Name
linkedDoc = self.targetLink.LinkedObject.Document.Name
datum = self.targetDatum
# the name of the datum in the assembly, as per the dialog box
setDatumName = self.datumName.text()
# check that all of them have something in
if not (linkName and linkedPart and datum and setDatumName):
self.datumName.setText( 'Problem in selections' )
else:
# create the Datum
createdDatum = App.activeDocument().getObject('Model').newObject( datum.TypeId, setDatumName )
self.datumName.setText( '=> ' +createdDatum.Name )
# build the expression for the ExpressionEngine
# if the linked part is in the same docmument as the assembly
if self.activeDoc == self.targetLink.LinkedObject.Document:
expr = linkName +'.Placement * '+ datum.Name +'.Placement'
# if the linked part is in another document
else:
# it's the App.Document, not the App::Part that must be set before the #
# expr = linkName +'.Placement * '+ linkedPart +'#'+ datum.Name +'.Placement'
expr = linkName +'.Placement * '+ linkedDoc +'#'+ datum.Name +'.Placement'
# load the built expression into the Expression field of the datum created in the assembly
self.activeDoc.getObject( createdDatum.Name ).setExpression( 'Placement', expr )
# recompute the object to apply the placement:
createdDatum.recompute()
# recompute assembly
self.parentAssembly.recompute(True)
return
"""
+-----------------------------------------------+
| Cancel |
+-----------------------------------------------+
"""
def onCancel(self):
self.close()
"""
+-----------------------------------------------+
| OK |
| accept and close |
+-----------------------------------------------+
"""
def onOK(self):
self.onApply()
self.close()
"""
+-----------------------------------------------+
| defines the UI, only static elements |
+-----------------------------------------------+
"""
def drawUI(self):
# Our main window will be a QDialog
self.setWindowTitle('Import a Datum object')
self.setWindowIcon( QtGui.QIcon( os.path.join( iconPath , 'FreeCad.svg' ) ) )
self.setMinimumSize(450, 470)
self.resize(450,470)
self.setModal(False)
# make this dialog stay above the others, always visible
self.setWindowFlags( QtCore.Qt.WindowStaysOnTopHint )
# Datum Object
self.labelRight = QtGui.QLabel(self)
self.labelRight.setText("Datum object to import :")
self.labelRight.move(10,20)
self.datumList = QtGui.QLineEdit(self)
self.datumList.setReadOnly(True)
self.datumList.move(40,50)
self.datumList.setMinimumSize(400, 1)
# Datum Type
self.labelType = QtGui.QLabel(self)
self.labelType.setText("Datum type :")
self.labelType.move(10,100)
self.datumType = QtGui.QLineEdit(self)
self.datumType.setReadOnly(True)
self.datumType.move(40,130)
self.datumType.setMinimumSize(400, 1)
# Link instance
self.linkLabel = QtGui.QLabel(self)
self.linkLabel.setText("Link instance's name:")
self.linkLabel.move(10,180)
self.linkName = QtGui.QLineEdit(self)
self.linkName.setReadOnly(True)
self.linkName.setMinimumSize(400, 1)
self.linkName.move(40,210)
# Orig Part
self.partLabel = QtGui.QLabel(self)
self.partLabel.setText("Linked Part's origin:")
self.partLabel.move(10,260)
self.partName = QtGui.QLineEdit(self)
self.partName.setReadOnly(True)
self.partName.move(40,290)
self.partName.setMinimumSize(400, 1)
# imported Link name
self.datumLabel = QtGui.QLabel(self)
self.datumLabel.setText("Enter the new Datum objects's name:")
self.datumLabel.move(10,350)
# the name as seen in the tree of the selected link
self.datumName = QtGui.QLineEdit(self)
self.datumName.setMinimumSize(430, 1)
self.datumName.move(10,380)
# Buttons
#
# Cancel button
self.CancelButton = QtGui.QPushButton('Cancel', self)
self.CancelButton.setAutoDefault(False)
self.CancelButton.move(10, 430)
# Import button
self.ImportButton = QtGui.QPushButton('Import', self)
self.ImportButton.setAutoDefault(False)
self.ImportButton.move(360, 430)
self.ImportButton.setDefault(True)
# OK button
#self.OKButton = QtGui.QPushButton('OK', self)
#self.OKButton.setAutoDefault(False)
#self.OKButton.move(310, 450)
#self.OKButton.setDefault(True)
# Actions
self.CancelButton.clicked.connect(self.onCancel)
self.ImportButton.clicked.connect(self.onOK)
#self.OKButton.clicked.connect(self.onOK)
#self.childrenList.currentIndexChanged.connect( self.onParentList )
#self.datumList.itemClicked.connect( self.onDatumClicked )
"""
+-----------------------------------------------+
| add the command to the workbench |
+-----------------------------------------------+
"""
Gui.addCommand( 'Asm4_importDatum', importDatum() )