-
Notifications
You must be signed in to change notification settings - Fork 58
/
SheetMetalBaseCmd.py
292 lines (255 loc) · 10.7 KB
/
SheetMetalBaseCmd.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
# -*- coding: utf-8 -*-
###################################################################################
#
# SheetMetalBaseCmd.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 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
import SheetMetalTools
translate = FreeCAD.Qt.translate
icons_path = SheetMetalTools.icons_path
panels_path = SheetMetalTools.panels_path
smBaseDefaults = {}
def modifiedWire(WireList, radius, thk, length, normal, Side, sign):
# If sketch is one type, make a face by extruding & offset it to correct position
wire_extr = WireList.extrude(normal * sign * length)
# Part.show(wire_extr,"wire_extr")
if Side == "Inside":
wire_extr = wire_extr.makeOffsetShape(thk / 2.0 * sign, 0.0, fill=False, join=2)
elif Side == "Outside":
wire_extr = wire_extr.makeOffsetShape(
-thk / 2.0 * sign, 0.0, fill=False, join=2
)
# Part.show(wire_extr,"wire_extr")
try:
filleted_extr = wire_extr.makeFillet((radius + thk / 2.0), wire_extr.Edges)
except:
filleted_extr = wire_extr
# Part.show(filleted_extr,"filleted_extr")
filleted_extr = filleted_extr.makeOffsetShape(
thk / 2.0 * sign, 0.0, fill=False, join=2
)
# Part.show(filleted_extr,"filleted_extr")
return filleted_extr
def smBase(
thk=2.0,
length=10.0,
radius=1.0,
Side="Inside",
midplane=False,
reverse=False,
MainObject=None,
):
# To Get sketch normal
WireList = MainObject.Shape.Wires[0]
mat = MainObject.getGlobalPlacement().Rotation
normal = (mat.multVec(FreeCAD.Vector(0, 0, 1))).normalize()
# print([mat, normal])
if WireList.isClosed():
# If Closed sketch is there, make a face & extrude it
sketch_face = Part.makeFace(MainObject.Shape.Wires, "Part::FaceMakerBullseye")
thk = -1.0 * thk if reverse else thk
wallSolid = sketch_face.extrude(sketch_face.normalAt(0, 0) * thk)
if midplane:
wallSolid = Part.Solid(
wallSolid.translated(sketch_face.normalAt(0, 0) * thk * -0.5)
)
else:
filleted_extr = modifiedWire(WireList, radius, thk, length, normal, Side, 1.0)
# Part.show(filleted_extr,"filleted_extr")
dist = WireList.Vertexes[0].Point.distanceToPlane(
FreeCAD.Vector(0, 0, 0), normal
)
# print(dist)
slice_wire = filleted_extr.slice(normal, dist)
# print(slice_wire)
# Part.show(slice_wire[0],"slice_wire")
traj = slice_wire[0]
# Part.show(traj,"traj")
if midplane:
traj.translate(normal * -length / 2.0)
elif reverse:
traj.translate(normal * -length)
traj_extr = traj.extrude(normal * length)
# Part.show(traj_extr,"traj_extr")
solidlist = []
for face in traj_extr.Faces:
solid = face.makeOffsetShape(thk, 0.0, fill=True)
solidlist.append(solid)
if len(solidlist) > 1:
wallSolid = solidlist[0].multiFuse(solidlist[1:])
else:
wallSolid = solidlist[0]
# Part.show(wallSolid,"wallSolid")
# Part.show(wallSolid,"wallSolid")
return wallSolid
class SMBaseBend:
def __init__(self, obj, sketch):
'''"Add wall or Wall with radius bend"'''
_tip_ = translate("App::Property", "Bend Plane")
obj.addProperty(
"App::PropertyEnumeration", "BendSide", "Parameters", _tip_
).BendSide = ["Outside", "Inside", "Middle"]
_tip_ = translate("App::Property", "Wall Sketch object")
obj.addProperty(
"App::PropertyLink", "BendSketch", "Parameters", _tip_
).BendSketch = sketch
_tip_ = translate("App::Property", "Extrude Symmetric to Plane")
self._addProperties(obj)
obj.Proxy = self
def _addProperties(self, obj):
SheetMetalTools.smAddLengthProperty(
obj,
"Radius",
translate("App::Property", "Bend Radius"),
1.0
)
SheetMetalTools.smAddLengthProperty(
obj,
"Thickness",
translate("App::Property", "Thickness of sheetmetal"),
1.0
)
SheetMetalTools.smAddLengthProperty(
obj,
"Length",
translate("App::Property", "Length of wall"),
100.0
)
SheetMetalTools.smAddBoolProperty(
obj,
"MidPlane",
FreeCAD.Qt.translate("App::Property", "Extrude Symmetric to Plane"),
False
)
SheetMetalTools.smAddBoolProperty(
obj,
"Reverse",
FreeCAD.Qt.translate("App::Property", "Reverse Extrusion Direction"),
False
)
def execute(self, fp):
'''"Print a short message when doing a recomputation, this method is mandatory"'''
self._addProperties(fp)
s = smBase(
thk=fp.Thickness.Value,
length=fp.Length.Value,
radius=fp.Radius.Value,
Side=fp.BendSide,
midplane=fp.MidPlane,
reverse=fp.Reverse,
MainObject=fp.BendSketch,
)
fp.Shape = s
SheetMetalTools.smHideObjects(fp.BendSketch)
##########################################################################################################
# Gui code
##########################################################################################################
if SheetMetalTools.isGuiLoaded():
from FreeCAD import Gui
##########################################################################################################
# View Provider
##########################################################################################################
class SMBaseViewProvider(SheetMetalTools.SMViewProvider):
''' Part / Part WB style ViewProvider '''
def getIcon(self):
return os.path.join(icons_path, 'SheetMetal_AddBase.svg')
def claimChildren(self):
objs = []
if hasattr(self, "Object") and hasattr(self.Object, "BendSketch"):
objs.append(self.Object.BendSketch)
return objs
def getTaskPanel(self, obj):
return SMBaseBendTaskPanel(obj)
##########################################################################################################
# Task Panel
##########################################################################################################
class SMBaseBendTaskPanel:
'''A TaskPanel for the Sheetmetal base bend command'''
def __init__(self, obj):
self.obj = obj
self.form = SheetMetalTools.taskLoadUI("CreateBaseShape.ui")
self.updateDisplay()
SheetMetalTools.taskConnectSelectionSingle(
self, self.form.pushSketch, self.form.txtSketch, obj, "BendSketch", ("Sketcher::SketchObject", [])
)
SheetMetalTools.taskConnectSpin(self, self.form.spinRadius, "Radius")
SheetMetalTools.taskConnectSpin(self, self.form.spinThickness, "Thickness")
SheetMetalTools.taskConnectSpin(self, self.form.spinLength, "Length")
SheetMetalTools.taskConnectEnum(self, self.form.comboBendPlane, "BendSide")
SheetMetalTools.taskConnectCheck(self, self.form.checkSymetric, "MidPlane", self.midplaneChanged)
SheetMetalTools.taskConnectCheck(self, self.form.checkRevDirection, "Reverse")
def isAllowedAlterSelection(self):
return True
def isAllowedAlterView(self):
return True
def updateDisplay(self):
self.form.checkRevDirection.setVisible(self.obj.MidPlane is False)
def midplaneChanged(self, value):
self.updateDisplay()
def accept(self):
SheetMetalTools.taskAccept(self)
SheetMetalTools.taskSaveDefaults(self.obj, smBaseDefaults, ["Radius", "Thickness"])
return True
def reject(self):
SheetMetalTools.taskReject(self)
#def retranslateUi(self, SMBendTaskPanel):
##########################################################################################################
# Command
##########################################################################################################
class AddBaseCommandClass:
"""Add Base Wall command"""
def GetResources(self):
return {
"Pixmap": os.path.join(
icons_path, "SheetMetal_AddBase.svg"
), # the name of a svg file available in the resources
"MenuText": translate("SheetMetal", "Make Base Wall"),
"Accel": "C, B",
"ToolTip": translate(
"SheetMetal",
"Create a sheetmetal wall from a sketch\n"
"1. Select a Sketch to create bends with walls.\n"
"2. Use Property editor to modify other parameters",
),
}
def Activated(self):
selobj = Gui.Selection.getSelectionEx()[0].Object
newObj, activeBody = SheetMetalTools.smCreateNewObject(selobj, "BaseBend")
if newObj is None:
return
SMBaseBend(newObj, selobj)
SMBaseViewProvider(newObj.ViewObject)
SheetMetalTools.smAddNewObject(
selobj, newObj, activeBody, SMBaseBendTaskPanel)
return
def IsActive(self):
if len(Gui.Selection.getSelection()) != 1:
return False
selobj = Gui.Selection.getSelection()[0]
if not (
selobj.isDerivedFrom("Sketcher::SketchObject")
or selobj.isDerivedFrom("PartDesign::ShapeBinder")
or selobj.isDerivedFrom("PartDesign::SubShapeBinder")
):
return False
return True
Gui.addCommand("SheetMetal_AddBase", AddBaseCommandClass())