-
Notifications
You must be signed in to change notification settings - Fork 58
/
SheetMetalBaseShapeCmd.py
462 lines (396 loc) · 17.1 KB
/
SheetMetalBaseShapeCmd.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
# -*- coding: utf-8 -*-
###################################################################################
#
# SheetMetalBaseShapeCmd.py
#
# Copyright 2023 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 Lesser 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 Lesser 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.
#
#
###################################################################################
import FreeCAD, Part, os, SheetMetalTools
from SheetMetalCmd import smBend
icons_path = SheetMetalTools.icons_path
panels_path = SheetMetalTools.panels_path
language_path = SheetMetalTools.language_path
base_shape_types = ["Flat", "L-Shape", "U-Shape", "Tub", "Hat", "Box"]
origin_location_types = ["-X,-Y", "-X,0", "-X,+Y", "0,-Y", "0,0", "0,+Y", "+X,-Y", "+X,0", "+X,+Y"]
origin_location_buttons = ["BL", "CL", "TL", "BC", "CC", "TC", "BR", "CR", "TR"]
# IMPORTANT: please remember to change the element map version in case of any
# changes in modeling logic
smElementMapVersion = 'sm1.'
##########################################################################################################
# Object class and creation function
##########################################################################################################
def GetOriginShift(dimension, type, bendCompensation):
type = type[0]
if type == '+':
return -dimension - bendCompensation
if type == '0':
return -dimension / 2.0
return bendCompensation
def smCreateBaseShape(type, thickness, radius, width, length, height, flangeWidth, fillGaps, origin):
bendCompensation = thickness + radius
height -= bendCompensation
compx = 0
compy = 0
if type == "U-Shape":
numfolds = 2
width -= 2.0 * bendCompensation
compy = bendCompensation
elif type in ["Tub", "Hat", "Box"]:
numfolds = 4
width -= 2.0 * bendCompensation
length -= 2.0 * bendCompensation
compx = compy = bendCompensation
elif type == "L-Shape":
numfolds = 1
width -= bendCompensation
else:
numfolds = 0
if type in ["Hat", "Box"]:
height -= bendCompensation
flangeWidth -= radius
if width < thickness: width = thickness
if height < thickness: height = thickness
if length < thickness: length = thickness
if flangeWidth < thickness: flangeWidth = thickness
originX, originY = origin.split(',')
offsx = GetOriginShift(length, originX, compx)
offsy = GetOriginShift(width, originY, compy)
if type == "L-Shape" and originY == "+Y":
offsy -= bendCompensation
box = Part.makeBox(length, width, thickness, FreeCAD.Vector(offsx, offsy, 0))
#box.translate(FreeCAD.Vector(offsx, offsy, 0))
if numfolds == 0:
return box
faces = []
for i, face in enumerate(box.Faces):
v = face.normalAt(0,0)
if (v.y > 0.5 or
(v.y < -0.5 and numfolds > 1) or
(v.x > 0.5 and numfolds > 2) or
(v.x < -0.5 and numfolds > 3)):
faces.append("Face" + str(i+1))
shape, _f = smBend(thickness, selFaceNames = faces, extLen = height, bendR = radius,
MainObject = box, automiter = fillGaps, maxExtendGap = 999999)
if type in ["Hat", "Box"]:
faces = []
invertBend = False
if type == "Hat":
invertBend = True
for i, face in enumerate(shape.Faces):
v = face.normalAt(0,0)
z = face.CenterOfGravity.z
if v.z > 0.9999 and z > bendCompensation:
faces.append("Face" + str(i+1))
shape, _f = smBend(thickness, selFaceNames = faces, extLen = flangeWidth,
bendR = radius, MainObject = shape, flipped = invertBend,
automiter = fillGaps)
#SMLogger.message(str(faces))
return shape
class SMBaseShape:
def __init__(self, obj):
'''"Add a base sheetmetal shape" '''
self._addVerifyProperties(obj)
obj.Proxy = self
def _addVerifyProperties(self, obj):
SheetMetalTools.smAddLengthProperty(
obj,
"thickness",
FreeCAD.Qt.translate("SMBaseShape", "Thickness of sheetmetal", "Property"),
1.0,
)
SheetMetalTools.smAddLengthProperty(
obj,
"radius",
FreeCAD.Qt.translate("SMBaseShape", "Bend Radius", "Property"),
1.0,
)
SheetMetalTools.smAddLengthProperty(
obj,
"width",
FreeCAD.Qt.translate("SMBaseShape", "Shape width", "Property"),
20.0,
)
SheetMetalTools.smAddLengthProperty(
obj,
"length",
FreeCAD.Qt.translate("SMBaseShape", "Shape length", "Property"),
30.0,
)
SheetMetalTools.smAddLengthProperty(
obj,
"height",
FreeCAD.Qt.translate("SMBaseShape", "Shape height", "Property"),
10.0,
)
SheetMetalTools.smAddLengthProperty(
obj,
"flangeWidth",
FreeCAD.Qt.translate("SMBaseShape", "Width of top flange", "Property"),
5.0,
)
SheetMetalTools.smAddEnumProperty(
obj,
"shapeType",
FreeCAD.Qt.translate("SMBaseShape", "Base shape type", "Property"),
base_shape_types,
defval = "L-Shape"
)
SheetMetalTools.smAddEnumProperty(
obj,
"originLoc",
FreeCAD.Qt.translate("SMBaseShape", "Location of part origin", "Property"),
origin_location_types,
defval = "0,0"
)
SheetMetalTools.smAddBoolProperty(
obj,
"fillGaps",
FreeCAD.Qt.translate(
"SMBaseShape", "Extend sides and flange to close all gaps", "Property"
),
True,
)
def getElementMapVersion(self, _fp, ver, _prop, restored):
if not restored:
return smElementMapVersion + ver
def onChanged(self, fp, prop):
if prop == "shapeType":
flat = fp.shapeType == "Flat"
hat_box = fp.shapeType in ["Hat", "Box"]
fp.setEditorMode("radius", flat)
fp.setEditorMode("height", flat)
fp.setEditorMode("flangeWidth", not hat_box)
def execute(self, fp):
self._addVerifyProperties(fp)
s = smCreateBaseShape(type = fp.shapeType, thickness = fp.thickness.Value,
radius = fp.radius.Value, width = fp.width.Value,
length = fp.length.Value, height = fp.height.Value,
flangeWidth = fp.flangeWidth.Value, fillGaps = fp.fillGaps,
origin = fp.originLoc)
fp.Shape = s
def getBaseShapeTypes(self):
return base_shape_types
def getOriginLocationTypes(self):
return origin_location_types
##########################################################################################################
# Gui code
##########################################################################################################
if SheetMetalTools.isGuiLoaded():
from PySide import QtCore, QtGui
from FreeCAD import Gui
from SheetMetalLogger import SMLogger
mw = Gui.getMainWindow()
##########################################################################################################
# Task
##########################################################################################################
class BaseShapeTaskPanel:
''' Task Panel for Base Shape sheetmetal command '''
def __init__(self, baseObj):
self.obj = baseObj
QtCore.QDir.addSearchPath('Icons', icons_path)
path = os.path.join(panels_path, 'BaseShapeOptions.ui')
self.form = Gui.PySideUic.loadUi(path)
self.formReady = False
self.firstTime = False
self.selOrigButton = None
self.spinPairs = []
self.ShowAxisCross()
self.setupUi()
def setupUi(self):
SheetMetalTools.taskConnectSpin(self, self.form.bRadiusSpin, "radius")
SheetMetalTools.taskConnectSpin(self, self.form.bThicknessSpin, "thickness")
SheetMetalTools.taskConnectSpin(self, self.form.bWidthSpin, "width")
SheetMetalTools.taskConnectSpin(self, self.form.bHeightSpin, "height")
SheetMetalTools.taskConnectSpin(self, self.form.bFlangeWidthSpin, "flangeWidth")
SheetMetalTools.taskConnectSpin(self, self.form.bLengthSpin, "length")
self.form.shapeType.currentIndexChanged.connect(self.typeChanged)
self.form.chkFillGaps.stateChanged.connect(self.checkChanged)
for origloc in origin_location_buttons:
buttname = 'push' + origloc
butt = self.form.findChild(QtGui.QPushButton, buttname)
butt.pressed.connect(lambda b = butt: self.origButtPressed(b))
self.form.update()
#SMLogger.log(str(self.formReady) + " <2 \n")
def updateEnableState(self):
shapeType = base_shape_types[self.form.shapeType.currentIndex()]
self.form.bFlangeWidthSpin.setEnabled(shapeType in ["Hat", "Box"])
self.form.bRadiusSpin.setEnabled(not shapeType == "Flat")
self.form.bHeightSpin.setEnabled(not shapeType == "Flat")
def buttonToOriginType(self, butt):
if butt is None:
name = 'pushCC'
else:
name = butt.objectName()
return origin_location_types[origin_location_buttons.index(name[-2:])]
def originTypeToButton(self, type):
name = "push" + origin_location_buttons[origin_location_types.index(type)]
return self.form.findChild(QtGui.QPushButton, name)
def setSelectedOrigButton(self, butt):
if self.selOrigButton is not None:
self.selOrigButton.setIcon(QtGui.QIcon())
if butt is not None:
butt.setIcon(QtGui.QIcon('Icons:BaseShape_Sel.svg'))
self.selOrigButton = butt
def spinValChanged(self):
if not self.formReady:
return
self.updateObj()
self.obj.recompute()
def typeChanged(self):
self.updateEnableState()
self.spinValChanged()
def origButtPressed(self, butt):
# print(butt.objectName())
self.setSelectedOrigButton(butt)
self.spinValChanged()
def checkChanged(self):
self.spinValChanged()
def ShowAxisCross(self):
self.hasAxisCross = Gui.ActiveDocument.ActiveView.hasAxisCross()
Gui.ActiveDocument.ActiveView.setAxisCross(True)
def RevertAxisCross(self):
Gui.ActiveDocument.ActiveView.setAxisCross(self.hasAxisCross)
def updateObj(self):
#spin = Gui.UiLoader().createWidget("Gui::QuantitySpinBox")
#SMLogger.log(str(self.form.bRadiusSpin.property('rawValue')))
for formvar, objvar in self.spinPairs:
setattr(self.obj, objvar, formvar.property("value"))
selected_type = self.form.shapeType.currentText()
if selected_type not in base_shape_types:
selected_type = base_shape_types[self.form.shapeType.currentIndex()]
self.obj.shapeType = selected_type
self.obj.originLoc = self.buttonToOriginType(self.selOrigButton)
self.obj.fillGaps = self.form.chkFillGaps.isChecked()
def accept(self):
doc = FreeCAD.ActiveDocument
self.updateObj()
if self.firstTime and self.form.chkNewBody.isChecked():
body = FreeCAD.activeDocument().addObject('PartDesign::Body','Body')
body.Label = 'Body'
body.addObject(self.obj)
Gui.ActiveDocument.ActiveView.setActiveObject('pdbody', body)
self.firstTime = False
doc.commitTransaction()
Gui.Control.closeDialog()
doc.recompute()
Gui.ActiveDocument.resetEdit()
self.RevertAxisCross()
def reject(self):
FreeCAD.ActiveDocument.abortTransaction()
Gui.Control.closeDialog()
FreeCAD.ActiveDocument.recompute()
Gui.ActiveDocument.resetEdit()
self.RevertAxisCross()
def updateSpin(self, spin, property):
Gui.ExpressionBinding(spin).bind(self.obj, property)
spin.setProperty('value', getattr(self.obj, property))
def update(self):
self.form.shapeType.setCurrentText(self.obj.shapeType)
self.setSelectedOrigButton(self.originTypeToButton(self.obj.originLoc))
self.form.chkFillGaps.setChecked(self.obj.fillGaps)
self.form.chkNewBody.setVisible(self.firstTime)
self.formReady = True
##########################################################################################################
# View Provider
##########################################################################################################
class SMBaseShapeViewProviderFlat:
"A View provider that nests children objects under the created one"
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):
self.loads(state)
# dumps and loads replace __getstate__ and __setstate__ post v. 0.21.2
def dumps(self):
return None
def loads(self, state):
if state is not None:
self.Object = FreeCAD.ActiveDocument.getObject(state['ObjectName'])
def claimChildren(self):
return []
def getIcon(self):
return os.path.join( icons_path , 'SheetMetal_AddBaseShape.svg')
def setEdit(self, vobj, mode):
SMLogger.log(
FreeCAD.Qt.translate("Logger", "Base shape edit mode: ") + str(mode)
)
if mode != 0:
return None
taskd = BaseShapeTaskPanel(vobj.Object)
taskd.firstTime = False
taskd.update()
#self.Object.ViewObject.Visibility=False
Gui.Selection.clearSelection()
FreeCAD.ActiveDocument.openTransaction("BaseShape")
Gui.Control.showDialog(taskd)
return True
def unsetEdit(self, vobj, mode):
Gui.Control.closeDialog()
self.Object.ViewObject.Visibility=True
return False
##########################################################################################################
# Command
##########################################################################################################
class SMBaseshapeCommandClass:
"""Open Base shape task"""
def GetResources(self):
# add translations path
Gui.addLanguagePath(language_path)
Gui.updateLocale()
return {
"Pixmap": os.path.join(
icons_path, "SheetMetal_AddBaseShape.svg"
), # the name of a svg file available in the resources
"MenuText": FreeCAD.Qt.translate("SheetMetal", "Add base shape"),
"Accel": "H",
"ToolTip": FreeCAD.Qt.translate(
"SheetMetal",
"Add basic sheet metal object."
),
}
def Activated(self):
doc = FreeCAD.ActiveDocument
doc.openTransaction("BaseShape")
a = doc.addObject("PartDesign::FeaturePython","BaseShape")
SMBaseShape(a)
SMBaseShapeViewProviderFlat(a.ViewObject)
doc.recompute()
dialog = BaseShapeTaskPanel(a)
dialog.firstTime = True
dialog.update()
Gui.Control.showDialog(dialog)
def IsActive(self):
return FreeCAD.ActiveDocument is not None
Gui.addCommand("SheetMetal_BaseShape", SMBaseshapeCommandClass())