delete pNewDb; return; }
//这里blcokId是insert运行后产生的,它代表的是一个块表记录AcDbBlockRecord的ID. pBlockName是记录名,要在insert运行前设定其值.
如果我们在这里结束程序,我们看不到任何东西,因为source并没有真正被插入.我们还要做一些事,首先是创建一个 AcDbBlockReference, 并将它指向blockId所代表的AcDbBlockRecord, 然后将这个AcDbBlockReference加入pDb所代表的图形数据库中. AcDbBlockReference *pBlkRef = new AcDbBlockReference; pBlkRef->setBlockTableRecord(blockId);//指向blockId; pBlkRef->setPosition(Pt);//设定位置 pBlkRef->setRotation(Angle);//设定转角
pBlkRef->setScaleFactors( XrefScale);//设定放大比例 AcDbBlockTable *pBlockTable;
pDb->getSymbolTable(pBlockTable, AcDb::kForRead); AcDbBlockTableRecord *pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite); pBlockTable->close(); AcDbObjectId newEntId;
pBlockTableRecord->appendAcDbEntity(newEntId, pBlkRef); pBlockTableRecord->close();
如果我们在这里结束程序,我们将看到当前图形中source.dwg已经被作为图块插入.但是图块中没有source.dwg所定义的Attibute. 因此我们还要做工作.后面的事情就简单了. AcDbBlockTableRecord *pBlockDef;
acdbOpenObject(pBlockDef, blockId, AcDb::kForRead); AcDbBlockTableRecordIterator *pIterator; pBlockDef->newIterator(pIterator); AcGePoint3d basePoint; AcDbEntity *pEnt;
AcDbAttributeDefinition *pAttdef; for (pIterator->start(); !pIterator->done();
pIterator->step())//将source.dwg中所有的Attibute进行遍历 {
pIterator->getEntity(pEnt, AcDb::kForRead); pAttdef = AcDbAttributeDefinition::cast(pEnt); if (pAttdef != NULL && !pAttdef->isConstant()) { AcDbAttribute *pAtt = new AcDbAttribute(); pAtt->setPropertiesFrom(pAttdef); pAtt->setInvisible(pAttdef->isInvisible()); basePoint = pAttdef->position();
basePoint += pBlkRef->position().asVector(); pAtt->setPosition(basePoint); pAtt->setHeight(pAttdef->height()); pAtt->setRotation(pAttdef->rotation()); pAtt->setTag(\pAtt->setFieldLength(25); char *pStr = pAttdef->tag(); pAtt->setTag(pStr); acutDelString(pStr);
pAtt->setFieldLength(pAttdef->fieldLength()); pAtt->setTextString(\AcDbObjectId attId;
pBlkRef->appendAttribute(attId, pAtt); pAtt->close(); }
pEnt->close(); // use pEnt... pAttdef might be NULL }
delete pIterator;
六 在ARX打开文件
在AutoCAD中打开图形,并且显示在图形窗口中,可以使用acedSyncFileOpen()函数。需要注意的是,这个函数只能在单文档模式中工作,
用户可以在AutoCAD“选项”对话框的“系统”选项卡中进行设置,或者在主函数中添加下面的语句: acrxDynamicLinker->registerAppNotMDIAware(pkt); 具体的函数如下: //加载模板文件 void LoadTemplate()
{
char fname[50];
strcpy(fname,\#ifndef _ACAD2000 Acad::ErrorStatuses;
es = acedSyncFileOpen(fname); #else
acDocManager->appContextOpenDocument(fname); #endif }
如果在多文档模式下,下面的方法可以在执行时弹出“选择文件”对话框,用户选择所要打开的文件后,在图形窗口中显示该图形。 void ZffOPENOpenDwg() {
// 使用“选择文件”对话框选择所要打开的文件 acDocManager->openDocument(); }
下面的方法则打开指定位置的DWG文件。 void OpenDoc( void *pData) {
AcApDocument* pDoc = acDocManager->curDocument(); if (acDocManager->isApplicationContext()) {
acDocManager->appContextOpenDocument((const char *)pData); } else {
acutPrintf(\} }
// This is command 'OPEN1' void ZffOPENopen1() {
// 直接打开系统中存在的某个图形文件G:\\AutoCAD图形\\wen2.dwg static char pData[] = \图形\\\\wen2.dwg\
acDocManager->executeInApplicationContext(OpenDoc, (void *)pData); }
void ProgressBarTest() {
acutPrintf(\启动进度条..........\\n\
acedSetStatusBarProgressMeter(\测试进度条\for (int value=0; value<=100; value++) { Sleep(100); // 暂停, 释放控制权 acedSetStatusBarProgressMeterPos(value); }
acedRestoreStatusBar(); return; }
如何获得程序路径 struct resbuf rb; char sTemp[1024],*str; ads_getvar(\strcpy(sTemp,rb.resval.string); acad_free(rb.resval.rstring); str=strchr(sTemp,';'); *str='\\0';
str=strrchr(sTemp,'\\\\'); *str='\\0';
上段程序中,sTemp中存储了安装CAD的目录
AUTOCAD的系统变量存储了一些与安装有关的信息,虽然不多,在正常情况是够用的.与目录有关的主要有: dwgprefix 当前dwg图形存储的目录 acadprefix acad环境变量存储的目录
dwgname 当前dwg文件名 savefile 当前自动存储文件名
///从RGB得到cad颜色索引值 int getNearestACI(COLORREF color) {
long acirgb, r,g,b;
long mindst = 2147483647L; long dst = 0; int minndx = 0;
long red=GetRValue(color); long green=GetGValue(color); long blue=GetBValue(color); for ( int i = 1; i < 255; i++ ) { acirgb = acdbGetRGB ( i ); r =GetRValue(acirgb); g =GetGValue(acirgb); b =GetBValue(acirgb);
dst = abs ( r-red) + abs ( g -green) + abs (b-blue); if ( dst < mindst ) { minndx = i; mindst = dst; } }
return minndx; }
//功 能:从CAD的颜色得到RGB
COLORREF CGlobal::GetColorFromIndex(int colorIndex) {
if(colorIndex < 0 || colorIndex > 255) {
ads_alert(\传入的颜色号不在0~255之间!\ return 0;