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

    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, when created with keepLines, retains the original source lines.

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

    Constructors

    Properties

    lineCount: number = 0

    How many lines have been parsed, counting every call.

    Tracked whether or not the lines themselves are kept. Counts exactly what lines would hold, so a trailing newline contributes one final empty line.

    lines: string[] = []

    Original G-code lines, in the order they were parsed.

    Only populated when the parser was created with keepLines. Streaming parses append to this, so it spans the whole input rather than just the most recent chunk.

    String input is split on '\n', so input ending in a newline yields one final empty line here. That is the split's honest answer and is deliberately not filtered out.

    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, when the parser was created with keepLines, appends the original source lines to 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.

      The in-progress thumbnail lives on the parser instance, so a block that spans multiple calls (a streaming parse hands each chunk to parseGCode separately) accumulates across them. Each call returns only the thumbnails completed during that call; a block still open at the end of a call carries into the next one.

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