Class: Laser::Cutter::Box

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

Overview

A box of six panels: front, back, top (the lid), bottom, left and right. It lays them out on the page and notches every side so that neighbouring panels interlock. BoxFactory builds it unless the settings ask for a slope.

Lengths are unitless, in the units of the settings.

Constant Summary collapse

LIDS =

How the lid, the top panel, joins the walls: notched on all four sides, on the side of the back wall only, or on none.

%i[full back plain].freeze
LID_SIDES =

The side of each wall that meets the lid, as an index into Rect#sides.

{ 'front' => 0, 'left' => 0, 'right' => 0, 'back' => 2 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Box

Returns a new instance of Box.

Parameters:

  • config (Configuration, Hash) (defaults to: {}) —

    width, height, depth and thickness, and optionally notch, kerf, padding, units, inside_box, lid, metadata_width and metadata_height



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/laser/cutter/box.rb', line 70

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 * longest / 5.0)
  self.kerf = config['kerf'] || 0.0
  self.padding = config['padding']
  self.units = config['units']
  self.inside_box = config['inside_box']
  self.lid = (config['lid'] || LIDS.first).to_sym

  self.notches = []
  self.outlines = {}

  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
    },
  }
end

Instance Attribute Details

#back ⇒ Geometry::Rect

Returns the back panel.

Returns:



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#bottom ⇒ Geometry::Rect

Returns the bottom panel.

Returns:



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#bounds ⇒ Object

Returns unused.

Returns:

  • (Object) —

    unused



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#conf ⇒ Hash

Returns for each face, whether the center notch of its horizontal (:halign) and vertical (:valign) sides is a tab, and which faces carry the corner squares (:corners).

Returns:

  • (Hash) —

    for each face, whether the center notch of its horizontal (:halign) and vertical (:valign) sides is a tab, and which faces carry the corner squares (:corners)



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#corner_face ⇒ Symbol?

Returns unused: #generate_notches picks it each time.

Returns:



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#dim ⇒ Geometry::Dimensions

Returns the inside width, height and depth.

Returns:



59
60
61
# File 'lib/laser/cutter/box.rb', line 59

def dim
  @dim
end

#faces ⇒ Array<Geometry::Rect>

Returns the six panels, in the order #conf indexes them.

Returns:



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#front ⇒ Geometry::Rect

Returns the front panel.

Returns:



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#inside_box ⇒ Object?

Returns read from the settings, and not used in drawing.

Returns:

  • (Object, nil) —

    read from the settings, and not used in drawing



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#kerf ⇒ Float

Returns the width the laser burns away.

Returns:

  • (Float) —

    the width the laser burns away



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#left ⇒ Geometry::Rect

Returns the left panel.

Returns:



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#lid ⇒ Symbol

Returns one of LIDS.

Returns:

  • (Symbol) —

    one of LIDS



62
63
64
# File 'lib/laser/cutter/box.rb', line 62

def lid
  @lid
end

#metadata ⇒ Geometry::Point

Returns the width and height kept free for the settings.

Returns:



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#notch_width ⇒ Float

Returns the notch width asked for; each side fits its own.

Returns:

  • (Float) —

    the notch width asked for; each side fits its own



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#notches ⇒ Array<Geometry::Line>

Returns every line to cut, once generated.

Returns:



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#outlines ⇒ Hash{String => Array<Geometry::Line>}

Returns the lines to cut for each face, by its name.

Returns:

  • (Hash{String => Array<Geometry::Line>}) —

    the lines to cut for each face, by its name



65
66
67
# File 'lib/laser/cutter/box.rb', line 65

def outlines
  @outlines
end

#padding ⇒ Float

Returns the space between panels on the page.

Returns:

  • (Float) —

    the space between panels on the page



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#right ⇒ Geometry::Rect

Returns the right panel.

Returns:



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#thickness ⇒ Float

Returns the thickness of the material.

Returns:

  • (Float) —

    the thickness of the material



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#top ⇒ Geometry::Rect

Returns the top panel, the lid.

Returns:



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

#units ⇒ String, Symbol

Returns the units of every length.

Returns:

  • (String, Symbol) —

    the units of every length



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

attr_accessor :dim, :thickness, :notch_width, :kerf, :padding, :units, :inside_box, :front, :back, :top, :bottom, :left, :right, :faces, :bounds, :conf, :corner_face, :metadata, :notches

Instance Method Details

#d ⇒ Float

Returns the depth.

Returns:

  • (Float) —

    the depth



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

def d; dim.d; end

#enclosure ⇒ Geometry::Rect

Returns the smallest rectangle around every line, generating the notches first if needed.

Returns:

  • (Geometry::Rect) —

    the smallest rectangle around every line, generating the notches first if needed



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/laser/cutter/box.rb', line 101

def enclosure
  generate_notches if 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 ⇒ Array<Geometry::Line>

Lays out the panels and notches them, filling #outlines and #notches.

Returns:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/laser/cutter/box.rb', line 119

def generate_notches
  position_faces!
  corner_face = pick_corners_face
  self.notches = []
  self.outlines = {}
  faces.each_with_index do |face, face_index|
    edges = edges_of(face, face_index, corner_face)

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

    outlines[face.name] = Aggregator.new(lines_of(face, edges)).lines
    notches << outlines[face.name]
  end
  notches.flatten!
end

#h ⇒ Float

Returns the height.

Returns:

  • (Float) —

    the height



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

def h; dim.h; end

#longest ⇒ Float

Returns the longest dimension.

Returns:

  • (Float) —

    the longest dimension



147
148
149
# File 'lib/laser/cutter/box.rb', line 147

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

#to_s ⇒ String

Returns the dimensions, thickness and notch width.

Returns:

  • (String) —

    the dimensions, thickness and notch width



152
153
154
# File 'lib/laser/cutter/box.rb', line 152

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

#w ⇒ Float

Returns the width.

Returns:

  • (Float) —

    the width



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

def w; dim.w; end