4.2 Export Rhino Geometry to Processing

You can export geometry in rhino to processing using grasshopper in a number of ways. One way is to use panels to stream data to a *.txt file. You can do this by breaking down geometry into segments and then streaming the vector coordinates of the start and end points of those segments. You can download the definition above that exports existing geometry: export_geometry.zip

You can also export geometry you create in grasshopper in the same way. This way when you change the geometry it will automatically stream the new geometry to the *.txt file and replace it. export_voronoi.zip

Once you have streamed data from Rhino you need to read the *.txt file(s) in processing. The sketch below will work for either of the definitions above because they are streaming the same structured data: two lists 1. the XYZ coordinates of the start points and 2. the XYZ coordinates of the endpoints.

//Vector lists for the start and end points
ArrayList <PVector> startpts = new ArrayList <PVector>();
ArrayList <PVector> endpts = new ArrayList <PVector>();

///camera varibles
int oldx = mouseX;
int oldy = mouseY;
float rotx = 0;
float roty = 0;
float zcam = 680;

void setup() {
  size(800, 800, P3D);
  perspective(PI/3.0,(float)width/height,1,100000);
  
  //Load the *.txt file for start points into an array
  //the loop runs 1/3 of the length of the list
  //because you are converting the data in groups of 3 to create vectors
  String[] coor = loadStrings("startpts.txt");
  for(int i = 0; i < (coor.length/3); ++i){
    float xx = float(coor[i*3]);
    float yy = float(coor[i*3+1])*-1;
    float zz = float(coor[i*3+2]);
    startpts.add(new PVector(xx,yy,zz));
  }
  //Load the *.txt file for wnd points into an array
  coor = loadStrings("endpts.txt");
  for(int i = 0; i < (coor.length/3); ++i){
    float xx = float(coor[i*3]);
    float yy = float(coor[i*3+1])*-1;
    float zz = float(coor[i*3+2]);
    endpts.add(new PVector(xx,yy,zz));
  }
}

void draw(){
  background(0);
  cam();
  noFill(); 
  stroke(255);

  //This loop cycles through the list of start and end points
  //and uses the vector of each to draw a line
  for(int i = 0; i < startpts.size(); i++){
    PVector p1 = startpts.get(i);
    PVector p2 = endpts.get(i);
    line(p1.x,p1.y,p1.z,p2.x,p2.y,p2.z);
  }
}

void cam() {
  int newx = mouseX;
  int newy = mouseY;
  translate(width/2, height/2,zcam);
  rotateY(rotx);
  rotateX(roty);
  //rotateZ(PI);
  if ((mousePressed == true)) {
    rotx = rotx + (oldx-newx)/50.0;
    roty = roty + (oldy-newy)/50.0;
  }
  oldx = newx;
  oldy = newy;
}

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