ArcGIS SDK  

Extending ArcObjects

Construct Point Edit Task Example

In this section:

  1. The case for a construct point edit task
  2. Creating an edit task
  3. Creating the ConstructPointTask

Construct Point Edit Task Example

Example Code Click here

Description This project provides a custom edit task, an edit task that can be used to create new point features at the end of a polyline edit sketch using any of the available sketch tools, without having to first construct a temporary geometry to help locate the new point feature. The task will appear in the Tasks combo box on the Editor toolbar.

Design Coclass ConstructPointTask is a subtype of the EditTask abstract class and also sinks the IEditEvents interface.

License required ArcEditor or above

Libraries Carto, Display, Editor, Geodatabase, and Geometry

Languages Visual Basic

Categories ESRI Edit Tasks

Interfaces IEditTask and IEditEvents

How to use

    1. Register the ConstructPointTaskVB.dll and double-click the ConstructPointVB.reg file to register to component categories.
    2. Open ArcMap, add data containing polylines, polygons, and at least one point layer. The data must be editable.
    3. Start an edit session.
    4. Choose the Create point at end of sketch edit task (which will be listed under Other Tasks), and make sure the target layer contains points.
    5. Using any of the edit tools, create a new edit sketch that terminates at the location you want to create your new point feature. For example, you could edit a polyline layer and use the Direction and Length commands from the Edit Sketch context menu to create a point at a certain distance and direction from an existing point.
    6. Using any of the edit tools, create a new polyline edit sketch which terminates at the location you want to create your new point feature. For example, you could edit a polyline layer and use the Direction and Length commands from the Edit Sketch context menu to create a point at a certain distance and direction from an existing point.
    7. Finish the edit sketch.

      A new point feature will be added to your target layer at the last vertex of the edit sketch.


The case for a construct point edit task

You want to be able to create new point features at precise locations relative to existing features. For example, you want to add a new Pole feature 145 feet from an existing pole, at a bearing of 40 degrees. Using the existing tool, you could create a temporary line feature starting at the existing pole and ending at the required location by using the Direction and Length command; then you could create the new Pole point feature at the end of the temporary line and delete the line.

The best solution for this scenario is to create a custom edit task that creates new point features at the end of a polyline edit sketch. When a polyline edit sketch has been completed, a new point feature will be added to the target layer at the location of the end of the edit sketch, and the remaining portion of the edit sketch geometry will be deleted.

A different way to solve this problem may be to create a custom edit tool. However, if you create an edit task, your code can take advantage of all the sketch tools, which already exist to locate the point, lending more flexibility to your solution.

This example demonstrates how to create a custom edit task to create point features at the end of an edit sketch, without needing to create a temporary feature as an interim step.


Creating an edit task

By reviewing the Editor object model diagram, you will see the EditTask abstract class, which implements the IEditTask interface. As mentioned in About Edit Tasks, most edit tasks also listen to events from the Editor.


Creating the ConstructPointTask

To meet the requirements described above, you will create a subtype of the EditTask abstract class by implementing IEditTask, and you will also sink the IEditEvents interface.


Implementing IEditTask

The first member of IEditTask to be called will be Activate, when the task is selected in the editor toolbar. Store a reference to the Editor object, which is passed in to this member; you will need to use the IEditSketch and IEditLayer interfaces of this member.

[Visual Basic 6]
Private m_pEditor As esriEditor.IEditor
Private m_pEditSketch As esriEditor.IEditSketch
Private m_pEditLayers As esriEditor.IEditLayer
  
Private Sub IEditTask_Activate(ByVal Editor As esriEditor.IEditor, ByVal oldTask As esriEditor.IEditTask)
  Set m_pEditor = Editor
  Set m_pEditSketch = m_pEditor 'QI
  Set m_pEditLayers = m_pEditor 'QI
End Sub

