From c7b6bb73646100cd87aeca8289e22968048be2b6 Mon Sep 17 00:00:00 2001 From: Michael Bugert Date: Wed, 24 Jul 2024 22:33:37 +0200 Subject: [PATCH] import_step: do not assign labels to Compound.for_construction --- src/build123d/importers.py | 13 +++++++------ src/build123d/topology.py | 3 ++- tests/test_importers.py | 6 ++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/build123d/importers.py b/src/build123d/importers.py index 068f40a0..36eb0a39 100644 --- a/src/build123d/importers.py +++ b/src/build123d/importers.py @@ -32,7 +32,7 @@ import os from math import degrees from pathlib import Path -from typing import TextIO, Union +from typing import TextIO, Union, Optional import OCP.IFSelect from OCP.BRep import BRep_Builder @@ -172,12 +172,14 @@ def get_col(obj: TopoDS_Shape) -> Quantity_ColorRGBA: return shape_color - def build_assembly(assembly: Compound) -> list[Shape]: + def build_assembly( + assembly: Compound, parent_tdf_label: Optional[TDF_Label] = None + ) -> list[Shape]: tdf_labels = TDF_LabelSequence() - if assembly.for_construction is None: + if parent_tdf_label is None: shape_tool.GetFreeShapes(tdf_labels) else: - shape_tool.GetComponents_s(assembly.for_construction, tdf_labels) + shape_tool.GetComponents_s(parent_tdf_label, tdf_labels) sub_shapes: list[Shape] = [] for i in range(tdf_labels.Length()): @@ -195,13 +197,12 @@ def build_assembly(assembly: Compound) -> list[Shape]: sub_shape_loc = assembly.location.wrapped.Multiplied(sub_shape_loc) sub_shape: Shape = sub_shape_type() sub_shape.wrapped = downcast(topo_shape.Moved(sub_shape_loc)) - sub_shape.for_construction = ref_tdf_label sub_shape.color = Color(get_color(topo_shape)) sub_shape.label = get_name(ref_tdf_label) sub_shape.parent = assembly if shape_tool.IsAssembly_s(ref_tdf_label): - sub_shape.children = build_assembly(sub_shape) + sub_shape.children = build_assembly(sub_shape, ref_tdf_label) sub_shapes.append(sub_shape) return sub_shapes diff --git a/src/build123d/topology.py b/src/build123d/topology.py index ce0c10a6..2888748a 100644 --- a/src/build123d/topology.py +++ b/src/build123d/topology.py @@ -2312,7 +2312,8 @@ def __deepcopy__(self, memo) -> Self: cls = self.__class__ result = cls.__new__(cls) memo[id(self)] = result - memo[id(self.wrapped)] = downcast(BRepBuilderAPI_Copy(self.wrapped).Shape()) + if self.wrapped is not None: + memo[id(self.wrapped)] = downcast(BRepBuilderAPI_Copy(self.wrapped).Shape()) for key, value in self.__dict__.items(): setattr(result, key, copy.deepcopy(value, memo)) if key == "joints": diff --git a/tests/test_importers.py b/tests/test_importers.py index a86febd7..db724cd8 100644 --- a/tests/test_importers.py +++ b/tests/test_importers.py @@ -108,6 +108,12 @@ def test_single_object(self): box = import_step("test.step") self.assertTrue(isinstance(box, Solid)) + def test_move_single_object(self): + export_step(Solid.make_box(1, 1, 1), "test.step") + box = import_step("test.step") + box_moved = Pos(X=1) * box + self.assertEqual(tuple(box_moved.location.position), (1, 0, 0)) + def test_single_label_color(self): box_to_export = Solid.make_box(1, 1, 1) box_to_export.label = "box"