Tuesday, October 29, 2013

Converting DEMs to STL files for 3D printing

Ever since we got a 3D printer in the Geography Department at the University of Oregon, we've wanted to print out topographic models. Thingaverse user Markus Fussenegger (mfussi) has a nice compact Java program, Heightmap to STL, for converting height map images into STL files for 3D printing. Markus's program is available on Sourceforge through Thingaverse (http://www.thingiverse.com/thing:15276).

This is just a quick tutorial on how I got his code up and running on my Windows 7 machine...


Download the software

  1. Download the Java program, http://www.thingiverse.com/thing:15276.  
    • Put it somewhere easily accessible-I made a new folder directly on my C: drive
    • Another easy place is in your User folder C:\Users\your_username
  2. Update your Java (which is important to do anyway...)
    • http://www.java.com/en/download/index.jsp

Setup Java for command line use

  1. To make most versions of Java work at the command line you need to add Java to the System PATH. This will allow you to call Java as a program from the command line.
  2. Find out where Java is installed and write down the file path down the the Bin file. On my machine:
    •  C:\Program Files\Java\jre7\bin
  3. Open the Start Menu, Right-click on Computer and click Properties.
  4. On the left hand side the window that opens...click Advanced System Settings.
  5. This will open another window, toward the bottom click the Environmental Variables button.
  6. In the bottom, System variables window, scroll down to the Path entry. Select it and click Edit.
  7. In the small window that pops up highlight the Variable Value field, scroll all the way to the end (to the right).
  8. Insert a semicolon after the last entry and type in the file path to Java.

  

Making a height field map

I'm writing this for geographers with some GIS knowledge in their back pockets. I'm using ArcMap to import DEMs and export the height maps, but there are plenty of other free/open-source options to do similar things; MicroDEM, qGIS, GrassGIS...(there are lots of tutorials).

  1. Download your DEM of choice. The National Map is a good place to start.
    • Go for the lower resolution (1/3 or 1-arcsecond) data. The 1/9-arcsecond lidar-derived data makes for slow processing, unless it's a small area.
    • SRTM data is also good for larger areas.
  2. Open your DEM in your GIS of choice.
  3. Clip it, Extract it by Mask...What ever you need to do to pull out the area you are interested in.
  4. Go the the Raster properties and ensure that the histogram stretch is Min-Max (or Linear in other software).
  5. Set the color ramp to a Black to White.
  6. In ArcMap...Right-click on the raster layer > Data > Export Data.
  7.  Select Use Renderer, then set your Output Location, File Name, and set the format to PNG.
  8. Click Save.
  9. Check the exported PNG file. If there are large black areas, crop them from the image using an image editing software (quick and dirty is Microsoft Office Picture Manager, but Photoshop and other are good too).
  10. Move the file to the same place you put the Heightmap to STL program.
Running the STL conversion
  1. Open up the Command Prompt (enter cmd in the Start Menu search box)
  2. Check that Java is working
    • Type:     java -version
    • It should return a couple of fun lines about the current version of Java you have installed.
  3. Navigate to the folder with the program and PNG file.   cd c:\ (you know the drill)
  4. Next, following Markus' instructions from Thingaverse, we''ll execute the program:
  5. java -jar heightmap2stl.jar 'path to imagefile' 'height of model' 'height of base'
  6. Replace the items in 'quotes' with the actual variables you want to use. I'll show examples for a file I created for Yosemite Valley. (This is assuming your files are in the same place as the program)
    • 'path to the image file' :  yosm_val.png
    • 'height of the model': you'll need to play with this one, this is how the program scales the 8-bit black to white values. It's also a vertical exaggeration. Start out with 100.
    • 'height of the base': the thickness of the lowest point on the model. This will also need some playing. I start out with 10.
  7. The final command should look something like this:
  8. java -jar heightmap2stl.jar yosm_val.png 100 10

 Print Setup 

Check out the exported STL file in a 3D model viewer, like Meshlab. If it looks good, we're off to our 3D printer. We have a Makerbot Replicator, so the screen captures are from MakerWare, but if you have another hardware/software combo the ideas are the same. 

If you print topographic models the standard way, flat on the build platform with standard fill settings, you waste a lot of plastic in the empty parts of the model. So we've been trying to print the models up on an edge so that the printer only has to print the shells of the model and not any fill. This is pretty straight forward in MakerWare, just rotate the model 90 degrees along the X or Y axis to turn the model up on end. You can then rescale it and set it up on the platform.

Set the quality of your print to Standard or High, then adjust the advanced options. Set the Fill to 0% and add an extra shell (3-ish). Send it to the printer and cross your fingers. 


11 comments:

  1. Hi,

    I'm having an issue of out of memory for Java heap space at com.heightmap.main.main(Main.java53)

    any suggestions?

    Thanks
    Sarah

    ReplyDelete
    Replies
    1. Sarah,
      You probably need to increase your default heap size. You can do as you run the program with the -Xmx option. The example below would increase the max memory to 2 GB (2048 MB) (you are only technically limited by your RAM).

      java -Xmx2048m -jar heightmap2stl.jar...

      If you increase your memory and still get an error, the only other option is to either create a lower resolution DEM or break it into tiles.

      Delete
    2. James thanks so much for your response. I have 8gb of ram but when I tried to increase heap size to 2048, I received an error that I could not reserve enough space for object heap. Is there a limit I should adjust or do I need to purchase more ram. thanks so much.

      Delete
  2. Hi

    I'm receiving this error message.

    java.lang.NegativeArraySizeException
    at com.heightmap.Main.main(Main.java:75)

    Any suggestions?

    Thank you
    Francois

    ReplyDelete
    Replies
    1. Sorry Francois, I've never seen that error before.

      Delete
  3. Hi James,

    I am doing the similar research following the steps you wrote.

    However, when I approach to the Section"Making a height field map", I have the following problems:

    1. When downloading the DEM from the National Map Viewer, which format should I download, ArcGrid, GeoTiff or someothers?

    2. How can I open DEM in ArcGIS and do the steps followed? I could not find the right button to click. Could you please show it step by step?

    Also, in the section "Running the STL conversion", why did you set the "height of the model"as 100 and what does 100 mean?

    I am a beginner in this field and thanks so much for your time and attention.

    Yi

    ReplyDelete
  4. I have a DEM in ArcGIS 10.1 that I would like to get into STL format for a 3D printer. Does anyone know some software that will allow me to make this conversion?I have to use the STL format because that is the format required by my university's 3D printer.
    java barcode creator

    ReplyDelete
  5. Hi I was searching for the blogs for many times, now I have reached at the right place.Tips and tricks

    ReplyDelete
  6. Hi,

    I'm trying to use my own data sets from other places, like .tiff files from WorldDEM or SRTM. Is there a way I can do this with this application? I got stuck at the "making a height map" section as I couldn't find a way to add the data I have.

    Thanks

    ReplyDelete
    Replies
    1. Kelsey,
      The trick is to convert the TIF files into PNG or JPEG with a grayscale (8-bit) color ramp. You normally need to use a GIS (ArcMap, GRASS, QGIS...) to do this because most image manipulation programs (Photoshop, GIMP) cannot handle the image headers that GeoTiff files have. I know there are some workarounds to get GeoTiff files into Photoshop, but I do not know how well they would work for this process.

      Delete