moving tool

1673
3
11-08-2013 01:48 AM
NestorasPapadopoulos
New Contributor
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
0 Kudos
3 Replies
DuncanHornby
MVP Notable Contributor
Here is some VBA code to show how you would call a model in a toolbox

    ' Create workspacefactory
    Dim pToolboxWorkspaceFactory As IWorkspaceFactory
    Set pToolboxWorkspaceFactory = New esriGeoprocessing.ToolboxWorkspaceFactory
    
    'Open a toolbox workspace - you need to change the path
    Dim pToolboxWorkspace As IToolboxWorkspace
    Set pToolboxWorkspace = pToolboxWorkspaceFactory.OpenFromFile("c:\temp", 0)
    
    ' Connect to tool - you need to change the toolbox name and model name
    Dim pGPToolBox As IGPToolbox
    Set pGPToolBox = pToolboxWorkspace.OpenToolbox("myToolbox.tbx")
    Dim pGPTool As IGPTool
    Set pGPTool = pGPToolBox.OpenTool("mdExplodeToSingleRoutes")


    ' Run tool
    Dim pGPToolCommandHelper As IGPToolCommandHelper
    Set pGPToolCommandHelper = New GPToolCommandHelper
    pGPToolCommandHelper.SetTool pGPTool
    pGPToolCommandHelper.Invoke Nothing


    ' Destroy objects
    Set pToolboxWorkspaceFactory = Nothing
    Set pToolboxWorkspace = Nothing
    Set pGPToolBox = Nothing
    Set pGPTool = Nothing
    Set pGPToolCommandHelper = Nothing
0 Kudos
NestorasPapadopoulos
New Contributor
Thank you very much.
I didn't have any idea how to call my tool!!
0 Kudos
NestorasPapadopoulos
New Contributor
Here is some VBA code to show how you would call a model in a toolbox

    ' Create workspacefactory
    Dim pToolboxWorkspaceFactory As IWorkspaceFactory
    Set pToolboxWorkspaceFactory = New esriGeoprocessing.ToolboxWorkspaceFactory
    
    'Open a toolbox workspace - you need to change the path
    Dim pToolboxWorkspace As IToolboxWorkspace
    Set pToolboxWorkspace = pToolboxWorkspaceFactory.OpenFromFile("c:\temp", 0)
    
    ' Connect to tool - you need to change the toolbox name and model name
    Dim pGPToolBox As IGPToolbox
    Set pGPToolBox = pToolboxWorkspace.OpenToolbox("myToolbox.tbx")
    Dim pGPTool As IGPTool
    Set pGPTool = pGPToolBox.OpenTool("mdExplodeToSingleRoutes")


    ' Run tool
    Dim pGPToolCommandHelper As IGPToolCommandHelper
    Set pGPToolCommandHelper = New GPToolCommandHelper
    pGPToolCommandHelper.SetTool pGPTool
    pGPToolCommandHelper.Invoke Nothing


    ' Destroy objects
    Set pToolboxWorkspaceFactory = Nothing
    Set pToolboxWorkspace = Nothing
    Set pGPToolBox = Nothing
    Set pGPTool = Nothing
    Set pGPToolCommandHelper = Nothing



I had some errors when I used your code
Whenever you use 'Set' erros is 'Syntax error'
Also
pGPToolCommandHelper.SetTool pGPTool
    pGPToolCommandHelper.Invoke Nothing

'pGPToolCommanderHelper' error is 'Declaration Expected'

Am I doing something wrong?
I really look forward to your answer beacause I don't know what to do.

I forgot to mention that I use Visual Studio 10 and I am making this tool as ArcMap addin
Thanks
0 Kudos