cam.cam_chunk

Contents

cam.cam_chunk#

Fabex ‘cam_chunk.py’ © 2012 Vilem Novak

Classes and Functions to build, store and optimize CAM path chunks.

Classes#

Functions#

chunks_coherency(chunks)

limit_chunks(chunks, o[, force])

mesh_from_curve_to_chunk(object)

curve_to_chunks(o[, use_modifiers])

shapely_to_chunks(p, zlevel)

curve_to_shapely(cob[, use_modifiers])

Convert a curve object to Shapely polygons.

sample_chunks_n_axis(o, pathSamples, layers)

Sample chunks along a specified axis based on provided paths and layers.

sample_path_low(o, ch1, ch2, dosample)

Generate a sample path between two channels.

polygon_boolean(context, boolean_type)

Perform a boolean operation on selected polygons.

polygon_convex_hull(context)

Generate the convex hull of a polygon from the given context.

silhouette_offset(context, offset[, style, mitrelimit])

Offset the silhouette of a curve or font object in Blender.

get_object_silhouette(stype[, objects, use_modifiers])

Get the silhouette of objects based on the specified type.

get_operation_silhouette(operation)

Gets the silhouette for the given operation.

get_object_outline(radius, o, Offset)

Get the outline of a geometric object based on specified parameters.

get_ambient(o)

Calculate and update the ambient geometry based on the provided object.

sample_chunks(o, pathSamples, layers)

Sample chunks of paths based on the provided parameters.

connect_chunks_low(chunks, o)

Connects chunks that are close to each other without lifting, sampling

sort_chunks(chunks, o[, last_pos])

Sort a list of chunks based on a specified strategy.

oclResampleChunks(operation, chunks_to_resample, ...)

Resample chunks of data using OpenCL operations.

oclGetWaterline(operation, chunks)

Generate waterline paths for a given machining operation.

image_edge_search_on_line(o, ar, zimage)

Search for edges in an image using a pencil strategy.

get_offset_image_cavities(o, i)

Detects areas in the offset image which are 'cavities' due to curvature

crazy_stroke_image(o)

Generate a toolpath for a milling operation using a crazy stroke

crazy_stroke_image_binary(o, ar, avoidar)

Perform a milling operation using a binary image representation.

image_to_chunks(o, image[, with_border])

Convert an image into chunks based on detected edges.

image_to_shapely(o, i[, with_border])

Convert an image to Shapely polygons.

Module Contents#

class CamPathChunkBuilder(inpoints=None, startpoints=None, endpoints=None, rotations=None)[source]#
points = None[source]#
startpoints = [][source]#
endpoints = [][source]#
rotations = [][source]#
depth = None[source]#
to_chunk()[source]#
class CamPathChunk(inpoints, startpoints=None, endpoints=None, rotations=None)[source]#
poly = None[source]#
simppoly = None[source]#
closed = False[source]#
children = [][source]#
parents = [][source]#
sorted = False[source]#
length = 0[source]#
zstart = 0[source]#
zend = 0[source]#
update_poly()[source]#
get_point(n)[source]#
get_points()[source]#
get_points_np()[source]#
set_points(points)[source]#
count()[source]#
copy()[source]#
shift(x, y, z)[source]#
set_z(z, if_bigger=False)[source]#
offset_z(z)[source]#
flip_x(x_centre)[source]#
is_below_z(z)[source]#
clamp_z(z)[source]#
clamp_max_z(z)[source]#
distance(pos, o)[source]#
distance_start(pos, o)[source]#
x_y_distance_within(other, cutoff)[source]#
x_y_distance_to(other, cutoff=0)[source]#
adapt_distance(pos, o)[source]#
get_next_closest(o, pos)[source]#
get_length()[source]#
reverse()[source]#
pop(index)[source]#
dedupe_points()[source]#
insert(at_index, point, startpoint=None, endpoint=None, rotation=None)[source]#
append(point, startpoint=None, endpoint=None, rotation=None, at_index=None)[source]#
extend(points, startpoints=None, endpoints=None, rotations=None, at_index=None)[source]#
clip_points(minx, maxx, miny, maxy)[source]#

Remove Any Points Outside This Range

ramp_contour(zstart, zend, o)[source]#
ramp_zig_zag(zstart, zend, o)[source]#
change_path_start(o)[source]#
break_path_for_leadin_leadout(o)[source]#
lead_contour(o)[source]#
chunks_coherency(chunks)[source]#
limit_chunks(chunks, o, force=False)[source]#
mesh_from_curve_to_chunk(object)[source]#
curve_to_chunks(o, use_modifiers=False)[source]#
shapely_to_chunks(p, zlevel)[source]#
curve_to_shapely(cob, use_modifiers=False)[source]#

Convert a curve object to Shapely polygons.

