zoukankan      html  css  js  c++  java
  • ArcEngine创建注记类及生成注记要素

    在宗地出图的时候遇到此功能要求,看代码

    ※创建注记类

    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

  • 相关阅读:
    《C#高级编程(第6版)》第10章筆記第10章集 合
    《C#高级编程(第6版)》第6章筆記第6章运算符和类型强制转换
    解决flash跨域读取XML的问题
    轉:showModalDialog和showModelessDialog使用心得
    用ASP为blog程序编写Trackback功能 小李刀刀(转载)
    轉:VB6中将数据导出到Excel提速之法
    C#讀書資源
    [轉]在SQL Server中使用种子表生成流水号注意顺序
    如何导入导出MySQL数据库*.sql文件操作
    《C#高级编程(第6版)》第7章筆記第7章委托和事件
  • 原文地址:https://www.cnblogs.com/clgis/p/10025055.html
Copyright © 2011-2022 走看看