-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstep_processor.py
More file actions
365 lines (292 loc) · 12 KB
/
Copy pathstep_processor.py
File metadata and controls
365 lines (292 loc) · 12 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
"""
STEP File Processor using Open CASCADE Technology (OCCT)
Handles reading, analysis, and extraction of data from STEP files
"""
import logging
from typing import Dict, Any, List, Tuple
import os
try:
from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.IFSelect import IFSelect_RetDone
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import (
TopAbs_SOLID, TopAbs_SHELL, TopAbs_FACE,
TopAbs_EDGE, TopAbs_VERTEX, TopAbs_COMPOUND
)
from OCC.Core.GProp import GProp_GProps
from OCC.Core.BRepGProp import (
brepgprop_VolumeProperties,
brepgprop_SurfaceProperties,
brepgprop_LinearProperties
)
from OCC.Core.Bnd import Bnd_Box
from OCC.Core.BRepBndLib import brepbndlib_Add
from OCC.Core.BRepCheck import BRepCheck_Analyzer
from OCC.Core.TopoDS import TopoDS_Shape, TopoDS_Compound
from OCC.Core.TopTools import TopTools_IndexedMapOfShape
from OCC.Core.TopExp import topexp
from OCC.Extend.TopologyUtils import TopologyExplorer
from OCC.Core.STEPCAFControl import STEPCAFControl_Reader
from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.XCAFDoc import (
XCAFDoc_DocumentTool_ShapeTool,
XCAFDoc_DocumentTool_ColorTool,
XCAFDoc_DocumentTool_LayerTool,
XCAFDoc_DocumentTool_MaterialTool
)
from OCC.Core.TCollection import TCollection_ExtendedString
from OCC.Core.TDF import TDF_LabelSequence
OCCT_AVAILABLE = True
except ImportError:
OCCT_AVAILABLE = False
logger = logging.getLogger(__name__)
class STEPProcessor:
"""Process STEP files and extract various data points"""
def __init__(self):
"""Initialize the STEP processor"""
if not OCCT_AVAILABLE:
logger.warning("pythonocc-core not available. Install it for STEP processing.")
self.occt_available = OCCT_AVAILABLE
def is_available(self) -> bool:
"""Check if OCCT is available"""
return self.occt_available
def analyze_file(self, file_path: str) -> Dict[str, Any]:
"""
Comprehensive analysis of STEP file
Args:
file_path: Path to STEP file
Returns:
Dictionary with all analysis results
"""
if not self.occt_available:
raise RuntimeError("pythonocc-core not installed")
if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found: {file_path}")
# Read the STEP file
shape, metadata = self._read_step_file(file_path)
# Gather all data
result = {
"file_info": {
"file_size_bytes": os.path.getsize(file_path),
"file_path": os.path.basename(file_path)
},
"metadata": metadata,
"geometry": self._extract_geometric_properties(shape),
"topology": self._extract_topology_info(shape),
"validation": self._validate_shape(shape),
"assembly": self._extract_assembly_structure(file_path)
}
return result
def get_geometric_properties(self, file_path: str) -> Dict[str, Any]:
"""Extract only geometric properties"""
shape, _ = self._read_step_file(file_path)
return self._extract_geometric_properties(shape)
def get_topology_info(self, file_path: str) -> Dict[str, Any]:
"""Extract only topology information"""
shape, _ = self._read_step_file(file_path)
return self._extract_topology_info(shape)
def validate_file(self, file_path: str) -> Dict[str, Any]:
"""Validate STEP file"""
shape, _ = self._read_step_file(file_path)
return self._validate_shape(shape)
def _read_step_file(self, file_path: str) -> Tuple[TopoDS_Shape, Dict[str, Any]]:
"""
Read STEP file and return shape and metadata
Args:
file_path: Path to STEP file
Returns:
Tuple of (shape, metadata)
"""
step_reader = STEPControl_Reader()
status = step_reader.ReadFile(file_path)
if status != IFSelect_RetDone:
raise ValueError(f"Failed to read STEP file: {file_path}")
# Transfer roots
step_reader.PrintCheckLoad(False, IFSelect_RetDone)
nb_roots = step_reader.NbRootsForTransfer()
step_reader.PrintCheckTransfer(False, IFSelect_RetDone)
logger.info(f"Found {nb_roots} roots in STEP file")
# Transfer all roots
step_reader.TransferRoots()
shape = step_reader.OneShape()
# Extract basic metadata
metadata = {
"nb_roots": nb_roots,
"transfer_status": "success"
}
return shape, metadata
def _extract_geometric_properties(self, shape: TopoDS_Shape) -> Dict[str, Any]:
"""
Extract geometric properties (volume, surface area, bounding box)
Args:
shape: TopoDS_Shape to analyze
Returns:
Dictionary with geometric properties
"""
properties = {}
# Volume calculation
try:
props = GProp_GProps()
brepgprop_VolumeProperties(shape, props)
volume = props.Mass()
center_of_mass = props.CentreOfMass()
properties["volume"] = {
"value": volume,
"unit": "cubic_units"
}
properties["center_of_mass"] = {
"x": center_of_mass.X(),
"y": center_of_mass.Y(),
"z": center_of_mass.Z()
}
except Exception as e:
logger.warning(f"Could not calculate volume: {e}")
properties["volume"] = None
# Surface area calculation
try:
props = GProp_GProps()
brepgprop_SurfaceProperties(shape, props)
surface_area = props.Mass()
properties["surface_area"] = {
"value": surface_area,
"unit": "square_units"
}
except Exception as e:
logger.warning(f"Could not calculate surface area: {e}")
properties["surface_area"] = None
# Bounding box
try:
bbox = Bnd_Box()
brepbndlib_Add(shape, bbox)
xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get()
properties["bounding_box"] = {
"min": {"x": xmin, "y": ymin, "z": zmin},
"max": {"x": xmax, "y": ymax, "z": zmax},
"dimensions": {
"length_x": xmax - xmin,
"length_y": ymax - ymin,
"length_z": zmax - zmin
}
}
except Exception as e:
logger.warning(f"Could not calculate bounding box: {e}")
properties["bounding_box"] = None
return properties
def _extract_topology_info(self, shape: TopoDS_Shape) -> Dict[str, Any]:
"""
Extract topology information (counts of solids, faces, edges, vertices)
Args:
shape: TopoDS_Shape to analyze
Returns:
Dictionary with topology counts
"""
topology = {
"solids": 0,
"compounds": 0,
"shells": 0,
"faces": 0,
"edges": 0,
"vertices": 0
}
# Count topology elements
explorer = TopologyExplorer(shape)
topology["solids"] = explorer.number_of_solids()
topology["compounds"] = explorer.number_of_compounds()
topology["shells"] = explorer.number_of_shells()
topology["faces"] = explorer.number_of_faces()
topology["edges"] = explorer.number_of_edges()
topology["vertices"] = explorer.number_of_vertices()
# Additional details
topology["details"] = {
"has_free_edges": len(list(explorer.edges())) != topology["edges"],
"shape_type": shape.ShapeType()
}
return topology
def _validate_shape(self, shape: TopoDS_Shape) -> Dict[str, Any]:
"""
Validate shape quality and check for issues
Args:
shape: TopoDS_Shape to validate
Returns:
Dictionary with validation results
"""
validation = {
"is_valid": False,
"is_done": False,
"issues": []
}
try:
analyzer = BRepCheck_Analyzer(shape)
validation["is_valid"] = analyzer.IsValid()
if not validation["is_valid"]:
# The shape has issues
validation["issues"].append("Shape contains geometric or topological errors")
validation["is_done"] = True
except Exception as e:
logger.error(f"Validation error: {e}")
validation["issues"].append(f"Validation failed: {str(e)}")
# Additional quality checks
try:
explorer = TopologyExplorer(shape)
# Check for degenerate edges
degenerate_count = 0
for edge in explorer.edges():
if edge.Degenerated():
degenerate_count += 1
if degenerate_count > 0:
validation["issues"].append(f"Found {degenerate_count} degenerate edges")
validation["quality_metrics"] = {
"degenerate_edges": degenerate_count,
"total_edges": explorer.number_of_edges()
}
except Exception as e:
logger.warning(f"Quality check error: {e}")
return validation
def _extract_assembly_structure(self, file_path: str) -> Dict[str, Any]:
"""
Extract assembly structure and metadata using XCAF
Args:
file_path: Path to STEP file
Returns:
Dictionary with assembly information
"""
assembly_info = {
"is_assembly": False,
"parts": [],
"layers": [],
"colors": []
}
try:
# Create XCAF document
doc = TDocStd_Document(TCollection_ExtendedString("MDTV-XCAF"))
# Read STEP file with XCAF
reader = STEPCAFControl_Reader()
reader.SetColorMode(True)
reader.SetLayerMode(True)
reader.SetNameMode(True)
status = reader.ReadFile(file_path)
if status == IFSelect_RetDone:
reader.Transfer(doc)
# Get shape tool
shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
# Get free shapes (top-level components)
labels = TDF_LabelSequence()
shape_tool.GetFreeShapes(labels)
assembly_info["is_assembly"] = labels.Length() > 1
assembly_info["part_count"] = labels.Length()
# Extract part names if available
parts = []
for i in range(1, labels.Length() + 1):
label = labels.Value(i)
name = label.EntryDumpToString()
# Try to get actual name
# Note: Name extraction can be complex, this is simplified
parts.append({
"label": name,
"index": i
})
assembly_info["parts"] = parts
except Exception as e:
logger.warning(f"Could not extract assembly structure: {e}")
assembly_info["error"] = str(e)
return assembly_info