Knowledge Base - Technical Articles


Technical Article   HowTo:  Delete selected features using ArcObjects

Article ID: 25130
Software:  ArcGIS - ArcEditor 8.1, 8.1.2, 8.2, 8.3 ArcGIS - ArcInfo 8.1, 8.1.2, 8.2, 8.3 ArcGIS - ArcView 8.1, 8.1.2, 8.2, 8.3
Platforms: N/A

Summary

Instructions provide a code sample which deletes selected features.

Procedure

  1. Start ArcMap.
  2. Open the Visual Basic Editor. -show me-

    In ArcMap, select Tools > Macros > Visual Basic Editor.
  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument then right-click and select View Code. -show me-

    [O-Image] Visual Basic Project  Explorer


     Code in the Project's ThisDocument code module will only run in the current map document. If you want to store the code in all your map documents open the Normal.mxt ThisDocument code module instead.

  4. Copy the following code into the code module.

    Option Explicit
    

    Public Sub Main()

    Dim pDoc As IMxDocument
    Dim pLayer As IFeatureLayer
    Dim mySet As ISet
    Dim pFeature As IFeature
    Dim pEnumFeature As IEnumFeature
    Dim pEditor As IEditor
    Dim pUID As New UID

    Set pDoc = Application.Document
    Set pEnumFeature = pDoc.ActiveView.Selection
    pEnumFeature.Reset
    Set pFeature = pEnumFeature.Next
    pUID = "esricore.editor"
    Set pEditor = Application.FindExtensionByCLSID(pUID)

    Set pDoc = Application.Document

    'Make certain the layer is selected in the TOC
    Set pLayer = pDoc.SelectedLayer

    'Check to make certain that there is an edit session started
    If pEditor.EditState = esriStateNotEditing Then
    MsgBox "Cannot Edit outside of an edit session"
    End If
    'Call the DeleteSelectedFeatures sub procedure
    'and pass in the EnumFeature object.

    DeleteSelectedFeatures pEnumFeature
    pDoc.ActiveView.Refresh



    End Sub

    Private Sub DeleteSelectedFeatures(pEnumFeature As IEnumFeature)

    Dim pFeature As IFeature
    Dim mySet As esriCore.ISet
    Set mySet = New esriCore.Set
    Dim pFeatureEdit As IFeatureEdit

    pEnumFeature.Reset
    Set pFeature = pEnumFeature.Next

    'Takes features and writes them out to an ISet object
    Do Until pFeature Is Nothing
    Set pFeatureEdit = pFeature
    mySet.Add pFeature
    Set pFeature = pEnumFeature.Next
    Loop

    'Calls the deleteset method from IFeatureEdit
    'to delete the selected set of records
    pFeatureEdit.DeleteSet mySet

    End Sub


  5. Close the Visual Basic Editor.
  6. Add a feature class to ArcMap and select some features to delete.
  7. Select the layer in the table of contents and run the code. -show me-

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.

Created: 6/18/2003
Last Modified: 4/25/2008

Article Rating: (1)
If you would like to post a comment, please login

Comments

By Anonymous - 04/14/2008 7:52 AM

The article needs to be updated.

redundent code. Also, it didn't work in our class demo. Not good PR for ESRI

Rating:

By Anonymous - 07/06/2007 3:25 AM

The article is incorrect or the solution didn’t work.

This code does not work for 9.2. The esricore library is not present at 9.2. The Iset member is now found in esriSystem.

By Anonymous - 07/03/2007 12:41 PM

The article has missing, misleading, or conflicting information.

In this article, Private Sub DeleteSelectedFeatures(pEnumFeature As IEnumFeature) Dim pFeature As IFeature Dim mySet As esriCore.ISet Set mySet = New esriCore.Set ... esriCore library should be replaced by esriSystem. At ArcGIS 8.x, all type library information was located in the one single file - esricore; starting with ArcGIS 9.0, ArcGIS was changed to be more modular, with the type library information broken up into smaller more manageable libraries (esriCarto, esriGeod

By Anonymous - 09/19/2006 3:14 AM

The article is incorrect or the solution didn’t work.

Hi there, I'm using ArcGIS 9.1 and the macro won't work. I did everything exaclty as the article says. And when I try to execute, I get TYPE MISMATCH error. Any ideas? Thanks in advance. Sincerely, Andrius