|
阅读:1418回复:0
AO加载CAD
<P>用AO打开CAD文件,代码如下,但打开后只有与MapControl相关联的TOCControl中有图层显示,但MapControl中为何没数据显示?</P>
<P>void CCADFileView::OnFileOpen() <BR>{<BR> // TOD Add your command handler code here<BR> CString strFilter;<BR> strFilter="Auto CAD(*.dwg)|*.dwg|Auto CAD(*.dxf)|.dxf||";<BR> CFileDialog fileDialog(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT|OFN_HIDEREADONLY|<BR> OFN_FILEMUSTEXIST,(LPCTSTR)strFilter,NULL);<BR> CString strTitle="Select CAD Files";<BR> fileDialog.m_ofn.lpstrTitle=strTitle;<BR> CString strInit("C:\\");<BR> fileDialog.m_ofn.lpstrInitialDir=strInit;<BR> if(IDCANCEL==fileDialog.DoModal()) return;<BR> CString strPath,strExt;<BR> strPath=fileDialog.GetPathName();<BR> strExt=fileDialog.GetFileExt();<BR> if(!(strExt.IsEmpty()))<BR> {<BR> int ncount;<BR> ncount=strPath.ReverseFind('\\');<BR> strPath=strPath.Left(ncount);<BR> }</P> <P> IWorkspaceFactoryPtr ipWorkspaceFactory(CLSID_CadWorkspaceFactory);<BR> IWorkspacePtr ipWorkspace;<BR> HRESULT hr=ipWorkspaceFactory->OpenFromFile((_bstr_t)strPath,NULL,;ipWorkspace);<BR> if(FAILED(hr)) return;<BR> <BR> IFeatureWorkspacePtr ipFeatureWorkspace(ipWorkspace);<BR> IFeatureLayerPtr ipFeatureLayer;<BR> IFeatureDatasetPtr ipFeatureDataset;<BR> IFeatureClassPtr ipFeatureClass;</P> <P> POSITION ps;<BR> ps=fileDialog.GetStartPosition();<BR> if(ps!=NULL)<BR> {<BR> CString strFullPath=fileDialog.GetNextPathName(ps);<BR> CString strName=strFullPath.Right(strFullPath.GetLength()-strPath.GetLength()-1);<BR> hr=ipFeatureWorkspace->OpenFeatureDataset((_bstr_t)strName,;ipFeatureDataset);<BR> if(FAILED(hr)) return;<BR> IFeatureClassContainerPtr ipFeatureClassContainer(ipFeatureDataset);<BR> long iTemp;<BR> ipFeatureClassContainer->get_ClassCount(;iTemp);<BR> enum esriFeatureType featuretype;<BR> BSTR aliasname;<BR> int i;<BR> for(i=0;i<iTemp;i++)<BR> {<BR> hr=ipFeatureClassContainer->get_Class(i,;ipFeatureClass);<BR> if(FAILED(hr)) return;<BR> hr=ipFeatureClass->get_FeatureType(;featuretype);<BR> if(FAILED(hr)) return;<BR> if(featuretype==esriFTCoverageAnnotation)<BR> {<BR> CoCreateInstance(CLSID_CadAnnotationLayer,NULL,CLSCTX_ALL,IID_IFeatureLayer,<BR> (void**);ipFeatureLayer);<BR> if(ipFeatureLayer==NULL) return;<BR> }<BR> else<BR> {<BR> CoCreateInstance(CLSID_CadFeatureLayer,NULL,CLSCTX_ALL,IID_IFeatureLayer,<BR> (void**);ipFeatureLayer);<BR> if(ipFeatureLayer==NULL) return;<BR> }<BR> ipFeatureClass->get_AliasName(;aliasname);<BR> ipFeatureLayer->put_Name(aliasname);<BR> ipFeatureLayer->putref_FeatureClass(ipFeatureClass);<BR> hr=m_ipMap->AddLayer(ipFeatureLayer);<BR> if(FAILED(hr)) return;<BR> }<BR> }<BR> IActiveViewPtr ipActive(m_ipMap);<BR> ipActive->Refresh();<BR>}<BR></P> |
|