【转载】关于Teigha的使用记录

目录

关于Teigha的使用记录

Teigha的加载引用

主要程序

1、新建DWG并写入文字

2、读取DWG文件并画圆

3、文字替换

4、DWG图纸绘制

关于Teigha的使用记录

因工作需要,实现脱离CAD环境下对DWG图纸的操作,研究Teigha的使用。本文是对研究内容做的记录。目前Teigha网上资料不是很多,还在学习中。

我使用的是Teigha 4.0 .net 版本,VS2018环境,.NET Framework 4框架。

Teigha的加载引用

1、Teigha下载之后是一堆动态链接库,解压放到项目文件bin/Debug文件夹下即可。

2、”添加引用“通过浏览,将Debug文件下的"TD_Mgd_4.00_10.dll"和“TD_MgdBrep_4.00_10.dll"两个文件加入到引用中。

3、在主程序中添加using。 会用到的会有如下几个。

using Teigha.DatabaseServices;

using Teigha.Runtime;

using Teigha.Geometry;

using Teigha.GraphicsInterface;

using Teigha.GraphicsSystem;

1

2

3

4

5

主要程序

作为练习和研究,先介绍一下简单的操作。对于Teigha的应用,目的主要是为了对一份cad文件进行重新绘制,主要在第4小节介绍。

1、新建DWG并写入文字

Teigha的使用习惯是必须用using(Services ser =new Services()){ }。代码必须写在花括弧之内。

通过 Database db=new Database(),新建一个数据库文件,用来打开DWG文件。

Database db = new Database();

1

添加文字。必须通过using( var trans =db.TransactionManager.StartTransaction()){ },在这个里面添加代码。

using (var tr = db.TransactionManager.StartTransaction())

