From a8ee1b492d146902f62c01066d4ac3fa51dc38bf Mon Sep 17 00:00:00 2001 From: gumyr Date: Sat, 3 Feb 2024 19:03:37 -0500 Subject: [PATCH] Adding Curve as an input to Wire constructor --- src/build123d/topology.py | 39 ++++++++++++++++++++++++++++++++++++--- tests/test_direct_api.py | 3 +++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/build123d/topology.py b/src/build123d/topology.py index 0797a25f..b0127dba 100644 --- a/src/build123d/topology.py +++ b/src/build123d/topology.py @@ -7154,7 +7154,7 @@ def __init__( color: Color = None, parent: Compound = None, ): - """Build a Wire from an WIre - used when the input could be an Edge or Wire. + """Build a Wire from an Wire - used when the input could be an Edge or Wire. Args: wire (Wire): Wire to convert to another Wire @@ -7163,6 +7163,23 @@ def __init__( parent (Compound, optional): assembly parent. Defaults to None. """ + @overload + def __init__( + self, + wire: Curve, + label: str = "", + color: Color = None, + parent: Compound = None, + ): + """Build a Wire from an Curve. + + Args: + curve (Curve): Curve to convert to a Wire + label (str, optional): Defaults to ''. + color (Color, optional): Defaults to None. + parent (Compound, optional): assembly parent. Defaults to None. + """ + @overload def __init__( self, @@ -7188,7 +7205,7 @@ def __init__( """ def __init__(self, *args, **kwargs): - edge, edges, wire, sequenced, obj, label, color, parent = (None,) * 8 + curve, edge, edges, wire, sequenced, obj, label, color, parent = (None,) * 9 if args: l_a = len(args) @@ -7198,12 +7215,24 @@ def __init__(self, *args, **kwargs): edge, label, color, parent = args[:4] + (None,) * (4 - l_a) elif isinstance(args[0], Wire): wire, label, color, parent = args[:4] + (None,) * (4 - l_a) + elif isinstance(args[0], Curve): + curve, label, color, parent = args[:4] + (None,) * (4 - l_a) elif isinstance(args[0], Iterable): edges, sequenced, label, color, parent = args[:5] + (None,) * (5 - l_a) unknown_args = ", ".join( set(kwargs.keys()).difference( - ["edge", "edges", "sequenced", "obj", "label", "color", "parent"] + [ + "curve", + "wire", + "edge", + "edges", + "sequenced", + "obj", + "label", + "color", + "parent", + ] ) ) if unknown_args: @@ -7216,9 +7245,13 @@ def __init__(self, *args, **kwargs): label = kwargs.get("label", label) color = kwargs.get("color", color) parent = kwargs.get("parent", parent) + wire = kwargs.get("wire", wire) + curve = kwargs.get("curve", curve) if edge is not None: edges = [edge] + elif curve is not None: + edges = curve.edges() if wire is not None: obj = wire.wrapped elif edges: diff --git a/tests/test_direct_api.py b/tests/test_direct_api.py index e2bc0180..824d387d 100644 --- a/tests/test_direct_api.py +++ b/tests/test_direct_api.py @@ -3675,6 +3675,9 @@ def test_constructor(self): self.assertTupleAlmostEquals(w6.color.to_tuple(), (1.0, 0.0, 0.0, 1.0), 5) w7 = Wire(w6) self.assertTrue(w7.is_valid()) + c0 = Polyline((0,0),(1,0),(1,1)) + w8 = Wire(c0) + self.assertTrue(w8.is_valid()) with self.assertRaises(ValueError): Wire(bob="fred")