单个要素制图表达的获取及修改

来源:互联网 发布:矩阵特征值 编辑:程序博客网 时间:2024/05/19 14:00

1.获取制图表达

    /// <summary>        /// 获取图层中某个要素的制图表达        /// </summary>        /// <param name="pLyr">图层</param>        /// <param name="pActiveView">视图</param>        /// <param name="pFea">单个要素</param>        /// <returns></returns>        public static IRepresentation getRepresentation(ILayer pLyr, IActiveView pActiveView, IFeature pFea)        {            try            {                IMapContext pMapContext = new MapContextClass();                pMapContext.InitFromDisplay(pActiveView.ScreenDisplay.DisplayTransformation);                IFeatureLayer pFeaLyr = pLyr as IFeatureLayer;                IRepresentationRenderer pRepRenderer = (pFeaLyr as IGeoFeatureLayer).Renderer as IRepresentationRenderer;                IRepresentation pRepresentation = pRepRenderer.RepresentationClass.GetRepresentation(pFea, pMapContext);                return pRepresentation;            }            catch (Exception ex)            {                return null;            }        }


2.获取MapControl中指定名称的图层

  /// <summary>        /// 获取地图上要素类图层        /// </summary>        /// <param name="pMap"></param>        /// <param name="sUID"></param>        /// <param name="sLayerName"></param>        /// <returns></returns>        public static ILayer GetLayer(IMap pMap, string sLayerName)        {            ILayer pLyr = null;            UID uid = new UIDClass();            uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";             IEnumLayer pEnumLayer = pMap.get_Layers(uid, true);            try            {                pEnumLayer.Reset();                ILayer pLayer = null;                while ((pLayer = pEnumLayer.Next()) != null)                {                    if (!pLayer.Visible)                    {                        continue;                    }                    if (!string.IsNullOrEmpty(sLayerName) && sLayerName == ((pLayer as IFeatureLayer).FeatureClass as IDataset).Name)                    {                        pLyr = pLayer;                    }                }                return pLyr;            }            catch (Exception ex)            {                return null;            }            finally            {                Marshal.ReleaseComObject(pEnumLayer);            }        }

3.获取制图表达属性信息并进行更改的操作

 private void UpdateRepColor(ILayer pLyr, string JBConde, List<ConvertMappingInfo> plistMappingInfo, ConvertToType convertToType)        {            IWorkspaceEdit2 pWsEdit = pWs as IWorkspaceEdit2;            try            {                IFeatureCursor pFeaCursor = (pLyr as IFeatureLayer).FeatureClass.Update(null, false);                IFeature pFea = null;                //开启编辑会话                WorkspaceAPI.StartWorkspaceEdit(pWs);                pWsEdit.StartEditOperation();                while ((pFea = pFeaCursor.NextFeature()) != null)                {                    IRepresentation pRepresentation = CommonAPI.getRepresentation(pLyr, ArcMap.Document.ActivatedView, pFea);                    IRepresentationRule pRepRule = pRepresentation.RepresentationClass.RepresentationRules.get_Rule(pRepresentation.RuleID);                    string strRuleName = pRepresentation.RepresentationClass.RepresentationRules.get_Name(pRepresentation.RuleID);                    if (strRuleName != JBConde)                    {                        continue;                    }                    for (int i = 0; i < pRepRule.LayerCount; i++)                    {                        IBasicSymbol pBasicSymbol = pRepRule.get_Layer(i);                        IGraphicAttributes pGraphicAttributes = null;                        //IGeometry pRepGeometry = null;                        if (pBasicSymbol is IBasicMarkerSymbol)                        {                            IBasicMarkerSymbol pBasicMarkerSymbol = pBasicSymbol as IBasicMarkerSymbol;                            //pRepGeometry = pRepresentation.Shape;                            pGraphicAttributes = pBasicMarkerSymbol as IGraphicAttributes;                        }                        if (pBasicSymbol is IBasicLineSymbol)                        {                            IBasicLineSymbol pBasicLineSymbol = pBasicSymbol as IBasicLineSymbol;                            ILineStroke pLineStroke = pBasicLineSymbol.Stroke;                            //IDrawingOutline pDrawingOutline = pLineStroke as IDrawingOutline;                            //IGeometry geo = pDrawingOutline.GetAllOutlineParts(feature.Shape, esriOutlineType.esriOutlineExact, esriOutlineOption.esriOutlineConvex, 3.14, null);                            //pRepGeometry = pRepresentation.Shape;                            pGraphicAttributes = pLineStroke as IGraphicAttributes;                        }                        if (pBasicSymbol is IBasicFillSymbol)                        {                            IBasicFillSymbol pBasicFillSymbol = pBasicSymbol as IBasicFillSymbol;                            IFillPattern pFillPattern = pBasicFillSymbol.FillPattern;                            //pRepGeometry = pRepresentation.Shape;                            pGraphicAttributes = pFillPattern as IGraphicAttributes;                        }                        for (int N = 0; N < pGraphicAttributes.GraphicAttributeCount; N++)                        {                            int iId = pGraphicAttributes.get_ID(N);                            string name = pGraphicAttributes.get_Name(iId);                            IGraphicAttributeType pGraphicAttributeType = pGraphicAttributes.get_Type(iId);                            if (pGraphicAttributeType.Type == esriGraphicAttributeType.esriAttributeTypeColor)                            {                                IColor pColor = pRepresentation.get_Value(pGraphicAttributes, iId) as IColor;                                ICmykColor pCMYKColor = pColor as ICmykColor;                                if (pCMYKColor==null)                                {                                    continue;                                }                                if (convertToType == ConvertToType.ChuBan)                                {                                    ConvertMappingInfo pConvertInfo = new ConvertMappingInfo();                                    pConvertInfo.PC = pCMYKColor.Cyan;                                    pConvertInfo.PM = pCMYKColor.Magenta;                                    pConvertInfo.PY = pCMYKColor.Yellow;                                    pConvertInfo.PK = pCMYKColor.Black;                                    foreach (ConvertMappingInfo c in plistMappingInfo)                                    {                                        if (pConvertInfo.CompareColorValue(c, convertToType))                                        {                                            pCMYKColor.Cyan = c.CC;                                            pCMYKColor.Magenta = c.CM;                                            pCMYKColor.Yellow = c.CY;                                            pCMYKColor.Black = c.CK;                                            pRepresentation.set_Value(pGraphicAttributes, iId, pCMYKColor);                                        }                                    }                                }                                if (convertToType == ConvertToType.PenHui)                                {                                    ConvertMappingInfo pConvertInfo = new ConvertMappingInfo();                                    pConvertInfo.CC = pCMYKColor.Cyan;                                    pConvertInfo.CM = pCMYKColor.Magenta;                                    pConvertInfo.CY = pCMYKColor.Yellow;                                    pConvertInfo.CK = pCMYKColor.Black;                                    foreach (ConvertMappingInfo c in plistMappingInfo)                                    {                                        if (pConvertInfo.CompareColorValue(c, convertToType))                                        {                                            pCMYKColor.Cyan = c.PC;                                            pCMYKColor.Magenta = c.PM;                                            pCMYKColor.Yellow = c.PY;                                            pCMYKColor.Black = c.PK;                                        }                                    }                                }                            }                        }                    }                    pRepresentation.UpdateFeature();                    pFeaCursor.UpdateFeature(pFea);                    //关闭编辑会话                    pWsEdit.StopEditOperation();                    pWsEdit.StopEditing(true);                }            }            catch (Exception ex)            {                WorkspaceAPI.RollbackWorkspaceEdit(pWsEdit);            }            finally            {             }        }