Repository navigation
Expand file tree
/
Copy pathimport_section.py
More file actions
128 lines (98 loc) · 4.54 KB
/
Copy pathimport_section.py
File metadata and controls
128 lines (98 loc) · 4.54 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
import FreeCAD as App
import FreeCADGui as Gui
import os
import re
from PySide import QtGui
from eds_config import module_path
# Path setup
ICONPATH = os.path.join(module_path, "resources")
class SectionImportCmd:
def GetResources(self):
return {
"Pixmap": os.path.join(ICONPATH, "icons/Tsec.svg"),
"Accel": "",
"MenuText": "Import Section properties",
"ToolTip": "Import section properties from text file"
}
def Activated(self):
dialog = QtGui.QFileDialog()
dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
dialog.setNameFilter("Text Files (*.txt);;All Files (*)")
dialog.setViewMode(QtGui.QFileDialog.Detail)
if dialog.exec_():
filepath = str(dialog.selectedFiles()[0])
else:
App.Console.PrintWarning("File selection cancelled.\n")
return
if not filepath or not os.path.isfile(filepath):
App.Console.PrintWarning("Invalid file path selected.\n")
return
doc = App.ActiveDocument
if not doc:
App.Console.PrintError("No active document found.\n")
return
# Create empty parametric object with a dummy shape
obj = doc.addObject("Part::FeaturePython", "SectionData")
obj.Label = "SectionData" # Default label, will be updated when loading properties
ViewProviderSection(obj.ViewObject)
proxy = SectionProxy(obj)
# Load section properties from file (this will also set the label)
SectionProxy.load_section_properties(filepath, obj)
doc.recompute()
def IsActive(self):
return App.ActiveDocument is not None
class ViewProviderSection:
def __init__(self, obj):
obj.Proxy = self
def getIcon(self):
return os.path.join(ICONPATH, "icons/Tse.svg")
class SectionProxy:
def __init__(self, obj):
obj.Proxy = self
obj.addProperty("App::PropertyArea", "AreaSection", "Section", "Section area")
obj.addProperty("App::PropertyFloat", "MomentInertiaPolar", "Section", "Polar Moment of Inertia")
obj.addProperty("App::PropertyFloat", "MomentInertiaY", "Section", "Moment of Inertia Y")
obj.addProperty("App::PropertyFloat", "MomentInertiaZ", "Section", "Moment of Inertia Z")
obj.addProperty("App::PropertyFloat", "ProductInertiaYZ", "Section", "Product of Inertia")
@staticmethod
def load_section_properties(filepath, target_obj):
try:
with open(filepath, 'r') as file:
lines = file.readlines()
section_label = None
for line in lines:
line = line.strip()
# Check for label in square brackets (e.g., [20x40])
label_match = re.match(r'^\[(.+)\]$', line)
if label_match:
section_label = label_match.group(1)
App.Console.PrintMessage(f"Found section label: {section_label}\n")
continue
# Process property assignments
if '=' in line:
key, value = line.split('=', 1)
key = key.strip()
value = value.strip()
# Attempt to convert the value to float
try:
value = float(value)
except ValueError:
App.Console.PrintWarning(f"Invalid value for {key}: {value}. Skipping.\n")
continue
# Set the property if it exists on the target object
if hasattr(target_obj, key):
setattr(target_obj, key, value)
App.Console.PrintMessage(f"Set {key} = {value}\n")
else:
App.Console.PrintWarning(f"Property {key} not found on object.\n")
# Set the label if found
if section_label:
target_obj.Label = section_label
App.Console.PrintMessage(f"Set object label to: {section_label}\n")
else:
App.Console.PrintWarning("No section label found in square brackets.\n")
except Exception as e:
App.Console.PrintError(f"Error reading file: {e}\n")
App.ActiveDocument.recompute()
# Register the command in FreeCAD
Gui.addCommand("Import_section", SectionImportCmd())