lib/svgcode/gcode/converter.rb in svgcode-0.4.0 vs lib/svgcode/gcode/converter.rb in svgcode-0.6.0
- old
+ new
@@ -1,6 +1,7 @@
require 'svgcode/gcode/program'
+require 'svgcode/svg/circle'
require 'svgcode/svg/path'
module Svgcode
module GCode
class Converter
@@ -19,43 +20,19 @@
@metric = opts[:metric] != false
@metric ? @program.metric! : @program.imperial!
@program.feedrate!
end
- def <<(svg_d)
- svg_start = nil
- path = SVG::Path.new(svg_d)
- start = nil
+ def <<(str_or_command)
+ @start = nil
- path.commands.each do |cmd|
- cmd.apply_transforms!(@transforms)
- cmd.absolute? ? cmd.flip_points_y!(@max_y) : cmd.negate_points_y!
-
- if metric?
- cmd.divide_points_by!(PX_PER_MM)
- else
- cmd.divide_points_by!(PX_PER_INCH)
- end
-
- if (cmd.name == :close || cmd.absolute?) && @program.relative?
- @program.absolute!
- elsif cmd.relative? && @program.absolute?
- @program.relative!
- end
-
- case cmd.name
- when :move
- start = cmd.relative? ? cmd.absolute(@program.pos) : cmd
- @program.go!(cmd.points.first.x, cmd.points.first.y)
- when :line
- @program.cut!(cmd.points.first.x, cmd.points.first.y)
- when :cubic
- cubic!(cmd)
- when :close
- @program.cut!(start.points.first.x, start.points.first.y)
- start = nil
- end
+ if str_or_command.is_a?(String)
+ path = SVG::Path.new(str_or_command)
+ path.commands.each { |cmd| add_command(cmd)}
+ else
+ cmd = SVG::Circle.new(str_or_command)
+ add_command(cmd)
end
end
def metric?
@metric
@@ -77,9 +54,42 @@
def to_s
@program.to_s
end
private
+
+ def add_command(cmd)
+ cmd.apply_transforms!(@transforms)
+ cmd.absolute? ? cmd.flip_points_y!(@max_y) : cmd.negate_points_y!
+
+ if metric?
+ cmd.divide_points_by!(PX_PER_MM)
+ else
+ cmd.divide_points_by!(PX_PER_INCH)
+ end
+
+ if cmd.name == :close || cmd.absolute?
+ @program.absolute!
+ elsif cmd.relative?
+ @program.relative!
+ end
+
+ case cmd.name
+ when :move
+ @start = cmd.relative? ? cmd.absolute(@program.pos) : cmd
+ @program.go!(cmd.points.first.x, cmd.points.first.y)
+ when :line
+ @program.cut!(cmd.points.first.x, cmd.points.first.y)
+ when :cubic
+ cubic!(cmd)
+ when :circle
+ @program.go!(cmd.start_x, cmd.centre_y)
+ @program.arc!(cmd.start_x, cmd.centre_y, cmd.radius)
+ when :close
+ @program.cut!(@start.points.first.x, @start.points.first.y)
+ @start = nil
+ end
+ end
def cubic!(cmd)
# A relative SVG cubic bezier has all control points relative to start.
# G-code I&J are relative to start, and P&Q relative to end.
# I, J, P & Q are always relative, but SVG values can be absolute.