Home

Awesome

CQ-Kit Logo

CQ-Kit

https://pypi.org/project/cqkit/ python version https://github.com/CadQuery/cadquery https://github.com/michaelgale/cq-kit/blob/master/LICENSE <a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>

codecov https://github.com/michaelgale/cq-kit/issues

This repository contains utility classes and functions to extend the features and capabilities of the CadQuery python library. CadQuery is a python module designed to build parametric 3D CAD models. Since CadQuery is based on python, you can develop a very capable scripted software stack to build 3D models and produce all of the various asset files used for design, prototyping, documentation and manufacturing. An example use case is how Fx Bricks makes hobby products and is described here.

CQ-Kit is designed to be easily included as a companion module to CadQuery and extends its functionality in these key areas:

Installation

Assuming CadQuery is installed, you can install CQ-Kit using a PyPI package as follows:

$ pip install cqkit

The CQ-Kit package can also be installed directly from the source code:

  $ git clone https://github.com/michaelgale/cq-kit.git
  $ cd cq-kit
  $ pip install .

If you want to create a fresh anaconda environment with CadQuery and CQ-Kit:

  $ cd cq-kit
  $ conda env create -f environment.yml --name $MY_NAME
  $ conda activate $MY_NAME
  $ conda install -c conda-forge -c defaults -c cadquery python=$VERSION cadquery=master
  $ pip install .

Substitute your desired python $VERSION with 3.8, 3.9, or 3.10 and optionally replace $MY_NAME with a different desired environment name than the default of cadquery specified in environment.yml.

Basic Usage

After installation, the package can imported:

    $ python
    >>> import cqkit
    >>> cqkit.__version__

An example of the package can be seen below

    import cadquery as cq
    from cqkit import *
    # make a simple box
    r = cq.Workplane("XY").rect(3, 5).extrude(2)
    # export the box with StepFileExporter
    export_step_file(r, "mybox.step", title="My Awesome Box", author="Michael Gale")

File I/O

CQ-Kit adds some convenient file import and export functions to a few supported formats.

Discrete Geometry

CQ-Kit includes functions to discretize either edges or solids:

Pretty Printers for Objects

CQ-Kit offers useful functions which return a string representing a geometric object. The string representation is automatically determined by the type of object. Objects which are containers for multiple other objects are automatically expanded, e.g. a Wire will expand its Edges and those edges will expand into coordinate tuples.

Examples

pprint_obj((1, 2))
# ( 1, 2)
pprint_obj((3, 4, 5))
# ( 3, 4, 5)
pprint_obj(Vector(-2, -4, 0))
# (-2,-4, 0)
pprint_obj(Vertex.makeVertex(-1, 0, 2))
# (-1, 0, 2)
pprint_obj(gp_Vec(8, 9, 10))
# ( 8, 9, 10)
r = cq.Workplane("XY").rect(1, 2)
pprint_obj(r.edges().vals())
# 4x Edges
# 1/4 Line: ( -0.5, -1,  0) -> (  0.5, -1,  0) length:  1
# 2/4 Line: (  0.5, -1,  0) -> (  0.5,  1,  0) length:  2
# 3/4 Line: (  0.5,  1,  0) -> ( -0.5,  1,  0) length:  1
# 4/4 Line: ( -0.5,  1,  0) -> ( -0.5, -1,  0) length:  2
r = cq.Workplane("XY").rect(1, 2).extrude(5)
pprint_obj(r)
# Compound (1x Solid), Solid (6x Faces)
#   1/6 Face (1x Wire), Wire (4x Edges) length:  12
#       1/4 Line: ( -0.5, -1,  0) -> ( -0.5, -1,  5) length:  5
#       2/4 Line: (  0.5, -1,  0) -> (  0.5, -1,  5) length:  5
#       3/4 Line: ( -0.5, -1,  0) -> (  0.5, -1,  0) length:  1
#       4/4 Line: ( -0.5, -1,  5) -> (  0.5, -1,  5) length:  1

#   2/6 Face (1x Wire), Wire (4x Edges) length:  14
#       1/4 Line: (  0.5, -1,  0) -> (  0.5, -1,  5) length:  5
#       2/4 Line: (  0.5,  1,  0) -> (  0.5,  1,  5) length:  5
#       3/4 Line: (  0.5, -1,  0) -> (  0.5,  1,  0) length:  2
#       4/4 Line: (  0.5, -1,  5) -> (  0.5,  1,  5) length:  2

#   3/6 Face (1x Wire), Wire (4x Edges) length:  12
#       1/4 Line: (  0.5,  1,  0) -> (  0.5,  1,  5) length:  5
#       2/4 Line: ( -0.5,  1,  0) -> ( -0.5,  1,  5) length:  5
#       3/4 Line: (  0.5,  1,  0) -> ( -0.5,  1,  0) length:  1
#       4/4 Line: (  0.5,  1,  5) -> ( -0.5,  1,  5) length:  1

#   4/6 Face (1x Wire), Wire (4x Edges) length:  14
#       1/4 Line: ( -0.5,  1,  0) -> ( -0.5,  1,  5) length:  5
#       2/4 Line: ( -0.5, -1,  0) -> ( -0.5, -1,  5) length:  5
#       3/4 Line: ( -0.5,  1,  0) -> ( -0.5, -1,  0) length:  2
#       4/4 Line: ( -0.5,  1,  5) -> ( -0.5, -1,  5) length:  2

#   5/6 Face (1x Wire), Wire (4x Edges) length:  6
#       1/4 Line: ( -0.5, -1,  0) -> (  0.5, -1,  0) length:  1
#       2/4 Line: (  0.5, -1,  0) -> (  0.5,  1,  0) length:  2
#       3/4 Line: (  0.5,  1,  0) -> ( -0.5,  1,  0) length:  1
#       4/4 Line: ( -0.5,  1,  0) -> ( -0.5, -1,  0) length:  2

#   6/6 Face (1x Wire), Wire (4x Edges) length:  6
#       1/4 Line: ( -0.5, -1,  5) -> (  0.5, -1,  5) length:  1
#       2/4 Line: (  0.5, -1,  5) -> (  0.5,  1,  5) length:  2
#       3/4 Line: (  0.5,  1,  5) -> ( -0.5,  1,  5) length:  1
#       4/4 Line: ( -0.5,  1,  5) -> ( -0.5, -1,  5) length:  2

Note that you can pass in either obj.edges().val(), obj.edges().vals(), obj.edges() etc. and the correct string representation will automatically be inferred. For more complex or compound objects, pprint_obj will recursively unwrap the hierarchy of shapes as well as computing length, radius, and coordinate data where applicable. Additionally, coordinate values are represented with colour highlighting. You will need install the crayons python module in order to see colour highlighting, otherwise it will use your terminal default style. crayons is optional and CQ-Kit will detect its availability.

<img src=./images/pprintsample.png>

XSection Class

The XSection object is a convenience container for points which represent a closed path cross-section. The points can be supplied either as-is or as the mirror-half of all the points. If provided as a mirrored/symmetric half, then only one half of the points need be specified and the other opposite (mirrored) points will automatically be generated.

This container object is useful for storing cross-sectional profiles which are used for extruded/lofted solid objects. It is also useful for obtaining variants of the cross-section such as:

The cross-section is initialized with the workplane ("XY", "XZ", "YZ", etc.), its 2D points (all points for unsymmetric, or half the points for symmetric), and if symmetric, then a specification of the mirror axis, e.g. for points in the XY plane, mirror_axis=X means that either the upper or lower half of the points are specified and mirror_axis=Y means that either the left or right half of the points are specified.

Points are usually supplied as 2D tuples; however, special points which result in curved lines can be specified with a simple dictionary:

- { "radiusArc": ((2, 3), 1) }
- { "tangentArc": (2, 3) }

A list of points can involve a mix of types such as:

  [ (0, 0), (3, 0), (2.5, 0.5), (2.5, 4), {"radiusArc": ((2, 4.5), -0.5)}, (0, 4.5) ]

Examples

    # half a triangle on XY plane
    xc = XSection([(0,0), (1,0), (0, 3)], "XY", symmetric=True, mirror_axis="Y")
    # get the outline object
    r = xc.render()
    # get an upside down outline object
    r = xc.render(flipped=True)
    # get an extruded version 2x taller:
    r = xc.render(scaled=(1, 2)).extrude(10)

<img src=./images/xsection.png>

Ribbon Class

The Ribbon class generates an arbitrary closed wire path of constant width. The path of ribbon/wire is described by a list of "turtle graphics" style plotting commands. From the starting position, one side of of the ribbon is drawn by parsing the commmands from start to finish. The opposite side of the ribbon is then drawn by parsing the commands in reverse order.

The commands describing the path are contained in a list of 2 element tuples. The first item of each tuple is a command, and the second item is a dictionary.

An example command list is as follows:

path = [
    ("start", {"position": (10.0, 0.0), "direction": 30.0, "width": 0.5}),
    ("line", {"length": 2.0}),
    ("arc", {"radius": 2.0, "angle": 145.0}),
    ("line", {"length": 2}),
    ("arc", {"radius": 0.5, "angle": -170}),
    ("line", {"length": 3}),
]

