forked from shaise/FreeCAD_FastenersWB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSChangeParams.py
More file actions
413 lines (366 loc) · 17.2 KB
/
Copy pathFSChangeParams.py
File metadata and controls
413 lines (366 loc) · 17.2 KB
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
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '..\DlgChangeParams.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
###################################################################################
# replace below with generated code from pyuic4
###################################################################################
from PySide import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_DlgChangeParams(object):
def setupUi(self, DlgChangeParams):
DlgChangeParams.setObjectName(_fromUtf8("DlgChangeParams"))
DlgChangeParams.resize(451, 216)
self.gridLayout_2 = QtGui.QGridLayout(DlgChangeParams)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.mainGroup = QtGui.QGroupBox(DlgChangeParams)
self.mainGroup.setObjectName(_fromUtf8("mainGroup"))
self.verticalLayout = QtGui.QVBoxLayout(self.mainGroup)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.labelFastenerType = QtGui.QLabel(self.mainGroup)
self.labelFastenerType.setObjectName(_fromUtf8("labelFastenerType"))
self.horizontalLayout.addWidget(self.labelFastenerType)
self.comboFastenerType = QtGui.QComboBox(self.mainGroup)
self.comboFastenerType.setObjectName(_fromUtf8("comboFastenerType"))
self.horizontalLayout.addWidget(self.comboFastenerType)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.checkAutoDiameter = QtGui.QCheckBox(self.mainGroup)
self.checkAutoDiameter.setObjectName(_fromUtf8("checkAutoDiameter"))
self.horizontalLayout_2.addWidget(self.checkAutoDiameter)
self.comboMatchType = QtGui.QComboBox(self.mainGroup)
self.comboMatchType.setObjectName(_fromUtf8("comboMatchType"))
self.horizontalLayout_2.addWidget(self.comboMatchType)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtGui.QHBoxLayout()
self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
self.labelDiameter = QtGui.QLabel(self.mainGroup)
self.labelDiameter.setObjectName(_fromUtf8("labelDiameter"))
self.horizontalLayout_3.addWidget(self.labelDiameter)
self.comboDiameter = QtGui.QComboBox(self.mainGroup)
self.comboDiameter.setObjectName(_fromUtf8("comboDiameter"))
self.horizontalLayout_3.addWidget(self.comboDiameter)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtGui.QHBoxLayout()
self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4"))
self.labelLength = QtGui.QLabel(self.mainGroup)
self.labelLength.setObjectName(_fromUtf8("labelLength"))
self.horizontalLayout_4.addWidget(self.labelLength)
self.comboLength = QtGui.QComboBox(self.mainGroup)
self.comboLength.setObjectName(_fromUtf8("comboLength"))
self.horizontalLayout_4.addWidget(self.comboLength)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.layoutSetVarLength = QtGui.QHBoxLayout()
self.layoutSetVarLength.setObjectName(_fromUtf8("layoutSetVarLength"))
self.checkSetLength = QtGui.QCheckBox(self.mainGroup)
self.checkSetLength.setObjectName(_fromUtf8("checkSetLength"))
self.layoutSetVarLength.addWidget(self.checkSetLength)
self.spinLength = QtGui.QDoubleSpinBox(self.mainGroup)
self.spinLength.setMinimum(2.0)
self.spinLength.setMaximum(9999.99)
self.spinLength.setObjectName(_fromUtf8("spinLength"))
self.layoutSetVarLength.addWidget(self.spinLength)
self.verticalLayout.addLayout(self.layoutSetVarLength)
spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.gridLayout_2.addWidget(self.mainGroup, 0, 0, 1, 1)
self.retranslateUi(DlgChangeParams)
QtCore.QMetaObject.connectSlotsByName(DlgChangeParams)
def retranslateUi(self, DlgChangeParams):
DlgChangeParams.setWindowTitle(_translate("DlgChangeParams", "Change fastener parameters", None))
self.mainGroup.setTitle(_translate("DlgChangeParams", "Fastener Parameters", None))
self.labelFastenerType.setText(_translate("DlgChangeParams", "Fastener type:", None))
self.checkAutoDiameter.setText(_translate("DlgChangeParams", "Auto set diameter", None))
self.labelDiameter.setText(_translate("DlgChangeParams", "Diameter:", None))
self.labelLength.setText(_translate("DlgChangeParams", "Length:", None))
self.checkSetLength.setText(_translate("DlgChangeParams", "Set length (mm):", None))
###################################################################################
# End position for generated code from pyuic4
###################################################################################
from FreeCAD import Gui
from FreeCAD import Base
import FreeCAD, FreeCADGui, Part, os, math
__dir__ = os.path.dirname(__file__)
iconPath = os.path.join( __dir__, 'Icons' )
import FastenerBase
from FastenerBase import FSBaseObject
import ScrewMaker
screwMaker = ScrewMaker.Instance()
import sys
import FastenersCmd, PEMInserts
def FSCPGetDiameters(type, item):
if type == "Screw" or type == "Washer" or type == "ScrewTap" or type == "Nut":
return screwMaker.GetAllDiams(item)
if type == "PressNut" or type == "StandOff" or type == "Stud":
return PEMInserts.FSPIGetAllDiameters(type)
return []
def FSCPGetLengths(type, item, diam):
if type == "Screw":
return screwMaker.GetAllLengths(item, diam)
if type == "StandOff" or type == "Stud":
return PEMInserts.FSPIGetAllLengths(item, diam)
return []
def FSCPGetDiametersFromSelection(sel):
try:
if len(sel) == 0:
return []
obj0 = sel[0]
listDiams = screwMaker.GetAllDiams(obj0.type)
if len(sel) == 1:
return listDiams
listTypes = [obj0.type]
for obj in sel:
if obj.type in listTypes:
continue
listTypes.append(obj.type)
tmpList = listDiams
listDiams = []
for diam in screwMaker.GetAllDiams(obj.type):
if diam in tmpList:
listDiams.append(diam)
return listDiams
except:
FastenerBase.FSShowError()
return []
class FSCPSelectionFilter:
''' Disable selection changes '''
def allow(self,doc,obj,sub):
return False
FSCPSelectionFilterGate = FSCPSelectionFilter()
class FSCPSelObserver:
''' monitor and disable selection changes '''
def __init__(self, curSel):
self.selection = curSel
self.disableObserver = False
def addSelection(self,doc,obj,sub,pnt):
FreeCAD.Console.PrintLog("FSO-AddObj:" + "\n")
return True
def removeSelection(self,doc,obj,sub): # Delete the selected object
FreeCAD.Console.PrintLog("FSO-RemSel:" +str(obj) + ":" + str(sub) + "\n")
def setSelection(self,doc): # Selection in ComboView
FreeCAD.Console.PrintLog("FSO-SetSel:" + "\n")
def clearSelection(self,doc): # If click on the screen, clear the selection
#if self.disableObserver:
# FreeCAD.Console.PrintLog("FSO-Reentering:" + "\n")
# return
self.disableObserver = True
#FreeCAD.Console.PrintLog("Clearing:" + str(len(Gui.Selection.getSelection())) + "\n")
#Gui.Selection.clearSelection()
for obj in self.selection:
#FreeCAD.Console.PrintLog("Adding:" + str(obj) + "\n")
Gui.Selection.addSelection(obj)
FreeCAD.Console.PrintLog("FSO-ClrSel:" + "\n")
#self.disableObserver = False
return False
class FSTaskChangeParamDialog:
def __init__(self, obj):
self.object = obj
self.baseObj = obj
self.disableUpdate = True
self.selection = Gui.Selection.getSelection()
self.matchOuter = FastenerBase.FSMatchOuter
FSChangeParamDialog = QtGui.QWidget()
FSChangeParamDialog.ui = Ui_DlgChangeParams()
FSChangeParamDialog.ui.setupUi(FSChangeParamDialog)
ui = FSChangeParamDialog.ui
#FSChangeParamDialog.ui.widgetVarLength.hide()
self.form = FSChangeParamDialog
self.form.setWindowTitle("Change fastener parameters")
Gui.Selection.addSelectionGate(FSCPSelectionFilterGate)
self.selobserver = FSCPSelObserver(self.selection)
Gui.Selection.addObserver(self.selobserver)
ui.comboFastenerType.currentIndexChanged.connect(self.onFastenerChange)
ui.comboDiameter.currentIndexChanged.connect(self.onDiameterChange)
ui.checkAutoDiameter.stateChanged.connect(self.onAutoDiamChange)
ui.checkSetLength.stateChanged.connect(self.onSetLengthChange)
ui.spinLength.setEnabled(False)
self.hatMatchOption = False
if len(self.selection) > 0:
selobj = self.selection[0]
#FreeCAD.Console.PrintLog("selobj: " + str(selobj.Proxy) + "\n")
if hasattr(selobj, 'Proxy') and hasattr(selobj.Proxy, 'VerifyCreateMatchOuter'):
self.hatMatchOption = True
if self.hatMatchOption:
ui.comboMatchType.addItem("No Change")
ui.comboMatchType.addItem(QtGui.QIcon(os.path.join(iconPath , 'IconMatchTypeInner.svg')), "Match inner thread")
ui.comboMatchType.addItem(QtGui.QIcon(os.path.join(iconPath , 'IconMatchTypeOuter.svg')), "Match outer thread")
ui.comboMatchType.setEnabled(False)
ui.comboMatchType.setCurrentIndex(0)
else:
ui.comboMatchType.hide()
def FillFields(self, fstype):
ui = self.form.ui
self.fstype = fstype
#FreeCAD.Console.PrintLog(fstype.typeName + str(fstype.hasLength) + str(fstype.lengthFixed) + "\n")
ui.comboFastenerType.addItem('No Change')
#FreeCAD.Console.PrintLog("nitems: " + str(len(fstype.items)) + "\n")
for screw in fstype.items:
ui.comboFastenerType.addItem(QtGui.QIcon(os.path.join(iconPath , screw + '.svg')), screw)
if len(fstype.items) == 1:
ui.comboFastenerType.setCurrentIndex(1)
ui.comboFastenerType.setEnabled(False)
#FreeCAD.Console.PrintLog("manual\n")
self.disableUpdate = False
self.UpdateDiameters()
return
def UpdateDiameters(self):
if self.disableUpdate:
return
try:
ui = self.form.ui
ui.comboDiameter.clear()
ui.comboDiameter.addItem('No Change')
#FreeCAD.Console.PrintLog(str(ui.comboFastenerType.currentIndex()) + " " + str(ui.comboFastenerType.count()) + "\n")
if ui.comboFastenerType.currentIndex() == 0 and ui.comboFastenerType.isEnabled():
listDiams = FSCPGetDiametersFromSelection(self.selection)
else:
listDiams = FSCPGetDiameters(self.fstype.typeName, ui.comboFastenerType.currentText())
for diam in listDiams:
ui.comboDiameter.addItem(diam)
except:
FastenerBase.FSShowError()
return
def UpdateLengths(self):
try:
ui = self.form.ui
self.fixedLength = ui.comboFastenerType.currentIndex() > 0 and ui.comboDiameter.currentIndex() > 0 and self.fstype.lengthFixed
if not(self.fstype.hasLength):
ui.labelLength.hide()
ui.comboLength.hide()
ui.checkSetLength.hide()
ui.spinLength.hide()
elif self.fixedLength:
ui.labelLength.show()
ui.comboLength.show()
ui.checkSetLength.hide()
ui.spinLength.hide()
ui.comboLength.clear()
ui.comboLength.addItem('No Change')
for slen in FSCPGetLengths(self.fstype.typeName, ui.comboFastenerType.currentText(), ui.comboDiameter.currentText()):
ui.comboLength.addItem(slen)
else:
ui.labelLength.hide()
ui.comboLength.hide()
ui.checkSetLength.show()
ui.spinLength.show()
except:
FastenerBase.FSShowError()
return
def onFastenerChange(self, findex):
#FreeCAD.Console.PrintLog("fastener change\n")
self.UpdateDiameters()
return
def onDiameterChange(self, dindex):
self.UpdateLengths()
return
def onAutoDiamChange(self, val):
try:
ui = self.form.ui
if (ui.checkAutoDiameter.isChecked()):
ui.comboDiameter.setEnabled(False)
ui.comboMatchType.setEnabled(True)
else:
ui.comboDiameter.setEnabled(True)
ui.comboMatchType.setEnabled(False)
except:
FastenerBase.FSShowError()
return
def onSetLengthChange(self, val):
try:
ui = self.form.ui
if (ui.checkSetLength.isChecked()):
ui.spinLength.setEnabled(True)
else:
ui.spinLength.setEnabled(False)
except:
FastenerBase.FSShowError()
return
def accept(self):
ui = self.form.ui
try:
# apply type and diameter
for obj in self.selection:
if ui.comboFastenerType.isEnabled() and ui.comboFastenerType.currentIndex() > 0:
obj.type = str(ui.comboFastenerType.currentText())
if ui.checkAutoDiameter.isChecked():
if self.hatMatchOption and ui.comboMatchType.currentIndex() > 0:
obj.Proxy.VerifyCreateMatchOuter(obj)
obj.matchOuter = ui.comboMatchType.currentIndex() == 2
obj.diameter = 'Auto'
elif ui.comboDiameter.currentIndex() > 0:
obj.diameter = str(ui.comboDiameter.currentText())
FreeCAD.ActiveDocument.recompute()
# apply length
for obj in self.selection:
if self.fstype.hasLength:
if self.fixedLength:
obj.length = str(ui.comboLength.currentText())
else:
if ui.checkSetLength.isChecked():
if isinstance(obj.Proxy, FastenersCmd.FSScrewRodObject):
obj.length = ui.spinLength.value()
else:
d, l = screwMaker.FindClosest(obj.type, obj.diameter, ui.spinLength.value())
obj.length = l
FreeCAD.ActiveDocument.recompute()
except:
FastenerBase.FSShowError()
self.DialogClosing()
return True
def reject(self):
self.DialogClosing()
return True
def DialogClosing(self):
Gui.Selection.removeSelectionGate()
Gui.Selection.removeObserver(self.selobserver)
FastenerBase.FSMatchIconNeedUpdate = 2
return
def getStandardButtons(self):
return int(QtGui.QDialogButtonBox.Ok) + int(QtGui.QDialogButtonBox.Cancel)
class FSChangeParamCommand:
"""Make holes for countersunk screws"""
def GetResources(self):
icon = os.path.join( iconPath , 'IconChangeParam.svg')
return {'Pixmap' : icon , # the name of a svg file available in the resources
'MenuText': "Change fastener parameters" ,
'ToolTip' : "Change parameters of selected fasteners"}
def Activated(self):
dlg = FSTaskChangeParamDialog(None)
fstype = FastenerBase.FSFasenerTypeDB[self.type]
dlg.FillFields(fstype)
Gui.Control.showDialog(dlg)
return
def IsActive(self):
sel = Gui.Selection.getSelection()
if len(sel) == 0:
return False
self.type = None
tmaxlen = 0
for typename in FastenerBase.FSFasenerTypeDB:
#FreeCAD.Console.PrintLog(typename + "\n")
if filter(lambda c: not c.isdigit(), sel[0].Name) == typename:
self.type = typename
if self.type == None:
return False
for obj in sel:
if filter(lambda c: not c.isdigit(), obj.Name) != self.type:
return False
return True
Gui.addCommand("FSChangeParams", FSChangeParamCommand())
FastenerBase.FSCommands.append("FSChangeParams", "command")