CadQuery / cadquery

A python parametric CAD scripting framework based on OCCT
https://cadquery.readthedocs.io
Other
3.15k stars 289 forks source link

Calling `faces` selector on Sketch changes what `wires` selector returns #1530

Closed zwn closed 6 months ago

zwn commented 6 months ago
import cadquery as cq

s = cq.Sketch()
s.rect(100, 100)
s.vertices().fillet(6)
#s.faces()
print(s.wires().vals())

This code prints an empty list. However, when I uncomment the call to s.faces() I do get one wire. I was expecting to get the one wire regardless.

Now thinking about it more I'd expect the call to s.faces().vals() to return a list with one face. It seems that the fillet call causes something strange to happen because before the fillet call, faces indeed returns a single face.

zwn commented 6 months ago

Calling faces() somehow fixes some internal state. It returns [] the first time I call it but the second time, I get the expected one Face list.

zwn commented 6 months ago

Oh my.

Note that selectors are implemented, but selection has to be explicitly reset. Sketch class does not implement history and all modifications happen in-place.

https://cadquery.readthedocs.io/en/latest/sketch.html

I have somehow missed this note in the documentation. Coming from the Workplane that is a pretty significant difference. I went through https://cadquery.readthedocs.io/en/latest/primer.html and it is all about selectors, stack, chaining etc.

Anyway. For anyone coming here in the future:

s = cq.Sketch()
s.rect(100, 100)
print(s._selection)
s.vertices().fillet(6)
print(s._selection)
s.reset()
print(s._selection)
print(s.wires().vals())

And the output

[]
[<cadquery.occ_impl.shapes.Vertex object at 0x7fd677ad23e0>, <cadquery.occ_impl.shapes.Vertex object at 0x7fd677ad22f0>, <cadquery.occ_impl.shapes.Vertex object at 0x7fd6eda711b0>, <cadquery.occ_impl.shapes.Vertex object at 0x7fd676c9e980>]
[]
[<cadquery.occ_impl.shapes.Wire object at 0x7fd6eda711b0>]

The hidden state is in _selection. After selecting vertices for the fillet, they are still selected and not "used up" as I've wrongly expected. After calling reset everything works as expected.