TopHat3D.frink

Download or view TopHat3D.frink in plain text format


/** This renders a 3-D model to render a top-hat-like shape to Wavefront .obj
    file format for 3-D printing.

    See: http://paulbourke.net/dataformats/obj/
*/


sides = 100
stopDiam = 10 mm
cylDiam = 18/128 in
cylHeight = 0.75 cm
stopHeight = 1.5 mm

filename = "TopHat" + format[stopDiam,in,2] + "x" + format[cylDiam, in, 2] + "x" + format[cylHeight,mm,1] + ".obj"
println["Filename is $filename"]

w = new Writer[filename]
w.println["# TopHat by Frink\n"]
w.println["o tophat\n"]

cylBottom = new array[sides]
cylTop    = new array[sides]
stopTop     = new array[sides]
stopBottom  = new array[sides]

z0 = 0 mm
z1 = -stopHeight

angleStep = circle/sides

// The length unit
u = mm

for i = 0 to sides-1
{
   angle = i angleStep

   x = cylDiam/2 cos[angle]
   y = cylDiam/2 sin[angle]
   zt = cylHeight
   cylBottom.push[[x, y, z0, i+1]]
   cylTop.push[[x, y, zt, sides+i+1]]
   stopTop.push[[stopDiam/2 cos[angle], stopDiam/2 sin[angle], z0, 2 sides+i+1]]
   stopBottom.push[[stopDiam/2 cos[angle], stopDiam/2 sin[angle], z1, 3 sides+i+1]]
}

w.println["# Vertex list \n"]

w.println["\n# Cyl bottom"]
dumpVertices[cylBottom, w, u]

w.println["\n# Cyl top"]
dumpVertices[cylTop, w, u]

w.println["\n# Stop top"]
dumpVertices[stopTop, w, u]

w.println["\n# Stop bottom"]
dumpVertices[stopBottom, w, u]

w.println["# Face list\n"]
//w.println["usemtl Default"]

// Now output the faces.  These should be output by vertex numbers in a
// counterclockwise "out" fashion.

topFace = "f"
bottomFace = new array

for i = 0 to sides-1
{
   // Cyl sides
   w.println["\n# Cyl sides"]
   [xt0, yt0, zt0, ovt0] = cylTop@i
   [xb0, yb0, zb0, ovb0] = cylBottom@i
   [xt1, yt1, zt1, ovt1] = cylTop@((i+1) mod sides)
   [xb1, yb1, zb1, ovb1] = cylBottom@((i+1) mod sides)
   w.println["f $ovt0 $ovb0 $ovb1 $ovt1"]

   // Upper surface of stop.
   [xt0, yt0, zt0, svt0] = stopTop@i
   [xb0, yb0, zb0, svb0] = stopBottom@i
   [xt1, yt1, zt1, svt1] = stopTop@((i+1) mod sides)
   [xb1, yb1, zb1, svb1] = stopBottom@((i+1) mod sides)
   w.println["\n# Upper Surface of stop"]
   w.println["f $ovb0 $svt0 $svt1 $ovb1"]

   // Sides of stop.
   w.println["\n# Sides of stop"]
   w.println["f $svt0 $svb0 $svb1 $svt1"]

   topFace = topFace + " " + ovt0
   bottomFace.push[svb0]   // We will reverse this later
}

w.println["\n# Top face"]
w.println[topFace]

w.println["\n# Bottom face"]
w.println["f " + join[" ", reverse[bottomFace]]]

w.close[]


/** Dump an array of vertices */
dumpVertices[array, writer, u] :=
{
   for [x,y,z] = array
      writer.println["v " + formatFix[x, u, 5] + " " + formatFix[y, u, 5] + " " + formatFix[z, u, 5]]
}


Download or view TopHat3D.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19945 days, 5 hours, 33 minutes ago.