class GCoder::GCode::Program

Public Class Methods

new(commands) click to toggle source
Calls superclass method
# File lib/gcoder/gcode.rb, line 57
def initialize(commands)
  super(commands)
end

Public Instance Methods

map_with_context() { |cmd, ctx| ... } click to toggle source

Interpretes aspects of the GCode program, namely position, feedrate, unit of measure and absolute/incremental positioning. This information is made available to the optional block as second parameter, through a ProgramContext instance.

Returns an array with two elements, the mapping result and context.

# File lib/gcoder/gcode.rb, line 69
def map_with_context(&block)
  ctx = ProgramContext.new

  map_result = self.map do |cmd|
    if block_given?
      r = yield(cmd, ctx)
    else
      r = cmd
    end

    if cmd.is_a? MoveRapid or cmd.is_a? MoveByFeedrate
      ctx.update_position cmd.position
      ctx.update_feedrate cmd.feedrate
    elsif cmd.is_a? ProgramCoordinatesAreMm
      ctx.units = :mm
    elsif cmd.is_a? ProgramCoordinatesAreInches
      ctx.units = :inch
    elsif cmd.is_a? AbsoluteProgrammingOfXYZ
      ctx.absolute = true
    elsif cmd.is_a? IncrementalProgrammingOfXYZ
      ctx.absolute = false
    end

    r
  end

  [map_result, ctx]
end