This function takes a curve object and converts it into a list of Shapely polygons. It first breaks the curve into chunks and then transforms those chunks into Shapely-compatible polygon representations. The use_modifiers parameter allows for additional processing of the curve before conversion, depending on the specific requirements of the application.

Parameters:
  • cob – The curve object to be converted.

  • use_modifiers (bool) – A flag indicating whether to apply modifiers during the conversion process. Defaults to False.

Returns:

A list of Shapely polygons created from the curve object.

Return type:

list

async sample_chunks_n_axis(o, pathSamples, layers)[source]#

Sample chunks along a specified axis based on provided paths and layers.

This function processes a set of path samples and organizes them into chunks according to specified layers. It prepares the collision world if necessary, updates the cutter’s rotation based on the path samples, and handles the sampling of points along the paths. The function also manages the relationships between the sampled points and their respective layers, ensuring that the correct points are added to each chunk. The resulting chunks can be used for further processing in a 3D environment.

Parameters:
  • o (object) – An object containing properties such as min/max coordinates, cutter shape, and other relevant parameters.

  • pathSamples (list) – A list of path samples, each containing start points, end points, and rotations.

  • layers (list) – A list of layer definitions that specify the boundaries for sampling.

Returns:

A list of sampled chunks organized by layers.

Return type:

list

sample_path_low(o, ch1, ch2, dosample)[source]#

Generate a sample path between two channels.

This function computes a series of points that form a path between two given channels. It calculates the direction vector from the end of the first channel to the start of the second channel and generates points along this vector up to a specified distance. If sampling is enabled, it modifies the z-coordinate of the generated points based on the cutter shape or image sampling, ensuring that the path accounts for any obstacles or features in the environment.

Parameters:
  • o – An object containing optimization parameters and properties related to the path generation.

  • ch1 – The first channel object, which provides a point for the starting location of the path.

  • ch2 – The second channel object, which provides a point for the ending location of the path.

  • dosample (bool) – A flag indicating whether to perform sampling along the generated path.

Returns:

An object representing the generated path points.

Return type:

CamPathChunk

polygon_boolean(context, boolean_type)[source]#

Perform a boolean operation on selected polygons.

This function takes the active object and applies a specified boolean operation (UNION, DIFFERENCE, or INTERSECT) with respect to other selected objects in the Blender context. It first converts the polygons of the active object and the selected objects into a Shapely MultiPolygon. Depending on the boolean type specified, it performs the corresponding boolean operation and then converts the result back into a Blender curve.

Parameters:
  • context (bpy.context) – The Blender context containing scene and object data.

  • boolean_type (str) – The type of boolean operation to perform. Must be one of ‘UNION’, ‘DIFFERENCE’, or ‘INTERSECT’.

Returns:

A dictionary indicating the operation result, typically {‘FINISHED’}.

Return type:

dict

polygon_convex_hull(context)[source]#

Generate the convex hull of a polygon from the given context.

This function duplicates the current object, joins it, and converts it into a 3D mesh. It then extracts the X and Y coordinates of the vertices to create a MultiPoint data structure using Shapely. Finally, it computes the convex hull of these points and converts the result back into a curve named ‘ConvexHull’. Temporary objects created during this process are deleted to maintain a clean workspace.

Parameters:

context – The context in which the operation is performed, typically related to Blender’s current state.

Returns:

A dictionary indicating the operation’s completion status.

Return type:

dict

silhouette_offset(context, offset, style=1, mitrelimit=1.0)[source]#

Offset the silhouette of a curve or font object in Blender.

This function takes an active curve or font object in Blender and creates an offset silhouette based on the specified parameters. It first retrieves the silhouette of the object and then applies a buffer operation to create the offset shape. The resulting shape is then converted back into a curve object in the Blender scene.

Parameters:
  • context (bpy.context) – The current Blender context.

  • offset (float) – The distance to offset the silhouette.

  • style (int?) – The join style for the offset. Defaults to 1.

  • mitrelimit (float?) – The mitre limit for the offset. Defaults to 1.0.

Returns:

A dictionary indicating the operation is finished.

Return type:

dict

get_object_silhouette(stype, objects=None, use_modifiers=False)[source]#

Get the silhouette of objects based on the specified type.

This function computes the silhouette of a given set of objects in Blender based on the specified type. It can handle both curves and mesh objects, converting curves to polygon format and calculating the silhouette for mesh objects. The function also considers the use of modifiers if specified. The silhouette is generated by processing the geometry of the objects and returning a Shapely representation of the silhouette.

Parameters:
  • stype (str) – The type of silhouette to generate (‘CURVES’ or ‘OBJECTS’).

  • objects (list?) – A list of Blender objects to process. Defaults to None.

  • use_modifiers (bool?) – Whether to apply modifiers to the objects. Defaults to False.

Returns:

The computed silhouette as a Shapely MultiPolygon.

