From 517002214415db4a8d63cc449460bf0f0a8b38e2 Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Thu, 23 Dec 2021 19:09:31 +0100 Subject: [PATCH] tests (wip) --- InitGui.py | 3 ++ ahb_cmd_export_csv.py | 13 ++++-- ahb_cmd_parse_step.py | 96 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 ahb_cmd_parse_step.py diff --git a/InitGui.py b/InitGui.py index c7dd394..a719139 100644 --- a/InitGui.py +++ b/InitGui.py @@ -69,6 +69,9 @@ class AssemblyHandbookWorkbench(Gui.Workbench): toolbox.append("AHB_animate") if self.dev: + self.importModule('ahb_cmd_parse_step') + toolbox.append("AHB_parse_step") + self.importModule('ahb_cmd_reload') toolbox.append("AHB_reload") diff --git a/ahb_cmd_export_csv.py b/ahb_cmd_export_csv.py index 591d650..bc11155 100644 --- a/ahb_cmd_export_csv.py +++ b/ahb_cmd_export_csv.py @@ -34,12 +34,15 @@ class AHB_ExportCsv: obj: App.DocumentObject for obj in doc.Objects: if obj.TypeId == 'Part::Feature': - parts.append((readProp(obj, 'Base_Layer', '').replace(',', '-') + "," + readProp(obj, 'Base_Reference', '').replace(',', '-'), "=\"" + obj.Label.replace(',', '-') + "\"")) + parts.append((readProp(obj, 'Base_Layer', '').replace(',', '-') + "," + readProp(obj, 'Base_Reference', '').replace(',', '-'), obj.Label.replace(',', '-'))) parts.sort() + export_layer_name = False with open(csvFileName, 'w', encoding='utf8') as csvFile: - csvFile.write("Group, Reference, Name, Quantity\n") + if export_layer_name: + csvFile.write("Group,") + csvFile.write("Reference, First object name, Quantity\n") quantity = 0 prevPart = parts[0] @@ -48,7 +51,11 @@ class AHB_ExportCsv: if part[0] == prevPart[0]: quantity += 1 else: - csvFile.write(prevPart[0] + "," + prevPart[1] + "," + str(quantity) + "\n") + if export_layer_name: + csvFile.write(prevPart[0] + ",") + else: + csvFile.write(prevPart[0].split(',')[1] + ",") + csvFile.write(prevPart[1] + "," + str(quantity) + "\n") quantity = 1 prevPart = part diff --git a/ahb_cmd_parse_step.py b/ahb_cmd_parse_step.py new file mode 100644 index 0000000..f897cea --- /dev/null +++ b/ahb_cmd_parse_step.py @@ -0,0 +1,96 @@ +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: + for idx in range(len(step_info.objects)): + if step_info.objects[idx].name == obj.Label: + 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())