在做CAD注记转GIS注记,坎坷,百度了几乎所有可能的资料。
主要参照这里的方法(c++)。我使用C#重写了,均测试通过。
http://www.cnblogs.com/junyuz/archive/2011/09/01/2162625.html
重要提示:
必须创建好注记图层,设置好比例尺和坐标系。困在这里两天没通过。
方法一 通过IFDOGraphicsLayer添加Elements的方式实现
/// <summary> /// 添加到注记图层 /// </summary> /// <param name="pFeatCls">注记图层</param> /// <param name="pGeometry">插入的位置:一般是一个IPoint</param> /// <returns></returns> protected bool InsertAnnoFea(IFeatureClass pFeatCls, IGeometry pGeometry) { IFeatureClass annocls = pFeatCls; IDataset pDataset = annocls as IDataset; ITransactions pTransactions = pDataset.Workspace as ITransactions; pTransactions.StartTransaction(); IFDOGraphicsLayerFactory pFDOGLFactory = new FDOGraphicsLayerFactoryClass(); ILayer tmpLayer = pFDOGLFactory.OpenGraphicsLayer(pDataset.Workspace as IFeatureWorkspace, annocls.FeatureDataset, pDataset.Name); IFDOGraphicsLayer pFDOGLayer = tmpLayer as IFDOGraphicsLayer; IElementCollection pElementColl = new ElementCollectionClass(); pFDOGLayer.BeginAddElements(); ITextElement pTextElement = AnnoUtil.MakeTextElement(text, dHSize, rgbColor, sHFont, dHAngle) as ITextElement; IElement pElement = pTextElement as IElement; pElement.Geometry = pGeometry; pElementColl.Add(pElement, 0); pFDOGLayer.DoAddElements(pElementColl, 0); pFDOGLayer.EndAddElements(); pElementColl.Clear(); pTransactions.CommitTransaction(); return true; }
方法二 通过IAnnotationFeature来实现
/// <summary> /// 添加到注记图层 /// </summary> /// <param name="pFeatCls">注记图层</param> /// <param name="pGeometry">插入的位置:一般是一个IPoint</param> /// <returns></returns> protected bool InsertAnnoFea2(IFeatureClass pFeatCls, IGeometry pGeometry) { IFeatureClass annocls = pFeatCls; IWorkspace workspace = ((IDataset)annocls).Workspace; IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit; bool startEdit = workspaceEdit.IsBeingEdited(); if (!startEdit) { workspaceEdit.StartEditing(false); } workspaceEdit.StartEditOperation(); ITextElement pTextElement = AnnoUtil.MakeTextElement(text, dHSize, rgbColor, sHFont, dHAngle) as ITextElement; IElement pElement = pTextElement as IElement; pElement.Geometry = pGeometry; IFeature pFeature = annocls.CreateFeature(); IAnnotationFeature pAnnoFeature = pFeature as IAnnotationFeature; pAnnoFeature.Annotation = pElement; pFeature.Store(); workspaceEdit.StopEditOperation(); workspaceEdit.StopEditing(true); return true; }
junyuz:这两种方法,经过实际测试都可以成功,在导入的时候还需要注意一下空间参考系的问题,需要对应上,特别要注意dwg中的数据是否正确,如果注记的坐标不在参考系范围内,会出现导入失败的现象,我就是因为这个低级的错误,纠结了两天。
ps. MakeTextElement是创建ITextElement的方法。
时间: 2024-11-09 09:47:49