/* * DXF2FIG : Converter for Autocad DXF format to FIG format. * Aug 1990 : Atari ST and VAX/VMS version by KL * Nov 2003 : Modified for Linux and Xfig by KL * Copyright (c) 1990-2003 by Kees Lemmens * * This software is distributed under the Gnu Public License and as such * may be copied, redistributed and modified freely as long as the original * Copyright above is retained and as long as any software derived from * this is distributed under the Gnu Public License as well. Use of this * software in any patented or closed source software is strictly * prohibited. * * For more information see the GPL : http://www.gnu.org/licenses/gpl.txt * */ #include #include #include #include #include #include "dxfstuff.h" /* Some global (static) variables */ static FILE *output; static int gpapersize; /* papersize */ static Paper formats[] = { { "A0" , 118.89 , 84.10 , 46.81 , 33.11 }, { "A1" , 84.10 , 59.41 , 33.11 , 23.39 }, { "A2" , 59.41 , 42.02 , 23.39 , 16.54 }, { "A3" , 42.02 , 29.70 , 16.54 , 11.69 }, { "A4" , 29.70 , 20.99 , 11.69 , 8.26 } }; /* End global variables */ long lookupcolor(int color) { switch(color) { case 0: return 0; // byblock or white case 1: return 4; // red case 2: return 6; // yellow case 3: return 2; // green case 4: return 3; // cyan case 5: return 1; // blue case 6: return 5; // magenta case 7: return 0; // black case 8: return 7; // gray case 15: return 7; // white default: return 0; } } int lookupltype(int ltype) { switch(ltype) { case DXFSOLID : return 0; case DXFDASHED : return 1; case DXFDOTTED : return 2; case DXFDOTDASHED: return 3; default : return 0; } } int lookuphorjust(int txthjst) { return txthjst; /* same coding as in dxfparser */ } int getxres() { return (int)(1200*formats[gpapersize].wdtin - 1200); /* Paper format at 1200 dpi */ } int getyres() { return (int)(1200*formats[gpapersize].hgtin) - 1200; /* Paper format at 1200 dpi */ } int yflip(int y) { /* xfig starts at upperleft corner and not at lowerleft corner ! */ return getyres() - y - 1200; } void dolineheader(Plotdata *d) { if(verbose >= 2) debug(" Linetype %d, width %d",d->ltype,d->width); fprintf(output,"2 1 %d %d %d %d %d %d %d %f %d %d %d %d %d %d\n" , d->ltype /* line_style */ , d->width /* thickness */ , (int)d->rgbcolor /* pen_color */ , 0 /* fill_color */ , d->depth /* depth */ , 0 /* pen_style (unused) */ , -1 /* area_fill */ , 0.0 /* style_val */ , 0 /* join_style */ , 0 /* cap_style */ , -1 /* radius */ , 0 /* forward_arrow */ , 0 /* backward_arrow */ , d->nrvrt /* npoints */ ); } void dopoint(Plotdata *d) { debug("Doing Dot ..."); if( filter(d) > 0) return; if(verbose >= 2) debug(" Dot : %d, %d ",d->xi[0],d->yi[0]); /* Just write a line with zero length : */ d->nrvrt = 2; dolineheader(d); fprintf(output,"%d %d " ,d->xi[0],yflip(d->yi[0])); fprintf(output,"%d %d \n",d->xi[0],yflip(d->yi[0])); } void doline(Plotdata *d) { debug("Doing Line ..."); if( filter(d) > 0) return; d->nrvrt = 2; dolineheader(d); fprintf(output,"%d %d " ,d->xi[0],yflip(d->yi[0])); fprintf(output,"%d %d \n",d->xi[1],yflip(d->yi[1])); } void dopline(Plotdata *d) { int t; debug("Doing Polyline with %d vertices ...",d->nrvrt); if( filter(d) > 0) return; dolineheader(d); for(t=0;tnrvrt;t++) fprintf(output,"%d %d \n",d->xvrt[t],yflip(d->yvrt[t])); } void dosolid(Plotdata *d) { debug("Doing Solid ..."); if( filter(d) > 0) return; d->nrvrt = 5; dolineheader(d); fprintf(output,"%d %d " ,d->xi[0],yflip(d->yi[0])); fprintf(output,"%d %d \n",d->xi[1],yflip(d->yi[1])); fprintf(output,"%d %d " ,d->xi[2],yflip(d->yi[2])); fprintf(output,"%d %d " ,d->xi[3],yflip(d->yi[3])); fprintf(output,"%d %d \n",d->xi[0],yflip(d->yi[0])); } void do3dface(Plotdata *d) { debug("Doing 3DFace ..."); dosolid(d); } void docircle(Plotdata *d) { debug("Doing Circle ..."); if( filter(d) > 0) return; /* Note that this routine can also plots ellipses ! */ fprintf(output,"1 1 %d %d %d %d %d %d %d %f %d %f " , d->ltype /* line_style */ , d->width /* thickness */ , (int)d->rgbcolor /* pen_color */ , 0 /* fill_color */ , d->depth /* depth */ , 0 /* pen_style (unused) */ , -1 /* area_fill */ , 0.0 /* style_val */ , 1 /* direction */ , 0.0 /* angle x-axis */ ); fprintf(output,"%d %d %d %d %d %d %d %d \n" , d->xi[0] /* center x & y */ , yflip(d->yi[0]) , d->rx /* radius x & y */ , d->ry , d->xi[0] + d->rx , yflip(d->yi[0]) , d->xi[0] , yflip(d->yi[0] + d->ry) ); } void doarc(Plotdata *d) { float hangle; debug("Doing Arc ..."); if( filter(d) > 0) return; // d->nrvrt = 3; // dolineheader(d); /* for testing */ fprintf(output,"5 1 %d %d %d %d %d %d %d %f %d %d %d %d %f %f " , d->ltype /* line_style */ , d->width /* thickness */ , (int)d->rgbcolor /* pen_color */ , 0 /* fill_color */ , d->depth /* depth */ , 0 /* pen_style (not used) */ , -1 /* area_fill */ , 0.0 /* style_val */ , 0 /* cap_style */ , 0 /* direction (1=counterclockwise) */ , 0 /* forward_arrow */ , 0 /* backward_arrow */ , (float)d->xi[0] /* center x & y */ , (float)yflip(d->yi[0]) ); while(d->angle1>d->angle2) d->angle1-=PI2; hangle = (d->angle1 + d->angle2) / 2; /* clockwise orientation : */ fprintf(output,"%d %d " ,(int)(d->xi[0]+d->rx*cos(d->angle2)), yflip((int)(d->yi[0]+d->ry*sin(d->angle2)))); fprintf(output,"%d %d " ,(int)(d->xi[0]+d->rx*cos(hangle)), yflip((int)(d->yi[0]+d->ry*sin(hangle)))); fprintf(output,"%d %d \n",(int)(d->xi[0]+d->rx*cos(d->angle1)), yflip((int)(d->yi[0]+d->ry*sin(d->angle1)))); } void dotext(Plotdata *d) { debug("Doing Text ... : %s",d->text); if( filter(d) > 0) return; fprintf(output,"4 %d %d %d %d %d %f %f %d %d %d " , d->txthjst /* horz. text justification */ , (int)d->rgbcolor /* pen_color */ , d->depth /* depth */ , 0 /* pen_style (unused) */ , 0 /* font */ , (double)d->theight/10 /* font_size */ , d->angle1 /* angle */ , 4 /* font_flags 4=PS */ , d->theight /* height */ , 0 /* length */ ); fprintf(output,"%d %d " ,d->xi[0],yflip(d->yi[0])); fprintf(output,"%s\\001\n",d->text); } void openblock(Plotdata *d) { fprintf(output,"6 0 %d %d %d %d\n" ,d->xi[0] ,d->yi[0] ,d->xi[0] /* doesn't seem to make any difference */ ,d->yi[0] ); } void closeblock(void) { fprintf(output,"-6 \n"); } void dofigheader(char *figname, int papersize) { if(papersize < 0 || papersize > 4) errorexit("Illegal papersize : A%d",papersize); gpapersize = papersize; /* set global variable for setxres() and setyres() */ if(verbose >= 3) output = stdout; else { /* Append a fig extension if not supplied : */ if(strstr(figname,".fig") == NULL) strcat(figname,".fig\0"); if((output=fopen(figname,"w"))==NULL) /* global variable */ errorexit("Cannot open outputfile %s \n",figname); } fprintf(output, "#FIG 3.2\n" "Landscape\n" "Center\n" "Metric\n" "%s\n" "100.00\n" /* export and print magnification, % */ "Single\n" /* multiple-page ? */ "-2\n" /* transparent color -3=backgr, -2=None, -1=Default */ "#Generated by dxf2fig " VERSION " (c) Kees Lemmens, " MODDATE "\n" "1200 2\n" /* resolution (DPI) and coord_system (2: upper left) */ ,formats[papersize].name); } void usage(char *basename,char *title) { fprintf(stderr,"\n%s\n",title); fprintf(stderr, "\n Usage : %s [[options] ]\n\n" " Options: -p <1-3> : viewplane , 1=xy, 2=xz, 3=yz\n" " -f <0-4> : papersize , 0=A0, 1=A1, ... , 4=A4\n" " -v [-v ...] : give (lots of) debug information\n" ,basename); fprintf(stderr,filterhelp()); exit(1); } int main(int argc, char *argv[]) { char *title= " *** CONVERT DXFFORMATS (AUTOCAD-DRAWINGS) TO XFIG ***\n" " KL, Jan 1990; " VERSION " (Linux version); KL " MODDATE; char *basename=argv[0]; /* should be preserved from option parser */ char dxfname[STRSIZE]; char figname[STRSIZE]; char *p; int x; int viewp; int papersize; char *filters=""; /* no filter */ papersize = 1; viewp = 1; /* defaults */ #if DEBUG >= 2 verbose = 2; #endif if(argc == 1) { printf("\n%s\n",title); printf("\nDXF input name (excl .dxf) : "); scanf("%250s",dxfname); printf("\nFIG output name (excl .fig) : "); scanf("%250s",figname); printf("\nViewplane 3D (1=xy,2=xz,3=yz) : "); scanf("%1d",&viewp); printf("\nPaper size (0=A0, 1=A1, ..., 4=A4) : "); scanf("%1d",&papersize); while(fgetc(stdin) != '\n'); /* empty stdin buffer (linebuffer) */ } else { /* parse options */ x=argc; while(--x>0) { if(*argv[1]=='-') { switch(*(++argv[1])) { case 'p': if(argc <= 2) usage(basename,title); sscanf(argv[2],"%d",&viewp); ++argv; --argc; --x; break; case 'f': if(argc <= 2) usage(basename,title); sscanf(argv[2],"%d",&papersize); ++argv; --argc; --x; break; case 'i': if(argc <= 2) usage(basename,title); filters = argv[2]; ++argv; --argc; --x; break; case 'v': verbose++; break; default: usage(basename,title); break; } ++argv; --argc; } } if(argc < 2) usage(basename,title); sscanf(argv[1],"%250s",dxfname); if(argc == 3) sscanf(argv[2],"%250s",figname); else { strcpy(figname,dxfname); /* search for .dxf or .DXF extensions : */ p = strstr(figname,".dxf"); if(p == NULL) p = strstr(figname,".DXF"); if(p == NULL) strcat(figname,".fig"); else /* replace .dxf extension with .fig */ strcpy(p,".fig"); } } dofigheader(figname,papersize); startparser(dxfname,viewp,filters); if(output != NULL) fclose(output); exit(0); /* Only to stop compiler complains */ }