Mach Modules Documentation
Lua Module API Documentation
Loading...
Searching...
No Matches
ShapeMaker.lua File Reference

Go to the source code of this file.

Functions

GetDrillCode ()
 Return the operation code for a drill operation.
GetBoreCode ()
 Return the operation code for a bore operation.
GetPocketCode ()
 Return the operation code for a pocket milling operation.
GetHingeRectCode ()
 Return the operation code for a hinge rectangle operation.
GetStrikeRectCode ()
 Return the operation code for a strike rectangle operation.
GetSquaringCode ()
 Return the operation code for a squaring corners operation.
CurrentSpindleSpeed ()
 Check if the spindle speed has changed and update the current speed tracker.
CurrentTool ()
 Check if the active tool has changed and update the current tool tracker.
ToolCall (tool_info, feed, speed, face)
 Insert tool change, tool offset, feedrate, and spindle speed G-code into a gcode table.
StopSpindle ()
 Conditionally insert a spindle stop command when the next operation uses a different tool.
Bevel (y_cmd, d, z_depth, inv)
 Calculate a beveled Z depth at a given Y position along a cut path.
GetPlane ()
 Return axis label strings and working plane code for a given machine face.
BuildShape ()
 Generate a complete G-code sequence for all operations in a shape.
OrderOps ()
 Order a list of machining operations by weight and tool number.
MakeHingeRect ()
 Generate G-code for milling a hinge recess rectangle.
MakeStrikeRect ()
 Generate G-code for milling a strike plate rectangle.
SquareCorners ()
 Generate G-code to square the corners of a previously milled rectangle.
MakePocket ()
 Generate G-code for a pocket milling operation with semicircular ends.
MakeDrill ()
 Generate G-code for a drill operation at one or more positions.
MakeBore ()
 Generate G-code for a helical bore operation at a specified position.

Function Documentation

◆ Bevel()

S Bevel ( y_cmd ,
d ,
z_depth ,
inv  )

Calculate a beveled Z depth at a given Y position along a cut path.

Applies a bevel angle to the nominal plunge depth based on how far the current Y position (y_cmd) has moved from the bevel reference position (start_y). The bevel offset is computed as the sine of the bevel angle times the Y distance traveled. If d is nil or zero, the original z_depth is returned unchanged. The inv parameter controls which direction the bevel slopes.

Parameters
start_y(number) The Y reference position from which bevel is measured
y_cmd(number) The current Y position for which to compute the beveled Z
d(number|nil) Bevel angle in degrees, or 0/nil for no bevel
z_depth(number) The nominal (unbeveled) Z plunge depth
inv(number|nil) Bevel direction multiplier: 1 or -1. Defaults to 1 if nil
Returns
(number) The adjusted Z depth with bevel applied
Note
Documentation generated by AI on 2026-03-03

◆ BuildShape()

S BuildShape ( )

Generate a complete G-code sequence for all operations in a shape.

Resets the current tool tracker, orders all operations by weight and tool number via S:OrderOps(), then dispatches each operation to its corresponding generator function based on the operation's type field. The G-code lines from all operations are concatenated into a single flat array and returned. Supported operation types are DRILL, BORE, POCKET, H_RECT, S_RECT, and SQUARING. The current and next operation are tracked in S.current_op and S.next_op to support spindle stop decisions between operations.

Parameters
shape(table) Array of operation parameter tables, each containing at minimum a 'type' field set to the appropriate operation code
Returns
(table) Flat array of G-code strings for the entire shape in execution order
Note
Documentation generated by AI on 2026-03-03

◆ CurrentSpindleSpeed()

S CurrentSpindleSpeed ( )

Check if the spindle speed has changed and update the current speed tracker.

Compares the requested spindle speed against S.CURRENT_SPINDLE. If different, updates S.CURRENT_SPINDLE to the new value and returns true to indicate the speed command should be output. Returns false if the speed is unchanged.

Parameters
spindle_speed(number) The spindle speed to compare against the current speed
Returns
(boolean) True if the spindle speed has changed, false if it is the same
Note
Documentation generated by AI on 2026-03-03

◆ CurrentTool()

S CurrentTool ( )

Check if the active tool has changed and update the current tool tracker.

Compares the requested tool number against S.CURRENT_TOOL. If different, updates S.CURRENT_TOOL to the new value and returns true to indicate a tool change is required. Returns false if the tool is unchanged.

Parameters
tool(number) The tool number to compare against the current tool
Returns
(boolean) True if the tool has changed, false if it is the same as the current tool
Note
Documentation generated by AI on 2026-03-03