Return type:

shapely.geometry.MultiPolygon

get_operation_silhouette(operation)[source]#

Gets the silhouette for the given operation.

This function determines the silhouette of an operation using image thresholding techniques. It handles different geometry sources, such as objects or images, and applies specific methods based on the type of geometry. If the geometry source is ‘OBJECT’ or ‘COLLECTION’, it checks whether to process curves or not. The function also considers the number of faces in mesh objects to decide on the appropriate method for silhouette extraction.

Parameters:

operation (Operation) – An object containing the necessary data

Returns:

The computed silhouette for the operation.

Return type:

Silhouette

get_object_outline(radius, o, Offset)[source]#

Get the outline of a geometric object based on specified parameters.

This function generates an outline for a given geometric object by applying a buffer operation to its polygons. The buffer radius can be adjusted based on the radius parameter, and the operation can be offset based on the Offset flag. The function also considers whether the polygons should be merged or not, depending on the properties of the object o.

Parameters:
  • radius (float) – The radius for the buffer operation.

  • o (object) – An object containing properties that influence the outline generation.

  • Offset (bool) – A flag indicating whether to apply a positive or negative offset.

Returns:

The resulting outline of the geometric object as a MultiPolygon.

Return type:

MultiPolygon

get_ambient(o)[source]#

Calculate and update the ambient geometry based on the provided object.

This function computes the ambient shape for a given object based on its properties, such as cutter restrictions and ambient behavior. It determines the appropriate radius and creates the ambient geometry either from the silhouette or as a polygon defined by the object’s minimum and maximum coordinates. If a limit curve is specified, it will also intersect the ambient shape with the limit polygon.

Parameters:

o (object) – An object containing properties that define the ambient behavior, cutter restrictions, and limit curve.

Returns:

The function updates the ambient property of the object in place.

Return type:

None

async sample_chunks(o, pathSamples, layers)[source]#

Sample chunks of paths based on the provided parameters.

This function processes the given path samples and layers to generate chunks of points that represent the sampled paths. It takes into account various optimization settings and strategies to determine how the points are sampled and organized into layers. The function handles different scenarios based on the object’s properties and the specified layers, ensuring that the resulting chunks are correctly structured for further processing.

Parameters:
  • o (object) – An object containing various properties and settings related to the sampling process.

  • pathSamples (list) – A list of path samples to be processed.

  • layers (list) – A list of layers defining the z-coordinate ranges for sampling.

Returns:

A list of sampled chunks, each containing points that represent

the sampled paths.

Return type:

list

async connect_chunks_low(chunks, o)[source]#

Connects chunks that are close to each other without lifting, sampling them ‘low’.

This function processes a list of chunks and connects those that are within a specified distance based on the provided options. It takes into account various strategies for connecting the chunks, including ‘CARVE’, ‘PENCIL’, and ‘MEDIAL_AXIS’, and adjusts the merging distance accordingly. The function also handles specific movement settings, such as whether to stay low or to merge distances, and may resample chunks if certain optimization conditions are met.

Parameters:
  • chunks (list) – A list of chunk objects to be connected.

  • o (object) – An options object containing movement and strategy parameters.

Returns:

A list of connected chunk objects.

Return type:

list

async sort_chunks(chunks, o, last_pos=None)[source]#

Sort a list of chunks based on a specified strategy.

This function sorts a list of chunks according to the provided options and the current position. It utilizes a recursive approach to find the closest chunk to the current position and adapts its distance if it has not been sorted before. The function also handles progress updates asynchronously and adjusts the recursion limit to accommodate deep recursion scenarios.

Parameters:
  • chunks (list) – A list of chunk objects to be sorted.

  • o (object) – An options object that contains sorting strategy and other parameters.

  • last_pos (tuple?) – The last known position as a tuple of coordinates. Defaults to None, which initializes the position to (0, 0, 0).

Returns:

A sorted list of chunk objects.

Return type:

list

async oclResampleChunks(operation, chunks_to_resample, use_cached_mesh)[source]#

Resample chunks of data using OpenCL operations.

This function takes a list of chunks to resample and performs an OpenCL sampling operation on them. It first prepares a temporary chunk that collects points from the specified chunks. Then, it calls the ocl_sample function to perform the sampling operation. After obtaining the samples, it updates the z-coordinates of the points in each chunk based on the sampled values.

Parameters:
  • operation (OperationType) – The OpenCL operation to be performed.

  • chunks_to_resample (list) – A list of tuples, where each tuple contains a chunk object and its corresponding start index and length for resampling.

  • use_cached_mesh (bool) – A flag indicating whether to use cached mesh data during the sampling process.

Returns:

This function does not return a value but modifies the input

chunks in place.

Return type:

None

async oclGetWaterline(operation, chunks)[source]#

Generate waterline paths for a given machining operation.

This function calculates the waterline paths based on the provided machining operation and its parameters. It determines the appropriate cutter type and dimensions, sets up the waterline object with the corresponding STL file, and processes each layer to generate the machining paths. The resulting paths are stored in the provided chunks list. The function also handles different cutter types, including end mills, ball nose cutters, and V-carve cutters.

Parameters:
  • operation (Operation) – An object representing the machining operation, containing details such as cutter type, diameter, and minimum Z height.

  • chunks (list) – A list that will be populated with the generated machining path chunks.

image_edge_search_on_line(o, ar, zimage)[source]#

Search for edges in an image using a pencil strategy.

This function implements an edge detection algorithm that simulates a pencil-like movement across the image represented by a 2D array. It identifies white pixels and builds chunks of points based on the detected edges. The algorithm iteratively explores possible directions to find and track the edges until a specified condition is met, such as exhausting the available white pixels or reaching a maximum number of tests.

Parameters:
  • o (object) – An object containing parameters such as min, max coordinates, cutter diameter, border width, and optimisation settings.

  • ar (np.ndarray) – A 2D array representing the image where edge detection is to be performed.

  • zimage (np.ndarray) – A 2D array representing the z-coordinates corresponding to the image.

Returns:

A list of chunks representing the detected edges in the image.

Return type:

list

get_offset_image_cavities(o, i)[source]#

Detects areas in the offset image which are ‘cavities’ due to curvature changes.

This function analyzes the input image to identify regions where the curvature changes, indicating the presence of cavities. It computes vertical and horizontal differences in pixel values to detect edges and applies a threshold to filter out insignificant changes. The resulting areas are then processed to remove any chunks that do not meet the minimum criteria for cavity detection. The function returns a list of valid chunks that represent the detected cavities.

Parameters:
  • o – An object containing parameters and thresholds for the detection process.

  • i (np.ndarray) – A 2D array representing the image data to be analyzed.

Returns:

A list of detected chunks representing the cavities in the image.

Return type:

list

crazy_stroke_image(o)[source]#

Generate a toolpath for a milling operation using a crazy stroke strategy.

This function computes a path for a milling cutter based on the provided parameters and the offset image. It utilizes a circular cutter representation and evaluates potential cutting positions based on various thresholds. The algorithm iteratively tests different angles and lengths for the cutter’s movement until the desired cutting area is achieved or the maximum number of tests is reached.

Parameters:

o (object) – An object containing parameters such as cutter diameter, optimization settings, movement type, and thresholds for determining cutting effectiveness.

Returns:

A list of chunks representing the computed toolpath for the milling

operation.

Return type:

list

crazy_stroke_image_binary(o, ar, avoidar)[source]#

Perform a milling operation using a binary image representation.

This function implements a strategy for milling by navigating through a binary image. It starts from a defined point and attempts to move in various directions, evaluating the cutter load to determine the appropriate path. The algorithm continues until it either exhausts the available pixels to cut or reaches a predefined limit on the number of tests. The function modifies the input array to represent the areas that have been milled and returns the generated path as a list of chunks.

Parameters:
  • o (object) – An object containing parameters for the milling operation, including cutter diameter, thresholds, and movement type.

  • ar (np.ndarray) – A 2D binary array representing the image to be milled.

  • avoidar (np.ndarray) – A 2D binary array indicating areas to avoid during milling.

Returns:

A list of chunks representing the path taken during the milling

operation.

Return type:

list

image_to_chunks(o, image, with_border=False)[source]#

Convert an image into chunks based on detected edges.

This function processes a given image to identify edges and convert them into polychunks, which are essentially collections of connected edge segments. It utilizes the properties of the input object o to determine the boundaries and size of the chunks. The function can optionally include borders in the edge detection process. The output is a list of chunks that represent the detected polygons in the image.

Parameters:
  • o (object) – An object containing properties such as min, max, borderwidth, and optimisation settings.

  • image (np.ndarray) – A 2D array representing the image to be processed, expected to be in a format compatible with uint8.

  • with_border (bool?) – A flag indicating whether to include borders in the edge detection. Defaults to False.

Returns:

A list of chunks, where each chunk is represented as a collection of

points that outline the detected edges in the image.

Return type:

list

image_to_shapely(o, i, with_border=False)[source]#

Convert an image to Shapely polygons.

This function takes an image and converts it into a series of Shapely polygon objects. It first processes the image into chunks and then transforms those chunks into polygon geometries. The with_border parameter allows for the inclusion of borders in the resulting polygons.

Parameters:
  • o – The input image to be processed.

  • i – Additional input parameters for processing the image.

  • with_border (bool) – A flag indicating whether to include borders in the resulting polygons. Defaults to False.

Returns:

A list of Shapely polygon objects created from the

image chunks.

Return type:

list