The Sketch.each()
method is used to iterate through all entities in a given sketch, including edges, faces, and vertices. This method is defined within the cadquery.Sketch
class and takes a single argument, a function that will be called for each entity in the sketch.
Sketch.each(func)
func
(function): A function that takes a single argument, an entity object, and performs some operation on it.This method does not return anything.
Here's an example of using Sketch.each()
to iterate through all edges in a sketch and print out their lengths:
import cadquery as cq
# Define a sketch with some geometry
sketch = cq.Workplane().rect(2, 2).extrude(1)
# Define a function to compute the length of an edge
def print_edge_length(edge):
length = edge.Length()
print(f"Edge length: {length}")
# Iterate through all edges in the sketch and print their lengths
sketch.each(print_edge_length)
func
function that is passed to Sketch.each()
must accept a single argument, which will be an entity object representing an edge, face, or vertex in the sketch.func
function will be called once for each entity in the sketch.func
function to conditionally execute different code for each entity type.