When the user finishes an edit sketch with the ConstructPointTask as the active task, the OnFinishSketch method will be called. In this method, you need to retrieve the final point of the sketch geometry and create a new feature. The new feature also needs any default values set, and finally, the map display should be refreshed so the new feature and new selection appear correctly.

  1. Start a new edit operation.
    [Visual Basic 6]
    Private Sub IEditTask_OnFinishSketch()
      Dim bInOperation As Boolean
      m_pEditor.StartOperation
      bInOperation = True
  2. Find the last point in the edit sketch.
    [Visual Basic 6]
      Dim pPoint As esriGeometry.IPoint
      Set pPoint = m_pEditSketch.LastPoint
  3. Create a new feature, and set the default values and subtype of the feature if necessary (not all FeatureClasses support ISubtypes).
    [Visual Basic 6]
      Dim pFeature As esriGeodatabase.IFeature
      Set pFeature = m_pEditLayers.CurrentLayer.FeatureClass.CreateFeature
      
      Dim pRowSubTypes As esriGeodatabase.IRowSubtypes
      Dim pSubtypes As esriGeodatabase.ISubtypes
      If TypeOf m_pEditLayers.CurrentLayer.FeatureClass Is esriGeodatabase.ISubtypes Then
        Set pSubtypes = m_pEditLayers.CurrentLayer.FeatureClass
        Set pRowSubTypes = pFeature
        If pSubtypes.HasSubtype Then
          pRowSubTypes.SubtypeCode = m_pEditLayers.CurrentSubtype
        End If
        pRowSubTypes.InitDefaultValues
      End If
  4. Set the geometry of the new point feature from the sketch LastPoint and store the new feature. At this point you can also stop the edit operation.
    [Visual Basic 6]
      Set pFeature.Shape = pPoint
      pFeature.Store
      m_pEditor.StopOperation "Add Point"
      bInOperation = False
  5. Invalidate the area around the new feature.
    [Visual Basic 6]
      Dim pInvalidArea As esriGeodatabase.IInvalidArea
      Set pInvalidArea = New esriCarto.InvalidArea
      Set pInvalidArea.Display = m_pEditor.Display
      pInvalidArea.Add pFeature
      pInvalidArea.Invalidate esriAllScreenCaches
  6. Refresh map according to old and new selections.
    [Visual Basic 6]
      Dim pActiveView As esriCarto.IActiveView
      Set pActiveView = m_pEditor.Map
      pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
      m_pEditor.Map.ClearSelection 'Clear the selection
      m_pEditor.Map.SelectFeature m_pEditLayers.CurrentLayer, pFeature
      pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
  7. You should add an error handler to OnMouseDown to stop any edit operation if there is a problem during the function.
    [Visual Basic 6]
    Private Sub IEditTask_OnFinishSketch()
      On Error GoTo ErrorHandler:
      
      ...
      
      Exit Sub ' Exit sub to avoid error handler
    ErrorHandler:
      If bInOperation Then m_pEditor.AbortOperation
      MsgBox "Failed to construct new point at end of sketch."
    End Sub

From IEditTask::Name return the string that you would like to appear in the edit tasks combo box to identify the task.

[Visual Basic 6]
Private Property Get IEditTask_Name() As String
  IEditTask_Name = "Create Point at End of Sketch"
End Propert

Implementing IEditEvents

Because this edit task deals strictly with creating new point features, the task should disable the sketch tools whenever the target layer is set to something other than a point feature layer. To do this, you can listen to the OnCurrentLayerChanged event of the IEditEvents interface.

  1. Begin by sinking the default interface of the Editor coclass, IEditEvents, in your class.
    [Visual Basic 6]
    Private WithEvents EditorEvents As esriEditor.Editor
  2. Add code to the IEditTask::Activate method to start listening to the editor events. Add a line to call the OnCurrentLayerChanged method straightaway. You will add this member in the next step.
    [Visual Basic 6]
    Private Sub IEditTask_Activate(ByVal Editor As esriEditor.IEditor, ByVal oldTask As esriEditor.IEditTask)
      Set m_pEditor = Editor
      Set m_pEditSketch = m_pEditor ' QI
      Set m_pEditLayers = m_pEditor ' QI
      
      Set EditorEvents = m_pEditor
      ' Call OnCurrentLayerChanged to see if sketch tools should be active or not
      EditorEvents_OnCurrentLayerChanged
    End Sub
  3. Then implement the OnCurrentLayerChanged member of IEditEvents. Check that the target layer is a point layer, and if it is not, set the edit sketch geometry type to null; this will disable the sketch tools until a different layer or task is selected is selected. If a point layer is selected, then set the edit sketch to contain a Polyline..
    [Visual Basic 6]
    Private Sub EditorEvents_OnCurrentLayerChanged()
      ' Exit if there is no target (current) layer
      If m_pEditLayers.CurrentLayer Is Nothing Then Exit Sub
      
      If Not m_pEditLayers.CurrentLayer.FeatureClass.ShapeType = esriGeometryPoint Then
        m_pEditSketch.GeometryType = esriGeometryNull
      Else
        m_pEditSketch.GeometryType = esriGeometryPolyline
      End If
    End Sub
  4. When the task is deactivated, your edit task should stop listening for events, as you should only respond to events when your task is the active task.
    [Visual Basic 6]
    Private Sub IEditTask_Deactivate()
      Set EditorEvents = Nothing
    End Sub

Now you can compile the ConstructPointTask project and use it in an ArcMap edit session. Below you can see the task being used to create a feature in conjunction with the Distance and Direction.

 


Back to top

Go to example code

See Also Extending the Editing Framework and About Edit Tasks.