FreeCAD workbench to create assembly handbooks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.9 KiB

import FreeCADGui as Gui
import FreeCAD as App
class AHB_ParseStep:
def GetResources(self):
return {"MenuText": "[dev] parse STEP",
"ToolTip": "Reload informations from the original STEP file",
"Pixmap": ""
}
def IsActive(self):
return True
def Activated(self):
import step_parser
import os
import importlib
importlib.reload(step_parser)
print("Reloading STEP file metadata...")
doc = App.activeDocument()
filename: str = doc.FileName
if filename is None:
raise BaseException("You must save your FreeCAD document before parsing the corresponding STEP file")
filename = os.path.splitext(filename)[0] + ".step"
step_info = step_parser.parse(filename)
objects = doc.Objects
override_names = False
feature_idx = 0
total_features = 0
obj: App.DocumentObject
for obj in objects:
if obj.TypeId == 'Part::Feature':
total_features = total_features + 1
object_idx = -1
if override_names:
object_idx = feature_idx + 1
else:
fixed_name = step_parser.process_name(obj.Label)[0]
for idx in range(len(step_info.objects)):
if step_info.objects[idx].name == fixed_name:
object_idx = idx
break
if 0 <= object_idx < len(step_info.objects):
object_info = step_info.objects[object_idx]
print(obj.Label + " => " + object_info.name)
obj.Label = object_info.name
if object_info.layer is not None:
if "Base_Reference" not in obj.PropertiesList:
obj.addProperty("App::PropertyString", "Base_Reference", "Base")
obj.Base_Reference = object_info.layer.reference
if "Base_Layer" not in obj.PropertiesList:
obj.addProperty("App::PropertyString", "Base_Layer", "Base")
obj.Base_Layer = object_info.layer.name
feature_idx = feature_idx + 1
step_info2 = step_parser.parse(filename, 'SOLIDS')
print("STEP names phase 2 (" + str(len(step_info2.objects)) + ")")
feature_idx = 0
for obj in objects:
if obj.TypeId == 'Part::Feature':
#print(obj.Label)
if obj.Label.startswith("COMPOUND"):
feature_from_end = total_features - feature_idx
object_idx = len(step_info2.objects) - feature_from_end
#print(obj.Label + " object_idx=" + str(object_idx))
if 0 <= object_idx < len(step_info2.objects):
object_info = step_info2.objects[object_idx]
print(obj.Label + " => " + object_info.name)
obj.Label = object_info.name
if object_info.layer is not None:
if "Base_Reference" not in obj.PropertiesList:
obj.addProperty("App::PropertyString", "Base_Reference", "Base")
obj.Base_Reference = object_info.layer.reference
if "Base_Layer" not in obj.PropertiesList:
obj.addProperty("App::PropertyString", "Base_Layer", "Base")
obj.Base_Layer = object_info.layer.name
feature_idx = feature_idx + 1
print("STEP names import finished")
from ahb_command import AHB_CommandWrapper
AHB_CommandWrapper.addGuiCommand('AHB_parse_step', AHB_ParseStep())