#include #include #include #define RN( t, v ) fscanf( fp, "%d %s", t, v ) int write_cnc( FILE *fpout, char *file, int flag ); main( int argc, char **argv ) { FILE *fpout; if( argc != 3 ) { fprintf( stderr, "Usage: DXF2CNC \n"); exit( 1 ); } printf("Infile: %s, Outfile : %s\n", argv[1], argv[2] ); fpout = fopen( argv[2], "w"); write_header( fpout ); printf("Start Circles\n"); write_cnc( fpout, argv[1], 0 ); printf("Start Polys\n"); write_cnc( fpout, argv[1], 1 ); printf("Trailer\n"); write_trailer( fpout ); fclose( fpout ); } int write_header( FILE *fpout ) { fprintf( fpout, "T0101\nG00\nF05\n" ); } int write_trailer( FILE *fpout ) { fprintf( fpout, "M08\nM30\n"); } int read_circle( FILE *fpout, FILE *fp, int *type, char *valstr, int flag ) { double x,y,r; printf("CIRCLE\n"); RN( type, valstr ); while( *type != 0 ) { switch( *type ) { case 10: sscanf( valstr, "%lf", &x ); break; case 20: sscanf( valstr, "%lf", &y ); break; case 40: sscanf( valstr, "%lf", &r ); break; default: break; } RN( type, valstr ); } if( flag == 0 ) fprintf( fpout, "G32X%05dY%05dA%05d\n", (int)(x*1000+0.5), (int)(y*1000+0.5), (int)(r*1000+0.5)); } int find_y( FILE *fp, double *x, double *y, double *r ) { int type; char valstr[10000]; *x = *y = *r = 1001.0; for(;;) { RN( &type, valstr ); switch( type ) { case 10: sscanf( valstr, "%lf", x ); break; case 20: sscanf( valstr, "%lf", y ); return; break; case 42: sscanf( valstr, "%lf", r ); *r = ( fabs(*r) < 1.0 ) ? *r : 0.0; break; default: } } } int read_lwpoly( FILE *fpout, FILE *fp, int *type, char *valstr, int flag ) { int num_point,i; double ox, oy, or, x, y, r, radius; /* find num_point tag and save */ printf("LWPOLY\n"); RN( type, valstr ); while( *type != 90 ) { if( *type == 0 ) { fprintf( stderr, "Looking for 90 found 0!\n"); exit( 1 ); } RN( type, valstr ); } sscanf( valstr, "%d", &num_point ); find_y( fp, &ox, &oy, &or ); if ( flag == 1 ) fprintf(fpout, "G00X%05dY%05d\nG42\nM15\n", (int)(ox*1000+0.5),(int)(oy*1000+0.5) ); for( i = 0; i < num_point -1; i++ ) { find_y( fp, &x, &y, &r ); if( r > 1000.0 ) { /* straight line */ if ( flag == 1 ) fprintf(fpout, "G01X%05dY%05d\n", (int)(x*1000+0.5),(int)(y*1000+0.5) ); } else { /* arc */ if( r == 0.0 ) { radius = 0.5*sqrt( (x-ox)*(x-ox)+(y-oy)*(y-oy) ); } else { radius = 0.5*sqrt( (x-ox)*(x-ox)+(y-oy)*(y-oy) ) / (2*r / (1+r*r ) ); } if ( flag == 1 ) { fprintf( fpout, "%s", (radius>=0.0)?"G03":"G02"); fprintf( fpout, "X%05dY%05dA%05d\n", (int)(x*1000+0.5),(int)(y*1000+0.5), (int)(fabs(radius)*1000+0.5 ) ); } } or = r; ox = x; oy = y; } RN( type, valstr ); while( *type != 0 ) RN( type, valstr ); if ( flag == 1 ) { fprintf( fpout, "M17\nG40\n"); } } int write_cnc( FILE *fpout, char *file, int flag ) { FILE *fp; int a, type; float f,x,y,r,or,ox,oy,radius; char str[10000], valstr[10000]; fp = fopen( file, "r" ); fscanf( fp, "%s", valstr ); while( strcmp( valstr, "ENTITIES" ) != 0 ) { fscanf( fp, "%s", valstr ); } printf("Found Entities\n"); RN( &type, valstr ); while( strcmp( valstr, "ENDSEC" ) != 0 ) { if( type == 0 ) { if( strcmp( valstr, "CIRCLE" ) == 0 ) { read_circle( fpout, fp, &type, valstr, flag ); } else if( strcmp( valstr, "LWPOLYLINE" ) == 0 ) { read_lwpoly( fpout, fp, &type,valstr, flag); } else { RN( &type, valstr ); } } else { RN( &type, valstr ); } } fclose( fp ); } int read_poly( FILE *fpout, FILE *fp, int *type, char *valstr ) { int a,p1; double f,x,y,r,or,ox,oy,radius; char str[10000]; fscanf( fp, "%s", str ); or = 1001.0; ox = 0.0; oy = 0.0; parse_coord( fp, &x, &y, &r ); p1 = 1; while( strcmp( str, "SEQEND" ) != 0 ) { if( or < 1000.0 ) { if( or == 0.0 ) { radius = 0.5*sqrt( (x-ox)*(x-ox)+(y-oy)*(y-oy) ); } else { radius = 0.5*sqrt( (x-ox)*(x-ox)+(y-oy)*(y-oy) ) / (2*or / (1+or*or ) ); } fprintf( fpout, "%s", (radius>=0.0)?"G03":"G02"); fprintf( fpout, "X%05dY%05dA%05d\n", (int)(x*1000+0.5),(int)(y*1000+0.5), (int)(fabs(radius)*1000+0.5 ) ); } else { if( p1 == 1 ) { fprintf(fpout, "G00X%05dY%05d\nG42\nM15\n", (int)(x*1000+0.5),(int)(y*1000+0.5) ); } else { fprintf(fpout, "G01X%05dY%05d\n", (int)(x*1000+0.5),(int)(y*1000+0.5) ); } } p1 = 0; or = r; ox = x; oy = y; fscanf( fp, "%s", str ); parse_coord( fp, &x, &y, &r ); } fprintf( fpout, "M17\nG40\n" ); } int parse_coord( FILE *fp, float *x, float *y, float *r ) { int code; float f; *x = *y = *r = 1001.0; fscanf( fp, "%d", &code ); while( code != 0 ) { fscanf( fp, "%f", &f ); switch( code ) { case 10: *x = f; break; case 20: *y = f; break; case 42: *r = (fabs((double)f)<1.0)?f:0.0; break; } fscanf( fp, "%d", &code ); } }