Class: BoxHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/convert_svg_string_to_gcode/helpers/box_helper.rb

Class Method Summary collapse

Class Method Details

.command_is_within_box?(command, box) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/convert_svg_string_to_gcode/helpers/box_helper.rb', line 36

def self.command_is_within_box?(command, box)
  command.first.p0x.between?(box.x_min, box.x_max) &&
    command.first.p0y.between?(box.y_min, box.y_max)
end

.sort_commands_into_boxes(svg_commands, rows: 10, columns: 3) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/convert_svg_string_to_gcode/helpers/box_helper.rb', line 6

def self.sort_commands_into_boxes(svg_commands, rows: 10, columns: 3)

  boxes = create_write_boxes(svg_commands, rows, columns)
  boxed_commands = boxes.map { |bc| [] }

  box_order = []

  boxes.each_slice(rows).with_index do |box_group, i|
    if i.even?
      box_order << box_group.map { |b| b.box_number }
    else
      box_order << box_group.reverse_each.map { |b| b.box_number }
    end
  end

  box_order_index = {}

  box_order.flatten.each.with_index do |order_number, i|
    box_order_index[order_number] = i
  end

  svg_commands.each do |command|
    box_order.flatten.each do |box_number|
      next unless command_is_within_box?(command, boxes[box_number])
      boxed_commands[box_order_index[box_number]] << command
    end
  end
  boxed_commands
end