Working on Compound changes

This commit is contained in:
gumyr 2025-06-24 19:44:43 -04:00
parent b3bf628584
commit 956fcd7438
3 changed files with 57 additions and 42 deletions

View file

@ -35,6 +35,7 @@ from build123d.build_enums import Align, Mode
from build123d.build_part import BuildPart
from build123d.geometry import Location, Plane, Rotation, RotationLike
from build123d.topology import Compound, Part, ShapeList, Solid, tuplify
from build123d.topology.utils import _make_topods_compound_from_shapes
class BasePartObject(Part):
@ -83,11 +84,15 @@ class BasePartObject(Part):
context._add_to_context(*new_solids, mode=mode)
if len(new_solids) > 1:
new_part = Compound(new_solids).wrapped
# new_part = Compound(new_solids).wrapped
new_part = _make_topods_compound_from_shapes(
[s.wrapped for s in new_solids]
)
elif isinstance(new_solids[0], Compound): # Don't add extra layers
new_part = new_solids[0].wrapped
else:
new_part = Compound(new_solids).wrapped
# new_part = Compound(new_solids).wrapped
new_part = _make_topods_compound_from_shapes([new_solids[0].wrapped])
super().__init__(
obj=new_part,

View file

@ -140,9 +140,9 @@ from .zero_d import Vertex
import inspect
def where_am_i_called_from():
def where_am_i_called_from(stack_levels: int = 2):
# inspect.stack()[0] is this frame; [2] is the caller
caller_frame_info = inspect.stack()[2]
caller_frame_info = inspect.stack()[stack_levels]
filename = caller_frame_info.filename
lineno = caller_frame_info.lineno
code_line = caller_frame_info.code_context[0].strip()
@ -197,7 +197,7 @@ class MixinComposite(NodeMixin):
if hasattr(node, "_base_wrapped") and node._base_wrapped is not None:
shapes.append(node._base_wrapped)
# then union in every childs current shape
# then add in every childs current shape
shapes.extend(child.wrapped for child in node.children)
if len(shapes) == 1:
node.wrapped = shapes[0]
@ -801,16 +801,16 @@ class Compound(Mixin3D, MixinComposite, Shape[TopoDS_Compound]):
"""
if isinstance(obj, TopoDS_Shape):
print("Making a Compound 1")
# self._base_wrapped = downcast(obj)
self._base_wrapped = obj
where_am_i_called_from(3)
self._base_wrapped = downcast(obj)
elif isinstance(obj, Iterable):
print("Making a Compound 2")
where_am_i_called_from(3)
self._base_wrapped = _make_topods_compound_from_shapes(
[s.wrapped for s in obj]
)
elif obj is None:
print("Making a Compound 3")
# self._base_wrapped = _make_topods_compound_from_shapes([])
self._base_wrapped = None
elif isinstance(obj, Assembly):
print("Making a Compound 4")
@ -818,6 +818,16 @@ class Compound(Mixin3D, MixinComposite, Shape[TopoDS_Compound]):
else:
raise ValueError(f"Invalid obj of type {type(obj)}")
# if isinstance(obj, Iterable):
# print("Making a Compound 1")
# topods_compound = _make_topods_compound_from_shapes(
# [s.wrapped for s in obj]
# )
# else:
# print("Making a Compound 2")
# topods_compound = obj
# self._base_wrapped = topods_compound
super().__init__(
obj=self._base_wrapped,
label=label,

View file

@ -617,26 +617,26 @@ class AlgebraTests(unittest.TestCase):
self.assertAlmostEqual(b.volume, r.volume, 5)
self.assertEqual(r._dim, 3)
# def test_empty_minus_part(self):
# b = Box(1, 2, 3)
# with self.assertRaises(ValueError):
# r = Part() - b
def test_empty_minus_part(self):
b = Box(1, 2, 3)
with self.assertRaises(ValueError):
r = Part() - b
# def test_part_minus_empty(self):
# b = Box(1, 2, 3)
# r = b - Part()
# self.assertAlmostEqual(b.volume, r.volume, 5)
# self.assertEqual(r._dim, 3)
def test_part_minus_empty(self):
b = Box(1, 2, 3)
r = b - Part()
self.assertAlmostEqual(b.volume, r.volume, 5)
self.assertEqual(r._dim, 3)
# def test_empty_and_part(self):
# b = Box(1, 2, 3)
# with self.assertRaises(ValueError):
# r = Part() & b
def test_empty_and_part(self):
b = Box(1, 2, 3)
with self.assertRaises(ValueError):
r = Part() & b
# def test_part_and_empty(self):
# b = Box(1, 2, 3)
# with self.assertRaises(ValueError):
# r = b & Part()
def test_part_and_empty(self):
b = Box(1, 2, 3)
with self.assertRaises(ValueError):
r = b & Part()
# Sketch + - & Empty
@ -652,26 +652,26 @@ class AlgebraTests(unittest.TestCase):
self.assertAlmostEqual(b.area, r.area, 5)
self.assertEqual(r._dim, 2)
# def test_empty_minus_sketch(self):
# b = Rectangle(1, 2)
# with self.assertRaises(ValueError):
# r = Sketch() - b
def test_empty_minus_sketch(self):
b = Rectangle(1, 2)
with self.assertRaises(ValueError):
r = Sketch() - b
# def test_sketch_minus_empty(self):
# b = Rectangle(1, 2)
# r = b - Sketch()
# self.assertAlmostEqual(b.area, r.area, 5)
# self.assertEqual(r._dim, 2)
def test_sketch_minus_empty(self):
b = Rectangle(1, 2)
r = b - Sketch()
self.assertAlmostEqual(b.area, r.area, 5)
self.assertEqual(r._dim, 2)
# def test_empty_and_sketch(self):
# b = Rectangle(1, 3)
# with self.assertRaises(ValueError):
# r = Sketch() & b
def test_empty_and_sketch(self):
b = Rectangle(1, 3)
with self.assertRaises(ValueError):
r = Sketch() & b
# def test_sketch_and_empty(self):
# b = Rectangle(1, 2)
# with self.assertRaises(ValueError):
# r = b & Sketch()
def test_sketch_and_empty(self):
b = Rectangle(1, 2)
with self.assertRaises(ValueError):
r = b & Sketch()
def test_1d_2d_minus(self):
line = Line((0, 0), (1, 1))