Class: Svgcode::GCode::Converter

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

Constant Summary collapse

PX_PER_INCH =
300
PX_PER_MM =
PX_PER_INCH / 25.4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Converter

Returns a new instance of Converter.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
# File 'lib/svgcode/gcode/converter.rb', line 13

def initialize(opts)
  raise ArgumentError.new if opts.nil? || opts[:max_y].nil?
  @finished = false
  @transforms = []
  @max_y = opts.delete(:max_y)
  @program = Program.new(opts)
  @metric = opts[:metric] != false
  @metric ? @program.metric! : @program.imperial!
  @program.feedrate!
end

Instance Attribute Details

#finishedObject (readonly)

Returns the value of attribute finished.



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

def finished
  @finished
end

#max_yObject (readonly)

Returns the value of attribute max_y.



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

def max_y
  @max_y
end

#metricObject (readonly)

Returns the value of attribute metric.



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

def metric
  @metric
end

#programObject (readonly)

Returns the value of attribute program.



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

def program
  @program
end

#transformsObject

Returns the value of attribute transforms.



10
11
12
# File 'lib/svgcode/gcode/converter.rb', line 10

def transforms
  @transforms
end

Instance Method Details

#<<(svg_d) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/svgcode/gcode/converter.rb', line 24

def <<(svg_d)
  svg_start = nil
  path = SVG::Path.new(svg_d)
  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
  end
end

#comment!(str) ⇒ Object



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

def comment!(str)
  @program.comment!(str)
end

#finishObject



68
69
70
71
72
73
74
75
# File 'lib/svgcode/gcode/converter.rb', line 68

def finish
  unless @finished
    @program.clear!
    @program.go!(0, 0)
    @program.stop!
    @finished = true
  end
end

#metric?Boolean



60
61
62
# File 'lib/svgcode/gcode/converter.rb', line 60

def metric?
  @metric
end

#to_sObject



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

def to_s
  @program.to_s
end