Alternatively, since CQ-Kit v.0.5.4, a more compact dictionary representation of the command list can be provided. It consists of a two element dictionary with the keys "start" and "path" shown as follows:

path = {
    "start": "(10,0) D30 W0.5",
    "path": "L:2.0 A:2/145 L2 arc(0.5,-170) line:3",
    }

Each element contains a string description of the start and path of the ribbon. The string consists of whitespace delimited tokens representing values. The values start with a case insensitive token followed by an optional colon ":" and the value. Value tokens are case insensitive and can be abbreviated up to the first character, e.g. "Line", "line", "li", "L" are all valid. Multiple values can be delimited by either a comma "," or slash "/" and optionally enclosed in brackets "()"; however, they should not contain any additional whitespace separators. As an example, all of these forms are valid:

# minimally concise
path = {
    "start": "(10,0) D30 W0.5",
    "path": "L2.0 A2/145 L2 A0.5,-170 L3",
    }
# maximally consise
path = {
    "start": "(10,0) direction:30 width:0.5",
    "path": "line:2.0 arc:(2,145) line:2.0 arc:(0.5,-170) line:3.0",
    }
# combination
path = {
    "start": "(10,0) dir:30 w:0.5",
    "path": "L:2 arc:2,145 L:2 arc(0.5,-170) L:3",
    }

A Ribbbon can be initialized with the commands argument passed either with the list of tuples or the compact dictionary of strings.

A Ribbon object is initialized with CadQuery workplane specification representing the 2D plane which the ribbon is constructed and a command list. The render method is called to construct the ribbon object and it is returned as a closed wire path CadQuery workplane object. This object can then be chained as any other CQ workplane object, e.g. using extrude() to transform the ribbon object into a 3D solid.

<img src=./images/ribbon.png>

Selector Classes

CQ-Kit extends CadQuery's powerful Selector base class with some additional utility classes. They are described below and are grouped by Selectors for edges and faces. Almost all of these custom selector classes can be passed a tolerance keyword argument to control the numerical tolerance of filtering operations (usually based on length).

The CQ-Kit Selector classes are categorized as follows:

  1. Geometric Property Selectors
  2. Orientation Selectors
  3. Association Selectors
  4. Location Selectors

1. Geometric Property Selectors

Grouped as follows:

2. Orientation Selectors

3. Association Selectors

4. Location Selectors

Selector Examples

<img src=./images/edgelen2.png width=220><img src=./images/edgelen3-14.png width=220><img src=./images/edgelen-35.png width=220>
# selects edges from solid with length 3.0
es = EdgeLengthSelector(3.0)
r = solid.edges(es)
# selects edges with a list of edge lengths
es = EdgeLengthSelector([3.0, 1.4])
# selects edges using string rules with >, <, >=, <=
es = EdgeLengthSelector(">3.5")
# selects edges which are 4.0 +/- 0.5 long
es = EdgeLengthSelector(4.0, tolerance=0.5)
<img src=./images/vertedges.png width=220><img src=./images/vertedge3.png width=220>
# selects all vertical edges
vs = VerticalEdgeSelector()
r = solid.edges(vs)
# selects all vertical edges 3.2 and 2.0 mm tall
vs = VerticalEdgeSelector([3.2, 2.0])
<img src=./images/planaratheight1.png width=220>
# select all edges at height = 1.0
es = FlatEdgeSelector(1.0)
# select all edges at heights 2, 5
es = FlatEdgeSelector([2, 5])
<img src=./images/xcoord3both.png width=300><img src=./images/xcoord3.png width=300>
# selects all edges which have X coordinate values = 3.0 at both ends
es = HasXCoordinateSelector(3.0, min_points=2)
# selects all edges which have X coordinate values = 3.0 at least one end
es = HasXCoordinateSelector(3.0, min_points=1)
<img src=./images/sharedvertex.png width=280>
# selects all edges which have one of its end points common with a specific vertex
es = SharedVerticesWithObjectSelector(Vector(1, 2, 1))
<img src=./images/commonvertface.png width=390>
# selects all edges which have any of its end points common with any vertex
# belonging to a specified face
face1 = solid.faces(FlatFaceSelector(1.0)).val()
es = SharedVerticesWithObjectSelector(face1)

Miscellaneous Helper Functions

CQ-Kit has several small convenience functions for performing simple object transformations or interogating attributes. These include:

# rotate object about either the X, Y, Z axis
r = rotate_x(obj, angle)
r = rotate_y(obj, angle)
r = rotate_z(obj, angle)
# or with fluent interface:
r = r.rotate_x(angle)
# move object centred about the origin
r = recentre(obj, axes=None, to_pt=None)
# axes defaults to "XYZ", i.e. centre the object in all three axes
# However, axes can be any string combination of "XYZ" to selectively
# centre about one or more desired axes:
r = recentre(obj, axes="XY")
# recentre just in X, Y with Z position the same
# to_pt optionally re-centres the object about another point instead
# of the origin:
r = recentre(obj, to_pt=(-5, 0, 10))

# or with fluent interface:
r = r.recentre(axes="YZ")
r = composite_from_pts(obj, pts, workplane="XY")
# returns the composite (union) of an object copied to list of locations
# specified by pts.
xlen, ylen = size_2d(obj)
# size of object in X, Y
xlen, ylen, zlen = size_3d(obj)
# size of object in X, Y, Z
(xmin, ymin), (xmax, ymax) = bounds_2d(obj)
# bounding box limits of object in X, Y
(xmin, ymin, zmin), (xmax, ymax, zmax) = bounds_3d(obj)
# bounding box limits of object in X, Y, Z
xc, yc, zc = centre_3d(obj)
# bounding box mid point of object in X, Y, Z
r = rounded_rect_sketch(length, width, radius=0)
# returns a cq.Sketch() of a rectangle shape with optional rounded corners
r = multi_extrude(obj, levels, face=">Z")
# returns a new object made by extruding a desired face from a reference
# object one or more successive levels
# levels are specified as a list of additional extrusion lengths and/or
# extrusion length/taper angle pairs, e.g.
rs = cq.Workplane("XY").placeSketch(rounded_rect_sketch(3, 4, 0.5)).extrude(1)
r = multi_extrude(rs, [5, (2, -45), 2, (2, 45), 3])
# extrudes a rounded box from its top face:
# - up 5 units
# - up 2 units at 45 degrees outward
# - up 2 units
# - up 2 units at 45 degrees inward
# - up 3 units
# new object is 15 units tall with 5x added extruded segments from its top
# Note that tapered extrusions are automatically corrected to the correct
# projected length, i.e. a segment will always be projected to the same
# length independent of taper angle.
r = extrude_xsection(obj, axis, extent, axis_offset=0, cut_only=False):
"""Cuts a cross-section through a solid along an axis and then
extrudes the exposed cross section over a desired extent.
axis is specified as either 'x', 'y', or 'z' and an optional
axis cut location can be specified instead of the default of 0
co-incident with the origin.  The sign of 'extent' determines
the direction from which the exposed face is extruded."""
# e.g.
rs = cq.Workplane("XZ").circle(4).extrude(5)
r = extrude_xsection(rs, "z", 4, axis_offset=0.5)
# returns a object derived from slicing a cylinder axially at z=0.5 and
# extruding the resulting exposed rectangular face upwards by 4 units
r = extrude_xsection(rs, "z", 4, axis_offset=0.5, cut_only=True)
# returns the same sliced cylinder without extruding the exposed cut face
# Alternative boolean operators to CadQuery union, cut, intersect
# methods.  These alternatives are helpful in situations where merging
# complex geometries result in strange artifacts or a proliferation
# of superfluous edges, faces, etc. The resulting solids are often
# "cleaner" and consolidate extra faces, edges, etc.  The caveat is
# a potential loss of accuracy; however, this can be tuned with the
# tolerance parameter.

r = cq_bop_fuse(obj1, obj2, tolerance=1e-5)
# returns the union of obj1 and obj2 using the OpenCascade "fuzzy"
# boolean operator algorithm with adjustable tolerance

r = cq_bop_cut(obj1, obj2, tolerance=1e-5):
# returns the cut of obj1 and obj2 using the OpenCascade "fuzzy"
# boolean operator algorithm with adjustable tolerance

r = cq_bop_intersect(obj1, obj2, tolerance=1e-5):
# returns the intersection of obj1 and obj2 using the OpenCascade "fuzzy"
# boolean operator algorithm with adjustable tolerance

r = inverse_fillet(obj, "<Z", 0.5)
# returns obj with 0.5 mm fillets applied on its bottom Z face with the
# fillets curving away from the object as if they were applied against
# a virtual plane co-incident with the "<Z" face.

r = inverse_chamfer(obj, "<Z", 0.5)
# same as inverse_fillet, but with chamfers applied instead

# or with a fluent interface:
r = r.inverse_fillet("<Z", 1.0)

To Do

Releases

Authors

CQ-Kit was written by Michael Gale