以下均为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++实现:
注意:c++的是不需要绑定的,必须实现AcDbHostApplicationServices,也的包含头文件.
如有其他的问题,请留言!
列举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;
}
#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,也的包含头文件.
posted on 2006-06-21 17:41 梦在天涯 阅读(4852) 评论(6) 编辑 收藏 引用 所属分类: ARX/DBX
评论
# re: ObjectDBX技术: 脱离AutoCAD处理dwg文件(NET 实现)2006-10-09 21:21y
我按你的方法做了,可到acdbSetHostApplicationServices(&gCreatentHostApp);就报“DBX CAS 4”的错误,如何解决?
回复 更多评论
# re: ObjectDBX技术: 脱离AutoCAD处理dwg文件(NET 实现)2006-10-10 08:57梦在天涯
这个错误经常的出现!
^_^!
你用的是c++的吗,你可能是由于连接了一些多余的跟AUTOCAD平台有关的DLL。
如果你用的是c#版的,你肯能是由于没有lience或是没有绑定成功,也可能是acdbmgd.dll的版本不正确!
^_^!
最后提醒这里说的Object DBX 不是Object ARX中的自定义实体啊,是Autodesk的另一个可以脱离autocad平台处理dwg的技术,最新的版本名字是RealDWG。
希望能够帮助大家! 回复 更多评论
# re: ObjectDBX技术: 脱离AutoCAD处理dwg文件(NET 实现)2006-11-16 14:26yy
麻烦给一个RealDWG的下载地址 回复 更多评论
# re: ObjectDBX技术: 脱离AutoCAD处理dwg文件(NET 实现)2006-11-17 12:32梦在天涯
RealDwg是AutoDesk的收费的,不知道有没有官方的下载连接啊,.net板的必须的有她的licence绑定以后才可以使用的啊!
你可以自己到AutoDesk的网站看看有没有! 回复 更多评论
# re: ObjectDBX技术: 脱离AutoCAD处理dwg文件(NET 实现)2006-11-21 12:15cj
请教一下 如何使用ObjectDBX导入ADT相关的aec开头的DBX文件?谢谢! 回复 更多评论
# re: ObjectDBX技术: 脱离AutoCAD处理dwg文件(NET 实现)2012-01-05 15:02aaa
我建立了一个DBX项目,写了一个自定义实体,想以外部图纸作为模板画这个实体.
用到readDwgFile函数时总是不成功,出现DBX CAS4错误.
看到你的文章中说要实现AcDbHostApplicationServices,请问能再说的具体点吗? 回复 更多评论