Sunday, March 16, 2008

Processing DXF export


This is an extension to the Processing Image Extrude example. I have included the DXF exporter which records the geometry as a dxf file that can accessed outside Processing.

Press 'd' to record dxf ;
Press 'r' to toggle rotation ;


//dxf export 

//make sure you have the dxf library in your Processing directory
import processing.dxf.*;

//-- GLOBAL VARIABLE DECLARATION --------------------------

//DXF object
RawDXF dxf;
//Boolean switch to turn DXF recording on / off.
boolean record;
// Boolean switch to turn rotation on / off
boolean rot;
// Boolean switch to turn DXF recording on / off.
PImage a;
//Pixel Array
int[][] aPixels;
//Values extracted from the pixel array
int[][] values;
//Rotation angle
float angle = -90;

//-- SETUP() -----------------------------------------------

//Processing calls this function once by default
void setup()
{
//Setup applet window. Set width, height to image size.
size(532, 800, P3D);
//Initialize arrays
aPixels = new int[width][height];
values = new int[width][height];
//Set fill property for closed curves off
noFill();
//Set dxf record switch off to begin with
record=false;
//Set rotation on to begin with
rot = true;
//Load the image from url into a new array
a = loadImage("http://web.mit.edu/kkdb/www/newhome/photography/bhai/images/0bhaionsofa.jpg");
//Extract the values and store in an array
for (int i=0; i< height; i++) {
for (int j=0; j< width; j++) {
aPixels[j][i] = a.pixels[i*width + j];
values[j][i] = int(red(aPixels[j][i]));
}//end for j
} //end for i
} //end setup()

//-- DRAW() -----------------------------------------------
//Processing keeps calling this function every 'tick'
//(ticks per second = framerate)
void draw()
{
//Clean up the screen with color '0' (black)
background(0);
// Update and constrain the angle
if(rot) angle += 0.05;
if (angle > TWO_PI) {
angle = 0;
}

// Rotate around the center axis
translate(width/2, 0, 128);
rotateY(angle);
translate(-width/2, 0, 128);

// Start dxf recording if record switch is on (true)
if (record){
beginRaw(DXF, "dxfout_" + angle + ".dxf");
}


// Display the image mass
for (int i=0; i< height; i+=2) {
for (int j=0; j< width; j+=2) {
stroke(values[j][i]);
point(j, i, -values[j][i]);
}
}

if (record){
endRaw();
record=false;
}
} // end Draw()

//-- CALLBACKS --------------------------------------------

void keyPressed(){
switch(key)
{
case 'r':
if(rot) rot = false;
else rot = true;
break;
case 'd':
record=true;
break;
default:
break;
}
}

No comments: