/* ============================================================================ Name : gerbcream.c Author : jopka1@gmail.com Version : 2011.11.21 Copyright : GPL license Description : Converts Gerber file to G-code for CNC paste dispencer. (Or even drill). also minimizes traces length as possible. Hope iе can be useful for everyone :-) See more here http://www.linuxcnc.org/component/option,com_kunena/Itemid,20/func,view/id,14755/catid,20/limit,6/limitstart,0/lang,english/ ============================================================================ */ #include #include #include #include #define debug 0 #define gbr_paste_ticket "%LNTop Paste*%" // define valid window here to throw away some outbound coords, if any. #define validxmin 100 #define validxmax 300 #define validymin 100 #define validymax 300 // sometimes shift is useful, if gerber's coords not starts from zero... #define shiftx -100 #define shifty -100 int ln; // line number int p; //process flag int n; // char position in string int i,j,k,l; int kk; #define slength 1024 // max string length #define pmax 16384 // max points (coords) char s[slength], ss[slength]; char infile[slength], outfile[slength] ; FILE *fp; float x,y; char r; // shows current value into which will be place readed digits, // like 'x', 'y' etc. float xx[pmax], yy[pmax]; int used[pmax]; // used flag int coord[pmax]; // coord. numbers (indexes) int xmin, xmax; float xxmin, xxmax, yymin, yymax; int nn; int mmm; float d, dd; int m; /*void printarray(void) { int i; for (i=0; i='0')&(s[n]<='9')) { if (r=='x') { x=x*10+(s[n]-'0'); } if (r=='y') { y=y*10+(s[n]-'0'); } k=1; } if (k==0) { //not x,y,digit - switch readout digits to null r='*'; // no read digits unless we knof if it is x or y } if ((r=='x')|(r=='y')) { kk=1; } } if (kk==1) { xx[nn]=x/1000.0; yy[nn]=y/1000.0; if ((x>validxmin*1000)&(xvalidxmin*1000)&(xxxmax) { xxmax=xx[nn]; xmax=nn; } if (yy[nn]yymax) { yymax=yy[nn]; } if (debug) printf("Valid nn=%d x=%7.3f y=%7.3f\n", nn, xx[nn], yy[nn]); nn++; } else { printf("Out of window nn=%d x=%7.3f y=%7.3f\n", nn, xx[nn], yy[nn]); } } } if (strncmp(s,gbr_paste_ticket,10)==0) { printf("%s found at line %d\n", gbr_paste_ticket, ln); p=1; } ln++; } if (p==1) { printf("Readed %d valid coords. Processing...\n", nn); printf("Xmin=%7.3f Xmax=%7.3f Ymin=%7.3f Ymax=%7.3f\n", xxmin, xxmax, yymin, yymax); // process now: memset(used, 0, sizeof(used)); // all as unused memset(coord, -1, sizeof(coord)); // for debug - because '0' is valid coord. coord[0]=xmin; used[xmin]=1; // now it is used. coord[1]=xmax; used[xmax]=1; // printarray(); printf("d=%7.3f\n", p2pdist(coord[0],coord[1])); if (debug) debug_print_coord(); for (k=2; kd) { ddd=d; mmmm=mmm; nnnn=m; } //insert point into output chain // not work: memcpy(coord[j+2], coord[j+1], sizeof(uu)-sizeof(coord[0])*(10000-(j+2)) ); } printf("Coord#=%d nnnn=%d mmmm=%d x=%7.3f y=%7.3f ddd=%7.3f\n", k, nnnn, mmmm, xx[m], yy[m], ddd); for (l=10000; l>=(mmmm+1); --l) { coord[l]=coord[l-1]; } // THE C LANGUAGE IS BROKEN! THERE SHOULD BE coord[mm+1]=m; BUT NOT WORK! coord[mmmm+1]=nnnn; // this taken instead, during long blind random tests. used[nnnn]=1; // now it is already used coord. if (debug) debug_print_coord(); } // done sotring here. //add your postprocessing here... // print results: printf("\n(Start optimized G-code.) \n"); for (k=0; k