AnsweredAssumed Answered

ChainedEditing not working

Question asked by bulla_b on Jul 23, 2019
Latest reply on Jul 24, 2019 by bulla_b



I'm trying to figure out where I am going wrong in my edit sequence.  What I am doing is selecting newly created features that have no attribute information entered.  The user then runs the tool and an attribute get populated.  The field is a numeric field and there is some code that looks for the last entered number (ie. the current highest number) and increases it by 1 for the new feature.


So, for example the user adds 3 new hydrants and selects them all, then runs the tool.  If there are already 10 hydrants the 3 new hydrants should get the numbers 11, 12, and 13 added into the SEQUENCENO attribute for the new features.


My problem is that the 11 and 12 get added, but then third feature gets 12 added again.  If I have 10 features selected, the same sort of pattern happens.  The 11 and 12 get added, but then the remaining 8 would also get 12. 


I only want one item to show on the Undo stack, and each edit will affect the next, so that is why I am using ChainedOperation, but I just don't think I am implementing it correctly.


Below is the code where it loops though all the selected features and assigns the next number.


If you can offer some advice on how to get this working I'd appreciate it.  



                    //Create the MAIN edit operation...this will show up on the Undo Stack
                    var editOperation = new ArcGIS.Desktop.Editing.EditOperation();
                    editOperation.Name = "Create new IDs";
                    editOperation.EditOperationType = ArcGIS.Desktop.Editing.EditOperationType.Long;

                    ArcGIS.Desktop.Editing.EditOperation chainedOp = null;

                    //create the Inspector Object
                    var inspector = new ArcGIS.Desktop.Editing.Attributes.Inspector();

                    //create a flag to track the 'main' operation
                    bool isMainOperation = true;

                            //Loop through each selected OBJECTID.
                            foreach (var oid in selectedOIDs)
                                inspector.Load(currentLayer, oid);
                                    //Need to pass a FeatureCLass to the GetNextFaciltyID procedure
                                    FeatureLayer currFLayer = currentLayer as FeatureLayer;
                                    FeatureClass currFClass = currFLayer.GetFeatureClass();

                                    //This is where the sequence number will get incremented each time (in the GetNextFacilityID)
                                    inspector["FACILITYID"] = GetNextFacilityID(currFClass, gridNumber);
                                    inspector["SEQUENCENO"] = sequenceNumber;

                                    if (isMainOperation)
                                        bool result = editOperation.Execute();

                                        //change the flag so we use the ChainedOp next
                                        isMainOperation = false;
                                        //only create the ChainedOp once
                                        if (chainedOp == null)
                                            chainedOp = editOperation.CreateChainedOperation();

                                        //queue up a Modify
                                    //Increase the feature count for the final tally

                            }   //go to next selected feature in current layer

if ((chainedOp != null) && !chainedOp.IsEmpty)
                        bool chainedResult = chainedOp.Execute();



And this is the section of code that determines the next available number.  I think the logic here is fine, but because my edits aren't really getting applied (??) properly though the chainedOp, its not getting the right next number.



        //This will return the next FacilityID
        public string GetNextFacilityID(FeatureClass fClass, string gridNum)
                QueryFilter queryFilter = new QueryFilter();
                queryFilter.WhereClause = "GRIDNO ='" + gridNum + "'";
                queryFilter.SubFields = "SEQUENCENO";

                //create an array to hold all of the SEQUENCENO;  give it's first member the value zero
                ArrayList idList = new ArrayList();

                RowCursor rCursor = fClass.Search(queryFilter);
                Feature feature;

                while (rCursor.MoveNext())
                    using (feature = (Feature)rCursor.Current)
                        int sequencePosition = feature.FindField("SEQUENCENO");

                //sort the list so we can get the highest value of SEQUENCENO

                //the next # we want is the current highest number + 1
                int number = (int)idList[idList.Count - 1] + 1;

                sequenceNumber = number;

                //convert it to a string so we can add the zero placeholders to the front of needs to be 4 digits.
                string finalNum = number.ToString();

                switch (finalNum.Length)
                    case 1:
                        finalNum = "000" + finalNum;

                    case 2:
                        finalNum = "00" + finalNum;

                    case 3:
                        finalNum = "0" + finalNum;

                //return the full facility id, with the STRUCTURETYPE, GRIDNO, and SEQUENCENO
                return structureType + "-" + gridNumber + "-" + finalNum;
            catch (Exception ex)
                return null;