class Gcode::Line

Represents a single line in a GCode file, parse expression tester: {rubular.com/}

Constants

GCODE_PATTERN

GCode matching pattern

Attributes

extrusion_multiplier[RW]

@!macro attr_accessor

@!attribute [rw] $1
  @param speed_multiplier [Float] number speed (F) will be multiplied by.
  @return [nil] if the speed multiplier is not set.
  @return [Float] the speed multiplier (print moves only).
@!attribute [rw] $2
  @param extrusion_multiplier [Float] number extrusions (E) will be multiplied by.
  @return [nil] if the extrusion multiplier is not set.
  @return [Float] the extrusion multiplier.
@!attribute [rw] $3
  @param travel_multiplier [Float] number travel move speeds (F) will be multiplied by.
  @return [nil] if the travel multiplier is not set.
  @return [Float] the travel multiplier.
@!attribute [rw] $4
  @param tool_number [Fixnum] the tool used in the command.
  @return [Fixnum] the tool used in the command.
@!attribute [rw] $5
  @param f [Float] speed of the command (in mm/minute).
  @return [Float] the speed of the command (in mm/minute).
f[RW]

@!macro attr_accessor

@!attribute [rw] $1
  @param speed_multiplier [Float] number speed (F) will be multiplied by.
  @return [nil] if the speed multiplier is not set.
  @return [Float] the speed multiplier (print moves only).
@!attribute [rw] $2
  @param extrusion_multiplier [Float] number extrusions (E) will be multiplied by.
  @return [nil] if the extrusion multiplier is not set.
  @return [Float] the extrusion multiplier.
@!attribute [rw] $3
  @param travel_multiplier [Float] number travel move speeds (F) will be multiplied by.
  @return [nil] if the travel multiplier is not set.
  @return [Float] the travel multiplier.
@!attribute [rw] $4
  @param tool_number [Fixnum] the tool used in the command.
  @return [Fixnum] the tool used in the command.
@!attribute [rw] $5
  @param f [Float] speed of the command (in mm/minute).
  @return [Float] the speed of the command (in mm/minute).
matches[R]

@!macro attr_reader

@!attribute [r] $1
  @return [String] the line, upcased and stripped of whitespace.
@!attribute [r] $2
  @return [nil] if the line wasn't valid GCode.
  @return [MatchData] the raw matches from the regular expression evaluation.
raw[R]

@!macro attr_reader

@!attribute [r] $1
  @return [String] the line, upcased and stripped of whitespace.
@!attribute [r] $2
  @return [nil] if the line wasn't valid GCode.
  @return [MatchData] the raw matches from the regular expression evaluation.
speed_multiplier[RW]

@!macro attr_accessor

@!attribute [rw] $1
  @param speed_multiplier [Float] number speed (F) will be multiplied by.
  @return [nil] if the speed multiplier is not set.
  @return [Float] the speed multiplier (print moves only).
@!attribute [rw] $2
  @param extrusion_multiplier [Float] number extrusions (E) will be multiplied by.
  @return [nil] if the extrusion multiplier is not set.
  @return [Float] the extrusion multiplier.
@!attribute [rw] $3
  @param travel_multiplier [Float] number travel move speeds (F) will be multiplied by.
  @return [nil] if the travel multiplier is not set.
  @return [Float] the travel multiplier.
@!attribute [rw] $4
  @param tool_number [Fixnum] the tool used in the command.
  @return [Fixnum] the tool used in the command.
@!attribute [rw] $5
  @param f [Float] speed of the command (in mm/minute).
  @return [Float] the speed of the command (in mm/minute).
tool_number[RW]

@!macro attr_accessor

@!attribute [rw] $1
  @param speed_multiplier [Float] number speed (F) will be multiplied by.
  @return [nil] if the speed multiplier is not set.
  @return [Float] the speed multiplier (print moves only).
@!attribute [rw] $2
  @param extrusion_multiplier [Float] number extrusions (E) will be multiplied by.
  @return [nil] if the extrusion multiplier is not set.
  @return [Float] the extrusion multiplier.
@!attribute [rw] $3
  @param travel_multiplier [Float] number travel move speeds (F) will be multiplied by.
  @return [nil] if the travel multiplier is not set.
  @return [Float] the travel multiplier.
@!attribute [rw] $4
  @param tool_number [Fixnum] the tool used in the command.
  @return [Fixnum] the tool used in the command.
@!attribute [rw] $5
  @param f [Float] speed of the command (in mm/minute).
  @return [Float] the speed of the command (in mm/minute).
travel_multiplier[RW]

@!macro attr_accessor

@!attribute [rw] $1
  @param speed_multiplier [Float] number speed (F) will be multiplied by.
  @return [nil] if the speed multiplier is not set.
  @return [Float] the speed multiplier (print moves only).
@!attribute [rw] $2
  @param extrusion_multiplier [Float] number extrusions (E) will be multiplied by.
  @return [nil] if the extrusion multiplier is not set.
  @return [Float] the extrusion multiplier.
@!attribute [rw] $3
  @param travel_multiplier [Float] number travel move speeds (F) will be multiplied by.
  @return [nil] if the travel multiplier is not set.
  @return [Float] the travel multiplier.
@!attribute [rw] $4
  @param tool_number [Fixnum] the tool used in the command.
  @return [Fixnum] the tool used in the command.
@!attribute [rw] $5
  @param f [Float] speed of the command (in mm/minute).
  @return [Float] the speed of the command (in mm/minute).
x_add[RW]

@!macro attr_accessor

@!attribute [rw] $1
  @param speed_multiplier [Float] number speed (F) will be multiplied by.
  @return [nil] if the speed multiplier is not set.
  @return [Float] the speed multiplier (print moves only).
@!attribute [rw] $2
  @param extrusion_multiplier [Float] number extrusions (E) will be multiplied by.
  @return [nil] if the extrusion multiplier is not set.
  @return [Float] the extrusion multiplier.
@!attribute [rw] $3
  @param travel_multiplier [Float] number travel move speeds (F) will be multiplied by.
  @return [nil] if the travel multiplier is not set.
  @return [Float] the travel multiplier.
@!attribute [rw] $4
  @param tool_number [Fixnum] the tool used in the command.
  @return [Fixnum] the tool used in the command.
@!attribute [rw] $5
  @param f [Float] speed of the command (in mm/minute).
  @return [Float] the speed of the command (in mm/minute).
y_add[RW]

@!macro attr_accessor

@!attribute [rw] $1
  @param speed_multiplier [Float] number speed (F) will be multiplied by.
  @return [nil] if the speed multiplier is not set.
  @return [Float] the speed multiplier (print moves only).
@!attribute [rw] $2
  @param extrusion_multiplier [Float] number extrusions (E) will be multiplied by.
  @return [nil] if the extrusion multiplier is not set.
  @return [Float] the extrusion multiplier.
@!attribute [rw] $3
  @param travel_multiplier [Float] number travel move speeds (F) will be multiplied by.
  @return [nil] if the travel multiplier is not set.
  @return [Float] the travel multiplier.
@!attribute [rw] $4
  @param tool_number [Fixnum] the tool used in the command.
  @return [Fixnum] the tool used in the command.
@!attribute [rw] $5
  @param f [Float] speed of the command (in mm/minute).
  @return [Float] the speed of the command (in mm/minute).
z_add[RW]

@!macro attr_accessor

@!attribute [rw] $1
  @param speed_multiplier [Float] number speed (F) will be multiplied by.
  @return [nil] if the speed multiplier is not set.
  @return [Float] the speed multiplier (print moves only).
@!attribute [rw] $2
  @param extrusion_multiplier [Float] number extrusions (E) will be multiplied by.
  @return [nil] if the extrusion multiplier is not set.
  @return [Float] the extrusion multiplier.
@!attribute [rw] $3
  @param travel_multiplier [Float] number travel move speeds (F) will be multiplied by.
  @return [nil] if the travel multiplier is not set.
  @return [Float] the travel multiplier.
@!attribute [rw] $4
  @param tool_number [Fixnum] the tool used in the command.
  @return [Fixnum] the tool used in the command.
