CAD註記層轉到SDE Annotation Features本文會提到兩種方式,不過都是要添加一個element,所以,首先給出一個建立element的函數
ITextElementPtr MakeTextElement(CString strText, double x, double y)
{
HRESULT hr;
ITextElementPtr ipTextElement(CLSID_TextElement);
hr = ipTextElement->put_ScaleText(VARIANT_TRUE);
hr = ipTextElement->put_Text(CComBSTR(strText));
IGroupSymbolElementPtr ipGroupSymEle = ipTextElement;
ipGroupSymEle->put_SymbolID(0);
IPointPtr ipPoint(CLSID_Point);
ipPoint->put_X(x);
ipPoint->put_Y(y);
IElementPtr ipElement = ipTextElement;
ipElement->put_Geometry(ipPoint);
return ipTextElement;
}
第一種辦法,通過IFDOGraphicsLayer添加Elements的方式實現,實現辦法如下
BOOL AddTextElementEx(CString strText, double x, double y)
{
HRESULT hr;
IFeatureWorkspacePtr ipFeatWorkspace = m_ipWorkspace;
if(ipFeatWorkspace == NULL)
return FALSE;
IFeatureClassPtr ipFeatureClass;
hr = ipFeatWorkspace->OpenFeatureClass(CComBSTR(_T("abc")),&ipFeatureClass);
IWorkspaceEditPtr ipWorkspaceEdit = m_ipWorkspace;
if(ipWorkspaceEdit==NULL)
return FALSE;
IDatasetPtr ipDataset(ipFeatureClass);
IFDOGraphicsLayerFactoryPtr ipFDOGLFactory(CLSID_FDOGraphicsLayerFactory);
IWorkspacePtr ipWs;
ipDataset->get_Workspace(&ipWs);
IFeatureWorkspacePtr ipFeatWs(ipWs);
IFeatureDatasetPtr ipFeatDataset;
ipFeatureClass->get_FeatureDataset(&ipFeatDataset);
ILayerPtr ipLayer;
BSTR bstr;
hr = ipDataset->get_Name(&bstr);
hr = ipFDOGLFactory->OpenGraphicsLayer(ipFeatWs,ipFeatDataset,bstr,&ipLayer);
if(FAILED(hr))
return FALSE;IFDOGraphicsLayerPtr ipFDOGLayer(ipLayer);
IElementCollectionPtr ipElementColl;
hr = ipElementColl.CreateInstance(CLSID_ElementCollection);
if(FAILED(hr)||ipElementColl==NULL)
return FALSE;
ITextElementPtr ipTextElement = MakeTextElement(strText,x,y);
hr = ipElementColl->Add((IElementPtr)ipTextElement,-1);
hr = ipFDOGLayer->BeginAddElements();
hr = ipFDOGLayer->DoAddElements(ipElementColl,0);
hr = ipFDOGLayer->EndAddElements();
return TRUE;
}
第二種方式,通過IAnnotationFeature來實現,實現代碼如下
BOOL AddTextElement(CString strText, double x, double y)
{
HRESULT hr;
IFeatureWorkspacePtr ipFeatWorkspace = m_ipWorkspace;
if(ipFeatWorkspace == NULL)
return FALSE;
IFeatureClassPtr ipFeatureClass;
hr = ipFeatWorkspace->OpenFeatureClass(CComBSTR(_T("abc")),&ipFeatureClass);
IWorkspaceEditPtr ipWorkspaceEdit = m_ipWorkspace;
if(ipWorkspaceEdit==NULL)
return FALSE;
hr = ipWorkspaceEdit->StartEditing(TRUE);
hr = ipWorkspaceEdit->StartEditOperation();
hr = ipWorkspaceEdit->EnableUndoRedo();
IElementPtr ipElement = MakeTextElement(strText,x , y);
IAnnotationFeaturePtr ipAnnotationFeature = ipFeature;
if(ipAnnotationFeature == NULL)
return FALSE;
hr=ipAnnotationFeature->put_Annotation(ipElement);
ipWorkspaceEdit->DisableUndoRedo();
ipWorkspaceEdit->StopEditOperation();
ipWorkspaceEdit->StopEditing(TRUE);
return TRUE;
}
這兩種方法,經過實際測試都可以成功,在匯入的時候還需要注意一下空間參考系的問題,需要對應上,特別要注意dwg中的資料是否正確,如果註記的座標不在參考系範圍內,會出現匯入失敗的現象,我就是因為這個低級的錯誤,糾結了兩天。
參考資料:http://www.cnblogs.com/iswszheng/archive/2009/03/18/1415496.html
http://blog.sina.com.cn/s/blog_5432e0220100kzui.html