forked from Zolko-123/FreeCAD_Assembly4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertLinkCmd.py
267 lines (212 loc) · 9.28 KB
/
insertLinkCmd.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
#!/usr/bin/env python3
# coding: utf-8
#
# insertLinkCmd.py
from PySide import QtGui, QtCore
import FreeCADGui as Gui
import FreeCAD as App
import Part, math, re
from libAsm4 import *
"""
+-----------------------------------------------+
| main class |
+-----------------------------------------------+
"""
class insertLink( QtGui.QDialog ):
"My tool object"
def __init__(self):
super(insertLink,self).__init__()
def GetResources(self):
return {"MenuText": "Link an external Part",
"Accel": "Ctrl+L",
"ToolTip": "Insert a link to external Part from another open document",
"Pixmap" : os.path.join( iconPath , 'Link_Part.svg')
}
def IsActive(self):
# We only insert a link into an Asm4 Model
if App.ActiveDocument and self.getSelection():
return True
return False
def getSelection(self):
selectedObj = None
# check that there is an App::Part called 'Model'
if App.ActiveDocument.getObject('Model'):
selectedObj = App.ActiveDocument.getObject('Model')
else:
return None
# if an App::Link is selected, return that (we'll duplicate it)
if Gui.Selection.getSelection():
selObj = Gui.Selection.getSelection()[0]
# it's an App::Link
if selObj.isDerivedFrom('App::Link'):
selectedObj = selObj
return selectedObj
"""
+-----------------------------------------------+
| the real stuff |
+-----------------------------------------------+
"""
def Activated(self):
# This function is executed when the command is activated
# get the current active document to avoid errors if user changes tab
self.activeDoc = App.ActiveDocument
# get the 'Model' object of the current document
# it has been checked that it exists before
# this is where the App::Link will be placed, even if there are other plain App::Parts
self.asmModel = self.activeDoc.getObject('Model')
# if an App::Link is selected, we'll ducplicate it
selObj = self.getSelection()
if selObj.isDerivedFrom('App::Link'):
self.origLink = selObj
else:
self.origLink = None
# the GUI objects are defined later down
self.drawUI()
# Search for all App::Parts in all open documents
# Also store the document of the part
self.allParts = []
self.partsDoc = []
for doc in App.listDocuments().values():
# there might be more than 1 App::Part per document
for obj in doc.findObjects("App::Part"):
# we don't want to link to itself to the 'Model' object
# other App::Part in the same document are OK
# (even though I don't see the use-case)
if obj != self.asmModel:
self.allParts.append( obj )
self.partsDoc.append( doc )
# build the list
for part in self.allParts:
newItem = QtGui.QListWidgetItem()
if part.Name == part.Label:
partText = part.Name
else:
partText = part.Label + ' (' +part.Name+ ')'
newItem.setText( part.Document.Name +"#"+ partText )
newItem.setIcon(part.ViewObject.Icon)
self.partList.addItem(newItem)
# if an existing App::Link was selected
if self.origLink:
origPart = self.origLink.LinkedObject
# try to find the original part of the selected link
# MatchExactly, MatchContains, MatchEndsWith, MatchStartsWith ...
origPartText = origPart.Document.Name +"#"+ origPart.Label
partFound = self.partList.findItems( origPartText, QtCore.Qt.MatchStartsWith )
if partFound:
self.partList.setCurrentItem(partFound[0])
# set the proposed name to a duplicate of the original link name
origName = self.origLink.Label
# if the last character is a number, we increment this number
lastChar = origName[-1]
if lastChar.isnumeric():
origInstanceNum = int(lastChar)
proposedLinkName = origName[:-1]+str(origInstanceNum+1)
# else we append a _2 to the original name (Label)
else:
proposedLinkName = origName+'_2'
self.linkNameInput.setText( proposedLinkName )
# show the UI
self.show()
"""
+-----------------------------------------------+
| the real stuff happens here |
+-----------------------------------------------+
"""
def onCreateLink(self):
# parse the selected items
# TODO : there should only be 1
selectedPart = []
for selected in self.partList.selectedIndexes():
# get the selected part
selectedPart = self.allParts[ selected.row() ]
# get the name of the link (as it should appear in the tree)
linkName = self.linkNameInput.text()
# only create link if there is a Part object and a name
if self.asmModel and selectedPart and linkName:
# create the App::Link with the user-provided name
createdLink = self.activeDoc.getObject('Model').newObject( 'App::Link', linkName )
# assigne the user-selected selectedPart to it
createdLink.LinkedObject = selectedPart
# update the link
createdLink.recompute()
# close the dialog UI...
self.close()
# ... and launch the placement of the inserted part
Gui.Selection.clearSelection()
Gui.Selection.addSelection( self.activeDoc.Name, 'Model', createdLink.Name+'.' )
Gui.runCommand( 'Asm4_placeLink' )
# if still open, close the dialog UI
self.close()
def onItemClicked( self, item ):
for selected in self.partList.selectedIndexes():
# get the selected part
part = self.allParts[ selected.row() ]
doc = self.partsDoc[ selected.row() ]
# if the App::Part has been renamed by the user, we suppose it's important
# thus we append the Label to the link's name
# this might happen if there are multiple App::Parts in a document
if doc == self.activeDoc:
proposedLinkName = part.Label
else:
if part.Name == 'Model':
proposedLinkName = part.Document.Name
else:
proposedLinkName = part.Document.Name+'_'+part.Label
self.linkNameInput.setText( proposedLinkName )
"""
+-----------------------------------------------+
| some functions |
+-----------------------------------------------+
"""
def onCancel(self):
self.close()
"""
+-----------------------------------------------+
| defines the UI, only static elements |
+-----------------------------------------------+
"""
def drawUI(self):
# Our main window is a QDialog
self.setModal(False)
# make this dialog stay above the others, always visible
self.setWindowFlags( QtCore.Qt.WindowStaysOnTopHint )
self.setWindowTitle('Insert a Model')
self.setWindowIcon( QtGui.QIcon( os.path.join( iconPath , 'FreeCad.svg' ) ) )
self.setMinimumSize(400, 500)
self.resize(400,500)
#self.Layout.addWidget(self.GUIwindow)
# label
self.labelMain = QtGui.QLabel(self)
self.labelMain.setText("Select Part to be inserted :")
self.labelMain.move(10,20)
#self.Layout.addWidget(self.labelMain)
# label
self.labelLink = QtGui.QLabel(self)
self.labelLink.setText("Enter a Name for the link :\n(Must be unique in the Model tree)")
self.labelLink.move(10,350)
# Create a line that will contain the name of the link (in the tree)
self.linkNameInput = QtGui.QLineEdit(self)
self.linkNameInput.setMinimumSize(380, 0)
self.linkNameInput.move(10, 400)
# The part list is a QListWidget
self.partList = QtGui.QListWidget(self)
self.partList.move(10,50)
self.partList.setMinimumSize(380, 280)
# Cancel button
self.CancelButton = QtGui.QPushButton('Cancel', self)
self.CancelButton.setAutoDefault(False)
self.CancelButton.move(10, 460)
# create Link button
self.createLinkButton = QtGui.QPushButton('Insert part', self)
self.createLinkButton.move(285, 460)
self.createLinkButton.setDefault(True)
# Actions
self.CancelButton.clicked.connect(self.onCancel)
self.createLinkButton.clicked.connect(self.onCreateLink)
self.partList.itemClicked.connect( self.onItemClicked)
"""
+-----------------------------------------------+
| add the command to the workbench |
+-----------------------------------------------+
"""
Gui.addCommand( 'Asm4_insertLink', insertLink() )