1. Products
  2.   CAD
  3.   Python
  4.   CadQuery

Open Source Python Library for 3D CAD File Formats

Free Python CAD library to create Parametric 3D CAD Models. It Create high quality CAD formats like STEP, AMF & 3MF in addition to traditional STL

The open source Python library CadQuery is an easy-to-use Python library for building parametric 3D CAD models. The library is very powerful and by writing short scripts can produce high quality CAD models inside Python applications. It is also possible to customize the script and make many different objects using a single script. The CadQuery scripts are very fast and can build STL, STEP, AMF, and 3MF faster than other available libraries.

The library offers advanced modeling abilities such as fillets, curvilinear extrudes, parametric curves, and lofts. It is a script-based library that can create a parametric model which can be easily customized by end users. It creates 3D models with scripts that are as close as possible to how you would describe the object to a human. CadQuery is licensed under the terms of the Apache Public License, version 2.0.

That CadQuery library is deliberately designed to be GUI-less, so that it can be used inside engineering and scientific applications for programmatically creating 3D models. If you need to use a GUI, you can consider Qt-based GUI CQ-editor or Jupyter extension jupyter-cadquery. It is also possible to use the library together with other Python libraries.

Previous Next

Getting Started with CadQuery

The easiest way to install CadQuery stable release is using pip. Please use the following command for a smooth installation.

Install CadQuery via pip

pip install cadquery 

You can download the compiled shared library from Github repository.

Work with Polygon via Python 3D Library

A polygon is a two-dimensional shape with straight sides. Polygonal modeling is an approach for modeling objects by representing or approximating their surfaces using polygon meshes. The Open source Python Library CadQuery enables software developers to create polygons for each stack point with just a couple of lines of Python code. It is very beneficial in 3d printers whose firmware does not correct for small hole sizes.

Create Workplanes on Faces via Python 3D Library

The Open source Python Library CadQuery has provided complete support for creating Workplanes on Faces inside Python applications. the use of workplanes in this way are a key feature of CadQuery. It frees users from searching the position of various features in variables, and enables the model to delete the redundant dimensions. Users can select the faces of the resultant solid by calling the Workplane.faces () method. Please remember that by default the origin of a new workplane is calculated by making a plane from the selected face and projecting the previous origin onto that plane.

Creating Workplanes on Faces via Python

result = cq.Workplane("front").box(2, 3, 0.5)  # make a basic prism
result = result.faces(">Z").workplane().hole(0.5)  # find the top-most face and make a hole

Working with 3D LEGO Bricks via Python

The CadQuery library has provided complete support for creating 3D animation representing the Lego bricks in different size using Python commands. It is also possible to recreate bricks by joining several cubes and cylinders together. We can create a compound object to join these 3D shapes together in a single object (brick). The following example demonstrates how to generate any size regular rectangular Lego(TM) brick. It’s only tricky because of the logic regarding the underside of the brick.

Create 3D LEGO Bricks via Python

# Inputs
lbumps = 6       # number of bumps long
wbumps = 2       # number of bumps wide
thin = True      # True for thin, False for thick

# Lego Brick Constants-- these make a lego brick a lego :)
pitch = 8.0
clearance = 0.1
bumpDiam = 4.8
bumpHeight = 1.8
if thin:
    height = 3.2
    height = 9.6

t = (pitch - (2 * clearance) - bumpDiam) / 2.0
postDiam = pitch - t  # works out to 6.5
total_length = lbumps*pitch - 2.0*clearance
total_width = wbumps*pitch - 2.0*clearance

# make the base
s = cq.Workplane("XY").box(total_length, total_width, height)

# shell inwards not outwards
s = s.faces("Z").workplane(). \
    rarray(pitch, pitch, lbumps, wbumps, True).circle(bumpDiam / 2.0) \

# add posts on the bottom. posts are different diameter depending on geometry
# solid studs for 1 bump, tubes for multiple, none for 1x1
tmp = s.faces(" 1 and wbumps > 1:
    tmp = tmp.rarray(pitch, pitch, lbumps - 1, wbumps - 1, center=True). \
        circle(postDiam / 2.0).circle(bumpDiam / 2.0).extrude(height - t)
elif lbumps > 1:
    tmp = tmp.rarray(pitch, pitch, lbumps - 1, 1, center=True). \
        circle(t).extrude(height - t)
elif wbumps > 1:
    tmp = tmp.rarray(pitch, pitch, 1, wbumps - 1, center=True). \
        circle(t).extrude(height - t)
    tmp = s

# Render the solid