Class: Gcode::Object

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Object, false

Creates a GCode Gcode::Object.

Parameters:

  • data (String)

    path to a GCode file on the system.

  • data (Array)

    with each element being a line of GCode.

  • auto_process (Boolean)

    enable/disable auto processing.

  • default_speed (Float)

    the default speed (in mm/minute) for moves that don’t have one declared.

  • acceleration (Float)

    the acceleration rate set in the printer’ firmware.



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(options = {})
  if options.is_a?(Array)
    temp_data = options
    options = {}
    options[:data] = temp_data
    temp_data = nil
  end
  options = default_options.merge!(options)
  return false unless positive_number?(options[:default_speed])
  return false unless positive_number?(options[:acceleration])
  if options[:data].class == String && self.class.is_file?(options[:data])
    options[:data] = self.class.get_file(options[:data])
  end
  return false if options[:data].nil? || options[:data].class != Array
  @options = 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 options[:auto_process]
  return false if empty?
end

Instance Attribute Details

#commentsFloat, ... (readonly)

Returns:

  • (Float)

    the estimated durration of the print in seconds.

  • (Array)

    of the lines that only contained comments found in the file.

  • (Array<Hash<Fixnum>>)

    ranges of commands with their respective layer represented by the array index.



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

#depthFloat (readonly)

Returns the depth of the print.

Returns:

  • (Float)

    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_travelFloat (readonly)

TODO:

implement this

Returns the distance in total that the E axis will travel in mm.

Returns:

  • (Float)

    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_usedArray<Float> (readonly)

Returns the amount in mm of fliament extruded with the index representing the extruder.

Returns:

  • (Array<Float>)

    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

#heightFloat (readonly)

Returns the height of the print.

Returns:

  • (Float)

    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_rangesFloat, ... (readonly)

Returns:

  • (Float)

    the estimated durration of the print in seconds.

  • (Array)

    of the lines that only contained comments found in the file.

  • (Array<Hash<Fixnum>>)

    ranges of commands with their respective layer represented by the array index.



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

#layersFixnum (readonly)

Returns the number of layers in the print.

Returns:

  • (Fixnum)

    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

#linesArray<Line> (readonly)

Returns an array of Lines.

Returns:



54
55
56
# File 'lib/gcode/object.rb', line 54

def lines
  @lines
end

#raw_dataArray

An array of the raw Gcode with each line as an element.

Returns:

  • (Array)

    of raw Gcode without the comments stripped out.



13
14
15
# File 'lib/gcode/object.rb', line 13

def raw_data
  @raw_data
end

#total_durationFloat, ... (readonly)

Returns:

  • (Float)

    the estimated durration of the print in seconds.

  • (Array)

    of the lines that only contained comments found in the file.

  • (Array<Hash<Fixnum>>)

    ranges of commands with their respective layer represented by the array index.



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

#widthFloat (readonly)

Returns the width of the print.

Returns:

  • (Float)

    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_maxFloat (readonly)

Returns the biggest X coordinate of an extrusion line.

Returns:

  • (Float)

    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_minFloat (readonly)

Returns the smallest X coordinate of an extrusion line.

Returns:

  • (Float)

    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_travelFloat (readonly)

Returns the distance in total that the X axis will travel in mm.

Returns:

  • (Float)

    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_maxFloat (readonly)

Returns the biggest Y coordinate of an extrusion line.

Returns:

  • (Float)

    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_minFloat (readonly)

Returns the smallest Y coordinate of an extrusion line.

Returns:

  • (Float)

    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_travelFloat (readonly)

Returns the distance in total that the Y axis will travel in mm.

Returns:

  • (Float)

    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_maxFloat (readonly)

Returns the biggest Z coordinate.

Returns:

  • (Float)

    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_minFloat (readonly)

Returns the smallest Z coordinate.

Returns:

  • (Float)

    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_travelFloat (readonly)

Returns the distance in total that the Z axis will travel in mm.

Returns:

  • (Float)

    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.

Parameters:

  • file (String)

    path to a file on the system.

Returns:

  • (Array)

    containting the lines of the given file as elements.

  • (false)

    if given string isn’t a file or doesn’t exist.



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.

Parameters:

  • file (String)

    path to a file on the system.

Returns:

  • (Boolean)

    true if is a file that exists on the system, false otherwise.



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?.

Returns:

  • (Boolean)

See Also:



115
116
117
# File 'lib/gcode/object.rb', line 115

def blank?
  empty?
end

#durration_in_wordsString

Returns estimated durration of the print in a human readable format.

Returns:

  • (String)

    human readable estimated durration of the print.



141
142
143
# File 'lib/gcode/object.rb', line 141

def durration_in_words
  seconds_to_words(@total_duration)
end

#empty?Boolean

Checks if there are any Lines in #lines.

Returns:

  • (Boolean)

    true if no lines, false otherwise.



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.

Parameters:

  • command_number (Fixnum)

    number of the command who’s layer number you’d lke to know.

Returns:

  • (Fixnum)

    layer number for the given command number.

  • (nil)

    if the given command number is invalid or if the object wasn’t processed.



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.

Returns:

  • (nil)

    if processing hasn’t been done.

  • (Boolean)

    true if multiple extruders used, false otherwise.



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?.

Returns:

  • (Boolean)

See Also:



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