IUniqueValueRenderer in an ArcGIS 10 AddIn

Discussion created by stansley on Sep 28, 2010
Latest reply on Sep 29, 2010 by stansley

I've borrowed the following code from:

In the Table of Contents, I can see the various Unique Values listed, but there is no symbology next to them.  When I enter the layer's properites, it is still showing as selected as a single class.  I've previously used the equivalent code sample in VBA and it works fine in 9.3.1, but I'm trying to complete my move over to C#.

My only wonder is if this is because of using an AddIn, but that doesn't feel right.  Any thoughts, sanity check would be most welcome.

Oh, and after executing this method, I call the following:


Thanks for your help.




Code used:

        string _colorName = "Color";

        private void SetUvRenderer(IFeatureClass featureClass)
            ////Code in this method taken and adapted from:

            int count = 0;

            IRandomColorRamp pRandomColorRamp = new RandomColorRampClass();
            //Create the color ramp for the symbols in the renderer.
            pRandomColorRamp.MinSaturation = 20;
            pRandomColorRamp.MaxSaturation = 40;
            pRandomColorRamp.MinValue = 85;
            pRandomColorRamp.MaxValue = 100;
            pRandomColorRamp.StartHue = 76;
            pRandomColorRamp.EndHue = 188;
            pRandomColorRamp.UseSeed = true;
            pRandomColorRamp.Seed = 43;

            //Create the renderer.
            IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRendererClass();

            ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
            pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
            pSimpleFillSymbol.Outline.Width = 0.4;

            //These properties should be set prior to adding values.
            pUniqueValueRenderer.FieldCount = 1;
            pUniqueValueRenderer.set_Field(0, _colorName);
            pUniqueValueRenderer.DefaultSymbol = pSimpleFillSymbol as ISymbol;
            pUniqueValueRenderer.UseDefaultSymbol = true;

            //Get the selected layer, create a display table and then a cursor
            IMxDocument doc = ArcMap.Document;
            IContentsView contentsView = doc.CurrentContentsView;
            IGeoFeatureLayer pGeoFeatureLayer = doc.CurrentContentsView.SelectedItem as IGeoFeatureLayer;

            //Set a display table and featurecursor, so all features can be iterated and values added.
            IDisplayTable pDisplayTable = pGeoFeatureLayer as IDisplayTable;
            IFeatureCursor pFeatureCursor = pDisplayTable.SearchDisplayTable(null, false) as IFeatureCursor;
            IFeature pFeature = pFeatureCursor.NextFeature();

            bool ValFound;

            IFields pFields = pFeatureCursor.Fields;
            int fieldIndex = pFields.FindField(_colorName);
            while (pFeature != null)
                ISimpleFillSymbol pClassSymbol = new SimpleFillSymbolClass();
                pClassSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
                pClassSymbol.Outline.Width = 0.4;

                string classValue;
                classValue = pFeature.get_Value(fieldIndex).ToString();

                //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's new and will be added.
                if (ValFound == false)
                    pUniqueValueRenderer.AddValue(classValue, _colorName, pClassSymbol as ISymbol);
                    pUniqueValueRenderer.set_Label(classValue, classValue);
                    pUniqueValueRenderer.set_Symbol(classValue, pClassSymbol as ISymbol);

                //update the status form to show current position
                FeedbackFeatureCount("Step 3.  Rendering polygon " + count.ToString() + " of " + featureClass.FeatureCount(null).ToString());

                pFeature = pFeatureCursor.NextFeature();
            //Since the number of unique values is known, the color ramp can be sized and the colors assigned.
            pRandomColorRamp.Size = pUniqueValueRenderer.ValueCount;
            bool bOK;
            pRandomColorRamp.CreateRamp(out bOK);

            IEnumColors pEnumColors = pRandomColorRamp.Colors;
            for (int j = 0; j <= pUniqueValueRenderer.ValueCount - 1; j++)
                string xv;
                xv = pUniqueValueRenderer.get_Value(j);
                if (xv != "")
                    ISimpleFillSymbol pSimpleFillColor = pUniqueValueRenderer.get_Symbol(xv) as ISimpleFillSymbol;
                    pSimpleFillColor.Style = esriSimpleFillStyle.esriSFSSolid;

                    pUniqueValueRenderer.set_Symbol(xv, pSimpleFillColor as ISymbol);

            //'** 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.
            IRendererPropertyPage hx = new UniqueValuePropertyPageClass();
            pGeoFeatureLayer.RendererPropertyPageClassID = hx.ClassID;

            pGeoFeatureLayer.DisplayField = _colorName;