Monday, May 14, 2012

Print a 3D Key Fob from an Image or Logo

I've also published this as an Instructable and uploaded the related files to Thingiverse.

Print a 3D Key Fob from an Image or LogoLet's say, hypothetically, that you need to design a 3D printable key fob for an event this weekend. First you check Instructables and Thingiverse to see if anyone has done it already. Unfortunately no one has, so you come back and read this Instructable. Don't worry, though, it's not that difficult. This basically involves converting a black and white image to vector format and extruding it into a 3D shape.

Through the process of modelling a key fob, students will be able to explore a practical application of geometry and algebra.

Instructors can also take this opportunity to remind students about the importance of respecting intellectual propertytrademarks, and copyright.

Software and Hardware Requirements

For converting the image to a vector graphic, we'll use Inkscape (with the Better DXF plugin or R12 plugin).

To extrude the vector graphic into 3D and add the key ring attachment we'll use OpenSCAD.

Preparing the file for printing will involve ReplicatorG or similar software.

Making a physical model will require a 3D Printer (e.g. MakerBot) or a service such as Shapeways.

Setting up Inkscape and "Better DXF Output"

Download and install Inkscape from If you're running Windows you can use the installable or portable (no install required) version.

To install the "Better DXF Output" extension, download the file Extract the three files and put them in "C:\Program Files\Inkscape\share\extensions" (if you installed the Windows version) or "...\InkscapePortable\App\Inkscape\share\extensions" (if you're using the Windows portable version) or "usr/share/inkscape/extensions" (if you installed the Linux version).

Start up Inkscape and you should now be able to save drawings as "Better DXF Output" files. We need to use this extension because OpenSCAD requires R12 DXF files, and Inkscape 0.48 and newer exports R13 DXF files.

Converting the Image to a Vector Graphic

This is probably the most difficult part, but it shouldn't be too hard. We're going to import the logo into Inkscape, trace it, clean it up if necessary, and export it as a DXF file for the next step. If you already have a (R12) DXF file of your logo, you can skip this step.

1. Under the File menu chose Import... and select your logo file. Hopefully it's a single colour (e.g. black and white) bitmap.
Converting the Image to a Vector Graphic

2. Click on your logo, then under the Path menu choose Trace Bitmap...
Converting the Image to a Vector Graphic

3. On the dialogue box that comes up, click OK and then close it. If all goes well you should now have a path object above your original bitmap.
Converting the Image to a Vector Graphic

4. Move the path object out of the way, and delete the original bitmap. Move the path object back to the bottom left (0, 0).
Converting the Image to a Vector Graphic

5. If there are parts of the image that you don't want, under the Path menu choose Break Apartand delete the unwanted parts
Converting the Image to a Vector Graphic

6. Select everything that remains and under the Path menu choose Combine.
Converting the Image to a Vector Graphic

7. Resize the path object to the size you'd like your key fob to be by typing the size in the W (width) or (height) box. Make sure your click the lock button first to make sure you don't change the aspect ratio of your logo.
Converting the Image to a Vector Graphic

8. Under the File menu choose Save As... then choose Better DXF Output (*.DXF) from the dropdown menu. Name your file logo.DXF and remember when you save it so that you can use it for the next step.
Converting the Image to a Vector Graphic

Creating a 3D Model

Now that you have a DXF file of your logo, we need to make it 3D.

Go to to download the program for LinuxOS X, or Windows and install (or unzip and run the portable version).

Run OpenSCAD and paste in the following code (or download the attached logo.scad file).

width = 50; // the x size of the logo
length = 40.572; // the y size of the logo
logodepth = 2; // how far in the z direction you want the logo extruded
backdepth = 1; // thinckness of the back of the key fob
padding = 3; // how far from the edge you want the logo
holewidth = 10; // the size of the ring attachment on the top

color("green") translate(v = [0, 0, backdepth]) linear_extrude(height = logodepth) import(file = "logo.DXF");  // importing and extruding the logo

minkowski()  // this is a transformation that adds a second shape (the cylinder) around the outside of the first shape (the cube)
 cube([width, length, backdepth/2]);  // a cube to go behind the logo
 cylinder(r=padding, h=backdepth/2);  // rounding the corners of the cube

 translate(v = [width/2, length+padding, 0]) cylinder(h = backdepth, r = holewidth/2);  // the outside of the key ring attachment
 translate(v = [width/2, length+padding, -1]) cylinder(h = backdepth+logodepth+2, r = holewidth/2-2);  // cut out the inside of the key ring attachement
 cube([width, length, backdepth+logodepth+2]);  // cut out so that we don't overlap with the logo

Rendering the 3D File

Make sure your logo.DXF file from the previous step is in the same folder as the OpenSCAD program, or specify the location in the  import(file = "logo.DXF") line.

Put in appropriate values for the variables at the top of the code (width, length, etc.).

Under the Design menu select (or press F5) and it will show you what your design looks like.
Rendering the 3D file

Under the Design menu select Compile and Render (CGAL) (or press F6) and it will create a rendering of your design. Then select Export as STL... under the Design menu to save your completed 3D model in a format that can be used for printing.
Rendering the 3D file

Printing Your 3D Model

Now that you have an STL file of your 3D model you can generate G-code and print as you normally would (likely using ReplicatorG), or you can order a printed part from an online service such as Shapeways.

The first time I made key fobs we needed 90 copies for a series of sessions I was teaching, the MakerBot Automated Build Platform was very useful for this.