Pioneer Ball Project: Basic Modifications to a STL file using openSCAD

Contributed by
Mark Peeters

Source files and helpful links:
youmagine
openscad

This outlines how you can use openSCAD to import a STL file and perform some interesting modifications to the file and export for printing.

  1. Import the STL file. (please note - all my code assumes you have the STL file and the openSCAD files in the SAME DIRECTORY. I do that so that I don’t have to worry about including the file’s path to the stl files)

    1. Make a folder, download “pioneerBallF360.stl” into the folder, openSCAD and save the file into this fold “ball_v1.scad”.
    2. In openSCAD enter the following code and then click the preview button or press F5.

      import("pioneerBallF360.stl", convexity=3);

    Your screen should look similar like this when you click preview or press F5.

       

    Step 1


  2. Center the STL file for simplicity. Most objects are created at the origin, so it will be simpler if the object we are modifying is also at the origin.The STL file came in on it’s side away from the origin so we will rotate it and move(translate) it so that it is at the origin.

    1. So first we will rotate the STL +90 degrees around the X axis to it will be upright. Add the following command in front of the import command:

      rotate([90,0,0])

      Your screen should look similar like this when you click preview or press F5..

         

      Step 2

    2. Now we will move (translate) the STL down along the Z axis so it sits on the origin. Add the following command in front of the rotate command:

      translate([0,0,-112])

      Your screen should look similar like this when you click preview or press F5.

         

      Step 3


  3. Modify the STL using the “difference” command. The difference command takes a list of objects and keeps the 1st in the list and removes all other objects from it. You will use the curly brackets to contain the list of objects.  https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/CSG_Modelling#difference

    1. Add the following command in front of the translate command:

      difference(){

    2. After the import command add some blank lines (enter key) and then close the curly bracket “}”.  Now any object you put inside the curly brackets will be removed from the STL. Your code should look like this:

         

      Step 4

    3. Remove a cylinder from the center of the STL by adding the following code after the import command:

      cylinder(r=20,h=50);

      Your screen should look similar like this when you click preview or press F5.

         

      mp05

    4. Any objects you add inside the curly brackets of the difference command will be removed from the STL file. Below is some sample code you can add to remove 2 cylinders and 2 cubes from the STL file:

      translate([20,0,0])cylinder(r=20,h=50);  

      translate([-20,0,0])cylinder(r=20,h=50);  

      translate([-10,18,0])cube([20,20,50]);  

      translate([-10,-38,0])cube([20,20,50]);

      Your screen should look similar like this when you click preview or press F5.

         

      Step 6

    5. Add your own shapes to the difference command. If some of your shapes remove the edges of the STL file , or remove the magnet holes needed for the ball sections to cling together don’t worry in Step 4 I will show you how to add back the sides and magnet holes.

      Here’s an example of what I mean, if I translate the cylinders by 40, you can see from the bottom view of the resulting model that the magnet holes have been removed.

      (NOTE: I’ve changed the Z Axis translation from zero to -1 so that the objects making the holes poke all the way through the bottom, this makes the preview render look better without the shadowy green film you see when the faces are aligned. )

         

      Step 7


  4. Make sure your final model will have the edges and magnet holes by “adding back” the edges and hole. We can simply import a second copy of the original STL file, remove the middle and keep the edges. This way no matter how creative you get with modifying your first imported STL the final model will still be able to connect to other parts.

    1. In order to keep organized within the code, add a comment to the bottom of the file using the double slash, my comment looks like this:

      //add edges of STL back on in case you've removed too much, you don't want to lose magnet holes or edges that touch the build plate----------

      then paste the entire difference code below you comment.

      Your screen should look similar like this when you click preview or press F5.

         

      Step 8

    2. Working on the second difference, the one you just pasted below. Start by putting an exclamation point “!” in front of the difference command, this is a programming trick which tells openSCAD to ignore all other code and only render the command with the exclamation point. This is so we can focus on just this code for a short time. Then remove all the objects you added to the difference list and replace them with this one cube:

      cube([83,83,100],center=true);

      So that this cube is the only thing you are removing from the STl file.

      Your screen should look similar like this when you click preview or press F5.

         

      Step 9

      NOTICE how this shape has all the edges and magnet holes you do not want to lose.

    3. Remove the exclamation point so that all code will be rendered. Notice now you have the original design and you have added back the edges so the model will be able to connect to make the ball.

      Your screen should look similar like this when you click preview or press F5.

         

      Step 10

         

      Step 11


  5. Modify the STL using the “intersection” command. The intersection command takes a list of objects and creates an intersection of all the objects in the list. You will use the curly brackets to contain the list of objects. https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/CSG_Modelling#intersection

    1. (first we will break things then fix them) At the top of the file replace the word “difference” with the word “intersection”, changing the difference command into an intersection command.

      Your screen should look similar like this when you click preview or press F5.

         

      Step 12

    2. Notice that we are getting nothing from the intersection now, and only see the bottom code for adding back the sides. This is because we have 5 shapes inside the curly brackets for the intersection command and they don’t all share any space. We can fix this by using the “union” command to tell openSCAD that the last 4 shapes should be treated as a single object, then we will only have two shapes that do intersect. Add a blank line (enter key) after the import command and type in the union command:

      union(){

      Then after the last shape in the list add a blank line (enter key) and type in the close curly bracket so that all 4 shape are inside the union commands curly brackets.

      Your screen should look similar like this when you click preview or press F5

         

      Step 13

    3. Add your own shapes to the intersection command. If you have a bunch of small shapes keep them inside the union. For example replacing the code inside the union with this:

      cylinder(r=15,h=50);    

      translate([40,0,0])sphere(30);  

      translate([-40,0,0])sphere(30);  

      translate([0,40,0])sphere(30);  

      translate([0,-40,0])sphere(30);

      Your screen should look similar like this when you click preview or press F5

         

      Step 14

    4. If you are using larger shape that all overlap you may want to remove the union command. Like this example where I use the “$fn” command to make the a sphere have less sides. Replace the whole union command with this code:

      translate([0,0,-42])sphere(r=75,$fn=16);

      AND at the bottom of the code put an asterisk in front of the last difference command to stop that from rendering.

      Your screen should look similar like this when you click preview or press F5

         

      Step 15

    5. You can add some more the the same sphere but rotate them slightly to get more facets. Add these lines of code below the sphere we just made:

      translate([0,0,-42])rotate([9,0,0])sphere(r=75,$fn=16);

      translate([0,0,-42])rotate([-9,0,0])sphere(r=75,$fn=16);

      translate([0,0,-42])rotate([0,9,0])sphere(r=75,$fn=16);

      translate([0,0,-42])rotate([0,-9,0])sphere(r=75,$fn=16);

      Your screen should look similar like this when you click preview or press F5

         

      Step 16


  6. Final render and export STL file for printing. Once you are ready to print, click the render button or press F6, and wait for the model to render. Then click the export STL button or use the menu File>Export>Export as STL to save you modified STL file for printing.
  7. Advanced. You can import other STL files to create differences! I made a custom random maze and saved the STL file as maze.stl. Code for maze generator: http://www.thingiverse.com/thing:1264082

    Then you use the same import command to bring in your STL file. It looks like this:

       

    Step 17

Index
Support