Referenced by SetWheelData(), and WriteToolData().

◆ GetBoreCode()

S GetBoreCode ( )

Return the operation code for a bore operation.

Returns
(number) The bore operation code (S.BORE)
Note
Documentation generated by AI on 2026-03-03

◆ GetDrillCode()

S GetDrillCode ( )

Return the operation code for a drill operation.

Returns
(number) The drill operation code (S.DRILL)
Note
Documentation generated by AI on 2026-03-03

◆ GetHingeRectCode()

S GetHingeRectCode ( )

Return the operation code for a hinge rectangle operation.

Returns
(number) The hinge rectangle operation code (S.H_RECT)
Note
Documentation generated by AI on 2026-03-03

◆ GetPlane()

S GetPlane ( )

Return axis label strings and working plane code for a given machine face.

Maps the named cutting face to the appropriate G-code axis labels and arc plane selection code. This allows operation generators to write axis-agnostic G-code that works correctly regardless of which face of the part is being machined. The returned labels should be used in string.format() calls to build move commands. Mapping:

  • Front / Back: X=X, Y=Y, Z=Z, plane=G17, I=I, J=J
  • Top / Bottom: X=X, Y=Z, Z=Y, plane=G18, I=I, J=K
  • Left / Right: X=Y, Y=Z, Z=X, plane=G19, I=J, J=K
    Parameters
    side(string) The face to cut on - 'Front', 'Back', 'Top', 'Bottom', 'Left', or 'Right'
    Returns
    (string) X-axis label to use in G-code strings
    (string) Y-axis label to use in G-code strings
    (string) Z-axis label (depth axis) to use in G-code strings
    (string) Plane selection G-code ('G17', 'G18', or 'G19')
    (string) Arc center I-component label
    (string) Arc center J-component label
    Note
    Documentation generated by AI on 2026-03-03

◆ GetPocketCode()

S GetPocketCode ( )

Return the operation code for a pocket milling operation.

Returns
(number) The pocket operation code (S.POCKET)
Note
Documentation generated by AI on 2026-03-03

◆ GetSquaringCode()

S GetSquaringCode ( )

Return the operation code for a squaring corners operation.

Returns
(number) The squaring operation code (S.SQUARING)
Note
Documentation generated by AI on 2026-03-03

◆ GetStrikeRectCode()

S GetStrikeRectCode ( )

Return the operation code for a strike rectangle operation.

Returns
(number) The strike rectangle operation code (S.S_RECT)
Note
Documentation generated by AI on 2026-03-03

◆ MakeBore()

S MakeBore ( )

Generate G-code for a helical bore operation at a specified position.

Creates a helical boring cycle that incrementally plunges the tool in a circular path to reach the target depth. The tool spirals inward using G02 arc moves, stepping down by step_down increments each revolution until the full bore depth is reached. An additional EXTRA_BORE amount is added to the end depth to ensure full material removal. The working plane and movement directions are automatically determined from the side parameter.

Parameters
info(table) Operation parameter table with the following keys:
  • x (number): X coordinate of the bore center
  • y (number): Y coordinate of the bore center
  • end_depth (number): Total depth of the bore
  • step_down (number): Depth increment per helical revolution
  • bore_dia (number): Diameter of the bore to cut
  • side (string): Face to cut on - 'Front', 'Back', 'Left', 'Right', 'Top', or 'Bottom'
  • tool (table): Tool data with keys: dia (number), tool_number (number), dir (number)
  • feedrate (number): Cutting feedrate
  • spindle_speed (number): Spindle speed in RPM
  • type (number): Operation type code from S:GetBoreCode()
Returns
(table) Array of G-code strings representing the complete bore operation
Note
Documentation generated by AI on 2026-03-03

◆ MakeDrill()

S MakeDrill ( )

Generate G-code for a drill operation at one or more positions.

Produces G-code for drilling multiple hole positions using one of four supported drill cycle types, selected by the info.drill_type field:

  • 99: Custom drill cycle using individual G00/G01 moves (no canned cycle)
  • 73: High-speed peck drilling using G73 canned cycle
  • 81: Standard drilling using G81 canned cycle
  • 83: Full-retract peck drilling using G83 canned cycle The working plane and movement directions are determined automatically from the side parameter. Movement direction is inverted for Left-side operations.
    Parameters
    info(table) Operation parameter table with the following keys:
  • pos (table): Array of position tables, each with keys x (number) and y (number)
  • end_depth (number): Final drill depth
  • peck (number): Peck increment for drill types 73 and 83
  • retract (number): Retract distance for canned cycles
  • side (string): Face to cut on - 'Front', 'Back', 'Left', 'Right', 'Top', or 'Bottom'
  • drill_type (number): Drill cycle type - 99 (custom), 73 (high-speed peck), 81 (drill), or 83 (peck)
  • tool (table): Tool data with keys: dia (number), tool_number (number), dir (number)
  • feedrate (number): Cutting feedrate
  • spindle_speed (number): Spindle speed in RPM
  • type (number): Operation type code from S:GetDrillCode()
    Returns
    (table) Array of G-code strings representing the complete drill operation
    Note
    Documentation generated by AI on 2026-03-03

◆ MakeHingeRect()

S MakeHingeRect ( )

Generate G-code for milling a hinge recess rectangle.

Creates toolpath G-code to mill out a hinge recess. The operation optionally uses a reversed tool to pre-cut the leading edge before switching to the primary milling tool. The profile pass cuts the rectangle boundary (with optional corner radii and bevel), followed by interior cleanup passes stepping down in Y from the top edge toward the bottom. Bevel angles are applied dynamically per move using S:Bevel(). Optional subroutine call strings can wrap the operation for use in parametric programs.

Parameters
info(table) Operation parameter table with the following keys:
  • height (number): Height of the hinge rectangle
  • width (number): Width of the hinge rectangle
  • step_over (number): Stepover factor relative to tool diameter for interior passes
  • corner (number|nil): Corner radius, or 0/nil for square corners
  • side (string): Face to cut on - 'Front', 'Back', 'Left', 'Right', 'Top', or 'Bottom'
  • depth (number): Depth of the cut
  • bevel (number|nil): Bevel angle in degrees, or 0/nil for no bevel
  • bevel_ref (number): Y reference position from which bevel angle is calculated
  • tool (table): Primary tool data with keys: dia (number), tool_number (number), dir (number)
  • feedrate (number): Primary tool cutting feedrate
  • spindle_speed (number): Primary tool spindle speed in RPM
  • rev_tool (table|nil): Optional reversed tool data (same keys as tool)
  • rev_feedrate (number|nil): Feedrate for the reversed tool
  • rev_spindle_speed (number|nil): Spindle speed for the reversed tool
  • start_sub (string|nil): Optional G-code string to insert at subroutine start
  • end_sub (string|nil): Optional G-code string to insert at subroutine end
  • type (number): Operation type code from S:GetHingeRectCode()
Returns
(table) Array of G-code strings representing the complete hinge rectangle operation
Note
Documentation generated by AI on 2026-03-03

◆ MakePocket()

S MakePocket ( )

Generate G-code for a pocket milling operation with semicircular ends.

Creates a pocket by incrementally plunging the tool while traversing a racetrack profile (rectangle with semicircular ends). The tool spirals to depth by advancing slightly in Z on each arc pass. The pocket profile consists of G02/G03 arc moves connecting the two semicircular ends, with straight G01 moves along the length. Depth is reached incrementally at a rate of tool_radius/5 per arc segment. Movement directions and the working plane are automatically determined from the side parameter.

Parameters
info(table) Operation parameter table with the following keys:
  • width (number): Width of the pocket
  • height (number): Height of the pocket
  • step_down (number): Factor of tool diameter to descend per pass around the edge
  • corner (number): Radius of the semicircular ends of the pocket
  • depth (number): Total depth of the pocket
  • side (string): Face to cut on - 'Front', 'Back', 'Left', 'Right', 'Top', or 'Bottom'
  • tool (table): Tool data with keys: dia (number), tool_number (number), dir (number)
  • feedrate (number): Cutting feedrate
  • spindle_speed (number): Spindle speed in RPM
  • type (number): Operation type code from S:GetPocketCode()
Returns
(table) Array of G-code strings representing the complete pocket operation
Note
Documentation generated by AI on 2026-03-03

◆ MakeStrikeRect()

S MakeStrikeRect ( )

Generate G-code for milling a strike plate rectangle.

Creates toolpath G-code to mill out a strike plate recess. The operation begins by profiling the four edges of the rectangle, then cleans out the interior with successive back-and-forth passes stepping down in Y. Supports optional bevel angles and rounded corners. The working plane is selected automatically based on the side parameter. Unlike MakeHingeRect, this operation profiles all four edges (including the top) and does not support a reversed tool for edge cleanup.

Parameters
info(table) Operation parameter table with the following keys:
  • height (number): Height of the strike rectangle
  • width (number): Width of the strike rectangle
  • step_over (number): Stepover factor relative to tool diameter for interior passes
  • corner (number|nil): Corner radius, or 0/nil for square corners
  • side (string): Face to cut on - 'Front', 'Back', 'Left', 'Right', 'Top', or 'Bottom'
  • depth (number): Depth of the cut (positive value)
  • bevel (number|nil): Bevel angle in degrees, or 0/nil for no bevel
  • tool (table): Tool data with keys: dia (number), tool_number (number), dir (number)
  • feedrate (number): Cutting feedrate
  • spindle_speed (number): Spindle speed in RPM
  • type (number): Operation type code from S:GetStrikeRectCode()
Returns
(table) Array of G-code strings representing the complete strike rectangle operation
Note
Documentation generated by AI on 2026-03-03

◆ OrderOps()

S OrderOps ( )

Order a list of machining operations by weight and tool number.

Sorts operations into an optimized execution sequence to minimize tool changes. Operations with a weight field are inserted first in ascending weight order. Among weighted operations, the last tool used is identified and any non-weighted operations sharing that tool are inserted next, reducing an unnecessary tool change. Remaining non-weighted operations are then appended grouped by tool number. Operations missing a type field are silently skipped.

Parameters
shape(table) Array of operation tables as passed to S:BuildShape()
Returns
(table) Ordered array of operation tables in optimized execution sequence
Note
Documentation generated by AI on 2026-03-03

◆ SquareCorners()

S SquareCorners ( )

Generate G-code to square the corners of a previously milled rectangle.

Uses a smaller tool to clean out the corner material left behind by the larger tool used to cut the parent rectangle. Supports both square corners (corner == 0 or nil) and rounded corners. When squaring a strike rectangle (sub_type == S.S_RECT), all four corners are squared; for hinge rectangles only the bottom two corners are addressed. The bevel reference and start positions are adjusted automatically based on sub_type. A safe rapid-in approach move precedes each corner plunge to protect the tool.

Parameters
info(table) Operation parameter table with the following keys:
  • height (number): Height of the rectangle being squared
  • width (number): Width of the rectangle being squared
  • side (string): Face to cut on - 'Front', 'Back', 'Left', 'Right', 'Top', or 'Bottom'
  • depth (number): Depth of the parent rectangle cut
  • bevel (number|nil): Bevel angle in degrees, or 0/nil for no bevel
  • bevel_ref (number): Y reference position from which bevel is calculated
  • corner (number|nil): Corner radius, or 0/nil for square corners
  • tool (table): Squaring tool data with keys: dia (number), tool_number (number), dir (number)
  • prev_tool (table): Previously used milling tool data - requires at least: dia (number)
  • feedrate (number): Cutting feedrate
  • spindle_speed (number): Spindle speed in RPM
  • sub_type (number): Parent shape type - S.S_RECT or S.H_RECT
  • type (number): Operation type code from S:GetSquaringCode()
  • start_sub (string|nil): Optional G-code string to insert at subroutine start
  • end_sub (string|nil): Optional G-code string to insert at subroutine end
Returns
(table) Array of G-code strings representing the complete corner squaring operation
Note
Documentation generated by AI on 2026-03-03

◆ StopSpindle()

S StopSpindle ( )

Conditionally insert a spindle stop command when the next operation uses a different tool.

Compares the tool number of the next operation (S.next_op) to the current operation (S.current_op). If they differ, or if there is no next operation, a spindle stop would be appropriate. The actual M5 D0 insertion is currently commented out in the implementation.

Parameters
gcode(table) The G-code line array to which the stop command would be appended
Note
The spindle stop insertion is currently disabled (commented out) in this implementation
Documentation generated by AI on 2026-03-03

◆ ToolCall()

S ToolCall ( tool_info ,
feed ,
speed ,
face  )

Insert tool change, tool offset, feedrate, and spindle speed G-code into a gcode table.

Checks whether the requested tool differs from the currently active tool using S:CurrentTool(). If a tool change is needed, generates the appropriate M6/G43 tool call sequence. For Right-side operations a G49 cancel and X positioning move are prepended. For Left-side operations a G53 machine-coordinate retract is used; otherwise a standard retract move is generated. Spindle speed is only output if it has changed from the current speed. The feedrate line is always appended when feed is non-nil, regardless of tool change. All generated lines are appended to the provided gcode table.

Parameters
gcode(table) The G-code line array to append tool call lines into
tool_info(table) Tool data table with keys: tool_number (number), dia (number), dir (number)
feed(number|nil) Feedrate to set. If nil, no feedrate line is output
speed(number) Spindle speed in RPM
face(string) The cutting face - 'Left', 'Right', or other face name
Note
Documentation generated by AI on 2026-03-03