Class: Svgcode::GCode::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/svgcode/gcode/command.rb

Constant Summary collapse

INT_FORMAT =
"%02d"
FLOAT_FORMAT =
"%.3f"
INT_LETTERS =
[ 'M', 'G' ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(letter_or_str = nil, number = nil, args = []) ⇒ Command

Returns a new instance of Command.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/svgcode/gcode/command.rb', line 10

def initialize(letter_or_str = nil, number = nil, args = [])
  if letter_or_str.length > 1
    parts = letter_or_str.split(/\s+/)
    cmd = Command.parse_single(parts.shift)
    @letter = cmd.letter
    @number = cmd.number
    @args = parts.collect { |arg| Command.parse_single(arg) }
  else
    @letter = letter_or_str
    @number = number
    @args = args
  end

  @letter = @letter.to_s.upcase
  @number = @number.to_f unless @number.nil?
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/svgcode/gcode/command.rb', line 8

def args
  @args
end

#letterObject (readonly)

Returns the value of attribute letter.



8
9
10
# File 'lib/svgcode/gcode/command.rb', line 8

def letter
  @letter
end

#numberObject (readonly)

Returns the value of attribute number.



8
9
10
# File 'lib/svgcode/gcode/command.rb', line 8

def number
  @number
end

Class Method Details

.absoluteObject



68
69
70
# File 'lib/svgcode/gcode/command.rb', line 68

def self.absolute
  Command.new(:g, 90)
end

.clear(clearance) ⇒ Object



121
122
123
124
125
# File 'lib/svgcode/gcode/command.rb', line 121

def self.clear(clearance)
  Command.new(:g, 0, [
    Command.new(:z, clearance)
  ])
end

.comment(str) ⇒ Object



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

def self.comment(str)
  str.gsub!(/[()]/, '')
  "\n(#{str})"
end

.cubic_spline(i, j, _p, q, x, y) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/svgcode/gcode/command.rb', line 110

def self.cubic_spline(i, j, _p, q, x, y)
  Command.new(:g, 5, [
    Command.new(:i, i),
    Command.new(:j, j),
    Command.new(:p, _p),
    Command.new(:q, q),
    Command.new(:x, x),
    Command.new(:y, y)
  ])
end

.cut(x, y) ⇒ Object



103
104
105
106
107
108
# File 'lib/svgcode/gcode/command.rb', line 103

def self.cut(x, y)
  Command.new(:g, 1, [
    Command.new(:x, x),
    Command.new(:y, y)
  ])
end

.feedrate(rate) ⇒ Object



92
93
94
# File 'lib/svgcode/gcode/command.rb', line 92

def self.feedrate(rate)
  Command.new(:f, rate)
end

.g(number, args = nil) ⇒ Object



133
134
135
# File 'lib/svgcode/gcode/command.rb', line 133

def self.g(number, args = nil)
  Command.new(:g, number, args)
end

.go(x, y) ⇒ Object



96
97
98
99
100
101
# File 'lib/svgcode/gcode/command.rb', line 96

def self.go(x, y)
  Command.new(:g, 0, [
    Command.new(:x, x),
    Command.new(:y, y)
  ])
end

.homeObject



84
85
86
# File 'lib/svgcode/gcode/command.rb', line 84

def self.home
  Command.new(:g, 30)
end

.imperialObject



80
81
82
# File 'lib/svgcode/gcode/command.rb', line 80

def self.imperial
  Command.new(:g, 20)
end

.m(number, args = nil) ⇒ Object



137
138
139
# File 'lib/svgcode/gcode/command.rb', line 137

def self.m(number, args = nil)
  Command.new(:m, number, args)
end

.metricObject



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

def self.metric
  Command.new(:g, 21)
end

.parse_single(str) ⇒ Object



57
58
59
60
61
# File 'lib/svgcode/gcode/command.rb', line 57

def self.parse_single(str)
  letter = str[0].to_sym
  number = str.length > 1 ? str[1..(str.length - 1)] : nil
  Command.new(letter, number)
end

.plunge(depth) ⇒ Object



127
128
129
130
131
# File 'lib/svgcode/gcode/command.rb', line 127

def self.plunge(depth)
  Command.new(:g, 1, [
    Command.new(:z, depth)
  ])
end

.relativeObject



72
73
74
# File 'lib/svgcode/gcode/command.rb', line 72

def self.relative
  Command.new(:g, 91)
end

.stopObject



88
89
90
# File 'lib/svgcode/gcode/command.rb', line 88

def self.stop
  Command.new(:m, 2)
end

Instance Method Details

#==(other) ⇒ Object



34
35
36
37
38
39
# File 'lib/svgcode/gcode/command.rb', line 34

def ==(other)
  other.is_a?(self.class) &&
    other.letter == @letter &&
    other.number.eql?(@number) &&
    other.args == @args
end

#roughly_equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/svgcode/gcode/command.rb', line 41

def roughly_equal?(other)
  return false unless @letter == other.letter
  return false unless @args.length == other.args.length
  return @number == other.number unless @number.nil? || other.number.nil?

  result = true
  @args.each_with_index do |arg, i|
    unless arg.roughly_equal?(other.args[i])
      result = false
      break
    end
  end

  result
end

#to_sObject



27
28
29
30
31
32
# File 'lib/svgcode/gcode/command.rb', line 27

def to_s
  num_fmt = INT_LETTERS.include?(@letter) ? INT_FORMAT : FLOAT_FORMAT
  str = "#{@letter}#{num_fmt % @number}"
  str += " #{@args.join(' ')}" unless @args.nil? || @args.empty?
  str
end