Class: Gcode::Line
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
- #extrusion_multiplier ⇒ nil, Float
-
#f ⇒ Float
The speed of the command (in mm/minute).
-
#matches ⇒ Object
readonly
Returns the value of attribute matches.
-
#raw ⇒ String
readonly
The line, upcased and stripped of whitespace.
- #speed_multiplier ⇒ nil, Float
-
#tool_number ⇒ Fixnum
The tool used in the command.
- #travel_multiplier ⇒ nil, Float
-
#x_add ⇒ Object
Returns the value of attribute x_add.
-
#y_add ⇒ Object
Returns the value of attribute y_add.
-
#z_add ⇒ Object
Returns the value of attribute z_add.
Instance Method Summary collapse
-
#command ⇒ String?
The command in the line, nil if no command is present.
-
#command_letter ⇒ String?
The command letter of the line, nil if no command is present.
-
#command_number ⇒ Fixnum?
The command number of the line, nil if no command is present.
-
#comment ⇒ String?
The comment of the line, nil if no comment is present.
-
#e ⇒ Float?
The E value of the line, nil if no E value is present.
-
#empty? ⇒ Boolean
Checks if the given line is more than just a comment.
-
#extrusion_move? ⇒ Boolean
Checks whether the line is as extrusion move or not.
-
#full_home? ⇒ Boolean
Checks wether the line is a full home or not.
-
#initialize(line) ⇒ false, Line
constructor
Creates a Line.
-
#is_move? ⇒ Boolean
Checks if the command in the line causes movement.
-
#line ⇒ String?
Striped version of the input GCode, or nil if not valid GCode.
-
#p ⇒ Fixnum?
The P value of the line, nil if no P value is present.
-
#s ⇒ Fixnum?
The S value of the line, nil if no S value is present.
-
#string_data ⇒ String?
The string data of the line, nil if no string data is present.
-
#to_s(line_number = nil) ⇒ String
Returns the line, modified if multipliers are set and a line number is given.
-
#travel_move? ⇒ Boolean
Checks whether the line is a travel move or not.
-
#x ⇒ Float?
The X value of the line, nil if no X value is present.
-
#y ⇒ Float?
The Y value of the line, nil if no Y value is present.
-
#z ⇒ Float?
The Z value of the line, nil if no Z value is present.
Constructor Details
#initialize(line) ⇒ false, Line
Creates a Gcode::Line
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_multiplier ⇒ nil, Float
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 |
#f ⇒ Float
Returns 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 |
#matches ⇒ Object (readonly)
Returns the value of attribute matches.
37 |
# File 'lib/gcode/line.rb', line 37 attr_reader :raw, :matches |
#raw ⇒ String (readonly)
Returns the line, upcased and stripped of whitespace.
37 38 39 |
# File 'lib/gcode/line.rb', line 37 def raw @raw end |
#speed_multiplier ⇒ nil, Float
27 28 29 |
# File 'lib/gcode/line.rb', line 27 def speed_multiplier @speed_multiplier end |
#tool_number ⇒ Fixnum
Returns 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_multiplier ⇒ nil, Float
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_add ⇒ Object
Returns the value of attribute x_add.
27 28 29 |
# File 'lib/gcode/line.rb', line 27 def x_add @x_add end |
#y_add ⇒ Object
Returns the value of attribute y_add.
27 28 29 |
# File 'lib/gcode/line.rb', line 27 def y_add @y_add end |
#z_add ⇒ Object
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
#command ⇒ String?
The 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_letter ⇒ String?
The 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_number ⇒ Fixnum?
The 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 |
#comment ⇒ String?
The 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 |
#e ⇒ Float?
The 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.
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.
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.
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.
64 65 66 |
# File 'lib/gcode/line.rb', line 64 def is_move? command == RAPID_MOVE || command == CONTROLLED_MOVE end |
#line ⇒ String?
Striped version of the input GCode, or nil if not valid GCode
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 |
#p ⇒ Fixnum?
The 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 |
#s ⇒ Fixnum?
The 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_data ⇒ String?
The 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.
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.
70 71 72 |
# File 'lib/gcode/line.rb', line 70 def travel_move? is_move? && e.nil? end |
#x ⇒ Float?
The 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 |
#y ⇒ Float?
The 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 |
#z ⇒ Float?
The 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 |