#include<STDIO.H> #include<STDLIB.H> #include<STRING> #include<MATH.H> #define PI 3.14159265 //用UltradiusaEdit打开DXF文件,查找LINE、CIradiusCLE等命令可以可发现,LINE到坐标点之间有 //12行的无用信息,转换成SCradius时,需要忽略掉 void Ignor(FILE *fp,int n,char name[80],char value[80]) { for(int i=0;i<n;i++)//读取2n行,但是不读入到fp2 { fgets(name,80,fp); fgets(value,80,fp); } } int main() { FILE *fp1,*fp2; double temp,pointx,pointy,pointz,radius;//temp用来存LINE等命令中的坐标值,pointx、pointy用来存取CIRCLE //ARC等命令中的圆心坐标,radius用来存取圆的半径; int vertex;//存储多义线中的顶点数 int closetag;//用于判断多义线时候封闭,closetag=1为封闭状态,closetag=0则不封闭 char name[80],value[80]; fp1=fopen("test.dxf","r"); fp2=fopen("Translated.scr","w");//创建并打开SCR文件 if (fp1==NULL) { printf("1Can not open this file!\n"); return 0; } if (fp2==NULL) { printf("2Can not open this file!\n"); return 0; } while(!feof(fp1)) { fgets(name,80,fp1); fgets(value,80,fp1); if (strcmp(value,"POINT\n")==0)//找到POINT命令 { Ignor(fp1,6,name,value);//跳到表示坐标的行 temp=atof(value);//获取点X坐标 fprintf(fp2,"point %lf,",temp);//写入X坐标 Ignor(fp1,1,name,value);//跳到表示Y坐标的行 temp=atof(value);//获取Y坐标 fprintf(fp2,"%lf,",temp);//写入Y坐标 Ignor(fp1,1,name,value);//跳到表示Z坐标的行 temp=atof(value);//获取Z坐标 fprintf(fp2,"%lf\n",temp);//写入Z坐标 } if (strcmp(value,"LINE\n")==0)//处理LINE命令 { Ignor(fp1,6,name,value);//跳至坐标表示行 temp=atof(value);//获取线起点X坐标 fprintf(fp2,"line %lf,",temp);//写入X坐标 Ignor(fp1,1,name,value);//跳到Y坐标表示行 temp=atof(value);//获取Y坐标 fprintf(fp2,"%lf,",temp);//写入Y坐标 Ignor(fp1,1,name,value);//跳至Z坐标表示行 temp=atof(value);//获取Z坐标 fprintf(fp2,"%lf ",temp);//写入Z坐标 //接下来对LINE的终点进行操作 Ignor(fp1,1,name,value); temp=atof(value); fprintf(fp2,"%lf,",temp); Ignor(fp1,1,name,value);//跳到Y坐标表示行 temp=atof(value);//获取Y坐标 fprintf(fp2,"%lf,",temp);//写入Y坐标 Ignor(fp1,1,name,value);//跳至Z坐标表示行 temp=atof(value);//获取Z坐标 fprintf(fp2,"%lf \n",temp);//写入Z坐标 } if (strcmp(value,"CIRCLE\n")==0)//处理CRICLE命令 { Ignor(fp1,6,name,value); pointx=atof(value);//获取圆形坐标X fprintf(fp2,"circle %lf,",pointx); Ignor(fp1,1,name,value); pointy=atof(value);//获取圆心坐标Y fprintf(fp2,"%lf,",pointy); Ignor(fp1,1,name,value); pointz=atof(value);//获取圆心坐标Z fprintf(fp2,"%lf ",pointz); Ignor(fp1,1,name,value); radius=atof(value);//获取半径 fprintf(fp2,"%lf\n",radius); } if (strcmp(value,"ARC\n")==0)//处理ARC命令 { Ignor(fp1,6,name,value); pointx=atof(value);//获取圆弧圆心X坐标 fprintf(fp2,"arc c %lf,",pointx); Ignor(fp1,1,name,value); pointy=atof(value);//获取圆弧圆心Y坐标 fprintf(fp2,"%lf,",pointy); Ignor(fp1,1,name,value); pointz=atof(value);//获取圆弧圆心Z坐标 fprintf(fp2,"%f ",pointz); Ignor(fp1,1,name,value); radius=atof(value);//获取圆弧半径值 Ignor(fp1,2,name,value); temp=atof(value);//圆弧起始角度 fprintf(fp2,"%f,%f ", (cos(temp*PI/180.0))*radius+pointx,(sin(temp*PI/180.0))*radius+pointy);//写入圆弧起点 Ignor(fp1,1,name,value); temp=atof(value); fprintf(fp2,"%f,%f\n", (cos(temp*PI/180.0))*radius+pointx,(sin(temp*PI/180.0))*radius+pointy);//写入圆弧终点 } if (strcmp(value,"LWPOLYLINE\n")==0)//处理多义线命令 { Ignor(fp1,6,name,value); vertex=(int)(atof(value));//获取顶点数 Ignor(fp1,1,name,value); closetag=(int)(atof(value));//获取多义线封闭状态 Ignor(fp1,1,name,value); fprintf(fp2,"pline "); for (int i=0;i<vertex;i++) { Ignor(fp1,1,name,value); temp=atof(value);//读取顶点X坐标 fprintf(fp2,"%lf,",temp); Ignor(fp1,1,name,value); temp=atof(value);//读取顶点Y坐标 fprintf(fp2,"%lf ",temp); } if (closetag==1)//如果是封闭的就输出C命令 { fprintf(fp2,"c\n"); } else fprintf(fp2,"\n"); } } fprintf(fp2,"zoom e "); fclose(fp1); fclose(fp2);//关闭两个文件 return 0; }