Class: Laser::Cutter::Box

Inherits:
Object
  • Object
show all
Defined in:
lib/laser-cutter/box.rb

Overview

Note: this class badly needs refactoring and tests. Both are coming.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Box

Returns a new instance of Box.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/laser-cutter/box.rb', line 15

def initialize(config = {})
  self.dim = Geometry::Dimensions.new(config['width'], config['height'], config['depth'])
  self.thickness = config['thickness']

  self.notch_width = config['notch'] || (1.0 * self.longest / 5.0)
  self.kerf = config['kerf'] || 0.0
  self.padding = config['padding']
  self.units = config['units']
  self.inside_box = config['inside_box']

  self.notches = []

  self. = Geometry::Point[config['metadata_width'] || 0, config['metadata_height'] || 0]

  create_faces! # generates dimensions for each side
  self.faces =     [top, front, bottom, back, left, right]

  self.conf   = {
      valign: [    :out, :out,  :out,   :out, :in, :in],
      halign: [    :in,  :out,  :in,    :out, :in, :in],
      corners: {
          front: [ :no,  :yes,  :no,    :yes, :no, :no], # our default choice, but may not work
          top:   [ :yes, :no,   :yes,   :no,  :no, :no]  # 2nd choice, has to work if 1st doesn't
      },
  }
  self
end

Instance Attribute Details

#back ⇒ Object

Returns the value of attribute back.



11
12
13
# File 'lib/laser-cutter/box.rb', line 11

def back
  @back
end

#bottom ⇒ Object

Returns the value of attribute bottom.



11
12
13
# File 'lib/laser-cutter/box.rb', line 11

def bottom
  @bottom
end

#bounds ⇒ Object

Returns the value of attribute bounds.



12
13
14
# File 'lib/laser-cutter/box.rb', line 12

def bounds
  @bounds
end

#conf ⇒ Object

Returns the value of attribute conf.



12
13
14
# File 'lib/laser-cutter/box.rb', line 12

def conf
  @conf
end

#corner_face ⇒ Object

Returns the value of attribute corner_face.



12
13
14
# File 'lib/laser-cutter/box.rb', line 12

def corner_face
  @corner_face
end

#dim ⇒ Object

Everything is in millimeters



8
9
10
# File 'lib/laser-cutter/box.rb', line 8

def dim
  @dim
end

#faces ⇒ Object

Returns the value of attribute faces.



12
13
14
# File 'lib/laser-cutter/box.rb', line 12

def faces
  @faces
end

#front ⇒ Object

Returns the value of attribute front.



11
12
13
# File 'lib/laser-cutter/box.rb', line 11

def front
  @front
end

#inside_box ⇒ Object

Returns the value of attribute inside_box.



9
10
11
# File 'lib/laser-cutter/box.rb', line 9

def inside_box
  @inside_box
end

#kerf ⇒ Object

Everything is in millimeters



8
9
10
# File 'lib/laser-cutter/box.rb', line 8

def kerf
  @kerf
end

#left ⇒ Object

Returns the value of attribute left.



11
12
13
# File 'lib/laser-cutter/box.rb', line 11

def left
  @left
end

#metadata ⇒ Object

Returns the value of attribute metadata.



13
14
15
# File 'lib/laser-cutter/box.rb', line 13

def 
  @metadata
end

#notch_width ⇒ Object

Everything is in millimeters



8
9
10
# File 'lib/laser-cutter/box.rb', line 8

def notch_width
  @notch_width
end

#notches ⇒ Object

Returns the value of attribute notches.



13
14
15
# File 'lib/laser-cutter/box.rb', line 13

def notches
  @notches
end

#padding ⇒ Object

Returns the value of attribute padding.



9
10
11
# File 'lib/laser-cutter/box.rb', line 9

def padding
  @padding
end

#right ⇒ Object

Returns the value of attribute right.



11
12
13
# File 'lib/laser-cutter/box.rb', line 11

def right
  @right
end

#thickness ⇒ Object

Everything is in millimeters



8
9
10
# File 'lib/laser-cutter/box.rb', line 8

def thickness
  @thickness
end

#top ⇒ Object

Returns the value of attribute top.



11
12
13
# File 'lib/laser-cutter/box.rb', line 11

def top
  @top
end

#units ⇒ Object

Returns the value of attribute units.



9
10
11
# File 'lib/laser-cutter/box.rb', line 9

def units
  @units
end

Instance Method Details

#d ⇒ Object



95
# File 'lib/laser-cutter/box.rb', line 95

def d; dim.d; end

#enclosure ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/laser-cutter/box.rb', line 43

def enclosure
  generate_notches if self.notches.empty?
  p1 = notches.first.p1.to_a
  p2 = notches.first.p2.to_a

  notches.each do |notch|
    n = notch.normalized
    n.p1.to_a.each_with_index {|c, i| p1[i] = c if c < p1[i] }
    n.p2.to_a.each_with_index {|c, i| p2[i] = c if c > p2[i] }
  end

  Geometry::Rect[Geometry::Point.new(p1), Geometry::Point.new(p2)]
end

#generate_notches ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/laser-cutter/box.rb', line 57

def generate_notches
  position_faces!
  corner_face = pick_corners_face
  self.notches = []
  faces.each_with_index do |face, face_index|
    bound = face_bounding_rect(face)
    side_lines = []
    edges = []
    bound.sides.each_with_index do |bounding_side, side_index |
      include_corners = (self.conf[:corners][corner_face][face_index] == :yes && side_index.odd?)
      key = side_index.odd? ? :valign : :halign
      center_out = (self.conf[key][face_index] == :out)
      edges << Notching::Edge.new(bounding_side, face.sides[side_index],
                      {:notch_width => notch_width,
                       :thickness => thickness,
                       :kerf => kerf,
                       :center_out => center_out,
                       :corners => include_corners
                      })
    end

    if edges.any?{|e| e.corners} && !edges.all?{|e| e.first_notch_out? }
      edges.each {|e| e.adjust_corners = true }
    end

    edges.each do |edge|
      side_lines << Notching::PathGenerator.new(edge).generate
    end

    aggregator = Aggregator.new(side_lines.flatten)
    aggregator.dedup!.deoverlap!.dedup!
    self.notches << aggregator.lines
  end
  self.notches.flatten!
end

#h ⇒ Object



94
# File 'lib/laser-cutter/box.rb', line 94

def h; dim.h; end

#longest ⇒ Object



97
98
99
# File 'lib/laser-cutter/box.rb', line 97

def longest
  [w, h, d].max()
end

#to_s ⇒ Object



101
102
103
# File 'lib/laser-cutter/box.rb', line 101

def to_s
  "Box:\nH:#{dim.h} W:#{dim.w} D:#{dim.d}\nThickness:#{thickness}, Notch:#{notch_width}"
end

#w ⇒ Object



93
# File 'lib/laser-cutter/box.rb', line 93

def w; dim.w; end