免费视频|新人指南|投诉删帖|广告合作|地信网APP下载

查看: 2852|回复: 1
收起左侧

[技巧] C 语言直接写 DWG 图形文件接口

[复制链接]

2072

主题

100000万

铜板

363

好友

地信专家组

每一次的分离都是为了下一次的相聚

Rank: 14Rank: 14Rank: 14Rank: 14

积分
17612

精华勋章宣传勋章爱心勋章组织勋章地信元老灌水勋章荣誉会员勋章活跃勋章贡献勋章

发表于 2009-12-1 21:04 | 显示全部楼层 |阅读模式
/* --------------------------------------------------------------------------
INTerface to AutoCAD DWG drawing format.

Environment: PC, DOS, Turbo C, AutoCAD 10~

Contain Files:
INT_DWG.C DWG 文件访问函数库的源码。
INT_DWG.H DWG 文件访问函数库的头文件,
由外部 DEMO.C 文件引用。
INT_DWG.OBJ DWG 文件访问函数库的连接用目标码,
INT_DWG.C 编译而成。
DWG.HED DWG 文件的头文件。
用户可根据自己的需要生成自己的"样板图",改名
*.DWG 为 DWG.HED 作为头文件。
DEMO.C 调用实例源码。该例为计算微波在两地间的通过能力。
运行该程序后,程序读取数据文件 DEMO.DAT 文件,
经处理后,利用 INT_DWG 接口程序自动生成图形
文件 DEMO.DWG。
DEMO.DAT 实例用的原始数据文件
DEMO.DWG 实例产生的 DWG 结果文件

Create Date: 1993.3.29 304
Last Update: 1994.1.16 320
Last Update: 17 Dpbgn/Dpvert/Dpend() 352
26 Dvtext() 384
Writted by JYC.
----------------------------------------------------------------------- */

#include
#include
#include
#include
#include

#define DISTANCE double
#define LENGTH double
#define ANGLE double
#define HEIGHT double
#define WIDTH double
#define SCALE double
#define RADIUS double
#define NUMBER int
#define MAXANG MAXFLOAT

#define diz(a,b,c,d) ((a)*(d)-(b)*(c))
#define sqr(x) (x)*(x)

#define Polar(p,p1,a,l) (p).x=(p1).x+(l)*cos(a);(p).y=(p1).y+(l)*sin(a)
#define Distance(p,p2) sqrt(sqr((p2).x-(p).x)+sqr((p2).y-(p).y))
#define Midpt(pt,pt1,pt2) pt-x=(pt1-x+pt2-x)/2; pt-y=(pt1-y+pt2-y)/2

#define SetLayer(x) D_Layer=x
#define SetWidth(x) D_Width=x
#define SetTexts(st,ht) D_Tstyle=st; D_Theight=ht
#define SetPoint(pt,u,v) pt.x=u;pt.y=v
#define SetQpt(pt,p1) memcpy(&pt,&p1,sizeof(pt))
#define Setptxy(p,p1,x,y) (p).x=(p1).x+x; (p).y=(p1).y+y

typedef struct { double x,y; }
POINT;
typedef enum {FALSE,TRUE}
BOOL;

void OpenDwg (char *dwgn);
void CloseDwg (void);
void SetPts (NUMBER ptn, POINT *pts, ...);
void Dline (POINT pt1,POINT pt2);
void Dpline (NUMBER ptn,POINT *pts);
void Dppts (NUMBER ptn, ...);
void Dpbgn (BOOL closed);
void Dpvert (POINT pt);
void Dpvertxy (double x,double y);
void Dpend (void);
void Dcircle (POINT pt1,DISTANCE r);
void Darc (POINT pt1,RADIUS r,ANGLE sa,ANGLE se);
void Dtext (POINT pt1,ANGLE ang,char *txt);
void Dvtext (POINT pt1,char *txt);
void Dinsert (NUMBER bn,POINT pt1,SCALE sx,SCALE sy,ANGLE ang);
void Dprintf (POINT pt1,ANGLE ang,char *format, ...);

void axbyc(POINT *pt1,POINT *pt2,float *a,float *b,float *c);
BOOL intspt(POINT *pt,POINT *p11,POINT *p12,POINT *p21,POINT *p22);
ANGLE angle(POINT *pt1,POINT *pt2);
DISTANCE perdist(POINT *pt,POINT *p1,POINT *p2);

static void filcpy(long len);
static long filpcp(long dt);
static void filecp(void);
static FILE *dwgh,*dwgf,*dwgt;
static long ent_p,pln_p;

NUMBER D_Layer=0;
NUMBER D_Tstyle=0;
NUMBER D_Tjustify=0;
HEIGHT D_Theight=1.0;
WIDTH D_Width=0.0;

void OpenDwg(char *dwgn)
{
char fn[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); /* Head 14h */
ent_p=filpcp(0); /* ent_p */
}

void CloseDwg( void)
{
long ent_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); /* System Variable */

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"); */
}

void Dline(POINT pt1,POINT pt2)
{
char str[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);
}

void Dpline (NUMBER ptn,POINT *pts)
{
char str[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};
NUMBER i;

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;i

/* ------------------------------------------------------------------------
INT_DWG.H:

Interface To AutoCAD DWG file Module.
History: 1992.4.12 FCGEOM
1992.11.3 INT_DWG
1994.1.17 Dpbgn/Dpvert/Dpend

1993.3.27 By JYC Co.
----------------------------------------------------------------------*/
#define MAXANG MAXFLOAT
#define DISTANCE double
#define LENGTH double
#define ANGLE double
#define HEIGHT double
#define WIDTH double
#define SCALE double
#define RADIUS double
#define NUMBER int

#define sqr(x) (x)*(x)
#define dxy(x,y) (double)(x),(double)(y)

#define SetLayer(x) D_Layer=x
#define SetWidth(x) D_Width=x
#define SetTexts(st,ht) D_Tstyle=st; D_Theight=ht
#define SetPoint(pt,u,v) pt.x=u; pt.y=v
#define SetQpt(pt,p1) memcpy(&pt,&p1,sizeof(pt))

#define Polar(p,p1,a,l) (p).x=(p1).x+(l)*cos(a);(p).y=(p1).y+(l)*sin(a)
#define Distance(p1,p2) sqrt(sqr((p2).x-(p1).x)+sqr((p2).y-(p1).y))
#define Midpt(p,p1,p2) p.x=(p1.x+p2.x)/2; p.y=(p1.y+p2.y)/2
#define Angle(p1,p2) angle(&p1,&p2)
#define Intspt(p,s,q,u,v) intspt(&p,&s,&q,&u,&v)
#define Perdist(p,p1,p2) perdist(&p,&p1,&p2)

typedef enum {FALSE,TRUE}
BOOL;
typedef struct { double x,y; }
POINT;

extern void OpenDwg (char *dwgn);
extern void CloseDwg (void);
extern void SetPts (NUMBER ptn,POINT *pts, ...);
extern void Dline (POINT pt1,POINT pt2);
extern void Dpline (NUMBER ptn,POINT *pts);
extern void Dppts (NUMBER ptn, ...);
extern void Dpbgn (BOOL closed);
extern void Dpvert (POINT pt);
extern void Dpvertxy (double x,double y);
extern void Dpend (void);
extern void Dcircle (POINT pt1,DISTANCE r);
extern void Darc (POINT pt1,RADIUS r,ANGLE sa,ANGLE se);
extern void Dtext (POINT pt1,ANGLE ang,char *txt);
extern void Dvtext (POINT pt1,char *txt);
extern void Dinsert (NUMBER bn,POINT pt1,SCALE sx,SCALE sy,ANGLE ang);
extern void Dprintf(POINT pt1,ANGLE ang,char *format, ...);

extern void intspt (POINT *p,POINT *p1,POINT *p2,POINT *p3,POINT *p4);
extern void axbyc (POINT *p1,POINT *p2,float *a,float *b,float *c);
extern DISTANCE perdist(POINT *p,POINT *p1,POINT *p2);
extern ANGLE angle(POINT *p1,POINT *p2);

extern NUMBER D_Layer;
extern WIDTH D_Width;
extern HEIGHT D_Theight;
extern NUMBER D_Tstyle;
extern NUMBER D_Tjustify;

--------------------------------------------------------------------------------

/* ------------------------------------------------------------------------
DEMO.C

Micro Wave Application:

Create Date:1994.1.25 Copy from demo.c 105
Last Update: 26 MWhead/topo/build
Dborder/globe/dthtp/name/symbol
ah[3]={0,ah,ah2} parallel operate
Last Update: 27 LAY_BORD/TOPO/BUILD_0/1/2 304
#define parameters 312 proj 314

Written by JYC.
---------------------------------------------------------------------- */

#include
#include
#include
#include "int_dwg.h"

#define lnwid 15.0
#define lmargin 65.0
#define lspace 5.0
#define rmargin 35.0

enum {LAY_BORD, LAY_TOPO_0,LAY_BUILD_0,
LAY_TOPO_1,LAY_BUILD_1,
LAY_TOPO_2,LAY_BUILD_2 };
enum {BLK_ROAD,BLK_PIVOT};

void MWhead(void);
void MWtopo(void);
void MWbuild(void);
void MWend(void);
void Dborder(float x,float y);
void Dglobe(void);
void Dtopvt(DISTANCE dt,DISTANCE ht);
void Ddthtp(float dt,float ht,char sw);
void Dname(float dt,char *txt);
void Dvilage(float dt,float ht,float vw,float vh);
void Dsymbol(float dt,float ht,int bn);

float chord(float Ah,float x);

char proj[80];
FILE *fi;
DISTANCE al,ah[3],bh,step;
SCALE sx,sy;
POINT mwbp;

void main(void)
{
clrscr();
OpenDwg("demo");

MWhead();
MWtopo();
MWbuild();

MWend();
CloseDwg();
printf("\nGood Lucky!");
getch();
}

void MWhead(void)
{
char txt[80];
fi=fopen("mwave.dat","rt");
while (fscanf(fi,"%s",txt)) {
if (*txt=='*')
fgets(txt,80,fi);
else
break;
}
strcpy(proj,txt);
fgets(txt,80,fi);
fscanf(fi,"%lf%lf%lf%lf%lf%lf%lf\n\r",
&al,&bh,&sx,&sy,&(ah[1]),&(ah[2]),&step);
ah[0]=0;
printf("%s\n\r%lf,%lf,%lf,%lf,%lf,%lf,%lf\n\r",
proj,al,ah[1],ah[2],bh,sx,sy,step);
sx/=1000; sy/=1000;
Dborder(0,0);
Dglobe();
getch();
}

void MWtopo(void)
{
char txt[80];
BOOL sta=FALSE;
DISTANCE dt,ht;

while (fscanf(fi,"%s",txt)) {
if (*txt=='*') {
if (sta) break;
fgets(txt,80,fi); }
else {
if (!sta) sta=TRUE;
sscanf(txt,"%lf",&dt);
fscanf(fi,"%lf\n\r",&ht);
printf("(%8.2lf,%8.2lf)\n\r",dt,ht);
Dtopvt(dt,ht);
}
}
getch();
}

void MWbuild(void)
{
char sw,txt[80];
BOOL sta=FALSE;
DISTANCE dt,ht,vw,vh;

while (fscanf(fi,"%s",txt)) {
if (*txt=='*') {
if (sta) break;
fgets(txt,80,fi); }
else {
if (!sta) sta=TRUE;
sw=*txt;
fscanf(fi,"%lf%lf",&dt,&ht);
printf("%c (%8.2lf,%8.2lf) ",sw,dt,ht);
Ddthtp(dt,ht,sw);
switch (sw) {
case 'V':
fscanf(fi,"%s%lf%lf\n\r",txt,&vw,&vh);
printf("(%8.2lf,%8.2lf) %s\n\r",vw,vh,txt);
Dvilage(dt,ht,vw,vh);
Dname(dt,txt);
break;
case 'S':
case 'E':
fscanf(fi,"%s\n\r",txt);
printf("%s\n\r",txt);
Dname(dt,txt);
break;
case 'R':
printf("\n\r");
Dsymbol(dt,ht,BLK_ROAD);
break;
case &#39':
printf("\n\r");
Dsymbol(dt,ht,BLK_PIVOT);
break;
default:
printf("\n\r");
}
}
}
}

void MWend(void)
{
fclose(fi);
}

void Dborder(float x,float y)
{
POINT p1,p2,p3,p4,p5,p6;

SetLayer(LAY_BORD);
SetPoint(mwbp,x,y);
SetWidth(0.25);
SetPoint(p1,x-lmargin,y-6*lnwid);
SetPoint(p2,x+al/sx,y);
SetPoint(p3,p2.x,y+bh/sy);
Dline(p2,p3);
p2.x += rmargin; p2.y=p1.y; p3.x=p2.x;
SetPoint(p4,p1.x,p3.y);
Dppts(-4,p1,p2,p3,p4);

SetTexts(1,0.8*lnwid);
SetPoint(p5,p4.x+lmargin/2-0.4*lnwid,p4.y-0.8*lnwid);
Dvtext(p5,proj);
SetPoint(p5,mwbp.x,p3.y);
Dline(p5,mwbp);
SetPoint(p5,mwbp.x-lspace,mwbp.y);
SetPoint(p6,p5.x,p2.y);
Dline(p5,p6);

SetTexts(1,lnwid/2);
p3.y=mwbp.y;
p4.y=mwbp.y;
p1.y=p3.y-0.75*lnwid; p1.x+=0.25*lnwid;
Dline(p3,p4);
Dtext(p1,0,"距 离(Km)");
p3.y -= lnwid;
p4.y -= lnwid;
p1.y -= lnwid;
Dline(p3,p4);
Dtext(p1,0,"海拔标高(m)");
p3.y -= lnwid;
p4.y -= lnwid;
p1.y -= lnwid;
Dline(p3,p4);
Dtext(p1,0,"地形特征");
p3.y -= lnwid;
p4.y -= lnwid;
p1.y -= 2*lnwid;
Dline(p3,p4);
Dtext(p1,0,"地 名");
SetWidth(0.0);
}

void Dglobe(void)
{
int i;
POINT pt;
DISTANCE rad;
ANGLE ang;

SetLayer(LAY_TOPO_0);
SetPoint(pt,mwbp.x+al/sx,mwbp.y);
Dline(mwbp,pt);
for (i=1;i<3;i++) {
SetLayer(LAY_TOPO_0+2*i);
rad=(sqr(al/sx/2)+sqr(ah/sy))/(2*ah/sy);
ang=atan2(al/sx/2,rad-ah/sy);
SetPoint(pt,mwbp.x+al/sx/2,ah/sy-rad);
Darc(pt,rad,M_PI/2-ang,M_PI/2+ang);
}
}

void Dtopvt(DISTANCE dt,DISTANCE ht)
{
static POINT p1,p2;
float a,b,c;
int i;
POINT pt;
if (dt==0.0) {
SetPoint(p1,mwbp.x,mwbp.y+ht/sy); }
else {
p2.x=p1.x+dt/sx;
p2.y=mwbp.y+ht/sy;
axbyc(&p1,&p2,&a,&b,&c);
SetLayer(LAY_TOPO_0);
Dppts(2,p1,p2);
for (i=1;i<3;i++) {
SetLayer(LAY_TOPO_0+2*i);
Dpbgn(FALSE);
for (pt.x=p1.x; pt.x

* +---------------------------------------------+
* | demo.dat |
* | 文峰山--锗墩煤矿微波通讯 地形剖面图 |
* | 1994.1.25 |
* +---------------------------------------------+
*
* Header 头信息
*
文峰山-锗墩矿地形断面图
34370 220 50000 1000 25 40 250
*
* Topograph 地形信息
*
0 203.4
1300 60
3000 60
350 117
300 60
3450 50
550 100
250 50
400 50
100 66
100 50
2800 50
800 110
450 70
650 70
250 90
250 50
5000 50
3100 45
500 80
500 50
4000 49
800 49
3600 55
570 50
*
* Building 建筑物信息
*
S 0 203 文峰山
V 3700 60 何庄 200 6
R 12500 50
R 15500 50
P 16000 50
R 17600 50
V 21000 50 三山后 200 6
V 24500 50 石杭 200 6
P 25100 50
P 28000 50
V 29000 49 王庄 200 6
P 31700 52
R 33300 55
V 33700 55 桥头 200 8
E 34370 50 锗墩煤矿
*
* END 结束.
*

10

主题

1835

铜板

4

好友

助理工程师

Rank: 5Rank: 5

积分
382
发表于 2017-6-16 14:04 | 显示全部楼层
ipnu ipnu
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

在线客服
快速回复 返回顶部 返回列表