Gem Version coverage documentation

LaserCutter (and the free website makeabox.io powered by it)

laser-cutter is a ruby library for generating PDF designs for boxes of custom dimensions that suit your project, that can be cut from wood or acrylic using a laser-cutter. The sides of the box snap together using alternating notches, that are deliberately layed out in a symmetric form.

An assembled box, 5 in wide, 3 in deep and 4 in tall, its six panels joined by notches

A box 5 in wide, 3 in deep and 4 in tall, assembled from the six panels laser-cutter draws.

To use laser-cutter you need to have a recent version of ruby interpreter, install it as a gem, and use command line to generate PDFs.

Make-A-Box is a online web application that uses laser-cutter library and provides a straight-forward user interface for generating PDF designs without the need to install the gem or use command line.

Use whatever suites you better.

Design Goals

One of the design goals of this project is to provide a highly extensible platform for creating laser-cut designs, where alternative strategies can be added over time, and supported by various command line options, and perhaps a light weight web application. If you are interested in contributing to the project, please see contributing for more details.

laser-cutter supports many flexible command line options that allow setting dimensions, stroke width, page size, layout, margins, padding (spacing between the boxes), and many more.

Version 3.0 adds a sloped front: a seven-panel enclosure whose front leans back, to hold a screen or a control panel.

Dependencies

The gem depends primarily on Prawn – a fantastic PDF generation library. SVG output uses Victor, and the command line is built on dry-cli. It needs Ruby 4.0 or newer.

Installation

Add this line to your application's Gemfile:

gem 'laser-cutter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install laser-cutter

Usage

The gem installs two executables that run the same program: laser-cutter, and its shortcut lc. The examples below use lc.

lc COMMAND [OPTIONS]

  generate, g    Draw the panels of a box into a PDF or an SVG file
  page-sizes     List every page size, with its dimensions
  examples       Show detailed usage examples
  help           Show help, for the program or for one command
  version        Print the version
  completion     Print a bash or zsh completion script

lc help generate lists every option:

Option Meaning Default
-b, --box WxHxD/T[/N]: width, height, depth, thickness and optional notch, in one expression
-w, --width Internal width of the box
-H, --height Internal height of the box
-d, --depth Internal depth of the box
-t, --thickness Thickness of the material
-S, --slope Angle of a sloped panel above horizontal, in degrees, for mounting a screen. Requires --front-height, see below
-F, --front-height Internal height at the front of a sloped box, where the slope begins
-L, --lid full (notched on every side), back (into the back wall only, not with --slope) or plain (no notches), see below full
-n, --notch Notch length, a guide only 3 × thickness
-k, --kerf Kerf, the width of the cut 0.0024in
-u, --units Units every dimension is in: in or mm in
-o, --file File to write required
-f, --format pdf or svg, in either case pdf
-m, --margin Margin from the edge of the page
-p, --padding Space between the panels
-s, --stroke Stroke width of the lines
-i, --page-size Page size, such as LETTER or A3; PDF only fit the box
-l, --page-layout portrait or landscape; PDF only portrait
-M, --[no-]metadata Print the settings of the box on the page true
-B, --[no-]inside-box Also draw the box without kerf, in red, to check the kerf false
-O, --[no-]open Open the file once it is written false
-W, --write Save the configuration to a file, or to STDOUT with -
-R, --read Read the configuration from a file, or from STDIN with -
-v, --[no-]verbose Print the configuration, and the full backtrace on failure false
-h, --help Show help

Height is -H, because -h prints help.

Examples

A box in inches, with the kerf set to 0.008", opened once it is written:

lc generate -b 3x2x2/0.125 -k 0.008 -O -o box.pdf

g is short for generate, and the rest of the examples use it.

A box with a lid that lifts off:

lc g -b 3x2x2/0.125 --lid plain -o box.pdf

The lid

The lid is the top panel. --lid sets how it joins the walls:

--lid The lid The walls under it
full Notched on all four sides Notched; the box is glued shut
back Notched where it meets the back wall The back is notched, the other three are straight
plain A rectangle, no notches All four are straight
--lid full --lid back --lid plain
A box with a full lid A box with a lid notched at the back A box with a plain lid

Each is lc g -b 3x2x2/0.125 --lid …. The lid is the panel at the bottom of the page, the back wall the one at the top.

A lid edge without notches reaches the outside of the wall under it, and that wall ends at the internal height. So a plain lid is W + 2T by D + 2T and lies on top of the box, and the space inside is still W by H by D.

A sloped front

--slope draws a console: a box whose front leans back, the kind that holds a display tilted towards the reader. --front-height is where the slope begins.

An assembled sloped box, 5 in wide, 3 in deep and 4 in tall, with a 2.1 in front and a 45 degree slope

Assembled: 5 in wide, 3 in deep, 4 in tall at the back, with a 2.1 in front and the tilted panel at 45°. Mount a display or a control panel on the tilted panel, and it faces the reader.

The panels to cut:

lc g -b 4x3x5/0.125 --slope 45 --front-height 1 -o console.pdf

A box whose front slopes back at 45 degrees

Seen from the side, with H the height at the back and D the depth:

        top strip
      +-----------+
     /            |
    /  tilted     | back, H
   +              |
   | front        |
   +--------------+
      bottom, D
  • Seven panels: two side walls, the bottom, the back, and the top strip, the tilted panel and the front strip, drawn in that order up the page from the back.
  • --slope is the angle of the tilted panel above horizontal, above 0 and below 90. The top strip takes the depth the slope leaves: D - (H - front height) / tan(slope).
  • A strip of no size is dropped: --front-height 0 starts the slope at the bottom, and a slope that reaches the back leaves no top strip.
  • The tilted panel meets the strips in a plain butt joint, to be glued. Every other joint is notched.
  • --lid plain makes the tilted panel a lid: a rectangle that lies on straight side walls and lifts off. --lid back does not apply.
  • Numbers that do not make the shape are refused, with the reason: a slope too shallow for the depth, or strips that leave no room for a tilted panel.

The same box as an SVG:

lc g -b 3x2x2/0.125 -f svg -o box.svg

A box in millimeters on a landscape A3 page, with a 0.5mm stroke:

lc g -u mm -w 70 -H 20 -d 50 -t 4.3 -n 5 -i A3 -l landscape -s 0.5 -o box.pdf

Every page size, in millimeters:

lc page-sizes -u mm

Save the settings of a box, and use them again:

lc g -b 1.1x2.5x1.5/0.125/0.125 -p 0.1 -o box.pdf -W box-settings.json
lc g -o box.pdf -R box-settings.json
cat box-settings.json | lc g -o box.pdf -R -

More boxes

A shallow box in millimeters, glued shut:

lc g -u mm -w 70 -H 20 -d 50 -t 4.3 -n 5 -f svg -o box.svg

A 70 by 20 by 50 millimeter box

A tray with a lid that lifts off, from 3mm material with wide notches:

lc g -u mm -w 120 -H 25 -d 80 -t 3 -n 12 --lid plain -f svg -o tray.svg

A 120 by 25 by 80 millimeter tray with a plain lid

Storage boxes

No special folding option is needed for notch-joined storage boxes: cut the separate panels and assemble them. These designs do not use hinges or fold flat as a single sheet. Use --lid plain for a removable lid, or leave that panel out when cutting to make an open tray.

For a 3" square footprint and 3" height, or a 9" square footprint and 4" height, using 1/8" material:

lc g -u in -b 3x3x3/0.125 --lid plain -o storage-small.pdf
lc g -u in -b 9x4x9/0.125 --lid plain -o storage-large.pdf

The shorthand is width × height × depth, and dimensions describe the inside space. To nest one assembled box inside another, allow clearance for the smaller box's outside dimensions, including the material thickness; sizes are not adjusted for nesting automatically.

The pictures on this page are the SVG files themselves, drawn with a thicker stroke (-s 0.5) and converted:

magick -density 96 -background white box.svg -flatten box.avif

Using it from Ruby

The API documentation is at kigster.github.io/laser-cutter, built from the YARD comments with rake doc.

A Ruby program draws a box in its own process; nothing runs the command line. It needs three things:

Call What it does
Laser::Cutter::Options.new Every setting of a box, typed and checked
Laser::Cutter.render(options) Returns the PDF or the SVG as a String
Laser::Cutter.write(options) Writes it to options.file, and returns the path
require "laser-cutter"

options = Laser::Cutter::Options.new(
  width: 70, height: 20, depth: 50, thickness: 4.3,
  units: :mm, lid: :plain, format: :svg
)

svg = Laser::Cutter.render(options)                      # a String, no file written
Laser::Cutter.write(options.new(file: "box.pdf", format: nil)) # the extension picks the format

Options has an attribute for each option of generate:

Attribute Type When left out
width, height, depth, thickness Float above zero MissingOption is raised
notch Float above zero Three times the thickness
kerf, margin, padding Float, zero or more The default for the units
stroke Float above zero The default for the units
units in or mm in
lid full, back, plain full
slope Float, above 0, below 90 The box has six panels
front_height Float, zero or more Required with slope
format pdf or svg pdf, or the file's extension
file String Only write needs it
page_size A name such as A4 The page fits the box
page_layout portrait, landscape portrait
metadata Boolean true
inside_box Boolean false
  • Values are coerced, so the Strings a web form sends will do, and a blank String counts as left out. Keys may be Strings or Symbols.
  • A value it cannot use, or a key that is not an option, raises Laser::Cutter::InvalidOption with a message such as lid cannot be "sliding", but must be one of: full, back, plain. Both errors descend from Laser::Cutter::Error.
  • An Options cannot be changed; options.new(lid: :back) returns a changed copy.

In a Rails controller:

def create
  box = params.require(:box).permit(*Laser::Cutter::Options.attribute_names)
  send_data Laser::Cutter.render(box.to_h), type: "application/pdf", filename: "box.pdf"
rescue Laser::Cutter::Error => e
  redirect_to new_box_path, alert: e.message
end

Laser::Cutter::Box::LIDS lists the lids, for a select, and Laser::Cutter::Sloped::Box::LIDS those of a sloped box. Configuration, Renderer::LayoutRenderer#render and PageManager#page_size_values, which MakeABox.io called in 1.0.3, still work.

Drawing a PDF or an SVG

render and write share one path. A slope picks the box, and the format picks the renderer. SlopedBox in the diagram is Sloped::Box.

---
config:
  layout: elk
  look: neo
  fontFamily: '''Open Sans Variable'', sans-serif'
  theme: redux-color
  htmlLabels: true
  themeVariables:
    fontFamily: '''Open Sans Variable'', sans-serif'
---
classDiagram
  direction TB
  class Cutter {
    +render(options) String
    +write(options) String
  }
  class Options
  class Configuration
  class Renderer {
    +for(format, config)
  }
  class LayoutRenderer
  class SvgRenderer
  class BoxFactory {
    +for(config)
  }
  class Box
  class SlopedBox

  Cutter --> Options
  Options --> Configuration
  Cutter --> Renderer
  Configuration --> Renderer
  Renderer ..> LayoutRenderer : pdf
  Renderer ..> SvgRenderer : svg
  LayoutRenderer --> BoxFactory
  SvgRenderer --> BoxFactory
  BoxFactory --> Box : no slope
  BoxFactory --> SlopedBox : slope
---
config:
  layout: elk
  look: neo
  fontFamily: '''Open Sans Variable'', sans-serif'
  theme: redux-color
  htmlLabels: true
  themeVariables:
    fontFamily: '''Open Sans Variable'', sans-serif'
---
stateDiagram-v2
  [*] --> Options
  Options --> Checked : validate
  Checked --> Configuration
  Configuration --> BoxKind
  state BoxKind <<choice>>
  BoxKind --> Straight : no slope
  BoxKind --> Sloped : slope
  Straight --> Notched : generate notches
  Sloped --> Notched : generate notches
  Notched --> Format
  state Format <<choice>>
  Format --> PDF : pdf
  Format --> SVG : svg
  PDF --> Document
  SVG --> Document
  Document --> [*] : render
  Document --> Written : write
  Written --> [*]

Feature Wish List

  • Create T-style joins, using various standard sizes of nuts and bolts (such as common #4-40 and M2 sizes)
  • Extensibility with various layout strategies, notch drawing strategies, basically plug and play model for adding new algorithms for path creation and box joining
  • Support more shapes than just box, such as prisms
  • Supporting lids and front panels, that are larger than the box itself and have holes for notches.
  • Your brilliant idea can be here too! Please see contributing for more info.

LaserCutter vs BoxMaker

Rahulbot-made BoxMaker is a functional generator of notched designs, similar to laser-cutter, and generously open sourced by the author, and so in no way this project disputes BoxMaker's viability. In fact BoxMaker was an inspiration for this project.

Laser-Cutter library attempts to further advance the concept of programmatically creating laser-cut box designs, provides additional fine tuning, many more options, strategies and most importantly – extensibility.

Unlike BoxMaker, this gem has a suit of automated tests (rspecs) around the core functionality. In addition, new feature contributions are highly encouraged, and in that regard having existing test suit offers confidence against regressions, and thus welcomes colaboration.

Finally, BoxMaker's notch-drawing algorithm generates non-symmetric and sometimes purely broken designs (see picture below).

laser-cutter's algorithm will create a symmetric design for most panels, but it might sacrifice identical notch length. Depending on the box dimensions you may end up with a slightly different notch length on each side of the box.

The choice ultimately comes down to the preference and feature set, so here I show you two boxes made with each program, so you can pick what you prefer.

Example Outputs

Below are two examples of boxes with identical dimensions produced with laser-cutter and boxmaker:

This is how you would make a box with Adam Phelp's fork of BoxMaker (which adds flags and a lot of niceties):

git clone https://github.com/aphelps/boxmaker && cd boxmaker && ant
java -cp BOX.jar com.rahulbotics.boxmaker.BoxMaker \
      -W 1 -H 2 -D 1.5 -T 0.125 -n 0.125 -o box.pdf

And laser-cutter:

gem install laser-cutter
lc g -b 1x1.5x2/0.125/0.125 -O -o box.pdf

LaserCutter Comparison.

Contributing to laser-cutter

The justfile runs the development commands. Ruby recipes load rbenv and then bundle exec, so they use the Ruby pinned in .ruby-version. just with no arguments opens a picker. just --list prints every recipe.

Command What it does
just lint Run RuboCop
just format Autocorrect Ruby, then format every Markdown file. Extra arguments go to RuboCop. -A allows unsafe corrections
just format-markdown Format every Markdown file
just test Run RSpec. A path limits the run: just test spec/laser/cutter/edge_spec.rb:42
just test-docs Run RSpec with the documentation formatter
just test-coverage Run RSpec with coverage on, including a partial run
just ci Lint, then run the coverage suite. just check-all is the same command
just lefthook Run the pre-commit hooks against every file
just clean Remove .DS_Store files, tmp/, and coverage/
just clobber Remove generated files (rake clobber)
just version Print the gem version
just doc Build the YARD documentation into doc/
just build Build the gem into pkg/
just changelog Write CHANGELOG.md from the GitHub releases. Requires CHANGELOG_GITHUB_TOKEN
just publish Build the gem and push it to RubyGems. just publish 123456 passes that two-factor code
just release Point the version tag at the current commit, force-push tags, and create the GitHub release

The fork and pull request steps are in CONTRIBUTING.md.

License

MIT License (MIT). Please see LICENSE for more information.

Author: © 2015-2024 Konstantin Gredeskoul @kigster