Delete Junctions with Edge
cDeleteJunction.cls

' Copyright 2008 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
' 




Option Explicit

'This sample deletes the selected set of edges and any junctions which are connected to only one
'edge feature
Private m_pApp As IApplication
Private m_pEditor As esriEditor.IEditor
Private m_pMxDoc As esriArcMapUI.IMxDocument
Private m_pBitmap As IPictureDisp

Implements ICommand
 
Private Sub Class_Initialize()
    Set m_pBitmap = LoadResPicture(101, vbResBitmap)
End Sub

Private Sub Class_Terminate()
    Set m_pBitmap = Nothing
End Sub

Private Property Get ICommand_Enabled() As Boolean
    
    Dim pEnumFeat As esriGeoDatabase.IEnumFeature
    Dim pFeature As esriGeoDatabase.IFeature
    Dim i As Integer

    If Not m_pEditor.EditState = esriStateEditing Then Exit Property
    
    Set pEnumFeat = m_pEditor.Map.FeatureSelection  'QI
    pEnumFeat.Reset
    
    For i = 0 To m_pEditor.Map.SelectionCount - 1
        Set pFeature = pEnumFeat.Next
        If Not pFeature Is Nothing Then
            If (pFeature.FeatureType = esriFTComplexEdge Or pFeature.FeatureType = esriFTSimpleEdge And TypeOf pFeature Is INetworkFeature) Then
                ICommand_Enabled = True
            Else
                'If any of the features do not meet the above criteria, Toggle the Enabled property and
                'Exit the property
                ICommand_Enabled = False
                Exit Property
            End If
        End If
    Next i
    
End Property
 
Private Property Get ICommand_Checked() As Boolean
    
    ICommand_Checked = False
    
End Property
 
Private Property Get ICommand_Name() As String
    
    ICommand_Name = "Developer Samples_Delete Junctions with Edge"
    
End Property
 
Private Property Get ICommand_Caption() As String
    
    ICommand_Caption = "Delete Junctions with Edge"
    
End Property
 
Private Property Get ICommand_Tooltip() As String
    
    ICommand_Tooltip = "Delete Junctions with Edge"
    
End Property
 
Private Property Get ICommand_Message() As String
    
    ICommand_Message = "Deletes the edge and junctions which are only attached to this edge"
    
End Property
 
Private Property Get ICommand_HelpFile() As String
    
    ICommand_HelpFile = ""
    
End Property
 
Private Property Get ICommand_HelpContextID() As Long
    
    ICommand_HelpContextID = 0
    
End Property
 
Private Property Get ICommand_Bitmap() As esriSystem.OLE_HANDLE
    
    ICommand_Bitmap = m_pBitmap.Handle
    
End Property
 
Private Property Get ICommand_Category() As String
    
    ICommand_Category = "Developer Samples"
    
End Property
 
Private Sub ICommand_OnCreate(ByVal hook As Object)
    
    Dim pUID As New UID

    Set m_pApp = hook
    pUID = "esriEditor.Editor"

    Set m_pEditor = m_pApp.FindExtensionByCLSID(pUID)
    Set m_pMxDoc = m_pApp.Document
    
End Sub
 
Private Sub ICommand_OnClick()
    
    On Error GoTo ErrorHandler
    
    Dim pEnumFeat As esriGeoDatabase.IEnumFeature
    Dim pFeature As esriGeoDatabase.IFeature
    Dim pNetFeat As esriGeoDatabase.INetworkFeature
    Dim pEdge As esriGeoDatabase.IEdgeFeature
    Dim pCEF As esriGeoDatabase.IComplexEdgeFeature
    Dim pFromJunc As esriGeoDatabase.IFeature
    Dim pToJunc As esriGeoDatabase.IFeature
    Dim pJunc As esriGeoDatabase.IFeature
    Dim pFromSJF As esriGeoDatabase.ISimpleJunctionFeature
    Dim pToSJF As esriGeoDatabase.ISimpleJunctionFeature
    Dim pSJF As esriGeoDatabase.ISimpleJunctionFeature
    Dim pCJF As esriGeoDatabase.IComplexJunctionFeature
    Dim pSJFCol As Collection
    
    Dim pEdgeFeatCount As cEdgeFeatCount
    
    Dim bFromSJF As Boolean
    Dim bToSJF As Boolean
    Dim i, j, k As Integer          'Counters
    Dim lCount As Long

    Set pSJFCol = New Collection
    Set pEdgeFeatCount = New cEdgeFeatCount            'Instantiate the object
    Set pEnumFeat = m_pEditor.Map.FeatureSelection
    pEnumFeat.Reset
    
    'Start the edit operation
    m_pEditor.StartOperation
        
  For i = 0 To m_pEditor.Map.SelectionCount - 1
    Set pFeature = pEnumFeat.Next
    
    'Inserted for robustness
    If Not (pFeature.FeatureType = esriFTComplexEdge Or pFeature.FeatureType = esriFTSimpleEdge And TypeOf pFeature Is INetworkFeature) Then Exit Sub
    Set pNetFeat = pFeature
    Set pEdge = pNetFeat
    
    'Take care of Simple Edges
    If TypeOf pEdge Is ISimpleEdgeFeature Then
        Set pFromJunc = pEdge.FromJunctionFeature
        Set pToJunc = pEdge.ToJunctionFeature
        If TypeOf pFromJunc Is ISimpleJunctionFeature Then Set pFromSJF = pFromJunc
        If TypeOf pToJunc Is ISimpleJunctionFeature Then Set pToSJF = pToJunc
        
        'The flags are used because once the edge is deleted, the number of edges in the count is reduced by 1
        'Cannot simply delete junctions on dangling edges, as the edge is deleted with the junction
        If pFromSJF.EdgeFeatureCount = 1 Then bFromSJF = True
        If pToSJF.EdgeFeatureCount = 1 Then bToSJF = True
        
        'Delete the feature and the appropriate edges.  Must delete the edge first so that the junctions can
        'deleted afterwards
        pFeature.Delete
        If bFromSJF Then pFromJunc.Delete
        If bToSJF Then pToJunc.Delete
    End If
    
    'Take care of Complex edges
    If TypeOf pEdge Is IComplexEdgeFeature Then
        Set pCEF = pEdge
        lCount = pCEF.JunctionFeatureCount
        
        For j = 0 To lCount - 1
            'Take care of Simple Junctions
            If TypeOf pCEF.JunctionFeature(j) Is ISimpleJunctionFeature Then
                'Add the junctions to delete to the Collection
                Set pSJF = pCEF.JunctionFeature(j)
                If pEdgeFeatCount.EdgeFeatCount(pSJF) = 1 Then pSJFCol.Add pSJF
            End If
         Next j
        If pSJFCol.Count = 0 Then Exit Sub
        
        'Delete the features
         pFeature.Delete
         For k = 1 To pSJFCol.Count
             Set pJunc = pSJFCol(k)
             pJunc.Delete
         Next k
    End If
  Next i
    
     m_pMxDoc.ActivatedView.Refresh
     m_pEditor.StopOperation "Delete Edges/Junctions"
     m_pEditor.Map.ClearSelection
    
    Exit Sub
ErrorHandler:
     m_pEditor.StopOperation "Delete Edges/Junctions"
End Sub