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