Repository navigation
Expand file tree
/
Copy pathreaction.py
More file actions
229 lines (187 loc) · 8.17 KB
/
Copy pathreaction.py
File metadata and controls
229 lines (187 loc) · 8.17 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
import FreeCAD as App
import FreeCADGui as Gui
import Part, os
from eds_config import module_path
try:
from PySide2 import QtWidgets
except ImportError:
from PySide import QtWidgets
ICONPATH = os.path.join(module_path, "resources")
class Reaction:
"""Reads reaction data stored by CalcFEM and shows App::Annotation labels."""
def __init__(self, obj, objCalc):
obj.Proxy = self
self._label_names = [] # track annotation object names for cleanup
obj.addProperty("App::PropertyLink", "ObjectBaseCalc", "Base",
"Calculation object with analysis results").ObjectBaseCalc = objCalc
obj.addProperty("App::PropertyBool", "ShowForces", "Display",
"Show force reaction labels").ShowForces = True
obj.addProperty("App::PropertyBool", "ShowMoments", "Display",
"Show moment reaction labels").ShowMoments = True
obj.addProperty("App::PropertyInteger", "FontSize", "Display",
"Annotation font size").FontSize = 12
obj.addProperty("App::PropertyInteger", "Precision", "Display",
"Decimal places").Precision = 2
# ── helpers ───────────────────────────────────────────────────────────────
def _clear_labels(self, obj):
doc = obj.Document if obj.Document else App.ActiveDocument
if doc is None:
return
for name in list(self._label_names):
try:
ann = doc.getObject(name)
if ann is not None:
doc.removeObject(name)
except Exception:
pass
self._label_names.clear()
def _create_annotation(self, obj, pos, lines, tag):
doc = obj.Document if obj.Document else App.ActiveDocument
if doc is None:
return
# sanitise tag so it becomes a valid FreeCAD object name
safe = "".join(c if c.isalnum() or c == '_' else '_' for c in tag)
name = f"RxnLabel_{safe}"
# avoid name collision
existing = doc.getObject(name)
if existing is not None:
try:
doc.removeObject(name)
except Exception:
pass
ann = doc.addObject("App::Annotation", name)
ann.LabelText = lines
ann.Position = pos
if hasattr(ann, 'ViewObject') and ann.ViewObject is not None:
vobj = ann.ViewObject
try:
vobj.FontSize = obj.FontSize
except Exception:
pass
try:
vobj.TextColor = (0.0, 0.0, 0.8) # blue
except Exception:
pass
try:
vobj.Justification = "Left"
except Exception:
pass
self._label_names.append(name)
# ── main execute ──────────────────────────────────────────────────────────
def execute(self, obj):
self._clear_labels(obj)
calc_obj = obj.ObjectBaseCalc
missing = [p for p in ('ReactionForces', 'ReactionPositions', 'ReactionLabels')
if not hasattr(calc_obj, p) or not getattr(calc_obj, p)]
if missing:
App.Console.PrintWarning(
"Reaction: no reaction data – run CalcFEM analysis first.\n")
obj.Shape = Part.Shape()
return
forces_data = calc_obj.ReactionForces
moments_data = getattr(calc_obj, 'ReactionMoments', [])
positions = calc_obj.ReactionPositions
labels = calc_obj.ReactionLabels
n_supports = min(len(forces_data), len(positions))
prec = obj.Precision
print("\n" + "=" * 60)
print("REACTION RESULTS")
print("=" * 60)
for i in range(n_supports):
pos = positions[i]
label = labels[i] if i < len(labels) else f"Support{i + 1}"
Fx, Fy, Fz = (float(v) for v in forces_data[i].split(','))
Tx = Ty = Tz = 0.0
if i < len(moments_data) and moments_data[i]:
Tx, Ty, Tz = (float(v) for v in moments_data[i].split(','))
print(f" {label}: "
f"Fx={Fx:.{prec}f} Fy={Fy:.{prec}f} Fz={Fz:.{prec}f} kN | "
f"Tx={Tx:.{prec}f} Ty={Ty:.{prec}f} Tz={Tz:.{prec}f} kNm")
# Build annotation lines
ann_lines = [label]
if obj.ShowForces:
if abs(Fx) > 1e-3:
ann_lines.append(f"Fx={Fx:.{prec}f} kN")
if abs(Fy) > 1e-3:
ann_lines.append(f"Fy={Fy:.{prec}f} kN")
if abs(Fz) > 1e-3:
ann_lines.append(f"Fz={Fz:.{prec}f} kN")
if obj.ShowMoments:
if abs(Tx) > 1e-3:
ann_lines.append(f"Tx={Tx:.{prec}f} kNm")
if abs(Ty) > 1e-3:
ann_lines.append(f"Ty={Ty:.{prec}f} kNm")
if abs(Tz) > 1e-3:
ann_lines.append(f"Tz={Tz:.{prec}f} kNm")
self._create_annotation(obj, App.Vector(pos.x, pos.y, pos.z),
ann_lines, label)
print("=" * 60 + "\n")
# The FeaturePython shape itself is empty — visuals are pure annotations
obj.Shape = Part.Shape()
def onChanged(self, obj, prop):
if prop in ('ShowForces', 'ShowMoments', 'FontSize', 'Precision'):
if hasattr(obj, 'ObjectBaseCalc') and obj.ObjectBaseCalc:
self.execute(obj)
class ViewProviderReaction:
def __init__(self, vobj):
vobj.Proxy = self
def getIcon(self):
icon = os.path.join(ICONPATH, "icons/reaction.svg")
if not os.path.exists(icon):
icon = os.path.join(ICONPATH, "reaction.svg")
return icon
def attach(self, vobj):
self.ViewObject = vobj
self.Object = vobj.Object
def updateData(self, obj, prop): pass
def onChanged(self, vobj, prop): pass
def getDisplayModes(self, obj): return ["Shaded", "Wireframe"]
def getDefaultDisplayMode(self): return "Shaded"
def setDisplayMode(self, mode): return mode
class CommandReaction:
def GetResources(self):
icon = os.path.join(ICONPATH, "icons/reaction.svg")
if not os.path.exists(icon):
icon = os.path.join(ICONPATH, "reaction.svg")
return {
'Pixmap': icon,
'MenuText': "Reaction Forces",
'ToolTip': "Display support reaction forces and moments"
}
def Activated(self):
calc_obj = None
for o in Gui.Selection.getSelection():
if hasattr(o, 'NameMembers'):
calc_obj = o
break
if not calc_obj:
for o in App.ActiveDocument.Objects:
if hasattr(o, 'NameMembers'):
calc_obj = o
break
if not calc_obj:
QtWidgets.QMessageBox.warning(
None, "Reaction",
"No CalcFEM object found. Select it or run the analysis first.")
return
if not getattr(calc_obj, 'NameMembers', None):
QtWidgets.QMessageBox.warning(
None, "Reaction",
"No analysis results found. Run structural analysis first.")
return
existing = [o for o in App.ActiveDocument.Objects
if hasattr(o, 'Proxy') and isinstance(o.Proxy, Reaction)]
if existing:
reaction_obj = existing[0]
reaction_obj.ObjectBaseCalc = calc_obj
reaction_obj.Proxy.execute(reaction_obj)
else:
reaction_obj = App.ActiveDocument.addObject(
"Part::FeaturePython", "ReactionForces")
Reaction(reaction_obj, calc_obj)
ViewProviderReaction(reaction_obj.ViewObject)
App.ActiveDocument.recompute()
def IsActive(self):
return App.ActiveDocument is not None
if hasattr(Gui, 'addCommand'):
Gui.addCommand('ReactionResults', CommandReaction())