gcode-preview - v3.0.0-alpha.3
    Preparing search index...

    Class Parser

    A G-code parser that processes G-code commands and extracts metadata.

    This parser handles both single-line and multi-line G-code input, extracting commands, parameters, and metadata such as thumbnails. It preserves comments and maintains the original source lines.

    const parser = new Parser();
    const result = parser.parseGCode('G1 X100 Y100 F1000 ; Move to position');
    Index

    Constructors

    Properties

    lines: string[] = []

    Original G-code lines stored for reference

    metadata: Metadata = ...

    Metadata extracted from G-code comments, including thumbnails

    Methods

    • Parses a single line of G-code into a command object.

      Parameters

      • line: string

        Single line of G-code to parse

      • keepComments: boolean = true

        Whether to preserve comments in the parsed command (default: true)

      Returns GCodeCommand

      Parsed GCodeCommand object or null if line is empty/invalid

      This method handles the parsing of individual G-code lines, including:

      • Separating commands from comments
      • Extracting the G-code command (e.g., G0, G1)
      • Parsing parameters
      const cmd = parser.parseCommand('G1 X100 Y100 F1000 ; Move to position');
      
    • Parses G-code input into commands and metadata

      Parameters

      • input: string | string[]

        G-code to parse, either as a string or array of lines

      Returns ParseResult

      Object containing parsed metadata and commands

      This method handles both single-line and multi-line G-code input, extracting commands, parameters, and metadata such as thumbnails. It preserves comments and maintains the original source lines.

      const parser = new Parser();
      const result = parser.parseGCode('G1 X100 Y100 F1000 ; Move to position');
    • Extracts metadata from G-code commands, particularly focusing on thumbnails.

      Parameters

      • metadata: GCodeCommand[]

        Array of G-code commands containing metadata in comments

      Returns Metadata

      Object containing extracted metadata (currently only thumbnails)

      This method processes special comments in the G-code that contain metadata. Currently, it focuses on extracting thumbnail data that some slicers embed in the G-code file. The thumbnail data is typically found between 'thumbnail begin' and 'thumbnail end' markers in the comments.

      The method handles multi-line thumbnail data by accumulating characters until it encounters the end marker. Once complete, it validates the thumbnail data before storing it in the thumbnails record.

      const commands = parser.parseGCode(gcode).commands;
      const metadata = parser.parseMetadata(commands.filter(cmd => cmd.comment));