Attribute VB_Name = "FlipLines" Option Explicit Public Sub FlipLines() 'Flips all selected lines in the highlighted layer. 'This routine does not consider any network flow direction. 'If flow direction is set, it will be set to uninitalized for each selected feature in the highlighted layer. On Error GoTo EH Dim pApp As IApplication Dim pDoc As IMxDocument Dim pMap As IMap Dim pLayer As ILayer Dim pFlayer As IFeatureLayer Dim pFC As IFeatureClass Dim pFSel As IFeatureSelection Dim pFCursor As IFeatureCursor Dim pQFilter As IQueryFilter Dim pFeature As IFeature Dim pGeometry As IGeometry Dim pPolyLine As IPolyline Dim pId As New UID Dim pEditor As IEditor Dim pDataset As IDataset Dim pELayers As IEditLayers Dim lCount As Long Dim lTotal As Long Dim i As Long Dim pNetFeature As INetworkFeature Dim pGeoNetwork As IGeometricNetwork Dim pUNetwork As IUtilityNetwork Dim pSEdgeFeature As ISimpleEdgeFeature Dim pCNetFeature As IComplexNetworkFeature Dim lEID As Long Dim pNetElements As iNetElements Dim pEnumNetEID As IEnumNetEID Result = pLayer.StartEditingShapes(True) Set pApp = Application Set pDoc = pApp.Document Set pMap = pDoc.FocusMap ' Verify that there are layers in the table on contents If pMap.LayerCount < 1 Then MsgBox "Must have at least one layer in your map." Exit Sub End If 'Get highlighted layer in the TOC Set pLayer = pDoc.SelectedLayer If pLayer Is Nothing Then MsgBox "You must have one higlighted layer in the TOC." & vbNewLine & _ ' "Any selected lines in this layer will be flipped." Exit Sub End If 'Verify that it is a feature layer If Not TypeOf pLayer Is IFeatureLayer Then MsgBox "The highlighted layer in the TOC must be a feature layer." Exit Sub End If 'Get the Feature layer and feature class Set pFlayer = pLayer 'QI Set pFC = pFlayer.FeatureClass Set pFSel = pFlayer 'Verify that it is a line layer If pFC.ShapeType <> esriGeometryPolyline Then MsgBox "The highlighted layer in the TOC must contain lines." Exit Sub End If 'Verify it has some features selected If pFSel.SelectionSet.Count < 1 Then MsgBox "No features selected in the highlighted layer." Exit Sub End If 'Verify that we are editing pId = "esriCore.Editor" Set pEditor = pApp.FindExtensionByCLSID(pId) If Not (pEditor.EditState = esriStateEditing) Then MsgBox "Must be editing." Exit Sub End If 'Update Message bar pApp.StatusBar.Message(0) = "Flipping selected lines..." 'Start edit operation (for undo) pEditor.StartOperation 'Step through each selected line in the highlighted layer pFSel.SelectionSet.Search Nothing, False, pFCursor 'Store number of selected features lTotal = pFSel.SelectionSet.Count 'Initalize loop counter lCount = 0 'Loop through each selected feature in the highlighted layer Set pFeature = pFCursor.NextFeature Do While Not pFeature Is Nothing lCount = lCount + 1 pApp.StatusBar.Message(0) = "Flipping selected lines... " & Str(lCount) & " of " & Str(lTotal) 'Disconnect from network before flip If TypeOf pFeature Is INetworkFeature Then Set pNetFeature = pFeature pNetFeature.Disconnect End If 'Flip the geometry of the feature Set pPolyLine = pFeature.ShapeCopy pPolyLine.ReverseOrientation Set pFeature.Shape = pPolyLine 'Reconnect If TypeOf pFeature Is INetworkFeature Then pNetFeature.Connect End If If TypeOf pNetFeature Is IComplexEdgeFeature Then Dim pCEdgeFeature As IComplexEdgeFeature Set pCEdgeFeature = pNetFeature pCEdgeFeature.ConnectAtIntermediateVertices End If 'Save changes pFeature.Store 'Set flow to uninitalized... If TypeOf pFeature Is INetworkFeature Then Set pNetFeature = pFeature Set pGeoNetwork = pNetFeature.GeometricNetwork Set pUNetwork = pGeoNetwork.Network 'for each element of a complex edge If TypeOf pNetFeature Is IComplexNetworkFeature Then Set pNetElements = pGeoNetwork.Network Set pEnumNetEID = pNetElements.GetEIDs(pFeature.Class.ObjectClassID, pFeature.OID, esriETEdge) pEnumNetEID.Reset lEID = pEnumNetEID.Next For i = 1 To pEnumNetEID.Count pUNetwork.SetFlowDirection lEID, esriFDUninitialized lEID = pEnumNetEID.Next Next i 'or for the simple edge ElseIf TypeOf pNetFeature Is ISimpleEdgeFeature Then Set pSEdgeFeature = pNetFeature lEID = pSEdgeFeature.EID pUNetwork.SetFlowDirection lEID, esriFDUninitialized End If End If Set pFeature = pFCursor.NextFeature Loop 'Stop feature editing pEditor.StopOperation ("Flip Lines") 'Redraw the map so you'll see the new points pDoc.ActiveView.Refresh MsgBox lCount & " features flipped.", vbOKOnly, "Flip Lines" Exit Sub EH: MsgBox Err.Number & " " & Err.Description End Sub