Class Sketch
- class cadscript.Sketch(sketch: Sketch)[source]
Represents a 2D sketch. Sketch instances are typically created using
cadscript.make_sketch()
.- add_rect(size_x: Tuple[float, float] | float, size_y: Tuple[float, float] | float, *, angle: float = 0, center: Literal['X', 'Y', 'XY'] | bool = True, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Adds a rectangle to the sketch object.
- Parameters:
size_x (DimensionDefinitionType) – The size of the rectangle along the x-axis.
size_y (DimensionDefinitionType) – The size of the rectangle along the y-axis.
angle (float, optional) – The angle of rotation. Defaults to 0.
center (CenterDefinition2DType, optional) – Determines whether the rectangle is centered. If False, the rectangle will start from the origin. Can also be “X” or “Y” to center in only one direction. Defaults to True.
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, a rectangle is added for each of the entries, specifying the offset as (x,y) tuple. Defaults to None, which results in a single rectangle added with no offset.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- cut_rect(size_x: Tuple[float, float] | float, size_y: Tuple[float, float] | float, *, angle: float = 0, center: Literal['X', 'Y', 'XY'] | bool = True, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Cuts a rectangle from the sketch object.
- Parameters:
size_x (DimensionDefinitionType) – The size of the rectangle along the x-axis.
size_y (DimensionDefinitionType) – The size of the rectangle along the y-axis.
angle (float, optional) – The angle of rotation. Defaults to 0.
center (CenterDefinition2DType, optional) – Determines whether the rectangle is centered. If False, the rectangle will start from the origin. Can also be “X” or “Y” to center in only one direction. Defaults to True.
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, a rectangle is cut for each of the entries, specifying the offset as (x,y) tuple. Defaults to None, which results in a single rectangle cut with no offset.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- add_circle(*, r: float | None = None, radius: float | None = None, d: float | None = None, diameter: float | None = None, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Adds a circle to the sketch. One of the parameters ‘r’, ‘radius’, ‘d’ or ‘diameter’ must be specified.
- Parameters:
r (float, optional) – The radius of the circle.
radius (float, optional) – The radius of the circle (alternative to ‘r’).
d (float, optional) – The diameter of the circle.
diameter (float, optional) – The diameter of the circle (alternative to ‘d’).
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, a circle is added for each of the entries, specifying the center as (x,y) tuple. If None, a single circle will be added at the origin.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- cut_circle(*, r: float | None = None, radius: float | None = None, d: float | None = None, diameter: float | None = None, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Cuts a circle from the sketch. One of the parameters ‘r’, ‘radius’, ‘d’ or ‘diameter’ must be specified.
- Parameters:
r (float, optional) – The radius of the circle.
radius (float, optional) – The radius of the circle (alternative to ‘r’).
d (float, optional) – The diameter of the circle.
diameter (float, optional) – The diameter of the circle (alternative to ‘d’).
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, a circle is cut for each of the entries, specifying the center as (x,y) tuple. If None, a single circle will be cut at the origin.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- add_ellipse(size_x: Tuple[float, float] | float, size_y: Tuple[float, float] | float, *, angle: float = 0, center: Literal['X', 'Y', 'XY'] | bool = True, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Adds an ellipse to the sketch object.
- Parameters:
size_x (DimensionDefinitionType) – The size of the ellipse along the x-axis.
size_y (DimensionDefinitionType) – The size of the ellipse along the y-axis.
angle (float, optional) – The angle of rotation. Defaults to 0.
center (CenterDefinition2DType, optional) – Determines whether the ellipse is centered. If False, the ellipse will start from the origin. Can also be “X” or “Y” to center in only one direction. Defaults to True.
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, a ellipse is added for each of the entries, specifying the offset as (x,y) tuple. Defaults to None, which results in a single ellipse added with no offset.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- cut_ellipse(size_x: Tuple[float, float] | float, size_y: Tuple[float, float] | float, *, angle: float = 0, center: Literal['X', 'Y', 'XY'] | bool = True, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Cuts an ellipse from the sketch object.
- Parameters:
size_x (DimensionDefinitionType) – The size of the ellipse along the x-axis.
size_y (DimensionDefinitionType) – The size of the ellipse along the y-axis.
angle (float, optional) – The angle of rotation. Defaults to 0.
center (CenterDefinition2DType, optional) – Determines whether the ellipse is centered. If False, the ellipse will start from the origin. Can also be “X” or “Y” to center in only one direction. Defaults to True.
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, a ellipse is cuts for each of the entries, specifying the offset as (x,y) tuple. Defaults to None, which results in a single ellipse cut with no offset.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- add_polygon(point_list: Iterable[Tuple[float, float]], *, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, auto_close: bool = True) Sketch [source]
Adds a polygon to the sketch.
- Parameters:
point_list (Iterable[Vector2DType]) – A list of points defining the polygon. The points must be given in counter-clockwise order.
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, a polygon is added for each of the entries, specifying the offset as (x,y) tuple. Defaults to None, which results in a single polygon added with no offset.
pos – Shorthand for positions parameter, only use one of them.
auto_close (bool, optional) – If True, the polygon will be automatically closed by adding a line from the last point to the first. If False, in the given point list, the last point is expected to be the same as the first point. Defaults to True.
- Returns:
The modified sketch object.
- Return type:
- cut_polygon(point_list: Iterable[Tuple[float, float]], *, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Cuts a polygon from the sketch.
- Parameters:
point_list (Iterable[Vector2DType]) – A list of points defining the polygon. The points must be given in counter-clockwise order.
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, a polygon is added for each of the entries, specifying the offset as (x,y) tuple. Defaults to None, which results in a single polygon added with no offset.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The modified sketch object.
- Return type:
- add_slot(*, width: float | None = None, height: float | None = None, angle: float = 0, start: Tuple[float, float] | None = None, end: Tuple[float, float] | None = None, r: float | None = None, radius: float | None = None, d: float | None = None, diameter: float | None = None, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Adds a slot to the sketch. The slot is either defined by a width, height and an angle of rotation, or by two points and a radius. One of the two methods must be used.
When using the width and height method, the slot is parallel to the x-axis. The width is the non-rounded part along the x-axis and the height is the extent along the y-axis. The part is centered at the origin unless the positions parameter is specified. If the angle parameter is specified, the slot is rotated around its center by the given angle.
When using the two points method, the slot is defined by two points and a radius. Use the start, end and one of the r, radius, d or diameter parameters to specify the slot.
- Parameters:
width (float, optional) – The width of the slot, the part without the rounded sides.
heigth (float, optional) – The height of the slot.
angle (float, optional) – The angle of rotation for the slot. Defaults to 0, which means the slot is parallel to the x-axis.
start (Vector2DType) – The start point of the slot.
end (Vector2DType) – The end point of the slot.
r (float, optional) – The radius of the slot (alternative to ‘radius’, ‘d’ or ‘diameter’).
radius (float, optional) – The radius of the slot (alternative to ‘r’, ‘d’ or ‘diameter’).
d (float, optional) – The diameter of the slot (alternative to ‘r’, ‘radius’ or ‘diameter’).
diameter (float, optional) – The diameter of the slot (alternative to ‘r’, ‘radius’ or ‘d’).
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, a slot is added for each of the entries, specifying the its respective center as (x,y) tuple. Defaults to None, which results in a single slot added at the origin.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- cut_slot(*, width: float | None = None, height: float | None = None, angle: float = 0, start: Tuple[float, float] | None = None, end: Tuple[float, float] | None = None, r: float | None = None, radius: float | None = None, d: float | None = None, diameter: float | None = None, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Cuts a slot from the sketch. The slot is either defined by a width, height and an angle of rotation, or by two points and a radius. One of the two methods must be used.
When using the width and height method, the slot is parallel to the x-axis. The width is the non-rounded part along the x-axis and the height is the extent along the y-axis. The part is centered at the origin unless the positions parameter is specified. If the angle parameter is specified, the slot is rotated around its center by the given angle.
When using the two points method, the slot is defined by two points and a radius. Use the start, end and one of the r, radius, d or diameter parameters to specify the slot.
- Parameters:
width (float, optional) – The width of the slot, the part without the rounded sides.
heigth (float, optional) – The height of the slot.
angle (float, optional) – The angle of rotation for the slot. Defaults to 0, which means the slot is parallel to the x-axis.
start (Vector2DType) – The start point of the slot.
end (Vector2DType) – The end point of the slot.
r (float, optional) – The radius of the slot (alternative to ‘radius’, ‘d’ or ‘diameter’).
radius (float, optional) – The radius of the slot (alternative to ‘r’, ‘d’ or ‘diameter’).
d (float, optional) – The diameter of the slot (alternative to ‘r’, ‘radius’ or ‘diameter’).
diameter (float, optional) – The diameter of the slot (alternative to ‘r’, ‘radius’ or ‘d’).
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, a slot is cut for each of the entries, specifying the its respective center as (x,y) tuple. Defaults to None, which results in a single slot cut at the origin.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- add_sketch(sketch: Sketch, *, angle: float = 0, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Adds another sketch to the sketch object.
- Parameters:
sketch (Sketch) – The sketch to add.
angle (float, optional) – The angle of rotation. Defaults to 0.
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, the sketch is added for each of the entries, specifying the position as (x,y) tuple. Defaults to None, which results in the sketch added once at the origin.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- cut_sketch(sketch: Sketch, *, angle: float = 0, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Cuts another sketch away from the sketch object.
- Parameters:
sketch (Sketch) – The sketch to cut.
angle (float, optional) – The angle of rotation. Defaults to 0.
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, the sketch is cut for each of the entries, specifying the position as (x,y) tuple. Defaults to None, which results in the sketch cut once at the origin.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- intersect_sketch(sketch: Sketch, *, angle: float = 0, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None) Sketch [source]
Intersects another sketch away from the sketch object.
- Parameters:
sketch (Sketch) – The sketch to intersect with.
angle (float, optional) – The angle of rotation. Defaults to 0.
positions (Vector2DType | Iterable[Vector2DType], optional) – If given, the sketch is cut for each of the entries, specifying the position as (x,y) tuple. Defaults to None, which results in the sketch intersected once at the origin.
pos – Shorthand for positions parameter, only use one of them.
- Returns:
The updated sketch object.
- Return type:
- add_import_dxf(dxf_filename: str, *, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, tolerance: float = 0.001) Sketch [source]
Imports a DXF file and adds it to the sketch object. The DXF mus contain one or more closed loops from lines and curves.
- Parameters:
dxf_filename (str) – The filename of the DXF file to import.
positions (Vector2DType | Iterable[Vector2DType], optional) – Optional positions to place the imported objects. If not provided, the objects will be placed at the origin.
pos – Shorthand for positions parameter, only use one of them.
tolerance (float) – The tolerance to use for the import. Defaults to 1e-3.
- Returns:
The updated sketch object.
- Return type:
- cut_import_dxf(dxf_filename: str, *, positions: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, pos: Tuple[float, float] | Iterable[Tuple[float, float]] | None = None, tolerance: float = 0.001) Sketch [source]
Imports a DXF file and cuts it from the sketch object. The DXF mus contain one or more closed loops from lines and curves.
- Parameters:
dxf_filename (str) – The filename of the DXF file to import.
positions (Vector2DType | Iterable[Vector2DType], optional) – Optional positions to place the imported objects. If not provided, the objects will be placed at the origin.
pos – Shorthand for positions parameter, only use one of them.
tolerance (float) – The tolerance to use for the import. Defaults to 1e-3.
- Returns:
The updated sketch object.
- Return type:
- fillet(query: str | Tuple[float, float] | Iterable[Tuple[float, float]], radius: float) Sketch [source]
Fillets corners of the sketch.
- Parameters:
query (str | Vector2DType | Iterable[Vector2DType]) – The vertices to fillet. The query syntax is documented at Selecting Vertices. You can also pass a point or a list of points to fillet the nearest vertices.
radius (float) – The fillet radius.
- Returns:
The modified sketch object.
- Return type:
- chamfer(query: str | Tuple[float, float] | Iterable[Tuple[float, float]], amount: float) Sketch [source]
Chamfers corners of the sketch.
- Parameters:
query (str | Vector2DType | Iterable[Vector2DType]) – The vertices to chamfer. The query syntax is documented at Selecting Vertices. You can also pass a point or a list of points to chamfer the nearest vertices.
amount (float) – The chamfer amount.
- Returns:
The modified sketch object.
- Return type:
- export_dxf(filepath: str) None [source]
Export the sketch to a DXF file.
- Parameters:
filename (str) – The name of the DXF file to export to. If passing a path, the parent directory must exist.
- move(translationVector: Tuple[float, float]) Sketch [source]
Moves the Sketch by the specified translation vector.
- Parameters:
translationVector (Vector2DType) – The translation vector specifying the amount and direction of the movement.
- Returns:
The updated Sketch after the movement.
- Return type:
- move_to_origin(axis: Literal['X', 'Y', 'XY'] | bool = True) Sketch [source]
Moves the sketch to the origin, i.e. that the lower corner of the bounding box is at the origin.
- Parameters:
axis (CenterDefinition2DType, optional) – Can be “X” or “Y” to move the object in only one direction or True which moves the sketch in both directions. If False, the sketch will be not moved at all. Defaults to True.
- center(center: Literal['X', 'Y', 'XY'] | bool = True) Sketch [source]
Centers the sketch at the origin.
- Parameters:
center (CenterDefinition2DType, optional) – Can be “X” or “Y” to move the object in only one direction or True which moves the sketch in both directions. If False, the sketch will be not moved at all. Defaults to True.
- mirror(axis: Literal['X', 'Y'], copy_and_merge: bool = True) Sketch [source]
Mirrors the sketch object.
- Parameters:
axis (Axis2DType) – The axis to mirror the sketch object along.
copy_and_merge (bool, optional) – If True, the sketch is mirrored and merged with the original sketch. If False, the original sketch is replaced by the mirrored sketch. Defaults to True.
- Returns:
The mirrored sketch object.
- Return type:
- cq() Sketch [source]
Returns the underlying CadQuery sketch object. Useful when mixing CadQuery and Cadscript code.