关于Teigha的使用记录
关于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;
主要程序
作为练习和研究,先介绍一下简单的操作。对于Teigha的应用,目的主要是为了对一份cad文件进行重新绘制,主要在第4小节介绍。
1、新建DWG并写入文字
- Teigha的使用习惯是必须用using(Services ser =new Services()){ }。代码必须写在花括弧之内。
- 通过 Database db=new Database(),新建一个数据库文件,用来打开DWG文件。
Database db = new Database();
- 添加文字。必须通过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();
}
- 注意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();
}
}
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();
}
}
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();
}
- 通过 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},
};
以上是部分代码节选,我将需要绘制的多段线和标注的坐标数据保存在单独的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;
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();
}
}
//画直线
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();
}
}
//画标注
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();
}
}
//画标注 重载 参数有文字内容 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();
}
}
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();
}
}
本文地址:https://blog.csdn.net/m0_38115909/article/details/107875103
上一篇: 小姐姐体验OPPO Reno5 Pro焕采人像视频:真香
下一篇: AI怎么设计立体旋转五角星球体?
推荐阅读