Free function API for CadQeury

Introduction

What is CadQuery?

  • CadQuery is a Python package for parametric 3D modeling.
  • Based on a B-rep kernel as opposed to CSG or F-rep
  • Can import and export existing CAD models using STEP
  • Relies heavily on selectors and fluent APIs

Overview

Capabilities

  • Selectors DSL
  • Free function modeling API
  • Fluent modeling API
  • Sketches
  • Assemblies
  • 2D primitives
  • 3D primitives
  • Boolean operations
  • 3D operations
    • Extrude (tapered, twisted)
    • Revolve
    • Loft
    • Shell
    • Fillet, chamfer
    • Sweep / multi-section sweep
    • 3D text
  • Supported formats
    • BREP (R/W)
    • STEP (R/W)
    • DXF (2D only) (R/W)
    • STL (W)
    • AMF (W)
    • SVG (W)
    • and more …

Fluent example

import cadquery as cq

w, h = 30.0,40.0
th, dia = 10.0, 22.0
pad, rf = 12.0, 5
cb_dia1,cb_dia2, cb_d = 2.5, 5, 4
ch = .5

result = (cq.Workplane().box(h, w, th)
    .faces(">Z").workplane().hole(dia)
    .faces(">Z").workplane()
    .rect(h - pad, w - pad, forConstruction=True)
    .vertices().cboreHole(cb_dia1,cb_dia2, cb_d)
    .edges("|Z").fillet(rf).faces('>Z').chamfer(ch))

Free functions example

from cadquery.func import *

r = rect(h, w)
locs_cbore = [v.Center() for v in rect(h-pad, w-pad).vertices()]
c = face(circle(dia/2))
cbs1 = face(circle(cb_dia1/2)).moved(locs_cbore)
cbs2 = face(circle(cb_dia2/2)).moved(locs_cbore).moved(z=th)

base = extrude(face(r), (0,0,th))
base = fillet(base, base.edges('|Z'), rf)
base -= compound(extrude(c, (0,0,th)), extrude(cbs1,(0,0,th)))
base -= extrude(cbs2, (0,0,-cb_d))

result = chamfer(base, base.faces('>Z').edges(), ch)

Key points

  • No hidden state
  • We are operating on cq.Shape objects
  • Primitives are created using free functions
  • All operations are free functions
  • Boolean operations are also implemented as operators
    • + is fuse()
    • - is cut()
    • * is intersect()
    • / is split()
  • Selectors can be used on Shape
  • Objects are placed and replicated using .moved(...)
  • When applicable, multiple overloads of the same function are provided.

Other capabilities

  • Higher order topological entities can be assembled form lower order topological entities

    • Wire can be assembled from Edge objects
    • Face from Wire objects
    • Solid form Face objects
  • This allows to build more complicated objects and often optimize boolean operations away

Other capabilities

c1 = circle(1)
c2 = circle(2)

f_bot = loft(c1, c2)
sides = extrude(compound(c1,c2), (0,0,3))
f_top = loft(c1.moved(z=3), circle(1.5).moved(z=2.8), c2.moved(z=3))

result = solid(f_bot, sides, f_top)

Other capabilities

  • Shape objects can be checked for correctness

    • check(s: Shape) -> bool or err ==[]; check(s: Shape, err) -> bool
  • We can mix and match different APIs

    • cq.Workplane.add(...)
    • cq.Sketch.face(...)
    • cq.Workplane.val() -> cq.Shape, cq.Sketch.val() -> cq.Shape
    • cq.Assembly.add(...)

Roadmap

  • Polish and stabilize the API based on field experience
  • Add local operations
  • Add history object for optional tracking relations between entities

What else is new

  • Many additions to Shape
  • invoke, filter, apply and map added to Workplane and Sketch
  • Many quality of life improvements, e.g.
    • bool ops as operators
    • __iter__ for Workplane and Sketch
    • Workplane can operate on selected faces

Built-in visualization

from cadquery.vis import show

f = face(rect(1,1))
show(f)

show(f, f.outerWire().sample(100), Location(z=10, rz=45))
  • CadQuery has a built in visualization function
  • Very handy for ad-hoc debugging
  • Recently extended to support
    • Location and Vector visualization
    • Support for visualization of lists
    • Arbitrary vtkActor support
  • There is also rudimentary support of rendering in Jupyter

Built-in visualization

Built-in visualization

Projects using CadQuery

CaduQuery helps design cutting edge nuclear fusion reactors.

How to support us

  • Share your work and nice usage examples
  • Cite CadQuery using 10.5281/zenodo.3955118
  • Sponsor development of new features

Summary and outlook

  • Free function API is a valuable addition to CadQuery
  • Its development will be continued and it should become stable soon
  • Other features in preparation:
    • Integration with meshing tools
    • NURBS geometry layer in CadQuery

Thank you!