在宗地出圖的時候遇到此功能要求,看代碼
※創建注記類
public void createBlackAnno(IMap pMap) { //創建mdb數據庫文件 IWorkspaceFactory pWorkspaceFactory = new AccessWorkspaceFactoryClass(); IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create("c:/Users/Administrator/Desktop/testfolder/","NewGDB.mdb",null,0); IName pName = pWorkspaceName as IName; IWorkspace pWorkspace = pName.Open() as IWorkspace; //創建數據集 IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace; ISpatialReferenceFactory pSpatialReferenceF = new SpatialReferenceEnvironmentClass(); ISpatialReference pSpatialReference = pSpatialReferenceF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954); pFeatureWorkspace.CreateFeatureDataset("abc",pSpatialReference); //創建注記要素類 IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset("abc"); IFeatureWorkspaceAnno pWorkspaceAnno = pFeatureDataset.Workspace as IFeatureWorkspaceAnno; /*------------------------------------------------*/ string Name = "AnnoFeatureClass"; IObjectClassDescription pObjectClassDes = new AnnotationFeatureClassDescriptionClass(); IFeatureClassDescription pFCDescription = pObjectClassDes as IFeatureClassDescription; IFields pFields = pObjectClassDes.RequiredFields; IAnnotateLayerProperties pALProperties = new LabelEngineLayerPropertiesClass(); pALProperties.FeatureLinked = false; pALProperties.AddUnplacedToGraphicsContainer = false; pALProperties.CreateUnplacedElements = true; pALProperties.DisplayAnnotation = true; pALProperties.UseOutput = true; ILabelEngineLayerProperties pLayerEngineLayerProps = pALProperties as ILabelEngineLayerProperties; IAnnotateLayerProperties pAlp = pLayerEngineLayerProps as IAnnotateLayerProperties; pAlp.Class = "Annotation Class 1"; IAnnotateLayerPropertiesCollection pAnnoPropscollection = new AnnotateLayerPropertiesCollection(); pAnnoPropscollection.Add(pAlp); IGraphicsLayerScale pGraphicsLayerScale = new GraphicsLayerScaleClass(); pGraphicsLayerScale.Units = esriUnits.esriMeters; pGraphicsLayerScale.ReferenceScale = 2000; /****************/ IFormattedTextSymbol myTextSymbol = new TextSymbolClass(); //peizhi 類里邊是三個基礎函數分辨是IRgbColor IFontDisp ITextSymbol 對應的,方法基礎,此處無代碼 Anno.peizhi pAnno = new Anno.peizhi(); IFontDisp pFont = pAnno.getFont("宋體", 12, false); IColor pColor = pAnno.getColor(34,56,78); myTextSymbol.Color = pColor; myTextSymbol.Font = pFont; myTextSymbol.Angle = 0; myTextSymbol.RightToLeft = false; myTextSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVABaseline; myTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHAFull; myTextSymbol.CharacterSpacing = 200; myTextSymbol.Case = esriTextCase.esriTCNormal; ISymbolCollection pSymbolCollection = new SymbolCollectionClass(); pSymbolCollection.set_Symbol(0, myTextSymbol as ISymbol); IFeatureClass pAnnoFeatureClass = pWorkspaceAnno.CreateAnnotationClass( Name, pFields, pObjectClassDes.InstanceCLSID, pObjectClassDes.ClassExtensionCLSID, pFCDescription.ShapeFieldName, "", pFeatureDataset, null, pAnnoPropscollection, pGraphicsLayerScale, pSymbolCollection, true); }
※生成注記要素
public void createAnnoFeature() { //pPoint 用於確定生成注記的位置 IPoint pPoint = new PointClass() { X = -10, Y = -6 }; peizhi p = new peizhi();//同上 ITextElement pTextElement = new TextElementClass() { Symbol = p.getTextSymbol(p.getColor(0, 255, 0), p.getFont("宋體", 12, true), 12), ScaleText = true, Text = "testet" }; IElement pElement = pTextElement as IElement; pElement.Geometry = pPoint; //以下兩行只是為了將生成的注記添加到調試界面,數據量大的時候可以刪除,以加快速度 IGraphicsContainer pGContainer = axMapControl1.Map as IGraphicsContainer; pGContainer.AddElement(pElement, 0); //將要素轉為注記 (axMapControl1.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, axMapControl1.Extent); IFeatureClass pFeatureClass = (this.axMapControl1.Map.get_Layer(0) as IFeatureLayer).FeatureClass; IDataset pDataset = pFeatureClass as IDataset; ITransactions pTransaction = pDataset.Workspace as ITransactions; pTransaction.StartTransaction(); IFDOGraphicsLayerFactory pFDOGLFactory = new FDOGraphicsLayerFactoryClass(); ILayer tempLayer = pFDOGLFactory.OpenGraphicsLayer(pDataset.Workspace as IFeatureWorkspace,pFeatureClass.FeatureDataset,pDataset.Name); IFDOGraphicsLayer pFDOGLayer = tempLayer as IFDOGraphicsLayer; IElementCollection pElementColl = new ElementCollectionClass(); pFDOGLayer.BeginAddElements(); pElementColl.Add(pElement, 0); pFDOGLayer.DoAddElements(pElementColl, 0); pFDOGLayer.EndAddElements(); pElementColl.Clear(); pTransaction.CommitTransaction(); }
※ps: 在生成注記的時候,考慮到注記類以及注記要素本身就屬於要素類要素,於是采用平常生成點線面要素的方法生成注記要素,使用CreateFeatureBuffer方法,由於Override字段不可賦值,與手動插入的注記要素僅有此字段不同,結果要素可以添加進注記的要素類中,但是不能顯示,用IDentity工具 也可以查看,確實有值,糾結好長時間,采用這個,有這方面的大哥希望一起討論一下。
QQ:1154048399