cadquery
Sketch
Workplane
Assembly

Sketch.each()

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.

Syntax

Sketch.each(func)

Parameters

  • func (function): A function that takes a single argument, an entity object, and performs some operation on it.

Return Value

This method does not return anything.

Example Usage

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)

Notes

  • The 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.
  • The func function will be called once for each entity in the sketch.
  • If you want to perform different operations on different types of entities (e.g. edges versus faces), you can use type checking in your func function to conditionally execute different code for each entity type.

See Also