Wiki
Clone wikiFlatCAM / gcode_line_segementation
GCode Line Segmentation for Z Auto Level
This document describes a new feature to allow user to segment long lines when creating a CNC job.
Some CNC job requires fine tuning of tool head Z level, such as PCB milling. Conventional Z auto level is to use the tool head to probe the work piece in a grid pattern to obtain a map of Z depth of the work piece surface. The map is than used to correct the Z movement of the GCode. However, most CNC software is only able to fix the start and end of existing gcode instruction. This makes Z auto level ineffective with long running lines.
Usage
Two new options are added for generating CNC job from geometry object, x step and y step, to control the maximum step size of each line in x and y direction. Lines exceeding the step size will be segmented to shorter lines. The resulting GCode traces will look exactly the same with or without segmentation, because current FlatCAM has no visual distinguish on line start and end.
Implementation
The algorithm used is pretty simply. To inline with how Z auto level works, the line is segmented by grid whose cell size is defined by x step and y step. A new function segment()
is added to class CNCJob
to segement all lines inside a given path. The function is called inside CNCJob.linear2gcode()
, i.e. when generating GCode for a linear path. For each line in the path, segment()
calls another new function linebreak()
to recursively break a given line until it is within the specified grid cell.
For each recursive iteration, linebreak()
calls linebreak_single()
to check and break the line in single dimension, starting with x, as shown in the picture below.
To avoid generating lines that are too short, it will check if the line in that dimension is longer than twice of the step size. If not, it will break the line in half instead, as shown below,
Then, linebreak_single()
is called again to break the line in y dimension the exact same way,
After that, if any clipping happened, the new point is inserted into the path. The remain line is processed in the next recursive iteration,
Updated