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.
- checksums.yaml +4 -4
- data/.github/workflows/pages.yml +44 -0
- data/.gitignore +2 -0
- data/.openlanes/config.yml +106 -0
- 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
- 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
- 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
- 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
- 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
- data/.plans/decisions.md +11 -0
- data/CHANGELOG.md +134 -0
- data/CLAUDE.md +30 -5
- data/Gemfile +2 -1
- data/README.md +309 -31
- data/Rakefile +11 -7
- data/docs/badges/coverage_badge.svg +2 -2
- data/docs/decisions/decisions-on-angled-box.md +93 -0
- data/docs/decisions/stack.md +6 -0
- data/docs/images/box-3d.avif +0 -0
- data/docs/images/box-lid-back.avif +0 -0
- data/docs/images/box-lid-full.avif +0 -0
- data/docs/images/box-lid-plain.avif +0 -0
- data/docs/images/box-metric.avif +0 -0
- data/docs/images/box-sloped-3d.avif +0 -0
- data/docs/images/box-sloped.avif +0 -0
- data/docs/images/box-tray.avif +0 -0
- data/justfile +24 -8
- data/laser-cutter.gemspec +2 -1
- data/lib/laser/cutter/aggregator.rb +28 -0
- data/lib/laser/cutter/box.rb +192 -24
- data/lib/laser/cutter/box_factory.rb +15 -0
- data/lib/laser/cutter/cli/command.rb +27 -1
- data/lib/laser/cutter/cli/config_file.rb +2 -0
- data/lib/laser/cutter/cli/examples.rb +8 -0
- data/lib/laser/cutter/cli/generate.rb +41 -5
- data/lib/laser/cutter/cli/help.rb +4 -0
- data/lib/laser/cutter/cli/page_sizes.rb +3 -0
- data/lib/laser/cutter/cli/version.rb +2 -0
- data/lib/laser/cutter/cli.rb +23 -4
- data/lib/laser/cutter/configuration.rb +42 -2
- data/lib/laser/cutter/geometry/dimensions.rb +7 -0
- data/lib/laser/cutter/geometry/line.rb +42 -0
- data/lib/laser/cutter/geometry/loop.rb +138 -0
- data/lib/laser/cutter/geometry/point.rb +3 -0
- data/lib/laser/cutter/geometry/rect.rb +25 -0
- data/lib/laser/cutter/geometry/shape.rb +19 -1
- data/lib/laser/cutter/geometry/transform.rb +50 -0
- data/lib/laser/cutter/geometry/tuple.rb +47 -11
- data/lib/laser/cutter/invalid_option.rb +8 -0
- data/lib/laser/cutter/launcher.rb +14 -1
- data/lib/laser/cutter/missing_option.rb +2 -0
- data/lib/laser/cutter/notching/angled_edge.rb +67 -0
- data/lib/laser/cutter/notching/base.rb +8 -0
- data/lib/laser/cutter/notching/edge.rb +59 -4
- data/lib/laser/cutter/notching/infinite_iterator.rb +11 -1
- data/lib/laser/cutter/notching/path_generator.rb +86 -81
- data/lib/laser/cutter/notching/shift.rb +4 -0
- data/lib/laser/cutter/options.rb +175 -0
- data/lib/laser/cutter/page_manager.rb +12 -2
- data/lib/laser/cutter/renderer/base.rb +26 -4
- data/lib/laser/cutter/renderer/box_renderer.rb +14 -1
- data/lib/laser/cutter/renderer/layout_renderer.rb +13 -3
- data/lib/laser/cutter/renderer/line_renderer.rb +6 -0
- data/lib/laser/cutter/renderer/meta_renderer.rb +33 -9
- data/lib/laser/cutter/renderer/rect_renderer.rb +5 -0
- data/lib/laser/cutter/renderer/svg_renderer.rb +12 -4
- data/lib/laser/cutter/renderer.rb +2 -0
- data/lib/laser/cutter/sloped/box.rb +90 -0
- data/lib/laser/cutter/sloped/corners.rb +58 -0
- data/lib/laser/cutter/sloped/layout.rb +97 -0
- data/lib/laser/cutter/sloped/notcher.rb +53 -0
- data/lib/laser/cutter/sloped/panels.rb +121 -0
- data/lib/laser/cutter/sloped/placement.rb +36 -0
- data/lib/laser/cutter/sloped/profile.rb +176 -0
- data/lib/laser/cutter/sloped/request.rb +31 -0
- data/lib/laser/cutter/sloped/ring_panel.rb +120 -0
- data/lib/laser/cutter/sloped/side_panel.rb +89 -0
- data/lib/laser/cutter/types.rb +60 -0
- data/lib/laser/cutter/units_converter.rb +5 -0
- data/lib/laser/cutter/version.rb +2 -1
- data/lib/laser/cutter/zero_value_not_allowed.rb +2 -0
- data/lib/laser/cutter.rb +40 -0
- data/spec/laser/cutter/angled_edge_spec.rb +103 -0
- data/spec/laser/cutter/box_factory_spec.rb +19 -0
- data/spec/laser/cutter/box_lid_spec.rb +142 -0
- data/spec/laser/cutter/cli_spec.rb +114 -7
- data/spec/laser/cutter/configuration_spec.rb +54 -0
- data/spec/laser/cutter/loop_spec.rb +107 -0
- data/spec/laser/cutter/options_spec.rb +162 -0
- data/spec/laser/cutter/renderer/layout_renderer_spec.rb +21 -20
- data/spec/laser/cutter/renderer/meta_renderer_spec.rb +82 -0
- data/spec/laser/cutter/renderer/svg_renderer_spec.rb +34 -0
- data/spec/laser/cutter/sloped/assembly_spec.rb +78 -0
- data/spec/laser/cutter/sloped/box_spec.rb +151 -0
- data/spec/laser/cutter/sloped/corners_spec.rb +45 -0
- data/spec/laser/cutter/sloped/layout_spec.rb +77 -0
- data/spec/laser/cutter/sloped/notcher_spec.rb +57 -0
- data/spec/laser/cutter/sloped/panels_spec.rb +175 -0
- data/spec/laser/cutter/sloped/placement_spec.rb +50 -0
- data/spec/laser/cutter/sloped/request_spec.rb +48 -0
- data/spec/laser/cutter/sloped/ring_panel_spec.rb +102 -0
- data/spec/laser/cutter/sloped/side_panel_spec.rb +98 -0
- data/spec/laser/cutter/sloped_profile_spec.rb +156 -0
- data/spec/laser/cutter/transform_spec.rb +53 -0
- data/spec/laser/cutter_spec.rb +114 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support/loops.rb +106 -0
- data/spec/support/outline.rb +53 -0
- data/spec/support/sloped.rb +26 -0
- data/spec/support/solid.rb +95 -0
- data/spec/wishes/wish-tilted-box.png +0 -0
- metadata +81 -7
- /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
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
5
|
module Notching
|
|
6
|
+
# Draws the notched path of one {Edge}: a zigzag between its inside and
|
|
7
|
+
# outside lines, plus the corner boxes at the ends the edge names.
|
|
6
8
|
class PathGenerator
|
|
7
9
|
extend ::Forwardable
|
|
8
10
|
|
|
@@ -10,11 +12,11 @@ module Laser
|
|
|
10
12
|
def_delegator :@edge, method_name, method_name
|
|
11
13
|
end
|
|
12
14
|
|
|
15
|
+
# @!attribute [rw] edge
|
|
16
|
+
# @return [Edge] the side to draw
|
|
13
17
|
attr_accessor :edge
|
|
14
18
|
|
|
15
|
-
#
|
|
16
|
-
# inside line of a single edge. Edge class encapsulates both of them with additional
|
|
17
|
-
# properties.
|
|
19
|
+
# @param edge [Edge] the side to draw, with both its lines
|
|
18
20
|
def initialize(edge)
|
|
19
21
|
@edge = edge
|
|
20
22
|
end
|
|
@@ -25,108 +27,80 @@ module Laser
|
|
|
25
27
|
#
|
|
26
28
|
# We always want to create a symmetric path that has a notch in the middle
|
|
27
29
|
# (for center_out = true) or dip in the middle (center_out = false)
|
|
30
|
+
#
|
|
31
|
+
# @return [Array<Geometry::Line>] the corner boxes, then the zigzag
|
|
28
32
|
def generate
|
|
29
|
-
shifts = define_shifts
|
|
30
|
-
vertices = []
|
|
31
33
|
lines = []
|
|
34
|
+
lines << corner_box_sides if corners
|
|
35
|
+
lines << notch_lines
|
|
36
|
+
lines.flatten
|
|
37
|
+
end
|
|
32
38
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
# The zigzag alone, without the corner boxes.
|
|
40
|
+
#
|
|
41
|
+
# @return [Array<Geometry::Line>]
|
|
42
|
+
def notch_lines
|
|
37
43
|
point = starting_point
|
|
38
|
-
|
|
39
|
-
vertices
|
|
40
|
-
|
|
41
|
-
shifts.each do |shift|
|
|
44
|
+
vertices = [point]
|
|
45
|
+
adjust_for_kerf(vertices, -1) if adjust_end?(1)
|
|
46
|
+
define_shifts.each do |shift|
|
|
42
47
|
point = shift.next_point_after point
|
|
43
48
|
vertices << point
|
|
44
49
|
end
|
|
45
|
-
adjust_for_kerf(vertices, 1) if
|
|
46
|
-
|
|
47
|
-
lines.flatten
|
|
50
|
+
adjust_for_kerf(vertices, 1) if adjust_end?(2)
|
|
51
|
+
create_lines(vertices)
|
|
48
52
|
end
|
|
49
53
|
|
|
54
|
+
# Moves the last vertex along the edge by the kerf.
|
|
55
|
+
#
|
|
56
|
+
# @param vertices [Array<Geometry::Point>] the path so far, changed in place
|
|
57
|
+
# @param direction [Integer] -1 backwards, 1 forwards
|
|
58
|
+
# @return [void]
|
|
50
59
|
def adjust_for_kerf(vertices, direction)
|
|
51
60
|
return unless kerf?
|
|
52
61
|
|
|
53
62
|
point = vertices.pop
|
|
54
|
-
point = point.plus(2 * direction * shift_vector(1))
|
|
63
|
+
point = point.plus(2 * direction * shift_vector(1))
|
|
55
64
|
vertices << point
|
|
56
65
|
end
|
|
57
66
|
|
|
67
|
+
# @return [Array<Geometry::Line>] the sides of the corner boxes, with
|
|
68
|
+
# their kerf strips when the corners are adjusted
|
|
58
69
|
def corner_box_sides
|
|
59
|
-
|
|
60
|
-
extra_lines = []
|
|
70
|
+
ends = Edge::ENDS.select { |end_index| edge.corner_at?(end_index) }
|
|
61
71
|
|
|
62
|
-
# These
|
|
72
|
+
# These boxes occupy the corners of the 3D box. They do not match
|
|
63
73
|
# in width to our notches because they are usually merged with them.
|
|
64
74
|
# It's just an aesthetic choice I guess.
|
|
65
|
-
boxes
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if kerf? && adjust_corners
|
|
69
|
-
if first_notch_out?
|
|
70
|
-
k = 2
|
|
71
|
-
direction = -1
|
|
72
|
-
dim_index = 1
|
|
73
|
-
extra_lines << add_corners_when_out(dim_index, direction, k)
|
|
74
|
-
else
|
|
75
|
-
k = -2
|
|
76
|
-
direction = 1
|
|
77
|
-
dim_index = 0
|
|
78
|
-
extra_lines << add_boxes_when_in(dim_index, direction, k)
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
sides = boxes.flatten.map(&:relocate!).map(&:sides)
|
|
82
|
-
sides << extra_lines if !extra_lines.empty?
|
|
75
|
+
boxes = ends.map { |end_index| Geometry::Rect[inside_point(end_index).clone, outside_point(end_index).clone] }
|
|
76
|
+
sides = boxes.map(&:relocate!).map(&:sides)
|
|
77
|
+
sides << ends.map { |end_index| kerf_strip(end_index) } if kerf? && adjust_corners
|
|
83
78
|
sides.flatten
|
|
84
79
|
end
|
|
85
80
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
coords = []
|
|
98
|
-
coords[d_index_along] = edge.inside.p2[d_index_along]
|
|
99
|
-
coords[d_index_across] = edge.outside.p2[d_index_across]
|
|
100
|
-
p2 = Geometry::Point[*coords]
|
|
101
|
-
r2 = Geometry::Rect[p1, p2]
|
|
102
|
-
lines = [r1, r2].map(&:sides).flatten
|
|
103
|
-
lines << Geometry::Line[edge.inside.p1.plus(v1), edge.inside.p1.clone]
|
|
104
|
-
lines << Geometry::Line[edge.inside.p2.plus(v2), edge.inside.p2.clone]
|
|
105
|
-
lines
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
def add_corners_when_out(dim_index, direction, k)
|
|
109
|
-
v1 = direction * k * shift_vector(1, dim_index)
|
|
110
|
-
v2 = direction * k * shift_vector(2, dim_index)
|
|
111
|
-
p1 = edge.inside.p1.plus(v1)
|
|
112
|
-
coords = []
|
|
113
|
-
coords[d_index_along] = edge.outside.p1[d_index_along]
|
|
114
|
-
coords[d_index_across] = edge.inside.p1[d_index_across]
|
|
115
|
-
p2 = Geometry::Point[*coords]
|
|
116
|
-
r1 = Geometry::Rect[p1, p2]
|
|
81
|
+
# Widens the corner box at one end by the kerf, on the side where
|
|
82
|
+
# nothing is attached to it: along the edge when the first notch is a
|
|
83
|
+
# hole, across it when the first notch is a tab.
|
|
84
|
+
#
|
|
85
|
+
# @param end_index [Integer] 1 for p1, 2 for p2
|
|
86
|
+
# @return [Array<Geometry::Line>]
|
|
87
|
+
def kerf_strip(end_index)
|
|
88
|
+
inside = inside_point(end_index)
|
|
89
|
+
outside = outside_point(end_index)
|
|
90
|
+
along, across = first_notch_out? ? [outside, inside] : [inside, outside]
|
|
91
|
+
moved = inside.plus(-2 * shift_vector(end_index, first_notch_out? ? 1 : 0))
|
|
117
92
|
|
|
118
|
-
p1 = edge.inside.p2.plus(v2)
|
|
119
93
|
coords = []
|
|
120
|
-
coords[d_index_along] =
|
|
121
|
-
coords[d_index_across] =
|
|
122
|
-
|
|
123
|
-
r2 = Geometry::Rect[p1, p2]
|
|
124
|
-
lines = [r1, r2].map(&:sides).flatten
|
|
125
|
-
lines << Geometry::Line[edge.inside.p1.plus(v1), edge.inside.p1.clone]
|
|
126
|
-
lines << Geometry::Line[edge.inside.p2.plus(v2), edge.inside.p2.clone]
|
|
127
|
-
lines
|
|
94
|
+
coords[d_index_along] = along[d_index_along]
|
|
95
|
+
coords[d_index_across] = across[d_index_across]
|
|
96
|
+
Geometry::Rect[moved, Geometry::Point[*coords]].sides << Geometry::Line[moved, inside.clone]
|
|
128
97
|
end
|
|
129
98
|
|
|
99
|
+
# Half the kerf, along one axis, outwards at one end of the edge.
|
|
100
|
+
#
|
|
101
|
+
# @param index [Integer] 1 for p1, 2 for p2
|
|
102
|
+
# @param dim_shift [Integer] 0 for along the edge, 1 for across it
|
|
103
|
+
# @return [Vector]
|
|
130
104
|
def shift_vector(index, dim_shift = 0)
|
|
131
105
|
shift = []
|
|
132
106
|
shift[(d_index_across + dim_shift) % 2] = 0
|
|
@@ -134,32 +108,58 @@ module Laser
|
|
|
134
108
|
Vector.[](*shift)
|
|
135
109
|
end
|
|
136
110
|
|
|
111
|
+
# @return [Geometry::Point] where the zigzag starts: p1 of the inside line
|
|
137
112
|
def starting_point
|
|
138
113
|
edge.inside.p1.clone # start
|
|
139
114
|
end
|
|
140
115
|
|
|
141
|
-
# 0
|
|
116
|
+
# @return [Integer] the axis the edge runs along: 0 for x, 1 for y
|
|
142
117
|
def d_index_along
|
|
143
118
|
edge.inside.p1.x == edge.inside.p2.x ? 1 : 0
|
|
144
119
|
end
|
|
145
120
|
|
|
121
|
+
# @return [Integer] the axis across the edge: 0 for x, 1 for y
|
|
146
122
|
def d_index_across
|
|
147
123
|
(d_index_along + 1) % 2
|
|
148
124
|
end
|
|
149
125
|
|
|
126
|
+
# @return [Integer] 1 when the edge runs towards greater coordinates, -1 otherwise
|
|
150
127
|
def direction_along
|
|
151
128
|
edge.inside.p1.coords.[](d_index_along) < edge.inside.p2.coords.[](d_index_along) ? 1 : -1
|
|
152
129
|
end
|
|
153
130
|
|
|
131
|
+
# @return [Integer] 1 when the outside line lies at greater coordinates, -1 otherwise
|
|
154
132
|
def direction_across
|
|
155
133
|
edge.inside.p1.coords.[](d_index_across) < edge.outside.p1.coords.[](d_index_across) ? 1 : -1
|
|
156
134
|
end
|
|
157
135
|
|
|
158
136
|
private
|
|
159
137
|
|
|
138
|
+
# @param end_index [Integer] 1 for p1, 2 for p2
|
|
139
|
+
# @return [Geometry::Point] that end of the inside line
|
|
140
|
+
def inside_point(end_index)
|
|
141
|
+
edge.inside.public_send(:"p#{end_index}")
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# @param end_index [Integer] 1 for p1, 2 for p2
|
|
145
|
+
# @return [Geometry::Point] that end of the outside line
|
|
146
|
+
def outside_point(end_index)
|
|
147
|
+
edge.outside.public_send(:"p#{end_index}")
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# A corner box next to a hole is widened by the kerf, so the hole starts that much later.
|
|
151
|
+
#
|
|
152
|
+
# @param end_index [Integer] 1 for p1, 2 for p2
|
|
153
|
+
# @return [Boolean] whether to move that end of the zigzag
|
|
154
|
+
def adjust_end?(end_index)
|
|
155
|
+
adjust_corners && !first_notch_out? && edge.corner_at?(end_index)
|
|
156
|
+
end
|
|
157
|
+
|
|
160
158
|
# This method has the bulk of the logic: we create the list of path deltas
|
|
161
|
-
# to be applied when we walk the edge next.
|
|
162
|
-
#
|
|
159
|
+
# to be applied when we walk the edge next. Tabs grow by the kerf and
|
|
160
|
+
# holes shrink by it, except the first and last.
|
|
161
|
+
#
|
|
162
|
+
# @return [Array<Shift>]
|
|
163
163
|
def define_shifts
|
|
164
164
|
along_iter = create_iterator_along
|
|
165
165
|
across_iter = create_iterator_across
|
|
@@ -192,15 +192,20 @@ module Laser
|
|
|
192
192
|
# As we draw notches, shifts define the 'delta' – movement from one point
|
|
193
193
|
# to the next. This method defines three types of movements we'll be doing:
|
|
194
194
|
# one alongside the edge, and two across (towards the box and outward from the box)
|
|
195
|
+
#
|
|
196
|
+
# @return [InfiniteIterator] of the one step along the edge
|
|
195
197
|
def create_iterator_along
|
|
196
198
|
InfiniteIterator.new([Shift.new(notch_width, direction_along, d_index_along)])
|
|
197
199
|
end
|
|
198
200
|
|
|
201
|
+
# @return [InfiniteIterator] of the steps out and back across the edge
|
|
199
202
|
def create_iterator_across
|
|
200
203
|
InfiniteIterator.new([Shift.new(thickness, direction_across, d_index_across),
|
|
201
204
|
Shift.new(thickness, -direction_across, d_index_across)])
|
|
202
205
|
end
|
|
203
206
|
|
|
207
|
+
# @param vertices [Array<Geometry::Point>]
|
|
208
|
+
# @return [Array<Geometry::Line>] a line between each vertex and the next
|
|
204
209
|
def create_lines(vertices)
|
|
205
210
|
lines = []
|
|
206
211
|
vertices.each_with_index do |v, i|
|
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
5
|
module Notching
|
|
6
|
+
# One step of a notched path: delta along one axis, x (0) or y (1),
|
|
7
|
+
# forwards (1) or backwards (-1).
|
|
6
8
|
Shift = Struct.new(:delta, :direction, :dim_index) do
|
|
9
|
+
# @param point [Geometry::Point]
|
|
10
|
+
# @return [Geometry::Point] the point one step on
|
|
7
11
|
def next_point_after(point)
|
|
8
12
|
p = point.clone
|
|
9
13
|
shift = []
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Laser
|
|
4
|
+
module Cutter
|
|
5
|
+
# Everything a box can be asked for, typed. This is what a Ruby program
|
|
6
|
+
# hands to {Laser::Cutter.render} or {Laser::Cutter.write}.
|
|
7
|
+
#
|
|
8
|
+
# Values are coerced, so the Strings of a web form will do, and an
|
|
9
|
+
# instance cannot be changed: #new(lid: :plain) returns a changed copy.
|
|
10
|
+
# Keys may be Strings or Symbols. A key it does not
|
|
11
|
+
# know, a value of the wrong type or a dimension left out all raise,
|
|
12
|
+
# at once, before anything is drawn.
|
|
13
|
+
#
|
|
14
|
+
# The distances left nil take the default for the units: a kerf of
|
|
15
|
+
# 0.0024in, a margin of 0.125in, a padding of 0.1in, a stroke of 0.001in,
|
|
16
|
+
# and a notch three times the thickness.
|
|
17
|
+
#
|
|
18
|
+
# @example A box in millimeters, with a plain lid, as an SVG
|
|
19
|
+
# options = Laser::Cutter::Options.new(width: 70, height: 20, depth: 50, thickness: 4.3,
|
|
20
|
+
# units: :mm, lid: :plain, format: :svg)
|
|
21
|
+
# options.lid # => "plain"
|
|
22
|
+
# Laser::Cutter.render(options) # => "<svg ..."
|
|
23
|
+
#
|
|
24
|
+
# @example A box whose front slopes back at 45 degrees, from 1 inch up
|
|
25
|
+
# Laser::Cutter::Options.new(width: 4, height: 3, depth: 5, thickness: 0.125, slope: 45, front_height: 1)
|
|
26
|
+
#
|
|
27
|
+
# @raise [MissingOption] when width, height, depth or thickness is left out, or a slope has no front height
|
|
28
|
+
# @raise [InvalidOption] when a value cannot be used, a key is not an option, or the numbers do not make a sloped box
|
|
29
|
+
class Options < Dry::Struct
|
|
30
|
+
# The dimensions a box cannot be drawn without.
|
|
31
|
+
REQUIRED = %i[width height depth thickness].freeze
|
|
32
|
+
|
|
33
|
+
# The longest list of allowed values an error spells out.
|
|
34
|
+
MAXIMUM_CHOICES = 5
|
|
35
|
+
|
|
36
|
+
transform_keys(&:to_sym)
|
|
37
|
+
schema schema.strict
|
|
38
|
+
|
|
39
|
+
# @!attribute [r] width
|
|
40
|
+
# @return [Float] internal width of the box
|
|
41
|
+
attribute :width, Types::Length
|
|
42
|
+
|
|
43
|
+
# @!attribute [r] height
|
|
44
|
+
# @return [Float] internal height of the box
|
|
45
|
+
attribute :height, Types::Length
|
|
46
|
+
|
|
47
|
+
# @!attribute [r] depth
|
|
48
|
+
# @return [Float] internal depth of the box
|
|
49
|
+
attribute :depth, Types::Length
|
|
50
|
+
|
|
51
|
+
# @!attribute [r] thickness
|
|
52
|
+
# @return [Float] thickness of the material
|
|
53
|
+
attribute :thickness, Types::Length
|
|
54
|
+
|
|
55
|
+
# @!attribute [r] slope
|
|
56
|
+
# @return [Float, nil] angle of a sloped front above horizontal, in degrees, above 0 and below 90;
|
|
57
|
+
# nil for the box with six panels
|
|
58
|
+
attribute? :slope, Types.optional(Types::Slope)
|
|
59
|
+
|
|
60
|
+
# @!attribute [r] front_height
|
|
61
|
+
# @return [Float, nil] internal height at the front of a sloped box, where the slope begins;
|
|
62
|
+
# required with a slope, and 0 starts the slope at the bottom
|
|
63
|
+
attribute? :front_height, Types.optional(Types::Allowance)
|
|
64
|
+
|
|
65
|
+
# @!attribute [r] notch
|
|
66
|
+
# @return [Float, nil] notch length, a guide only; nil for three times the thickness
|
|
67
|
+
attribute? :notch, Types.optional(Types::Length)
|
|
68
|
+
|
|
69
|
+
# @!attribute [r] kerf
|
|
70
|
+
# @return [Float, nil] width of the cut; nil for the default
|
|
71
|
+
attribute? :kerf, Types.optional(Types::Allowance)
|
|
72
|
+
|
|
73
|
+
# @!attribute [r] units
|
|
74
|
+
# @return [String] "in" or "mm", the units of every distance here
|
|
75
|
+
attribute :units, Types::Units
|
|
76
|
+
|
|
77
|
+
# @!attribute [r] lid
|
|
78
|
+
# @return [String] "full", "back" or "plain": notched on every side, into the back wall only, or on no side.
|
|
79
|
+
# On a sloped box it is the tilted panel: "full" notches it into the side walls, "plain" lays it on them.
|
|
80
|
+
attribute :lid, Types::Lid
|
|
81
|
+
|
|
82
|
+
# @!attribute [r] format
|
|
83
|
+
# @return [String, nil] "pdf" or "svg"; nil for a PDF, or for what the extension of the file says
|
|
84
|
+
attribute? :format, Types.optional(Types::Format)
|
|
85
|
+
|
|
86
|
+
# @!attribute [r] file
|
|
87
|
+
# @return [String, nil] the file {Laser::Cutter.write} writes
|
|
88
|
+
attribute? :file, Types.optional(Types::Coercible::String)
|
|
89
|
+
|
|
90
|
+
# @!attribute [r] margin
|
|
91
|
+
# @return [Float, nil] margin from the edge of the page; nil for the default
|
|
92
|
+
attribute? :margin, Types.optional(Types::Allowance)
|
|
93
|
+
|
|
94
|
+
# @!attribute [r] padding
|
|
95
|
+
# @return [Float, nil] space between the panels; nil for the default
|
|
96
|
+
attribute? :padding, Types.optional(Types::Allowance)
|
|
97
|
+
|
|
98
|
+
# @!attribute [r] stroke
|
|
99
|
+
# @return [Float, nil] stroke width of the lines; nil for the default
|
|
100
|
+
attribute? :stroke, Types.optional(Types::Length)
|
|
101
|
+
|
|
102
|
+
# @!attribute [r] page_size
|
|
103
|
+
# @return [String, nil] a page size such as "A4", for a PDF; nil to fit the box
|
|
104
|
+
attribute? :page_size, Types.optional(Types::PageSize)
|
|
105
|
+
|
|
106
|
+
# @!attribute [r] page_layout
|
|
107
|
+
# @return [String] "portrait" or "landscape", for a PDF
|
|
108
|
+
attribute :page_layout, Types::PageLayout
|
|
109
|
+
|
|
110
|
+
# @!attribute [r] metadata
|
|
111
|
+
# @return [Boolean] whether the settings of the box are printed on the page
|
|
112
|
+
attribute :metadata, Types::Params::Bool.default(true)
|
|
113
|
+
|
|
114
|
+
# @!attribute [r] inside_box
|
|
115
|
+
# @return [Boolean] whether the box is also drawn without kerf, in red
|
|
116
|
+
attribute :inside_box, Types::Params::Bool.default(false)
|
|
117
|
+
|
|
118
|
+
class << self
|
|
119
|
+
# @param attributes [Hash, Options] the options, by name
|
|
120
|
+
# @return [Options]
|
|
121
|
+
# @raise [MissingOption, InvalidOption]
|
|
122
|
+
def new(attributes = {}, *)
|
|
123
|
+
return attributes if attributes.is_a?(self)
|
|
124
|
+
|
|
125
|
+
require_dimensions!(attributes) if attributes.respond_to?(:to_h)
|
|
126
|
+
super.tap { |options| Sloped::Request.validate!(options.to_h) }
|
|
127
|
+
rescue Dry::Struct::Error => e
|
|
128
|
+
raise InvalidOption, explain(e)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
private
|
|
132
|
+
|
|
133
|
+
# @param attributes [#to_h] the options, by name
|
|
134
|
+
# @return [void]
|
|
135
|
+
# @raise [MissingOption] when a dimension in {REQUIRED} is blank
|
|
136
|
+
def require_dimensions!(attributes)
|
|
137
|
+
given = attributes.to_h.transform_keys(&:to_sym)
|
|
138
|
+
missing = REQUIRED.select { |name| Types.blank?(given[name]) }
|
|
139
|
+
return if missing.empty?
|
|
140
|
+
|
|
141
|
+
raise MissingOption, "#{missing.join(', ')} #{missing.size > 1 ? 'are' : 'is'} required, but missing."
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Says which option was refused, and what it may be when that is a short list.
|
|
145
|
+
#
|
|
146
|
+
# @param error [Dry::Struct::Error]
|
|
147
|
+
# @return [String] the message for an {InvalidOption}
|
|
148
|
+
def explain(error)
|
|
149
|
+
cause = error.cause
|
|
150
|
+
return error.message.sub(/\A\[.*?\] /, '') unless cause.respond_to?(:key) && cause.respond_to?(:value)
|
|
151
|
+
|
|
152
|
+
choices = choices_of(schema.key(cause.key).type)
|
|
153
|
+
allowed = choices && choices.size <= MAXIMUM_CHOICES ? ", but must be one of: #{choices.join(', ')}" : ''
|
|
154
|
+
"#{cause.key} cannot be #{cause.value.inspect}#{allowed}."
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# The words an attribute may be, found under whatever wraps its type.
|
|
158
|
+
#
|
|
159
|
+
# @param type [Dry::Types::Type]
|
|
160
|
+
# @return [Array<String>, nil] nil when the attribute is not one of a list
|
|
161
|
+
def choices_of(type)
|
|
162
|
+
return type.values if type.respond_to?(:values)
|
|
163
|
+
|
|
164
|
+
inner = %i[right type].find { |name| type.respond_to?(name) }
|
|
165
|
+
choices_of(type.public_send(inner)) if inner
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# @return [Configuration] the same settings, as the renderers take them
|
|
170
|
+
def to_configuration
|
|
171
|
+
Configuration.new(to_h.except(:format, :inside_box).merge(debug: inside_box))
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
|
+
# Lists the page sizes a PDF can take, in inches or millimetres.
|
|
5
6
|
class PageManager
|
|
7
|
+
# The page sizes Prawn knows, in PDF points, by name.
|
|
6
8
|
SIZES = PDF::Core::PageGeometry::SIZES.clone.freeze
|
|
7
9
|
|
|
10
|
+
# @return [String] "in" or "mm"
|
|
8
11
|
attr_reader :units
|
|
9
12
|
|
|
10
13
|
# @param units [String, Symbol] 'in' or 'mm'; the configuration's default is a Symbol
|
|
@@ -12,6 +15,7 @@ module Laser
|
|
|
12
15
|
@units = units.to_s
|
|
13
16
|
end
|
|
14
17
|
|
|
18
|
+
# @return [String] one line for each page size, with its width and height
|
|
15
19
|
def all_page_sizes
|
|
16
20
|
output = +""
|
|
17
21
|
page_size_values.each do |k|
|
|
@@ -20,8 +24,12 @@ module Laser
|
|
|
20
24
|
output
|
|
21
25
|
end
|
|
22
26
|
|
|
23
|
-
#
|
|
24
|
-
#
|
|
27
|
+
# Converts a length into the units of this page manager.
|
|
28
|
+
#
|
|
29
|
+
# @param value [Numeric] the length
|
|
30
|
+
# @param from_units [String, nil] "in" or "mm"; nil for PDF points,
|
|
31
|
+
# 72 to the inch, which is what Prawn measures in
|
|
32
|
+
# @return [Float]
|
|
25
33
|
def value_from_units(value, from_units = nil)
|
|
26
34
|
multiplier = if from_units.nil?
|
|
27
35
|
if units.eql?('in')
|
|
@@ -39,6 +47,8 @@ module Laser
|
|
|
39
47
|
value.to_f * multiplier
|
|
40
48
|
end
|
|
41
49
|
|
|
50
|
+
# @return [Array<Array(String, Float, Float)>] each page size, sorted by
|
|
51
|
+
# name, with its width and height in {#units}
|
|
42
52
|
def page_size_values
|
|
43
53
|
h = Hashie::Mash.new(SIZES)
|
|
44
54
|
h.keys.sort.map do |k|
|
|
@@ -3,25 +3,47 @@
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
5
|
module Renderer
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
# page_manager contains access to units and page sizes
|
|
6
|
+
# The base of every renderer: it holds the settings and the thing it
|
|
7
|
+
# draws, its subject.
|
|
9
8
|
class Base
|
|
9
|
+
# The color of the lines to cut.
|
|
10
10
|
BLACK = "000000"
|
|
11
|
+
# The color of the settings block.
|
|
11
12
|
BLUE = "0070E0"
|
|
12
13
|
|
|
14
|
+
# @!attribute [rw] config
|
|
15
|
+
# @return [Configuration] the settings
|
|
16
|
+
# @!attribute [rw] subject
|
|
17
|
+
# @return [Object, nil] what is drawn: a box, a line or a rectangle
|
|
18
|
+
# @!attribute [rw] enclosure
|
|
19
|
+
# @return [Geometry::Rect, nil] the rectangle around what is drawn
|
|
20
|
+
# @!attribute [rw] page_manager
|
|
21
|
+
# @return [PageManager] converts PDF points into the units of the settings
|
|
13
22
|
attr_accessor :config, :subject, :enclosure, :page_manager
|
|
14
23
|
|
|
24
|
+
# @param config [Configuration]
|
|
25
|
+
# @param subject [Object, nil] what to draw
|
|
15
26
|
def initialize(config, subject = nil)
|
|
16
27
|
self.config = config
|
|
17
28
|
self.subject = subject
|
|
18
29
|
self.page_manager = Laser::Cutter::PageManager.new(config.units)
|
|
19
30
|
end
|
|
20
31
|
|
|
21
|
-
|
|
32
|
+
# Writes the document to config.file, yielding after each line drawn.
|
|
33
|
+
#
|
|
34
|
+
# @yieldparam line [Geometry::Line]
|
|
35
|
+
# @return [Integer] the number of bytes written
|
|
36
|
+
def render(&)
|
|
37
|
+
File.binwrite(config.file, document(&))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @return [String] the document, yielding after each line drawn
|
|
41
|
+
# @raise [RuntimeError] when the renderer does not implement it
|
|
42
|
+
def document
|
|
22
43
|
raise 'Abstract method'
|
|
23
44
|
end
|
|
24
45
|
|
|
46
|
+
# @return [Symbol] :in or :mm
|
|
25
47
|
def units
|
|
26
48
|
config.units.to_sym
|
|
27
49
|
end
|
|
@@ -3,19 +3,28 @@
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
5
|
module Renderer
|
|
6
|
+
# Draws the lines of a box onto a Prawn document.
|
|
6
7
|
class BoxRenderer < Base
|
|
8
|
+
# @return [Box, Sloped::Box] the box to draw
|
|
7
9
|
alias_method :box, :subject
|
|
8
10
|
|
|
11
|
+
# @param config [Configuration] the box is built from it by {BoxFactory}
|
|
9
12
|
def initialize(config)
|
|
10
13
|
super
|
|
11
|
-
self.subject = Laser::Cutter::
|
|
14
|
+
self.subject = Laser::Cutter::BoxFactory.for(config)
|
|
12
15
|
end
|
|
13
16
|
|
|
17
|
+
# Keeps room on the page for a rectangle at the bottom left, such as
|
|
18
|
+
# the settings block.
|
|
19
|
+
#
|
|
20
|
+
# @param rect [Geometry::Rect] in PDF points
|
|
21
|
+
# @return [Geometry::Point] its top right corner, in the units of the box
|
|
14
22
|
def ensure_space_for(rect)
|
|
15
23
|
coords = [rect.p2.x, rect.p2.y].map{ |a| page_manager.value_from_units(a) }
|
|
16
24
|
box.metadata = Geometry::Point.new(coords)
|
|
17
25
|
end
|
|
18
26
|
|
|
27
|
+
# @return [Geometry::Rect] the rectangle around every line of the box
|
|
19
28
|
def enclosure
|
|
20
29
|
box.enclosure
|
|
21
30
|
end
|
|
@@ -26,6 +35,10 @@ module Laser
|
|
|
26
35
|
end
|
|
27
36
|
|
|
28
37
|
# Draws the box, yielding after each line so a caller can count them.
|
|
38
|
+
#
|
|
39
|
+
# @param pdf [Prawn::Document]
|
|
40
|
+
# @yieldparam line [Geometry::Line]
|
|
41
|
+
# @return [Array<Geometry::Line>] the lines
|
|
29
42
|
def render(pdf = nil)
|
|
30
43
|
pdf.line_width = config.stroke.send(units)
|
|
31
44
|
pdf.stroke_color config[:color] || BLACK
|
|
@@ -6,8 +6,10 @@ module Laser
|
|
|
6
6
|
# Writes the PDF: the box, its metadata block, and the unkerfed outline
|
|
7
7
|
# when asked for.
|
|
8
8
|
class LayoutRenderer < Base
|
|
9
|
+
# The color of the box drawn without kerf, under config.debug.
|
|
9
10
|
UNKERFED_COLOR = 'DD2211'
|
|
10
11
|
|
|
12
|
+
# @param config [Configuration]
|
|
11
13
|
def initialize(config)
|
|
12
14
|
self.config = config
|
|
13
15
|
super
|
|
@@ -18,8 +20,8 @@ module Laser
|
|
|
18
20
|
box_renderers.sum { |renderer| renderer.lines.size }
|
|
19
21
|
end
|
|
20
22
|
|
|
21
|
-
#
|
|
22
|
-
def
|
|
23
|
+
# @return [String] the PDF, yielding after each line drawn
|
|
24
|
+
def document(&)
|
|
23
25
|
margin = config.margin.to_f.send(units)
|
|
24
26
|
pdf = Prawn::Document.new(margin: margin,
|
|
25
27
|
page_size: config.page_size || calculate_image_boundary(box_renderers.first, margin),
|
|
@@ -28,9 +30,14 @@ module Laser
|
|
|
28
30
|
box_renderers.first.render(pdf, &)
|
|
29
31
|
meta_renderer&.render(pdf)
|
|
30
32
|
box_renderers.drop(1).each { |renderer| renderer.render(pdf, &) }
|
|
31
|
-
pdf.
|
|
33
|
+
pdf.render
|
|
32
34
|
end
|
|
33
35
|
|
|
36
|
+
# The page size that fits the box.
|
|
37
|
+
#
|
|
38
|
+
# @param box_renderer [BoxRenderer]
|
|
39
|
+
# @param margin [Float] in PDF points
|
|
40
|
+
# @return [Array(Float, Float)] the width and height, in PDF points
|
|
34
41
|
def calculate_image_boundary(box_renderer, margin)
|
|
35
42
|
box_renderer.enclosure.to_a[1].map do |c|
|
|
36
43
|
c.send(units) + (2 * margin)
|
|
@@ -39,11 +46,14 @@ module Laser
|
|
|
39
46
|
|
|
40
47
|
private
|
|
41
48
|
|
|
49
|
+
# @return [MetaRenderer, nil] nil unless config.metadata is set
|
|
42
50
|
def meta_renderer
|
|
43
51
|
@meta_renderer ||= MetaRenderer.new(config) if config.metadata
|
|
44
52
|
end
|
|
45
53
|
|
|
46
54
|
# The box, then the same box without kerf when config.debug is set.
|
|
55
|
+
#
|
|
56
|
+
# @return [Array<BoxRenderer>]
|
|
47
57
|
def box_renderers
|
|
48
58
|
@box_renderers ||= begin
|
|
49
59
|
configs = [config]
|
|
@@ -3,8 +3,13 @@
|
|
|
3
3
|
module Laser
|
|
4
4
|
module Cutter
|
|
5
5
|
module Renderer
|
|
6
|
+
# Strokes one line onto a Prawn document.
|
|
6
7
|
class LineRenderer < Base
|
|
8
|
+
# @return [Geometry::Line] the line to draw
|
|
7
9
|
alias_method :line, :subject
|
|
10
|
+
|
|
11
|
+
# @param pdf [Prawn::Document]
|
|
12
|
+
# @return [void]
|
|
8
13
|
def render(pdf = nil)
|
|
9
14
|
pdf.stroke {
|
|
10
15
|
pdf.line([line.p1.x, line.p1.y].map{ |p| p.send(units) },
|
|
@@ -12,6 +17,7 @@ module Laser
|
|
|
12
17
|
}
|
|
13
18
|
end
|
|
14
19
|
|
|
20
|
+
# @return [Geometry::Line] the line itself
|
|
15
21
|
def enclosure
|
|
16
22
|
line
|
|
17
23
|
end
|