Class: Svgcode::GCode::Program

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

Constant Summary collapse

DEFAULT_FEEDRATE =
120
DEFAULT_CLEARANCE =
5
DEFAULT_DEPTH =
-0.075

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Program

Returns a new instance of Program.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/svgcode/gcode/program.rb', line 20

def initialize(opts = {})
  @is_plunged = false
  @is_poised = false
  @commands = opts.delete(:commands) || []
  @is_absolute = opts.delete(:absolute)
  @is_absolute = true if @is_absolute.nil?

  @opts = {
    feedrate: DEFAULT_FEEDRATE,
    clearance: DEFAULT_CLEARANCE,
    depth: DEFAULT_DEPTH
  }.merge(opts)
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



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

def commands
  @commands
end

#is_absoluteObject (readonly)

3 states

at Z home height: @is_poised = false, @is_plunged = false at Z clearance height: @is_poised = true, @is_plunged = false at Z cutting depth height: @is_poised = true, @is_plunged = true



18
19
20
# File 'lib/svgcode/gcode/program.rb', line 18

def is_absolute
  @is_absolute
end

#is_plungedObject (readonly)

3 states

at Z home height: @is_poised = false, @is_plunged = false at Z clearance height: @is_poised = true, @is_plunged = false at Z cutting depth height: @is_poised = true, @is_plunged = true



18
19
20
# File 'lib/svgcode/gcode/program.rb', line 18

def is_plunged
  @is_plunged
end

#is_poisedObject (readonly)

3 states

at Z home height: @is_poised = false, @is_plunged = false at Z clearance height: @is_poised = true, @is_plunged = false at Z cutting depth height: @is_poised = true, @is_plunged = true



18
19
20
# File 'lib/svgcode/gcode/program.rb', line 18

def is_poised
  @is_poised
end

#optsObject

Returns the value of attribute opts.



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

def opts
  @opts
end

#xObject (readonly)

3 states

at Z home height: @is_poised = false, @is_plunged = false at Z clearance height: @is_poised = true, @is_plunged = false at Z cutting depth height: @is_poised = true, @is_plunged = true



18
19
20
# File 'lib/svgcode/gcode/program.rb', line 18

def x
  @x
end

#yObject (readonly)

3 states

at Z home height: @is_poised = false, @is_plunged = false at Z clearance height: @is_poised = true, @is_plunged = false at Z cutting depth height: @is_poised = true, @is_plunged = true



18
19
20
# File 'lib/svgcode/gcode/program.rb', line 18

def y
  @y
end

Instance Method Details

#<<(command) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/svgcode/gcode/program.rb', line 62

def <<(command)
  if !command.is_a?(String) &&
    (@x.nil? || @y.nil?) && 
    command.letter == 'G' &&
    command.number < 6 &&
    command != Command.relative &&
    command != Command.absolute
  then
    if relative?
      raise InvalidCommandError.new(
        'Cannot add a command when relative and @x or @y are nil'
      )
    else
      @commands << Command.absolute
    end
  end

  @commands << command
end

#absolute!Object



94
95
96
97
98
99
# File 'lib/svgcode/gcode/program.rb', line 94

def absolute!
  unless absolute?
    self << Command.absolute
    @is_absolute = true
  end
end

#absolute?Boolean

Returns:

  • (Boolean)


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

def absolute?
  @is_absolute
end

#clear!Object



131
132
133
134
135
# File 'lib/svgcode/gcode/program.rb', line 131

def clear!
  temp_absolute { self << Command.clear(clearance) }
  @is_plunged = false
  @is_poised = true
end

#clearanceObject



38
39
40
# File 'lib/svgcode/gcode/program.rb', line 38

def clearance
  @opts[:clearance]
end

#comment!(str) ⇒ Object



82
83
84
# File 'lib/svgcode/gcode/program.rb', line 82

def comment!(str)
  self << Command.comment(str)
end

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



153
154
155
156
157
# File 'lib/svgcode/gcode/program.rb', line 153

def cubic_spline!(i, j, _p, q, x, y)
  perform_cut(x, y) do
    self << Command.cubic_spline(i, j, _p, q, x, y)
  end
end

#cut!(x, y) ⇒ Object



149
150
151
# File 'lib/svgcode/gcode/program.rb', line 149

def cut!(x, y)
  perform_cut(x, y) { self << Command.cut(x, y) }
end

#depthObject



42
43
44
# File 'lib/svgcode/gcode/program.rb', line 42

def depth
  @opts[:depth]
end

#feedrateObject



34
35
36
# File 'lib/svgcode/gcode/program.rb', line 34

def feedrate
  @opts[:feedrate]
end

#feedrate!(rate = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/svgcode/gcode/program.rb', line 108

def feedrate!(rate = nil)
  if rate.nil?
    rate = feedrate
  else
    @opts[:feedrate] = rate
  end

  self << Command.feedrate(rate)
end

#go!(x, y) ⇒ Object



143
144
145
146
147
# File 'lib/svgcode/gcode/program.rb', line 143

def go!(x, y)
  clear! if plunged?
  self << Command.go(x, y)
  set_coords(x, y)
end

#home!Object



122
123
124
125
126
127
128
129
# File 'lib/svgcode/gcode/program.rb', line 122

def home!
  clear! if plunged?
  @commands.pop if @commands.last == Command.relative
  self << Command.home if poised?
  @x = nil
  @y = nil
  @is_poised = false
end

#imperial!Object



90
91
92
# File 'lib/svgcode/gcode/program.rb', line 90

def imperial!
  self << Command.imperial
end

#metric!Object



86
87
88
# File 'lib/svgcode/gcode/program.rb', line 86

def metric!
  self << Command.metric
end

#plunge!Object



137
138
139
140
141
# File 'lib/svgcode/gcode/program.rb', line 137

def plunge!
  clear! unless poised?
  temp_absolute { self << Command.plunge(depth) }
  @is_plunged = true
end

#plunged?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/svgcode/gcode/program.rb', line 46

def plunged?
  @is_plunged
end

#poised?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/svgcode/gcode/program.rb', line 50

def poised?
  @is_poised
end

#posObject



159
160
161
# File 'lib/svgcode/gcode/program.rb', line 159

def pos
  Svgcode::SVG::Point.new(@x, @y)
end

#relative!Object



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

def relative!
  unless relative?
    self << Command.relative
    @is_absolute = false
  end
end

#relative?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/svgcode/gcode/program.rb', line 58

def relative?
  @is_absolute.nil? ? nil : !@is_absolute
end

#stop!Object



118
119
120
# File 'lib/svgcode/gcode/program.rb', line 118

def stop!
  self << Command.stop
end

#to_sObject



163
164
165
# File 'lib/svgcode/gcode/program.rb', line 163

def to_s
  @commands.join("\n")
end