Merge Network Features
clsMergeNetFeats.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 command merges features togehter and utilizes merge rules to determine the value
'of the merged feature based on the attribute values of the original features

Implements ICommand

Public m_pApp As IApplication
Public m_pEditor As IEditor
Public m_pEditLayers As IEditLayers
Public m_pFC As IFeatureClass      'Feature Class of the selected features
Public m_lSubtype As Long
Public m_colFeatures As Collection 'Collection of features selected

Dim pEnumFeature As IEnumFeature
Dim lGTotalVal As Double
Dim lSubTypeCode As Long

Private m_pFrm As frmMergeOptions

Private Property Get ICommand_HelpContextID() As Long
  ICommand_HelpContextID = 0
End Property

Private Property Get ICommand_HelpFile() As String
  ICommand_HelpFile = ""
End Property

Private Property Get ICommand_Message() As String
  ICommand_Message = "Merge network features with obeying attribute merge rules."
End Property

Private Property Get ICommand_Name() As String
  ICommand_Name = "Developer Samples_Merge network features"
End Property

Private Property Get ICommand_Tooltip() As String
  ICommand_Tooltip = "Merge network features"
End Property

Private Property Get ICommand_Bitmap() As esriSystem.OLE_HANDLE
  'no bitmap associated with this command
End Property

Private Property Get ICommand_Caption() As String
  ICommand_Caption = "Merge network features"
End Property

Private Property Get ICommand_Category() As String
  ICommand_Category = "Developer Samples"
End Property

Private Property Get ICommand_Checked() As Boolean
  ICommand_Checked = False
End Property

Private Property Get ICommand_Enabled() As Boolean
  'code to check if at least two features are selected,
  'and if so, if they are in the same f-class
  
  'Check if we're in an Edit session or if 1 feature is selcted
  If m_pEditor.EditState = esriStateNotEditing Or m_pEditor.SelectionCount < 2 Then
    ICommand_Enabled = False
    Exit Property
  End If
  
  'Multiple selected features - are they from the same feature class,
  'and are they of the same subtype?
  Set pEnumFeature = m_pEditor.EditSelection
  
  Dim lCLSID As Long
  Dim pFeature As IFeature
  pEnumFeature.Reset
  Set pFeature = pEnumFeature.Next
  
  'Make sure it's not a Junction feature class.... and that it's a network feature...
  Set m_pFC = m_pEditLayers.CurrentLayer.FeatureClass
  m_lSubtype = m_pEditLayers.CurrentSubtype
  If m_pFC.ShapeType = esriGeometryPoint Or Not TypeOf m_pFC Is INetworkClass Then
    ICommand_Enabled = False
    Exit Property
  End If

  'Get the CLSID and Subtype of the first feature in the Enum. Check all of the features against
  'this one in the Do..Loop that follows.
  lCLSID = pFeature.Class.ObjectClassID
  lGTotalVal = 0
  
  Do
    If Not pFeature.Class.ObjectClassID = lCLSID Then
      ICommand_Enabled = False
      Exit Property
    End If
    Set pFeature = pEnumFeature.Next
  Loop Until pFeature Is Nothing
    
  ICommand_Enabled = True
  
End Property

Private Sub ICommand_OnCreate(ByVal hook As Object)
  'get a reference to the editor extension
    On Error GoTo EH
    Set m_pApp = hook
    Set m_pEditor = m_pApp.FindExtensionByName("ESRI Object Editor")
    Set m_pEditLayers = m_pEditor
    
    Set m_pFrm = New frmMergeOptions
    
    Exit Sub
EH:
    MsgBox "OnCreate Error" + vbCrLf + Err.Description, vbCritical, App.Title

End Sub

Private Sub ICommand_OnClick()
    'This is here to regulate the error code that gets displayed based on which section of the code we're in...
    Dim ErrCode As Long
    ErrCode = 0
    'code to merge the features, evaluate the merge rules and assign values to fields appropriatly
    Dim pDoc As IMxDocument
    Set pDoc = m_pApp.Document
    
    'Gets the combined shape of the selected features.... Doing it here so it's not called whenever
    'Enabled gets called....
    'Build an array of ObjectIDs, so we can ask the user from which feature the attribute values should be taken from

    Dim pEnumFeature As IEnumFeature
    Dim pFeature As IFeature
    Dim i As Long
    
    Set pEnumFeature = m_pEditor.EditSelection
    pEnumFeature.Reset

    Set m_colFeatures = New Collection
    Set pFeature = pEnumFeature.Next
    Do
      m_colFeatures.Add pFeature, CStr(pFeature.OID)
      Set pFeature = pEnumFeature.Next
    Loop Until pFeature Is Nothing
    
    'Open the dialog display the list of OIDs, so the user can select which one should be used
    m_pFrm.ShowModal Me
    
End Sub

Private Sub Class_Terminate()
    If Not m_pFrm Is Nothing Then
      Unload m_pFrm
      Set m_pFrm = Nothing
    End If
End Sub