ArcEnging 导入shp文件后自定义图层中各要素的颜色
经过前面一段时间狂乱的baidu&google,目前ArcEngine开的项目算是完成了基本的外观,这次解决的问题是从CAD导成shp文件后,先前在CAD中定义的颜色也是可以读取到的,只不过它的Color值是采用的 ACI 规范,需要转换成RGB我们才方便使用。当然主要的问题是MapControl导入shp文件时,它给每个图层中要素的颜色是随机的,不是原图中所定义的颜色,所以我们需要这样在google中搜索”ArcEngine 修改 图层颜色”,”ArcEngine 自定义要素颜色”,又是一阵狂乱的搜索,当然也能找到一些信息,但最后还是在看那个E文的MSDN帮助时找到了解决办法。上面直接有一例子,这里做了一些修改,贴出来:
public static void DefineFeatureColor(IGeoFeatureLayer pGeoFeatureLayer, string fieldName) { //Make the renderer. IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRendererClass(); //These properties should be set prior to adding values. pUniqueValueRenderer.FieldCount = 1; pUniqueValueRenderer.set_Field(0, fieldName); IDisplayTable pDisplayTable = pGeoFeatureLayer as IDisplayTable; IFeatureCursor pFeatureCursor = pDisplayTable.SearchDisplayTable(null, false) as IFeatureCursor; IFeature pFeature = pFeatureCursor.NextFeature(); bool ValFound; int fieldIndex; IFields pFields = pFeatureCursor.Fields; fieldIndex = pFields.FindField(fieldName); while (pFeature != null) { string classValue = pFeature.get_Value(fieldIndex).ToString(); IColor color = ColorUtilities.GetColorByACI(Int16.Parse(classValue)); IRgbColor rgb = color as IRgbColor; //System.Console.WriteLine("KVL:" + classValue+"-->RGB"+ rgb.Red + "," + rgb.Green + "," + rgb.Blue); ISymbol pClassSymbol = getSymbolByShapType(pGeoFeatureLayer.FeatureClass.ShapeType, color); //Test to see if this value was added //to the renderer. If not, add it. ValFound = false; for (int i = 0; i <= pUniqueValueRenderer.ValueCount - 1; i++) { if (pUniqueValueRenderer.get_Value(i) == classValue) { ValFound = true; break; //Exit the loop if the value was found. } } //If the value was not found, it is new and it will be added. if (ValFound == false) { pUniqueValueRenderer.AddValue(classValue, fieldName, pClassSymbol as ISymbol); pUniqueValueRenderer.set_Label(classValue, classValue); pUniqueValueRenderer.set_Symbol(classValue, pClassSymbol as ISymbol); } pFeature = pFeatureCursor.NextFeature(); } //'** If you didn't use a predefined color ramp //'** in a style, use "Custom" here. Otherwise, //'** use the name of the color ramp you selected. pUniqueValueRenderer.ColorScheme = "Custom"; ITable pTable = pDisplayTable as ITable; bool isString = pTable.Fields.get_Field(fieldIndex).Type == esriFieldType.esriFieldTypeString; pUniqueValueRenderer.set_FieldType(0, isString); pGeoFeatureLayer.Renderer = pUniqueValueRenderer as IFeatureRenderer; //This makes the layer properties symbology tab //show the correct interface. IUID pUID = new UIDClass(); pUID.Value = "{683C994E-A17B-11D1-8816-080009EC732A}"; pGeoFeatureLayer.RendererPropertyPageClassID = pUID as UIDClass; }
上面的代码大部分来自帮助文档中的例子,做了一些逻辑上的修改,就是把原例子中的Random颜色换成读取要素Color转成RGB再渲染。参数fieldName其实可以不用要,我这里就是”Color”这个字段名。
转载自:https://blog.csdn.net/Rocye/article/details/84184026