nespap

moving tool

Discussion created by nespap on Nov 8, 2013
Latest reply on Nov 16, 2013 by nespap
Hallo,
I am new to Arcobjects and I am trying to make a tool.
Here is the case: I have a point shapefile (trig_srtm30) and a polygon shapefile (zone_C1toend). At first I place manually the polygon shapefile in a relation with the first point of the point shapefile (FID=0). Then I want to run a tool that have made in model builder. After that, I want to move the polygon shapefile according to the coordinates of the next point (FID=1) and run again my tool. Then continue to the next point and so on. I have written some code about moving the polygons but it doesn't seem to work well.
Can anyone help me?
Next I would like to ask how can I implement the tool I have made in model builder, with the moving tool in arcobjects, so that they can run together?
I am using VStudio 2010 .net framework 3.5, ArcMap 10.0

Here is the code I wrote for the moving tool:

Public Class Tool1
  Inherits ESRI.ArcGIS.Desktop.AddIns.Tool
    Private Function GetLayerByName(ByVal lyrName As String) As ILayer
        Dim pDoc As IMxDocument
        Dim pEnumLayer As IEnumLayer
        Dim pLyr As ILayer

        GetLayerByName = Nothing

        pDoc = ThisDocument
        pEnumLayer = pDoc.FocusMap.Layers

        pLyr = pEnumLayer.Next
        While Not pLyr Is Nothing
            If pLyr.Name = lyrName Then
                GetLayerByName = pLyr
                Exit Function
            End If
            pLyr = pEnumLayer.Next
        End While

    End Function
    Public Sub SelectMapFeatures()
        Dim pDoc As IMxDocument
        Dim pMap As IMap
        Dim pActiveView As IActiveView
        Dim pFeatureLayer As IFeatureLayer
        Dim pFeatureSelection As IFeatureSelection
        Dim pQueryFilter As IQueryFilter

        pDoc = Application.Document
        pMap = pMxDoc.FocusMap
        pActiveView = pMap

        pFeatureLayer = pMap.Layer(lyr)
        pFeatureSelection = pFeatureLayer 'QI

        'Create the query filter
        pQueryFilter = New QueryFilter
        pQueryFilter.WhereClause = "FID = row"

        'Invalidate only the selection cache
        'Flag the original selection
        pActiveView.PartialRefresh(esriViewGeoSelection, Nothing, Nothing)
        'Perform the selection
        pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultNew, False)
        'Flag the new selection
        pActiveView.PartialRefresh(esriViewGeoSelection, Nothing, Nothing)

    End Sub
    Public Sub New()






        ' VBA for moving features
        Dim pEditor As IEditor
        Dim pEndPoint As IPoint
        Dim pEnumFeature As IEnumFeature
        Dim pFeature As IFeature
        Dim pFeatureEdit As IFeatureEdit
        Dim pID As New UID
        Dim pInvalidArea As IInvalidArea
        Dim pLine As ILine
        Dim pMoveSet As ISet
        Dim pSpatialReference As ISpatialReference
        Dim pStartPoint As IPoint
        Dim pFeatureLayer As IFeatureLayer
        Dim pFeatureSelection As IFeatureSelection

        Dim origX As Double
        Dim origY As Double
        Dim bInOperation As Boolean

        Dim POINT_X As Double
        Dim POINT_Y As Double

        Dim row As Integer

        row = 0
        ' select rows of point shapefile by query

        GetLayerByName(trig_srtm30)
        SelectMapFeatures()

        ' Get the coordinates of trig shapefile
        POINT_X = pFeatureSelection.Value("X")
        POINT_Y = pFeatureSelection.Value("Y")


        GetLayerByName(zone_C1toend)

        On Error GoTo ErrorHandler

        'Get a reference to the editor extension
        pID = "esriEditor.Editor"
        pEditor = Application.FindExtensionByCLSID(pID)

        'Create an edit operation enabling undo for the operation
        pEditor.StartOperation()
        bInOperation = True

        'Make sure something has been selected
        If pEditor.SelectionCount = 0 Then Exit Sub

        'Add all the editor's selected features to a new set
        pEnumFeature = pEditor.EditSelection

        'Flag those areas of the display that need refreshing
        pInvalidArea = New InvalidArea
        pInvalidArea.Display = pEditor.Display
        pInvalidArea.Add(pEnumFeature)

        pMoveSet = New esriSystem.Set
        pEnumFeature.Reset()
        pFeature = pEnumFeature.Next

        Do While Not pFeature Is Nothing
            pMoveSet.Add(pFeature)
            pFeature = pEnumFeature.Next
        Loop

        'Reset the Set
        pMoveSet.Reset()

        'MoveSet requires a line to specify the new location'Use the selection anchor as a starting point for the line
        pStartPoint = pEditor.SelectionAnchor.Point
        pLine = New Line
        pStartPoint.QueryCoords(origX, origY)
        pEndPoint = New Point
        'offset the selection by new coordinates
    pEndPoint.PutCoords (POINT_X - origX ), (POINT_Y - origY)
        pLine.PutCoords(pStartPoint, pEndPoint)

        'Get the spatial reference from the map and assign it to the new line
        pSpatialReference = pEditor.Map.SpatialReference
        pLine.SpatialReference = pSpatialReference 'Set the spatial reference of the new line'Do the move while looping through the set
        pFeatureEdit = pMoveSet.Next
        Do While Not pFeatureEdit Is Nothing
            pFeatureEdit.MoveSet(pMoveSet, pLine) 'Move all the selected features 50 units to the right
            pFeatureEdit = pMoveSet.Next
        Loop

        'Stop the Edit Operation
        pEditor.StopOperation("Move Selection")
        bInOperation = False

        pInvalidArea.Invalidate(esriAllScreenCaches)

        'Additionally move the selection anchor
        pEditor.SelectionAnchor.MoveTo(pEndPoint, pEditor.Display)

        Exit Sub

ErrorHandler:
        If bInOperation Then
            pEditor.AbortOperation()
            MsgBox("Error moving features.  Check selected features for topological associations.")
        End If
    End Sub

  Protected Overrides Sub OnUpdate()
    Enabled = My.ArcMap.Application IsNot Nothing
  End Sub
End Class

Outcomes