Skip to content

Improved error when pendingWires/pendingEdges is empty #621

Closed
@marcus7070

Description

@marcus7070

Minimal example:

w = (
    cq.Workplane()
    .box(10, 10, 10)
    .faces(">Z")
    .edges()
    .extrude(3, taper=40)
)

Result is IndexError: list index is out of range in CQ-editor.

A more detailed trace from the interpreter is still pretty opaque:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nix/store/sgx51ii0rf6872aj5ab5qvf5fw3kdll4-python3-3.8.6-env/lib/python3.8/site-packages/cadquery/cq.py", line 2745, in extrude
    r = self._extrude(
  File "/nix/store/sgx51ii0rf6872aj5ab5qvf5fw3kdll4-python3-3.8.6-env/lib/python3.8/site-packages/cadquery/cq.py", line 3177, in _extrude
    thisObj = Solid.extrudeLinear(ws[0], [], eDir, taper)
IndexError: list index out of range

To be clear, the error is caused by ctx.pendingWires and pendingEdges being empty. I need to push the results from .edges() to them by calling toPending() before extruding.

Here are some issues where other users have run into this, and I'm sure I'm missing a few more:

So question 1, we could use a more informative error than IndexError for when we expect a pendingWire or Edge but get nothing, right?

I think these methods would all fail with an IndexError if used like above:

  • twistExtrude
  • cutThruAll
  • loft
  • _extrude
  • _revolve
  • _sweep

You could check in each Workplane method if ctx.pendingWires was empty, but to save code repetition I suggest we add a new method to CQContext that either will either return a pending wire/edge or raise an error like RuntimeError('Requested pending wires but none present in modelling context'). Then change the relevant methods to use this getter instead of directly accessing ctx.pendingWires. Also this would need a pop=True keyword option, considering that almost every method does:

        wires = self.ctx.pendingWires
        self.ctx.pendingWires = []

Question 2: Does this sound like a good approach?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions