laser-cutter 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/pages.yml +44 -0
  3. data/.gitignore +2 -0
  4. data/.openlanes/config.yml +106 -0
  5. data/.plans/002.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257lid-options/plan.md +45 -0
  6. data/.plans/003.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257ruby-api/plan.md +37 -0
  7. data/.plans/004.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257can-we-create-an-angled-box-where-angle-is-configurable-like-in-spec-wishes-image-shows/spec.md +40 -0
  8. data/.plans/004.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257can-we-create-an-angled-box-where-angle-is-configurable-like-in-spec-wishes-image-shows/state.json +337 -0
  9. data/.plans/004.01/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257sloped-box-panels/plan.md +91 -0
  10. data/.plans/decisions.md +11 -0
  11. data/CHANGELOG.md +134 -0
  12. data/CLAUDE.md +30 -5
  13. data/Gemfile +2 -1
  14. data/README.md +309 -31
  15. data/Rakefile +11 -7
  16. data/docs/badges/coverage_badge.svg +2 -2
  17. data/docs/decisions/decisions-on-angled-box.md +93 -0
  18. data/docs/decisions/stack.md +6 -0
  19. data/docs/images/box-3d.avif +0 -0
  20. data/docs/images/box-lid-back.avif +0 -0
  21. data/docs/images/box-lid-full.avif +0 -0
  22. data/docs/images/box-lid-plain.avif +0 -0
  23. data/docs/images/box-metric.avif +0 -0
  24. data/docs/images/box-sloped-3d.avif +0 -0
  25. data/docs/images/box-sloped.avif +0 -0
  26. data/docs/images/box-tray.avif +0 -0
  27. data/justfile +24 -8
  28. data/laser-cutter.gemspec +2 -1
  29. data/lib/laser/cutter/aggregator.rb +28 -0
  30. data/lib/laser/cutter/box.rb +192 -24
  31. data/lib/laser/cutter/box_factory.rb +15 -0
  32. data/lib/laser/cutter/cli/command.rb +27 -1
  33. data/lib/laser/cutter/cli/config_file.rb +2 -0
  34. data/lib/laser/cutter/cli/examples.rb +8 -0
  35. data/lib/laser/cutter/cli/generate.rb +41 -5
  36. data/lib/laser/cutter/cli/help.rb +4 -0
  37. data/lib/laser/cutter/cli/page_sizes.rb +3 -0
  38. data/lib/laser/cutter/cli/version.rb +2 -0
  39. data/lib/laser/cutter/cli.rb +23 -4
  40. data/lib/laser/cutter/configuration.rb +42 -2
  41. data/lib/laser/cutter/geometry/dimensions.rb +7 -0
  42. data/lib/laser/cutter/geometry/line.rb +42 -0
  43. data/lib/laser/cutter/geometry/loop.rb +138 -0
  44. data/lib/laser/cutter/geometry/point.rb +3 -0
  45. data/lib/laser/cutter/geometry/rect.rb +25 -0
  46. data/lib/laser/cutter/geometry/shape.rb +19 -1
  47. data/lib/laser/cutter/geometry/transform.rb +50 -0
  48. data/lib/laser/cutter/geometry/tuple.rb +47 -11
  49. data/lib/laser/cutter/invalid_option.rb +8 -0
  50. data/lib/laser/cutter/launcher.rb +14 -1
  51. data/lib/laser/cutter/missing_option.rb +2 -0
  52. data/lib/laser/cutter/notching/angled_edge.rb +67 -0
  53. data/lib/laser/cutter/notching/base.rb +8 -0
  54. data/lib/laser/cutter/notching/edge.rb +59 -4
  55. data/lib/laser/cutter/notching/infinite_iterator.rb +11 -1
  56. data/lib/laser/cutter/notching/path_generator.rb +86 -81
  57. data/lib/laser/cutter/notching/shift.rb +4 -0
  58. data/lib/laser/cutter/options.rb +175 -0
  59. data/lib/laser/cutter/page_manager.rb +12 -2
  60. data/lib/laser/cutter/renderer/base.rb +26 -4
  61. data/lib/laser/cutter/renderer/box_renderer.rb +14 -1
  62. data/lib/laser/cutter/renderer/layout_renderer.rb +13 -3
  63. data/lib/laser/cutter/renderer/line_renderer.rb +6 -0
  64. data/lib/laser/cutter/renderer/meta_renderer.rb +33 -9
  65. data/lib/laser/cutter/renderer/rect_renderer.rb +5 -0
  66. data/lib/laser/cutter/renderer/svg_renderer.rb +12 -4
  67. data/lib/laser/cutter/renderer.rb +2 -0
  68. data/lib/laser/cutter/sloped/box.rb +90 -0
  69. data/lib/laser/cutter/sloped/corners.rb +58 -0
  70. data/lib/laser/cutter/sloped/layout.rb +97 -0
  71. data/lib/laser/cutter/sloped/notcher.rb +53 -0
  72. data/lib/laser/cutter/sloped/panels.rb +121 -0
  73. data/lib/laser/cutter/sloped/placement.rb +36 -0
  74. data/lib/laser/cutter/sloped/profile.rb +176 -0
  75. data/lib/laser/cutter/sloped/request.rb +31 -0
  76. data/lib/laser/cutter/sloped/ring_panel.rb +120 -0
  77. data/lib/laser/cutter/sloped/side_panel.rb +89 -0
  78. data/lib/laser/cutter/types.rb +60 -0
  79. data/lib/laser/cutter/units_converter.rb +5 -0
  80. data/lib/laser/cutter/version.rb +2 -1
  81. data/lib/laser/cutter/zero_value_not_allowed.rb +2 -0
  82. data/lib/laser/cutter.rb +40 -0
  83. data/spec/laser/cutter/angled_edge_spec.rb +103 -0
  84. data/spec/laser/cutter/box_factory_spec.rb +19 -0
  85. data/spec/laser/cutter/box_lid_spec.rb +142 -0
  86. data/spec/laser/cutter/cli_spec.rb +114 -7
  87. data/spec/laser/cutter/configuration_spec.rb +54 -0
  88. data/spec/laser/cutter/loop_spec.rb +107 -0
  89. data/spec/laser/cutter/options_spec.rb +162 -0
  90. data/spec/laser/cutter/renderer/layout_renderer_spec.rb +21 -20
  91. data/spec/laser/cutter/renderer/meta_renderer_spec.rb +82 -0
  92. data/spec/laser/cutter/renderer/svg_renderer_spec.rb +34 -0
  93. data/spec/laser/cutter/sloped/assembly_spec.rb +78 -0
  94. data/spec/laser/cutter/sloped/box_spec.rb +151 -0
  95. data/spec/laser/cutter/sloped/corners_spec.rb +45 -0
  96. data/spec/laser/cutter/sloped/layout_spec.rb +77 -0
  97. data/spec/laser/cutter/sloped/notcher_spec.rb +57 -0
  98. data/spec/laser/cutter/sloped/panels_spec.rb +175 -0
  99. data/spec/laser/cutter/sloped/placement_spec.rb +50 -0
  100. data/spec/laser/cutter/sloped/request_spec.rb +48 -0
  101. data/spec/laser/cutter/sloped/ring_panel_spec.rb +102 -0
  102. data/spec/laser/cutter/sloped/side_panel_spec.rb +98 -0
  103. data/spec/laser/cutter/sloped_profile_spec.rb +156 -0
  104. data/spec/laser/cutter/transform_spec.rb +53 -0
  105. data/spec/laser/cutter_spec.rb +114 -0
  106. data/spec/spec_helper.rb +4 -0
  107. data/spec/support/loops.rb +106 -0
  108. data/spec/support/outline.rb +53 -0
  109. data/spec/support/sloped.rb +26 -0
  110. data/spec/support/solid.rb +95 -0
  111. data/spec/wishes/wish-tilted-box.png +0 -0
  112. metadata +81 -7
  113. /data/.plans/{001.00-dry-cli-migration/plan.md → 001.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257dry-cli-migration/plan.md"} +0 -0
@@ -30,6 +30,8 @@ module Laser
30
30
 
31
31
  # Lines running along one axis: 0 for horizontal (constant y), 1 for vertical (constant x).
32
32
  #
33
+ # @param lines [Array<Geometry::Line>]
34
+ # @param axis [Integer] 0 for horizontal, 1 for vertical
33
35
  # @return [Array<Geometry::Line>]
34
36
  def sweep(lines, axis)
35
37
  across = (axis + 1) % 2
@@ -37,16 +39,25 @@ module Laser
37
39
  groups(along, across).flat_map { |offset, group| cut_intervals(group, axis, offset, across) }
38
40
  end
39
41
 
42
+ # @param lines [Array<Geometry::Line>]
43
+ # @return [Array<Geometry::Line>] the lines on neither axis
40
44
  def skewed(lines)
41
45
  lines.reject { |line| along?(line, 0) || along?(line, 1) }
42
46
  end
43
47
 
48
+ # @param line [Geometry::Line]
49
+ # @param axis [Integer] 0 for horizontal, 1 for vertical
50
+ # @return [Boolean] whether the line runs along the axis and has a length
44
51
  def along?(line, axis)
45
52
  across = (axis + 1) % 2
46
53
  (line.p1[across] - line.p2[across]).abs <= TOLERANCE && (line.p1[axis] - line.p2[axis]).abs > TOLERANCE
47
54
  end
48
55
 
49
56
  # Lines on the same row or column, keyed by the offset of the first one seen.
57
+ #
58
+ # @param lines [Array<Geometry::Line>] lines along one axis
59
+ # @param across [Integer] the other axis
60
+ # @return [Hash{Float => Array<Geometry::Line>}]
50
61
  def groups(lines, across)
51
62
  lines.sort_by { |line| line.p1[across] }.slice_when { |a, b| b.p1[across] - a.p1[across] > TOLERANCE }
52
63
  .to_h { |group| [group.first.p1[across], group] }
@@ -54,6 +65,12 @@ module Laser
54
65
 
55
66
  # Sweeps the endpoints of one collinear group, keeping the stretches an
56
67
  # odd number of lines cover.
68
+ #
69
+ # @param group [Array<Geometry::Line>] collinear lines
70
+ # @param axis [Integer] the axis they run along
71
+ # @param offset [Float] their coordinate on the other axis
72
+ # @param across [Integer] the other axis
73
+ # @return [Array<Geometry::Line>]
57
74
  def cut_intervals(group, axis, offset, across)
58
75
  events = group.flat_map do |line|
59
76
  from, to = [line.p1[axis], line.p2[axis]].sort
@@ -77,6 +94,11 @@ module Laser
77
94
  end
78
95
 
79
96
  # Joins an interval onto the last one when they touch, and drops one with no length.
97
+ #
98
+ # @param intervals [Array<Array(Float, Float)>] changed in place
99
+ # @param from [Float]
100
+ # @param to [Float]
101
+ # @return [void]
80
102
  def extend_or_add(intervals, from, to)
81
103
  return if to - from <= TOLERANCE
82
104
 
@@ -88,6 +110,12 @@ module Laser
88
110
  end
89
111
  end
90
112
 
113
+ # @param from [Float] where the line starts along the axis
114
+ # @param to [Float] where it ends
115
+ # @param axis [Integer] the axis it runs along
116
+ # @param offset [Float] its coordinate on the other axis
117
+ # @param across [Integer] the other axis
118
+ # @return [Geometry::Line]
91
119
  def line_between(from, to, axis, offset, across)
92
120
  a = []
93
121
  b = []
@@ -2,13 +2,71 @@
2
2
 
3
3
  module Laser
4
4
  module Cutter
5
- # Note: this class badly needs refactoring and tests. Both are coming.
6
-
5
+ # A box of six panels: front, back, top (the lid), bottom, left and
6
+ # right. It lays them out on the page and notches every side so that
7
+ # neighbouring panels interlock. {BoxFactory} builds it unless the
8
+ # settings ask for a slope.
9
+ #
10
+ # Lengths are unitless, in the units of the settings.
7
11
  class Box
8
- # Everything is in millimeters
9
-
12
+ # How the lid, the top panel, joins the walls: notched on all four sides,
13
+ # on the side of the back wall only, or on none.
14
+ LIDS = %i[full back plain].freeze
15
+
16
+ # The side of each wall that meets the lid, as an index into Rect#sides.
17
+ LID_SIDES = { 'front' => 0, 'left' => 0, 'right' => 0, 'back' => 2 }.freeze
18
+
19
+ # @!attribute [rw] dim
20
+ # @return [Geometry::Dimensions] the inside width, height and depth
21
+ # @!attribute [rw] thickness
22
+ # @return [Float] the thickness of the material
23
+ # @!attribute [rw] notch_width
24
+ # @return [Float] the notch width asked for; each side fits its own
25
+ # @!attribute [rw] kerf
26
+ # @return [Float] the width the laser burns away
27
+ # @!attribute [rw] padding
28
+ # @return [Float] the space between panels on the page
29
+ # @!attribute [rw] units
30
+ # @return [String, Symbol] the units of every length
31
+ # @!attribute [rw] inside_box
32
+ # @return [Object, nil] read from the settings, and not used in drawing
33
+ # @!attribute [rw] front
34
+ # @return [Geometry::Rect] the front panel
35
+ # @!attribute [rw] back
36
+ # @return [Geometry::Rect] the back panel
37
+ # @!attribute [rw] top
38
+ # @return [Geometry::Rect] the top panel, the lid
39
+ # @!attribute [rw] bottom
40
+ # @return [Geometry::Rect] the bottom panel
41
+ # @!attribute [rw] left
42
+ # @return [Geometry::Rect] the left panel
43
+ # @!attribute [rw] right
44
+ # @return [Geometry::Rect] the right panel
45
+ # @!attribute [rw] faces
46
+ # @return [Array<Geometry::Rect>] the six panels, in the order {#conf} indexes them
47
+ # @!attribute [rw] bounds
48
+ # @return [Object] unused
49
+ # @!attribute [rw] conf
50
+ # @return [Hash] for each face, whether the center notch of its
51
+ # horizontal (:halign) and vertical (:valign) sides is a tab, and
52
+ # which faces carry the corner squares (:corners)
53
+ # @!attribute [rw] corner_face
54
+ # @return [Symbol, nil] unused: {#generate_notches} picks it each time
55
+ # @!attribute [rw] metadata
56
+ # @return [Geometry::Point] the width and height kept free for the settings
57
+ # @!attribute [rw] notches
58
+ # @return [Array<Geometry::Line>] every line to cut, once generated
10
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
11
60
 
61
+ # @return [Symbol] one of LIDS
62
+ attr_accessor :lid
63
+
64
+ # @return [Hash{String => Array<Geometry::Line>}] the lines to cut for each face, by its name
65
+ attr_accessor :outlines
66
+
67
+ # @param config [Configuration, Hash] width, height, depth and thickness,
68
+ # and optionally notch, kerf, padding, units, inside_box, lid,
69
+ # metadata_width and metadata_height
12
70
  def initialize(config = {})
13
71
  self.dim = Geometry::Dimensions.new(config['width'], config['height'], config['depth'])
14
72
  self.thickness = config['thickness']
@@ -18,8 +76,10 @@ module Laser
18
76
  self.padding = config['padding']
19
77
  self.units = config['units']
20
78
  self.inside_box = config['inside_box']
79
+ self.lid = (config['lid'] || LIDS.first).to_sym
21
80
 
22
81
  self.notches = []
82
+ self.outlines = {}
23
83
 
24
84
  self.metadata = Geometry::Point[config['metadata_width'] || 0, config['metadata_height'] || 0]
25
85
 
@@ -36,6 +96,8 @@ module Laser
36
96
  }
37
97
  end
38
98
 
99
+ # @return [Geometry::Rect] the smallest rectangle around every line,
100
+ # generating the notches first if needed
39
101
  def enclosure
40
102
  generate_notches if notches.empty?
41
103
  p1 = notches.first.p1.to_a
@@ -50,53 +112,149 @@ module Laser
50
112
  Geometry::Rect[Geometry::Point.new(p1), Geometry::Point.new(p2)]
51
113
  end
52
114
 
115
+ # Lays out the panels and notches them, filling {#outlines} and
116
+ # {#notches}.
117
+ #
118
+ # @return [Array<Geometry::Line>] every line to cut
53
119
  def generate_notches
54
120
  position_faces!
55
121
  corner_face = pick_corners_face
56
122
  self.notches = []
123
+ self.outlines = {}
57
124
  faces.each_with_index do |face, face_index|
58
- bound = face_bounding_rect(face)
59
- edges = []
60
- bound.sides.each_with_index do |bounding_side, side_index|
61
- include_corners = conf[:corners][corner_face][face_index] == :yes && side_index.odd?
62
- key = side_index.odd? ? :valign : :halign
63
- center_out = (conf[key][face_index] == :out)
64
- edges << Notching::Edge.new(bounding_side,
65
- face.sides[side_index],
66
- { notch_width: notch_width,
67
- thickness: thickness,
68
- kerf: kerf,
69
- center_out: center_out,
70
- corners: include_corners })
71
- end
125
+ edges = edges_of(face, face_index, corner_face)
72
126
 
73
127
  if edges.any?(&:corners) && !edges.all?(&:first_notch_out?)
74
128
  edges.each { |e| e.adjust_corners = true }
75
129
  end
76
130
 
77
- side_lines = edges.map do |edge|
78
- Notching::PathGenerator.new(edge).generate
79
- end
80
-
81
- notches << Aggregator.new(side_lines.flatten).lines
131
+ outlines[face.name] = Aggregator.new(lines_of(face, edges)).lines
132
+ notches << outlines[face.name]
82
133
  end
83
134
  notches.flatten!
84
135
  end
85
136
 
137
+ # @return [Float] the width
86
138
  def w; dim.w; end
139
+
140
+ # @return [Float] the height
87
141
  def h; dim.h; end
142
+
143
+ # @return [Float] the depth
88
144
  def d; dim.d; end
89
145
 
146
+ # @return [Float] the longest dimension
90
147
  def longest
91
148
  [w, h, d].max
92
149
  end
93
150
 
151
+ # @return [String] the dimensions, thickness and notch width
94
152
  def to_s
95
153
  "Box:\nH:#{dim.h} W:#{dim.w} D:#{dim.d}\nThickness:#{thickness}, Notch:#{notch_width}"
96
154
  end
97
155
 
98
156
  private
99
157
 
158
+ # One edge for each side of a face, pairing the side with the matching
159
+ # side of the face grown by the thickness.
160
+ #
161
+ # @param face [Geometry::Rect]
162
+ # @param face_index [Integer] its index in {#faces}
163
+ # @param corner_face [Symbol] :front or :top, from {#pick_corners_face}
164
+ # @return [Array<Notching::Edge>]
165
+ def edges_of(face, face_index, corner_face)
166
+ bound = face_bounding_rect(face)
167
+ bound.sides.each_with_index.map do |bounding_side, side_index|
168
+ include_corners = conf[:corners][corner_face][face_index] == :yes && side_index.odd?
169
+ key = side_index.odd? ? :valign : :halign
170
+ Notching::Edge.new(bounding_side,
171
+ face.sides[side_index],
172
+ { notch_width: notch_width,
173
+ thickness: thickness,
174
+ kerf: kerf,
175
+ center_out: conf[key][face_index] == :out,
176
+ corners: include_corners,
177
+ corner_ends: corner_ends(face, side_index) })
178
+ end
179
+ end
180
+
181
+ # A lid that lifts off covers the corners above the walls, so a wall
182
+ # keeps no corner box at the end of a side that touches the lid.
183
+ #
184
+ # @param face [Geometry::Rect]
185
+ # @param side_index [Integer] an index into Rect#sides
186
+ # @return [Array<Integer>] the ends of the side that may carry a corner box
187
+ def corner_ends(face, side_index)
188
+ lid_side = LID_SIDES[face.name]
189
+ return Notching::Edge::ENDS if lid == :full || lid_side.nil?
190
+
191
+ case (side_index - lid_side) % 4
192
+ when 1 then [2]
193
+ when 3 then [1]
194
+ else Notching::Edge::ENDS
195
+ end
196
+ end
197
+
198
+ # @param face [Geometry::Rect]
199
+ # @param edges [Array<Notching::Edge>] from {#edges_of}
200
+ # @return [Array<Geometry::Line>] the lines of a face before they are merged
201
+ def lines_of(face, edges)
202
+ return lid_lines(edges) if face.equal?(top)
203
+
204
+ edges.each_with_index.flat_map do |edge, side_index|
205
+ straight?(face, side_index) ? [edge.inside] : Notching::PathGenerator.new(edge).generate
206
+ end
207
+ end
208
+
209
+ # Whether a side of a wall lies under a lid edge that has no notches.
210
+ #
211
+ # @param face [Geometry::Rect]
212
+ # @param side_index [Integer] an index into Rect#sides
213
+ # @return [Boolean]
214
+ def straight?(face, side_index)
215
+ return false if lid == :full || (lid == :back && face.equal?(back))
216
+
217
+ LID_SIDES[face.name] == side_index
218
+ end
219
+
220
+ # @param edges [Array<Notching::Edge>] the edges of the top panel
221
+ # @return [Array<Geometry::Line>] the lines of the lid, as {#lid} asks
222
+ def lid_lines(edges)
223
+ case lid
224
+ when :plain then edges.map(&:outside)
225
+ when :back then back_lid_lines(edges)
226
+ else edges.flat_map { |edge| Notching::PathGenerator.new(edge).generate }
227
+ end
228
+ end
229
+
230
+ # The lid notched into the back wall only. Its other three edges are
231
+ # straight, along the outside of the walls. Either side of the notches
232
+ # it has a foot, the corner square above the side wall.
233
+ #
234
+ # @param edges [Array<Notching::Edge>] the edges of the top panel, the back one first
235
+ # @return [Array<Geometry::Line>]
236
+ def back_lid_lines(edges)
237
+ joint = edges.first
238
+ joint.corners = true
239
+ joint.adjust_corners = true
240
+ path = Notching::PathGenerator.new(joint).notch_lines
241
+
242
+ path + foot(joint.outside.p1, path.first.p1) + foot(joint.outside.p2, path.last.p2) + edges.drop(1).map(&:outside)
243
+ end
244
+
245
+ # The two sides of a foot the notches do not draw: its bottom, from the
246
+ # outer corner of the lid, and the side that faces the first notch.
247
+ #
248
+ # @param corner [Geometry::Point] the outer corner of the lid
249
+ # @param notch [Geometry::Point] where the notches start or end
250
+ # @return [Array(Geometry::Line, Geometry::Line)]
251
+ def foot(corner, notch)
252
+ turn = Geometry::Point[notch.x, corner.y]
253
+ [Geometry::Line[corner, turn], Geometry::Line[turn, notch]]
254
+ end
255
+
256
+ # @param face [Geometry::Rect]
257
+ # @return [Geometry::Rect] the face grown by the thickness on every side
100
258
  def face_bounding_rect(face)
101
259
  b = face.clone
102
260
  b.move_to(b.position.plus(-thickness, -thickness))
@@ -125,7 +283,11 @@ module Laser
125
283
  #
126
284
  # 0,0
127
285
  # ___________________________________________________________________
128
-
286
+ #
287
+ # Places the six panels in the cross above, leaving room for the
288
+ # settings at the bottom left.
289
+ #
290
+ # @return [Array<Geometry::Rect>] the faces
129
291
  def position_faces!
130
292
  offset_x = [padding + d + (3 * thickness), metadata.x + (2 * thickness) + padding].max
131
293
  offset_y = [padding + d + (3 * thickness), metadata.y + (2 * thickness) + padding].max
@@ -146,6 +308,9 @@ module Laser
146
308
  faces.each(&:relocate!)
147
309
  end
148
310
 
311
+ # Creates the six panels at the origin, each the size of its side.
312
+ #
313
+ # @return [Geometry::Rect] the right panel
149
314
  def create_faces!
150
315
  zero = Geometry::Point.new(0, 0)
151
316
  self.front = Geometry::Rect.create(zero, dim.w, dim.h, "front")
@@ -160,6 +325,9 @@ module Laser
160
325
 
161
326
  # Choose which face will be responsible for filling out the little square overlap
162
327
  # in the corners. Only one of the 3 possible sides need to be picked.
328
+ #
329
+ # @return [Symbol] :top when every notch count of the front is 3 more
330
+ # than a multiple of 4, :front otherwise
163
331
  def pick_corners_face
164
332
  b = face_bounding_rect(front)
165
333
  edges = []
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ # Picks the box a configuration asks for, so a renderer need not know
6
+ # how many kinds there are.
7
+ module BoxFactory
8
+ # @param config [Configuration]
9
+ # @return [Box, Sloped::Box] a sloped box when the configuration has a slope
10
+ def self.for(config)
11
+ config[:slope] ? Sloped::Box.new(config) : Box.new(config)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -10,15 +10,37 @@ module Laser
10
10
  # Cells in a progress bar.
11
11
  BAR_WIDTH = 60
12
12
 
13
+ # Gives every command the -v, --verbose flag.
14
+ #
15
+ # @param subclass [Class] the new command
16
+ # @return [void]
13
17
  def self.inherited(subclass)
14
18
  super
19
+
20
+ # Add --verbose flag to the command, but at the very end of the class definition.
21
+ trace = TracePoint.new(:end) do |tp|
22
+ # skip nested classes closing inside the body
23
+ next unless tp.self == subclass
24
+
25
+ # disable the trace point
26
+ trace.disable
27
+
28
+ # add the verbose option to the subclass as the last option
29
+ subclass.class_exec { finalize!(subclass) }
30
+ end
31
+ trace.enable(target_thread: Thread.current)
32
+ end
33
+
34
+ def self.finalize!(subclass)
15
35
  subclass.option :verbose, type: :boolean, default: false, aliases: ['-v'],
16
36
  desc: 'Print the configuration, and the full backtrace on failure'
17
37
  end
18
38
 
19
39
  # Boxes are as wide as help is.
40
+ #
41
+ # @return [Dry::CLI::UI::Console]
20
42
  def ui
21
- @ui ||= Dry::CLI::UI::Console.new(out: out, err: err, box_width: CLI.help_width)
43
+ @ui ||= CLI.console(out: out, err: err)
22
44
  end
23
45
 
24
46
  # A green bar of BAR_WIDTH cells, or fewer when the terminal is narrower.
@@ -26,6 +48,7 @@ module Laser
26
48
  # @param label [String]
27
49
  # @param total [Integer]
28
50
  # @yieldparam bar [#advance]
51
+ # @return [Object] what the block returns
29
52
  def progress(label, total:, &)
30
53
  Dry::CLI::UI::Console
31
54
  .new(out: out, err: err, box_width: CLI.help_width, width: progress_width(label))
@@ -36,6 +59,9 @@ module Laser
36
59
 
37
60
  # dry-cli-ui sizes a bar as the terminal width less the label and the
38
61
  # counters around it, so the bar is capped by capping that width.
62
+ #
63
+ # @param label [String]
64
+ # @return [Integer] the width to give the console
39
65
  def progress_width(label)
40
66
  wanted = BAR_WIDTH + label.length + Dry::CLI::UI::Widgets::Progress::CHROME
41
67
  err.respond_to?(:tty?) && err.tty? ? [wanted, TTY::Screen.width].min : wanted
@@ -6,6 +6,7 @@ module Laser
6
6
  # A configuration saved as JSON. The path '-' stands for STDIN when
7
7
  # reading, and for the command's own output when writing.
8
8
  class ConfigFile
9
+ # The path that stands for STDIN or the command's output.
9
10
  STDIO = '-'
10
11
 
11
12
  # @param path [String]
@@ -23,6 +24,7 @@ module Laser
23
24
 
24
25
  # @param settings [Hash]
25
26
  # @param out [IO] where '-' writes to
27
+ # @return [void]
26
28
  # @raise [Error] when the file cannot be written
27
29
  def write(settings, out)
28
30
  json = JSON.pretty_generate(settings)
@@ -3,9 +3,11 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module CLI
6
+ # The examples command: prints worked examples of every command.
6
7
  class Examples < Command
7
8
  desc 'Show detailed usage examples'
8
9
 
10
+ # The examples it prints.
9
11
  TEXT = <<~EXAMPLES
10
12
  1. A box in inches, with the kerf set to 0.008", opened once it is written:
11
13
 
@@ -31,8 +33,14 @@ module Laser
31
33
 
32
34
  laser-cutter generate -o box.pdf -R box-settings.json
33
35
  cat box-settings.json | laser-cutter generate -o box.pdf -R -
36
+
37
+ 7. A box with a lid that lifts off: a plain rectangle, or one notched into the back wall only:
38
+
39
+ laser-cutter generate -b 3x2x2/0.125 --lid plain -o box.pdf
40
+ laser-cutter generate -b 3x2x2/0.125 --lid back -o box.pdf
34
41
  EXAMPLES
35
42
 
43
+ # @return [void]
36
44
  def call(**)
37
45
  out.puts TEXT
38
46
  end
@@ -3,9 +3,12 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module CLI
6
+ # The generate command: draws a box into a PDF or an SVG, with a
7
+ # progress bar, and saves or reads its settings as JSON.
6
8
  class Generate < Command
7
9
  desc 'Draw the panels of a box into a PDF or an SVG file'
8
10
 
11
+ # The first line of the opening box.
9
12
  BANNER = "Laser-Cutter (ruby gem) Version #{Laser::Cutter::VERSION}, © 2015-2026 Konstantin Gredeskoul".freeze
10
13
 
11
14
  # Columns a box spends on its borders and padding.
@@ -17,13 +20,18 @@ module Laser
17
20
  # Options that describe the run rather than the box, kept out of a saved configuration.
18
21
  RUN_ONLY = %i[verbose read write open format args].freeze
19
22
 
20
- option :box, aliases: ['-b'], desc: 'WxHxD/T[/N]: width, height, depth, thickness and optional notch, in one'
23
+ option :box, aliases: ['-b'], desc: 'WxHxD/T[/N]: width, height, depth, thickness and optional notch, in one expression'
21
24
  option :width, aliases: ['-w'], desc: 'Internal width of the box'
22
25
  option :height, aliases: ['-H'], desc: 'Internal height of the box'
23
26
  option :depth, aliases: ['-d'], desc: 'Internal depth of the box'
24
27
  option :thickness, aliases: ['-t'], desc: 'Thickness of the material'
28
+ option :slope, aliases: ['-S'], desc: 'When provided, changes the shape of the enclosure from a parallilepiped to one with a side sloped, for mounting an LCD or any other screen. This options defines the angle above horizontal (0º), in degrees. This option requires --front-height'
29
+ option :front_height, aliases: ['-F'], desc: 'Used with --slope: internal height at the front of a sloped box, where the slope begins. Please refer to images in the README, or 3D projections on https://makeabox.io'
30
+ option :lid, values: %w[full back plain], aliases: ['-L'],
31
+ desc: 'Lid: notched on every side, into the back wall only (not with --slope), or on no side (default: full)'
25
32
  option :notch, aliases: ['-n'], desc: 'Notch length, a guide only (default: three times the thickness)'
26
33
  option :kerf, aliases: ['-k'], desc: 'Kerf, the width of the cut (default: 0.0024in)'
34
+ # No default here: one would override the lid a saved configuration asks for.
27
35
  option :units, default: 'in', values: %w[in mm], aliases: ['-u'], desc: 'Units every dimension is in'
28
36
 
29
37
  option :file, aliases: ['-o'], desc: 'File to write (required)'
@@ -39,15 +47,22 @@ module Laser
39
47
  option :inside_box, type: :boolean, default: false, aliases: ['-B'],
40
48
  desc: 'Also draw the box without kerf, in red, to check the kerf'
41
49
  option :open, type: :boolean, default: false, aliases: ['-O'], desc: 'Open the file once it is written'
42
- option :write, aliases: ['-W'], desc: "Save the configuration to a file, or to STDOUT with '-'"
43
- option :read, aliases: ['-R'], desc: "Read the configuration from a file, or from STDIN with '-'"
50
+ option :write, aliases: ['-W'], desc: "Save the configuration to a file or to STDOUT with '-'"
51
+ option :read, aliases: ['-R'], desc: "Read the configuration from a file or from STDIN with '-'"
44
52
 
