processing stl

    PShape s; int p; String sub; Float X,Y,Z;
    float rotX, rotY, camX, camY, camZ;
 
    void setup() 
    { 
      size(640, 360, P3D); 
      background(0); 
      stroke(255);
      fill(130); 
      s=createShape(); 
      s.beginShape(TRIANGLES); 
      String[] lines = loadStrings("test.stl"); //LOAD ASCII STL ONLY!!!
      for (int i = 0 ; i < lines.length; i++) 
      { 
        p=lines[i].indexOf("vertex"); 
        if (p>-1) 
        { 
         sub=lines[i].substring(p+7); 
         String[] list = split(sub, ' ');
         X=float(list[0]); Y=float(list[1]); Z=float(list[2]);
         s.vertex(X, Y, Z);
        } 
      } 
      s.endShape(); 
      camX=width/2;
      camY=height/2;
      rotX=0;
      rotY=0;
    }
 
    void draw() 
    { 
     background(0);
     translate(camX, camY, camZ);
     rotateY(rotY);
     rotateX(rotX);
     shape(s, 0, 0);
    }
 
void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  camZ+=e*5;
}
 
void mouseDragged()
{
  if (mouseButton == LEFT)
  {
    rotY += (pmouseX - mouseX)*0.01;
    rotX += (pmouseY - mouseY)*0.01;
  }
  if (mouseButton == RIGHT)
  {
    camX -= (pmouseX - mouseX);
    camY -= (pmouseY - mouseY);
  }
  if (mouseButton == CENTER)
  {
    camZ += (pmouseY - mouseY);
  }
}

3.56
9
Phoenix Logan 186120 points

                                        ASCII
    PShape s; int p; String sub; Float X,Y,Z;
    float rotX, rotY, camX, camY, camZ;
 
    void setup() 
    { 
      size(640, 360, P3D); 
      background(0); 
      stroke(255);
      fill(130); 
      s=createShape(); 
      s.beginShape(TRIANGLES); 
      String[] lines = loadStrings(&quot;test.stl&quot;); //LOAD ASCII STL ONLY!!!
      for (int i = 0 ; i &lt; lines.length; i++) 
      { 
        p=lines[i].indexOf(&quot;vertex&quot;); 
        if (p&gt;-1) 
        { 
         sub=lines[i].substring(p+7); 
         String[] list = split(sub, ' ');
         X=float(list[0]); Y=float(list[1]); Z=float(list[2]);
         s.vertex(X, Y, Z);
        } 
      } 
      s.endShape(); 
      camX=width/2;
      camY=height/2;
      rotX=0;
      rotY=0;
    }
 
    void draw() 
    { 
     background(0);
     translate(camX, camY, camZ);
     rotateY(rotY);
     rotateX(rotX);
     shape(s, 0, 0);
    }
 
void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  camZ+=e*5;
}
 
void mouseDragged()
{
  if (mouseButton == LEFT)
  {
    rotY += (pmouseX - mouseX)*0.01;
    rotX += (pmouseY - mouseY)*0.01;
  }
  if (mouseButton == RIGHT)
  {
    camX -= (pmouseX - mouseX);
    camY -= (pmouseY - mouseY);
  }
  if (mouseButton == CENTER)
  {
    camZ += (pmouseY - mouseY);
  }
}

3.56 (9 Votes)
0
3.89
9
A-312 69370 points

                                    Bin
import java.io.FileInputStream;
import java.io.DataInputStream;

    PShape s;  
    float rotX, rotY, camX, camY, camZ;
    boolean shape=false;
    
    java.io.File folder;
    int showfiles=0;
    File[] files;

    void setup() 
    { 
      size(1024, 768, P3D); 
      background(0); 
      stroke(255);
      fill(130); 
      camX=width/2;
      camY=height/2;
      rotX=0;
      rotY=0;
    }

    void draw() 
    { 
     background(0);
     textSize(32);
     if (showfiles&gt;0) {     
      for (int i = 0; i &lt; files.length; i++) {
        if (files[i].isDirectory()) fill(255,0,0); else fill(150);
        text(files[i].getName(),20,32+i*32);
       }
      }
     fill(130);   
     translate(camX, camY, camZ);
     rotateY(rotY);
     rotateX(rotX);
     if (shape) shape(s, 0, 0);
      else text(&quot;Click for menu&quot;,0,0);
    }
    
void loadSTL(String filename)
{
   float N,X,Y,Z;
   int vertices=0;
   int p; String sub;
      s=createShape(); 
      s.beginShape(TRIANGLES); 
      String[] lines = loadStrings(filename); //LOAD ASCII STL ONLY!!!
      for (int i = 0 ; i &lt; lines.length; i++) 
      { 
        p=lines[i].indexOf(&quot;vertex&quot;); 
        if (p&gt;-1) 
        { 
         sub=lines[i].substring(p+7); 
         String[] list = split(sub, ' ');
         X=float(list[0]); Y=float(list[1]); Z=float(list[2]);
         s.vertex(X, Y, Z);
         vertices++;
        } 
      } 
      s.endShape();
   if (vertices==0) //no vertices? it could be binary STL
    {
      println(filename,&quot;is a binary file&quot;);
      try{
      FileInputStream fis = new FileInputStream(sketchPath(filename));
      DataInputStream input = new DataInputStream(fis);
      byte[] header = new byte[80];
      byte[] b4=new byte[4];
      byte[] attribute=new byte[2];
      byte[] normal = new byte[12];
      input.read(header);  //read header and throw away
      input.read(b4); vertices=unhex((hex(b4[3])+hex(b4[2])+hex(b4[1])+hex(b4[0]))); //again this endianity
      println(&quot;vertices: &quot;,vertices);
      s=createShape(); 
      s.beginShape(TRIANGLES); 
      for (int i = 0 ; i &lt; vertices; i++){
       input.read(normal); //ignore surface normals
       for (int j=0;j&lt;3;j++){
        input.read(b4); X=annoyingEndian(b4);
        input.read(b4); Y=annoyingEndian(b4);
        input.read(b4); Z=annoyingEndian(b4);
        s.vertex(X, Y, Z);}
        input.read(attribute); //ignore attribute
      }
        s.endShape();      
        input.close();
      }
          catch (IOException e)
    {
      System.out.println(&quot;IOException : &quot; + e);
    }
    }
   shape=true;   
}

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  camZ+=e*5;
}

void mouseDragged()
{
  if (mouseButton == LEFT)
  {
    rotY+=(pmouseX-mouseX)*0.01;
    rotX+=(pmouseY-mouseY)*0.01;
  }
  if (mouseButton==RIGHT)
  {
    camX-=(pmouseX-mouseX);
    camY-=(pmouseY-mouseY);
  }
  if (mouseButton==CENTER)
  {
    camZ+=(pmouseY-mouseY);
  }
}

void mouseClicked()
{
 if (showfiles==0) {  
 files = listFiles(sketchPath(&quot;&quot;));
 showfiles=1;
 }
 else
 {showfiles=0;
  if (mouseX&lt;500)
   {
    int Choice=mouseY/32;
    if (Choice&lt;files.length)
    {
     showfiles=1;
     String filename;
     filename=files[Choice].getName();
     println(filename);
     if (filename.toLowerCase().indexOf(&quot;.stl&quot;)&gt;-1) loadSTL(filename);
    }
   }
 }
}

Float annoyingEndian(byte[] b)
{
  return Float.intBitsToFloat(unhex((hex(b[3])+hex(b[2])+hex(b[1])+hex(b[0]))));
}

3.89 (9 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source