This document is archived and information here might be outdated.  Recommended version.


Creating features in the editor (ArcObjects .NET 10.4 SDK)

Creating features in the editor


Summary
This topic outlines the general process for creating features in the editor. This topic also describes a number of ways to use existing features or geometries to construct new features in the editor.

In this topic


Start an edit operation

Each edit made in the editor should be completed inside an edit operation. This ensures that transactions made against the data and the operation stack are synchronized and maintained. The following code shows how to start an edit operation in the editor:
[C#]
UID editorUID=new UIDClass();
editorUID.Value="esriEditor.Editor";
IEditor3 editor=ArcMap.Application.FindExtensionByCLSID(editorUID)as IEditor3;

editor.StartOperation();
[VB.NET]
Dim editorUID As UID=New UIDClass()
editorUID.Value="esriEditor.Editor"
Dim editor As IEditor3=TryCast(ArcMap.Application.FindExtensionByCLSID(editorUID), IEditor3)
editor.StartOperation()
For more information on edit operations, see Managing edit sessions and edit operations.

Create the feature in the geodatabase

The editor uses feature templates to determine where (in which feature class) the new feature will be created. Feature templates (EditTemplates) define, among other properties, the target layer where new features will be created. The following code shows how to create a feature in the feature template's feature class:
[C#]
//Get the editor's current template.
IEditTemplate editTemplate=editor.CurrentTemplate;
if (editTemplate == null)
    return ;

//Create a new (empty) feature in the template's target layer.
IFeature newFeature=((IFeatureLayer)editTemplate.Layer).FeatureClass.CreateFeature
    ();
[VB.NET]
'Get the editor's current template.
Dim editTemplate As IEditTemplate=editor.CurrentTemplate
If editTemplate Is Nothing Then
    Return
End If

'Create a new (empty) feature in the template's target layer.
Dim newFeature As IFeature=DirectCast(editTemplate.Layer, IFeatureLayer).
FeatureClass.CreateFeature()

Define feature geometry

Defining the new feature's geometry is essential. How this geometry is defined will vary, in some cases requiring the user to define the geometry on the map, to use existing features' geometries, or to enter a set of parameters to define the location and shape of the new feature.

Digitize on the map

Defining new features by digitizing on the map is the most common way to define a new feature's geometry. In the editor, the edit sketch is the preferred way to capture and store this geometry until the user is finished.
For more information on the edit sketch, see Working with the edit sketch.

Use existing features

Sometimes it is most efficient to define new feature geometry by using existing features. The capabilities in the Geometry assembly (particularly the methods on ITopologicalOperator) are typically used to complete operations to produce new feature geometry. For more information on how you can apply these operations, see How to create a union of several polygons.
In other instances, you may be generating different types of geometries, such as using a closed set of line features to create a new polygon. This is best accomplished using the FeatureConstruction class. The FeatureConstruction class provides a number of methods that allow you to construct higher level features using a set of input features or geometries along with existing features. These methods produce topologically correct results.

Simplify feature geometry

Once the geometry has been defined, it is imperative to ensure that the geometry is topologically correct ("simple") before it is committed and stored. ArcGIS clients assume that stored features are simple; working with non-simple features can result in unexpected behavior.
ArcGIS has multiple methods to simplify a feature's geometry. For more information, see Simplifying a feature geometry.

Assign simple geometry to the new feature

Once the geometry is simple, you need to assign the geometry to the newly created feature. This is generally accomplished by setting the feature's shape (IFeature.Shape) to a reference of the simple geometry. See the following code example:
[C#]
//Assign the simplified geometry to the new feature.
newFeature.Shape=simpleGeom;
[VB.NET]
'Assign the simplified geometry to the new feature.
newFeature.Shape=simpleGeom

Apply attributes

In addition to defining the new feature's geometry, it is also important to specify attributes for the newly created feature. Specifying attributes is primarily accomplished through the use of feature templates, which extend the default values defined for the target feature class where the new feature will be stored. For more information on applying templates to newly created features, see Using feature templates.
See the following code example:
[C#]
//Apply the template's default attribute values.
editTemplate.SetDefaultValues(newFeature);
[VB.NET]
'Apply the template's default attribute values.
editTemplate.SetDefaultValues(newFeature)

Store the new feature

Once the new feature's geometry and attributes are defined, the new feature needs to be committed to the feature class being edited. This is usually accomplished by calling Store if the new feature was created by calling IFeatureClass.CreateFeature, or IFeatureCursor.Flush if it was created using an insert cursor.

Stop the edit operation

Once the new feature is committed to the database, close the edit operation by calling StopOperation. This commits the transaction and adds it to the application's operation stack.

The following code example commits the new feature and stops the edit operation:[C#]

newFeature.Store();

editor.StopOperation("Add new");
[VB.NET]
newFeature.Store()

editor.StopOperation("Add new")

Refresh the display

Once the operation has been completed, ensure that the display matches the current state, which usually requires invalidating the display to account for the newly created feature.
For more information on effectively refreshing the display after an edit, see Invalidating the display.






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):

Development licensing Deployment licensing
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Basic ArcGIS for Desktop Basic