Class: Gcode::Line

Inherits:
Object
  • Object
show all
Includes:
Codes
Defined in:
lib/gcode/line.rb

Overview

Represents a single line in a GCode file, parse expression tester: /

Constant Summary collapse

GCODE_PATTERN =

GCode matching pattern

/^(?<line>(?<command>((?<command_letter>[G|M|T])(?<command_number>\d{1,3}))) ?(?<regular_data>([S](?<s_data>\d*))? ?([P](?<p_data>\d*))? ?([X](?<x_data>[-]?\d+\.?\d*))? ?([Y](?<y_data>[-]?\d+\.?\d*))? ?([Z](?<z_data>[-]?\d+\.?\d*))? ?([F](?<f_data>\d+\.?\d*))? ?([E|A](?<e_data>[-]?\d+\.?\d*))?)? ?(?<string_data>[^;]*)?)? ?;?(?<comment>.*)?$/

Constants included from Codes

Codes::ABS_EXT_MODE, Codes::ABS_POSITIONING, Codes::COMMENT_SYMBOL, Codes::CONTROLLED_MOVE, Codes::DISABLE_MOTORS, Codes::DWELL, Codes::EMRG_STOP, Codes::ENABLE_MOTORS, Codes::FAN_OFF, Codes::FAN_ON, Codes::GET_EXT_TEMP, Codes::GET_FW_DETAILS, Codes::GET_POSITION, Codes::HEAD_OFFSET, Codes::HOME, Codes::IDLE_HOLD_OFF, Codes::INIT_SD, Codes::LIST_SD, Codes::PAUSE_SD_PRINT, Codes::POWER_OFF, Codes::POWER_ON, Codes::RAPID_MOVE, Codes::RELEASE_SD, Codes::REL_EXT_MODE, Codes::REL_POSITIONING, Codes::SD_PRINT_STATUS, Codes::SELECT_SD_FILE, Codes::SET_BED_TEMP_NW, Codes::SET_BED_TEMP_W, Codes::SET_EXT_TEMP_NW, Codes::SET_EXT_TEMP_W, Codes::SET_LINE_NUM, Codes::SET_POSITION, Codes::SET_SD_POSITION, Codes::SLEEP, Codes::START_SD_PRINT, Codes::START_SD_WRITE, Codes::STOP, Codes::STOP_SD_WRITE, Codes::USE_INCHES, Codes::USE_MILLIMETRES, Codes::WIAT_FOR_TEMP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ false, Line

Creates a Gcode::Line

Parameters:

  • line (String)

    a line of GCode.



46
47
48
49
50
51
52
53
54
# 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

Instance Attribute Details

#extrusion_multipliernil, Float

Parameters:

  • extrusion_multiplier (Float)

    number extrusions (E) will be multiplied by.

Returns:

  • (nil)

    if the extrusion multiplier is not set.

  • (Float)

    the extrusion multiplier.



27
28
29
# File 'lib/gcode/line.rb', line 27

attr_accessor :speed_multiplier, :extrusion_multiplier,
:travel_multiplier, :tool_number, :f, :x_add,
:y_add, :z_add

#fFloat

Returns the speed of the command (in mm/minute).

Parameters:

  • f (Float)

    speed of the command (in mm/minute).

Returns:

  • (Float)

    the speed of the command (in mm/minute).



27
28
29
# File 'lib/gcode/line.rb', line 27

attr_accessor :speed_multiplier, :extrusion_multiplier,
:travel_multiplier, :tool_number, :f, :x_add,
:y_add, :z_add

#matchesObject (readonly)

Returns the value of attribute matches.



37
# File 'lib/gcode/line.rb', line 37

attr_reader :raw, :matches

#rawString (readonly)

Returns the line, upcased and stripped of whitespace.

Returns:

  • (String)

    the line, upcased and stripped of whitespace.



37
38
39
# File 'lib/gcode/line.rb', line 37

def raw
  @raw
end

#speed_multipliernil, Float

Parameters:

  • speed_multiplier (Float)

    number speed (F) will be multiplied by.

Returns:

  • (nil)

    if the speed multiplier is not set.

  • (Float)

    the speed multiplier (print moves only).



27
28
29
# File 'lib/gcode/line.rb', line 27

def speed_multiplier
  @speed_multiplier
end

#tool_numberFixnum

Returns the tool used in the command.

Parameters:

  • tool_number (Fixnum)

    the tool used in the command.

Returns:

  • (Fixnum)

    the tool used in the command.



27
28
29
# File 'lib/gcode/line.rb', line 27

attr_accessor :speed_multiplier, :extrusion_multiplier,
:travel_multiplier, :tool_number, :f, :x_add,
:y_add, :z_add

#travel_multipliernil, Float

Parameters:

  • travel_multiplier (Float)

    number travel move speeds (F) will be multiplied by.

Returns:

  • (nil)

    if the travel multiplier is not set.

  • (Float)

    the travel multiplier.



