Select to view content in your preferred language

NEED HELP recording only point OID's of a multi-Geometry Type selection when deleted

756
2
07-13-2010 12:22 PM
NickMoylan
Deactivated User
Hello all,

I'm using the OnDeleteFeature Event to monitor for deleted POINTS only and then write the ID to a text file.  I've set up a test map document where I have point and line layers. 

If I delete a single point or group of points, they are recorded --> This is good
If I delete a single line or group of lines, they are not recorded --> This is also good

However, if I have a selection of lines and points and delete them, it will record them all as deleted points. I need the code to only record the points of the selection and not the lines.

I have the OnDeleteFeature sub that I am using below.  If anyone has Ideas I would really appreciate it.

Thanks,
Nick

Private Sub EditEvents_OnDeleteFeature(ByVal obj As IObject)
    Set pMxDoc = ThisDocument      

    pID = "esriEditor.Editor"  
    Set m_pEditor = Application.FindExtensionByCLSID(pID)
   
    Dim pFeature As IFeature
    Dim pGeometry As IGeometry
    Dim pEnumFeat As IEnumFeature
   
    Set pEnumFeat = m_pEditor.EditSelection
    Set pFeature = pEnumFeat.Next
   
    Do While Not pFeature Is Nothing
        If pFeature.Shape.GeometryType = esriGeometryPoint Then    
            IdTag = obj.OID
   
            fileNum = FreeFile
            Open path For Input As #fileNum 'Opens the file for reading
           
            Do While Not EOF(fileNum)
                Input #fileNum, prevData 'stores string data in the prevData variable
            Loop
            Close #fileNum  'must always close the file
   
            fileNum2 = FreeFile 
            Open path For Output As #fileNum2 'Opens file for writing
                       
            Write #fileNum2, vbCrLf & "(" & IdTag & ")" & vbTab & vbTab & Format(Now, "dd-mmm-yyyy  hh:mm:ss AM/PM")_
       & vbTab & "*TAG DELETED*" & vbCrLf & prevData
            Close #fileNum2  'must always close the file
            MsgBox "Point Deleted: " & obj.OID 
            Exit Sub
        Else
            MsgBox "Line Deleted: " & obj.OID 
            Exit Sub
        End If
        Set pFeature = pEnumFeat.Next
    Loop   
End Sub
0 Kudos
2 Replies
NeilClemmons
Honored Contributor
The feature being deleted is passed into the event.  There's no need to loop through the editor's feature selection as you already have a reference to the feature being deleted.

Private Sub EditEvents_OnDeleteFeature(ByVal obj As IObject)

obj is the feature object being deleted.  Check its type, and if it's a point, do what you need to do with it.
0 Kudos
NickMoylan
Deactivated User
Thanks for the help, I am posting my completed code below if anyone is interested.  The comments might not all be 100% correct, but they're close.  The client wanted everything commented, so it's a little comment heavy.

Criticism is welcome.  Enjoy!!


Option Explicit     'Requires user to declare all of your variables
'Below, the user will have to declare two constant values
'Const path As String = "E:/School/Semester3/GIS4309/my_id_tags_92.txt" 'path of the text file being written to
'Const layerName As String = "TestPoints"  'Name of the Layer to be tracked

Private m_pEditor As IEditor  'Declare an IEditor variable to be available at the module level

Private WithEvents EditEvents As Editor  'use the WithEvents keyword and provide appropriate functions to handle the events.

'Declare variables available to the whole module
Dim pMap As IMap
Dim prevData As String
Dim IdTag As String
Dim pMxDoc As IMxDocument
Dim pFeatureLayer As IFeatureLayer
Dim pFeature As IFeature
Dim pLayer As ILayer
Dim pEnumLayer As IEnumLayer
Dim fileNum As Integer
Dim fileNum2 As Integer
Dim pEditLayers As IEditLayers
Dim pID As New UID
Dim targetLayer As IFeatureLayer
Public path As String
Public layerName As String




'will run the StartListeningToEditEvents subroutine when the button is clicked
'It will also prompt the user to enter the path to the text file for storing the info and the Layer to listen to
Public Sub TagListener_Click()
    
    'path = "E:/School/Semester3/GIS4309/my_id_tags_92.txt" 'path of the text file being written to
    
    layerName = "TestPoints"  'Name of the Layer to be tracked
    
    vaFiles = Application.GetOpenFilename(FileFilter:="Excel Filer (*.txt),*.txt", Title:="Open File", MultiSelect:=False)

    'This section will prompt the user to enter the path to the text file for storing the info
    Dim newpath As String
    newpath = InputBox("Please enter the path to your Tag Tracking File (ie: E:/School/Semester3/GIS4309/my_id_tags_92.txt)", "Path Required", path)
    If StrPtr(newpath) = 0 Or newpath = "" Then   'Handles the Cancel button or if the user enters nothing
    Exit Sub
    End If
    
    'This section will prompt the user to enter the name of the Layer in the map document to listen to
    Dim newlayername As String
    newlayername = InputBox("Please enter the layer name you are tracking (ie: MyPoints)", "Layer Name Required", "TestPoints")
    If StrPtr(newlayername) = 0 Or newlayername = "" Then   'Handles the Cancel button or if the user enters nothing
    Exit Sub
    End If
    
    'These two staements will pass the user input into these variables for use later on in the code
    path = newpath
    layerName = newlayername
    
    StartTagListener       'call to this subroutine
End Sub

'This sub will be the macro that the button on the toolbar runs
Public Sub StartTagListener()
    
    Set EditEvents = Application.FindExtensionByName("ESRI Object Editor") 'Listen for events in the editor environment
  
End Sub