45
53
  example [
46
54
  '-b 3x2x2/0.125 -o box.pdf # a box in inches',
47
55
  '-b 3x2x2/0.125 -f svg -o box.svg # as an SVG',
56
+ '-b 3x2x2/0.125 --lid plain -o box.pdf # with a lid that lifts off',
57
+ '-b 4x3x5/0.125 --slope 45 --front-height 1 -o box.pdf # with a front that slopes back',
48
58
  '-u mm -w 70 -H 20 -d 50 -t 4.3 -o box.pdf'
49
59
  ]
50
60
 
61
+ # @param format [String] "pdf" or "svg"
62
+ # @param verbose [Boolean] whether to print the configuration first
63
+ # @param options [Hash] the other options, by name
64
+ # @return [void]
65
+ # @raise [MissingOption, InvalidOption, Error] when the settings do not make a box
51
66
  def call(format:, verbose:, **options)
52
67
  config = configuration(options)
53
68
  ui.debug('Configuration:', JSON.pretty_generate(config.to_hash)) if verbose
@@ -68,11 +83,18 @@ module Laser
68
83
 
69
84
  # A box wraps in the middle of a word, which would break a path nobody
70
85
  # could then copy, so this one widens to fit a path longer than it.
86
+ #
87
+ # @param format [String]
88
+ # @param path [String] the full path of the file written
89
+ # @return [void]
71
90
  def report(format, path)
72
91
  ui.success("Generated #{format.upcase} file:", path, width: [CLI.help_width, path.length + BOX_CHROME].max)
73
92
  end
74
93
 
75
94
  # What was read from a file, overridden by what the command line gave.
95
+ #
96
+ # @param options [Hash] the options of the command
97
+ # @return [Configuration]
76
98
  def configuration(options)
77
99
  given = options.except(*RUN_ONLY).compact
78
100
  saved = options[:read] ? ConfigFile.new(options[:read]).read : {}
@@ -80,9 +102,23 @@ module Laser
80
102
  Configuration.new(settings.merge(debug: settings.delete(:inside_box)))
81
103
  end
82
104
 
83
- # One line per dimension, aligned, in the units of the box.
105
+ # One line per dimension, aligned, in the units of the box, then the lid.
106
+ #
107
+ # @param config [Configuration]
108
+ # @return [String]
84
109
  def dimensions(config)
85
- DIMENSIONS.map { |name| "#{"#{name.capitalize}:".ljust(11)} #{config[name]} #{config.units}" }.join("\n")
110
+ rows = (DIMENSIONS.map { |name| [name, "#{config[name]} #{config.units}"] } + slope(config)) << [:lid, config.lid]
111
+ rows.map { |name, value| "#{"#{name.capitalize}:".ljust(11)} #{value}" }.join("\n")
112
+ end
113
+
114
+ # The two lines a sloped box adds: the height of its front, and the angle.
115
+ #
116
+ # @param config [Configuration]
117
+ # @return [Array<Array(Symbol, String)>] none for a box without a slope
118
+ def slope(config)
119
+ return [] unless config.slope
120
+
121
+ [[:front, "#{config.front_height} #{config.units}"], [:slope, "#{config.slope}°"]]
86
122
  end
87
123
  end
88
124
  end
@@ -3,6 +3,7 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module CLI
6
+ # The help command: help for the program, or for one command.
6
7
  class Help < Command
7
8
  desc 'Show help, for the program or for one command'
8
9
 
@@ -11,6 +12,9 @@ module Laser
11
12
  example ['', 'generate']
12
13
 
13
14
  # Asks dry-cli for the help it prints on --help, which exits by itself.
15
+ #
16
+ # @param command [String, nil] the command to explain; nil for the program
17
+ # @return [void]
14
18
  def call(command: nil, **)
15
19
  Dry::CLI.new(CLI).call(arguments: [command, '--help'].compact, out: out, err: err)
16
20
  end
@@ -3,6 +3,7 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module CLI
6
+ # The page-sizes command: lists every page size a PDF can take.
6
7
  class PageSizes < Command
7
8
  desc 'List every page size, with its dimensions'
8
9
 
@@ -10,6 +11,8 @@ module Laser
10
11
 
11
12
  example ['', '--units mm']
12
13
 
14
+ # @param units [String] "in" or "mm"
15
+ # @return [void]
13
16
  def call(units:, **)
14
17
  out.puts PageManager.new(units).all_page_sizes
15
18
  end
@@ -3,9 +3,11 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module CLI
6
+ # The version command: prints the version of the gem.
6
7
  class Version < Command
7
8
  desc 'Print the version'
8
9
 
10
+ # @return [void]
9
11
  def call(**)
10
12
  out.puts Laser::Cutter::VERSION
11
13
  end