-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathffDesign_RoofBridge.py
More file actions
281 lines (238 loc) · 10.9 KB
/
Copy pathffDesign_RoofBridge.py
File metadata and controls
281 lines (238 loc) · 10.9 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
import math
from PySide import QtCore, QtGui
import FreeCADGui as Gui
import FreeCAD as App
import Part
import Sketcher
import ffDesign_Utils as Utils
def make_parametric_roof_bridge(
sketch,
*,
hole_loc: Utils.LocationExprSet,
diameter_expr: str,
angle_expr: str,
rotation_expr: str,
clearance_expr: str,
):
Utils.assert_sketch(sketch)
last_geo_id = len(sketch.Geometry)
new_geo = [
Part.ArcOfCircle(
Part.Circle(App.Vector(0, 0, 0), App.Vector(0, 0, 1), 1),
math.pi / 4 * 3,
math.pi / 4 * 1,
),
Part.LineSegment(App.Vector(-1, 1, 0), App.Vector(-0.5, 2, 0)),
Part.LineSegment(App.Vector(1, 1, 0), App.Vector(0.5, 2, 0)),
Part.LineSegment(App.Vector(0, 0, 0), App.Vector(0, 2, 0)),
Part.LineSegment(App.Vector(-0.5, 2, 0), App.Vector(0.5, 2, 0)),
]
sketch.addGeometry(new_geo, False)
sketch.toggleConstruction(last_geo_id + 3)
new_constraints = [
Sketcher.Constraint("Coincident", last_geo_id + 1, 2, last_geo_id + 4, 1),
Sketcher.Constraint("Coincident", last_geo_id + 2, 2, last_geo_id + 4, 2),
Sketcher.Constraint("Coincident", last_geo_id + 0, 3, last_geo_id + 3, 1),
Sketcher.Constraint("Tangent", last_geo_id + 0, 1, last_geo_id + 1, 1),
Sketcher.Constraint("Tangent", last_geo_id + 0, 2, last_geo_id + 2, 1),
Sketcher.Constraint("Symmetric", last_geo_id + 4, 1, last_geo_id + 4, 2, last_geo_id + 3, 2),
Sketcher.Constraint("Perpendicular", last_geo_id + 4, last_geo_id + 3),
]
sketch.addConstraint(new_constraints)
last_c = len(sketch.Constraints)
new_constraints = [
Sketcher.Constraint("DistanceX", last_geo_id + 0, 3, 0),
Sketcher.Constraint("DistanceY", last_geo_id + 0, 3, 0),
Sketcher.Constraint("Diameter", last_geo_id + 0, 2),
Sketcher.Constraint("Angle", last_geo_id + 1, 2, last_geo_id + 2, 2, math.pi / 2),
Sketcher.Constraint("Angle", last_geo_id + 3, math.pi / 2),
Sketcher.Constraint("Distance", last_geo_id + 3, 1, last_geo_id + 3, 2, 2),
]
sketch.addConstraint(new_constraints)
sketch.setExpression(f"Constraints[{last_c + 0}]", f"{hole_loc.x_expr}")
sketch.setExpression(f"Constraints[{last_c + 1}]", f"{hole_loc.y_expr}")
sketch.setExpression(f"Constraints[{last_c + 2}]", f"{diameter_expr}")
sketch.setExpression(f"Constraints[{last_c + 3}]", f"{angle_expr} * 2")
sketch.setExpression(f"Constraints[{last_c + 4}]", f"{rotation_expr}")
sketch.setExpression(f"Constraints[{last_c + 5}]", f"{diameter_expr} / 2 + {clearance_expr}")
sketch.recompute()
def make_roof_bridges(
body,
hole,
*,
angle: App.Units.Quantity,
rotation: App.Units.Quantity,
do_counterbore: bool,
do_doublesided: bool,
bridge_clearance: App.Units.Quantity,
):
Utils.assert_body(body)
Utils.assert_hole(hole)
angle = App.Units.Quantity(angle)
assert angle.Unit.Type == "Angle"
rotation = App.Units.Quantity(rotation)
assert rotation.Unit.Type == "Angle"
bridge_clearance = App.Units.Quantity(bridge_clearance)
assert bridge_clearance.Unit.Type == "Length"
if "RoofBridgeOverhangAngle" not in hole.PropertiesList:
hole.addProperty("App::PropertyAngle", "RoofBridgeOverhangAngle", group="FusedFilamentDesign")
hole.RoofBridgeOverhangAngle = angle
if "RoofBridgeRotation" not in hole.PropertiesList:
hole.addProperty("App::PropertyAngle", "RoofBridgeRotation", group="FusedFilamentDesign")
hole.RoofBridgeRotation = rotation
if "RoofBridgeClearance" not in hole.PropertiesList:
hole.addProperty("App::PropertyLength", "RoofBridgeClearance", group="FusedFilamentDesign")
hole.RoofBridgeClearance = bridge_clearance
profile_sketch = Utils.get_hole_profile_sketch(hole)
roofbridge_sketch = Utils.make_derived_sketch(body, profile_sketch, "_RoofBridge")
roofbridge_sketch_other_side = None
if do_doublesided:
roofbridge_sketch_other_side = Utils.make_derived_sketch(body, profile_sketch, "_RoofBridge2")
hole_locations = Utils.get_sketch_locations(profile_sketch, Utils.get_hole_profile_type(hole))
for hole_loc in hole_locations:
make_parametric_roof_bridge(
roofbridge_sketch,
hole_loc=hole_loc,
diameter_expr=f"{hole.Name}.Diameter",
angle_expr=f"{hole.Name}.RoofBridgeOverhangAngle",
rotation_expr=f"{hole.Name}.RoofBridgeRotation",
clearance_expr=f"{hole.Name}.RoofBridgeClearance",
)
if roofbridge_sketch_other_side is not None:
make_parametric_roof_bridge(
roofbridge_sketch_other_side,
hole_loc=hole_loc,
diameter_expr=f"{hole.Name}.Diameter",
angle_expr=f"{hole.Name}.RoofBridgeOverhangAngle",
rotation_expr=f"{hole.Name}.RoofBridgeRotation + 180 deg",
clearance_expr=f"{hole.Name}.RoofBridgeClearance",
)
pocket = body.newObject("PartDesign::Pocket", f"{hole.Name}_RoofBridge")
pocket.Profile = (roofbridge_sketch, "")
pocket.ReferenceAxis = (roofbridge_sketch, ["N_Axis"])
pocket.Reversed = hole.Reversed
roofbridge_sketch.Visibility = False
pocket.setExpression("Type", f"{hole.Name}.DepthType")
pocket.setExpression("Length", f"{hole.Name}.Depth")
pocket.Label = f"{hole.Label}_RoofBridge"
pocket.recompute()
if roofbridge_sketch_other_side is not None:
pocket = body.newObject("PartDesign::Pocket", f"{hole.Name}_RoofBridge2")
pocket.Profile = (roofbridge_sketch_other_side, "")
pocket.ReferenceAxis = (roofbridge_sketch_other_side, ["N_Axis"])
pocket.Reversed = hole.Reversed
roofbridge_sketch_other_side.Visibility = False
pocket.setExpression("Type", f"{hole.Name}.DepthType")
pocket.setExpression("Length", f"{hole.Name}.Depth")
pocket.Label = f"{hole.Label}_RoofBridge2"
pocket.recompute()
if not do_counterbore or not Utils.hole_has_counterbore_maybe(hole):
return
roofbridge_cb_sketch = Utils.make_derived_sketch(body, profile_sketch, "_RoofBridgeCb")
roofbridge_cb_sketch_other_side = None
if do_doublesided:
roofbridge_cb_sketch_other_side = Utils.make_derived_sketch(body, profile_sketch, "_RoofBridgeCb2")
for hole_loc in hole_locations:
make_parametric_roof_bridge(
roofbridge_cb_sketch,
hole_loc=hole_loc,
diameter_expr=f"{hole.Name}.HoleCutDiameter",
angle_expr=f"{hole.Name}.RoofBridgeOverhangAngle",
rotation_expr=f"{hole.Name}.RoofBridgeRotation",
clearance_expr=f"{hole.Name}.RoofBridgeClearance",
)
if roofbridge_cb_sketch_other_side is not None:
make_parametric_roof_bridge(
roofbridge_cb_sketch_other_side,
hole_loc=hole_loc,
diameter_expr=f"{hole.Name}.HoleCutDiameter",
angle_expr=f"{hole.Name}.RoofBridgeOverhangAngle",
rotation_expr=f"{hole.Name}.RoofBridgeRotation + 180 deg",
clearance_expr=f"{hole.Name}.RoofBridgeClearance",
)
pocket = body.newObject("PartDesign::Pocket", f"{hole.Name}_RoofBridgeCb")
pocket.Profile = (roofbridge_cb_sketch, "")
pocket.ReferenceAxis = (roofbridge_cb_sketch, ["N_Axis"])
pocket.Reversed = hole.Reversed
roofbridge_cb_sketch.Visibility = False
pocket.setExpression("Length", f"{hole.Name}.HoleCutDepth")
pocket.Label = f"{hole.Label}_RoofBridgeCb"
pocket.recompute()
if roofbridge_cb_sketch_other_side is not None:
pocket = body.newObject("PartDesign::Pocket", f"{hole.Name}_RoofBridgeCb2")
pocket.Profile = (roofbridge_cb_sketch_other_side, "")
pocket.ReferenceAxis = (roofbridge_cb_sketch_other_side, ["N_Axis"])
pocket.Reversed = hole.Reversed
roofbridge_cb_sketch_other_side.Visibility = False
pocket.setExpression("Length", f"{hole.Name}.HoleCutDepth")
pocket.Label = f"{hole.Label}_RoofBridgeCb2"
pocket.recompute()
class RoofBridgeTaskPanel:
def __init__(self, body, hole):
Utils.assert_body(body)
Utils.assert_hole(hole)
self.body = body
self.hole = hole
self.form = Gui.PySideUic.loadUi(Utils.Resources.get_panel("ffDesign_RoofBridge.ui"))
has_counterbore = Utils.hole_has_counterbore_maybe(hole)
self.form.DoCounterbore.setEnabled(has_counterbore)
if Utils.hole_has_counterbore_sure(hole):
self.form.DoCounterbore.setCheckState(QtCore.Qt.CheckState.Checked)
self.form.BridgeClearance.setProperty("rawValue", 0.2)
# Default is 45° overhang angle
self.form.Angle45.toggle()
def accept(self):
try:
angle = "45 deg"
if self.form.Angle45.isChecked():
angle = "45 deg"
elif self.form.Angle60.isChecked():
angle = "60 deg"
do_counterbore = self.form.DoCounterbore.checkState() == QtCore.Qt.CheckState.Checked
do_doublesided = self.form.DoubleSided.checkState() == QtCore.Qt.CheckState.Checked
bridge_clearance = self.form.BridgeClearance.property("value")
Gui.Control.closeDialog()
try:
App.ActiveDocument.openTransaction("Add roof bridge")
make_roof_bridges(
self.body,
self.hole,
angle=angle,
rotation="90 deg",
do_counterbore=do_counterbore,
do_doublesided=do_doublesided,
bridge_clearance=bridge_clearance,
)
App.ActiveDocument.recompute()
except Exception as e:
App.ActiveDocument.abortTransaction()
raise e from None
else:
App.ActiveDocument.commitTransaction()
except Utils.ffDesignError as e:
e.emit_to_user()
def reject(self):
Gui.Control.closeDialog()
class RoofBridgeCommand:
def GetResources(self):
return {
"Pixmap": "icons:ffDesign_RoofBridge.svg",
"MenuText": App.Qt.translate("ffDesign", "Add roof bridge"),
"ToolTip": App.Qt.translate(
"ffDesign",
"Add a roof bridge to a hole to avoid a steep overhang.\n"
"1. Select a Hole feature in the active body.\n"
"2. Run this command.\n",
),
}
def Activated(self):
try:
hole = Utils.get_selected_hole()
body = Utils.get_active_part_design_body_for_feature(hole)
dialog = RoofBridgeTaskPanel(body, hole)
Gui.Control.showDialog(dialog)
except Utils.ffDesignError as e:
e.emit_to_user()
def IsActive(self):
return Utils.check_hole_tool_preconditions()
Gui.addCommand("ffDesign_RoofBridge", RoofBridgeCommand())