cadquery
Sketch
Workplane
Assembly

Sketch.push()

The Sketch.push() method is a function in the CADquery library that allows the user to save the current state of the sketch and continue to draw new geometry. This function creates a backup of the current sketch state and allows the user to easily revert back to the previous state.

Syntax

 CADSketch.push()

Parameters

The push() method does not take any parameters.

Return Value

This method returns nothing.

Description

When working on complex sketches, it can be difficult to keep track of every modification made. The push() method helps in such cases by creating a backup of the current sketch state. When new geometry is drawn, the newly created state is saved on top of the previous one.

The push() method creates a stack of altered states of the sketch, and as long as one keeps pushing new states to the stack, it can easily revert to a previous one using the pop() method.

Examples

Here is a simple example demonstrating how to use the Sketch.push() method:

import cadquery as cq

# Create a new sketch
sketch = cq.Workplane("XY").sketch()

# Draw a line
sketch.lineTo(1, 0)

# Save the current state
sketch.push()

# Draw another line
sketch.lineTo(1, 1)

# Print the current number of edges
print(len(sketch.val().Edges()))

# Revert to the previous state
sketch.pop()

# Print the number of edges after the revert
print(len(sketch.val().Edges()))

Output:

2
1

In this example, a new sketch is created on the XY plane, and a line is drawn using the lineTo() method. The push() method is then used to save the current state of the sketch. Another line is drawn, and the total number of edges in the current state is printed. The pop() method is then used to revert back to the previous state, and the number of edges is printed again, showing that the sketch has been restored to its previous state.

Notes

  • It's important to note that the push() method should be used judiciously. Creating too many states can consume a lot of memory and slow down the sketch creation process. It's a good idea to use this method sparingly and only when necessary.

  • The push() method only saves the state of the sketch, not any other objects that may have been created using the CADQuery library. To save the entire state of the sketch, one must use a more sophisticated method such as serialization.

  • The push() method can be used in conjunction with other CADQuery functions to create complex sketches. All CADQuery functions are executed in the order they are called, so the order in which they are used is very important.