gcode_reader.emulate.exporters

Classes

GcodeExporter

Translates abstract Command objects into G-code strings or data series.

VTKExporter

Exports geometry and scalar data to PyVista and VTK formats.

Module Contents

class gcode_reader.emulate.exporters.GcodeExporter(syntax_name='default')[source]

Translates abstract Command objects into G-code strings or data series.

This class uses configurable “syntax flavors” to handle different G-code dialects. It can export a sequence of Command objects into a final G-code file or into a format suitable for data analysis (e.g., a Pandas DataFrame).

syntax

The syntax definition dictionary for the chosen flavor.

Type:

dict

word_map

A map to translate Command attribute names to G-code words.

Type:

dict

code_map

A map for specific G-code numbers (e.g., rapid move code).

Type:

dict

gcode_line_helpers

A list of helper methods used to construct a G-code line.

Type:

list

write_order

A list of G-code word keys defining their output order.

Type:

list

Initializes the GcodeExporter with a specific syntax flavor.

Parameters:

syntax_name (str, optional) – The name of the syntax flavor to use, which corresponds to a definition in gcode_reader.syntax.flavors. Defaults to “default”.

command_to_dict(command: gcode_reader.emulate.commands.Command) dict[source]

Converts a Command instance into a plain dict suitable for a DataFrame row.

Prefer this over command_to_pd_series when building a DataFrame from many commands — passing a list of dicts to pd.DataFrame() is significantly faster than passing a list of pd.Series.

Parameters:

command (Command) – The Command object to convert.

Returns:

A dictionary containing the command’s G-code words and metadata.

Return type:

dict

command_to_gcode_line(command: gcode_reader.emulate.commands.Command) str[source]

Constructs a complete G-code line string from a Command object.

This method orchestrates the G-code generation process. It first converts the command to a dictionary of words, then uses the list of gcode_line_helpers to assemble those words into a final, space- delimited G-code string.

Parameters:

command (Command) – The Command to convert into a G-code line.

Returns:

A single, formatted G-code line.

Return type:

str

command_to_gcode_words(command: gcode_reader.emulate.commands.Command) dict[source]

Converts a Command’s data into a dictionary of G-code words.

This method uses the exporter’s word_map to translate the Command’s internal attributes (e.g., position) into the corresponding G-code words and values (e.g., {‘X’: 10, ‘Y’: 20}). It’s the first step in converting a command to either a string or a data series.

Parameters:

command (Command) – The Command object to process.

Returns:

A dictionary mapping G-code words (e.g., ‘G’, ‘X’, ‘F’) to

their respective values.

Return type:

dict

command_to_pd_series(command: gcode_reader.emulate.commands.Command)[source]

Converts a Command instance into a pd.Series.

For bulk DataFrame construction use command_to_dict instead.

property code_map
gcode_line_helpers
property syntax
property word_map
write_order = ('G', 'M', 'X', 'Y', 'Z', 'A', 'B', 'C', 'F', 'E')
class gcode_reader.emulate.exporters.VTKExporter[source]

Exports geometry and scalar data to PyVista and VTK formats.

static additive_part_to_polydata(additive_part: gcode_reader.emulate.additive_part.AdditivePart, deposition_range: tuple = None, custom_scalars: dict = None) pyvista.PolyData[source]

Converts an AdditivePart to a PyVista PolyData mesh with scalar fields.

Parameters:
  • additive_part (AdditivePart) – The part to convert.

  • deposition_range (tuple, optional) – (start, end) pair of 0-based indices into the ordered sequence of depositing segments, selecting a contiguous subset to mesh. None includes all depositing segments.

  • custom_scalars (dict, optional) – Mapping of {name: array} for additional or replacement scalar arrays stored as cell data. Use this to supply measured data (e.g. temperature) or any other derived field. Array length must equal mesh.n_cells (one value per line-segment).

Returns:

Mesh with scalar arrays in cell_data.

Return type:

pv.PolyData

Raises:
  • TypeError – If additive_part is not an AdditivePart.

  • ImportError – If pyvista is not installed.

static additive_part_to_vtk(additive_part: gcode_reader.emulate.additive_part.AdditivePart, filepath: str, binary: bool = True, deposition_range: tuple = None)[source]

Exports an AdditivePart to a .vtk file with embedded scalar fields.

Parameters:
  • additive_part (AdditivePart) – The part to export.

  • filepath (str) – Destination path; must end with .vtk.

  • binary (bool, optional) – Write binary VTK format. Defaults to True.

  • deposition_range (tuple, optional) – (start, end) pair of 0-based indices into the ordered sequence of depositing segments to export. None exports all depositing segments.

Raises:
  • TypeError – If additive_part is not an AdditivePart.

  • ValueError – If filepath does not end with .vtk.

  • FileNotFoundError – If the parent directory of filepath does not exist.