void CAoEx1View::OnAddcad()
{
CString strFilter;
strFilter = "Auto CAD(*.dxf)| *.dxf|Auto CAD(*.dwg)|*.dwg||";
CFileDialog pDlg(TRUE,NULL,NULL,
OFN_ALLOWMULTISELECT|OFN_EXPLORER|OFN_LONGNAMES|OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,
(LPCTSTR)strFilter,
NULL);
CString strTitle =L"select CAD file";
pDlg.m_ofn.lpstrTitle = strTitle;
if(IDCANCEL ==pDlg.DoModal()) return;
CString strExt,strPath;
strExt = pDlg.GetFileExt();
strPath = pDlg.GetPathName();
if(!(strExt.IsEmpty()))
{
int temp;
temp = strPath.ReverseFind('//');
strPath = strPath.Left(temp);
}
IWorkspaceFactoryPtr ipCadWKSFact(CLSID_CadWorkspaceFactory);
IWorkspacePtr ipwspace;
HRESULT hr = ipCadWKSFact->OpenFromFile((_bstr_t)strPath,NULL,&ipwspace);
if(FAILED(hr)) return;
IFeatureWorkspacePtr ipWorkspace(ipwspace);
IFeatureLayerPtr ipCadFeatureLayer;
IFeatureDatasetPtr ipFeatureDataset;
IFeatureClassPtr ipFeatureClass;
CString strTemp;
int iPathLength;
int i;
iPathLength =strPath.GetLength();
POSITION ps;
ps= pDlg.GetStartPosition();
ISetPtr ipSet(CLSID_Set);
while (ps != NULL)
{
strTemp = pDlg.GetNextPathName(ps);
strTemp = strTemp.Right(strTemp.GetLength() - (iPathLength + 1));
hr = ipWorkspace->OpenFeatureDataset((_bstr_t)strTemp,&ipFeatureDataset);
if(FAILED(hr)) return;
IFeatureClassContainerPtr ipFeatureClassContainer(ipFeatureDataset);
long ITemp;
ipFeatureClassContainer->get_ClassCount(&ITemp);
enum esriFeatureType featuretype;
BSTR aliasname;
for(i = 0;i<ITemp;i++)
{
hr = ipFeatureClassContainer->get_Class(i,&ipFeatureClass);
if(FAILED(hr)) return;
hr = ipFeatureClass->get_FeatureType(&featuretype);
if(FAILED(hr)) return;
if (featuretype == esriFTCoverageAnnotation)
{
CoCreateInstance(CLSID_CadAnnotationLayer,NULL,CLSCTX_ALL,IID_IFeatureLayer,(void**)&ipCadFeatureLayer);
if(ipCadFeatureLayer == NULL) return;
}
else
{
CoCreateInstance(CLSID_CadFeatureLayer,NULL,CLSCTX_ALL,IID_IFeatureLayer,(void**)&ipCadFeatureLayer);
if(ipCadFeatureLayer == NULL) return;
}
ipFeatureClass->get_AliasName(&aliasname);
ipCadFeatureLayer->put_Name(aliasname);
ipCadFeatureLayer->putref_FeatureClass(ipFeatureClass);
m_ipMap = m_ctrlMap.get_Map();
hr = m_ipMap->AddLayer(ipCadFeatureLayer);
if(FAILED(hr)) return;
}
}
IEnvelopePtr ipEnv;
IActiveViewPtr ipActiveView(m_ipMap);
hr = ipActiveView->get_FullExtent(&ipEnv);
if(FAILED(hr)) return;
hr = ipActiveView->put_Extent(ipEnv);
if(FAILED(hr)) return;
hr = ipActiveView->Refresh();
if(FAILED(hr)) return;
}