-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathffDesign_ZipTieChannels.py
More file actions
244 lines (195 loc) · 8.13 KB
/
ffDesign_ZipTieChannels.py
File metadata and controls
244 lines (195 loc) · 8.13 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
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_zip_tie_channel_settings(
body,
original,
*,
width: App.Units.Quantity,
):
Utils.assert_body(body)
width = App.Units.Quantity(width)
assert width.Unit.Type == "Length"
varset = body.newObject("App::VarSet", f"{original.Name}_ZipTieChannel_Settings")
varset.Label = f"{original.Label}_ZipTieChannel_Settings"
varset.addProperty("App::PropertyLength", "ChannelWidth", group="Base")
varset.ChannelWidth = width
varset.addProperty("App::PropertyAngle", "ChannelRotation", group="Base")
varset.ChannelRotation = "90 deg"
varset.recompute()
return varset
def make_zip_tie_channel_template(
body,
original,
*,
thickness: App.Units.Quantity,
bridge_dia: App.Units.Quantity,
):
Utils.assert_body(body)
sketch = body.newObject("Sketcher::SketchObject", f"{original.Name}_ZipTieChannel_Template")
sketch.Label = f"{original.Label}_ZipTieChannel_Template"
sketch.Visibility = False
thickness = App.Units.Quantity(thickness)
assert thickness.Unit.Type == "Length"
bridge_dia = App.Units.Quantity(bridge_dia)
assert bridge_dia.Unit.Type == "Length"
dist_inner = bridge_dia.Value / 2
dist_outer = dist_inner + thickness.Value
last_geo_id = len(sketch.Geometry)
new_geo = [
Part.ArcOfCircle(
Part.Circle(App.Vector(0, 0, 0), App.Vector(0, 0, 1), dist_outer),
math.pi * 1,
math.pi * 0,
),
Part.ArcOfCircle(
Part.Circle(App.Vector(0, 0, 0), App.Vector(0, 0, 1), dist_inner),
math.pi * 1,
math.pi * 0,
),
Part.LineSegment(App.Vector(-dist_inner, 0, 0), App.Vector(-dist_outer, 0, 0)),
Part.LineSegment(App.Vector(dist_inner, 0, 0), App.Vector(dist_outer, 0, 0)),
]
sketch.addGeometry(new_geo, False)
new_constraints = [
Sketcher.Constraint("Coincident", last_geo_id + 0, 1, last_geo_id + 2, 2),
Sketcher.Constraint("Coincident", last_geo_id + 1, 1, last_geo_id + 2, 1),
Sketcher.Constraint("Coincident", last_geo_id + 0, 2, last_geo_id + 3, 2),
Sketcher.Constraint("Coincident", last_geo_id + 1, 2, last_geo_id + 3, 1),
Sketcher.Constraint("Coincident", last_geo_id + 0, 3, -1, 1),
Sketcher.Constraint("Coincident", last_geo_id + 1, 3, -1, 1),
Sketcher.Constraint("PointOnObject", last_geo_id + 0, 1, -1),
Sketcher.Constraint("PointOnObject", last_geo_id + 0, 2, -1),
Sketcher.Constraint("PointOnObject", last_geo_id + 1, 1, -1),
Sketcher.Constraint("PointOnObject", last_geo_id + 1, 2, -1),
]
sketch.addConstraint(new_constraints)
last_c = len(sketch.Constraints)
new_constraints = [
Sketcher.Constraint("Diameter", last_geo_id + 1, bridge_dia.Value),
Sketcher.Constraint("DistanceX", last_geo_id + 0, 1, last_geo_id + 1, 1, thickness.Value),
]
sketch.addConstraint(new_constraints)
sketch.renameConstraint(last_c + 0, "ChannelBridgeDiameter")
sketch.renameConstraint(last_c + 1, "ChannelThickness")
sketch.recompute()
return sketch
def make_zip_tie_channel(body, original, template, settings, suffix: str, location: Utils.LocationExprSet):
Utils.assert_body(body)
Utils.assert_sketch(original)
Utils.assert_sketch(template)
Utils.assert_varset(settings)
binder = Utils.make_sketch_offset_shape_binder(
body,
template,
sketch=original,
suffix=suffix + "_Binder",
location=location,
rotation_expr=f"rotation({settings.Name}.ChannelRotation; 0; 90 deg)",
)
pocket = body.newObject("PartDesign::Pocket", original.Name + suffix)
pocket.Profile = (binder, "")
Utils.set_pocket_symmetric(pocket)
binder.Visibility = False
pocket.setExpression("Length", f"{settings.Name}.ChannelWidth")
pocket.Label = original.Label + suffix
pocket.recompute()
def find_points_in_sketch(sketch):
Utils.assert_sketch(sketch)
def is_valid_point(index, point):
return point.TypeId == "Part::GeomPoint" and not sketch.getConstruction(index)
return list(index for index, point in enumerate(sketch.Geometry) if is_valid_point(index, point))
def make_zip_tie_channels_from_sketch(
body,
sketch,
*,
width: App.Units.Quantity,
thickness: App.Units.Quantity,
bridge_dia: App.Units.Quantity,
):
Utils.assert_body(body)
Utils.assert_sketch(sketch)
settings = make_zip_tie_channel_settings(body, sketch, width=width)
template = make_zip_tie_channel_template(body, sketch, thickness=thickness, bridge_dia=bridge_dia)
for i, loc in enumerate(Utils.get_sketch_locations(sketch, Utils.MASK_PROFILE_POINTS)):
make_zip_tie_channel(
body,
sketch,
template,
settings,
suffix=f"_ZipTieChannel{i+1:03}",
location=loc,
)
sketch.Visibility = False
class ZipTieChannelsTaskPanel:
def __init__(self, body, sketch):
Utils.assert_body(body)
Utils.assert_sketch(sketch)
self.body = body
self.sketch = sketch
self.form = Gui.PySideUic.loadUi(Utils.Resources.get_panel("ffDesign_ZipTieChannels.ui"))
# TODO: Implement line ends
self.form.LineEnds.setEnabled(False)
self.updateMessage()
def updateMessage(self):
n_points = len(find_points_in_sketch(self.sketch))
color = "#008000" if n_points > 0 else "#800000"
self.form.InfoMessage.setTextFormat(QtCore.Qt.TextFormat.RichText)
self.form.InfoMessage.setText(f'<font color="{color}">Found {n_points} points in "{self.sketch.Label}"</font>')
# Default values
self.form.ChannelWidth.setProperty("rawValue", 3.5)
self.form.ChannelThickness.setProperty("rawValue", 1.5)
self.form.BridgeDiameter.setProperty("rawValue", 2.5)
def accept(self):
try:
Gui.Control.closeDialog()
if len(find_points_in_sketch(self.sketch)) == 0:
raise Utils.ffDesignError("Cannot generate zip tie channels: No points were found in the sketch!")
try:
if Utils.undo_shapebinder_is_safe():
App.ActiveDocument.openTransaction("Add zip tie channels")
make_zip_tie_channels_from_sketch(
self.body,
self.sketch,
width=self.form.ChannelWidth.property("value"),
thickness=self.form.ChannelThickness.property("value"),
bridge_dia=self.form.BridgeDiameter.property("value"),
)
App.ActiveDocument.recompute()
if Utils.undo_shapebinder_is_safe():
App.ActiveDocument.commitTransaction()
except Exception as e:
if Utils.undo_shapebinder_is_safe():
App.ActiveDocument.abortTransaction()
raise e from None
except Utils.ffDesignError as e:
e.emit_to_user()
def reject(self):
Gui.Control.closeDialog()
class ZipTieChannelsCommand:
def GetResources(self):
return {
"Pixmap": "icons:ffDesign_ZipTieChannels.svg",
"MenuText": App.Qt.translate("ffDesign", "Add zip tie channels"),
"ToolTip": App.Qt.translate(
"ffDesign",
"Add zip tie channels for easily fastening wires to a part.\n"
"1. Select a reference sketch which has points marking the locations of the zip tie channels.\n"
"2. Run this command.\n",
),
}
def Activated(self):
try:
sketch = Utils.get_selected_sketch()
body = Utils.get_active_part_design_body_for_feature(sketch)
dialog = ZipTieChannelsTaskPanel(body, sketch)
Gui.Control.showDialog(dialog)
except Utils.ffDesignError as e:
e.emit_to_user()
def IsActive(self):
return Utils.check_sketch_tool_preconditions()
Gui.addCommand("ffDesign_ZipTieChannels", ZipTieChannelsCommand())