1 #include <ShLwApi.h>
  2 #pragma comment(lib, "ShLwApi.lib")
  3 //插入影像图
  4 Acad::ErrorStatus insertIMGFile(AcDbObjectId& ImgId,CString strCadLayerName,CString strFilePathName,CString KeyWord,AcGePoint2d pntLeftDown,AcGePoint2d pntRightUp)
  5 {
  6 #if _MSC_VER >= 1600
  7 //一定要加载此文件,否则加载影像图时会失败
  8 if (!acrxDynamicLinker->loadModule(_T("acISMobj19.dbx"), true))
  9 {
 10 acutPrintf(_T("无法加载acISMobj19.dbx\n"));
 11 }
 12 #elif _MSC_VER >= 1500
 13 //一定要加载此文件,否则加载影像图时会失败
 14 if (!acrxDynamicLinker->loadModule(_T("acISMobj18.dbx"), true))
 15 {
 16 acutPrintf(_T("无法加载acISMobj18.dbx\n"));
 17 }
 18 #elif _MSC_VER >= 1400
 19 //一定要加载此文件,否则加载影像图时会失败
 20 if (!acrxDynamicLinker->loadModule(_T("acISMobj17.dbx"), true))
 21 {
 22 acutPrintf(_T("无法加载acISMobj17.dbx\n"));
 23 }
 24 #elif _MSC_VER >= 1300
 25 if (!acrxDynamicLinker->loadModule(_T("acISMobj16.dbx"), true))
 26 {
 27 acutPrintf(_T("无法加载acISMobj16.dbx\n"));
 28 }
 29 #else
 30 if (!acrxDynamicLinker->loadModule(_T("acISMobj15.dbx"), true))
 31 {
 32 acutPrintf(_T("无法加载acISMobj15.dbx\n"));
 33 }
 34 #endif
 35 if(strFilePathName.IsEmpty())
 36 return Acad::eInvalidInput;
 37 if(KeyWord.IsEmpty())
 38 {
 39 KeyWord = PathFindFileName(strFilePathName);
 40 }
 41 if(KeyWord.IsEmpty())//该影像已经加载
 42 {
 43 return Acad::eInvalidInput;
 44 }
 45 AcDbDictionary* pDictionary;
 46 Adesk::Boolean bNameInUse;
 47 AcDbObjectId DictionaryId;
 48 AcDbObjectId RasterImgDefId;
 49 AcDbObjectId RasterImgId;
 50 AcDbRasterImageDef* pAcDbRasterImageDef;
 51 Acad::ErrorStatus es;
 52 
 53 //找到当前数据库的imageDictionary
 54 DictionaryId=AcDbRasterImageDef::imageDictionary(acdbHostApplicationServices()->workingDatabase());
 55 if(!DictionaryId.isValid())
 56 {
 57 //该词典还未创建
 58 es = AcDbRasterImageDef::createImageDictionary(acdbHostApplicationServices()->workingDatabase(),DictionaryId);
 59 if(es!=Acad::eOk)
 60 {
 61 return es;
 62 }
 63 }
 64 
 65 //打开词典
 66 es = acdbOpenObject((AcDbObject*&)pDictionary,DictionaryId,AcDb::kForWrite);
 67 if(es!=Acad::eOk)
 68 {
 69 return es;
 70 }
 71 
 72 bNameInUse=pDictionary->has(KeyWord);
 73 //如果词典中不存在该影像
 74 if(!bNameInUse)
 75 {
 76 pAcDbRasterImageDef=new AcDbRasterImageDef;
 77 es = pAcDbRasterImageDef->setSourceFileName(strFilePathName);
 78 if(es!=Acad::eOk)
 79 {
 80 pDictionary->close();
 81 return es;
 82 }
 83 
 84 es = pAcDbRasterImageDef->load();
 85 if(es!=Acad::eOk)
 86 {
 87 pDictionary->close();
 88 return es;
 89 }
 90 es = pDictionary->setAt(KeyWord,pAcDbRasterImageDef,RasterImgDefId);
 91 if(es!=Acad::eOk)
 92 {
 93 pDictionary->close();
 94 return es;
 95 }
 96 pAcDbRasterImageDef->close();
 97 }
 98 else
 99 pDictionary->getAt(KeyWord,RasterImgDefId);
100 pDictionary->close();
101 
102 //设置插入点以及宽度和高度
103 AcDbRasterImage *pRasterImg=new AcDbRasterImage();
104 AcGeVector3d CorVert,HigVert;
105 CorVert.x=pntRightUp.x-pntLeftDown.x;
106 CorVert.y=0;
107 CorVert.z=0;
108 HigVert.x=0;
109 HigVert.y=pntRightUp.y-pntLeftDown.y;
110 HigVert.z=0;
111 pRasterImg->setImageDefId(RasterImgDefId);
112 pRasterImg->setOrientation(AcGePoint3d(pntLeftDown.x,pntLeftDown.y,0),CorVert,HigVert);
113 pRasterImg->setDisplayOpt(AcDbRasterImage::kTransparent , Adesk::kTrue);
114 es=pRasterImg->setLayer(strCadLayerName);
115 if(es!=Acad::eOk)
116 {
117 pRasterImg->close();
118 return es;
119 }
120 
121 //添加到数据库
122 AcDbDatabase *pDb=acdbHostApplicationServices()->workingDatabase();
123 AcDbBlockTable *pBt=NULL;
124 AcDbBlockTableRecord *pRc=NULL;
125 es=pDb->getBlockTable(pBt,AcDb::kForRead);
126 if(es!=Acad::eOk)
127 {
128 pRasterImg->close();
129 return es;
130 }
131 es=pBt->getAt(ACDB_MODEL_SPACE,pRc,AcDb::kForWrite);
132 if (es!=Acad::eOk)
133 {
134 pBt->close();
135 pBt=NULL;
136 pRasterImg->close();
137 return es;
138 }
139 
140 Acad::ErrorStatus result=pRc->appendAcDbEntity(RasterImgId,pRasterImg);
141 if(result!=Acad::eOk)
142 {
143 pRc->close();
144 pBt->close();
145 pRasterImg->close();
146 return es;
147 }
148 ImgId=RasterImgId;
149 
150 pRc->close();
151 pBt->close();
152 pRasterImg->close();
153 return es;
154 }

View Code

 

版权声明:本文为xzh1993原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xzh1993/p/5250440.html