Simple tool for creating polyhedrons in OpenSCAD

Creating a polyhedron in OpenSCAD can be very complicated to work with, especially if there are a lot of points and faces.

the syntax is:

polyhedron(points = [ [x, y, z], ... ], faces = [ [p1, p2, p3..], ... ], convexity = N);

If you have complex polyhedron with many points and faces it is very complicated to build. Also take note of the documentation about the ordering of the faces:

Point ordering for faces When looking at the face from the outside inwards, the points must be clockwise. You can rearrange the order of the points or the order they are referenced in each tuple. The order of faces is immaterial. Note that if your polygons are not all oriented the same way OpenSCAD will either print an error or crash completely, so pay attention to the vertex ordering. Again, remember that the 'pN' components of the faces vector are 0-indexed references to the elements of the points vector.

Either you know how to draw the faces by memory or draw the polygon on paper and keep track of all the point locations.

I created this simple helper function to visually see the indexes of all the points in the planes. This makes drawing all the faces very simple. But you still need to look at a face from the outside inwards. So you have to rotate the object and draw the faces in the correct ordering of points.

This is an example of a simple polygon. It can be easily build using other tools but it’s to demonstrate the technique:
polygon1

These are the points:

p = 
[
  [-5,-5,0]
  , [15,-5,0]
  , [-5,15,0]
  , [15,15,0]
  , [0,0,10]
  , [0,10,10]
  , [10, 0,10]
  , [10,10,10]
  , [5,5,15]
];

Running this module shows all the points and displays the index number of each point at their location:

showPoints(p);

module showPoints(v) {
    for (i = [0: len(v)-1]) {
        translate(v[i]) color("red") 
        text(str(i), font = "Courier New", size=1.5);
        
    }
}

This is the output seen from the top because the labels are best seen from that direction.

polygon2

Now it’s easy to draw all the faces by looking at the points indexes.

face [0,4,6]

polygon3a

face [6,1,0]

polygon3b

face [3,1,6]

polygon3c

face [6,7,3]

polygon3d

and so on…

But you can still mix up the point ordering by not looking at the outside of the faces inwards. Try pressing F5, F6, F12 to see if the object is still correctly drawn.
Because if there is an error either you see nothing at all or missing faces.

For debugging any object I use a few cubes and the difference tool to see if the object is still drawn with holes in it and see if the object is solid everywhere.

polygon_debug1

polygon_debug2

polygon_debug3

Full script for this example:

// points to be used
p = 
[
  [-5,-5,0]
  , [15,-5,0]
  , [-5,15,0]
  , [15,15,0]
  , [0,0,10]
  , [0,10,10]
  , [10, 0,10]
  , [10,10,10]
  , [5,5,15]
];

// faces drawn
f = [
    [1,3,2]
    ,[2,0,1]
    ,[0,4,6]
    ,[6,1,0]
    ,[0,2,5]
    ,[5,4,0]
    ,[2,3,7]
    ,[7,5,2]
    ,[3,1,6]
    ,[6,7,3]
    ,[4,8,6]
    ,[5,8,4]
    ,[7,8,5]
    ,[6,8,7]
];



difference() {
    polyhedron(points = p, faces = f);
    
    // for debugging use to see if the object is solid inside the polygon
    showDebugCubes();
}
     

showPoints(p);

// show the index of all points at the corresponding location
module showPoints(v) {
    for (i = [0: len(v)-1]) {
        translate(v[i]) color("red") 
        text(str(i), font = "Courier New", size=1.5);
        
    }
}

// dummy cubes for debugging purpose
module showDebugCubes() {
    translate([3,-5, 5]) cube([3,20,3]);
    translate([-10, 5, 5]) cube([30,3,3]);
    translate([3, 3, 0]) cube([3,3,45]);
}

Leave a comment