天天看點

為圖層增加注記表達式

從注記表達式的文本檔案中程式裡讀取并動态地增加注記。label expression 可以為簡單的或vb腳本等

/// <summary>

        /// 圖層增加注記表達式

        /// </summary>

        /// <param name="map">The map.</param>

        /// <param name="tcname">The tcname.</param>

        public static void AddAnno(ILayer plyr , string tcname)

        {

            if (!System.IO.File.Exists(注記檔案存儲路徑))

                return;

                        IGeoFeatureLayer pGeoFeaLayer = (plyr as IFeatureLayer) as IGeoFeatureLayer;

                        IAnnotateLayerPropertiesCollection pAnoLayPC = pGeoFeaLayer.AnnotationProperties;

                        pAnoLayPC.Clear();

 

                        ILabelEngineLayerProperties pAnnoLayerProps = new LabelEngineLayerPropertiesClass();

                        (pAnnoLayerProps.BasicOverposterLayerProperties as IOverposterLayerProperties2).TagUnplaced = false;  ////是否覆寫,對應Arcmap:layer properties-》labels-》placement properties-》conflict detection ::place overlapping labels

                        pAnnoLayerProps.SymbolID = 0;

                        IBasicOverposterLayerProperties4 blp = pAnnoLayerProps.BasicOverposterLayerProperties as IBasicOverposterLayerProperties4;

                        //blp.PointPlacementMethod = esriOverposterPointPlacementMethod.esriAroundPoint;

                        

                        blp.NumLabelsOption = esriBasicNumLabelsOption.esriOneLabelPerShape; ////每個圖形隻标注一個  label properties--palcement properties--duplicate labels

                        blp.PolygonPlacementMethod = esriOverposterPolygonPlacementMethod.esriAlwaysHorizontal; //// label properties--palcement properties--polygon settings

                        // blp.PlaceOnlyInsidePolygon = true;  ////保證在圖形内部  label properties--palcement properties--only place label inside polygon

 

 

                        string annoExpression = GetAnnoExpression(注記檔案存儲路徑);

                        if (annoExpression.ToUpper().IndexOf("FUNCTION") >= 0)

                            pAnnoLayerProps.IsExpressionSimple = false;

                        else

                            pAnnoLayerProps.IsExpressionSimple = true; //對應aArcMap的advanced選項

                        pAnnoLayerProps.Expression = annoExpression;

 

                        pAnnoLayerProps.Symbol = CreateTextSymbol();

                        pGeoFeaLayer.DisplayAnnotation = true;

        }

 

/// <summary>

        /// 根據注記檔案存儲路徑擷取标注表達式

        /// </summary>

        /// <param name="tcname">The tcname.</param>

        /// <returns></returns>

        public static string GetAnnoExpression(string注記檔案存儲路徑)

        {

            StreamReader sr = new StreamReader(注記檔案存儲路徑);

            string annoExpression = string.Empty;

            while (!sr.EndOfStream)

            {

                string text = sr.ReadLine();

                if (annoExpression == string.Empty)

                    annoExpression = text;

                else

                    annoExpression = annoExpression + "\r\n" + text;

            }

            return annoExpression;

        }      

繼續閱讀