@!attribute [rw] $5
  @param f [Float] speed of the command (in mm/minute).
  @return [Float] the speed of the command (in mm/minute).

Public Class Methods

new(line) click to toggle source

Creates a {Line} @param line [String] a line of GCode. @return [false] if line is empty or doesn’t match the evaluation expression. @return [Line]

# File lib/gcode/line.rb, line 46
def initialize(line)
  return false if line.nil? || line.empty?
  @raw = line
  @matches = @raw.match(GCODE_PATTERN)
  return false if @matches.nil?
  # assign_values
  @f = @matches[:f_data].to_f unless @matches[:f_data].nil?
  @tool_number = command_number if !command_letter.nil? && command_letter == 'T'
end

Public Instance Methods

command() click to toggle source

The command in the line, nil if no command is present. @return [String] command in the line. @return [nil] if no command is present.

# File lib/gcode/line.rb, line 123
def command
  @matches[:command]
end
command_letter() click to toggle source

The command letter of the line, nil if no command is present. @return [String] command letter of the line. @return [nil] if no command is present.

# File lib/gcode/line.rb, line 130
def command_letter
  @matches[:command_letter]
end
command_number() click to toggle source

The command number of the line, nil if no command is present. @return [Fixnum] command number of the line. @return [nil] if no command is present.

# File lib/gcode/line.rb, line 137
def command_number
  if @command_number.nil? && !@matches[:command_number].nil?
    @command_number = @matches[:command_number].to_i
  else
    @command_number
  end
end
comment() click to toggle source

The comment of the line, nil if no comment is present. @return [String] comment of the line. @return [nil] if no comment is present

# File lib/gcode/line.rb, line 225
def comment
  if @comment.nil? && !@matches[:comment].nil?
    @comment ||= @matches[:comment].strip
  else
    @comment
  end
end
e() click to toggle source

The E value of the line, nil if no E value is present. @return [Float] E value of the line. @return [nil] if no E value is present.

# File lib/gcode/line.rb, line 181
def e
  if @e.nil? && !@matches[:e_data].nil?
    @e = @matches[:e_data].to_f
  else
    @e
  end
end
empty?() click to toggle source

Checks if the given line is more than just a comment. @return [Boolean] true if empty/invalid

# File lib/gcode/line.rb, line 58
def empty?
  command.nil?
end
extrusion_move?() click to toggle source

Checks whether the line is as extrusion move or not. @return [Boolean] true if line is an extrusion move, false otherwise.

# File lib/gcode/line.rb, line 76
def extrusion_move?
  is_move? && !e.nil? && e > 0
end
full_home?() click to toggle source

Checks wether the line is a full home or not. @return [Boolean] true if line is full home, false otherwise.

# File lib/gcode/line.rb, line 82
def full_home?
  command == HOME && !x.nil? && !y.nil? && !z.nil?
end
is_move?() click to toggle source

Checks if the command in the line causes movement. @return [Boolean] true if command moves printer, false otherwise.

# File lib/gcode/line.rb, line 64
def is_move?
  command == RAPID_MOVE || command == CONTROLLED_MOVE
end
line() click to toggle source

Striped version of the input GCode, or nil if not valid GCode @return [String] striped line of GCode. @return [nil] if no GCode was present .

# File lib/gcode/line.rb, line 112
def line
  if @line.nil? && !@matches[:line].nil?
    @line = @matches[:line].strip
  else
    @line
  end
end
p() click to toggle source

The P value of the line, nil if no P value is present. @return [Fixnum] P value of the line. @return [nil] if no P value is present

# File lib/gcode/line.rb, line 203
def p
  if @p.nil? && !@matches[:p_data].nil?
    @p = @matches[:p_data].to_i
  else
    @p
  end
end
s() click to toggle source

The S value of the line, nil if no S value is present. @return [Fixnum] S value of the line. @return [nil] if no S value is present.

# File lib/gcode/line.rb, line 192
def s
  if @s.nil? && !@matches[:s_data].nil?
    @s = @matches[:s_data].to_i
  else
    @s
  end
