IUniqueValueRenderer in an ArcGIS 10 AddIn

2553
1
09-28-2010 03:11 PM
ScottTansley
New Contributor II
Hi,

I've borrowed the following code from:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000000t8000000

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:

            doc.ActiveView.ContentsChanged();
            doc.UpdateContents();
            doc.ActiveView.Refresh();

Thanks for your help.

Regards,

Scott


---

Code used:


        string _colorName = "Color";

        private void SetUvRenderer(IFeatureClass featureClass)
        {
            ////Code in this method taken and adapted from:
            ////http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000000t8000000

            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);
                }

                count++;
                //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;
            pEnumColors.Reset();
            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;

        }
0 Kudos
1 Reply
ScottTansley
New Contributor II
Hi,

I've sorted this now.  From the OnClick Event I opened a form as a dialog, and in the Form1_Load event I was calling some code that amended some Polygons.  At the end of these changes I was calling the method that was giving me the problems. 

I ended up creating a seperate add-in, and testing the above code in isolation which then worked.  🙂  So it was something to do with my earlier code. 

Now, what I hadn't considered was that the form was running in the same thread as ArcMap, but I'd passed the processing code out to a seperate thread. 

When I moved the above method into a class, on it's own, and then called this from the Form1_Closing event, everything started working again...

So I guess this simply comes down to me being a newbie at threading, and making a classic mistake that I've already learnt from.

Sorry to waste your time folks...

Scott
0 Kudos