用.NET從外部dwg文件導入塊


翻譯並引自Kean's blog的兩篇文章:

http://through-the-interface.typepad.com/through_the_interface/2006/08/import_blocks_f.html.

http://through-the-interface.typepad.com/through_the_interface/2006/08/breaking_it_dow.html

  我們將使用一個中介“side database”-先把dwg讀取在內存中,而不是直接導入Autocad編輯器,之后再將“塊”導入到我們的正在運行的編輯器(Autocad文件數據庫的結構,請參看相關資料)。

  下面使C#代碼,其中的注釋說明了每條的功用。之后,我們也將關鍵語句,進行分析。

 1 using System;
 2 using Autodesk.AutoCAD;
 3 using Autodesk.AutoCAD.Runtime;
 4 using Autodesk.AutoCAD.Geometry;
 5 using Autodesk.AutoCAD.ApplicationServices;
 6 using Autodesk.AutoCAD.DatabaseServices;
 7 using Autodesk.AutoCAD.EditorInput;
 8 using System.Collections.Generic;
 9 
10 namespace BlockImport
11 {
12 publicclassBlockImportClass
13   {
14     [CommandMethod("IB")]
15 publicvoid ImportBlocks()
16     {
17 DocumentCollection dm =
18 Application.DocumentManager;
19 Editor ed = dm.MdiActiveDocument.Editor;
20 Database destDb = dm.MdiActiveDocument.Database;
21 Database sourceDb = newDatabase(false, true);
22 PromptResult sourceFileName;
23 try
24       {
25 // Get name of DWG from which to copy blocks
26         sourceFileName =
27           ed.GetString("\nEnter the name of the source drawing: ");
28 // Read the DWG into a side database
29         sourceDb.ReadDwgFile(sourceFileName.StringResult,
30                             System.IO.FileShare.Read,
31 true,
32 "");
33 
34 // Create a variable to store the list of block identifiers
35 ObjectIdCollection blockIds = newObjectIdCollection();
36 
37         Autodesk.AutoCAD.DatabaseServices.TransactionManager tm =
38           sourceDb.TransactionManager;
39 
40 using (Transaction myT = tm.StartTransaction())
41         {
42 // Open the block table
43 BlockTable bt =
44               (BlockTable)tm.GetObject(sourceDb.BlockTableId,
45 OpenMode.ForRead,
46 false);
47 
48 // Check each block in the block table
49 foreach (ObjectId btrId in bt)
50           {
51 BlockTableRecord btr =
52               (BlockTableRecord)tm.GetObject(btrId,
53 OpenMode.ForRead,
54 false);
55 // Only add named & non-layout blocks to the copy list
56 if (!btr.IsAnonymous && !btr.IsLayout)
57               blockIds.Add(btrId);
58             btr.Dispose();
59           }
60         }
61 // Copy blocks from source to destination database
62 IdMapping mapping = newIdMapping();
63         sourceDb.WblockCloneObjects(blockIds,
64                                     destDb.BlockTableId,
65                                     mapping,
66 DuplicateRecordCloning.Replace,
67 false);
68         ed.WriteMessage("\nCopied "
69                         + blockIds.Count.ToString()
70                         + " block definitions from "
71                         + sourceFileName.StringResult
72                         + " to the current drawing.");
73       }
74 catch(Autodesk.AutoCAD.Runtime.Exception ex)
75       {
76           ed.WriteMessage("\nError during copy: " + ex.Message);
77       }
78       sourceDb.Dispose();
79     }
80   }
81 }

  首先,Line21,我們聲明並舉例了一個Database對象,這樣就會在內存中開辟一定空間(也就是我們說的side database),這個空間對於我們是可以進入的,對於Autocad編輯器是不可以的。其中,第1個參數(buildDefaultDrawing)要設置為false,如果設置為true,則函數不會反饋錯誤,因此你不知道程序發生了什么。在下面兩種情況中,你才會把buildDefaultDrawing設置為true:

    1.你自己創造一個dwg文件,而不是從其他地方讀取來;

    2.從其他地方讀取來,而且dwg文件的版本在R12及之前。

    (版本對照如下:

    AC1.50 = R2.05
    AC1002 = R2.6
    AC1004 = R9
    AC1006 = R10
    AC1009 = R11/R12
    AC1012 = R13
    AC1014 = R14
    AC1015 = 2000/2000i/2002
    AC1018 = 2004/2005/2006
    AC1021 = 2007

    )

  下一步,Line26,27,我們要求用戶輸入想要引入的文件地址。這里我們沒有檢查用戶是否輸入內容或者輸入的內容是否存在,是因為接下來的ReadDwgFile()方法在不能讀取文件時,會拋出一個error;

  接下來,Line29,30,將文件內容讀取到side database.並且我們在Line35,我們創造一個塊集合(同樣,請參考Autocad文件數據庫的結構);

  下面的最為關鍵,Line40 tm.StartTransaction() 才是與CAD編輯器交互的接口。其中,運用循環將有名字或者未布局的塊收集起來,看代碼就不過多解釋了。這個函數WblockCloneObjects()中最后一個參數,REPLACE則擦除之前的內容覆蓋寫,如果設為IGNORE,則續寫。

 


免責聲明!

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



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