cadquery
Sketch
Workplane
Assembly

Sketch.offset()

The Sketch.offset() method in CadQuery is used to create a new sketch that is offset from an existing sketch. This new sketch can be used to generate 3D geometries by lofting, extruding, or other operations.

Syntax

offset(distance, keep_source=False)
  • distance (float): The distance by which the sketch should be offset. Positive values will offset the sketch outwards, while negative values will offset it inwards.
  • keep_source (bool): Whether to keep the original sketch, or delete it and only keep the new offset sketch. Default is False.

Parameters

distance

The distance parameter is a float value that determines the distance by which the sketch is to be offset. Positive values will offset the sketch outwards from its original position, while negative values will offset it inwards. The offset distance can be any positive or negative float value.

keep_source

The keep_source parameter is a boolean value that determines whether to keep the original sketch or just the new offset sketch. If set to True, both sketches will remain in the model history, while setting it to False will delete the original sketch and keep only the new offset sketch.

Example

# Import necessary libraries
import cadquery as cq

# Create a sketch
s = cq.Workplane("XY").rectangle(2, 2).edges()

# Offset the sketch
offset_s = s.offset(0.5)

# Extrude the offset sketch
result = offset_s.toPending().extrude(1)

# Display in 3D viewer
show_object(result)

This example creates a simple rectangular sketch on the XY-plane with a length and width of 2 units each. The sketch is then offset by 0.5 units using the offset() method, creating a new sketch that is offset from the original. The offset sketch is then extruded into a 3D solid using the extrude() method, and the resulting shape is displayed in a 3D viewer.

Offset Sketch Example

Note

  • The Sketch.offset() method can only be applied to a planar 2D sketch, not to a 3D solid or face.
  • The offset sketch is automatically closed, even if the original sketch was not closed.
  • If the offset distance is too large, the resulting sketch may self-intersect or overlap itself. It's recommended to use small incremental offsets and check the results at each step.