Visualization

Pure Python

Since version 2.4 CadQuery supports visualization without any external tools. Those facilities are based on the VTK library and are not tied to any external tool.

from cadquery import *
from cadquery.vis import show

w = Workplane().sphere(1).split(keepBottom=True) - Workplane().sphere(0.5)
r = w.faces('>Z').fillet(0.1)

# Show the result
show(r, alpha=0.5)
_images/show.PNG

One can visualize objects of type Workplane, Sketch, Assembly, Shape, Vector, Location and lists thereof.

from cadquery import *
from cadquery.func import *
from cadquery.vis import show

w = Workplane().sphere(0.5).split(keepTop=True)
sk = Sketch().rect(1.5, 1.5)
sh = torus(5, 0.5)

r = rect(2, 2)
c = circle(2)

N = 50
params = [i/N for i in range(N)]

vecs = r.positions(params)
locs = c.locations(params)

# Render the solid
show(w, sk, sh, vecs, locs)
_images/show_demo.PNG

Additionally it is possible to integrate with other libraries using VTK and display any vtkProp object.

from cadquery.vis import show
from cadquery.func import torus

from vtkmodules.vtkRenderingAnnotation import vtkAnnotatedCubeActor


a = vtkAnnotatedCubeActor()
t = torus(5,1)

show(t, a)
_images/show_vtk.PNG

Note that currently the show function is blocking.

Screenshots

show() allows additionally to take screenshots in png format. One can specify zoom, camera position and windows size.

from cadquery.vis import show
from cadquery.func import box

b = box(1,1,1)

show(b, width=800, height=800, screenshot='img.png', zoom=2, roll=-20, elevation=-30, interact=False)

Warning

Intermittent issues were observed with this functionality, please submit detailed bug reports in case of problems.

Sometimes it is desirable to control the camera position precisely. This can be achieved as follows.

from cadquery.vis import show
from cadquery.func import torus

R = 10
r = 1
h = 2

t = torus(R, r)

show(t, position=(R, -R, R/h), roll=-45, zoom=0.9)
_images/show_camera_position.png

Control points

ctrlPts() allows to visualize control points of surfaces and curves.

from cadquery.func import *
from cadquery.vis import *

c = circle(1).toSplines()
spine = spline([(0, 0, 0), (-3, -3, 5)], tgts=[(0, 0, 1), (0, -1, 0)])
f = sweep(c, spine)

show(
    f,
    ctrlPts(f),
    spine.moved(x=7),
    ctrlPts(spine.moved(x=7), color="green"),
    alpha=0.0,
)
_images/ctrl_pts.png

Note that for some geometries explicit conversion to spline representation might be needed. toSplines() performs approximate conversion and toNURBS() performs exact one.

Styling

Fine-grained control of the appearance of every item can be achieved using style().

from cadquery.vis import *
from cadquery.func import *

show(
    style(
        torus(10, 2),
        color="crimson",
        tubes=True,
        linewidth=5,
        mesh=True,
        meshcolor="blue",
        tolerance=0.1,
    ),
    style(box(3, 3, 3), color="green", markersize=0.1, alpha=0.5),
)
_images/show_styling.png

Jupyter/JupterLab

There is also more limited support for displaying Workplane, Sketch, Assembly, Shape in Jupyter and JupyterLab. This functionality is implemented using VTK.js.

from cadquery import *

Workplane().sphere(1).split(keepTop=True)
_images/show_jupyter.PNG