nmfadhil

ArcMap Add-Ins to Add Shapefile Using OpenFileDialog

Discussion created by nmfadhil on Feb 7, 2011
Latest reply on Feb 9, 2011 by nmfadhil
G'day all,

I am trying to build ArcMap Add-Ins to Add Shapefile Using OpenFileDialog.

I am stuck on "name m_application is not declared"

Anybody can help me?

Below is my code:

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI

Public Class ZoomToLayerButton
    Inherits ESRI.ArcGIS.Desktop.AddIns.Button

    Public Sub New()

    End Sub

    Protected Overrides Sub OnClick()
        'TODO: Add AddShapefile.OnClick implementation.
        Dim activeview As ESRI.ArcGIS.Carto.IActiveView = GetActiveViewFromArcMap(m_application)
        AddShapefileUsingOpenFileDialog(activeview)
    End Sub

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


#Region "Add Shapefile Using OpenFileDialog"
    ' ArcGIS Snippet Title:
    ' Add Shapefile Using OpenFileDialog
    ' 
    ' Long Description:
    ' Add a shapefile to the ActiveView using the Windows.Forms.OpenFileDialog control.
    ' 
    ' Add the following references to the project:
    ' ESRI.ArcGIS.Carto
    ' ESRI.ArcGIS.DataSourcesFile
    ' ESRI.ArcGIS.Display
    ' ESRI.ArcGIS.Geodatabase
    ' ESRI.ArcGIS.Geometry
    ' ESRI.ArcGIS.System
    ' System
    ' System.Windows.Forms
    ' 
    ' Intended ArcGIS Products for this snippet:
    ' ArcGIS Desktop (ArcEditor, ArcInfo, ArcView)
    ' ArcGIS Engine
    ' 
    ' Applicable ArcGIS Product Versions:
    ' 9.2
    ' 9.3
    ' 9.3.1
    ' 10.0
    ' 
    ' Required ArcGIS Extensions:
    ' (NONE)
    ' 
    ' Notes:
    ' This snippet is intended to be inserted at the base level of a Class.
    ' It is not intended to be nested within an existing Function or Sub.
    ' 

    '''<summary>Add a shapefile to the ActiveView using the Windows.Forms.OpenFileDialog control.</summary>
    '''
    '''<param name="activeView">An IActiveView interface</param>
    ''' 
    '''<remarks></remarks>
    Public Sub AddShapefileUsingOpenFileDialog(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView)

        'parameter check
        If activeView Is Nothing Then

            Return

        End If

        ' Use the OpenFileDialog Class to choose which shapefile to load.
        Dim openFileDialog As System.Windows.Forms.OpenFileDialog = New System.Windows.Forms.OpenFileDialog
        openFileDialog.InitialDirectory = "c:\"
        openFileDialog.Filter = "Shapefiles (*.shp)|*.shp"
        openFileDialog.FilterIndex = 2
        openFileDialog.RestoreDirectory = True
        openFileDialog.Multiselect = False

        If openFileDialog.ShowDialog = System.Windows.Forms.DialogResult.OK Then

            ' The user chose a particular shapefile.

            ' The returned string will be the full path, filename and file-extension for the chosen shapefile. Example: "C:\test\cities.shp"
            Dim shapefileLocation As String = openFileDialog.FileName

            If shapefileLocation <> "" Then

                ' Ensure the user chooses a shapefile

                ' Create a new ShapefileWorkspaceFactory CoClass to create a new workspace
                Dim workspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass

                ' System.IO.Path.GetDirectoryName(shapefileLocation) returns the directory part of the string. Example: "C:\test\"
                Dim featureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace = CType(workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(shapefileLocation), 0), ESRI.ArcGIS.Geodatabase.IFeatureWorkspace) ' Explicit Cast

                ' System.IO.Path.GetFileNameWithoutExtension(shapefileLocation) returns the base filename (without extension). Example: "cities"
                Dim featureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation))

                Dim featureLayer As ESRI.ArcGIS.Carto.IFeatureLayer = New ESRI.ArcGIS.Carto.FeatureLayerClass
                featureLayer.FeatureClass = featureClass
                featureLayer.Name = featureClass.AliasName
                featureLayer.Visible = True
                activeView.FocusMap.AddLayer(featureLayer)

                ' Zoom the display to the full extent of all layers in the map
                activeView.Extent = activeView.FullExtent
                activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography, Nothing, Nothing)

            Else

                ' The user did not choose a shapefile.
                ' Do whatever remedial actions as necessary
                ' System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #1", System.Windows.Forms.MessageBoxButtons.OK,  System.Windows.Forms.MessageBoxIcon.Exclamation)

            End If

        Else

            ' The user did not choose a shapefile. They clicked Cancel or closed the dialog by the "X" button.
            ' Do whatever remedial actions as necessary.
            ' System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #2", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation)

        End If

    End Sub
#End Region

#Region "Get ActiveView from ArcMap"
    ' ArcGIS snippet title:
    ' Get ActiveView from ArcMap.
    '
    ' Long description:
    ' Get ActiveView from ArcMap.
    '
    ' Add the following references to the project:
    ' ESRI.ArcGIS.ArcMapUI.
    ' ESRI.ArcGIS.Carto.
    ' ESRI.ArcGIS.Framework.
    ' ESRI.ArcGIS.System.
    '
    ' Intended ArcGIS products for this snippet:
    ' ArcGIS Desktop (ArcEditor, ArcInfo, ArcView).
    '
    ' Applicable ArcGIS product versions:
    ' 9.2.
    ' 9.3.
    '
    ' Required ArcGIS extensions:
    ' (None)
    '
    ' Notes:
    ' This snippet is intended to be inserted at the base level of a class.
    ' It is not intended to be nested within an existing function or sub.
    '
    '''<summary>Get ActiveView from ArcMap</summary>
    '''
    '''<param name="application">An IApplication interface that is the ArcMap application.</param>
    '''
    '''<returns>An IActiveView interface.</returns>
    '''
    '''<remarks></remarks>

    Public Function GetActiveViewFromArcMap(ByVal application As ESRI.ArcGIS.Framework.IApplication) As ESRI.ArcGIS.Carto.IActiveView

        If application Is Nothing Then
            Return Nothing
        End If

        Dim mxDocument As ESRI.ArcGIS.ArcMapUI.IMxDocument = TryCast(application.Document, ESRI.ArcGIS.ArcMapUI.IMxDocument) ' Dynamic Cast
        Dim activeView As ESRI.ArcGIS.Carto.IActiveView = mxDocument.ActiveView

        Return activeView

    End Function

#End Region


End Class




Outcomes