Class: Gcode::Object
- Inherits:
-
Object
- Object
- Gcode::Object
- Includes:
- Codes, PrettyOutput
- Defined in:
- lib/gcode/object.rb
Overview
A class that represents a processed Gcode file.
Constant Summary
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
- #comments ⇒ Float, ... readonly
-
#depth ⇒ Float
readonly
The depth of the print.
-
#e_travel ⇒ Float
readonly
The distance in total that the E axis will travel in mm.
-
#filament_used ⇒ Array<Float>
readonly
The amount in mm of fliament extruded with the index representing the extruder.
-
#height ⇒ Float
readonly
The height of the print.
- #layer_ranges ⇒ Float, ... readonly
-
#layers ⇒ Fixnum
readonly
The number of layers in the print.
-
#lines ⇒ Array<Line>
readonly
An array of Lines.
-
#raw_data ⇒ Array
An array of the raw Gcode with each line as an element.
- #total_duration ⇒ Float, ... readonly
-
#width ⇒ Float
readonly
The width of the print.
-
#x_max ⇒ Float
readonly
The biggest X coordinate of an extrusion line.
-
#x_min ⇒ Float
readonly
The smallest X coordinate of an extrusion line.
-
#x_travel ⇒ Float
readonly
The distance in total that the X axis will travel in mm.
-
#y_max ⇒ Float
readonly
The biggest Y coordinate of an extrusion line.
-
#y_min ⇒ Float
readonly
The smallest Y coordinate of an extrusion line.
-
#y_travel ⇒ Float
readonly
The distance in total that the Y axis will travel in mm.
-
#z_max ⇒ Float
readonly
The biggest Z coordinate.
-
#z_min ⇒ Float
readonly
The smallest Z coordinate.
-
#z_travel ⇒ Float
readonly
The distance in total that the Z axis will travel in mm.
Class Method Summary collapse
-
.get_file(file) ⇒ Array, false
Returns an array of the lines of the file if it exists.
-
.is_file?(file) ⇒ Boolean
Checks if the given string is a file and if it exists.
Instance Method Summary collapse
-
#blank? ⇒ Boolean
alias for #empty?.
-
#durration_in_words ⇒ String
Returns estimated durration of the print in a human readable format.
- #empty? ⇒ Boolean
-
#in_what_layer?(command_number) ⇒ Fixnum?
Get the layer number the given command number is in.
-
#initialize(options = {}) ⇒ Object, false
constructor
Creates a GCode Object.
-
#multi_material? ⇒ nil, Boolean
Checks if the GCode object contains multiple materials.
-
#present? ⇒ Boolean
Opposite of #empty?.
- #write_to_file(output_file, encoding = "us-ascii") ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Object, false
Creates a GCode Gcode::Object.
67 68 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 |
# File 'lib/gcode/object.rb', line 67 def initialize( = {}) if .is_a?(Array) temp_data = = {} [:data] = temp_data temp_data = nil end = .merge!() return false unless positive_number?([:default_speed]) return false unless positive_number?([:acceleration]) if [:data].class == String && self.class.is_file?([:data]) [:data] = self.class.get_file([:data]) end return false if [:data].nil? || [:data].class != Array @options = set_variables @raw_data.each do |line| line = set_line_properties(Line.new(line)) if line unless line.empty? @lines << line else @comments << line.comment end end end process if [:auto_process] return false if empty? end |
Instance Attribute Details
#comments ⇒ Float, ... (readonly)
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#depth ⇒ Float (readonly)
Returns the depth of the print.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#e_travel ⇒ Float (readonly)
implement this
Returns the distance in total that the E axis will travel in mm.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#filament_used ⇒ Array<Float> (readonly)
Returns the amount in mm of fliament extruded with the index representing the extruder.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#height ⇒ Float (readonly)
Returns the height of the print.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#layer_ranges ⇒ Float, ... (readonly)
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#layers ⇒ Fixnum (readonly)
Returns the number of layers in the print.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#lines ⇒ Array<Line> (readonly)
Returns an array of Lines.
54 55 56 |
# File 'lib/gcode/object.rb', line 54 def lines @lines end |
#raw_data ⇒ Array
An array of the raw Gcode with each line as an element.
13 14 15 |
# File 'lib/gcode/object.rb', line 13 def raw_data @raw_data end |
#total_duration ⇒ Float, ... (readonly)
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#width ⇒ Float (readonly)
Returns the width of the print.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#x_max ⇒ Float (readonly)
Returns the biggest X coordinate of an extrusion line.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#x_min ⇒ Float (readonly)
Returns the smallest X coordinate of an extrusion line.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#x_travel ⇒ Float (readonly)
Returns the distance in total that the X axis will travel in mm.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#y_max ⇒ Float (readonly)
Returns the biggest Y coordinate of an extrusion line.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#y_min ⇒ Float (readonly)
Returns the smallest Y coordinate of an extrusion line.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#y_travel ⇒ Float (readonly)
Returns the distance in total that the Y axis will travel in mm.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#z_max ⇒ Float (readonly)
Returns the biggest Z coordinate.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#z_min ⇒ Float (readonly)
Returns the smallest Z coordinate.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
#z_travel ⇒ Float (readonly)
Returns the distance in total that the Z axis will travel in mm.
54 55 56 57 |
# File 'lib/gcode/object.rb', line 54 attr_reader :lines, :x_min, :x_max, :y_min, :y_max, :z_min, :z_max, :filament_used, :x_travel, :y_travel, :z_travel, :e_travel, :width, :depth, :height, :layers, :total_duration, :comments, :layer_ranges |
Class Method Details
.get_file(file) ⇒ Array, false
Returns an array of the lines of the file if it exists.
108 109 110 111 |
# File 'lib/gcode/object.rb', line 108 def self.get_file(file) return false unless self.is_file?(file) IO.readlines(file) end |
.is_file?(file) ⇒ Boolean
Checks if the given string is a file and if it exists.
100 101 102 |
# File 'lib/gcode/object.rb', line 100 def self.is_file?(file) !file.nil? && !file.empty? && File.exist?(file) && File.file?(file) end |
Instance Method Details
#blank? ⇒ Boolean
alias for #empty?.
115 116 117 |
# File 'lib/gcode/object.rb', line 115 def blank? empty? end |
#durration_in_words ⇒ String
Returns estimated durration of the print in a human readable format.
141 142 143 |
# File 'lib/gcode/object.rb', line 141 def durration_in_words seconds_to_words(@total_duration) end |
#empty? ⇒ Boolean
121 122 123 |
# File 'lib/gcode/object.rb', line 121 def empty? @lines.empty? end |
#in_what_layer?(command_number) ⇒ Fixnum?
Get the layer number the given command number is in.
149 150 151 152 153 154 155 156 157 |
# File 'lib/gcode/object.rb', line 149 def in_what_layer?(command_number) return nil if @width.nil? || !command_number.is_a?(Fixnum) || command_number < 0 || command_number > @lines.length layer = 1 @layers.times do return layer if (@layer_ranges[layer][:lower]..@layer_ranges[layer][:upper]).include?(command_number) layer += 1 end nil end |
#multi_material? ⇒ nil, Boolean
Checks if the GCode object contains multiple materials.
134 135 136 137 |
# File 'lib/gcode/object.rb', line 134 def multi_material? return nil unless @width @filament_used.length > 1 end |
#present? ⇒ Boolean
Opposite of #empty?.
127 128 129 |
# File 'lib/gcode/object.rb', line 127 def present? !empty? end |
#write_to_file(output_file, encoding = "us-ascii") ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/gcode/object.rb', line 159 def write_to_file(output_file, encoding = "us-ascii") begin fd = File.open(output_file, 'w+') @lines.each do |line| fd.write (line.to_s+"; #{line.comment.to_s}"+"\n").encode(encoding) end ensure fd.close end end |