插入一个光栅图
C++开发|大少爷|2017-07-27 17:02
-
回答:
void CDrawCommand::InsertImage() { CFileDialog openDlg(TRUE,_T("jpg"),NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("jpg(*.jpg) | *.jpg|bmp(*.bmp) | *.bmp|tif(*.tif) | *.tif||"), MxDraw::GetMxDrawWnd(MxDraw::GetCurOcxHandle() ) ); CString sInitialDir = AfxGetApp()->GetProfileString(_T("MxDraw"), _T("InsertImager"),Mx::GetAppPath() ); CString sFileName; openDlg.m_ofn.lpstrInitialDir= sInitialDir; //设置打开文件的缺省路径 if(openDlg.DoModal() == IDOK) { sFileName = openDlg.GetPathName(); } else { return; } int iFindPos = sFileName.ReverseFind('\'); if(iFindPos >= 0) { CString sPath = sFileName.Left(iFindPos + 1); AfxGetApp()->WriteProfileString(_T("MxDraw"),_T("InsertImager"),sPath); } double dW = 10; double dH = 10; { // 得到相关图片消息 Gdiplus::Image tmpImage(sFileName); if(tmpImage.GetLastStatus() != Gdiplus::Ok) { acutPrintf(_T("n 图片文件不能打开")); return; } dW = tmpImage.GetWidth() / 10.0; dH = tmpImage.GetHeight() / 10.0; } // 得到插入点。 mds_point apt; int iRet = Mx::mcedGetPoint(NULL,_T("n 指定插入点:"),apt); if(iRet != RTNORM) return; CString sName = sFileName; { sName = MxT::AnalyzeFileName(sFileName,FALSE); } if(sName.IsEmpty() ) { MXASSERT(0); return; } // 在字典对对象中创建图片定义对象。 McDbDatabase* pDatabase = Mx::mcdbHostApplicationServices()->workingDatabase(); McDbDictionary* pDict = NULL; pDatabase->getNamedObjectsDictionary(pDict,McDb::kForWrite); if(pDict == NULL) return; // McDbObjectId imageDictId; if(pDict->getAt(MCAD_IMAGE_DICT_NAME,imageDictId) != Mcad::eOk) { // 创建该字典. McDbDictionary* pImageDict = new McDbDictionary; if(pDict->setAt(MCAD_IMAGE_DICT_NAME, pImageDict,imageDictId) != Mcad::eOk) { MXASSERT(0); delete pImageDict; pImageDict = NULL; } else { pImageDict->close(); } } pDict->close(); McDbObjectId imageDefId; if(!imageDictId.isNull() ) { McDbObjectPointer<McDbDictionary> spImageDict(imageDictId,McDb::kForWrite); if(spImageDict.openStatus() == Mcad::eOk) { if(spImageDict->getAt(sName,imageDefId) != Mcad::eOk) { // 添加图象定义. McDbRasterImageDef* pNewImageDef = new McDbRasterImageDef; pNewImageDef->setSourceFileName(sFileName ); if(spImageDict->setAt(sName, pNewImageDef,imageDefId) != Mcad::eOk) { MXASSERT(0); delete pNewImageDef; pNewImageDef = NULL; } else { pNewImageDef->close(); } } } } if(!imageDefId.isNull() ) { // 创建Image实例。 McDbRasterImage* pImage = new McDbRasterImage; McGeVector3d uCorner = McGeVector3d::kXAxis * dW; McGeVector3d vOnPlane = McGeVector3d::kYAxis * dH; pImage->setOrientation( asPnt3d(apt),uCorner,vOnPlane); pImage->setImageDefId(imageDefId); MrxDbgUtils::addToCurrentSpaceAndClose(pImage); } }