OpenSCAD Parametric Packaging Script v2

by M_G, published

OpenSCAD Parametric Packaging Script v2 by M_G Mar 25, 2013
3 Share
Download All Files

Thing Apps Enabled

Order This Printed View All Apps


Use This Project

Give a Shout Out

If you print this Thing and display it in public proudly give attribution by printing and displaying this tag.

Print Thing Tag


Thing Statistics

7856Views 4541Downloads Found in Electronics


This OpenSCAD script will generate a 2 part fit together packaging for a device of user selectable size. Any amount of various shaped cutouts, support posts, engraved text and logos can be easily placed on any face using special array variables. Check out the instructions below as well as the examples and comments in the code for more information on how to use them.

Author: Mark Gaffney
Version: 2.6c (versions d e & f are slight modifications for a specific item without proper controls but these will be included in v2.7)
Date: 2013-04-04

add user controllable flanges and interlocking lip features
Some combinations of values, shapes and locations etc. may lead to an invalid or non-manifold shape beign created, preventing you from exporting a .stl
If this happens try to remove or change some of the features or values until you get a valid shape and then add them back gradually until you find the offending item, then change it so it doesn't cause a problem

Note: This thing uses  HarlanDMii's"write.scad" module http://www.thingiverse.com/thing:16193  
which you will need to have downloaded and located in the directory if you want to run this on your PC  
    The version I had did not properly implement spacing on 'writecube', a modified version that implements this is included here...  

Generating text can be very slow!  

When generating a stacked layout it may look like the top is taller than it should be, this seems to be a visualisation bug in OpenSCAD, if you create a .STL it will be perfect  

Changes from previous versions:


  • Implemented ability to choose box shape i.e. "cuboid","rounded4sides", "rounded6sides", "chamfered6sides"
  • Ensured "box_type" module is used in generation of each box_half
  • investigated and fixed some top lip mis alignment
  • implements loading external objects (such as .stl files) to place on faces
  • implemented overlooked translation of text on faces from previous version
  • fixed box_t in make_cutouts
    v2.5 (AKA cutouts_on_faces_for_loop-v0_2.scad)
  • Implement tolerancing of connection between 2 halves using lip_fit
  • implement text on faces
  • Complete documentation
    v2.4 (AKA cutouts_on_faces_for_loop-v0_4.scad)
  • user provides device dimensions and internal clearance(s), packaging dimensions are calculated based on this
  • preparations to allow for different wall_t in x,y,z
  • implement posts
  • mouse ears work properly
  • 2 halves in "beside" layout joined by union

v2.3 2013-03-23 (AKA cutouts_on_faces_for_loop-v0_3.scad)

  • fixed calls to make_box and half_box modules
  • added ability to handle "holes" array for making cutouts to make_box and half_box modules
  • added module for rounded cuboid
  • fixed errors on rounded cuboid cutouts's use of a_bit
  • fixed translate errors for making lips on box
  • ensured all parts generate on z=0
  • gave top (greenish) and bottom (reddish) different colours for ease of identification
  • rotated top in "beside" layout to more intuitive orientation
  • added box cross module and included in hull calculations to ensure box sizes are exact even with low side count
  • added box_type module to allow choice of different shaped boxes or automatically swap them based on the current variables
  • added mouse_ears module
    Note: In stacked mode you may notice the red half looks like it is the full length, this appears to be a visualisation bug in openscad, the part is generated correctly

