C语言直接写DWG图形文件接口.docx
《C语言直接写DWG图形文件接口.docx》由会员分享,可在线阅读,更多相关《C语言直接写DWG图形文件接口.docx(29页珍藏版)》请在冰豆网上搜索。
C语言直接写DWG图形文件接口
C语言直接写DWG图形文件接口
[返回主页||作品建筑|作品符号|作品动画]
/*--------------------------------------------------------------------------
INTerfacetoAutoCADDWGdrawingformat.
Environment:
PC,DOS,TurboC,AutoCAD10~
ContainFiles:
INT_DWG.CDWG文件访问函数库的源码。
INT_DWG.HDWG文件访问函数库的头文件,
由外部DEMO.C文件引用。
INT_DWG.OBJDWG文件访问函数库的连接用目标码,
INT_DWG.C编译而成。
DWG.HEDDWG文件的头文件。
用户可根据自己的需要生成自己的"样板图",改名
*.DWG为DWG.HED作为头文件。
DEMO.C调用实例源码。
该例为计算微波在两地间的通过能力。
运行该程序后,程序读取数据文件DEMO.DAT文件,
经处理后,利用INT_DWG接口程序自动生成图形
文件DEMO.DWG。
DEMO.DAT实例用的原始数据文件
DEMO.DWG实例产生的DWG结果文件
CreateDate:
1993.3.29304
LastUpdate:
1994.1.16320
LastUpdate:
17Dpbgn/Dpvert/Dpend()352
26Dvtext()384
WrittedbyJYC.
-----------------------------------------------------------------------*/
#include
#include
#include
#include
#include
#defineDISTANCEdouble
#defineLENGTHdouble
#defineANGLEdouble
#defineHEIGHTdouble
#defineWIDTHdouble
#defineSCALEdouble
#defineRADIUSdouble
#defineNUMBERint
#defineMAXANGMAXFLOAT
#definediz(a,b,c,d)((a)*(d)-(b)*(c))
#definesqr(x)(x)*(x)
#definePolar(p,p1,a,l)(p).x=(p1).x+(l)*cos(a);(p).y=(p1).y+(l)*sin(a)
#defineDistance(p,p2)sqrt(sqr((p2).x-(p).x)+sqr((p2).y-(p).y))
#defineMidpt(pt,pt1,pt2)pt-x=(pt1-x+pt2-x)/2;pt-y=(pt1-y+pt2-y)/2
#defineSetLayer(x)D_Layer=x
#defineSetWidth(x)D_Width=x
#defineSetTexts(st,ht)D_Tstyle=st;D_Theight=ht
#defineSetPoint(pt,u,v)pt.x=u;pt.y=v
#defineSetQpt(pt,p1)memcpy(&pt,&p1,sizeof(pt))
#defineSetptxy(p,p1,x,y)(p).x=(p1).x+x;(p).y=(p1).y+y
typedefstruct{doublex,y;}
POINT;
typedefenum{FALSE,TRUE}
BOOL;
voidOpenDwg(char*dwgn);
voidCloseDwg(void);
voidSetPts(NUMBERptn,POINT*pts,...);
voidDline(POINTpt1,POINTpt2);
voidDpline(NUMBERptn,POINT*pts);
voidDppts(NUMBERptn,...);
voidDpbgn(BOOLclosed);
voidDpvert(POINTpt);
voidDpvertxy(doublex,doubley);
voidDpend(void);
voidDcircle(POINTpt1,DISTANCEr);
voidDarc(POINTpt1,RADIUSr,ANGLEsa,ANGLEse);
voidDtext(POINTpt1,ANGLEang,char*txt);
voidDvtext(POINTpt1,char*txt);
voidDinsert(NUMBERbn,POINTpt1,SCALEsx,SCALEsy,ANGLEang);
voidDprintf(POINTpt1,ANGLEang,char*format,...);
voidaxbyc(POINT*pt1,POINT*pt2,float*a,float*b,float*c);
BOOLintspt(POINT*pt,POINT*p11,POINT*p12,POINT*p21,POINT*p22);
ANGLEangle(POINT*pt1,POINT*pt2);
DISTANCEperdist(POINT*pt,POINT*p1,POINT*p2);
staticvoidfilcpy(longlen);
staticlongfilpcp(longdt);
staticvoidfilecp(void);
staticFILE*dwgh,*dwgf,*dwgt;
staticlongent_p,pln_p;
NUMBERD_Layer=0;
NUMBERD_Tstyle=0;
NUMBERD_Tjustify=0;
HEIGHTD_Theight=1.0;
WIDTHD_Width=0.0;
voidOpenDwg(char*dwgn)
{
charfn[20];
strcpy(fn,dwgn);
strcat(fn,".dwg");
dwgh=fopen("dwg.hed","rb");
dwgf=fopen(fn,"wb");
dwgt=fopen("dwg.tmp","wb");
fseek(dwgh,0,SEEK_SET);
filcpy(0x14);/*Head14h*/
ent_p=filpcp(0);/*ent_p*/
}
voidCloseDwg(void)
{
longent_l,
tab_p,
blk_t_p,
lay_t_p,
sty_t_p,
ltp_t_p,
viw_t_p,
blk_p,
dwg_l;
FILE*tmpf;
fclose(dwgt);dwgt=fopen("dwg.tmp","rb");
ent_l=filelength(fileno(dwgt))-0x28;
fseek(dwgh,0x18,SEEK_SET);
filpcp(ent_l);/*tab_p*/
filpcp(ent_l);/*blk_d_p*/
filpcp(0);/*unknown*/
filpcp(ent_l);/*dwg_l*/
filpcp(0);filcpy(6);/*unknown*/
filpcp(ent_l);filcpy(6);/*blk_t_p*/
filpcp(ent_l);filcpy(6);/*lay_t_p*/
filpcp(ent_l);filcpy(6);/*sty_t_p*/
filpcp(ent_l);filcpy(6);/*ltp_t_p*/
filpcp(ent_l);filcpy(6);/*vie_t_p*/
filcpy(0x4AE);/*SystemVariable*/
tmpf=dwgh;dwgh=dwgt;
fseek(dwgh,0,SEEK_SET);
filcpy(ent_l+0x28);
dwgh=tmpf;
fseek(dwgh,0x28,SEEK_CUR);
filecp();
fclose(dwgh);fclose(tmpf);fclose(dwgt);fclose(dwgf);
/*unlink("dwg.tmp");*/
}
voidDline(POINTpt1,POINTpt2)
{
charstr[8]={0x01,0x04,0x28,0,0,0,0,0};
str[4]=D_Layer;
fwrite(str,8,1,dwgt);
fwrite(&pt1,16,1,dwgt);
fwrite(&pt2,16,1,dwgt);
}
voidDpline(NUMBERptn,POINT*pts)
{
charstr[8]={0x13,0x80,0x18,0,0,0,6,0},
stc[9]={0x13,0x80,0x19,0,0,0,7,0,1},
sti[8]={0x14,0x00,0x18,0,0,0,0,0},
stn[8]={0x11,0x00,0x0C,0,0,0,0,0};
NUMBERi;
str[4]=D_Layer;
stc[4]=D_Layer;
sti[4]=D_Layer;
stn[4]=D_Layer;
pln_p=ftell(dwgt)+ent_p;
if(ptn0)
fwrite(str,8,1,dwgt);
else
fwrite(stc,9,1,dwgt);
fwrite(&D_Width,8,1,dwgt);
fwrite(&D_Width,8,1,dwgt);
for(i=0;ifwrite(sti,8,1,dwgt);
fwrite(pts+i,16,1,dwgt);
}
fwrite(stn,8,1,dwgt);
fwrite(&pln_p,4,1,dwgt);
}
voidDppts(NUMBERptn,...)
{
POINT*pts;
va_listap;
va_start(ap,1);
pts=(POINT*)ap;
Dpline(ptn,pts);
}
voidDpbgn(BOOLclosed)
{
charstr[8]={0x13,0x80,0x18,0,0,0,6,0},
stc[9]={0x13,0x80,0x19,0,0,0,7,0,1};
str[4]=D_Layer;
stc[4]=D_Layer;
pln_p=ftell(dwgt)+ent_p;
if(closed)fwrite(stc,9,1,dwgt);
elsefwrite(str,8,1,dwgt);
fwrite(&D_Width,8,1,dwgt);
fwrite(&D_Width,8,1,dwgt);
}
voidDpvert(POINTpt)
{
charsti[8]={0x14,0x00,0x18,0,0,0,0,0};
sti[4]=D_Layer;
fwrite(sti,8,1,dwgt);
fwrite(&pt,1,sizeof(POINT),dwgt);
}
voidDpvertxy(doublex,doubley)
{
charsti[8]={0x14,0x00,0x18,0,0,0,0,0};
sti[4]=D_Layer;
fwrite(sti,8,1,dwgt);
fwrite(&x,1,sizeof(double),dwgt);
fwrite(&y,1,sizeof(double),dwgt);
}
voidDpend(void)
{
charstn[8]={0x11,0x00,0x0C,0,0,0,0,0};
stn[4]=D_Layer;
fwrite(stn,8,1,dwgt);
fwrite(&pln_p,4,1,dwgt);
}
voidDcircle(POINTpt1,RADIUSr)
{
charstr[8]={03,0,0x20,0,0,0,0,0};
str[4]=D_Layer;
fwrite(str,8,1,dwgt);
fwrite(&pt1,16,1,dwgt);
fwrite(&r,8,1,dwgt);
}
voidDarc(POINTpt1,RADIUSr,ANGLEsa,ANGLEse)
{
charstr[8]={8,0,0x30,0,0,0,0,0};
str[4]=D_Layer;
fwrite(str,8,1,dwgt);
fwrite(&pt1,16,1,dwgt);
fwrite(&r,8,1,dwgt);
fwrite(&sa,8,1,dwgt);
fwrite(&se,8,1,dwgt);
}
voidDtext(POINTpt1,ANGLEang,char*txt)
{
charstr[8]={07,0,0x2b,0,0,0,0x69,0};
intstrl=strlen(txt);
str[2]=0x3C+strlen(txt);
str[4]=D_Layer;
fwrite(str,8,1,dwgt);
fwrite(&pt1,16,1,dwgt);
fwrite(&D_Theight,8,1,dwgt);
fwrite(&strl,2,1,dwgt);
fwrite(txt,strl,1,dwgt);
fwrite(&ang,8,1,dwgt);
fputc(D_Tstyle,dwgt);
fputc(D_Tjustify,dwgt);
fwrite(&pt1,16,1,dwgt);
}
voidDvtext(POINTpt1,char*txt)
{
charhz[3];
POINTpt;
SetPoint(pt,pt1.x,pt1.y-D_Theight);
while(*txt){
if(*txt<0){
hz[0]=*txt++;
hz[1]=*txt++;
hz[2]=0;}
else{
hz[0]=*txt++;
hz[1]=0;
}
Dtext(pt,0,hz);
pt.y-=1.2*D_Theight;
}
}
voidDprintf(POINTpt1,ANGLEang,char*format,...)
{
va_listarg_ptr;
charoutput[256];
va_start(arg_ptr,format);
vsprintf(output,format,arg_ptr);
Dtext(pt1,ang,output);
}
voidDinsert(NUMBERbn,POINTpt1,SCALEsx,SCALEsy,ANGLEang)
{
charstr[8]={0x0E,0x00,0x3A,0,0,0,0x0F,0};
str[4]=D_Layer;
fwrite(str,8,1,dwgt);
fwrite(&bn,2,1,dwgt);
fwrite(&pt1,16,1,dwgt);
fwrite(&sx,8,1,dwgt);
fwrite(&sy,8,1,dwgt);
fwrite(&ang,8,1,dwgt);
fwrite(&sx,8,1,dwgt);
}
voidSetPts(NUMBERptn,POINT*pts,...)
{
va_listap;
va_start(ap,ptn);
memcpy(pts,ap,16*ptn);
}
voidaxbyc(POINT*pt1,POINT*pt2,float*a,float*b,float*c)
{
*a=pt1-y-pt2-y;
*b=pt2-x-pt1-x;
*c=-((*b)*(pt1-y)+(*a)*(pt1-x));
}
BOOLintspt(POINT*pt,POINT*p11,POINT*p12,POINT*p21,POINT*p22)
{
floata11,a12,b1,a21,a22,b2,r;
axbyc(p11,p12,&a11,&a12,&b1);
axbyc(p21,p22,&a21,&a22,&b2);
r=diz(a11,a12,a21,a22);
if(fabs(r)<=0.000001)
return(FALSE);
pt-x=diz(-b1,a12,-b2,a22)/r;
pt-y=diz(a11,-b1,a21,-b2)/r;
return(TRUE);
}
ANGLEangle(POINT*pt1,POINT*pt2)
{
doubledx,dy;
dx=pt2-x-pt1-x;
dy=pt2-y-pt1-y;
if(dx==0&&dy==0)
return(MAXANG);
return(atan2(dy,dx));
}
DISTANCEperdist(POINT*pt,POINT*p1,POINT*p2)
{
floatat,a2;
if((at=angle(p1,pt))==MAXANG||(a2=angle(p1,p2))==MAXANG)
return(0.0);
return(fabs(Distance(*pt,*p1)*sin(at-a2)));
}
voidfilcpy(longlen)
{
charch;
longi;
for(i=0;ifputc(fgetc(dwgh),dwgf);
}
longfilpcp(longdt)
{
longlen;
fread(&len,4,1,dwgh);
len+=dt;
fwrite(&len,4,1,dwgf);
returnlen;
}
voidfilecp(void)
{
charch;
while(!
feof(dwgh))
fputc(fgetc(dwgh),dwgf);
}
--------------------------------------------------------------------------------
/*------------------------------------------------------------------------
INT_DWG.H:
InterfaceToAutoCADDWGfileModule.
History:
1992.4.12FCGEOM
1992.11.3INT_DWG
1994.1.17Dpbgn/Dpvert/Dpend
1993.3.27ByJYCCo.
----------------------------------------------------------------------*/
#defineMAXANGMAXFLOAT
#defineDISTANCEdouble
#defineLENGTHdouble
#defineANGLEdouble
#defineHEIGHTdouble
#defineWIDTHdouble
#defineSCALEdouble
#defineRADIUSdouble
#defineNUMBERint
#definesqr(x)(x)*(x)
#definedxy(x,y)(double)(x),(double)(y)
#defineSetLayer(x)D_Layer=x
#defineSetWidth(x)D_Width=x
#defineSetTexts(st,ht)D_Tstyle=st;D_Theight=ht
#defineSetPoint(pt,u,v)pt.x=u;pt.y=v
#defineSetQpt(pt,p1)memcpy(&pt,&p1,sizeof(pt))
#definePolar(p,p1,a,l)(p).x=(p1).x+(l)*cos(a);(p).y=(p1).y+(l)*sin(a)
#defineDistance(p1,p2)sqrt(sqr((p2).x-(p1).x)+sqr((p2).y-(p1).y))
#defineMidpt(p,p1,p2)p.x=(p1.x+p2.x)/2;p.y=(p1.y+p2.y)/2
#defineAngle(p1,p2)angle(&p1,&p2)
#defineIntspt(p,s,q,u,v)intspt(&p,&s,&q,&u,&v)
#definePerdist(p,p1,p2)perdist(&p,&p1,&p2)
typedefenum{FALSE,TRUE}
BOOL;
typedefstruct{doublex,y;}
POINT;
externvoidOpenDwg(char*dwgn);
externvoidCloseDwg(void);
externvoidSetPts(NUMBERptn,POINT*pts,...);
externvoidDline(POINTpt1,POINTpt2);
externvoidDpline(NUMBERptn,POINT*pts);
externvoidDppts(NUMBERptn,...);
externvoidDpbgn(BOOLclosed);
externvoidDpvert(POINTpt);
externvoidDpvertxy(doublex,doubley);
externvoidDpend(void);
externvoidDcircl