以下均為console application,在vs2005中編譯通過!需要引用Autodesk的Object DBX 或最新的Real DWG 的SDK。
如有其他的問題,請留言!
列舉dwg文件中的所有的blcok的name:
C#實現:

using?System;
using?System.Collections;
using?System.Collections.Generic;
using?System.Text;
using?System.Diagnostics;
using?System.Reflection;
using?System.Runtime;
using?System.Windows.Forms;
using?System.IO;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;


using?Autodesk.AutoCAD.DatabaseServices;
using?System.Runtime.InteropServices;
using?Autodesk.AutoCAD.Runtime;
using?Autodesk.AutoCAD.Geometry;




[assembly:?SecuredApplication(
@"license")]

namespace?GetFrameOfRealDwg


{
????class?MyHostApplicationServices?:?Autodesk.AutoCAD.DatabaseServices.HostApplicationServices

????
{
????????public?override?System.String?FindFile(System.String?fileName,
????????????????????????????????????????????????Autodesk.AutoCAD.DatabaseServices.Database?database,
?????????????????????????????????????????????????Autodesk.AutoCAD.DatabaseServices.FindFileHint?hint
?????????????????????????????????????????????????)

????????
{

????????????return?string.Empty;
????????}
????????static?public?ArrayList?GetBlockNames(Database?db)

????????
{
????????????ArrayList?array?=?new?ArrayList();
????????????Transaction?tran?=?db.TransactionManager.StartTransaction();
????????????try

????????????
{
????????????????BlockTable?bt?=?(BlockTable)tran.GetObject(db.BlockTableId,?OpenMode.ForWrite);
????????????????foreach?(ObjectId?recordid?in?bt)

????????????????
{
????????????????????BlockTableRecord?record?=?(BlockTableRecord)tran.GetObject(recordid,?OpenMode.ForRead);
????????????????????array.Add(record.Name);
????????????????}
????????????}
????????????catch

????????????
{
????????????}
????????????finally

????????????
{
????????????????tran.Dispose();
????????????}
????????????return?array;
????????}
????????static?void?Main(string[]?args)

????????
{


????????????MyHostApplicationServices?myserver?=?new?MyHostApplicationServices();
????????????int?lcid?=?0x00001033;?//?English
????????????RuntimeSystem.Initialize(myserver,?lcid);
????????????Database?Db?=?new?Database(false,?true);
????????????Db.ReadDwgFile(@"filepath",?FileShare.Read,?false,?"");
????????????ArrayList?ar?=?GetBlockNames(Db);
????????????foreach?(string?str?in?ar)

????????????
{
????????????????System.Console.WriteLine(str);
????????????}

????????????RuntimeSystem.Terminate();
????????????System.Console.WriteLine();

????????}
????}
}

注意:? 只需ref? :acdbmgd.dll?
???????????必須實現HostApplicationServices及她的findfile().
?????????? 生成的dll必須使用綁定工具綁定.
????????? (license是很貴的啊,可以到autodesk公司主頁)
c++實現:
#pragma?once
#define?_CRT_SECURE_NO_DEPRECATE

#include?"windows.h"
#include?"dbsymtb.h"
#include?"dbents.h"
#include?<stdio.h>
#include?<string.h>
#include?"tchar.h"
#include?<string>
#include?<atlconv.h>
#include?<iostream>

#include?"dbapserv.h"
using?namespace?std;
class?CreatentHostApp?:?public?AcDbHostApplicationServices


{
????Acad::ErrorStatus?findFile(TCHAR*?pcFullPathOut,?int?nBufferLength,
?????????????????????????const?TCHAR*?pcFilename,?AcDbDatabase*?pDb?=?NULL,
?????????????????????????AcDbHostApplicationServices::FindFileHint?=?kDefault);

????//?These?two?functions?return?the?full?path?to?the?root?folder?where?roamable/local?
????//?customizable?files?were?installed.?Note?that?the?user?may?have?reconfigured?
????//?the?location?of?some?the?customizable?files?using?the?Options?Dialog?
????//?therefore?these?functions?should?not?be?used?to?locate?customizable?files.?
????//?To?locate?customizable?files?either?use?the?findFile?function?or?the?
????//?appropriate?system?variable?for?the?given?file?type.?
????//
????Acad::ErrorStatus?getRoamableRootFolder(const?TCHAR*&?folder);
????Acad::ErrorStatus?getLocalRootFolder(const?TCHAR*&?folder);
????//?make?sure?you?implement?getAlternateFontName.?In?case?your?findFile?implementation
????//?fails?to?find?a?font?you?should?return?a?font?name?here?that?is?guaranteed?to?exist.
????virtual?TCHAR?*?getAlternateFontName()?const

????
{
????????return?_T("txt.shx");?//findFile?will?be?called?again?with?this?name
????}
};

