Skip to content

Instantly share code, notes, and snippets.

Created July 31, 2010 14:47
Show Gist options
  • Save anonymous/502250 to your computer and use it in GitHub Desktop.
Save anonymous/502250 to your computer and use it in GitHub Desktop.
import processing.serial.*;
class Print extends Thread {
Vector list;
float dt=millis();
public Print(Vector l) {list = l; }
public void run() {
println("Starting to print...");
curX=curY=100;
//port=new Serial(this,Serial.list()[0],9600);
Enumeration e = list.elements();
while(e.hasMoreElements()) {
Vector data = (Vector) e.nextElement();
Enumeration i = data.elements();
boolean nofirst=false;
while(i.hasMoreElements()) {
int x = (Integer) i.nextElement();
int y = (Integer) i.nextElement();
port.write(x+" "+y);
print(x+" "+y);
curX=x;
curY=y;
if (i.hasMoreElements()) port.write(" ");
else port.write(0xd);
if (i.hasMoreElements()) print(" ");
else println();
// now wait for ack
String s=null;
if(nofirst) while((s=port.readStringUntil(13))==null); // wait for a line to be read
nofirst=true;
print(s);
}
// now wait for OK response
// while(port.available()<2) ;
// println(port.readString());
}
println("Total print time = "+ (millis()-dt)/1000 + " seconds");
}
}
int curX,curY;
Vector list = new Vector();
Serial port;
void setup() {
double offsetY=0.0;
double offsetX=0.0;
String file = dataPath("circle-titulo.svg");
curX=curY=0;
ellipseMode(CENTER);
port=new Serial(this,Serial.list()[0],9600);
//port.bufferUntil(0x0d); // lines end with CR
try {
Scanner sc = new Scanner (new File(file));
while(sc.hasNext()) {
String line = sc.nextLine();
if(match(line,"d=.M")!=null) {
String[] dots = line.split("L");
//println(dots);
Vector data = new Vector();
for(int i=0; i<dots.length; i++) {
//println(dots[i]);
String[] v = match(dots[i],"([0-9\\-\\.]+) ([0-9\\-\\.]+)") ; // [0] all [1] first [2] second
Double x = Double.parseDouble(v[1])+offsetX;
Double y = Double.parseDouble(v[2])+offsetY;
data.add(x.intValue());
data.add(y.intValue());
print(x.intValue()+" "+y.intValue());
if(i<dots.length-1) print(" ");
}
list.add(data);
println();
}
else
if(match(line," d=.m ")!=null) {
line=line.substring(12);
String[] dots = line.split(" ");
//println(dots);
Vector data = new Vector();
Double x=new Double(0);
Double y= new Double(0);
for(int i=0; i<dots.length; i++) {
//println(dots[i]);
String[] v = match(dots[i],"([0-9\\-\\.]+),([0-9\\-\\.]+)") ; // [0] all [1] first [2] second
//println(i);println(v);
if(i==0) {
x = Double.parseDouble(v[1])+offsetX;
y = Double.parseDouble(v[2])+offsetY;
}
else {
x+= Double.parseDouble(v[1]);
y+= Double.parseDouble(v[2]);
}
data.add(x.intValue());
data.add(y.intValue());
print(x.intValue()+" "+y.intValue());
if(i<dots.length-1) print(" ");
}
list.add(data);
println();
}
if(match(line,"translate")!=null) {
String v[]=match(line,"\\(([0-9\\-\\.]+),([0-9\\-\\.]+)\\)");
println("OFFSET="+v[1]+" "+v[2]);
offsetX+=Double.parseDouble(v[1]);
offsetY+=Double.parseDouble(v[2]);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
size(600,800);
}
void mousePressed() {
Thread t = new Print(list);
t.start();
}
void draw() {
background(200);
Enumeration e = list.elements();
while(e.hasMoreElements()) {
Vector data = (Vector) e.nextElement();
Enumeration i = data.elements();
int ox=-1,oy=0;
while(i.hasMoreElements()) {
int x = (Integer) i.nextElement();
int y = (Integer) i.nextElement();
if(ox>-1) line(ox/20,oy/20,x/20,y/20);
ox=x;
oy=y;
}
}
fill(255,0,0);
noStroke();
if(curX>-1) ellipse(curX/20,curY/20,4,4);
stroke(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment