3D Modelling

CADQUERY and Python – Automated Parametric models

Getting started:

Make a series of points, join them, and then revolve around an axis

import cadquery as cq
from cadquery import exporters

# parameters
box_w = 5.0
box_d = 5.0
circle_d = 2.0


s = cq.Workplane("XY")
sPnts = [
    (5.55, 1.5),
    (4.5, 1.75),
    (3.0, 1.5),
    (1.5, 1.0),
    (1.0, 1.25),
    (0.5, 1.0),
    (0, 1.0)
]
result = s.lineTo(5.75, 0).lineTo(5.75, 1).spline(sPnts,includeCurrent=True).close()
result = result.revolve(270,(0,0,0),(1,0,0))
exporters.export(result, 'op.step')# Set up the length, width, and thickness

270 revolved closed spline

All code made with Python (no GUI!)

Make a parametric 3D model of a candlestick:

cadquery
Parametric candlestick created completely with Python code using cadquery
Using Python code and Cadquery methods we created this 3D model which can be exported as a .STEP file for a CNC or Printer.
Python and Cadquery example code used to make the parametric model :
import cadquery as cq
from cadquery import exporters

# parameters
length = 16
llow_stem = -(length/2)
bdia = 1.1

s = cq.Workplane("XY")
sPnts = [
    (length, 1.9),
    (length-2, 1.35),
    (length-5, 1.25),
    (5, 1.5),
    (1.5, 1.0),
    (1.0, 1.35),
    (0.5, 1.0),
    (0, 1.0)
]

result =
s.lineTo(length,0).lineTo(length,+1).spline(sPnts,includeCurrent=True).close()

# revolve the 2d profile to make the stem
result = result.revolve(360,(0,0,0),(1,0,0))

# extrude the lower stem
result = result.copyWorkplane(
# create a temporary object with the required workplane
              cq.Workplane("right", origin=(5, 0, 0))
          ).circle(bdia).circle(0.4).extrude(llow_stem)

# Add the 2 wider cylinders for the base
result = result.faces("<X").workplane(offset=0)       
#workplane is offset from the object surface
result = result.circle(length/6).extrude(1.5)
#
result = result.faces("<X").workplane(offset=0)       
#workplane is offset from the object surface
result = result.circle(length/4).extrude(1.5)
#
result = result.fillet(0.2).faces(">X").circle(0.9).cutThruAll()
result = result.rotateAboutCenter((0,1,0),270)
                       
exporters.export(result, 'op.step')
cadquery-example
Once you have output from your python code you can open the .STEP file in a 3d modelling program, eg FreeCAD or SOLIDWORKS
Quick wood texture render by opening the .STEP file in FreeCAD
Quick wood texture render by opening the .STEP file in FreeCAD

Watch the video on how to make a 3d parametric model with Python