CAD注记层转到SDE Annotation Features(ArcEngine,C++实现)

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

原文地址:https://www.cnblogs.com/junyuz/p/2162625.html