-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathffDesign_Teardrop.py
More file actions
254 lines (215 loc) · 8.19 KB
/
Copy pathffDesign_Teardrop.py
File metadata and controls
254 lines (215 loc) · 8.19 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
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 _single_tear(
sketch,
hole_loc: Utils.LocationExprSet,
diameter_expr: str,
angle_expr: str,
rotation_expr: str,
):
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, 2, 0)),
Part.LineSegment(App.Vector(1, 1, 0), App.Vector(0, 2, 0)),
Part.LineSegment(App.Vector(0, 0, 0), App.Vector(0, 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 + 2, 2),
Sketcher.Constraint("Coincident", last_geo_id + 1, 2, last_geo_id + 3, 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),
]
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),
]
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}")
sketch.setExpression(f"Constraints[{last_c + 4}]", f"{rotation_expr}")
return last_geo_id, last_c
def make_parametric_teardrop(
sketch,
*,
hole_loc: Utils.LocationExprSet,
diameter_expr: str,
angle_expr: str,
rotation_expr: str,
double_sided: bool = False,
):
Utils.assert_sketch(sketch)
last_geo_id, last_c = _single_tear(
sketch, hole_loc, diameter_expr, angle_expr, rotation_expr
)
if double_sided:
last_geo_id2, last_c2 = _single_tear(
sketch, hole_loc, diameter_expr, angle_expr, f"({rotation_expr}) + 180 deg"
)
sketch.delConstraint(last_c2 - 2)
sketch.delConstraint(last_c - 2)
double_constraints = [
Sketcher.Constraint("Tangent", last_geo_id + 0, 1, last_geo_id2 + 1, 1),
Sketcher.Constraint("Tangent", last_geo_id2 + 0, 1, last_geo_id + 1, 1),
]
sketch.addConstraint(double_constraints)
sketch.recompute()
def make_teardrops(
body,
hole,
*,
angle: App.Units.Quantity,
rotation: App.Units.Quantity,
do_counterbore: bool = False,
double_sided: bool = False,
):
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"
if "TeardropAngle" not in hole.PropertiesList:
hole.addProperty(
"App::PropertyAngle", "TeardropAngle", group="FusedFilamentDesign"
)
hole.TeardropAngle = angle
if "TeardropRotation" not in hole.PropertiesList:
hole.addProperty(
"App::PropertyAngle", "TeardropRotation", group="FusedFilamentDesign"
)
hole.TeardropRotation = rotation
profile_sketch = Utils.get_hole_profile_sketch(hole)
teardrop_sketch = Utils.make_derived_sketch(body, profile_sketch, "_Teardrops")
hole_locations = Utils.get_sketch_locations(
profile_sketch, Utils.get_hole_profile_type(hole)
)
for hole_loc in hole_locations:
make_parametric_teardrop(
teardrop_sketch,
hole_loc=hole_loc,
diameter_expr=f"{hole.Name}.Diameter",
angle_expr=f"{hole.Name}.TeardropAngle",
rotation_expr=f"{hole.Name}.TeardropRotation",
double_sided=double_sided,
)
pocket = body.newObject("PartDesign::Pocket", f"{hole.Name}_Teardrops")
pocket.Profile = (teardrop_sketch, "")
pocket.ReferenceAxis = (teardrop_sketch, ["N_Axis"])
pocket.Reversed = hole.Reversed
teardrop_sketch.Visibility = False
pocket.setExpression("Type", f"{hole.Name}.DepthType")
pocket.setExpression("Length", f"{hole.Name}.Depth")
pocket.Label = f"{hole.Label}_Teardrops"
pocket.recompute()
if not do_counterbore or not Utils.hole_has_counterbore_maybe(hole):
return
teardrops_cb_sketch = Utils.make_derived_sketch(
body, profile_sketch, "_TeardropsCb"
)
for hole_loc in hole_locations:
make_parametric_teardrop(
teardrops_cb_sketch,
hole_loc=hole_loc,
diameter_expr=f"{hole.Name}.HoleCutDiameter",
angle_expr=f"{hole.Name}.TeardropAngle",
rotation_expr=f"{hole.Name}.TeardropRotation",
double_sided=double_sided,
)
pocket = body.newObject("PartDesign::Pocket", f"{hole.Name}_TeardropsCb")
pocket.Profile = (teardrops_cb_sketch, "")
pocket.ReferenceAxis = (teardrops_cb_sketch, ["N_Axis"])
pocket.Reversed = hole.Reversed
teardrops_cb_sketch.Visibility = False
pocket.setExpression("Length", f"{hole.Name}.HoleCutDepth")
pocket.Label = f"{hole.Label}_TeardropsCb"
pocket.recompute()
class TeardropTaskPanel:
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_Teardrop.ui")
)
# Default is 120° teardrop angle
self.form.Angle120.toggle()
def accept(self):
try:
angle = "120 deg"
if self.form.Angle90.isChecked():
angle = "90 deg"
elif self.form.Angle120.isChecked():
angle = "120 deg"
do_counterbore = (
self.form.DoCounterbore.checkState() == QtCore.Qt.CheckState.Checked
)
double_sided = (
self.form.DoubleSided.checkState() == QtCore.Qt.CheckState.Checked
)
Gui.Control.closeDialog()
try:
App.ActiveDocument.openTransaction("Add teardrop hole")
make_teardrops(
self.body,
self.hole,
angle=angle,
rotation="90 deg",
do_counterbore=do_counterbore,
double_sided=double_sided,
)
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 TeardropCommand:
def GetResources(self):
return {
"Pixmap": "icons:ffDesign_Teardrop.svg",
"MenuText": App.Qt.translate("ffDesign", "Add teardrop shape"),
"ToolTip": App.Qt.translate(
"ffDesign",
"Add a teardrop shape to a hole.\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 = TeardropTaskPanel(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_Teardrop", TeardropCommand())