用.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,则续写。

 

posted on 2016-10-11 21:34  Big_Z  阅读(2268)  评论(1编辑  收藏  举报

导航