v2.2 2013-03-22 (AKA cutouts_on_faces_for_loop-v0_2.scad)
based on My own structure

  • New format [face_name, shape_name, shape_position[x_pos,y_pos,x_offs,y_offs,rotate,align], shape_size[depth,,,]]
    • face_name ("N", "S", "E", "W", "T", "B")
    • shape_name ("Square", "Rectangle" , "Round_Rect", "Cylinder", "Ellipse" , "Cone")
    • shape_position[x_pos,y_pos,x_offs,y_offs,rotate,align=("inside" or "outside")]
      • "Square" shape_size[depth, length_breadth]
      • "Rectangle" shape_size[depth, length, breadth]
      • "Round_Rect" shape_size[depth, length, breadth, corner_radius, corner_sides]
      • "Cylinder" shape_size[depth, radius ,sides]
      • "Ellipse" shape_size[depth, radius_length, radius_breadth, sides]
      • "Cone" shape_size[[depth, radius_bottom, radius_top ,sides]
  • pos_x and pos_y are chosen to align with views taken from the North, Top or East faces towards the origin so that they are aligned with [-x,+z], [+x,+y] & [+y,+z] respectively
  • rotation is clockwise about the plane of the North, Top or East faces as lookign towards the origin this means they are anticlockwise for the opposite faces(i.e. same convention as above)
  • these 2 conventiosn are chosen to make it easier to position cutouts that align with oppsite sides. e.g. a box that fits around a rotated ellipse pipe

v2.1 2013-03-22 (AKA cutouts_on_faces_for_loop-v0_1.scad)

  • based on kitlaan's array structure, supporting cones and rectangles
  • from kitlaan's Customisable Electronic Device Packaging http://www.thingiverse.com/thing:8607
    • rect [ x-offset, y-offset, x-width, y-width, corner-radius, corner-sides, depth, rotate ]
    • cylinder [ x-offset, y-offset, inner-radius, outer-radius, sides, depth, rotate ]


This starts with the user entering some basic values:

  1. Dimensions of the object to be packaged in XYZ (device_xyz, [x,y,z])
  2. Size of the gap between each side of the object and the internal wall of the packaging (clearance_xyz, [x,y,z])
  3. How thick the material of the packaging is (wall_t)
  4. The external radius of the rounded corners of the packaging (corner_radius)
  5. How many sides do these rounded corners have? (corner_sides, 3 to 10 are good for most items, 1 will give you chamfered edges)
  6. How high is the lip that connects the 2 halves? (lip_h, 1.5 to 3 are good for most applications)
  7. How tall is the top relative to the bottom? (top_bottom_ratio, 0.1 to 0.9, 0.5 will give you 2 halves of equal height)
  8. Does the part have mouse ears or not? (has_mouseears, true or false)
  9. How thick the flat discs that make the mosue ears should be (mouse_ear_thickness, twice print layer thickness is a good idea)
  10. How large the flat discs that make the mouse ears are (mouse_ear_radius, 5 to 15 generally work well)
  11. What parts you want and how they are laid out (layout, [beside, stacked, top, bottom])
  12. How far apart the 2 halves are in the "beside" layout (separation, 2 is good)
  13. How much of an overlap (-ve) or gap (+ve) is there between the inner and outer lip surfaces (lip_fit, a value of 0 implies they meet perfectly in the middle, this will depend on your material printer etc. so you will likely need to play around with this variable)

Next the user can modify the data structures that create holes, posts and text on each face of the packaging. These are complicated and may require you te play around with values or read some of the comments further down in the code

  1. The cutouts and depressions used on the packaging (holes=[[]])
    format for each hole [face_name, shape_name, shape_position[x_pos,y_pos,x_offs,y_offs,rotate,align], shape_size[depth,,,]]
  2. The internal supporting structures used on the packaging (posts=[[]])
    format for each post [face_name, shape_name shape_position[x_pos,y_pos,x_offs,y_offs,rotate,align], shape_size[depth,,,]]
  3. The engraved text used on the packaging (text=[[]])
    format for each piece of text [face_name, text_to_write, shape_position[x_pos,y_pos,x_offs,y_offs,rotate,align], shape_size[depth,font_height,font_spacing,mirror]] Note: for silly reasons mirror must be 0 or 1 corresponding to false and true in this version

    Which of the 6 sides of the packaging do you want this feature on? (face_name, T,B,N,E,W,S)
    "T", The Top or Z+ face
    "B", The Bottom or Z- face
    "N", the North or Y+ face
    "E", the East or X+ face
    "W", the West or X- face
    "S", the South or Y- Face
    Where on the face do you want this feature (shape_position [x_pos,y_pos,x_offs,y_offs,rotate,align] )
    x_pos, how far along the face do you move in X
    y_pos, how far along the face do you move in Y
    x_offs, where along the face do you take measurements from in X
    y_offs, where along the face do you take measurements from in Y
    rotate, how much do you want the object rotated in degrees
    if you do not use any of the above please set them to 0! do not just leave them empty!
    align, do you want the object aligned with the "inside" or the "outside" of the packaging face

    What shape do you want? (shape_name, Cone/Ellipse/Cylinder/Round_Rect/Square/Rectangle/Nub_Post/Dip_Post/Hollow_Cylinder)
    What are the shape's dimensions (shape_size[depth,,,])...you will need to read the section below as they are different for each shape
    "Square" shape_size[depth, length_breadth]
    "Rectangle" shape_size[depth, length, breadth]
    "Round_Rect" shape_size[depth, length, breadth, corner_radius, corner_sides]
    "Cylinder" shape_size[depth, radius ,sides]
    "Ellipse" shape_size[depth, radius_length, radius_breadth, sides]
    "Cone" shape_size[depth, radius_bottom, radius_top ,sides]
    "Nub_Post" shape_size[depth, radius_bottom, radius_top, depth_nub, sides]
    "Dip_Post" shape_size[depth, radius_bottom, radius_top, depth_dip, sides]
    "Hollow_Cylinder" shape_size[depth, radius_outside, radius_inside ,sides]
    A string of text you want to have carved into a face (text_to_write)
    for text shape_size[depth,font_height,font_spacing,mirror]

Once the user has provided this data the shape is made as follows:

  1. Calculate external size of packaging based on "device_xyz", "clearance_xyz" & "wall_t"
  2. Create Hollow cuboidal shape by differencing a slightly smaller one from a slightly larger one
  3. From "posts" create internal support structures and union that with the previous shape
  4. From "holes" create shapes on each face and difference that from the previous shape
  5. From "text" create letters on each face and difference that from the previous shape
  6. Using the packaging height, "lip_h", "top_bottom_ratio" & "lip_fit" split teh previous shape into a top and bottom half
  7. Using layout & separation arrange the parts as specified by the user
  8. Using layout, mouse_ears, mouse_ear_thickness & mouse_ear_radius union the mouse ears to the previous shapes correctly
  9. Done!

More from Electronics

view more

File Name



All Apps

3D Print your file with 3D Hubs, the world’s largest online marketplace for 3D printing services.

App Info Launch App

Auto-magically prepare your 3D models for 3D printing. A cloud based 3D models Preparing and Healing solution for 3D Printing, MakePrintable provides features for model repairing, wall thickness...

App Info Launch App

Kiri:Moto is an integrated cloud-based slicer and tool-path generator for 3D Printing, CAM / CNC and Laser cutting. *** 3D printing mode provides model slicing and GCode output using built-in...

App Info Launch App
KiriMoto Thing App

With 3D Slash, you can edit 3d models like a stonecutter. A unique interface: as fun as a building game! The perfect tool for non-designers and children to create in 3D.

App Info Launch App

very nice thing. one thing missing is some way to use screws to close the box (either nut trap slots on sides or top 'thru' style screws)

Thanks! The code should include an option to make hollow and recessed hollow posts on one side for the screw head and solid receiving posts on the other side for the screw thread to bite into. You can also combine this with the ability to make square or hexagonal (actually circular but with number of sides set to 6) cutouts that could be used to hold nuts inserted sideways or along the bolt path respectively. Best of luck.

Is there a way to only have mouse ears on the bottom part of the box? I'm new to openscad... Thanks!

The mouse ears exist to help the parts stick to your build platform without the corners curling up. Currently there is no way to generate both parts at once in a manner suitable for printing where only the bottom part would have mouse ears as this kinda defeats the purpose of having them! Either you will need them for your printer and plastic combination or you wont, I can't see a reason why the bottom would need them but the top wouldn't!

Even so... you COULD generate both parts attached to each other by setting layout="stacked", setting has_mouseears=true would them give you mouse ears on only the bottom part... but this is not really a good idea as if you tried to print it you would probably have a lot of support material inside the enclosure and the top and bottom would probably be fused together which would make inserting whatever you want to enclose pretty much impossible!

ALSO you could generate the top (layout="top", has_mouseears=false) and bottom (layout="bottom", has_mouseears=true) separately and combining them into the one build plate... however again I don't see why you would want this!

FINALLY you could go into the code (search for |module half_box") and change the line has_mouse_ears=mouse_ears[0]; to something like...

if (which_half=="bottom"){has_mouse_ears=true;}else{has_mouse_ears=false;}

Nice work!! thanks a BUNCH!

M_G - in reply to Ktronik

You're very welcome. Show us all what you made please!