//?Return?the?Install?directory?for?customizable?files
Acad::ErrorStatus?
CreatentHostApp::getRoamableRootFolder(const?TCHAR*&?folder)


{
????Acad::ErrorStatus?ret?=?Acad::eOk;
????static?TCHAR?buf[MAX_PATH]?=?_T("\0");?//MDI?SAFE
????if?(buf[0]==0)
????????if?(GetModuleFileName(NULL,?buf,?MAX_PATH)?!=?0)
????????????ret?=?Acad::eRegistryAccessError;
????folder?=?buf;
????return?ret;
}

//?Return?the?Install?directory?for?customizable?files
Acad::ErrorStatus?
CreatentHostApp::getLocalRootFolder(const?TCHAR*&?folder)


{
????Acad::ErrorStatus?ret?=?Acad::eOk;
????static?TCHAR?buf[MAX_PATH]?=?_T("\0");?//MDI?SAFE
????if?(buf[0]==0)
????????if?(GetModuleFileName(NULL,?buf,?MAX_PATH)?!=?0)
????????????ret?=?Acad::eRegistryAccessError;
????folder?=?buf;
????return?ret;
}


Acad::ErrorStatus?
CreatentHostApp::findFile(TCHAR*?pcFullPathOut,?int?nBufferLength,
????const?TCHAR*?pcFilename,?AcDbDatabase*?pDb,?
????AcDbHostApplicationServices::FindFileHint?hint)


{
????TCHAR?pExtension[5];
????switch?(hint)

????
{
????????case?kCompiledShapeFile:
????????????_tcscpy(pExtension,?_T(".shx"));
????????????break;
????????case?kTrueTypeFontFile:
????????????_tcscpy(pExtension,?_T(".ttf"));
????????????break;
????????case?kPatternFile:
????????????_tcscpy(pExtension,?_T(".pat"));
????????????break;
????????case?kARXApplication:
????????????_tcscpy(pExtension,?_T(".dbx"));
????????????break;
????????case?kFontMapFile:
????????????_tcscpy(pExtension,?_T(".fmp"));
????????????break;
????????case?kXRefDrawing:
????????????_tcscpy(pExtension,?_T(".dwg"));
????????????break;
????????case?kFontFile:????????????????//?Fall?through.??These?could?have
????????case?kEmbeddedImageFile:???????//?various?extensions
????????default:
????????????pExtension[0]?=?_T('\0');
????????????break;
????}
????TCHAR*?filePart;
????DWORD?result;
????result?=?SearchPath(NULL,?pcFilename,?pExtension,?nBufferLength,?
????????????????????????pcFullPathOut,?&filePart);
????if?(result?&&?result?<?(DWORD)nBufferLength)
????????return?Acad::eOk;
????else
????????return?Acad::eFileNotFound;
}
void?dumpBlockTable(AcDbBlockTable?*pBlockTable)


{
????
????AcDbBlockTableIterator?*pIter;
????AcDbBlockTableRecord?*pRecord;
????pBlockTable->newIterator(pIter);???
????while?(!pIter->done())

????
{
????????if?(pIter->getRecord(pRecord,?AcDb::kForRead)?==?Acad::eOk)

????????
{
????????????TCHAR?*pName;????????????
????????????if?(pRecord->getName(pName)?==?Acad::eOk)

????????????
{??
????????????????cout<<pName<<endl;
????????????????printf("%s",pName);
????????????}??????????
????????????pRecord->close();
????????}
????????pIter->step();
????}
????delete?pIter;
????
}
CreatentHostApp?gCreatentHostApp;

int?_tmain(int?argc,?TCHAR?*argv[])


{
????acdbSetHostApplicationServices(&gCreatentHostApp);
????long?lcid?=?0x00000409;?//?English
????acdbValidateSetup(lcid);

????//?Create?an?AcDbDatabase?and?initialize?its?tables.
????AcDbDatabase?*pDb?=?new?AcDbDatabase(Adesk::kFalse);
????if?(pDb?==?NULL)
????????return?0;
????pDb->readDwgFile(_T("C:\\Documents?and?Settings\\xhzhu\\Desktop\\Test?Template?Explorer?1.1.4.46?for?XUPU\\02\\aaaaaa.dwg"));

????acdbHostApplicationServices()->setWorkingDatabase(pDb);

????//?Open?the?block?table.
????AcDbBlockTable*?pBlockTable;
????if?(pDb->getBlockTable(pBlockTable,?AcDb::kForRead)?==?Acad::eOk)

????
{??????
????????dumpBlockTable(pBlockTable);??//get?name?list
????????//?Close?the?block?table.
????????pBlockTable->close();
????}?

????delete?pDb;
???
????acdbCleanUp();
????return?0;
}

注意:c++的是不需要綁定的,必須實現AcDbHostApplicationServices,也的包含頭文件.