Select to view content in your preferred language

Code only works in debug mode

581
1
Jump to solution
06-15-2018 05:43 AM
BrianBulla
Honored Contributor

Hi,

I have some code I'm trying to troubleshoot.  It does some editing of attributes to a point and the intersecting line feature.  A user can select multiple points, and then click the tool to run the code on each selected point.  I get no error messages, but the results are not as expected when selecting more than one feature.

The weird thing is that if I step through the code, line by line, everything works fine.  The only time I have problems is when running the code in real-time.  For some reason, only the first feature selected gets updated, and all the other features keep their original attributes.

I will post the code below, but here is the main logic behind it:

1.  select one or more hydrant features and click on the tool

2.  the tool work through each selected hydrant one at a time

3.  it finds the intersecting water service connected to the hydrant and updates the SUBTYPE attribute of the water service

4.  it then finds the intersecting water main of the water service, and updates the WATERMAINSIZE and WATERMAINID on the hydrant feature

5.  goes to the next selected hydrant and repeats steps 2 through 4

If anyone has any ideas as to why this code will not work in real-time, please let me know.

Thanks!

internal class FittingsButton : Button
    {

        public string GetIntersectingFeature(MapPoint point, FeatureLayer intersectLayer, string fieldName)
        {
            try
            {
                //ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(point.X.ToString() + " : " + point.Y.ToString());

                //Create a Spatial Query using the supplied point, layer and fieldname
                SpatialQueryFilter spatialFilter = new SpatialQueryFilter();
                spatialFilter.FilterGeometry = point;
                spatialFilter.SpatialRelationship = SpatialRelationship.Intersects;
                spatialFilter.SubFields = fieldName;

                //Gets the intersecting feature
                string fieldValue = null;
                RowCursor featureCursor = intersectLayer.Search(spatialFilter);
                Feature feature;

                //Gets the value using the fieldName
                while (featureCursor.MoveNext())
                {
                    using (feature = (Feature)featureCursor.Current)
                    {
                        int fieldPosition = feature.FindField(fieldName);
                        if (feature[fieldPosition] != null)
                            fieldValue = feature[fieldPosition].ToString();
                        else
                            fieldValue = null;
                    }
                }

                return fieldValue;
            }

            catch (Exception ex)
            {
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("An error occured while finding the intersecting feature." + "\n" + "\n" + ex.ToString(), "GetIntersecingFeature", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                return null;
            }
        }

        protected async override void OnClick()
        {
            try
            {
                await QueuedTask.Run(() =>
                {
                    //Start the edit operation, and place on the UNDO stack
                    var editOperation = new ArcGIS.Desktop.Editing.EditOperation();
                    editOperation.Name = "Update Fittings Diameters on Selected Features";
                    editOperation.EditOperationType = ArcGIS.Desktop.Editing.EditOperationType.Long;

                    Map map = MapView.Active.Map;

                    FeatureLayer waterMainLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WaterMain").FirstOrDefault();
                    FeatureLayer waterServiceLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WaterService").FirstOrDefault();
                    FeatureLayer hydrantLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_Hydrant").FirstOrDefault();
                    FeatureLayer controlValveLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_ControlValve").FirstOrDefault();
                    FeatureLayer waterFittingsLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_Fitting").FirstOrDefault();
                    FeatureLayer reliefValveLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_ReliefValve").FirstOrDefault();
                    FeatureLayer pressureZoneLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_PressureZone").FirstOrDefault();

                    #region Check for hydrants; Update the 

                    if (hydrantLayer != null)
                    {
                        if (hydrantLayer.SelectionCount > 0)
                        {
                            var hydrantSelection = hydrantLayer.GetSelection();
                            IReadOnlyList<long> selectedOIDs = hydrantSelection.GetObjectIDs();

                            foreach(var oid in selectedOIDs)
                            {
                                var insp = new ArcGIS.Desktop.Editing.Attributes.Inspector();
                                insp.Load(hydrantLayer, oid);

                                var thePoint = insp.Shape;
                                MapPoint hydrantPoint = (MapPoint)insp.Shape;

                                //Update the subtype of the water service to 2 - HydrantLateral
                                var wsInspector = new ArcGIS.Desktop.Editing.Attributes.Inspector();
                                wsInspector.Load(waterServiceLayer, Convert.ToInt64(GetIntersectingFeature(hydrantPoint, waterServiceLayer, "OBJECTID")));
                                wsInspector["SubTypeCD"] = 2;
                                editOperation.Modify(wsInspector);

                                //Now get the watermain that intersect the From point of the HydrantLateral.
                                //Get the To and From point of the currently selected HydrantLateral
                                var pointCollection = ((Multipart)wsInspector.Shape).Points;
                                MapPoint fromPoint = pointCollection[0];
                                MapPoint toPoint = pointCollection[pointCollection.Count - 1];

                                string watermainID = GetIntersectingFeature(fromPoint, waterMainLayer, "OBJECTID");

                                //The HydrantLateral could be digitized backwards, so use the toPoint if the fromPoint returns NULL
                                if (watermainID == null)
                                    watermainID = GetIntersectingFeature(toPoint, waterMainLayer, "OBJECTID");

                                if (watermainID != null)
                                {
                                    var wmInspector = new ArcGIS.Desktop.Editing.Attributes.Inspector();
                                    wmInspector.Load(waterMainLayer, Convert.ToInt64(watermainID));
                                    insp["WaterMainSize"] = wmInspector["Diameter"];
                                    insp["WATERMAINID"] = wmInspector["FACILITYID"];
                                }

                                editOperation.Modify(insp);
                            }

                        }
                    }

                    #endregion

                    #region Check for Control Valves

                    #endregion


                    editOperation.Execute();
                });
            }

            catch
            {

            }

            finally
            {

            }
        }
    }
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BrianBulla
Honored Contributor

OK, I think I have figured it out.  I have moved my

var insp = new ArcGIS.Desktop.Editing.Attributes.Inspector();

to before the foreach loop, and now all is working as expected.  I'm still not really sure why it worked in debug mode before, but it seems to be working now in real-time and debug mode now.

Thanks!!

View solution in original post

0 Kudos
1 Reply
BrianBulla
Honored Contributor

OK, I think I have figured it out.  I have moved my

var insp = new ArcGIS.Desktop.Editing.Attributes.Inspector();

to before the foreach loop, and now all is working as expected.  I'm still not really sure why it worked in debug mode before, but it seems to be working now in real-time and debug mode now.

Thanks!!

0 Kudos