關於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;
<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>

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM