one could define functions for individual features and then write this:
result = cq.Workplane("XY") >> Body >> CenterHole >> MountHoles >> (FilletZ, 2.0)
Cq uses method chaining to great effect, with workplane serving as coin of the realm. However, as in OpenSCAD , it's all too easy to create a long, hard to decipher chain, essentially creating write-only code. In OpenSCAD, modules came to the rescue, creating a module for each feature and the modules naturally chain in with existing functions. As a nice side effect, features become easily testable. For cq, it's not quite so natural to mix cq and user level code, unless you add your code as methods to the global workspace class. That doesn't scale well. But you can add a single chaining operator to Workplane, so code looks something like:
import cadquery as cq
(height, width, thickness) = (60.0, 80.0, 10.0)
(diameter, padding) = (22.0, 12)
#add function chaining operator to Workplane
cq.Workplane.__rshift__ = lambda self, a: a[0](self, *a[1:]) if type(a) == tuple else a(self)
def Body(wp): return wp.box(height, width, thickness)
def CenterHole(wp): return wp.faces(">Z").workplane().hole(diameter)
def MountHoles(wp):
return (wp.faces(">Z").workplane()
.rect(height - padding, width - padding, forConstruction=True)
.vertices().cboreHole(2.4, 4.4, 2.1))
def FilletZ(wp, dia): return wp.edges("|Z").fillet(dia)
result = cq.Workplane("XY") >> Body >> CenterHole >> MountHoles >> (FilletZ, 2.0)
Really that's too simple of a model to bother decomposing. but it gives the gist of the technique. Note I didn't implement kwargs. That should be doable, although a bit clunky.
Anyhow, I'm a python noob, so there are probably better ways to do this. (pypy's python--chain, for example, which isn't a great fit for this). I could also imagine a different syntax that isn't specific to workplane:
chain((cq.Workplane, "XY"), Body, CenterHole, MountHoles, (FilletZ, 2.0))
Anyhow, does this look generally useful? Are there ways to do this better?