程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c# cad中插入另一個dwg的圖塊

c# cad中插入另一個dwg的圖塊

編輯:C#入門知識

1.在cad2012環境下:

須引用objectarx開發包

PromptPointResult ppr = ed.GetPoint("請選擇插入點:");
 
Point3d pt = ppr.Value; //這裡獲得插入點
 
utility.WriteToEditor(pt.ToString());
 
blockPath = "b_sample.dwg";
using (Database blkDb = new Database(false, true))
{
 
    //read drawing
 
    blkDb.ReadDwgFile(blockPath , System.IO.FileShare.Read, true, null);
 
    blkDb.CloseInput(true);
 
    using (DocumentLock docLock = doc.LockDocument())//多文檔要先這樣,否則報至命錯誤
 
    {
 
        using (Transaction t = doc.TransactionManager.StartTransaction())
 
        {
     string name=“aa”;//aa是不與blockPath文件中的任何塊重名的字符串
            //insert it as a new block
 
            ObjectId idBTR = doc.Database.Insert(aa, blkDb, false);
 
            //create a ref to the block
 
            BlockTable bt = (BlockTable)t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
 
            BlockTableRecord btr = (BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
 
            using (BlockReference bref = new BlockReference(pt, idBTR)) //pt是一個Point3D坐標,這裡是插入進當前dwg文件中
 
            {
 
                btr.AppendEntity(bref);
 
                t.AddNewlyCreatedDBObject(bref, true);
 
            }
 
            t.Commit();   就是這樣  謝謝
       }
   }
}

 

 

2.脫離CAD環境下

需引用TDWGNET開發包

 using (new Services())
             {
                 using (Database db = new Database(true, true))
                 {
                     using (Transaction ts = db.TransactionManager.StartTransaction())
                     {
                         using (BlockTable bt = ts.GetObject(db.BlockTableId,OpenMode.ForWrite) as BlockTable)
                         {
                             BlockTableRecord btr1 = new BlockTableRecord();
                             Database odb = new Database(false, false);
                             odb.ReadDwgFile("aaa.dwg", FileOpenMode.OpenForReadAndAllShare, true, null);
                             odb.CloseInput(true);
                             ObjectId objid = db.Insert("aa", odb, false);//這裡插入進當前的dwg文件了
                             BlockTableRecord btr = new BlockTableRecord();//這裡是插入另一個塊
                             btr.Name = "000";
                             Circle c = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 100);
                             btr.AppendEntity(c);
                             ts.AddNewlyCreatedDBObject(c, true);
                             bt.Add(btr);
                             ts.AddNewlyCreatedDBObject(btr, true);
                         }
                         ts.Commit();
                     }


                     //using (Transaction ts = db.TransactionManager.StartTransaction())
                     //{
                     //    using (BlockTable bt = (BlockTable)ts.GetObject(db.BlockTableId, OpenMode.ForRead))
                     //    {
                     //        BlockTableRecord btr=ts.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                     //        BlockReference br = new BlockReference(new Point3d(0, 0, 0), bt["aa"]);//這裡是插入塊於當前的dwg中
                     //        btr.AppendEntity(br);
                     //        ts.AddNewlyCreatedDBObject(br, true);
                     //    }
                     //    ts.Commit();
                     //}
                     db.SaveAs(path + "\\test.dwg", DwgVersion.Current);//保存文件
                 }
             }
        }


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved