Source code for gcode_reader.emulate.tools
from .operations import ProcessData, SubtractiveProcessData
from .machine_options import MachineOptions
from .commands import Command, Mill
[docs]
class Tool:
"""A base class representing a generic tool on a manufacturing machine.
This class manages the tool's fundamental state. It provides a common interface
for processing commands and can be extended by specific tool types.
Attributes:
on (bool): Indicates if the tool is currently active.
options (MachineOptions): Configuration options for the machine and tool.
"""
def __init__(self, options: MachineOptions = None):
"""Initializes a new Tool instance.
Args:
options (MachineOptions): An instance of the MachineOptions class
containing machine and tool configuration.
"""
self.on = True
if options is None:
options = MachineOptions()
self.options = options
@property
def options(self):
return self._options
@options.setter
def options(self, value):
self._options = value
[docs]
def process_command(self, command: Command, process_data: ProcessData):
"""Processes a command to update the tool's state and record data.
Subclasses should extend this to handle tool-specific command attributes
(e.g. spindle speed, extrusion volume). Tool direction is tracked by the
machine via ``_tool_rotate`` and does not need to be set here.
Args:
command (Command): The command to be processed.
process_data (ProcessData): The data object to populate with
post-processing information.
Returns:
The updated ProcessData object.
"""
return process_data
[docs]
class MillingTool(Tool):
"""A subtractive tool, such as a CNC mill, controlled by a spindle speed.
Attributes:
spindle_speed (float): The current rotational speed of the spindle in RPM.
diameter (float): The diameter of the milling tool.
"""
def __init__(self, options, diameter: float = 1.0):
super().__init__(options)
self.spindle_speed = 0.0
self.diameter = diameter
[docs]
def process_command(self, command: Command, process_data: SubtractiveProcessData):
"""Propagates spindle speed into process_data on every command.
Updates self.spindle_speed if a Mill command provides an explicit speed,
then writes the current speed to process_data whenever the spindle is on.
Args:
command (Command): The command to process.
process_data (SubtractiveProcessData): The data object to populate.
Returns:
The updated SubtractiveProcessData object.
"""
process_data = super().process_command(command, process_data)
if isinstance(command, Mill):
self.spindle_speed = command.spindle
if self.on:
process_data.spindle_speed = self.spindle_speed
return process_data