end
string_data() click to toggle source

The string data of the line, nil if no string data is present. @return [String] string data of the line. @return [nil] if no string data is present

# File lib/gcode/line.rb, line 214
def string_data
  if @string_data.nil? && (!@matches[:string_data].nil? || !@matches[:string_data].empty?)
    @string_data = @matches[:string_data].strip
  else
    @string_data
  end
end
to_s(line_number = nil) click to toggle source

Returns the line, modified if multipliers are set and a line number is given. @return [String] the line.

# File lib/gcode/line.rb, line 88
def to_s(line_number = nil)
  # return line if line_number.nil? || !line_number.is_a?(Fixnum)
  # return prefix_line(line, line_number) if @extrusion_multiplier.nil? && @speed_multiplier.nil?

  new_f = multiplied_speed
  new_e = multiplied_extrusion

  x_string = !x.nil? ? " X#{x+@x_add.to_f}" : ''
  y_string = !y.nil? ? " Y#{y+@y_add.to_f}" : ''
  z_string = !z.nil? ? " Z#{z+@z_add.to_f}" : ''
  e_string = !e.nil? ? " E#{new_e}" : ''
  f_string = !f.nil? ? " F#{new_f}" : ''
  p_string = !p.nil? ? " P#{p}" : ""
  s_string = !s.nil? ? " S#{s}" : ""
  string = !string_data.nil? ? " #{string_data}" : ''

  prefix_line("#{command}#{s_string}#{p_string}#{x_string}#{y_string}#{z_string}#{f_string}#{e_string}#{string}".strip, line_number)
end
travel_move?() click to toggle source

Checks whether the line is a travel move or not. @return [Boolean] true if line is a travel move, false otherwise.

# File lib/gcode/line.rb, line 70
def travel_move?
  is_move? && e.nil?
end
x() click to toggle source

The X value of the line, nil if no X value is present. @return [Float] X value of the line. @return [nil] if no X value is present.

# File lib/gcode/line.rb, line 148
def x
  if @x.nil? && !@matches[:x_data].nil?
    @x = @matches[:x_data].to_f
  else
    @x
  end
end
y() click to toggle source

The Y value of the line, nil if no Y value is present. @return [Float] Y value of the line. @return [nil] if no Y value is present.

# File lib/gcode/line.rb, line 159
def y
  if @y.nil? && !@matches[:y_data].nil?
    @y = @matches[:y_data].to_f
  else
    @y
  end
end
z() click to toggle source

The Z value of the line, nil if no Z value is present. @return [Float] Z value of the line. @return [nil] if no Z value is present.

# File lib/gcode/line.rb, line 170
def z
  if @z.nil? && !@matches[:z_data].nil?
    @z = @matches[:z_data].to_f
  else
    @z
  end
end

Private Instance Methods

get_checksum(command) click to toggle source
# File lib/gcode/line.rb, line 257
def get_checksum(command)
  command.bytes.inject{|a,b| a^b}.to_s
end
multiplied_extrusion() click to toggle source
# File lib/gcode/line.rb, line 235
def multiplied_extrusion
  if !e.nil? && valid_multiplier?(@extrusion_multiplier)
    return e * @extrusion_multiplier
  else
    e
  end
end
multiplied_speed() click to toggle source
# File lib/gcode/line.rb, line 243
def multiplied_speed
  if travel_move? && valid_multiplier?(@travel_multiplier)
    return f * @travel_multiplier
  elsif extrusion_move? && valid_multiplier?(@speed_multiplier)
    return f * @speed_multiplier
  else
   return f
  end
end
prefix_line(command, line_number) click to toggle source
# File lib/gcode/line.rb, line 261
def prefix_line(command, line_number)
  return command if line_number.nil?
  prefix = 'N' + line_number.to_s + ' ' + command
  (prefix+'*'+get_checksum(prefix))
end
valid_multiplier?(multiplier) click to toggle source
# File lib/gcode/line.rb, line 253
def valid_multiplier?(multiplier)
  !multiplier.nil? && (multiplier.class == Fixnum || multiplier.class == Float) && multiplier > 0
end