2024年1月4日发(作者:)

1、Teigha中DWG数据库结构:

经常用到的有TextStyleTable、LayerTable、LinetypeTable、BlockTable及其对应的TextStyleTableRecord、LayerTableRecord、LinetypeTableRecord、BlockTableRecord及Entity。

2、具体使用

2.1添加引用

使用前应该添加TD_Mgd_3.03_或是其他版本类库,类库分为64位与32位,32位类库在64位系统上运行效果可能不太好。3.02版本及一下版本可能没有64位的类库。

命名空间有:

seServices;

ry;

;

_Import;

csInterface;

csSystem;

e;

3.02及以下版本命名空间应将Teigha换为DWGdirect。

2.2打开、新建、保存数据库

使用之前应加上这个:

using (Services ser = new Services())//一个应用程序加上一个就行了,否则出错

1、打开数据库(dwg文件)

using (Database pDb = new Database(false, false))//不加参数会出错

{

gFile(pPath

rReadAndWriteNoShare, false, "");

}

+ "",

2、新建数据库

using (Database pDb = new Database())//加参数出错

3、保存

(1)保存格式

SaveType pSavetype = 12; //Save12为.dwg Save13为dxf

默认保存为dwg,可以不用指定。

(2)保存版本类型

DwgVersion dwgver = 18; //ACAD2010为vAC24;ACAD2007为vAC21;ACAD2004为vAC18;

很重要,保存时要用,版本过高时低版本AutoCAD不能打开。

(3)保存

(filename, dwgver);

pDb为数据库(Database),filename为dwg文件名,dwgver为版本。

2.3写数据

2.3.1添加文本样式

ObjectId styleId = ;

using (TextStyleTable pStyles =

(TextStyleTable)ect(te))

{

//文本样式记录

using (TextStyleTableRecord pStyle = new TextStyleTableRecord())

{

// 表对象(记录)添加到表之前必须命名

// isShapeFile flag must also be set (if true) before adding the object

// to the database.

= styleName;//必须设定

eFile = isShapeFile;//添加(记录)到数据库之前必须设定(如果为true)

// Add the object to the table.添加对象(记录)到表

styleId = (pStyle);

// 设置剩下的属性。(可以添加后设置也可以添加之前设置)

ze = textSize;

= xScale;

ize = priorSize;

ingAngle = obliquing;

me = fileName;

if (isShapeFile)

ize = 22.45;

if (!OrEmpty(ttFaceName))

= new FontDescriptor(ttFaceName, bold, italic, charset,

pitchAndFamily);

return styleId;

}

}

注:pDb为Database

2.3.2添加线型

using (LinetypeTable pLinetypes =

(LinetypeTable)ect(te))

{

//线表记录

using (LinetypeTableRecord pLinetype = new LinetypeTableRecord())

{

= name;//必须命名

ObjectId linetypeId = (pLinetype);//添加记录

return linetypeId;

}

}

注:线型要有相应的线型文件,且不一定能够加成功,线型可以在使用之前手动加在dwg模板中,从其他文件向dwg文件复制线型,可能不成功。

2.3.3添加块

例:

using (BlockTable blockTable = (BlockTable)ect(te))

{

ObjectId annoBlockId;

using (BlockTableRecord btr = new BlockTableRecord())

{

= "AnnoBlock";

annoBlockId = (btr);

using (Circle pCircle = new Circle())

{

abaseDefaults(pDb);

Entity(pCircle);

Point3d center = new Point3d(0, 0, 0);

= center;

= 0.5;

}

}

}

向块表中加入块之前,块一定要有名字。同时可以从其他文件中提取块,加入到目标数据库中

例:

using (Database db = new Database(false, false))

{

if (!(pPath + "BLOCKS" + blockname +

".dwg"))

{

("没找到CASS块文件");

return ;

}

gFile(pPath + "BLOCKS" + blockname + ".dwg",

rReadAndAllShare, false, "");

using (BlockTable pTable =

(BlockTable)(d))

{

using (BlockTable bt =

(BlockTable)(te))

{

using (BlockTableRecord btr = new BlockTableRecord())

{

foreach (ObjectId id in pTable)

{

using (BlockTableRecord pBlock =

(BlockTableRecord)(d))

{

foreach (ObjectId entid in pBlock)

{

using (Entity pEnt =

(Entity)(d, false, true))

{

Entity((Entity)());

}

}

}

}

= blockname;

ObjectId blockid = (btr);

return blockid;

}

}

}

}

