Class: Svgcode::GCode::Command

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

Constant Summary collapse

INT_LETTERS =
[ 'M', 'G' ]
INT_ROUND_TO =
0
INT_FORMAT =
"%02d"
FLOAT_ROUND_TO =
3
FLOAT_FORMAT =
"%.#{FLOAT_ROUND_TO}f"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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



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

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.



11
12
13
# File 'lib/svgcode/gcode/command.rb', line 11

def args
  @args
end

#letterObject (readonly)

Returns the value of attribute letter.



11
12
13
# File 'lib/svgcode/gcode/command.rb', line 11

def letter
  @letter
end

#numberObject (readonly)

Returns the value of attribute number.



11
12
13
# File 'lib/svgcode/gcode/command.rb', line 11

def number
  @number
end

Class Method Details

.absoluteObject



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

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

.clear(clearance) ⇒ Object



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

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

.comment(str) ⇒ Object



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

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

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



115
116
117
118
119
120
121
122
123
124
# File 'lib/svgcode/gcode/command.rb', line 115

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



108
109
110
111
112
113
# File 'lib/svgcode/gcode/command.rb', line 108

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

.feedrate(rate) ⇒ Object



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

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

.g(number, args = nil) ⇒ Object



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

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

.go(x, y) ⇒ Object



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

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

.homeObject



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

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

.imperialObject



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

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

.m(number, args = nil) ⇒ Object



142
143
144
# File 'lib/svgcode/gcode/command.rb', line 142

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

.metricObject



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

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

.parse_single(str) ⇒ Object



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

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



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

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

.relativeObject



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

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

.stopObject



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

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

Instance Method Details

#==(other) ⇒ Object



39
40
41
42
43
44
# File 'lib/svgcode/gcode/command.rb', line 39

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

#roughly_equal?(other) ⇒ Boolean



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/svgcode/gcode/command.rb', line 46

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



30
31
32
33
34
35
36
37
# File 'lib/svgcode/gcode/command.rb', line 30

def to_s
  num_fmt = INT_LETTERS.include?(@letter) ? INT_FORMAT : FLOAT_FORMAT
  round_to = num_fmt == INT_FORMAT ? INT_ROUND_TO : FLOAT_ROUND_TO

  str = "#{@letter}#{num_fmt % @number.round(round_to)}"
  str += " #{@args.join(' ')}" unless @args.nil? || @args.empty?
  str
end