// views/blog.ejs Blog - Tronkits

🧰 Creating a Simple Box in OpenSCAD

This tutorial shows how to design a basic, parametric box using OpenSCAD. It's perfect for 3D printing or as a base for more complex designs.

🔄 Set Up Your Parameters

We define variables for the box's size and wall thickness so we can easily tweak it later:


w = 25;     // Outer width of the box
d = 15;     // Outer depth/length of the box
h = 10;     // Outer height of the box
wall = 2;   // Wall thickness
Statements must end with a ";" (semi-colon)

Create the outer shell

cube([width, length, height]);
If all sides are the same you could do cube(size);
You don't need the brackets for a single value.

Hollow out the middle


difference(){
    cube([w, d, h]);
    translate([wall, wall, wall])
    cube([w-2*wall, d-2*wall, h]);
}
difference() removes the second object from the first.
translate() moves the second object to the specified coordinates.
The first cube is the outer shell, and the second cube is the hollow part.

Create a module


module box() {
    difference(){
        cube([w, d, h]);
        translate([wall, wall, wall])
        cube([w-2*wall, d-2*wall, h]);
    }
}
module acts like creating a function.
You must call the module for it to run "box();".
You can also create and pass parameters to modules.

Create a lid


  module lid() {
    cube([w, d, wall]);
    translate([wall, wall, wall])
    cube([w-2*wall, d-2*wall, wall]);
}
lid();
You can create a lid by using the same method as the box.
This is a cube on top of a cube.
The smaller cube fits inside the box. It will be a tight fit.

Final Code


  // Simple Box Design
  w       = 25;
  d       = 15;
  h       = 10;
  wall    = 2;
  
  // Design the box
  module box() {
      difference(){
          cube([w, d, h]);
          translate([wall, wall, wall])
          cube([w-2*wall, d-2*wall, h]);
      }
  }
  
  // Design the lid
  module lid() {
      cube([w, d, wall]);
      translate([wall, wall, wall])
      cube([w-2*wall, d-2*wall, wall]);
  }
  
  
  box(); // Call the box
  translate([w+5, 0, 0]){ // move lid to side
  lid(); }// Call the lid

That's it! You've created a simple box in OpenSCAD. You can adjust the parameters to create different sizes and shapes.

Happy designing!