Skip to content

Commit

Permalink
Adding Curve as an input to Wire constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Feb 4, 2024
1 parent 92d3dc4 commit a8ee1b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/build123d/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_direct_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit a8ee1b4

Please sign in to comment.