|
本次完成的是dxf文件的修改。
问题:
将上题的图形用DXFOUT命令生成完整的DXF文件。编写一C/C++语言程序,使之能够根据用户输入的时间,通过修改DXF文件来控制时针和分针的位置。交C/C++语言源程序。
利用dxfout命令后,此时的dxf文件已经是完整的了,从几十行变成了2347行,内容就比较复杂了:
代码如下:
#include <stdio.h>
#include <string.h>
#include <math.h>
const double PI=3.14159265358;
void main()
{
FILE *fp1,*fp2;//文件指针
double h,m,r;
char name[80];//组代码字符串
char value[80];//组值字符串
int tag;//中间控制变量
printf("Please input a number for the hour:\n");
scanf("%lf",&h);
printf("\nPlease input a number for the minutes:\n");
scanf("%lf",&m);
h=h+m/60;//对h进行处理
fp1 = fopen("clock.dxf","r");//读取原始DXF文件
if(fp1 == NULL)
{
printf("Can not open this file1 !\n");
return;
}
fp2 = fopen("采用clock.dxf","w"); //修改后写入新的DXF文件
if(fp2 == NULL)
{
printf("Can not open this file2 !\n");
return;
}
tag = 0;
while( !feof( fp1 ) )
{
fgets(name,80,fp1);// 读入组代码
fgets(value,80,fp1); // 读入组值
fprintf(fp2,"%s",name);// 写入组代码
fprintf(fp2,"%s",value);// 写入组值
if(strcmp(value,"CIRCLE\n") == 0)//找第一个圆实体
{
while(1)
{
fgets(name,80,fp1);
fgets(value,80,fp1);
fprintf(fp2,"%s",name);
fprintf(fp2,"%s",value);
if(strcmp(name," 40\n") == 0) // 读取半径
{
r = double (atof(value));
printf("found radius r=%f\n",r);
break;
}
}
}
if(strcmp(value,"LINE\n") == 0)//找到线实体
{
while(1)
{
fgets(name,80,fp1);
fgets(value,80,fp1);
fprintf(fp2,"%s",name);
fprintf(fp2,"%s",value);// 写入组值
if((strcmp(name," 10\n") == 0)&&(strcmp(value,"0.0\n") == 0))
{
fgets(name,80,fp1);
fgets(value,80,fp1);
fprintf(fp2,"%s",name);
fprintf(fp2,"%s",value);
if((strcmp(name," 20\n") == 0)&&(strcmp(value,"0.0\n") == 0))//找到以坐标原点为起点的线实体
{
fgets(name,80,fp1);
fgets(value,80,fp1);
fprintf(fp2,"%s",name);
fprintf(fp2,"%s",value);//满足z坐标
if(tag==0)//hour
{
fgets(name,80,fp1);
fgets(value,80,fp1);
fgets(name,80,fp1);
fgets(value,80,fp1);//消耗4行
fprintf(fp2," 11\n%f\n",0.4*r*cos((90-30*h)*PI/180));
fprintf(fp2," 21\n%f\n",0.4*r*sin((90-30*h)*PI/180));
tag = 1;
break;
}
if(tag==1)//minute
{
fgets(name,80,fp1);
fgets(value,80,fp1);
fgets(name,80,fp1);
fgets(value,80,fp1);//消耗4行
fprintf(fp2," 11\n%f\n",0.6*r*cos((90-6*m)*PI/180));
fprintf(fp2," 21\n%f\n",0.6*r*sin((90-6*m)*PI/180));
break;
}
}
if((strcmp(name," 20\n") == 0)&&(strcmp(value,"0.0\n") == 1))
{
break;
}
}
if((strcmp(name," 10\n") == 0)&&(strcmp(value,"0.0\n") == 1))
{
break;
}
}
}
}
} |
|