标签:动态 未来 部分 主程序 action 修改 size tab write
因工作需要,实现脱离CAD环境下对DWG图纸的操作,研究Teigha的使用。本文是对研究内容做的记录。目前Teigha网上资料不是很多,还在学习中。
我使用的是Teigha 4.0 .net 版本,VS2018环境,.NET Framework 4框架。
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小节介绍。
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\\text01.dwg", DwgVersion.AC1800); db.Dispose(); }
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(); } }
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 圆的半径属性
其它操作同第一节
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(); }
以上三小节是对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;
<span class="token keyword">double</span><span class="token punctuation">[</span><span class="token punctuation">,</span><span class="token punctuation">]</span> line4 <span class="token operator">=</span> textpoint<span class="token punctuation">.</span>line4<span class="token punctuation">;</span> <span class="token keyword">double</span><span class="token punctuation">[</span><span class="token punctuation">,</span><span class="token punctuation">]</span> N_pline3 <span class="token operator">=</span> textpoint<span class="token punctuation">.</span>N_pline3<span class="token punctuation">;</span> <span class="token keyword">double</span><span class="token punctuation">[</span><span class="token punctuation">,</span><span class="token punctuation">]</span> line5 <span class="token operator">=</span> textpoint<span class="token punctuation">.</span>line5<span class="token punctuation">;</span>
标签:动态 未来 部分 主程序 action 修改 size tab write
原文地址:https://www.cnblogs.com/wwssgg/p/14718598.html