Repository navigation
Expand file tree
/
Copy pathget_profile.py
More file actions
170 lines (150 loc) · 8.76 KB
/
Copy pathget_profile.py
File metadata and controls
170 lines (150 loc) · 8.76 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
"""
Create an edsFEM profile-like object from FreeCAD section and material
"""
import FreeCAD as App
import FreeCADGui as Gui
def create_edsfem_profile_from_freecad(self, section_obj, mat_obj):
"""
Create an edsFEM profile-like object from FreeCAD section and material.
This creates a simple class that edsFEM can use.
"""
def safe_get_value(prop, unit, prop_name="unknown"):
"""Safely get value - handle both Quantity and plain float/int"""
if prop is None:
print(f" Warning: {prop_name} is None, using 0.0")
return 0.0
# If it's already a number, just convert to float
if isinstance(prop, (int, float)):
print(f" {prop_name}: {prop} (already float/int)")
return float(prop)
# If it has getValueAs method (Quantity object)
if hasattr(prop, 'getValueAs'):
val = float(prop.getValueAs(unit))
print(f" {prop_name}: {val} {unit} (from Quantity)")
return val
# Try to convert directly
try:
val = float(prop)
print(f" {prop_name}: {val} (converted to float)")
return val
except:
print(f" Warning: Cannot convert {prop_name}, using 0.0")
return 0.0
class CustomProfile:
def __init__(self, section, material):
print(f" Creating profile from section: {section.Label if hasattr(section, 'Label') else 'Unknown'}")
# Section properties (convert to mm)
# Note: edsFEM expects I_y, I_z, I_t (with underscores)
self.A = safe_get_value(section.AreaSection, 'mm^2', 'Area')
self.I_y = safe_get_value(section.MomentInertiaY, 'mm^4', 'Iy')
self.I_z = safe_get_value(section.MomentInertiaZ, 'mm^4', 'Iz')
self.J = safe_get_value(section.MomentInertiaPolar, 'mm^4', 'J')
self.I_t = self.J # Torsional constant = Polar moment of inertia
# Elastic section moduli (for stress calculations, not stiffness)
# Set to 0 for now - only needed for capacity checking later
self.Wy_el = 0.0 # Elastic section modulus about y (mm³)
self.Wz_el = 0.0 # Elastic section modulus about z (mm³)
# Plastic section moduli (for plastic capacity checking)
# Set to 0 for now - only needed for capacity checking later
self.Wy_pl = 0.0 # Plastic section modulus about y (mm³)
self.Wz_pl = 0.0 # Plastic section modulus about z (mm³)
# Also set Iy, Iz for compatibility
self.Iy = self.I_y
self.Iz = self.I_z
# Material properties - handle different material object types
print(f" Getting material from: {material.Label if hasattr(material, 'Label') else 'Unknown'}")
print(f" Material object type: {type(material).__name__}")
# Debug: check what Material attribute contains
if hasattr(material, 'Material'):
mat_attr = getattr(material, 'Material', None)
print(f" Has Material attribute, type: {type(mat_attr).__name__}")
if mat_attr is not None:
# Check if it's dict-like (has keys)
if hasattr(mat_attr, 'keys'):
print(f" Material is dict-like with keys: {list(mat_attr.keys())[:5]}")
else:
print(f" Material value: {mat_attr}")
# Check if material has a Material dictionary property (common in FreeCAD materials)
# Accept both dict and dict-like objects (with keys method)
mat_dict = None
if hasattr(material, 'Material'):
mat_attr = getattr(material, 'Material', None)
if mat_attr is not None and hasattr(mat_attr, 'keys'):
mat_dict = mat_attr
print(f" Using Material dictionary")
if mat_dict is not None:
print(f" Material dict keys: {list(mat_dict.keys())}")
# Extract Young's modulus from dictionary
# Format is typically "210000 MPa" or "210000.0 MPa"
E_str = mat_dict.get('YoungsModulus', mat_dict.get('YoungModulus', '210000 MPa'))
try:
# Parse string like "210000 MPa" -> 210000.0
if isinstance(E_str, str):
self.E = float(E_str.split()[0])
else:
self.E = float(E_str)
print(f" E: {self.E} MPa (from Material dict)")
except Exception as e:
self.E = 210000.0
print(f" E: {self.E} MPa (default - couldn't parse '{E_str}': {e})")
# Extract Shear modulus
G_str = mat_dict.get('ShearModulus', '')
if G_str:
try:
if isinstance(G_str, str):
self.G = float(G_str.split()[0])
else:
self.G = float(G_str)
print(f" G: {self.G} MPa (from Material dict)")
except:
# Calculate from E if can't parse
self.G = self.E / (2 * 1.3) # Assuming nu=0.3
print(f" G: {self.G} MPa (calculated from E)")
else:
# Calculate from Poisson's ratio
nu_str = mat_dict.get('PoissonRatio', '0.3')
try:
if isinstance(nu_str, str):
nu = float(nu_str.split()[0])
else:
nu = float(nu_str)
except:
nu = 0.3
self.G = self.E / (2 * (1 + nu))
print(f" G: {self.G} MPa (calculated from E and nu={nu})")
# Try direct attributes if no Material dict
else:
print(f" No Material dict found, trying direct attributes")
# Try different possible attribute names for Young's modulus
E_val = None
for attr_name in ['YoungModulus', 'YoungsModulus', 'Emodulus', 'E', 'ModulusYoung']:
if hasattr(material, attr_name):
E_val = getattr(material, attr_name)
print(f" Found E as '{attr_name}': {E_val}")
break
if E_val is None:
print(f" Warning: Young's modulus not found, using default 210000 MPa")
self.E = 210000.0 # Default steel
else:
self.E = safe_get_value(E_val, 'MPa', 'E')
# Try different possible attribute names for Shear modulus
G_val = None
for attr_name in ['ShearModulus', 'Gmodulus', 'G', 'ModulusShear']:
if hasattr(material, attr_name):
G_val = getattr(material, attr_name)
print(f" Found G as '{attr_name}': {G_val}")
break
if G_val is None:
# Calculate from E and Poisson's ratio if not found
nu = 0.3 # Default Poisson's ratio for steel
if hasattr(material, 'PoissonRatio'):
nu = safe_get_value(material.PoissonRatio, '', 'nu')
self.G = self.E / (2 * (1 + nu))
print(f" Calculated G from E and nu: {self.G} MPa")
else:
self.G = safe_get_value(G_val, 'MPa', 'G')
# Additional properties edsFEM might expect
self.designation = section.Label if hasattr(section, 'Label') else 'Custom'
self.g = 0 # Weight per length - can calculate if needed
self.g_stiff = 0 # Stiffener weight
return CustomProfile(section_obj, mat_obj)