nespap

Not running tool

Discussion created by nespap on Nov 21, 2013
Latest reply on Nov 25, 2013 by Hornbydd
Hallo,
I have made an Add-In tool using Visual Studio 2010 for ArcMap 10.0
I added it with the Add-In Manager in a custom toolbar, but when I pressed
the button nothing happend. It should select and move a polygon shapefile (poly_C1toend)
according to the coordinates of a selected row of a point shapefile (trig_srtm30). Then it should
run a tool (topo_iter) mabe in model builder from a custom toolbox (Toolbox_g).
Can anyone help me with that?
Here is the code
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS

Public Class Tool1
    Inherits ESRI.ArcGIS.Desktop.AddIns.Tool



#Region "Select Map Features by Attribute Query"

    '''<summary>Select features in the IActiveView by an attribute query using a SQL syntax in a where clause.</summary>
    ''' 
    '''<param name="activeView">An IActiveView interface</param>
    '''<param name="featureLayer">An IFeatureLayer interface to select upon</param>
    '''<param name="whereClause">A System.String that is the SQL where clause syntax to select features. Example: "CityName = 'Redlands'"</param>
    '''  
    '''<remarks>Providing and empty string "" will return all records.</remarks>
    Public Sub SelectMapFeaturesByAttributeQuery(ByVal activeView As IActiveView, ByVal featureLayer As IFeatureLayer, ByVal whereClause As System.String)

        If activeView Is Nothing OrElse featureLayer Is Nothing OrElse whereClause Is Nothing Then
            Return
        End If

        Dim featureSelection As IFeatureSelection = TryCast(featureLayer, IFeatureSelection) ' Dynamic Cast

        ' Set up the query
        Dim queryFilter As IQueryFilter = New QueryFilterClass
        queryFilter.WhereClause = whereClause

        ' Invalidate only the selection cache. Flag the original selection
        activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

        ' Perform the selection
        featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

        ' Flag the new selection
        activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

    End Sub
#End Region



#Region "Clear Selected Map Features"

    '''<summary>Clear the selected features in the IActiveView for a specified IFeatureLayer.</summary>
    ''' 
    '''<param name="activeView">An IActiveView interface</param>
    '''<param name="featureLayer">An IFeatureLayer</param>
    ''' 
    '''<remarks></remarks>
    Public Sub ClearSelectedMapFeatures(ByVal activeView As IActiveView, ByVal featureLayer As IFeatureLayer)

        If activeView Is Nothing OrElse featureLayer Is Nothing Then

            Return

        End If

        Dim featureSelection As IFeatureSelection = TryCast(featureLayer, IFeatureSelection) ' Dynamic Cast

        ' Invalidate only the selection cache. Flag the original selection
        activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

        ' Clear the selection
        featureSelection.Clear()

        ' Flag the new selection
        activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

    End Sub
#End Region


#Region "MoveFeatures"

    Public Sub MoveFeatures()
        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 myApplication As IApplication

        Dim origX As Double
        Dim origY As Double
        Dim bInOperation As Boolean
        Dim pX As Double
        Dim pY As Double

        On Error GoTo ErrorHandler

        'Get a reference to the editor extension
        pID = "esriEditor.Editor"
        pEditor = myApplication.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 pX units in the x direction, pY units in y direction
        pEndPoint.PutCoords((origX + pX), (origY + pY))
        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

#End Region



    Public Sub New()

        ' select coords and assign to variables by select Map Features by Atribute Query
        Dim row As Integer
        Dim pX As Double
        Dim pY As Double
        row = 0
        SelectMapFeaturesByAttributeQuery(activeView, trig_srtm30, "FID=row")

        pX = trig_srtm30.X.Value
        pY = trig_srtm30.Y.Value



        ' Clear selection 
        ClearSelectedMapFeatures(activeView, trig_srtm30)



        ' select polygon shapefile which will be moved by select Map Features by Atribute Query
        SelectMapFeaturesByAttributeQuery(activeView, poly_C1toend, "")



        ' move shapefile according to new coords

        MoveFeatures()




        ' make calculations by the tool made by modelbuilder

        ' Create workspacefactory
        Dim pToolboxWorkspaceFactory As IWorkspaceFactory
        pToolboxWorkspaceFactory = New esriGeoprocessing.ToolboxWorkspaceFactory

        'Open a toolbox workspace - you need to change the path
        Dim pToolboxWorkspace As IToolboxWorkspace
        pToolboxWorkspace = pToolboxWorkspaceFactory.OpenFromFile("C:\akis\my data\documents\proffesional\geoid_works\rasters", 0)

        ' Connect to tool - you need to change the toolbox name and model name
        Dim pGPToolBox As IGPToolbox
        pGPToolBox = pToolboxWorkspace.OpenToolbox("Toolbox_g.tbx")
        Dim pGPTool As IGPTool
        pGPTool = pGPToolBox.OpenTool("topo_iter")

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

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






    End Sub

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

    Private Function activeView() As IActiveView
        Throw New NotImplementedException
    End Function

    Private Function trig_srtm30() As IFeatureLayer
        Throw New NotImplementedException
    End Function

    Private Function poly_C1toend() As IFeatureLayer
        Throw New NotImplementedException
    End Function

    Private Function esriAllScreenCaches() As Short
        Throw New NotImplementedException
    End Function

End Class

Outcomes