gcode_reader.emulate.machine ============================ .. py:module:: gcode_reader.emulate.machine Classes ------- .. autoapisummary:: gcode_reader.emulate.machine.Machine Module Contents --------------- .. py:class:: Machine(tools: Tuple = None, parser: gcode_reader.emulate.parsers.GcodeParser = GcodeParser(), exporter: gcode_reader.emulate.exporters.GcodeExporter = GcodeExporter(), **options) An emulation environment for a generic CNC machine. This class acts as an orchestrator for processing sequences of machine commands (Operations). It maintains the machine's state, such as position and feed rate, and applies kinematic models (motion profiles) to each movement. It is designed to be configurable with different tools, parsers, exporters, and machine-specific options. .. attribute:: options The configuration object containing parameters like max feedrate and acceleration. :type: machine_options.MachineOptions .. attribute:: tools A tuple of tool objects attached to the machine. :type: Tuple[Tool, ...] .. attribute:: parser The parser instance used to convert G-code files into Command objects. :type: GcodeParser .. attribute:: exporter The exporter instance used to convert Command objects back into G-code strings or other formats. :type: GcodeExporter .. attribute:: home The machine's home coordinates (X, Y, Z). :type: Tuple[float, float, float] .. attribute:: motion_profile_type The motion profile class (e.g., Trapezoid) used to calculate movement kinematics. :type: type Initializes a new Machine instance. :param tools: A tuple of Tool instances for the machine to use. Defaults to None. :type tools: Tuple[Tool, ...], optional :param parser: An instance of a GcodeParser. Defaults to a new GcodeParser(). :type parser: GcodeParser, optional :param exporter: An instance of a GcodeExporter. Defaults to a new GcodeExporter(). :type exporter: GcodeExporter, optional :param \*\*options: Keyword arguments passed to the constructor of the machine's `MachineOptions` type. .. py:method:: abc_to_tool_direction(A: float, B: float, C: float) -> tuple :staticmethod: Convert 5-axis A/B/C Euler angles (degrees) to a unit tool direction vector. Rotations about X (A), Y (B), Z (C) are applied in that order from Z-up. Reference Image: https://www.mastercam.com/wp-content/uploads/2021/09/axes.png :param A: Rotation about X-axis in degrees. :param B: Rotation about Y-axis in degrees. :param C: Rotation about Z-axis in degrees. :returns: Normalised (x, y, z) tuple representing the tool direction. .. py:method:: apply_motion_profile_to_operation(operation: gcode_reader.emulate.operations.Operation) .. py:method:: clear_history() Clears this Machine's command history. .. py:method:: export_operation_event_series(operation: gcode_reader.emulate.operations.Operation, spatial_scale_factor: float = 0.001, destination: str = None) Write a process event series for an Operation :param operation: The Operation object to be processed. It should have been created or processed by this same machine type. :type operation: Operation :param spatial_scale_factor: A multiplicative factor to convert spatial units from the Operation (typically millimeters) to the desired output units (typically meters). Defaults to 1e-3. :type spatial_scale_factor: float, optional :param destination: The full path for the output file. If `None`, a path is automatically generated from the operation's name, e.g., 'MyPart_event_series.aes'. Defaults to None. :type destination: str | None, optional :returns: A DataFrame containing the event series. :rtype: pandas.DataFrame .. py:method:: gcode_file_to_operation(filepath: str, operation_type: type = Operation) Parses a G-code file and returns a processed Operation. This method uses the machine's configured parser to read a G-code file into a list of commands, wraps them in the specified `operation_type`, and then processes the operation to populate its process data. :param filepath: The path to the G-code file. :type filepath: str :param operation_type: The class of Operation to create (e.g., `Operation`, `AdditiveOperation`). Defaults to `Operation`. :type operation_type: type, optional :returns: A new, processed Operation instance. :rtype: Operation :raises ValueError: If `operation_type` is not a valid subclass of `Operation`. .. py:method:: get_motion_limits() Retrieves and prepares motion limits from the machine's options. :returns: A dictionary containing 'velocity', 'acceleration', and 'jerk' limits, with velocity converted to units/sec. :rtype: dict .. py:method:: operation_to_dataframe(operation: gcode_reader.emulate.operations.Operation) -> pandas.DataFrame Processes an Operation and converts it into a Pandas DataFrame. This method ensures an operation is fully processed, then converts each command and its corresponding process data into a pandas Series. These are combined into a single DataFrame for analysis. :param operation: The operation to be converted. :type operation: Operation :returns: A DataFrame where each row represents a command and its associated process data. :rtype: pd.DataFrame .. py:method:: process_command(command: gcode_reader.emulate.commands.Command, operation_type=Operation, skip_tool=False) -> gcode_reader.emulate.operations.ProcessData Processes a single command to generate its ProcessData. This method dispatches a command to the appropriate internal helper (_move, _dwell, etc.) and optionally to any attached tools to calculate derived data like new position, distance traveled, and tool-specific metrics. :param command: The command to process. :param operation_type: The parent operation's type, used to determine the type of ProcessData to create. Defaults to Operation. :param skip_tool: If True, skips tool processing. Defaults to False. :returns: The populated data object for the command. :rtype: ProcessData .. py:method:: process_operation(operation: gcode_reader.emulate.operations.Operation, force=False, skip_tool=False) -> gcode_reader.emulate.operations.Operation Fully processes an Operation, calculating all derived data. This is the main entry point for simulating an operation. It first calculates basic process data (location, distance, etc.) for each command. It then applies the machine-wide motion profile to calculate kinematic data like elapsed time. It avoids re-processing an operation that has already been processed by this same machine instance unless `force` is True. :param operation: The operation to process. :param force: If True, re-processes the operation even if it has been processed by this machine before. Defaults to False. :param skip_tool: If True, skips tool processing during the command processing phase. Useful for two-pass processing where tools depend on motion profile data (e.g., elapsed_time) that isn't available until after the first pass. Defaults to False. :returns: A new, fully processed Operation instance. :rtype: Operation .. py:method:: translate_command(command: gcode_reader.emulate.commands.Command, old_meta: gcode_reader.emulate.operations.ProcessData) Translates a command for a different machine context. This helper function is intended to be called when transferring an operation processed by one machine to another. It allows for machine- specific adjustments to the command itself. :param command: The command to translate. :type command: Command :param old_meta: The process data of the command as calculated by the previous machine. :type old_meta: ProcessData :returns: The translated command. :rtype: Command .. py:method:: write_gcode_file(filepath: str, operation: gcode_reader.emulate.operations.Operation, overwrite: bool = False) Processes an Operation and writes the resulting G-code to a file. This method processes the sequence of commands in the operation and uses the exporter to generate a formatted G-code string for each one. The resulting lines are then written to the specified file. WARNING: ALWAYS REVIEW GENERATED G-CODE BEFORE FOR CORRECTNESS AND SAFETY BEFORE EXECUTING ON A MACHINE. :param filepath: The path to the output file. :type filepath: str :param operation: The operation containing the commands to write. :type operation: Operation :param overwrite: If True, an existing file at the path will be overwritten. If False (default), the operation will fail if the file already exists. :type overwrite: bool, optional .. py:property:: absolute_position .. py:property:: coordinate_system .. py:property:: exporter .. py:attribute:: feed_rate_time_base_s :value: 1.0 .. py:property:: home .. py:property:: incremental_position .. py:property:: motion_profile .. py:property:: options .. py:property:: parser .. py:attribute:: process_command_helpers .. py:property:: tools .. py:attribute:: unit_per_meter :value: 1.0 .. py:property:: work_offset