2.3.4向模型(model)空间画实体(线、面等)

加入线,例:

using (BlockTable blockTable = (BlockTable)ect(te))

{

ObjectId modelSpaceID = blockTable[pace];

using (BlockTableRecord btr =

(BlockTableRecord)ect(te))

{

using (Polyline2d pline = new Polyline2d())

{

Entity(pline);//向块记录中添加线

Vertex2d pVertex = new Vertex2d();//顶点

Point3d pos = start;//起点

pVertex = new Vertex2d();

Vertex(pVertex);

pos = start;//起点

on = pos;

e();

pVertex = new Vertex2d();

Vertex(pVertex);

pos = end;//顶点,终点

on = pos;

if (linewidth >= 0.0)

{

idth = linewidth;//线宽

th = linewidth;

}

e();

// = false;//此属性在画线时不加,但在成面时将属性变为true

if (linestyle != null)

{

pe = linestyle;//线型

}

= LayerName;//图层名

}

}

}

}

加入面的操作与上面加入线类似,但最后线的Closed属性应设置成true。

插入文字:

using (BlockTableRecord bBTR =

(BlockTableRecord)ect(te))

{

using (DBText pText = new DBText())

{

//开始时插入文字以左上点为准插入

using (Database pDb = se)

abaseDefaults(pDb);

ObjectId textId = Entity(pText);

// 注释

tive = ;

//加入到特殊群

if (pGroup != null)

(textId);

on = position;//位置(应该是左上方)

entPoint = alignmentPoint;//校准点什么东西

= height;//高度

actor = 1.0;//什么东西

ring = text;

ntalMode = hMode;//文字模式

alMode = vMode;//垂直模式

e = OdaToRadian(oblique);//倾斜

on = OdaToRadian(rotation);//旋转

//文字样式

if (!)

yleId = textstyleID;

//层名

if (!)

erId(layerId, false);

if (widthfactor != 0.0)

{

actor = widthfactor;//宽度因子

}

}

}

插入块:

using (BlockTableRecord btr =

(BlockTableRecord)ect(te))

{

BlockReference pBlkRef = new BlockReference(point, Id);//point为插入的位置

ableRecord = BlockRefID;//块在数据库中的id

actors = new Scale3d(scale, scale, scale);//比例

d = layerID;//图层id

Entity(pBlkRef);//插入块

}

2.3.5图层

加入图层:

using (LayerTable pLayers = (LayerTable)ect(te))

{

//图层

using (LayerTableRecord pLayer = new LayerTableRecord())

{

= name;//图层名必须有

= lorIndex(, color);//颜色

//图层线型

using (LinetypeTable pLinetypes =

(LinetypeTable)ect(te))

{

ObjectId linetypeId = pLinetypes[linetype];

peObjectId = linetypeId;

}

return (pLayer);//ObjectID

}

}

2.4关于视图

在写完dwg文件之后,打开dwg有时会找不到所画的实体,因此在保存前应先定义好视图:

方法一:

using (ViewportTable vt =

(ViewportTable)ect(te))

{

foreach (ObjectId item in vt)

{

using (ViewportTableRecord vtr =

(ViewportTableRecord)ect(te))

{

Point2d center = new Point2d((X ,Y);//定义中心点

Point = center;

= 105;//高度

= 130;宽度

}

}

}

方法二:

LayoutManager lm = t;

ObjectId layoutid = outId(tLayout);//tLayout新建时默认为”Model”,当其改变时应换为”Model”的名字

using (Layout pLayout=(Layout)ect(te))

{

ObjectId viewportid = tViewportId;

using (ViewportTableRecord

vtr=(ViewportTableRecord)ect(te))

{

Point2d center = new Point2d((X ,Y);//定义中心点

Point = center;

= 105;//高度

= 130;//宽度

}

}