Use popPending methods

This commit is contained in:
Marcus Boyd
2021-03-08 14:22:47 +10:30
parent b316461b49
commit 7be6070534
2 changed files with 50 additions and 43 deletions

View File

@ -2122,6 +2122,8 @@ class Workplane(object):
def _consolidateWires(self) -> List[Wire]:
# note: do not use CQContext.popPendingEdges or Wires here, this method does not
# clear pending edges or wires.
wires = cast(
List[Union[Edge, Wire]],
[el for el in chain(self.ctx.pendingEdges, self.ctx.pendingWires)],
@ -2159,39 +2161,33 @@ class Workplane(object):
Returns a CQ object with all pending edges connected into a wire.
All edges on the stack that can be combined will be combined into a single wire object,
and other objects will remain on the stack unmodified
and other objects will remain on the stack unmodified. If there are no pending edges,
this method will just return self.
:param forConstruction: whether the wire should be used to make a solid, or if it is just
for reference
:type forConstruction: boolean. true if the object is only for reference
This method is primarily of use to plugin developers making utilities for 2-d construction.
This method should be called when a user operation implies that 2-d construction is
finished, and we are ready to begin working in 3d
finished, and we are ready to begin working in 3d.
SEE '2-d construction concepts' for a more detailed explanation of how CadQuery handles
edges, wires, etc
edges, wires, etc.
Any non edges will still remain.
"""
edges = self.ctx.pendingEdges
# do not consolidate if there are no free edges
if len(edges) == 0:
if len(self.ctx.pendingEdges) == 0:
return self
self.ctx.pendingEdges = []
others = []
for e in self.objects:
if type(e) != Edge:
others.append(e)
edges = self.ctx.popPendingEdges()
w = Wire.assembleEdges(edges)
if not forConstruction:
self._addPendingWire(w)
others = [e for e in self.objects if not isinstance(e, Edge)]
return self.newObject(others + [w])
def each(
@ -2757,10 +2753,7 @@ class Workplane(object):
"""
# group wires together into faces based on which ones are inside the others
# result is a list of lists
wireSets = sortWiresByBuildOrder(list(self.ctx.pendingWires))
# now all of the wires have been used to create an extrusion
self.ctx.pendingWires = []
wireSets = sortWiresByBuildOrder(self.ctx.popPendingWires())
# compute extrusion vector and extrude
eDir = self.plane.zDir.multiply(distance)
@ -3210,13 +3203,12 @@ class Workplane(object):
:param boolean clean: call :py:meth:`clean` afterwards to have a clean shape
:raises ValueError: if there is no solid to subtract from in the chain
:raises ValueError: if there are no pending wires to cut with
:return: a CQ object with the resulting object selected
see :py:meth:`cutBlind` to cut material to a limited depth
"""
wires = self.ctx.pendingWires
self.ctx.pendingWires = []
wires = self.ctx.popPendingWires()
solidRef = self.findSolid()
rv = []
@ -3237,8 +3229,7 @@ class Workplane(object):
Make a lofted solid, through the set of wires.
:return: a CQ object containing the created loft
"""
wiresToLoft = self.ctx.pendingWires
self.ctx.pendingWires = []
wiresToLoft = self.ctx.popPendingWires()
r: Shape = Solid.makeLoft(wiresToLoft, ruled)
@ -3268,9 +3259,7 @@ class Workplane(object):
# group wires together into faces based on which ones are inside the others
# result is a list of lists
wireSets = sortWiresByBuildOrder(list(self.ctx.pendingWires))
# now all of the wires have been used to create an extrusion
self.ctx.pendingWires = []
wireSets = sortWiresByBuildOrder(self.ctx.popPendingWires())
# compute extrusion vector and extrude
eDir = self.plane.zDir.multiply(distance)
@ -3316,11 +3305,8 @@ class Workplane(object):
This method is a utility method, primarily for plugin and internal use.
"""
# We have to gather the wires to be revolved
wireSets = sortWiresByBuildOrder(list(self.ctx.pendingWires))
# Mark that all of the wires have been used to create a revolution
self.ctx.pendingWires = []
# Get the wires to be revolved
wireSets = sortWiresByBuildOrder(self.ctx.popPendingWires())
# Revolve the wires, make a compound out of them and then fuse them
toFuse = []
@ -3373,19 +3359,17 @@ class Workplane(object):
mode = wire
if not multisection:
wireSets = sortWiresByBuildOrder(list(self.ctx.pendingWires))
wireSets = sortWiresByBuildOrder(self.ctx.popPendingWires())
for ws in wireSets:
thisObj = Solid.sweep(
ws[0], ws[1:], p, makeSolid, isFrenet, mode, transition
)
toFuse.append(thisObj)
else:
sections = self.ctx.pendingWires
sections = self.ctx.popPendingWires()
thisObj = Solid.sweep_multi(sections, p, makeSolid, isFrenet, mode)
toFuse.append(thisObj)
self.ctx.pendingWires = []
return Compound.makeCompound(toFuse)
def interpPlate(

View File

@ -395,7 +395,6 @@ class TestCadQuery(BaseTest):
def testLoft(self):
"""
Test making a lofted solid
:return:
"""
s = Workplane("XY").circle(4.0).workplane(5.0).rect(2.0, 2.0).loft()
self.saveModel(s)
@ -405,10 +404,13 @@ class TestCadQuery(BaseTest):
# the resulting loft had a split on the side, not sure why really, i expected only 3 faces
self.assertEqual(7, s.faces().size())
def testLoftWithOneWireRaisesValueError(self):
s = Workplane("XY").circle(5)
def testLoftRaisesValueError(self):
s0 = Workplane().hLine(1) # no wires
with raises(ValueError):
s0.loft()
s1 = Workplane("XY").circle(5) # one wire
with self.assertRaises(ValueError) as cm:
s.loft()
s1.loft()
err = cm.exception
self.assertEqual(str(err), "More than one wire is required")
@ -551,6 +553,14 @@ class TestCadQuery(BaseTest):
self.assertEqual(2, result.vertices().size())
self.assertEqual(2, result.edges().size())
def testRevolveErrors(self):
"""
Test that revolve raises errors when used incorrectly.
"""
result = Workplane("XY").lineTo(0, 10).lineTo(5, 0)
with raises(ValueError):
result.revolve()
def testSpline(self):
"""
Tests construction of splines
@ -1040,6 +1050,11 @@ class TestCadQuery(BaseTest):
self.assertAlmostEqual(v1.getAngle(v2), math.pi / 4, 6)
# test for ValueError if pending wires is empty
w0 = Workplane().hLine(1).vLine(1)
with raises(ValueError):
w0.sweep(path)
# Test aux spine invalid input handling
with raises(ValueError):
result = (
@ -1655,10 +1670,13 @@ class TestCadQuery(BaseTest):
self.assertTrue(r.val().isValid())
self.assertEqual(r.faces().size(), 7)
# test ValueError when no solids found
w0 = Workplane().hLine(1).vLine(1).close()
# test errors
box0 = Workplane().box(1, 1, 1).faces(">Z").workplane().hLine(1)
with raises(ValueError):
w0.cutThruAll()
box0.cutThruAll()
no_box = Workplane().hLine(1).vLine(1).close()
with raises(ValueError):
no_box.cutThruAll()
def testCutToFaceOffsetNOTIMPLEMENTEDYET(self):
"""
@ -4290,3 +4308,8 @@ class TestCadQuery(BaseTest):
w4 = Workplane().circle(1).extrude(1)
with self.assertRaises(ValueError):
w4.ctx.popPendingWires()
# test via cutBlind
w5 = Workplane().circle(1).extrude(1)
with self.assertRaises(ValueError):
w5.cutBlind(-1)