Simple example of how to generate meshes with Processing and then output them to STL using unlekkerLib.

import unlekker.util.*;
import unlekker.geom.*;
import unlekker.data.*;

import processing.opengl.*;

boolean doSTL=false;

void setup() {
  size(400,400, OPENGL);
}

void draw() {
  background(100);

  if(doSTL) {
    beginRaw("unlekker.data.STL","cyl.stl");
  }

  translate(width/2, height/2, 0);
  rotateY(radians(frameCount));
  rotateX(radians(frameCount));

  fill(255,255,255);

  for(int i=0; i<100; i++) {
    pushMatrix();
    translate(random(-300,300),random(-300,300),random(-300,300));
    rotateY(random(PI*2));
    rotateX(random(PI*2));
    cylinder(50,random(50,200));
    popMatrix();
  }

  if(doSTL) {
    endRaw();
    doSTL=false;
  }

}

void keyPressed() {
  if(key=='s') doSTL=true;
}

void cylinder(float w,float h) {
  float px,pz;

  beginShape(QUAD_STRIP);
  for(float i=0; i<13; i++) {
    px=cos(radians(i*30))*w;
    pz=sin(radians(i*30))*w;
    vertex(px,-h,pz);
    vertex(px,h,pz);
  }
  endShape();

  beginShape(TRIANGLE_FAN);
  vertex(0,-h,0);
  for(float i=12; i>-1; i--) {
    px=cos(radians(i*30))*w;
    pz=sin(radians(i*30))*w;
    vertex(px,-h,pz);
  }
  endShape();

  beginShape(TRIANGLE_FAN);
  vertex(0,h,0);
  for(float i=0; i<13; i++) {
    px=cos(radians(i*30))*w;
    pz=sin(radians(i*30))*w;
    vertex(px,h,pz);
  }
  endShape();
}

8 Comments »

There are 8 comments to "Generator.x 2.0: STL output". You may leave your own comment.
1. yeyo, February 4th, 2008 at 05:50

it didnt work for me… maybe i am doing something wrong. im using processing beta and it highlights the line :

for(float i=12; i>-1; i–) {

and says unexpected char: ‘\’

please help!!!!

2. Leandro, February 4th, 2008 at 15:10

Yes, I’m getting same yeyo’s issue.

an idea why?

3. marius watz, February 5th, 2008 at 17:07

Sorry about that, I think the syntax highlighting plugin I was using is to blame, it had some issues with backspaces etc. I just switched to SyntaxHighlighter, which looks good and also allows you to copy the plaintext without any problem.

4. Paul Rabattski, February 21st, 2008 at 18:16

I think it should be i–

5. Paul Rabattski, February 21st, 2008 at 18:17

That’s odd. i minus minus ends up as being i minus

6. marius watz, February 22nd, 2008 at 15:49

Ouch, I see it now. Wordpress automagically corrects two minus signs into one long hyphen. Will figure out how to turn that off. There should be a tutorial on how to post code on WordPress somewhere..

7. Lee Byron » How » Global Type – Tech Demo, March 7th, 2008 at 06:49

[...] Luckily I’m not the first to tackle this in the processing community. Marius Watz provides a wonderful library which includes an STL renderer. Totally fantastic. All that I have to do is provide geometry in [...]

8. kim joong hee, December 14th, 2008 at 19:14

Option Explicit

Call Main
Sub Main
Dim point (80)
Dim i, x, y, z

For i = 0 To 80

x = (20 + 20*Cos (i))*Cos (i*Sin(i))
y = (20 + 20*Cos (i))*Sin (Cos(i)*i)
z = y*i/100 * Sin (i)

point(i)=Array (x,y,z)

Next

Call mifunction(point)

End Sub

Function mifunction(point)

Rhino.AddCurve(point)

End Function

Sub Main
Dim Curve
Dim Domain ,Param, Point, i,sections

sections=150
ReDim collection (sections)

curve=Rhino.GetObject(“sel curve”,4)
If IsNull (Curve) Then Exit Sub

Domain = Rhino.CurveDomain(curve)

For i = 0 To 100

Param =i * ((Domain(1)-Domain(0))/100)

point =Rhino.EvaluateCurve(Curve,Param)
Rhino.AddPoint point
collection(i) = point

Next

Dim Newpoint
Dim arrBase, dblMajorRadius, dblMinorRadius, arrDirection
For i = 0 To 100

dblMajorRadius = i/13

dblMinorRadius = dblMajorRadius – 5

arrBase = newpoint

newpoint=collection(i)

Rhino.AddTorus newpoint, dblMajorRadius, dblMinorRadius
Next

End Sub

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">