Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cadquery/occ_impl/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ def __iter__(self) -> Iterator[float]:

yield from (self.x, self.y, self.z)

def toXYZ(self) -> gp_XYZ:

return self.wrapped.XYZ()

def toPnt(self) -> gp_Pnt:

return gp_Pnt(self.wrapped.XYZ())
Expand Down
139 changes: 124 additions & 15 deletions cadquery/occ_impl/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@
)

from OCP.BRepBuilderAPI import (
BRepBuilderAPI_Command,
BRepBuilderAPI_MakeShape,
BRepBuilderAPI_MakeVertex,
BRepBuilderAPI_MakeEdge,
BRepBuilderAPI_MakeFace,
BRepBuilderAPI_MakePolygon,
BRepBuilderAPI_MakeWire,
BRepBuilderAPI_WireError,
BRepBuilderAPI_Sewing,
BRepBuilderAPI_Copy,
BRepBuilderAPI_GTransform,
Expand Down Expand Up @@ -276,6 +278,7 @@
ShapeAnalysis_Wire,
ShapeAnalysis_Surface,
ShapeAnalysis,
ShapeAnalysis_WireOrder,
)
from OCP.TopTools import TopTools_HSequenceOfShape

Expand Down Expand Up @@ -2078,6 +2081,12 @@ def curvatureAt(
def paramsLength(self, locations: Iterable[float]) -> list[float]:
...

def startVertex(self) -> Vertex:
...

def endVertex(self) -> Vertex:
...


T1D = TypeVar("T1D", bound=Mixin1DProtocol)

Expand All @@ -2103,10 +2112,7 @@ def startPoint(self: Mixin1DProtocol) -> Vector:
Note, circles may have the start and end points the same
"""

v1, _ = TopoDS_Vertex(), TopoDS_Vertex()
ShapeAnalysis.FindBounds_s(self.wrapped, v1, _)

return Vertex(v1).Center()
return self.startVertex().Center()

def endPoint(self: Mixin1DProtocol) -> Vector:
"""
Expand All @@ -2116,10 +2122,33 @@ def endPoint(self: Mixin1DProtocol) -> Vector:
Note, circles may have the start and end points the same
"""

return self.endVertex().Center()

def startVertex(self: Mixin1DProtocol) -> Vertex:
"""

:return: a vertex representing the start point of this edge

Note, circles may have the start and end vertex the same
"""

v1, _ = TopoDS_Vertex(), TopoDS_Vertex()
ShapeAnalysis.FindBounds_s(self.wrapped, v1, _)

return Vertex(v1)

def endVertex(self: Mixin1DProtocol) -> Vertex:
"""

:return: a vertex representing the end point of this edge.

Note, circles may have the start and end vertex the same
"""

_, v2 = TopoDS_Vertex(), TopoDS_Vertex()
ShapeAnalysis.FindBounds_s(self.wrapped, _, v2)

return Vertex(v2).Center()
return Vertex(v2)

def _approxCurve(self: Mixin1DProtocol) -> Geom_BSplineCurve:
"""
Expand Down Expand Up @@ -5152,7 +5181,7 @@ def _get_wires(s: Shape) -> Iterable[Shape]:
raise ValueError(f"Required type(s): Edge, Wire; encountered {t}")


def _get_edges(*shapes: Shape) -> Iterable[Shape]:
def _get_edges(*shapes: Shape) -> Iterable[Edge]:
"""
Get edges or edges from wires.
"""
Expand All @@ -5161,7 +5190,7 @@ def _get_edges(*shapes: Shape) -> Iterable[Shape]:
t = s.ShapeType()

if t == "Edge":
yield s
yield s.edge()
elif t == "Wire":
yield from _get_edges(s.edges())
elif t == "Compound":
Expand Down Expand Up @@ -5588,7 +5617,7 @@ def modified(self, s: Shape | None = None) -> Shape:
if s:
return self._get(self._modified, s)

return _normalize(compound(*self._modified.values()))
return _normalize(compound(*set(self._modified.values())))

def generated(self, s: Shape | None = None) -> Shape:
"""
Expand Down Expand Up @@ -5726,8 +5755,6 @@ def _update_history(
# construct the history step
op = Op()

history.append(op, name)

# track all subshapes
for shape in shapes:
op._tracked.update(shape.Faces())
Expand All @@ -5737,6 +5764,12 @@ def _update_history(
# iterate over all builders and collect history information
builder: Any
for builder in builders:

if isinstance(builder, BRepBuilderAPI_Command):
assert (
builder.IsDone()
), f"Builder {builder} not done, cannot fill history."

has_first_last = isinstance(
builder, (BRepPrimAPI_MakeRevol, BRepPrimAPI_MakePrism,),
)
Expand All @@ -5760,6 +5793,7 @@ def _update_history(
BRepBuilderAPI_MakeShape,
BOPAlgo_Builder,
BRepOffset_MakeOffset,
BRepBuilderAPI_MakeWire,
),
)
has_modifidied = isinstance(
Expand Down Expand Up @@ -5823,6 +5857,8 @@ def _update_history(
op._first_shape |= _compound_or_shape(builder.FirstShape())
op._last_shape |= _compound_or_shape(builder.LastShape())

history.append(op, name)


def _remap_history_values(history: History | None, aux: History,) -> None:
"""
Expand Down Expand Up @@ -5967,24 +6003,97 @@ def wireOn(base: Shape, w: Shape, tol: float = 1e-6, N: int = 20) -> Wire:
return wire(rvs)


def _reorder_edges(edges: list[Edge]) -> tuple[list[int], list[bool]]:
"""
Private helper for reordering of edges before wire construction. Returns order and
correct orientation.
"""

n_edges = len(edges)
order = [0] * len(edges)
reverse = [False] * n_edges

ana = ShapeAnalysis_WireOrder()

for e in edges:
ana.Add(e.startPoint().toXYZ(), e.endPoint().toXYZ())

ana.Perform()

for i in range(n_edges):
i_old = ana.Ordered(i + 1)
i_old_abs = abs(i_old) - 1

order[i] = i_old_abs
reverse[i] = i_old < 0

return order, reverse


@multidispatch
def wire(*s: Shape) -> Wire:
def wire(*s: Shape, history: History | None = None, name: str | None = None,) -> Wire:
"""
Build wire from edges.
"""

builder = BRepBuilderAPI_MakeWire()

edges = _shapes_to_toptools_list(e for el in s for e in _get_edges(el))
builder.Add(edges)
# collect all edges and initialize
new_edges = []
edges: list[Edge] = []
for el in s:
edges.extend(_get_edges(el))

# reorder and check orientation
order, reverse = _reorder_edges(edges)

# build and store new edges for history construction
for i, rev in zip(order, reverse):
edge = edges[i].edge().wrapped

if rev:
edge = TopoDS.Edge(edge.Reversed())

builder.Add(edge)
status = builder.Error()

assert (
status == BRepBuilderAPI_WireError.BRepBuilderAPI_WireDone
), f"Wire construction failed: {status}"

new_edges.append(Edge(builder.Edge()))

# NB: this is a dummy update, the builder does not implement history correctly
_update_history(history, name, s, builder)

# Update history manually
if history:
op = history[-1]
for ix, v, rev in zip(order, new_edges, reverse):
k = edges[ix]
op._modified[k] = v
op._images[k] = v

if rev:
op._modified[k.endVertex()] = v.startVertex()
op._modified[k.startVertex()] = v.endVertex()
op._images[k.endVertex()] = v.startVertex()
op._images[k.startVertex()] = v.endVertex()
else:
op._modified[k.startVertex()] = v.startVertex()
op._modified[k.endVertex()] = v.endVertex()
op._images[k.startVertex()] = v.startVertex()
op._images[k.endVertex()] = v.endVertex()

return _shape(builder.Shape(), Wire)


@multidispatch
def wire(s: Sequence[Shape]) -> Wire:
def wire(
s: Sequence[Shape], history: History | None = None, name: str | None = None,
) -> Wire:

return wire(*s)
return wire(*s, history=history, name=name)


@multidispatch
Expand Down
50 changes: 50 additions & 0 deletions tests/test_free_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1460,3 +1460,53 @@ def test_chamfer2D_history():

assert new_edges.edges().size() == 4
assert mod_edges.edges().size() == 4


def test_wire_history():

# private helper
def _check(op, edges):
for e in edges:
assert (e.Center() - op.modified(e).Center()).Length == approx(0)
assert (e.Center() - op.images(e).Center()).Length == approx(0)

for v in e:
assert (v.Center() - op.modified(v).Center()).Length == approx(0)
assert (v.Center() - op.images(v).Center()).Length == approx(0)

h = History()
pts = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]

edges = [segment(p1, p2) for p1, p2 in zip(pts, pts[1:] + pts[:1])]

# regular case
_ = wire(edges, history=h)

op = h[-1]

assert op.modified().edges().size() == 4
assert op.modified().vertices().size() == 4

_check(op, edges)

# test with wrong edge order
edges = [edges[0], edges[2].reverse(), edges[1], edges[3]]
_ = wire(edges, history=h)

op = h[-1]

assert op.modified().edges().size() == 4
assert op.modified().vertices().size() == 4

_check(op, edges)

# same but open wire
edges = edges[:-1]
_ = wire(edges, history=h)

op = h[-1]

assert op.modified().edges().size() == 3
assert op.modified().vertices().size() == 4

_check(op, edges)