Skip to content

Commit b316461

Browse files
committed
Added popPendingEdges and popPendingWires to CQ Context
Also tests
1 parent 0325474 commit b316461

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

cadquery/cq.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ def __init__(self):
9393
self.tolerance = 0.0001 # user specified tolerance
9494
self.tags = {}
9595

96+
def popPendingEdges(self, errorOnEmpty: bool = True) -> List[Edge]:
97+
"""
98+
Get and clear pending edges.
99+
100+
:raises ValueError: if errorOnEmpty is True and no edges are present.
101+
"""
102+
if errorOnEmpty and not self.pendingEdges:
103+
raise ValueError("No pending edges present")
104+
out = self.pendingEdges
105+
self.pendingEdges = []
106+
return out
107+
108+
def popPendingWires(self, errorOnEmpty: bool = True) -> List[Wire]:
109+
"""
110+
Get and clear pending wires.
111+
112+
:raises ValueError: if errorOnEmpty is True and no wires are present.
113+
"""
114+
if errorOnEmpty and not self.pendingWires:
115+
raise ValueError("No pending wires present")
116+
out = self.pendingWires
117+
self.pendingWires = []
118+
return out
119+
96120

97121
class Workplane(object):
98122
"""

tests/test_cadquery.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4255,3 +4255,38 @@ def testFindFace(self):
42554255
self.assertTrue(isinstance(w2.findFace(searchParents=True), Face))
42564256
with raises(ValueError):
42574257
w2.findFace(searchParents=False)
4258+
4259+
def testPopPending(self):
4260+
# test pending edges
4261+
w0 = Workplane().hLine(1)
4262+
self.assertEqual(len(w0.ctx.pendingEdges), 1)
4263+
edges = w0.ctx.popPendingEdges()
4264+
self.assertEqual(len(edges), 1)
4265+
self.assertEqual(edges[0], w0.val())
4266+
# pending edges should now be cleared
4267+
self.assertEqual(len(w0.ctx.pendingEdges), 0)
4268+
4269+
# test pending wires
4270+
w1 = Workplane().hLine(1).vLine(1).close()
4271+
wire = w1.val()
4272+
self.assertEqual(w1.ctx.pendingWires[0], wire)
4273+
pop_pending_output = w1.ctx.popPendingWires()
4274+
self.assertEqual(pop_pending_output[0], wire)
4275+
# pending wires should now be cleared
4276+
self.assertEqual(len(w1.ctx.pendingWires), 0)
4277+
4278+
# test error when empty pending edges
4279+
w2 = Workplane()
4280+
# the following 2 should not raise an exception
4281+
w2.ctx.popPendingEdges(errorOnEmpty=False)
4282+
w2.ctx.popPendingWires(errorOnEmpty=False)
4283+
4284+
# empty edges
4285+
w3 = Workplane().hLine(1).vLine(1).close()
4286+
with self.assertRaises(ValueError):
4287+
w3.ctx.popPendingEdges()
4288+
4289+
# empty wires
4290+
w4 = Workplane().circle(1).extrude(1)
4291+
with self.assertRaises(ValueError):
4292+
w4.ctx.popPendingWires()

0 commit comments

Comments
 (0)