.. _freefuncapi: ***************** Free function API ***************** .. warning:: The free function API is experimental and may change. For situations when more freedom in crafting individual objects is required, a free function API is provided. This API has no hidden state, but may result in more verbose code. One can still use selectors as methods, but all other operations are implemented as free functions. Placement of objects and creation of patterns can be achieved using the various overloads of the moved method. Currently this documentation is incomplete, more examples can be found in the tests. Tutorial -------- The purpose of this section is to demonstrate how to construct Shape objects using the free function API. .. cadquery:: :height: 600px from cadquery.func import * dh = 2 r = 1 # construct edges edge1 = circle(r) edge2 = circle(1.5*r).moved(z=dh) edge3 = circle(r).moved(z=1.5*dh) # loft the side face side = loft(edge1, edge2, edge3) # bottom face bottom = fill(side.edges('Z'), side, [(0,0,1.6*dh)]) # assemble into a solid s = solid(side, bottom, top) # construct the final result result = s.moved((-3*r, 0, 0), (3*r, 0, 0)) The code above builds a non-trivial object by sequentially constructing individual faces, assembling them into a solid and finally generating a pattern. It begins with defining few edges. .. code-block:: python edge1 = circle(r) edge2 = circle(2*r).moved(z=dh) edge3 = circle(r).moved(z=1.5*dh) Those edges are used to create the side faces of the final solid using :meth:`~cadquery.occ_impl.shapes.loft`. .. code-block:: python side = loft(edge1, edge2, edge3) Once the side is there, :meth:`~cadquery.occ_impl.shapes.cap` and :meth:`~cadquery.occ_impl.shapes.fill` are used to define the top and bottom faces. Note that :meth:`~cadquery.occ_impl.shapes.cap` tries to maintain curvature continuity with respect to the context shape. This is not the case for :meth:`~cadquery.occ_impl.shapes.fill`. .. code-block:: python # bottom face bottom = fill(side.edges('Z'), side, [(0,0,1.75*dh)]) Next, all the faces are assembled into a solid. .. code-block:: python s = solid(side, bottom, top) Finally, the solid is duplicated and placed in the desired locations creating the final compound object. Note various usages of :meth:`~cadquery.Shape.moved`. .. code-block:: python result = s.moved((-3*r, 0, 0), (3*r, 0, 0)) In general all the operations are implemented as free functions, with the exception of placement and selection which are strictly related to a specific shape. Primitives ---------- Various 1D, 2D and 3D primitives are supported. .. cadquery:: from cadquery.func import * e = segment((0,0), (0,1)) c = circle(1) f = plane(1, 1.5) b = box(1, 1, 1) result = compound(e, c.move(2), f.move(4), b.move(6)) Boolean operations ------------------ Boolean operations are supported and implemented as operators and free functions. In general boolean operations are slow and it is advised to avoid them and not to perform the in a loop. One can for example union multiple solids at once by first combining them into a compound. .. cadquery:: from cadquery.func import * c1 = cylinder(1, 2) c2 = cylinder(0.5, 3) f1 = plane(2, 2).move(z=1) f2 = plane(1, 1).move(z=1) e1 = segment((0,-2.5, 1), (0,2.5,1)) # union r1 = c2 + c1 r2 = fuse(f1, f2) # difference r3 = c1 - c2 r4 = cut(f1, f2) # intersection r5 = c1*c2 r6 = intersect(f1, f2) # splitting r7 = (c1 / f1).solids('Z') # inner face inner = extrude(circle(r), (0,0,w)) # add holes to the bottom and top face b_bot_hole = b_bot.addHole(inner.edges('Z')) # construct the final solid result = solid( b.remove(b_top, b_bot).faces(), #side faces b_bot_hole, # bottom with a hole inner, # inner cylinder face b_top_hole, # top with a hole ) If the base shape is more complicated, it is possible to use local sewing that takes into account on indicated elements of the context shape. This, however, necessitates a two step approach - first a shell needs to be explicitly sewn and only then the final solid can be constructed. .. cadquery:: from cadquery.func import * w = 1 h = 0.1 r = 0.9*w/2 # box b = box(w, w, w) # top face b_top = b.faces('>Z') # protrusion feat_side = extrude(circle(r).moved(b_top.Center()), (0,0,h)) feat_top = face(feat_side.edges('>Z')) feat = shell(feat_side, feat_top) # sew into a shell # add hole to the box b_top_hole = b_top.addHole(feat.edges('