{

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

string str = "AAA";

DBText txt = new DBText();

txt.Position = pt1;

txt.TextString= str;

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

btr.AppendEntity(txt);

tr.AddNewlyCreatedDBObject(txt, true);

tr.Commit();

db.SaveAs("E:\\vswork\\CADfile\\text01.dwg", DwgVersion.AC1800);

db.Dispose();

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

注意Point3d pt1=new Point3d(0,0,0)是建立一个三维坐标位置,用来后面确定添加文字时,给文字一个放置的坐标。默认设为(0,0,0)点。

DBtext txt =new DBText() 新建一个DBtext对象,用来给DWG文件增加text文字。

将位置坐标和文字内容赋给DBtext对象。 txt.Position = pt1; txt.TextString= str;

通过建立一个块的表记录对象BlockTableRecord btr,将文字txt 添加到该表记录对象中:btr.AppendEntity(txt);

将txt文字添加到传输对象中tr。tr负责往数据库中写入该文字。tr.AddNewlyCreatedDBObject(txt, true);

tr.Commit(); 传输对象tr的确认操作

db.SaveAs(“E:\vswork\CADfile\text01.dwg”, DwgVersion.AC1800);是保存文件。AC1800是保存格式(CAD2010版本)。尽量保存低版本格式,方便其他人使用。

db.Dispose()是数据库的处置操作。

using (var tr = db.TransactionManager.StartTransaction())也是必须写在 using (Services svc = new Services())之内。完整的代码如下:

using (Services svc = new Services())

{

Database db = new Database();

using (var tr = db.TransactionManager.StartTransaction())

{

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

string str = "AAA";

DBText txt = new DBText();

txt.Position = pt1;

txt.TextString= str;

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

btr.AppendEntity(txt);

tr.AddNewlyCreatedDBObject(txt, true);

tr.Commit();

db.SaveAs("E:\\vswork\\CADfile\\test01.dwg", DwgVersion.AC1800);

db.Dispose();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

2、读取DWG文件并画圆

通过 Database db=new Database(false,false),新建一个数据库文件,用来打开DWG文件。因为DWG格式本身就是一个数据库。第一个false是指建筑默认图纸,第二个false指”noDucoment" ,暂时不知道什么意思,不用管。

通过db.ReadDwgFile方法读取dwg文件。完整代码如下:

using (Services svc = new Services())

{

Database db = new Database(false,false);

db.ReadDwgFile(fname, System.IO.FileShare.Read,false , null);

using (var tr = db.TransactionManager.StartTransaction())

{

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

double rad = 1000;

Circle cir = new Circle();

cir.Center = pt1;

cir.Radius = rad;

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

btr.AppendEntity(cir);

tr.AddNewlyCreatedDBObject(cir, true);

tr.Commit();

db.SaveAs("E:\\vswork\\CADfile\\text01.dwg", DwgVersion.AC1800);

db.Dispose();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Circle cir=new Circle() 创建一个圆对象

cir.Center 圆的圆心坐标属性

cir.Radius 圆的半径属性

其它操作同第一节

3、文字替换

using (Services ser = new Services())

{

string fname = "E:\\vswork\\CADfile\\text01.dwg";

Database db = new Database(false,false);

db.ReadDwgFile(fname,System.IO.FileShare.Read,false,null);

using (var trans = db.TransactionManager.StartTransaction())

{

BlockTableRecord btrec = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

foreach (ObjectId objid in btrec)

{

Entity ent = trans.GetObject(objid,OpenMode.ForWrite) as Entity;

if (ent.GetType().Name == "DBText")

{

DBText txt = (DBText)ent;

if (txt.TextString == "aaa")

{

txt.TextString = "bbb";

}

}

}

trans.Commit();

}

db.Save();

db.Dispose();

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

通过 BlockTableRecord btrec = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);从数据库中得到表记录对象。

通过Entity ent = trans.GetObject(objid,OpenMode.ForWrite) as Entity;从表记录对象中取出实体对象。

if (ent.GetType().Name == “DBText”)判断实体对象是否文本格式

DBText txt = (DBText)ent;新建一个文本格式对象用来保存和操作

if (txt.TextString == “aaa”)判断文本对象内容

4、DWG图纸绘制

以上三小节是对Teigha的基本操作的熟悉。接下来我需要实际写我的CAD文件。目的是在不变图元的基础上,将可变图元根据参数重新绘制在CAD文件中,保存为DWG格式。

4.1首先我会建立一个类库文件,保存需要变动的图元的坐标参数。

//钢筋预制伸出部分

public static double[,] xlsm = new double[9, 5]

{

{0,0,23.53738644,0,0.75 },

{25.53738644,0,49.07477288,0,0.75 },

{0,-70,23.53738644,-70 ,0.75},

{25.53738644,-70,49.07477288,-70 ,0.75},

{0,-140,23.53738644,-140,0.75},

{25.53738644,-140,49.07477288,-140,0.75 },

{1,2,48.07477288,2 ,0.75},

{1,-68,48.07477288,-68 ,0.75},

{1,-138,48.07477288,-138,0.75},

};

//湿接缝斜长标注

public static double[,] DIMLINEAR1 = new double[9, 5]

{

{0,0,49.07477,0,30},

{23.53738644,0,25.53739,0,10},

{1,2,48.07477,2,-20},

{0,-70,49.07477288,-70 ,30},

{23.53738644,-70,25.53738644,-70,10},

{1,-68,48.07477,-68,-20 },

{0,-140,49.07477,-140,30 },

{23.53738644,-140,25.53739,-140,10},

{1,-138,48.07477288,-138,-20},

};

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

以上是部分代码节选,我将需要绘制的多段线和标注的坐标数据保存在单独的cs文件中。未来有变化,直接修改这里的参数就可以。

4.2 在主程序中加入上面的参数表

double[,] xl_pline = textpoint.xlsm;

double[,] line2 = textpoint.line2;

double[,] N_pline2 = textpoint.N_pline2;

//double[,] DIMLINEAR = textpoint.DIMLINEAR1;

//double[,] DIMLINEAR2 = textpoint.DIMLINEAR2;

double[,] line3 = textpoint.line3;

double[,] DIMLINEAR4 = textpoint.DIMLINEAR4;

double[,] line4 = textpoint.line4;

double[,] N_pline3 = textpoint.N_pline3;

double[,] line5 = textpoint.line5;

1

2

3

4

5

6

7

8

9

10

11

4.3考虑到绘制多次绘制多段线,需要重复对数据进行操作,最好的方式是进行代码封装。我建立了“画多段线”、“画直线”、“画标注”等不同的方法以供调用。

//画多段线

private void Print_Pline(double[,] in_pline, ref Database db)

{

//Database db = new Database(false, false);

using (var tr = db.TransactionManager.StartTransaction())

{

for (int i = 0; i < in_pline.GetLength(0); i++)

{

Point2d point2D1 = new Point2d(in_pline[i, 0], in_pline[i, 1]);

Point2d point2D2 = new Point2d(in_pline[i, 2], in_pline[i, 3]);

Teigha.DatabaseServices.Polyline pl = new Teigha.DatabaseServices.Polyline();

pl.AddVertexAt(0, point2D1, 0, in_pline[i, 4], in_pline[i, 4]);

pl.AddVertexAt(1, point2D2, 0, in_pline[i, 4], in_pline[i, 4]);

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

btr.AppendEntity(pl);

tr.AddNewlyCreatedDBObject(pl, true);

}

tr.Commit();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

//画直线

private void Print_line(double[,] in_line, ref Database db)

{

//Database db = new Database(false, false);

using (var tr = db.TransactionManager.StartTransaction())

{

for (int i = 0; i < in_line.GetLength(0); i++)

{

Point3d pt1 = new Point3d(in_line[i, 0], in_line[i, 1], 0);

Point3d pt2 = new Point3d(in_line[i, 2], in_line[i, 3], 0);

Line pl = new Line(new Point3d(in_line[i, 0], in_line[i, 1], 0), new Point3d(in_line[i, 2], in_line[i, 3], 0));

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

btr.AppendEntity(pl);

tr.AddNewlyCreatedDBObject(pl, true);

}

tr.Commit();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

//画标注

private void Print_Dimlinear(double[,] DIMLINEAR, ref Database db)

{

//Database db = new Database(false, false);

using (var tr = db.TransactionManager.StartTransaction())

{

for (int i = 0; i < DIMLINEAR.GetLength(0); i++)

{

Point3d pt1 = new Point3d(DIMLINEAR[i, 0], DIMLINEAR[i, 1], 0);

Point3d pt2 = new Point3d(DIMLINEAR[i, 2], DIMLINEAR[i, 3], 0);

Point3d ptline = Dimlintpt( pt1, pt2, DIMLINEAR[i, 4]);

string dimtext = Convert.ToString(Math.Round(Math.Abs(pt2.X-pt1.X),2));

AlignedDimension dim = new AlignedDimension(pt1, pt2, ptline, dimtext, ObjectId.Null);

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

btr.AppendEntity(dim);

tr.AddNewlyCreatedDBObject(dim, true);

}

tr.Commit();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

//画标注 重载 参数有文字内容 dimtext

private void Print_Dimlinear(double[,] DIMLINEAR,string dimtext, ref Database db)

{

//Database db = new Database(false, false);

using (var tr = db.TransactionManager.StartTransaction())

{

for (int i = 0; i < DIMLINEAR.GetLength(0); i++)

{

Point3d pt1 = new Point3d(DIMLINEAR[i, 0], DIMLINEAR[i, 1], 0);

Point3d pt2 = new Point3d(DIMLINEAR[i, 2], DIMLINEAR[i, 3], 0);

Point3d ptline = Dimlintpt(pt1, pt2, DIMLINEAR[i, 4]);

AlignedDimension dim = new AlignedDimension(pt1, pt2, ptline, dimtext, ObjectId.Null);

dim.TextPosition=(new Point3d(14.76869322, -123,0));

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

btr.AppendEntity(dim);

tr.AddNewlyCreatedDBObject(dim, true);

}

tr.Commit();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

4.4 接下来,只需要调用这些方法,将double[,] 参数表对应传入即可。

using (Services svc = new Services())

{

string fname = "E:\\vswork\\RepText\\CADfile\\test.dwg";

Database db = new Database(false, false);

db.ReadDwgFile(fname, System.IO.FileShare.Read, false, null);

Print_Pline(N_pline2, ref db);

Print_line(line2,ref db);

//Print_Dimlinear(DIMLINEAR,ref db);

//Print_Dimlinear(DIMLINEAR2, ref db);

Print_line(line3, ref db);

//Print_Dimlinear(DIMLINEAR3, ref db);

//circle text ROMANS 6

Point3d pt_cir1 = new Point3d(17, 15, 0);

Print_Circle(pt_cir1,5,"6",ref db); //circle text ROMANS 6

//标注

Point3d pt1 = new Point3d(DIMLINEAR4[0, 0], DIMLINEAR4[0, 1], 0);

Point3d pt2 = new Point3d(DIMLINEAR4[0, 2], DIMLINEAR4[0, 3], 0);

Print_Dimlinear(pt1, pt2, DIMLINEAR4[0, 4], "1x100+191/2", ref db);

pt1 = new Point3d(DIMLINEAR4[1, 0], DIMLINEAR4[1, 1], 0);

pt2 = new Point3d(DIMLINEAR4[1, 2], DIMLINEAR4[1, 3], 0);

Print_Dimlinear(pt1, pt2, DIMLINEAR4[1, 4], "491/2", ref db);

Print_line(line4, ref db);

Print_Pline(N_pline3, ref db);

Print_line(line5, ref db);

//circle text ROMANS 6

Point3d pt_cir2 = new Point3d(372.47, 15, 0);

Print_Circle(pt_cir2, 5, "6", ref db); //circle text ROMANS 6

db.SaveAs("E:\\vswork\\RepText\\CADfile\\test-副本1.dwg", DwgVersion.AC1800);

db.Dispose();

}

}

————————————————

版权声明:本文为CSDN博主「xianghero2020」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/m0_38115909/article/details/107875103

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Teigha是一套基于DWG格式的开发工具包(SDK),用于开发支持CAD文件格式的应用程序。Teigha提供了广泛的API,允许开发者在自己的应用程序中读写、编辑和呈现DWG文件。 使用Teigha进行开发,首先需要从Open Design Alliance(ODA)官方网站下载Teigha SDK并按照安装说明进行安装。安装完成后,开发者可以将Teigha链接到他们的应用程序中,并使用Teigha提供的API进行开发。Teigha支持多种编程语言,如C++、C#、VB.NET等。 Teigha的主要功能包括DWG文件的读写、编辑和转换。开发者可以使用Teigha读取DWG文件中的实体、块、图层等信息,并对其进行修改、删除或添加新的实体。此外,Teigha还可以实现DWG文件的格式转换,例如将DWG文件转换为DXF、PDF或其他格式。 在进行Teigha开发时,开发者需要熟悉Teigha的API文档和示例代码。API文档详细介绍了Teigha各个功能模块的使用方法和参数说明,示例代码则提供了一些典型应用场景的代码范例,可以帮助开发者更好地理解和使用Teigha。 此外,Teigha还提供了一些辅助工具和插件,例如用于快速浏览和查看DWG文件的Teigha Viewer,以及用于将Teigha与其他软件集成的插件。这些工具和插件可以帮助开发者更便捷地进行Teigha开发。 总之,Teigha是一套强大的DWG文件处理工具包,通过使用Teigha,开发者可以轻松地读写、编辑和转换DWG文件,为用户提供更强大、更丰富的CAD应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值