27
28
29
# File 'lib/gcode/line.rb', line 27

attr_accessor :speed_multiplier, :extrusion_multiplier,
:travel_multiplier, :tool_number, :f, :x_add,
:y_add, :z_add

#x_addObject

Returns the value of attribute x_add.



27
28
29
# File 'lib/gcode/line.rb', line 27

def x_add
  @x_add
end

#y_addObject

Returns the value of attribute y_add.



27
28
29
# File 'lib/gcode/line.rb', line 27

def y_add
  @y_add
end

#z_addObject

Returns the value of attribute z_add.



27
28
29
# File 'lib/gcode/line.rb', line 27

def z_add
  @z_add
end

Instance Method Details

#commandString?

The command in the line, nil if no command is present.

Returns:

  • (String)

    command in the line.

  • (nil)

    if no command is present.



123
124
125
# File 'lib/gcode/line.rb', line 123

def command
  @matches[:command]
end

#command_letterString?

The command letter of the line, nil if no command is present.

Returns:

  • (String)

    command letter of the line.

  • (nil)

    if no command is present.



130
131
132
# File 'lib/gcode/line.rb', line 130

def command_letter
  @matches[:command_letter]
end

#command_numberFixnum?

The command number of the line, nil if no command is present.

Returns:

  • (Fixnum)

    command number of the line.

  • (nil)

    if no command is present.



137
138
139
140
141
142
143
# 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

#commentString?

The comment of the line, nil if no comment is present.

Returns:

  • (String)

    comment of the line.

  • (nil)

    if no comment is present



225
226
227
228
229
230
231
# File 'lib/gcode/line.rb', line 225

def comment
  if @comment.nil? && !@matches[:comment].nil?
    @comment ||= @matches[:comment].strip
  else
    @comment
  end
end

#eFloat?

The E value of the line, nil if no E value is present.

Returns:

  • (Float)

    E value of the line.

  • (nil)

    if no E value is present.



181
182
183
184
185
186
187
# 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?Boolean

Checks if the given line is more than just a comment.

Returns:

  • (Boolean)

    true if empty/invalid



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

def empty?
  command.nil?
end

#extrusion_move?Boolean

Checks whether the line is as extrusion move or not.

Returns:

  • (Boolean)

    true if line is an extrusion move, false otherwise.



76
77
78
# File 'lib/gcode/line.rb', line 76

def extrusion_move?
  is_move? && !e.nil? && e > 0
end

#full_home?Boolean

Checks wether the line is a full home or not.

Returns:

  • (Boolean)

    true if line is full home, false otherwise.



82
83
84
# File 'lib/gcode/line.rb', line 82

def full_home?
  command == HOME && !x.nil? && !y.nil? && !z.nil?
end

#is_move?Boolean

Checks if the command in the line causes movement.

Returns:

  • (Boolean)

    true if command moves printer, false otherwise.



64
65
66
# File 'lib/gcode/line.rb', line 64

def is_move?
  command == RAPID_MOVE || command == CONTROLLED_MOVE
end

#lineString?

Striped version of the input GCode, or nil if not valid GCode

Returns:

  • (String)

    striped line of GCode.

  • (nil)

    if no GCode was present .



112
113
114
115
116
117
118
# File 'lib/gcode/line.rb', line 112

def line
  if @line.nil? && !@matches[:line].nil?
    @line = @matches[:line].strip
  else
    @line
  end
end

#pFixnum?

The P value of the line, nil if no P value is present.

Returns:

  • (Fixnum)

    P value of the line.

  • (nil)

    if no P value is present



203
204
205
206
207
208
209
# 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

#sFixnum?

The S value of the line, nil if no S value is present.

Returns:

  • (Fixnum)

    S value of the line.

  • (nil)

    if no S value is present.



192
193
194
195
196
197
198
# 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_dataString?

The string data of the line, nil if no string data is present.

Returns:

  • (String)

    string data of the line.

  • (nil)

    if no string data is present



214
215
216
217
218
219
220
# 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) ⇒ String

Returns the line, modified if multipliers are set and a line number is given.

Returns:

  • (String)

    the line.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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?Boolean

Checks whether the line is a travel move or not.

Returns:

  • (Boolean)

    true if line is a travel move, false otherwise.



70
71
72
# File 'lib/gcode/line.rb', line 70

def travel_move?
  is_move? && e.nil?
end

#xFloat?

The X value of the line, nil if no X value is present.

Returns:

  • (Float)

    X value of the line.

  • (nil)

    if no X value is present.



148
149
150
151
152
153
154
# 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

#yFloat?

The Y value of the line, nil if no Y value is present.

Returns:

  • (Float)

    Y value of the line.

  • (nil)

    if no Y value is present.



159
160
161
162
163
164
165
# 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

#zFloat?

The Z value of the line, nil if no Z value is present.

Returns:

  • (Float)

    Z value of the line.

  • (nil)

    if no Z value is present.



170
171
172
173
174
175
176
# 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