Class: GCoder::GCode::Program

Inherits:
SimpleDelegator
  • Object
show all
Includes:
GCoder::GCode
Defined in:
lib/gcoder/gcode.rb

Instance Method Summary collapse

Constructor Details

#initialize(commands) ⇒ Program

Returns a new instance of Program.



57
58
59
# File 'lib/gcoder/gcode.rb', line 57

def initialize(commands)
  super(commands)
end

Instance Method Details

#map_with_context(&block) ⇒ Object

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.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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