CAD ObjectARX扩展工具的源码

Posted mjgw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CAD ObjectARX扩展工具的源码相关的知识,希望对你有一定的参考价值。

CAD ObjectARX扩展工具的源码(一)
收藏的CAD扩展工具的源码:

 

Acad::ErrorStatus CDrawFunction::getAllEntity(AcDbDatabase *pDb,AcDbObjectIdArray& IdArr,
const AcArray& layerNameArr)
{
Acad::ErrorStatus es=Acad::eOk;
ASSERT(pDb);
if(pDb==NULL)
return Acad::eInalidInput;
AcDbBlockTable *pBlkTable=NULL;
if((es=pDb->getBlockTable(pBlkTable,AcDb::kForRead))!=Acad::eOk)//打开块表
{
acedAlert("打开块表失败");
return es;
}
AcDbBlockTableRecord *pBlkTableRecord=NULL;
if((es=pBlkTable->getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForRead))!=Acad::eOk)//打开块表记录
{
acedAlert("打开块表记录失败");
pBlkTable->close();
return es;
}
pBlkTable->close();//关闭块表

AcDbBlockTableRecordIterator *pIterator=NULL; //创建叠代器
if((es=pBlkTableRecord->newIterator(pIterator))!=Acad::eOk)
{
pBlkTableRecord->close();
return es;
}

CAcModuleResourceOerride resoerride;
CProgressDlg progress;
progress.Create();
progress.SetPos(0);
progress.SetWindowText("正在检测图形中所有实体...");

for(pIterator->start();!pIterator->done();pIterator->step())//遍历整个数据库
{
AcDbEntity *entity=NULL;
es=pIterator->getEntity(entity,AcDb::kForRead); //打开实体

if(es==Acad::eLockiolation)
{
acedAlert("内存锁定");
}
else if(es==Acad::eWasOpenForWrite)
{
acedAlert("实体以写方式打开");
}
else if(es==Acad::eWasOpenForRead)
{
acedAlert("实体以读方式打开");
}
else
{
if(layerNameArr.contains(entity->layer()))
IdArr.append(entity->objectId());
entity->close();
}
progress.StepIt();
}
delete pIterator;pIterator=NULL;
pBlkTableRecord->close();
acutPrintf("eend");
return es;
}

//得到指定层上指定颜色的所有实体
Acad::ErrorStatus CDrawFunction::getAllEntity(AcDbDatabase *pDb,AcDbObjectIdArray& IdArr,
const AcArray& layerNameArr,Adesk::UInt16 colorIndex)
{
Acad::ErrorStatus es=Acad::eOk;
ASSERT(pDb);
if(pDb==NULL)
return Acad::eInalidInput;
AcDbBlockTable *pBlkTable=NULL;
if((es=pDb->getBlockTable(pBlkTable,AcDb::kForRead))!=Acad::eOk)//打开块表
{
acedAlert("打开块表失败");
return es;
}
AcDbBlockTableRecord *pBlkTableRecord=NULL;
if((es=pBlkTable->getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForRead))!=Acad::eOk)//打开块表记录
{
acedAlert("打开块表记录失败");
pBlkTable->close();
return es;
}
pBlkTable->close();//关闭块表

AcDbBlockTableRecordIterator *pIterator=NULL; //创建叠代器
if((es=pBlkTableRecord->newIterator(pIterator))!=Acad::eOk)
{
pBlkTableRecord->close();
return es;
}

CAcModuleResourceOerride resoerride;
CProgressDlg progress;
progress.Create();
progress.SetPos(0);
progress.SetWindowText("正在检测图形中所有实体...");

for(pIterator->start();!pIterator->done();pIterator->step())//遍历整个数据库
{
AcDbEntity *entity=NULL;
if((es=pIterator->getEntity(entity,AcDb::kForRead))!=Acad::eOk)//打开实体
{
delete pIterator;pIterator=NULL;
pBlkTableRecord->close();
return es;
}
if(layerNameArr.contains(entity->layer())&&entity->colorIndex()==colorIndex)
IdArr.append(entity->objectId());
entity->close();
progress.StepIt();
}
delete pIterator;pIterator=NULL;
pBlkTableRecord->close();
return es;
}

//得到图形中所有的层
Acad::ErrorStatus CDrawFunction::getAllLayerName(AcDbDatabase *pDb,CStringArray& layerArray)
{
Acad::ErrorStatus es=Acad::eOk;
if(pDb==NULL)
return Acad::eInalidInput;
layerArray.RemoeAll();
AcDbLayerTable *pLayerTable=NULL;
if((es=pDb->getSymbolTable(pLayerTable,AcDb::kForRead))!=Acad::eOk)
{
pLayerTable->close();
return es;
}
//创建一个层表迭代器
AcDbLayerTableIterator *pLayerTableIterator;
pLayerTable->newIterator(pLayerTableIterator);
pLayerTable->close();

char *pLayerName=NULL;
CString name;
for(int i=0;!pLayerTableIterator->done();pLayerTableIterator->step(),i++)
{
AcDbLayerTableRecord *pLayerTableRecord=NULL;
pLayerTableIterator->getRecord(pLayerTableRecord,AcDb::kForRead);
pLayerTableRecord->getName(pLayerName);
name.Format("%s",pLayerName);
pLayerTableRecord->close();
layerArray.Add(name);

}

if(pLayerName) acutDelString(pLayerName);
delete pLayerTableIterator;pLayerTableIterator=NULL;
return es;
}


//
//
BOOL CDrawFunction::getLinetypeIdFromString(const char* str, AcDbObjectId& id)
{
//----查找安装目录----//
CString AcadInstallPath;
FindAcadInstallPath(AcadInstallPath);
CString File=AcadInstallPath+"//linetype//user.lin";
//-----查找完毕--------//
ASSERT(str!=NULL);
AcDbLinetypeTable *pLinetypeTable;
acdbHostApplicationSerices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
Acad::ErrorStatus mess;
mess=pLinetypeTable->getAt(str,id);
if(mess==Acad::eKeyNotFound||mess==Acad::ePermanentlyErased)
{
pLinetypeTable->close();
Acad::ErrorStatus error;

error=acdbLoadLineTypeFile(str,File.GetBuffer(0),acdbHostApplicationSerices()->workingDatabase());
if(error==Acad::eNullObjectPointer)
{
AcDbLinetypeTable *pLinetypeTable;
acdbHostApplicationSerices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
pLinetypeTable->getAt("CONTINUOUS", id);
pLinetypeTable->close();
return FALSE;
}
else if(error==Acad::eFileSystemErr)
{
AfxMessageBox("the specified file cannot be opened");
return FALSE;
}
else if(error==Acad::eUndefinedLineType)
{
AcDbLinetypeTable *pLinetypeTable;
acdbHostApplicationSerices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
AfxMessageBox("the linetype name specified by ltname is not found in the file");
pLinetypeTable->getAt("CONTINUOUS", id);
pLinetypeTable->close();
return TRUE;
}
AcDbLinetypeTable *pLinetypeTable;
acdbHostApplicationSerices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
pLinetypeTable->getAt(str,id);
pLinetypeTable->close();
return TRUE;
}
pLinetypeTable->close();
return TRUE;
}

 

oid CDrawFunction::FindAcadInstallPath(CString &AcadInstallPath)
{
//查找样式目录安装路径
TCHAR AcadPath[255];
HKEY hKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE//线路处开发组//RDS2002//1.00"),0,KEY_QUERY_ALUE,&hKey)!=ERROR_SUCCESS)
{
::AfxMessageBox("注册路径不对");
return ;
}
DWORD dwDataType=REG_SZ;
DWORD dwLength=255;
LONG lRet=RegQueryalueEx(hKey,TEXT("path"),NULL,NULL,(LPBYTE)AcadPath,&dwLength);
RegCloseKey(hKey);
if(lRet!=ERROR_SUCCESS)
{
acutPrintf("Read failed/n");
return ;
}
//-----查找完毕--------//
AcadInstallPath.Format("%s",AcadPath);
}

AcDbObjectId CDrawFunction::createTextStyle(CString fontName,CString bigFontName,CString textStyleName)
{
AcGiTextStyle *TextStyle=new AcGiTextStyle
(fontName,
bigFontName,
0,
0.67,
0,
0,
Adesk::kFalse,
Adesk::kFalse,
Adesk::kFalse,
Adesk::kFalse,
Adesk::kFalse,
textStyleName); //字体名
AcDbObjectId textStyleId;
toAcDbTextStyle(*TextStyle,textStyleId);
return textStyleId;
}

AcDbObjectId CDrawFunction::createMutiText(AcGePoint3d BasePoint,AcDb::TextHorzMode hMode,AcDb::TextertMode Mode,CString Text,double texthight,double widthfactor,double angle,int color,CString smallFontName,CString bigFontName,CString layerName)
{
ASSERT(Text!=NULL);
AcDbMText *pMText=new AcDbMText();
if(pMText==NULL)
throw Acad::eOutOfMemory;
AcDbObjectId TextStyleId;
TextStyleId=createTextStyle(smallFontName,bigFontName,"xianlu");
pMText->setTextStyle(TextStyleId);
pMText->setContents(Text.GetBuffer(Text.GetLength()));
pMText->setTextHeight(texthight);
pMText->setRotation(angle);
pMText->setLineSpacingFactor(0.8);
pMText->setColorIndex(color);
if(layerName!="")
pMText->setLayer(layerName.GetBuffer(0));
AcDbObjectId MTextId;
addToModelSpace(MTextId, pMText);
pMText->close();
return MTextId;
}

以上是关于CAD ObjectARX扩展工具的源码的主要内容,如果未能解决你的问题,请参考以下文章

CAD ObjectARX扩展工具的源码

CAD 中二次开发

怎么用objectarx对cad添加菜单栏,求详细步骤

求教大神 我现在想学ARX编译CAD图形,恩。也就是CAD二次开发(一定是用ObjectARX开发环境)

请问 哪位大哥熟悉CAD二次开发ObjectARX CAD请进

ObjectARX-给cad实体贴图