-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathffDesign_Utils.py
More file actions
384 lines (283 loc) · 11.6 KB
/
ffDesign_Utils.py
File metadata and controls
384 lines (283 loc) · 11.6 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
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
import os
import dataclasses
from PySide import QtCore, QtGui
import FreeCADGui as Gui
import FreeCAD as App
class Resources:
mod_path = os.path.dirname(__file__)
icons_path = os.path.join(mod_path, "Resources", "icons")
panels_path = os.path.join(mod_path, "Resources", "panels")
@classmethod
def get_panel(cls, name: str) -> str:
path = os.path.join(cls.panels_path, name)
if not os.path.exists(path):
raise ffDesignError(f"Missing task panel {name!r}!")
return path
@classmethod
def register_search_paths(cls):
QtCore.QDir.addSearchPath("icons", cls.icons_path)
class Log:
addon = "FusedFilamentDesign"
@classmethod
def error(cls, msg: str) -> None:
App.Console.PrintError(f"[{cls.addon}] {msg}\n")
@classmethod
def warning(cls, msg: str) -> None:
App.Console.PrintWarning(f"[{cls.addon}] {msg}\n")
@classmethod
def info(cls, msg: str) -> None:
App.Console.PrintMessage(f"[{cls.addon}] {msg}\n")
class ffDesignError(Exception):
def __init__(self, message: str, *, dialog: bool = True):
self.message = message
self.dialog = dialog
super().__init__(message)
def emit_to_user(self):
Log.error(self.message)
if self.dialog:
# Also show as a modal dialog
QtGui.QMessageBox.warning(None, Log.addon, f"[{Log.addon}] {self.message}")
class ffDesignPreconditionError(ffDesignError):
"""A precondition for using a command was not met."""
pass
def warning_confirm_proceed(message: str, question: str = "Proceed anyway?"):
Log.warning(message)
reply = QtGui.QMessageBox.question(None, Log.addon, f"[{Log.addon}] {message}\n{question}")
if reply != QtGui.QMessageBox.Yes:
raise ffDesignError("Aborted on user request due to previous warning", dialog=False)
def assert_body(obj):
assert obj.TypeId == "PartDesign::Body"
def assert_hole(obj):
assert obj.TypeId == "PartDesign::Hole"
def assert_sketch(obj):
assert obj.TypeId == "Sketcher::SketchObject"
def assert_varset(obj):
assert obj.TypeId == "App::VarSet"
def get_active_part_design_body_for_feature(obj):
parent_body = obj.getParent()
active_body = Gui.ActiveDocument.ActiveView.getActiveObject("pdbody")
if active_body is None:
warning_confirm_proceed(
"No active PartDesign Body!",
f'Make "{parent_body.Label}" active?',
)
Gui.ActiveDocument.ActiveView.setActiveObject("pdbody", parent_body)
active_body = Gui.ActiveDocument.ActiveView.getActiveObject("pdbody")
if parent_body != active_body:
warning_confirm_proceed(
"Selected feature is not part of the active PartDesign body!",
f'Make "{parent_body.Label}" active?',
)
Gui.ActiveDocument.ActiveView.setActiveObject("pdbody", parent_body)
active_body = Gui.ActiveDocument.ActiveView.getActiveObject("pdbody")
assert_body(active_body)
return active_body
def get_selected_hole():
if not App.ActiveDocument:
raise ffDesignPreconditionError("No active document")
sel = Gui.Selection.getSelection()
if len(sel) == 1:
if sel[0].TypeId != "PartDesign::Hole":
raise ffDesignPreconditionError(
f"Selected object is not a PartDesign Hole feature (is a {sel[0].TypeId!r} instead)."
)
return sel[0]
active_body = Gui.ActiveDocument.ActiveView.getActiveObject("pdbody")
if len(sel) == 0 and active_body is not None:
if active_body.Tip is not None:
if active_body.Tip.TypeId != "PartDesign::Hole":
raise ffDesignPreconditionError(f"Tip of the active body is not a PartDesign Hole feature.")
return active_body.Tip
raise ffDesignPreconditionError("Exactly one Hole feature must be selected.")
def check_hole_tool_preconditions() -> bool:
try:
get_selected_hole()
return True
except ffDesignPreconditionError:
return False
def get_selected_sketch():
if not App.ActiveDocument:
raise ffDesignPreconditionError("No active document")
sel = Gui.Selection.getSelection()
if len(sel) != 1:
raise ffDesignPreconditionError("Exactly one Sketch must be selected.")
if sel[0].TypeId != "Sketcher::SketchObject":
raise ffDesignPreconditionError(f"Selected object is not a Sketch (is a {sel[0].TypeId!r} instead).")
return sel[0]
def check_sketch_tool_preconditions() -> bool:
try:
get_selected_sketch()
return True
except ffDesignPreconditionError:
return False
def hole_has_counterbore_maybe(hole) -> bool:
"""
Check if a Hole feature has a counterbore.
This check is True if it maybe has a counterbore, but it could also be a
countersink or counterdrill.
If this check is False, the hole definitely does not have any counterbore.
"""
assert_hole(hole)
return hole.HoleCutType != "None"
def hole_has_counterbore_sure(hole) -> bool:
"""
Check if a Hole feature has a counterbore.
This check is True the hole for sure has some type of counterbore.
If this check is False, the hole may still have a counterbore, but it could
also be a countersink, counterdrill or none at all.
"""
assert_hole(hole)
return hole.HoleCutType in [
"Counterbore",
"ISO 4762",
"ISO 14583 (partial)",
"DIN 7984",
"ISO 4762 + 7089",
"ISO 14583",
"ISO 12474",
]
def hole_prepare_layer_height_property(hole):
assert_hole(hole)
if "LayerHeight" not in hole.PropertiesList:
hole.addProperty("App::PropertyLength", "LayerHeight", group="FusedFilamentDesign")
# TODO: Add some configuration setting for the default layer height
hole.LayerHeight = "0.2 mm"
def get_hole_profile_sketch(hole):
assert_hole(hole)
if len(hole.Profile) < 1:
raise ffDesignError("Hole does not have a profile!")
# TODO: Check for list of profiles
profile_sketch = hole.Profile[0]
if profile_sketch.TypeId != "Sketcher::SketchObject":
raise ffDesignError("Hole profile must be a Sketch!")
return profile_sketch
def make_derived_sketch(body, original, suffix: str):
assert_body(body)
assert_sketch(original)
sketch = body.newObject("Sketcher::SketchObject", original.Name + suffix)
sketch.AttachmentSupport = [(original, "")]
sketch.MapMode = "ObjectXY"
sketch.Label = original.Label + suffix
sketch.recompute()
return sketch
# The `BaseProfileType` property introduced for `PartDesign_Hole` in
# FreeCAD 1.1 is a mask of the sketch features to be used. Any combination of
# the following bit-flags may be used.
MASK_PROFILE_POINTS = 1
MASK_PROFILE_CIRCLES = 2
MASK_PROFILE_ARCS = 4
def get_hole_profile_type(hole):
assert_hole(hole)
# In older FreeCAD versions where `BaseProfileType` did not exist, the
# `PartDesign_Hole` feature only used circles.
return getattr(hole, "BaseProfileType", MASK_PROFILE_CIRCLES)
@dataclasses.dataclass
class LocationExprSet:
vector_expr: str
x_expr: str
y_expr: str
def get_sketch_locations(sketch, profile_type):
assert_sketch(sketch)
def try_make_loc_expr_set(index, obj):
# Construction geometry is always ignored
if sketch.getConstruction(index):
return None
if (profile_type & MASK_PROFILE_POINTS) != 0:
if obj.TypeId == "Part::GeomPoint":
x_expr = f"{sketch.Name}.Geometry[{index}].X"
y_expr = f"{sketch.Name}.Geometry[{index}].Y"
return LocationExprSet(
vector_expr=f"vector({x_expr}, {y_expr}, 0)",
x_expr=f"{x_expr} * 1mm",
y_expr=f"{y_expr} * 1mm",
)
if (profile_type & MASK_PROFILE_CIRCLES) != 0:
if obj.TypeId == "Part::GeomCircle":
center = f"{sketch.Name}.Geometry[{index}].Center"
return LocationExprSet(
vector_expr=center,
x_expr=f"{center}.x * 1mm",
y_expr=f"{center}.y * 1mm",
)
if (profile_type & MASK_PROFILE_ARCS) != 0:
if obj.TypeId == "Part::GeomArcOfCircle":
center = f"{sketch.Name}.Geometry[{index}].Center"
return LocationExprSet(
vector_expr=center,
x_expr=f"{center}.x * 1mm",
y_expr=f"{center}.y * 1mm",
)
return None
return [h for h in (try_make_loc_expr_set(i, obj) for i, obj in enumerate(sketch.Geometry)) if h is not None]
def set_shape_binder_styles(binder):
binder.ViewObject.LineColor = (1.0, 0.84, 0.0, 0.60)
binder.ViewObject.PointColor = (1.0, 0.84, 0.0, 0.60)
m = binder.ViewObject.ShapeAppearance[0]
m.DiffuseColor = (1.0, 0.84, 0.0, 0.60)
binder.ViewObject.ShapeAppearance = (m,)
binder.ViewObject.Transparency = 60
def make_sketch_offset_shape_binder(body, template, sketch, suffix: str, location: LocationExprSet, rotation_expr: str):
assert_body(body)
assert_sketch(template)
assert_sketch(sketch)
shape_binder = body.newObject("PartDesign::SubShapeBinder", sketch.Name + suffix)
shape_binder.Support = (template, "")
shape_binder.Relative = False
shape_binder.Visibility = False
set_shape_binder_styles(shape_binder)
shape_binder.setExpression(
"Placement",
f"{sketch.Name}.Placement * placement({location.vector_expr}; {rotation_expr})",
)
shape_binder.Label = sketch.Label + suffix
return shape_binder
def int_or_zero(string) -> int:
try:
return int(string)
except ValueError:
return 0
def check_freecad_version(*, min_version) -> bool:
current = [int_or_zero(v.split()[0]) for v in App.Version()[:4]]
return current >= min_version
def undo_shapebinder_is_safe() -> bool:
"""
Undoing transactions where shape-binders are created is broken in FreeCAD
1.0 and a fix will be released in 1.1.
"""
return check_freecad_version(min_version=[1, 1, 0])
def set_pocket_two_lengths(pocket):
try:
# In more recent versions, a two-length pocket is created using `SideType`
# See https://github.com/FreeCAD/FreeCAD/pull/21794
pocket.SideType = "Two sides"
pocket.Type = "Length"
pocket.Type2 = "Length"
except AttributeError:
# Legacy code for FreeCAD 1.0 and early 1.1 development builds
pocket.Type = "TwoLengths"
def set_pocket_symmetric(pocket):
try:
# In more recent versions, a symmetric pocket is created using `SideType`
# See https://github.com/FreeCAD/FreeCAD/pull/21794
pocket.SideType = "Symmetric"
except AttributeError:
# Legacy code for FreeCAD 1.0 and early 1.1 development builds
pocket.Midplane = True
class ffDesignAboutCommand:
def GetResources(self):
return {
"Pixmap": "icons:ffDesign_Logo.svg",
"MenuText": App.Qt.translate("ffDesign", "FusedFilamentDesign"),
"ToolTip": App.Qt.translate("ffDesign", "About the FusedFilamentDesign addon."),
}
def Activated(self):
QtGui.QMessageBox.information(
None,
Log.addon,
"FusedFilamentDesign is a FreeCAD addon for FFF/FDM 3D-printing design. "
"It includes various tools to generate geometry for better printability of a part.\n"
"\n"
"Check the tooltip for each command to understand how to use them.",
)
Resources.register_search_paths()
Gui.addCommand("ffDesign_About", ffDesignAboutCommand())