Custom Button fires, but Edits not always taking

175
0
06-19-2019 08:13 AM
BrianBulla
Occasional Contributor III

Hi,

I have a button that edits selected features in the map.  The code looks for selected features in specific layers, and then converts the values of certain fields from Imperial to Metric.  It's fairly simple, but what I am noticing is that sometimes the edits do not take.  I have a messagebox on editOperation.Execute() == true that displays, but even when it gets displayed sometimes the features are not actually updated.  The user then just clicks the tool button again, with the same selection, and then it all goes through ok 99% of the time.

Any ideas as to why it sometimes doesn't work??  My code is below....

This isn't the only tool that I have this problem with.  I use the same methodology for my editing in all of them.  If you don't see any issues with my code, could it possibly be a network/system issue??  We are using a versioned Enterprise Geodatabase.

Thanks,

namespace CalcMetric_ArcPro
{
    internal class btnCalcMetric : Button
    {
        protected override void OnClick()
        {
            if (MessageBox.Show("This will convert INVERT, LENGTH and ELEVATION of selected features to metric.  Are you sure you want to do this?", "Convert to metric?", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.No)
                return;

            int counter = 0;

            try
            {
                QueuedTask.Run(() =>
                {
                    Map map = MapView.Active.Map;

                    var editOperation = new ArcGIS.Desktop.Editing.EditOperation();
                    editOperation.Name = "Convert to Metric";
                    editOperation.EditOperationType = EditOperationType.Long;

                    foreach (Layer layer in map.GetLayersAsFlattenedList().OfType<FeatureLayer>())
                    {
                        var currentLayer = map.FindLayers(layer.Name).FirstOrDefault() as BasicFeatureLayer;
                        switch (currentLayer.Name)
                        {
                            case "GISWRKS1.WORKS.GravitySewer":
                                if (currentLayer.SelectionCount > 0)
                                {
                                    var selection = currentLayer.GetSelection();
                                    IReadOnlyList<long> selectedOIDs = selection.GetObjectIDs();

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

                                    foreach (var oid in selectedOIDs)
                                    {
                                        insp.Load(currentLayer, oid);
                                        if (!DBNull.Value.Equals(insp["LENGTH"]) && !DBNull.Value.Equals(insp["UPSTREAMINVERT"]) && !DBNull.Value.Equals(insp["DOWNSTREAMINVERT"]))
                                        {
                                            insp["UPSTREAMINVERT"] = Math.Round(Convert.ToDouble(insp["UPSTREAMINVERT"]) * .3048, 2);
                                            insp["DOWNSTREAMINVERT"] = Math.Round(Convert.ToDouble(insp["DOWNSTREAMINVERT"]) * .3048, 2);
                                            insp["LENGTH"] = Math.Round(Convert.ToDouble(insp["LENGTH"]) * .3048, 2);
                                            editOperation.Modify(insp);
                                            counter++;
                                        }  
                                        else
                                            MessageBox.Show("Sanitary GravityMain " + insp["FACILITYID"].ToString() + " has a NULL value for LENGTH/UPSTREAMID/DOWNSTREAMID.", "NULL value", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                                    }
                                }
                                break;

                            case "GISWRKS1.WORKS.ssGravityMain":
                                if (currentLayer.SelectionCount > 0)
                                {
                                    var selection = currentLayer.GetSelection();
                                    IReadOnlyList<long> selectedOIDs = selection.GetObjectIDs();

                                    var insp = new ArcGIS.Desktop.Editing.Attributes.Inspector();
                                        
                                    foreach (var oid in selectedOIDs)
                                    {
                                        insp.Load(currentLayer, oid);
                                        if (!DBNull.Value.Equals(insp["LENGTH"]) && !DBNull.Value.Equals(insp["UPSTREAMINVERT"]) && !DBNull.Value.Equals(insp["DOWNSTREAMINVERT"]))
                                        {
                                            insp["UPSTREAMINVERT"] = Math.Round(Convert.ToDouble(insp["UPSTREAMINVERT"]) * .3048, 2);
                                            insp["DOWNSTREAMINVERT"] = Math.Round(Convert.ToDouble(insp["DOWNSTREAMINVERT"]) * .3048, 2);
                                            insp["LENGTH"] = Math.Round(Convert.ToDouble(insp["LENGTH"]) * .3048, 2);
                                            editOperation.Modify(insp);
                                            counter++;
                                        }
                                        else
                                            MessageBox.Show("Storm GravityMain " + insp["FACILITYID"].ToString() + " has a NULL value for LENGTH/UPSTREAMID/DOWNSTREAMID.", "NULL value", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                                    }                                    
                                }
                                break;

                            case "GISWRKS1.WORKS.SAN_Manhole":
                                if (currentLayer.SelectionCount > 0)
                                {
                                    var selection = currentLayer.GetSelection();
                                    IReadOnlyList<long> selectedOIDs = selection.GetObjectIDs();

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

                                    foreach (var oid in selectedOIDs)
                                    {
                                        insp.Load(currentLayer, oid);
                                        if (!DBNull.Value.Equals(insp["ELEVATION"]))
                                        {
                                            insp["ELEVATION"] = Math.Round(Convert.ToDouble(insp["ELEVATION"]) * .3048, 2);
                                            editOperation.Modify(insp);
                                            counter++;
                                        }
                                        else
                                            MessageBox.Show("Sanitary Manhole " + insp["FACILITYID"].ToString() + " has a NULL value for ELEVATION.", "NULL value", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                                    }
                                }
                                break;

                            case "GISWRKS1.WORKS.ssMaintenanceHole":
                                if (currentLayer.SelectionCount > 0)
                                {
                                    var selection = currentLayer.GetSelection();
                                    IReadOnlyList<long> selectedOIDs = selection.GetObjectIDs();

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

                                    foreach (var oid in selectedOIDs)
                                    {
                                        insp.Load(currentLayer, oid);
                                        if (!DBNull.Value.Equals(insp["ELEVATION"]))
                                        {
                                            insp["ELEVATION"] = Math.Round(Convert.ToDouble(insp["ELEVATION"]) * .3048, 2);
                                            editOperation.Modify(insp);
                                            counter++;
                                        }
                                        else
                                            MessageBox.Show("Storm Manhole " + insp["FACILITYID"].ToString() + " has a NULL value for ELEVATION.", "NULL value", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                                    }
                                }
                                break;

                            case "GISWRKS1.WORKS.ssCatchBasin":
                                if (currentLayer.SelectionCount > 0)
                                {
                                    var selection = currentLayer.GetSelection();
                                    IReadOnlyList<long> selectedOIDs = selection.GetObjectIDs();

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

                                    foreach (var oid in selectedOIDs)
                                    {
                                        insp.Load(currentLayer, oid);
                                        if (!DBNull.Value.Equals(insp["ELEVATION"]))
                                        {
                                            insp["ELEVATION"] = Math.Round(Convert.ToDouble(insp["ELEVATION"]) * .3048, 2);
                                            editOperation.Modify(insp);
                                            counter++;
                                        }
                                        else
                                            MessageBox.Show("Catchbasin " + insp["FACILITYID"].ToString() + " has a NULL value for ELEVATION.", "NULL value", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
                                    }
                                }
                                break;
                        }
                    }

                    //show message if editing was successfull
                    if (editOperation.Execute() == true)
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(counter.ToString() + " features updated.", "Convert to Metric Finished");
                    else
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("No features were updated.", "Convert to Metric Finished");
                });
            }

            catch (Exception ex)
            {
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(ex.ToString());
            }            

        }
    }
}
0 Kudos
0 Replies