Select to view content in your preferred language

Create a UN Association in an Edit Event Handler

203
2
3 weeks ago
EdwardBlair
Frequent Contributor

Hi -

The code below attempts to create an association between two utility network features within a HandleEditCompletedEvent.   As it is, it consistently throws an error when attempting to add the association, line:

_un.AddAssociation(assoc)

The error message states that "This task must be performed within an Edit Operation" - but the AddAssociation *is* performed within an edit operation.

Now, it may be that what I'm trying to do -- perform an edit to create an association within an edit event fired on creation of a feature that would be part of that association -- is just not supported.  But that's not what the error says.

Anyway, any guidance on this would be much appreciated.

Thx,

Ed

------------------------------------------------------------------------

private async void HandleEditCompletedEvent(EditCompletedEventArgs args)
{
    if (args.CompletedType == EditCompletedType.Operation && args.Creates != null)
    {
        string message = string.Empty;
        foreach (var kvp in args.Creates.ToDictionary())
        {
            var oids = string.Join(",", kvp.Value.Select(n => n.ToString()).ToArray());
            if (kvp.Key.Name == "Underground Open Point")
            {
                #region attempt to create association - must run in edit operation
                await QueuedTask.Run(() =>
                {
                    EditOperation editOperation = new EditOperation() { Name = "Create Association" };
                    {
                        QueryFilter qf = new QueryFilter();
                        qf.WhereClause = string.Format("OBJECTID in ({0})", oids);
 
                        MapMember mm = kvp.Key as MapMember;
                        FeatureLayer fl = mm as FeatureLayer;
                        FeatureClass fc = fl.GetTable() as FeatureClass;
                        FeatureClassDefinition fcDef = fc.GetDefinition();
 
                        using (RowCursor cur = fc.Search(qf))
                        {
                            if (cur.MoveNext())
                            {
                                Row row = cur.Current;
 
                                // Search for an underground transformer
                                MapPoint mp = row[fcDef.GetShapeField()] as MapPoint;
                                SpatialQueryFilter sqf = new SpatialQueryFilter();
                                sqf.FilterGeometry = GeometryEngine.Instance.Buffer(mp, 20.0);
                                sqf.WhereClause = string.Format("ASSETGROUP = {0}", 220);
                                sqf.SpatialRelationship = SpatialRelationship.Intersects;
 
                                using (RowCursor deviceCur = fc.Search(sqf, false))
                                {
                                    if (deviceCur.MoveNext())
                                    {
                                        using (Row deviceRow = deviceCur.Current)
                                        {
                                            Element elbowElement = _un.CreateElement(row);
                                            Element xfrElement = _un.CreateElement(deviceRow);
                                            Association assoc = new Association(AssociationType.Containment, elbowElement, xfrElement);
 
// ERROR HERE WITH MESSAGE THAT THIS MUST BE DONE WITHIN
// AN EDIT OPERATION
                                            _un.AddAssociation(assoc);
                                        }
                                    }
                                }
                            }
                        }
                        
                    }
 
                    try
                    {
                        var editResult = editOperation.ExecuteAsync();
                        if (editResult.IsCompletedSuccessfully == false) message = editOperation.ErrorMessage;
                        //success = true;
                    }
                    catch (Exception geoex)
                    {
                        message = geoex.Message;
                    }
                    // TODO
                });
                #endregion
            }
        }
 
    }
 
    bool testHere = true;
}

 

0 Kudos
2 Replies
NarelleChedzey
Esri Contributor

Hello, 

You cannot add a new edit operation within an EditCompleted event.   To add your associations you should subscribe to the RowCreatedEvent for the datasets you are interested in and place your code there.  Make sure to use the EditOperation that is passed in the RowChangedEventArgs of the row created event handler.  This ensures that the association created is part of the same ongoing edit transaction as the element creation and will be part of the same undo / redo item. 

Here is some additional information on the edit events and row events specifically -  https://github.com/Esri/arcgis-pro-sdk/wiki/ProConcepts-Editing#edit-events and  https://github.com/Esri/arcgis-pro-sdk/wiki/ProConcepts-Editing#row-events

 

Narelle

 

0 Kudos
EdwardBlair
Frequent Contributor

Hi Narelle -

Thanks for the feedback!  I'll definitely check out the info provided.

Ed

0 Kudos