Working with ArcGIS Desktop 10.5.1 and .NET SDK. Building ArcMap add-in extension. I'm trying to cause a newly created feature to be saved programatically as the last operation of the OnCreateFeature edit event. The problem I'm having is that when the save edit command completes, another feature is created and the OnCreateFeature event is fired again. I do not understand what is causing another feature to be created. Here's the important parts of my OnCreateFeature event handler:
private void m_editEvents_OnCreateFeature(ESRI.ArcGIS.Geodatabase.IObject obj)
{
IFeature pFeature = obj as IFeature;
// Set feature case number and other stuff...
// Save the newly created feature so that the feature and case number is commited
UID pUID = new UID();
pUID.Value = "{59D2AFD2-9EA2-11D1-9165-0080C718DF97}"; // esriEditor.SaveEditsCommand
ICommandItem pCmdItem = ArcMap.Application.Document.CommandBars.Find(pUID, false, false);
pCmdItem.Execute();
}
I know that the SaveEditsCommand internally executes StopEditing and StartEditing. However, I do not see why another feature is created and goes into endless loop (create, save, create, save, on and on).
If there is a better way to accomplish saving automatically on creation, I would appreciate greatly finding out how to do it.
The reason I need this to happen is to commit the feature with its calculated case number to the geodatabase instantly so that other concurrent editors can get the next available sequential number from the feature class itself.
Solved! Go to Solution.
Yes, the save edits command will fire the events again if its called from within one.
Use the workspaceedit methods to do the save.
private void OnCreateFeature(IObject obj)
{
//do something and save?
var pUID = new UID();
pUID.Value = "{59D2AFD2-9EA2-11D1-9165-0080C718DF97}"; // esriEditor.SaveEditsCommand
var pCmdItem = ArcMap.Application.Document.CommandBars.Find(pUID, false, false);
//pCmdItem.Execute();
var ws = ArcMap.Editor.EditWorkspace as IWorkspaceEdit;
ws.StopEditing(true);
ws.StartEditing(true);
}
If you're after sequential numbers from a DBMS you can also look into a sequence generator, which will be more efficient than saving edits and subsequent table scans to find the highest number.
Yes, the save edits command will fire the events again if its called from within one.
Use the workspaceedit methods to do the save.
private void OnCreateFeature(IObject obj)
{
//do something and save?
var pUID = new UID();
pUID.Value = "{59D2AFD2-9EA2-11D1-9165-0080C718DF97}"; // esriEditor.SaveEditsCommand
var pCmdItem = ArcMap.Application.Document.CommandBars.Find(pUID, false, false);
//pCmdItem.Execute();
var ws = ArcMap.Editor.EditWorkspace as IWorkspaceEdit;
ws.StopEditing(true);
ws.StartEditing(true);
}
If you're after sequential numbers from a DBMS you can also look into a sequence generator, which will be more efficient than saving edits and subsequent table scans to find the highest number.
Your solution works perfectly.
I can't utilize an identity field in the database to increment the numbers because the case number is a text concatenation of the year and a sequence number which starts over at the beginning every year (like 18-006).
Thanks for your help Sean!