CadQuery Selector Full Capability Via Code
Since CadQuery designs are described via Python code syntax, it has several code-friendly advantages that I hope will prove useful. But I was wary of CadQuery selector examples like .edges("|Z")
to select all edges parallel to the Z axis. This seems useful, but "|Z" means an opaque string as far as Python syntax checking is concerned. Special syntax for strings embedded in source code is nothing new, inline assembly and inline SQL as the top two examples coming to my mind. And as one language hiding inside another, I've always encountered problems with them.
So I was happy to learn that such strings are merely a shorthand and not selectors' true form. Officially they are Python code just like the rest of CadQuery. That "|Z" shorthand translates into a call to cadquery.selectors.ParallelDirSelector()
with a vector pointing along the Z axis. That is one of several listed in selector section of the API reference. In addition to selectors getting their own page in CadQuery documentation. The string shorthand is itself a Python selector method StringSyntaxSelector
.
Selectors were one of the biggest features that motivated me to ramp up on CadQuery, so learning their implementation as actual methods gave me peace of mind. It means if I run into problems with the special text string, I can always fall back to code. The other advantage is that using long form code syntax selectors open up capabilities not (yet?) available via a shorthand string. Three selectors caught my attention:
-
NearestPointSelector
to select an entity near the given coordinate. Useful for finding that one face or edge at a specific known point. -
BoxSelector
to select all entities within a box. This should allow me to do things like selecting all edges where two objects met up, so I can perform a chamfer operation to smooth edges transitioning from one object to another. -
BaseDirSelector
may be part of a solution to build a CadQuery UI. GUI CAD users are accustomed to clicking on an object to select it. This involves performing a 3D ray cast to transform the mouse click coordinate backwards through the 3D camera transform to obtain a vector through 3D space. I think that vector can be passed into this selector to perform hit-testing, but I could be wrong. This is pretty far down the line (if I get to it at all) so there's little immediate need to figure it out just yet. It's one of many potential CadQuery extension project ideas.