'This sub will listen for any features that are being deleted
'it will then write the OBJECTID Tag of the object to the external text file
'only if the feature being deleted is a a point feature

'when listening for the delete event, the sub will note the object being deleted
Private Sub EditEvents_OnDeleteFeature(ByVal obj As IObject)
    
    Set pMxDoc = ThisDocument   'Sets the pMxDoc to the current document using the ThisDocument keyword
    
    'This next section set up access to the ESRI Editor Library
    'this library implements ArcMap's object editor including the
    'editing of feature geometry and attributes as well as
    'topological display and editing
    pID = "esriEditor.Editor"   'Sets the pID variable to the UID with this name
    Set m_pEditor = Application.FindExtensionByCLSID(pID) 'creates the access to the ESRIEditor Library
    
    'Declare Variables for use in this section of the code
    Dim pFeature As IFeature
    Dim pGeometry As IGeometry
    
    'This next line will set the current object being deleted to the feature variable
    'This line is important as it will allow the GeometryType property to act on the IObject
    Set pFeature = obj
    
    If pFeature.Shape.GeometryType = esriGeometryPoint Then 'Will run the code if the Feature's geometry type is a point type
        
        IdTag = obj.OID 'Sets the IdTag variable to the OID of the object being deleted
    
        fileNum = FreeFile 'Obtain next free file handle number.
        Open path For Input As #fileNum 'Opens the file for reading
        'This loop will store all the text from the beginning to the End of the File (EOF)
        Do While Not EOF(fileNum)
            Input #fileNum, prevData 'stores string data in the prevData variable
        Loop
        Close #fileNum  'must always close the file
    
        fileNum2 = FreeFile  'Obtain next free file handle number.
        Open path For Output As #fileNum2 'Opens file for writing
        'the next line appends a string of content to the top of the text file
        'including the deleted tag ID in brackets, The current date and time,
        'and a TAG DELETED Marker
        Write #fileNum2, vbCrLf & "(" & IdTag & ")" & vbTab & vbTab & Format(Now, "dd-mmm-yyyy  hh:mm:ss AM/PM") & vbTab & "*TAG DELETED*" & vbCrLf & prevData
        Close #fileNum2  'must always close the file
        'MsgBox "Point Deleted: " & obj.OID  'Displays a message box when a feature is deleted --> for testing only
        Exit Sub
    Else
        'MsgBox "Line Deleted: " & obj.OID  'Displays a message box when a feature is deleted --> for testing only
        Exit Sub  'will exit the sub if the layer names do not match and write nothing to the file
    End If
End Sub

'This sub will listen for any features that are being created
'it will then write the OBJECTID Tag of the object to the external text file
'only if the target layer selected is that of the layer we wish to
'record the ID's of ie: TestPoints Layer

'when listening for the create event, the sub will note the object being deleted
Private Sub EditEvents_OnCreateFeature(ByVal obj As IObject)
    
    Set pMxDoc = ThisDocument   'Sets the pMxDoc to the current document using the ThisDocument keyword
    
    'This next section set up access to the ESRI Editor Library
    'this library implements ArcMap's object editor including the
    'editing of feature geometry and attributes as well as
    'topological display and editing
    pID = "esriEditor.Editor"   'Sets the pID variable to the UID with this name
    Set m_pEditor = Application.FindExtensionByCLSID(pID) 'creates the access to the ESRIEditor Library
    Set pEditLayers = m_pEditor 'QI
    
    'sets the tagetLayer variable to the layer currently selected in the editor's taget drop-down list
    Set targetLayer = pEditLayers.CurrentLayer
    
    ' Get all the layers in the Map
    Set pEnumLayer = pMxDoc.FocusMap.Layers
    
    Set pLayer = pEnumLayer.Next 'Sets pLayer to the first layer in the table of contents window
    
    ' Loop through all the layers until the desired layer name is found
    Do Until pLayer Is Nothing
        If pLayer.Name = layerName Then
            Exit Do
        End If
        Set pLayer = pEnumLayer.Next 'If the layer name was not found, go to next layer
    Loop
    
    Set pFeatureLayer = pLayer  'QI
    
    If pFeatureLayer.Name = targetLayer.Name Then 'Will run the code if the FeatureLayer and targetLayer have the same name
    
        IdTag = obj.OID 'Sets the IdTag variable to the OID of the object being deleted
    
        fileNum = FreeFile 'Obtain next free file handle number.
        Open path For Input As #fileNum 'Opens the file for reading
        'This loop will store all the text from the beginning to the End of the File (EOF)
        Do While Not EOF(fileNum)
            Input #fileNum, prevData 'stores string data in the prevData variable
        Loop
        Close #fileNum  'must always close the file
    
        fileNum2 = FreeFile  'Obtain next free file handle number.
        Open path For Output As #fileNum2 'Opens file for writing
        'the next line appends a string of content to the top of the text file
        'including the deleted tag ID in brackets, and the current date and time
        Write #fileNum2, vbCrLf & IdTag & vbTab & vbTab & Format(Now, "dd-mmm-yyyy  hh:mm:ss AM/PM") & vbCrLf & prevData
        Close #fileNum2  'must always close the file
    Else
        Exit Sub  'will exit the sub if the layer names do not match and write nothing to the file
    End If
    
    'MsgBox "EditEvents_OnCreateFeature : " & obj.OID  'Displays a message box when a feature is deleted --> for testing only
    
End Sub

'This function will add a Tool Tip to the UI button
'When the user hovers over the button it will provide info on what it does
Private Function TagListener_ToolTip() As String
    TagListener_ToolTip = "Click this button to start listening for tage creation/deletion events"  'Tool Tip statement
End Function
0 Kudos