-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtm4p_command.py
More file actions
517 lines (444 loc) · 18 KB
/
dtm4p_command.py
File metadata and controls
517 lines (444 loc) · 18 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
"""FreeCAD command and task-panel UI for the DTM4P Termination Housing tool."""
import os
import FreeCAD
import FreeCADGui
import Part
from PySide import QtWidgets, QtCore
_plugin_dir = os.path.dirname(os.path.abspath(__file__))
# ---------------------------------------------------------------------------
# FeaturePython proxy & ViewProvider (enables double-click re-edit)
# ---------------------------------------------------------------------------
class _DTM4PProxy:
"""Data proxy for a DTM4P PartDesign::FeatureAdditivePython."""
def __init__(self, obj):
obj.Proxy = self
def execute(self, obj):
"""Recompute the fused shape."""
FreeCAD.Console.PrintMessage(
"DTM4P.execute() called for {}\n".format(obj.Name))
if not hasattr(obj, "DTM4P_FaceName"):
FreeCAD.Console.PrintWarning(
"DTM4P.execute(): no DTM4P_FaceName property yet\n")
return
if hasattr(obj, "BaseFeature") and obj.BaseFeature is not None:
base_shape = obj.BaseFeature.Shape
elif hasattr(obj, "DTM4P_OriginalBody") and obj.DTM4P_OriginalBody:
original = FreeCAD.ActiveDocument.getObject(
obj.DTM4P_OriginalBody)
if original is None or original.Shape.isNull():
return
base_shape = original.Shape
else:
return
try:
from dtm4p_geometry import place_housing
face_name = obj.DTM4P_FaceName
if not face_name:
return
face_idx = int(face_name.replace("Face", ""))
if face_idx < 1 or face_idx > len(base_shape.Faces):
FreeCAD.Console.PrintWarning(
"DTM4P: {} not found on base shape "
"(has {} faces), keeping existing shape.\n"
.format(face_name, len(base_shape.Faces)))
return
face = base_shape.Faces[face_idx - 1]
# Resolve edge if stored
edge = None
edge_name = getattr(obj, "DTM4P_EdgeName", "")
if edge_name:
edge_idx = int(edge_name.replace("Edge", ""))
if 1 <= edge_idx <= len(base_shape.Edges):
edge = base_shape.Edges[edge_idx - 1]
new_shape = place_housing(
base_shape, face,
x_offset=getattr(obj, "DTM4P_XOffset", 0.0),
y_offset=getattr(obj, "DTM4P_YOffset", 0.0),
rotation=getattr(obj, "DTM4P_Rotation", 0.0),
edge=edge,
)
if new_shape and not new_shape.isNull():
new_shape.transformShape(
obj.Placement.inverse().toMatrix(), True)
obj.Shape = new_shape
if hasattr(obj, "AddSubShape"):
tool = new_shape.cut(base_shape)
tool.transformShape(
obj.Placement.inverse().toMatrix(), True)
obj.AddSubShape = tool
except Exception as e:
FreeCAD.Console.PrintError(
"DTM4P recompute failed: {}\n".format(e))
def __getstate__(self):
return None
def __setstate__(self, state):
return None
class _DTM4PViewProvider:
"""ViewProvider that enables double-click re-editing of DTM4P objects."""
def __init__(self, vobj):
vobj.Proxy = self
def attach(self, vobj):
self.Object = vobj.Object
def _open_edit_panel(self, obj):
if not hasattr(obj, "DTM4P_FaceName"):
return False
original = _get_dtm4p_base_object(obj)
if original is None:
QtWidgets.QMessageBox.warning(
None, "DTM4P",
"Cannot re-edit: the original body object was deleted.",
)
return True
prefill = {
"x_offset": getattr(obj, "DTM4P_XOffset", 0.0),
"y_offset": getattr(obj, "DTM4P_YOffset", 0.0),
"rotation": getattr(obj, "DTM4P_Rotation", 0.0),
}
edge_name = getattr(obj, "DTM4P_EdgeName", "")
panel = DTM4PTaskPanel(
original, obj.DTM4P_FaceName,
edge_name=edge_name,
edit_obj=obj, prefill=prefill,
)
FreeCADGui.Control.showDialog(panel)
return True
def doubleClicked(self, vobj):
try:
return self._open_edit_panel(vobj.Object)
except Exception as e:
FreeCAD.Console.PrintError(
"DTM4P doubleClicked error: {}\n".format(e))
return False
def setEdit(self, vobj, mode=0):
if mode != 0:
return False
try:
return self._open_edit_panel(vobj.Object)
except Exception as e:
FreeCAD.Console.PrintError(
"DTM4P setEdit error: {}\n".format(e))
return False
def unsetEdit(self, vobj, mode=0):
FreeCADGui.Control.closeDialog()
return True
def getIcon(self):
return os.path.join(
_plugin_dir, "resources", "icons", "DTM4P.svg"
)
def __getstate__(self):
return None
def __setstate__(self, state):
return None
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _find_body(obj):
"""Return the PartDesign::Body that *obj* belongs to, or None."""
if hasattr(obj, "TypeId") and obj.TypeId == "PartDesign::Body":
return obj
if hasattr(obj, "getParentGeoFeatureGroup"):
parent = obj.getParentGeoFeatureGroup()
if (parent is not None
and hasattr(parent, "TypeId")
and parent.TypeId == "PartDesign::Body"):
return parent
return None
def _add_dtm4p_properties(obj):
"""Add custom storage properties to a DTM4P feature object."""
obj.addProperty(
"App::PropertyFloat", "DTM4P_XOffset", "DTM4P",
"Offset along the face U axis (mm)")
obj.addProperty(
"App::PropertyFloat", "DTM4P_YOffset", "DTM4P",
"Offset along the face V axis (mm)")
obj.addProperty(
"App::PropertyFloat", "DTM4P_Rotation", "DTM4P",
"Rotation angle on the face (degrees)")
obj.addProperty(
"App::PropertyString", "DTM4P_FaceName", "DTM4P",
"Face used on the original body")
obj.addProperty(
"App::PropertyString", "DTM4P_EdgeName", "DTM4P",
"Edge used for front alignment (optional)")
obj.addProperty(
"App::PropertyString", "DTM4P_OriginalBody", "DTM4P",
"Original body object name")
def _get_dtm4p_base_object(obj):
"""Return the base object that a DTM4P result was derived from."""
if hasattr(obj, "BaseFeature") and obj.BaseFeature is not None:
return obj.BaseFeature
orig_name = getattr(obj, "DTM4P_OriginalBody", None)
if orig_name:
return FreeCAD.ActiveDocument.getObject(orig_name)
return None
# ---------------------------------------------------------------------------
# Task Panel (sidebar UI)
# ---------------------------------------------------------------------------
class DTM4PTaskPanel:
"""Task panel shown in the FreeCAD sidebar when the DTM4P command is active."""
def __init__(self, body_obj, face_name, edge_name="",
edit_obj=None, prefill=None):
self.body_obj = body_obj
self.face_name = face_name
self.edge_name = edge_name
self.edit_obj = edit_obj
self.form = self._build_ui()
if prefill:
self._apply_prefill(prefill)
def _build_ui(self):
widget = QtWidgets.QWidget()
layout = QtWidgets.QFormLayout(widget)
layout.setFieldGrowthPolicy(
QtWidgets.QFormLayout.AllNonFixedFieldsGrow
)
header = QtWidgets.QLabel("<b>DTM4P Termination Housing</b>")
header.setAlignment(QtCore.Qt.AlignCenter)
layout.addRow(header)
info = QtWidgets.QLabel(
"<i>Places a Deutsch DTM04-4P termination housing\n"
"onto the selected face, extending outward.</i>"
)
info.setWordWrap(True)
layout.addRow(info)
# Edge alignment info
if self.edge_name:
edge_label = QtWidgets.QLabel(
"<b>Front aligned to:</b> {}".format(self.edge_name)
)
layout.addRow(edge_label)
else:
edge_label = QtWidgets.QLabel(
"<i>Tip: Select a Face + Edge to align the\n"
"housing front to the edge.</i>"
)
edge_label.setWordWrap(True)
layout.addRow(edge_label)
sep = QtWidgets.QFrame()
sep.setFrameShape(QtWidgets.QFrame.HLine)
layout.addRow(sep)
self.x_offset_spin = QtWidgets.QDoubleSpinBox()
self.x_offset_spin.setRange(-500.0, 500.0)
self.x_offset_spin.setValue(0.0)
self.x_offset_spin.setSingleStep(1.0)
self.x_offset_spin.setDecimals(1)
self.x_offset_spin.setSuffix(" mm")
if self.edge_name:
self.x_offset_spin.setToolTip(
"Offset along the edge direction.")
layout.addRow("Along Edge:", self.x_offset_spin)
else:
self.x_offset_spin.setToolTip(
"Offset along the model X axis (projected onto the face).")
layout.addRow("X Offset:", self.x_offset_spin)
self.y_offset_spin = QtWidgets.QDoubleSpinBox()
self.y_offset_spin.setRange(-500.0, 500.0)
self.y_offset_spin.setValue(0.0)
self.y_offset_spin.setSingleStep(1.0)
self.y_offset_spin.setDecimals(1)
self.y_offset_spin.setSuffix(" mm")
if self.edge_name:
self.y_offset_spin.setToolTip(
"Offset perpendicular to the edge (positive = away from edge).")
layout.addRow("From Edge:", self.y_offset_spin)
else:
self.y_offset_spin.setToolTip(
"Offset along the model Y axis (projected onto the face).")
layout.addRow("Y Offset:", self.y_offset_spin)
self.rotation_spin = QtWidgets.QDoubleSpinBox()
self.rotation_spin.setRange(-180.0, 180.0)
self.rotation_spin.setValue(0.0)
self.rotation_spin.setSingleStep(5.0)
self.rotation_spin.setDecimals(1)
self.rotation_spin.setSuffix(" deg")
self.rotation_spin.setToolTip(
"Rotation angle on the face (degrees)."
)
layout.addRow("Rotation:", self.rotation_spin)
return widget
def _apply_prefill(self, p):
if "x_offset" in p:
self.x_offset_spin.setValue(p["x_offset"])
if "y_offset" in p:
self.y_offset_spin.setValue(p["y_offset"])
if "rotation" in p:
self.rotation_spin.setValue(p["rotation"])
def accept(self):
from dtm4p_geometry import place_housing
doc = FreeCAD.ActiveDocument
body_shape = self.body_obj.Shape
face = getattr(body_shape, self.face_name)
x_offset = self.x_offset_spin.value()
y_offset = self.y_offset_spin.value()
rotation = self.rotation_spin.value()
# Resolve edge
edge = None
if self.edge_name:
edge = getattr(body_shape, self.edge_name, None)
try:
new_shape = place_housing(
body_shape, face,
x_offset=x_offset,
y_offset=y_offset,
rotation=rotation,
edge=edge,
)
except Exception as e:
FreeCAD.Console.PrintError(
"DTM4P placement failed: {}\n".format(e)
)
QtWidgets.QMessageBox.critical(
None,
"DTM4P Error",
"Fuse operation failed:\n\n{}\n\n"
"Make sure you selected a flat face with enough "
"room for the housing.".format(e),
)
return False
body = _find_body(self.body_obj)
is_pd_edit = (self.edit_obj is not None
and hasattr(self.edit_obj, "BaseFeature"))
if is_pd_edit:
result_obj = self.edit_obj
result_obj.DTM4P_XOffset = x_offset
result_obj.DTM4P_YOffset = y_offset
result_obj.DTM4P_Rotation = rotation
result_obj.DTM4P_EdgeName = self.edge_name
elif body is not None:
result_obj = doc.addObject(
"PartDesign::FeatureAdditivePython", "DTM4P")
_DTM4PProxy(result_obj)
_DTM4PViewProvider(result_obj.ViewObject)
_add_dtm4p_properties(result_obj)
result_obj.DTM4P_XOffset = x_offset
result_obj.DTM4P_YOffset = y_offset
result_obj.DTM4P_Rotation = rotation
result_obj.DTM4P_FaceName = self.face_name
result_obj.DTM4P_EdgeName = self.edge_name
result_obj.DTM4P_OriginalBody = body.Name
body.addObject(result_obj)
else:
if self.edit_obj is not None:
doc.removeObject(self.edit_obj.Name)
result_obj = doc.addObject("Part::FeaturePython", "DTM4P")
_DTM4PProxy(result_obj)
_DTM4PViewProvider(result_obj.ViewObject)
_add_dtm4p_properties(result_obj)
result_obj.DTM4P_XOffset = x_offset
result_obj.DTM4P_YOffset = y_offset
result_obj.DTM4P_Rotation = rotation
result_obj.DTM4P_FaceName = self.face_name
result_obj.DTM4P_EdgeName = self.edge_name
result_obj.DTM4P_OriginalBody = self.body_obj.Name
result_obj.Shape = new_shape
self.body_obj.ViewObject.Visibility = False
if hasattr(self.body_obj, "ViewObject"):
src_vo = self.body_obj.ViewObject
dst_vo = result_obj.ViewObject
if hasattr(src_vo, "ShapeColor"):
dst_vo.ShapeColor = src_vo.ShapeColor
if hasattr(src_vo, "Transparency"):
dst_vo.Transparency = src_vo.Transparency
doc.recompute()
FreeCADGui.Control.closeDialog()
FreeCAD.Console.PrintMessage(
"DTM4P housing placed onto {}.{}{}\n"
.format(self.body_obj.Label, self.face_name,
" aligned to " + self.edge_name if self.edge_name else "")
)
return True
def reject(self):
FreeCADGui.Control.closeDialog()
return True
def getStandardButtons(self):
return (
QtWidgets.QDialogButtonBox.Ok
| QtWidgets.QDialogButtonBox.Cancel
)
# ---------------------------------------------------------------------------
# FreeCAD Command
# ---------------------------------------------------------------------------
class DTM4PCommand:
"""FreeCAD command that places a DTM4P termination housing onto a face."""
def GetResources(self):
return {
"Pixmap": os.path.join(
_plugin_dir, "resources", "icons", "DTM4P.svg"
),
"MenuText": "Place DTM4P Housing",
"ToolTip": (
"DTM4P\n"
"Place a Deutsch DTM04-4P termination housing onto the\n"
"selected flat face, extending outward from the surface.\n"
"Select a Face + Edge to align the housing front to the edge.\n"
"Select an existing DTM4P result to re-edit."
),
}
def IsActive(self):
sel = FreeCADGui.Selection.getSelectionEx()
if len(sel) != 1:
return False
obj = sel[0].Object
if hasattr(obj, "DTM4P_FaceName"):
return True
if not sel[0].SubElementNames:
return False
# Active if at least one Face is selected
return any(n.startswith("Face") for n in sel[0].SubElementNames)
def Activated(self):
sel = FreeCADGui.Selection.getSelectionEx()
if not sel:
return
obj = sel[0].Object
sub_names = sel[0].SubElementNames
has_face_sub = (
bool(sub_names) and any(n.startswith("Face") for n in sub_names)
)
# --- Re-edit an existing DTM4P result (only when no face is selected) ---
if hasattr(obj, "DTM4P_FaceName") and not has_face_sub:
original = _get_dtm4p_base_object(obj)
if original is None:
QtWidgets.QMessageBox.warning(
None, "DTM4P",
"Cannot re-edit: the original body object was deleted.",
)
return
prefill = {
"x_offset": getattr(obj, "DTM4P_XOffset", 0.0),
"y_offset": getattr(obj, "DTM4P_YOffset", 0.0),
"rotation": getattr(obj, "DTM4P_Rotation", 0.0),
}
edge_name = getattr(obj, "DTM4P_EdgeName", "")
panel = DTM4PTaskPanel(
original, obj.DTM4P_FaceName,
edge_name=edge_name,
edit_obj=obj, prefill=prefill,
)
FreeCADGui.Control.showDialog(panel)
return
# --- New placement: extract face and optional edge ---
face_name = ""
edge_name = ""
for name in sub_names:
if name.startswith("Face") and not face_name:
face_name = name
elif name.startswith("Edge") and not edge_name:
edge_name = name
if not face_name:
return
face = getattr(obj.Shape, face_name)
# Validate: face must be planar
surface = face.Surface
is_planar = hasattr(surface, "Axis") or isinstance(
surface, Part.Plane
)
if not is_planar:
QtWidgets.QMessageBox.warning(
None,
"Non-Planar Face",
"Please select a flat (planar) face.\n"
"The DTM4P housing can only be placed on flat surfaces.",
)
return
panel = DTM4PTaskPanel(obj, face_name, edge_name=edge_name)
FreeCADGui.Control.showDialog(panel)
FreeCADGui.addCommand("TrailCurrent_DTM4P", DTM4PCommand())