CAD插入影像图

#include <ShLwApi.h>
#pragma comment(lib, “ShLwApi.lib”)
//插入影像图
Acad::ErrorStatus insertIMGFile(AcDbObjectId& ImgId,CString strCadLayerName,CString strFilePathName,CString KeyWord,AcGePoint2d pntLeftDown,AcGePoint2d pntRightUp)
{
#if _MSC_VER >= 1600
//一定要加载此文件,否则加载影像图时会失败
if (!acrxDynamicLinker->loadModule(_T(“acISMobj19.dbx”), true))
{
acutPrintf(_T(“无法加载acISMobj19.dbx\n”));
}
#elif _MSC_VER >= 1500
//一定要加载此文件,否则加载影像图时会失败
if (!acrxDynamicLinker->loadModule(_T(“acISMobj18.dbx”), true))
{
acutPrintf(_T(“无法加载acISMobj18.dbx\n”));
}
#elif _MSC_VER >= 1400
//一定要加载此文件,否则加载影像图时会失败
if (!acrxDynamicLinker->loadModule(_T(“acISMobj17.dbx”), true))
{
acutPrintf(_T(“无法加载acISMobj17.dbx\n”));
}
#elif _MSC_VER >= 1300
if (!acrxDynamicLinker->loadModule(_T(“acISMobj16.dbx”), true))
{
acutPrintf(_T(“无法加载acISMobj16.dbx\n”));
}
#else
if (!acrxDynamicLinker->loadModule(_T(“acISMobj15.dbx”), true))
{
acutPrintf(_T(“无法加载acISMobj15.dbx\n”));
}
#endif
if(strFilePathName.IsEmpty())
return Acad::eInvalidInput;
if(KeyWord.IsEmpty())
{
KeyWord = PathFindFileName(strFilePathName);
}
if(KeyWord.IsEmpty())//该影像已经加载
{
return Acad::eInvalidInput;
}
AcDbDictionary* pDictionary;
Adesk::Boolean bNameInUse;
AcDbObjectId DictionaryId;
AcDbObjectId RasterImgDefId;
AcDbObjectId RasterImgId;
AcDbRasterImageDef* pAcDbRasterImageDef;
Acad::ErrorStatus es;

//找到当前数据库的imageDictionary
DictionaryId=AcDbRasterImageDef::imageDictionary(acdbHostApplicationServices()->workingDatabase());
if(!DictionaryId.isValid())
{
//该词典还未创建
es = AcDbRasterImageDef::createImageDictionary(acdbHostApplicationServices()->workingDatabase(),DictionaryId);
if(es!=Acad::eOk)
{
return es;
}
}

//打开词典
es = acdbOpenObject((AcDbObject*&)pDictionary,DictionaryId,AcDb::kForWrite);
if(es!=Acad::eOk)
{
return es;
}

bNameInUse=pDictionary->has(KeyWord);
//如果词典中不存在该影像
if(!bNameInUse)
{
pAcDbRasterImageDef=new AcDbRasterImageDef;
es = pAcDbRasterImageDef->setSourceFileName(strFilePathName);
if(es!=Acad::eOk)
{
pDictionary->close();
return es;
}

es = pAcDbRasterImageDef->load();
if(es!=Acad::eOk)
{
pDictionary->close();
return es;
}
es = pDictionary->setAt(KeyWord,pAcDbRasterImageDef,RasterImgDefId);
if(es!=Acad::eOk)
{
pDictionary->close();
return es;
}
pAcDbRasterImageDef->close();
}
else
pDictionary->getAt(KeyWord,RasterImgDefId);
pDictionary->close();

//设置插入点以及宽度和高度
AcDbRasterImage *pRasterImg=new AcDbRasterImage();
AcGeVector3d CorVert,HigVert;
CorVert.x=pntRightUp.x-pntLeftDown.x;
CorVert.y=0;
CorVert.z=0;
HigVert.x=0;
HigVert.y=pntRightUp.y-pntLeftDown.y;
HigVert.z=0;
pRasterImg->setImageDefId(RasterImgDefId);
pRasterImg->setOrientation(AcGePoint3d(pntLeftDown.x,pntLeftDown.y,0),CorVert,HigVert);
pRasterImg->setDisplayOpt(AcDbRasterImage::kTransparent , Adesk::kTrue);
es=pRasterImg->setLayer(strCadLayerName);
if(es!=Acad::eOk)
{
pRasterImg->close();
return es;
}

//添加到数据库
AcDbDatabase *pDb=acdbHostApplicationServices()->workingDatabase();
AcDbBlockTable *pBt=NULL;
AcDbBlockTableRecord *pRc=NULL;
es=pDb->getBlockTable(pBt,AcDb::kForRead);
if(es!=Acad::eOk)
{
pRasterImg->close();
return es;
}
es=pBt->getAt(ACDB_MODEL_SPACE,pRc,AcDb::kForWrite);
if (es!=Acad::eOk)
{
pBt->close();
pBt=NULL;
pRasterImg->close();
return es;
}

Acad::ErrorStatus result=pRc->appendAcDbEntity(RasterImgId,pRasterImg);
if(result!=Acad::eOk)
{
pRc->close();
pBt->close();
pRasterImg->close();
return es;
}
ImgId=RasterImgId;

pRc->close();
pBt->close();
pRasterImg->close();
return es;
}

此条目发表在ObjectArx分类目录,贴了, 标签。将固定链接加入收藏夹。

发表评论