Class: Gerber::Layer::Parser
- Inherits:
-
Object
- Object
- Gerber::Layer::Parser
- Defined in:
- lib/gerber/layer/parser.rb
Constant Summary collapse
- DCodeError =
Class.new(ParseError)
Instance Attribute Summary collapse
-
#coordinate_mode ⇒ Object
readonly
Returns the value of attribute coordinate_mode.
-
#current_aperture ⇒ Object
Returns the value of attribute current_aperture.
-
#layer ⇒ Object
readonly
Returns the value of attribute layer.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#quadrant_mode ⇒ Object
readonly
Returns the value of attribute quadrant_mode.
Accessors collapse
-
#name ⇒ String
The name of the current Gerber::Layer.
-
#name=(name) ⇒ Object
Set the name of the current Gerber::Layer.
-
#polarity ⇒ Symbol
The polarity setting of the current Gerber::Layer (:dark or :clear).
-
#polarity=(polarity) ⇒ Object
Set the polarity of the current Gerber::Layer.
- #set_inches ⇒ Object
- #set_millimeters ⇒ Object
Instance Method Summary collapse
- #<<(arg) ⇒ Object
- #apply_units(a) ⇒ Object
-
#initialize(*args) ⇒ Parser
constructor
A new instance of Parser.
- #is_valid_geometry(arg) ⇒ Object
- #parse_dcode(s) ⇒ Object
- #parse_g1(x, y, dcode) ⇒ Object
- #parse_g2(x, y, i, j, dcode) ⇒ Object
- #parse_g3(x, y, i, j, dcode) ⇒ Object
- #parse_gcode(gcode, x = nil, y = nil, i = nil, j = nil, dcode = nil) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Parser
Returns a new instance of Parser.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gerber/layer/parser.rb', line 14 def initialize(*args) super @coordinate_mode = :absolute @dcode = 2 # off @gcode = 1 # linear interpolation @layer = Gerber::Layer.new @position = Point[0,0] @quadrant_mode = :single @repeat = Vector[1,1] @step = Vector[0,0] @units = nil end |
Instance Attribute Details
#coordinate_mode ⇒ Object (readonly)
Returns the value of attribute coordinate_mode.
9 10 11 |
# File 'lib/gerber/layer/parser.rb', line 9 def coordinate_mode @coordinate_mode end |
#current_aperture ⇒ Object
Returns the value of attribute current_aperture.
8 9 10 |
# File 'lib/gerber/layer/parser.rb', line 8 def current_aperture @current_aperture end |
#layer ⇒ Object (readonly)
Returns the value of attribute layer.
10 11 12 |
# File 'lib/gerber/layer/parser.rb', line 10 def layer @layer end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
11 12 13 |
# File 'lib/gerber/layer/parser.rb', line 11 def position @position end |
#quadrant_mode ⇒ Object (readonly)
Returns the value of attribute quadrant_mode.
12 13 14 |
# File 'lib/gerber/layer/parser.rb', line 12 def quadrant_mode @quadrant_mode end |
Instance Method Details
#<<(arg) ⇒ Object
32 33 34 35 |
# File 'lib/gerber/layer/parser.rb', line 32 def <<(arg) raise ParseError, "Must set an aperture before generating geometry" unless self.current_aperture self.layer.geometry[current_aperture] << arg if is_valid_geometry(arg) end |
#apply_units(a) ⇒ Object
200 201 202 203 204 |
# File 'lib/gerber/layer/parser.rb', line 200 def apply_units(a) raise ParseError, "Units must be set before specifying coordinates" unless @units return nil unless a (@units == 'inch') ? a.inch : a.mm end |
#is_valid_geometry(arg) ⇒ Object
28 29 30 |
# File 'lib/gerber/layer/parser.rb', line 28 def is_valid_geometry(arg) arg.kind_of?(Line) || arg.kind_of?(Point) || arg.kind_of?(Arc) end |
#name ⇒ String
Returns The name of the current Gerber::Layer.
44 45 46 |
# File 'lib/gerber/layer/parser.rb', line 44 def name self.layer.name end |
#name=(name) ⇒ Object
Set the name of the current Gerber::Layer
50 51 52 |
# File 'lib/gerber/layer/parser.rb', line 50 def name=(name) self.layer.name = name end |
#parse_dcode(s) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/gerber/layer/parser.rb', line 74 def parse_dcode(s) /D(\d{2,3})/ =~ s dcode = $1.to_i case dcode when 1, 2, 3 @dcode = dcode when 10...999 self.current_aperture = dcode else raise ParseError, "Invalid D Code #{dcode}" end end |
#parse_g1(x, y, dcode) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/gerber/layer/parser.rb', line 128 def parse_g1(x, y, dcode) point = Point[apply_units(x) || @position.x, apply_units(y) || @position.y] case dcode when 1 line = Geometry::Line[@position, point] self << line @position = point when 2 @position = point when 3 self << point @position = point else raise DCodeError, "Invalid D parameter (#{dcode}) in G1" end end |
#parse_g2(x, y, i, j, dcode) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/gerber/layer/parser.rb', line 145 def parse_g2(x, y, i, j, dcode) raise DCodeError, "In G2 dcode must be either 1 or 2" unless [1, 2].include? dcode if 1 == dcode x, y, i, j = [x, y, i, j].map {|a| apply_units(a)} startPoint = if self.quadrant_mode == :single # start and end are swapped in clockwise mode (Geometry::Arc defaults to counterclockwise) # i and j should have the same signs as the x and y components of the vector from the startpoint to the endpoint if self.coordinate_mode == :absolute delta = Point[x, y] - @position i = i * (delta.x<=>0) j = j * (delta.y<=>0) Point[x, y] elsif self.coordinate_mode == :incremental i = i * (x<=>0) j = j * (y<=>0) @position + Point[x, y] end elsif @quadrant_mode == :multi Point[x, y] else raise ParseError, "Unrecognized quadrant mode: #{self.quadrant_mode}" end arc = Geometry::Arc.new(@position + Point[i, j], startPoint, @position) self << arc @position = arc.first end end |
#parse_g3(x, y, i, j, dcode) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/gerber/layer/parser.rb', line 173 def parse_g3(x, y, i, j, dcode) raise DCodeError, "In G3 dcode must be either 1 or 2" unless [1, 2].include? dcode if 1 == dcode x, y, i, j = [x, y, i, j].map {|a| apply_units(a)} endPoint = if self.quadrant_mode == :single # i and j should have the same signs as the x and y components of the vector from the startpoint to the endpoint if self.coordinate_mode == :absolute delta = Point[x, y] - @position i = i * (delta.x<=>0) j = j * (delta.y<=>0) Point[x, y] elsif self.coordinate_mode == :incremental i = i * (x<=>0) j = j * (y<=>0) @position + Point[x, y] end elsif @quadrant_mode == :multi Point[x, y] else raise ParseError, "Unrecognized quadrant mode: #{self.quadrant_mode}" end arc = Geometry::Arc.new(@position + Point[i, j], @position, endPoint) self << arc @position = arc.last end end |
#parse_gcode(gcode, x = nil, y = nil, i = nil, j = nil, dcode = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/gerber/layer/parser.rb', line 87 def parse_gcode(gcode, x=nil, y=nil, i=nil, j=nil, dcode=nil) gcode = gcode ? gcode.to_i : @gcode dcode = dcode ? dcode.to_i : @dcode case gcode when 1, 55 # G55 is deprecated, but behaves like G01 parse_g1(x, y, dcode) @dcode = dcode @gcode = gcode when 2 parse_g2(x, y, i, j, dcode) @dcode = dcode @gcode = gcode when 3 parse_g3(x, y, i, j, dcode) @dcode = dcode @gcode = gcode when 4 # G04 is used for single-line comments. Ignore the block and carry on. when 36 p "enable outline fill" when 37 p "disable outline fill" when 54 raise DCodeError, "G54 requires a D code (found #{x}, #{y}, #{dcode})" unless dcode self.current_aperture = dcode.to_i when 70 set_inches when 71 set_millimeters when 74 @quadrant_mode = :single when 75 @quadrant_mode = :multi when 90 @coordinate_mode = :absolute when 91 @coordinate_mode = :incremental else raise ParseError, "Unrecognized GCode #{gcode}" end end |
#polarity ⇒ Symbol
Returns The polarity setting of the current Gerber::Layer (:dark or :clear).
55 56 57 |
# File 'lib/gerber/layer/parser.rb', line 55 def polarity self.layer.polarity end |
#polarity=(polarity) ⇒ Object
Set the polarity of the current Gerber::Layer
61 62 63 |
# File 'lib/gerber/layer/parser.rb', line 61 def polarity=(polarity) self.layer.polarity = polarity end |
#set_inches ⇒ Object
65 66 67 |
# File 'lib/gerber/layer/parser.rb', line 65 def set_inches @units = 'inch' end |
#set_millimeters ⇒ Object
69 70 71 |
# File 'lib/gerber/layer/parser.rb', line 69 def set_millimeters @units = 'millimeters' end |