forked from shaise/FreeCAD_FastenersWB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastenersCmd.py
438 lines (362 loc) · 16.1 KB
/
FastenersCmd.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
# -*- coding: utf-8 -*-
###################################################################################
#
# FastenersCmd.py
#
# Copyright 2015 Shai Seger <shaise at gmail dot com>
#
# 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.
#
#
###################################################################################
from FreeCAD import Gui
import FreeCAD, FreeCADGui, Part, os
__dir__ = os.path.dirname(__file__)
iconPath = os.path.join( __dir__, 'Icons' )
import FastenerBase
from FastenerBase import FSBaseObject
import ScrewMaker
screwMaker = ScrewMaker.Instance()
class FSScrewObject(FSBaseObject):
def __init__(self, obj, type, attachTo):
'''"Add screw type fastener" '''
FSBaseObject.__init__(self, obj, attachTo)
self.itemText = screwMaker.GetTypeName(type)
diameters = screwMaker.GetAllDiams(type)
diameters.insert(0, 'Auto')
#self.Proxy = obj.Name
obj.addProperty("App::PropertyEnumeration","type","Parameters","Screw type").type = screwMaker.GetAllTypes(self.itemText)
obj.addProperty("App::PropertyEnumeration","diameter","Parameters","Screw diameter standard").diameter = diameters
self.VerifyCreateMatchOuter(obj)
if (self.itemText == "Screw"):
obj.addProperty("App::PropertyEnumeration","length","Parameters","Screw length").length = screwMaker.GetAllLengths(type, diameters[1])
if (self.itemText != "Washer"):
obj.addProperty("App::PropertyBool", "thread", "Parameters", "Generate real thread").thread = False
obj.type = type
obj.Proxy = self
def VerifyCreateMatchOuter(self, obj):
if not (hasattr(obj,'matchOuter')):
obj.addProperty("App::PropertyBool", "matchOuter", "Parameters", "Match outer thread diameter").matchOuter = FastenerBase.FSMatchOuter
def execute(self, fp):
'''"Print a short message when doing a recomputation, this method is mandatory" '''
try:
baseobj = fp.baseObject[0]
shape = baseobj.Shape.getElement(fp.baseObject[1][0])
except:
baseobj = None
shape = None
# for backward compatibility: add missing attribute if needed
self.VerifyCreateMatchOuter(fp)
FreeCAD.Console.PrintLog("MatchOuter:" + str(fp.matchOuter) + "\n")
typechange = False
if fp.type == "ISO7380":
fp.type = "ISO7380-1" # backward compatibility
if not (hasattr(self,'type')) or fp.type != self.type:
typechange = True
curdiam = fp.diameter
diameters = screwMaker.GetAllDiams(fp.type)
diameters.insert(0, 'Auto')
if not(curdiam in diameters):
curdiam='Auto'
fp.diameter = diameters
fp.diameter = curdiam
diameterchange = False
if not (hasattr(self,'diameter')) or self.diameter != fp.diameter:
diameterchange = True
matchouterchange = not (hasattr(self,'matchOuter')) or self.matchOuter != fp.matchOuter
if fp.diameter == 'Auto' or matchouterchange:
d = screwMaker.AutoDiameter(fp.type, shape, baseobj, fp.matchOuter)
fp.diameter = d
diameterchange = True
else:
d = fp.diameter
if hasattr(fp,'length'):
d , l = screwMaker.FindClosest(fp.type, d, fp.length)
if d != fp.diameter:
diameterchange = True
fp.diameter = d
if l != fp.length or diameterchange or typechange:
if diameterchange or typechange:
fp.length = screwMaker.GetAllLengths(fp.type, fp.diameter)
fp.length = l
else:
l = 1
threadType = 'simple'
if hasattr(fp,'thread') and fp.thread:
threadType = 'real'
(key, s) = FastenerBase.FSGetKey(self.itemText, fp.type, d, l, threadType)
if s == None:
s = screwMaker.createScrew(fp.type, d, l, threadType, True)
FastenerBase.FSCache[key] = s
else:
FreeCAD.Console.PrintLog("Using cached object\n")
self.type = fp.type
self.diameter = fp.diameter
self.matchOuter = fp.matchOuter
if hasattr(fp,'length'):
self.length = fp.length
fp.Label = fp.diameter + 'x' + fp.length + '-' + self.itemText
else:
fp.Label = fp.diameter + '-' + self.itemText
if hasattr(fp,'thread'):
self.realThread = fp.thread
#self.itemText = s[1]
fp.Shape = s
if shape != None:
#feature = FreeCAD.ActiveDocument.getObject(self.Proxy)
#fp.Placement = FreeCAD.Placement() # reset placement
FastenerBase.FSMoveToObject(fp, shape, fp.invert, fp.offset.Value)
#def getItemText():
# return self.itemText
class FSViewProviderTree:
"A View provider for custom icon"
def __init__(self, obj):
obj.Proxy = self
self.Object = obj.Object
def attach(self, obj):
self.Object = obj.Object
return
def updateData(self, fp, prop):
return
def getDisplayModes(self,obj):
modes=[]
return modes
def setDisplayMode(self,mode):
return mode
def onChanged(self, vp, prop):
return
def __getstate__(self):
# return {'ObjectName' : self.Object.Name}
return None
def __setstate__(self,state):
if state is not None:
import FreeCAD
doc = FreeCAD.ActiveDocument #crap
self.Object = doc.getObject(state['ObjectName'])
def getIcon(self):
if hasattr(self.Object, "type"):
return os.path.join( iconPath , self.Object.type + '.svg')
elif isinstance(self.Object.Proxy, FSScrewRodObject):
return os.path.join( iconPath , 'ScrewTap.svg')
return os.path.join( iconPath , 'ISO4017.svg')
class FSScrewCommand:
"""Add Screw command"""
def __init__(self, type, help):
self.Type = type
self.Help = help
self.TypeName = screwMaker.GetTypeName(type)
def GetResources(self):
icon = os.path.join( iconPath , self.Type + '.svg')
return {'Pixmap' : icon , # the name of a svg file available in the resources
'MenuText': "Add " + self.Help ,
'ToolTip' : self.Help}
def Activated(self):
for selObj in FastenerBase.FSGetAttachableSelections():
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython",self.TypeName)
FSScrewObject(a, self.Type, selObj)
a.Label = a.Proxy.itemText
FSViewProviderTree(a.ViewObject)
FreeCAD.ActiveDocument.recompute()
return
def IsActive(self):
return Gui.ActiveDocument != None
def FSAddScrewCommand(type, help, dropGroup = None):
cmd = 'FS' + type
Gui.addCommand(cmd,FSScrewCommand(type, help))
FastenerBase.FSCommands.append(cmd, "screws", dropGroup)
FSAddScrewCommand("ISO4017", "ISO 4017 Hex head screw", "Hex head")
FSAddScrewCommand("ISO4014", "ISO 4014 Hex head bolt", "Hex head")
FSAddScrewCommand("EN1662", "EN 1662 Hexagon bolt with flange, small series", "Hex head")
FSAddScrewCommand("EN1665", "EN 1665 Hexagon bolt with flange, heavy series", "Hex head")
FSAddScrewCommand("ISO4762", "ISO4762 Hexagon socket head cap screw", "Hexagon socket")
FSAddScrewCommand("ISO7380-1", "ISO 7380 Hexagon socket button head screw", "Hexagon socket")
FSAddScrewCommand("ISO7380-2", "ISO 7380 Hexagon socket button head screws with collar", "Hexagon socket")
FSAddScrewCommand("ISO10642", "ISO 10642 Hexagon socket countersunk head screw", "Hexagon socket")
FSAddScrewCommand("ISO2009", "ISO 2009 Slotted countersunk flat head screw", "Slotted")
FSAddScrewCommand("ISO2010", "ISO 2010 Slotted raised countersunk head screw", "Slotted")
FSAddScrewCommand("ISO1580", "ISO 1580 Slotted pan head screw", "Slotted")
FSAddScrewCommand("ISO1207", "ISO 1207 Slotted cheese head screw", "Slotted")
FSAddScrewCommand("DIN967", "DIN 967 Cross recessed pan head screws with collar", "H cross")
FSAddScrewCommand("ISO7045", "ISO 7045 Pan head screws type H cross recess", "H cross")
FSAddScrewCommand("ISO7046", "ISO 7046 Countersunk flat head screws H cross r.", "H cross")
FSAddScrewCommand("ISO7047", "ISO 7047 Raised countersunk head screws H cross r.", "H cross")
FSAddScrewCommand("ISO7048", "ISO 7048 Cheese head screws with type H cross r.", "H cross")
FSAddScrewCommand("ISO14579", "ISO 14579 Hexalobular socket head cap screws", "Hexalobular socket")
FSAddScrewCommand("ISO14580", "ISO 14580 Hexalobular socket cheese head screws", "Hexalobular socket")
#FSAddScrewCommand("ISO14581", "ISO 14581 Hexalobular socket countersunk flat head screws", "Hexalobular socket")
FSAddScrewCommand("ISO14582", "ISO 14582 Hexalobular socket countersunk head screws, high head", "Hexalobular socket")
FSAddScrewCommand("ISO14583", "ISO 14583 Hexalobular socket pan head screws", "Hexalobular socket")
FSAddScrewCommand("ISO14584", "ISO 14584 Hexalobular socket raised countersunk head screws", "Hexalobular socket")
FSAddScrewCommand("ISO7089", "ISO 7089 Washer", "Washer")
FSAddScrewCommand("ISO7090", "ISO 7090 Plain Washers, chamfered - Normal series", "Washer")
#FSAddScrewCommand("ISO7091", "ISO 7091 Plain washer - Normal series Product Grade C", "Washer") # same as 7089??
FSAddScrewCommand("ISO7092", "ISO 7092 Plain washers - Small series", "Washer")
FSAddScrewCommand("ISO7093-1", "ISO 7093-1 Plain washers - Large series", "Washer")
FSAddScrewCommand("ISO7094", "ISO 7094 Plain washers - Extra large series", "Washer")
FSAddScrewCommand("ISO4032", "ISO 4032 Hexagon nuts, Style 1", "Nut")
FSAddScrewCommand("ISO4033", "ISO 4033 Hexagon nuts, Style 2", "Nut")
FSAddScrewCommand("ISO4035", "ISO 4035 Hexagon thin nuts, chamfered", "Nut")
#FSAddScrewCommand("ISO4036", "ISO 4035 Hexagon thin nuts, unchamfered", "Nut")
FSAddScrewCommand("EN1661", "EN 1661 Hexagon nuts with flange", "Nut")
#deprecated
class FSWasherObject(FSBaseObject):
def __init__(self, obj, type, attachTo):
'''"Add washer / nut type fastener" '''
FSBaseObject.__init__(self, obj, attachTo)
self.itemText = screwMaker.GetTypeName(type)
diameters = screwMaker.GetAllDiams(type)
diameters.insert(0, 'Auto')
#self.Proxy = obj.Name
obj.addProperty("App::PropertyEnumeration","type","Parameters","Screw type").type = screwMaker.GetAllTypes(self.itemText)
obj.addProperty("App::PropertyEnumeration","diameter","Parameters","Screw diameter standard").diameter = diameters
obj.type = type
obj.Proxy = self
def execute(self, fp):
'''"Print a short message when doing a recomputation, this method is mandatory" '''
try:
baseobj = fp.baseObject[0]
shape = baseobj.Shape.getElement(fp.baseObject[1][0])
except:
baseobj = None
shape = None
if (not (hasattr(self,'diameter')) or self.diameter != fp.diameter):
if fp.diameter == 'Auto':
d = screwMaker.AutoDiameter(fp.type, shape)
diameterchange = True
else:
d = fp.diameter
d , l = screwMaker.FindClosest(fp.type, d, '0')
if d != fp.diameter:
fp.diameter = d
s = screwMaker.createScrew(fp.type, d, l, 'simple', True)
self.diameter = fp.diameter
fp.Label = fp.diameter + '-' + self.itemText
#self.itemText = s[1]
fp.Shape = s
else:
FreeCAD.Console.PrintLog("Using cached object\n")
if shape != None:
#feature = FreeCAD.ActiveDocument.getObject(self.Proxy)
#fp.Placement = FreeCAD.Placement() # reset placement
FastenerBase.FSMoveToObject(fp, shape, fp.invert, fp.offset.Value)
def getItemText():
return self.itemText
#deprecated
class FSWasherCommand:
"""Add Screw command"""
def __init__(self, type, help):
self.Type = type
self.Help = help
def GetResources(self):
icon = os.path.join( iconPath , self.Type + '.svg')
return {'Pixmap' : icon , # the name of a svg file available in the resources
'MenuText': "Add " + self.Help ,
'ToolTip' : self.Help}
def Activated(self):
for selObj in FastenerBase.FSGetAttachableSelections():
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Washer")
FSWasherObject(a, self.Type, selObj)
a.Label = a.Proxy.itemText
FSViewProviderTree(a.ViewObject)
FreeCAD.ActiveDocument.recompute()
return
def IsActive(self):
return Gui.ActiveDocument != None
#Gui.addCommand("FSISO7089",FSWasherCommand("ISO7089", "Washer"))
#FastenerBase.FSCommands.append("FSISO7089")
class FSScrewRodObject(FSBaseObject):
def __init__(self, obj, attachTo):
'''"Add screw rod" '''
FSBaseObject.__init__(self, obj, attachTo)
self.itemText = "ScrewTap"
self.type = 'ScrewTap'
diameters = screwMaker.GetAllDiams(self.type)
diameters.insert(0, 'Auto')
#self.Proxy = obj.Name
obj.addProperty("App::PropertyEnumeration","diameter","Parameters","Screw diameter standard").diameter = diameters
obj.addProperty("App::PropertyLength","length","Parameters","Screw length").length = 20.0
self.VerifyCreateMatchOuter(obj)
obj.addProperty("App::PropertyBool", "thread", "Parameters", "Generate real thread").thread = False
obj.Proxy = self
def VerifyCreateMatchOuter(self, obj):
if not (hasattr(obj,'matchOuter')):
obj.addProperty("App::PropertyBool", "matchOuter", "Parameters", "Match outer thread diameter").matchOuter = FastenerBase.FSMatchOuter
def execute(self, fp):
'''"Print a short message when doing a recomputation, this method is mandatory" '''
try:
baseobj = fp.baseObject[0]
shape = baseobj.Shape.getElement(fp.baseObject[1][0])
except:
baseobj = None
shape = None
self.VerifyCreateMatchOuter(fp)
diameterchange = False
if not (hasattr(self,'diameter')) or self.diameter != fp.diameter:
diameterchange = True
matchouterchange = not (hasattr(self,'matchOuter')) or self.matchOuter != fp.matchOuter
if fp.diameter == 'Auto' or matchouterchange:
d = screwMaker.AutoDiameter(self.type, shape, baseobj, fp.matchOuter)
fp.diameter = d
diameterchange = True
else:
d = fp.diameter
l = fp.length.Value
if l < 2.0:
l = 2.0
fp.length = 2.0
threadType = 'simple'
if hasattr(fp,'thread') and fp.thread:
threadType = 'real'
(key, s) = FastenerBase.FSGetKey(self.itemText, d, str(l), threadType)
if s == None:
s = screwMaker.createScrew(self.type, d, str(l), threadType, True)
FastenerBase.FSCache[key] = s
else:
FreeCAD.Console.PrintLog("Using cached object\n")
self.diameter = fp.diameter
self.length = l
self.matchOuter = fp.matchOuter
fp.Label = fp.diameter + 'x' + str(l) + '-' + self.itemText
self.realThread = fp.thread
fp.Shape = s
if shape != None:
FastenerBase.FSMoveToObject(fp, shape, fp.invert, fp.offset.Value)
class FSScrewRodCommand:
"""Add Screw Rod command"""
def GetResources(self):
icon = os.path.join( iconPath , 'ScrewTap.svg')
return {'Pixmap' : icon , # the name of a svg file available in the resources
'MenuText': "Add Threaded Rod" ,
'ToolTip' : "Add arbitrary length threaded rod"}
def Activated(self):
for selObj in FastenerBase.FSGetAttachableSelections():
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","ScrewTap")
FSScrewRodObject(a, selObj)
a.Label = a.Proxy.itemText
FSViewProviderTree(a.ViewObject)
FreeCAD.ActiveDocument.recompute()
return
def IsActive(self):
return Gui.ActiveDocument != None
Gui.addCommand("FSScrewTap",FSScrewRodCommand())
FastenerBase.FSCommands.append("FSScrewTap", "screws", "misc")
## add fastener types
FastenerBase.FSAddFastenerType("Screw")
FastenerBase.FSAddFastenerType("Washer", False)
FastenerBase.FSAddFastenerType("Nut", False)
FastenerBase.FSAddFastenerType("ScrewTap", True, False)
for item in ScrewMaker.screwTables:
FastenerBase.FSAddItemsToType(ScrewMaker.screwTables[item][0], item)