源文档链接
    以下均为console application,在vs2005中编译通过!需要引用Autodesk的Object DBX 或最新的Real DWG 的SDK。
    如有其他的问题,请留言!

    列举dwg文件中的所有的blcok的name:
    C#实现:

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Text;
    5. using System.Diagnostics;
    6. using System.Reflection;
    7. using System.Runtime;
    8. using System.Windows.Forms;
    9. using System.IO;
    10. using System.ComponentModel;
    11. using System.Data;
    12. using System.Drawing;
    13. using Autodesk.AutoCAD.DatabaseServices;
    14. using System.Runtime.InteropServices;
    15. using Autodesk.AutoCAD.Runtime;
    16. using Autodesk.AutoCAD.Geometry;
    17. [assembly: SecuredApplication(@"license")]
    18. namespace GetFrameOfRealDwg
    19. {
    20. class MyHostApplicationServices : Autodesk.AutoCAD.DatabaseServices.HostApplicationServices
    21. {
    22. public override System.String FindFile(System.String fileName,
    23. Autodesk.AutoCAD.DatabaseServices.Database database,
    24. Autodesk.AutoCAD.DatabaseServices.FindFileHint hint
    25. )
    26. {
    27. return string.Empty;
    28. }
    29. static public ArrayList GetBlockNames(Database db)
    30. {
    31. ArrayList array = new ArrayList();
    32. Transaction tran = db.TransactionManager.StartTransaction();
    33. try
    34. {
    35. BlockTable bt = (BlockTable)tran.GetObject(db.BlockTableId, OpenMode.ForWrite);
    36. foreach (ObjectId recordid in bt)
    37. {
    38. BlockTableRecord record = (BlockTableRecord)tran.GetObject(recordid, OpenMode.ForRead);
    39. array.Add(record.Name);
    40. }
    41. }
    42. catch
    43. {
    44. }
    45. finally
    46. {
    47. tran.Dispose();
    48. }
    49. return array;
    50. }
    51. static void Main(string[] args)
    52. {
    53. MyHostApplicationServices myserver = new MyHostApplicationServices();
    54. int lcid = 0x00001033; // English
    55. RuntimeSystem.Initialize(myserver, lcid);
    56. Database Db = new Database(false, true);
    57. Db.ReadDwgFile(@"filepath", FileShare.Read, false, "");
    58. ArrayList ar = GetBlockNames(Db);
    59. foreach (string str in ar)
    60. {
    61. System.Console.WriteLine(str);
    62. }
    63. RuntimeSystem.Terminate();
    64. System.Console.WriteLine();
    65. }
    66. }
    67. }

    注意: 只需ref :acdbmgd.dll
    必须实现HostApplicationServices及她的findfile().
    生成的dll必须使用绑定工具绑定.
    (license是很贵的啊,可以到autodesk公司主页)

    c++实现:

    1. 以下均为console application,在vs2005中编译通过!需要引用AutodeskObject DBX 或最新的Real DWG SDK
    2. 如有其他的问题,请留言!
    3. 列举dwg文件中的所有的blcokname:
    4. C#实现:
    5. using System;
    6. using System.Collections;
    7. using System.Collections.Generic;
    8. using System.Text;
    9. using System.Diagnostics;
    10. using System.Reflection;
    11. using System.Runtime;
    12. using System.Windows.Forms;
    13. using System.IO;
    14. using System.ComponentModel;
    15. using System.Data;
    16. using System.Drawing;
    17. using Autodesk.AutoCAD.DatabaseServices;
    18. using System.Runtime.InteropServices;
    19. using Autodesk.AutoCAD.Runtime;
    20. using Autodesk.AutoCAD.Geometry;
    21. [assembly: SecuredApplication(
    22. @"license")]
    23. namespace GetFrameOfRealDwg
    24. {
    25. class MyHostApplicationServices : Autodesk.AutoCAD.DatabaseServices.HostApplicationServices
    26. {
    27. public override System.String FindFile(System.String fileName,
    28. Autodesk.AutoCAD.DatabaseServices.Database database,
    29. Autodesk.AutoCAD.DatabaseServices.FindFileHint hint
    30. )
    31. {
    32. return string.Empty;
    33. }
    34. static public ArrayList GetBlockNames(Database db)
    35. {
    36. ArrayList array = new ArrayList();
    37. Transaction tran = db.TransactionManager.StartTransaction();
    38. try
    39. {
    40. BlockTable bt = (BlockTable)tran.GetObject(db.BlockTableId, OpenMode.ForWrite);
    41. foreach (ObjectId recordid in bt)
    42. {
    43. BlockTableRecord record = (BlockTableRecord)tran.GetObject(recordid, OpenMode.ForRead);
    44. array.Add(record.Name);
    45. }
    46. }
    47. catch
    48. {
    49. }
    50. finally
    51. {
    52. tran.Dispose();
    53. }
    54. return array;
    55. }
    56. static void Main(string[] args)
    57. {
    58. MyHostApplicationServices myserver = new MyHostApplicationServices();
    59. int lcid = 0x00001033; // English
    60. RuntimeSystem.Initialize(myserver, lcid);
    61. Database Db = new Database(false, true);
    62. Db.ReadDwgFile(@"filepath", FileShare.Read, false, "");
    63. ArrayList ar = GetBlockNames(Db);
    64. foreach (string str in ar)
    65. {
    66. System.Console.WriteLine(str);
    67. }
    68. RuntimeSystem.Terminate();
    69. System.Console.WriteLine();
    70. }
    71. }
    72. }
    73. 注意: 只需ref :acdbmgd.dll
    74. 必须实现HostApplicationServices及她的findfile().
    75. 生成的dll必须使用绑定工具绑定.
    76. (license是很贵的啊,可以到autodesk公司主页)
    77. c++实现:
    78. #pragma once
    79. #define _CRT_SECURE_NO_DEPRECATE
    80. #include "windows.h"
    81. #include "dbsymtb.h"
    82. #include "dbents.h"
    83. #include <stdio.h>
    84. #include <string.h>
    85. #include "tchar.h"
    86. #include <string>
    87. #include <atlconv.h>
    88. #include <iostream>
    89. #include "dbapserv.h"
    90. using namespace std;
    91. class CreatentHostApp : public AcDbHostApplicationServices
    92. {
    93. Acad::ErrorStatus findFile(TCHAR* pcFullPathOut, int nBufferLength,
    94. const TCHAR* pcFilename, AcDbDatabase* pDb = NULL,
    95. AcDbHostApplicationServices::FindFileHint = kDefault);
    96. // These two functions return the full path to the root folder where roamable/local
    97. // customizable files were installed. Note that the user may have reconfigured
    98. // the location of some the customizable files using the Options Dialog
    99. // therefore these functions should not be used to locate customizable files.
    100. // To locate customizable files either use the findFile function or the
    101. // appropriate system variable for the given file type.
    102. //
    103. Acad::ErrorStatus getRoamableRootFolder(const TCHAR*& folder);
    104. Acad::ErrorStatus getLocalRootFolder(const TCHAR*& folder);
    105. // make sure you implement getAlternateFontName. In case your findFile implementation
    106. // fails to find a font you should return a font name here that is guaranteed to exist.
    107. virtual TCHAR * getAlternateFontName() const
    108. {
    109. return _T("txt.shx"); //findFile will be called again with this name
    110. }
    111. };
    112. // Return the Install directory for customizable files
    113. Acad::ErrorStatus
    114. CreatentHostApp::getRoamableRootFolder(const TCHAR*& folder)
    115. {
    116. Acad::ErrorStatus ret = Acad::eOk;
    117. static TCHAR buf[MAX_PATH] = _T("\0"); //MDI SAFE
    118. if (buf[0]==0)
    119. if (GetModuleFileName(NULL, buf, MAX_PATH) != 0)
    120. ret = Acad::eRegistryAccessError;
    121. folder = buf;
    122. return ret;
    123. }
    124. // Return the Install directory for customizable files
    125. Acad::ErrorStatus
    126. CreatentHostApp::getLocalRootFolder(const TCHAR*& folder)
    127. {
    128. Acad::ErrorStatus ret = Acad::eOk;
    129. static TCHAR buf[MAX_PATH] = _T("\0"); //MDI SAFE
    130. if (buf[0]==0)
    131. if (GetModuleFileName(NULL, buf, MAX_PATH) != 0)
    132. ret = Acad::eRegistryAccessError;
    133. folder = buf;
    134. return ret;
    135. }
    136. Acad::ErrorStatus
    137. CreatentHostApp::findFile(TCHAR* pcFullPathOut, int nBufferLength,
    138. const TCHAR* pcFilename, AcDbDatabase* pDb,
    139. AcDbHostApplicationServices::FindFileHint hint)
    140. {
    141. TCHAR pExtension[5];
    142. switch (hint)
    143. {
    144. case kCompiledShapeFile:
    145. _tcscpy(pExtension, _T(".shx"));
    146. break;
    147. case kTrueTypeFontFile:
    148. _tcscpy(pExtension, _T(".ttf"));
    149. break;
    150. case kPatternFile:
    151. _tcscpy(pExtension, _T(".pat"));
    152. break;
    153. case kARXApplication:
    154. _tcscpy(pExtension, _T(".dbx"));
    155. break;
    156. case kFontMapFile:
    157. _tcscpy(pExtension, _T(".fmp"));
    158. break;
    159. case kXRefDrawing:
    160. _tcscpy(pExtension, _T(".dwg"));
    161. break;
    162. case kFontFile: // Fall through. These could have
    163. case kEmbeddedImageFile: // various extensions
    164. default:
    165. pExtension[0] = _T('\0');
    166. break;
    167. }
    168. TCHAR* filePart;
    169. DWORD result;
    170. result = SearchPath(NULL, pcFilename, pExtension, nBufferLength,
    171. pcFullPathOut, &filePart);
    172. if (result && result < (DWORD)nBufferLength)
    173. return Acad::eOk;
    174. else
    175. return Acad::eFileNotFound;
    176. }
    177. void dumpBlockTable(AcDbBlockTable *pBlockTable)
    178. {
    179. AcDbBlockTableIterator *pIter;
    180. AcDbBlockTableRecord *pRecord;
    181. pBlockTable->newIterator(pIter);
    182. while (!pIter->done())
    183. {
    184. if (pIter->getRecord(pRecord, AcDb::kForRead) == Acad::eOk)
    185. {
    186. TCHAR *pName;
    187. if (pRecord->getName(pName) == Acad::eOk)
    188. {
    189. cout<<pName<<endl;
    190. printf("%s",pName);
    191. }
    192. pRecord->close();
    193. }
    194. pIter->step();
    195. }
    196. delete pIter;
    197. }
    198. CreatentHostApp gCreatentHostApp;
    199. int _tmain(int argc, TCHAR *argv[])
    200. {
    201. acdbSetHostApplicationServices(&gCreatentHostApp);
    202. long lcid = 0x00000409; // English
    203. acdbValidateSetup(lcid);
    204. // Create an AcDbDatabase and initialize its tables.
    205. AcDbDatabase *pDb = new AcDbDatabase(Adesk::kFalse);
    206. if (pDb == NULL)
    207. return 0;
    208. pDb->readDwgFile(_T("C:\\Documents and Settings\\xhzhu\\Desktop\\Test Template Explorer 1.1.4.46 for XUPU\\02\\aaaaaa.dwg"));
    209. acdbHostApplicationServices()->setWorkingDatabase(pDb);
    210. // Open the block table.
    211. AcDbBlockTable* pBlockTable;
    212. if (pDb->getBlockTable(pBlockTable, AcDb::kForRead) == Acad::eOk)
    213. {
    214. dumpBlockTable(pBlockTable); //get name list
    215. // Close the block table.
    216. pBlockTable->close();
    217. }
    218. delete pDb;
    219. acdbCleanUp();
    220. return 0;
    221. }
    222. 注意:c++的是不需要绑定的,必须实现AcDbHostApplicationServices,也的包含头文件.

    注意:c++的是不需要绑定的,必须实现AcDbHostApplicationServices,也的包含头文件.