用.NET从外部dwg文件导入块

翻译并引自Kean's blog的两篇文章:html

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.htmlapp

  咱们将使用一个中介“side database”-先把dwg读取在内存中,而不是直接导入Autocad编辑器,以后再将“块”导入到咱们的正在运行的编辑器(Autocad文件数据库的结构,请参看相关资料)。编辑器

  下面使C#代码,其中的注释说明了每条的功用。以后,咱们也将关键语句,进行分析。ide

 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及以前。ui

    (版本对照以下:spa

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

相关文章
相关标签/搜索