Select to view content in your preferred language

problem with IMap interface

1040
7
05-13-2010 10:05 AM
AdrianWelsh
MVP Honored Contributor
I am using a standalone form to get a feature layer and then put it in an open ArcMap project.  It's a short code and it works in VBA, but I don't know why it wouldn't work in a VB.NET form.  Here is the code

Private Sub btnImportArcMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportArcMap.Click

        Dim pMxDoc As IMxDocument
        Dim pMap As IMap

        pMxDoc = m_pApp.Document
        pMap = pMxDoc.FocusMap

        Dim pFLayer As IFeatureLayer = New FeatureLayer
        Dim pFClass As IFeatureClass
        Dim pGPUtilities As IGPUtilities = New GPUtilities

        Try

            pFClass = pGPUtilities.OpenFeatureClassFromString("C:\Visualization.gdb\temp")

            pFLayer.FeatureClass = pFClass
            pFLayer.Name = pFClass.AliasName

            pMap.AddLayer (pFLayer)

            pMxDoc.UpdateContents()
            pMxDoc.ActiveView.Refresh()

        Catch ex As Exception

            MessageBox.Show (ex.Message)

        End Try

        MessageBox.Show ("Complete")
    End Sub


I have a sub before this that calls the m_pApp as IApplication and opens a new ArcMap project.  The problem with my code is at the pMap.AddLayer (pFLayer) part.  Can anyone tell me what I need to add or change to this code?

Thanks!
0 Kudos
7 Replies
NeilClemmons
Honored Contributor
If this code is not running inside of ArcMap then you can't use New to instantiate your objects.  Your application and ArcMap are running in two separate process spaces and objects created in one can't be used in the other.  You'll need to use IObjectFactory to create instances of the ArcObjects you want to use with ArcMap.  You may not be able to use the objects returned by the geoprocessor methods your using either.
0 Kudos
AdrianWelsh
MVP Honored Contributor
Neil, thanks for the response.  I am not too familiar with the IObjectFactory interface.  Is there a way that you can give me an example on how to instantiate these objects from an outside form to work inside an open ArcMap document?  If the IGPUtilities interface wouldn't work for this then what about the IFeatureWorkspace interface?  Let me know if there's a solution to this.

Thanks!
Adrian
0 Kudos
NeilClemmons
Honored Contributor
The developer help topic for the IObjectFactory interface has a link to a sample for automating the ArcGIS desktop apps.  The sample is available in VB.NET and C#.
0 Kudos
AdrianWelsh
MVP Honored Contributor
Neil, thanks for the example. It seems like it would fix the issue but I'm still having an issue.  When I run this code now, it tells me that the table can't be found (from the feature layer).  Here is my code:

Private Sub btnImportArcMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportArcMap.Click

        Try

            Dim pObjFactory As IObjectFactory = DirectCast(m_pApp, IObjectFactory)
            Dim FileGDBType As Type = GetType(FileGDBWorkspaceFactoryClass)
            Dim typeCLsID As String = FileGDBType.GUID.ToString("B")
            Dim filename As String = "C:\Visualization.gdb\erasemepleaseBilling"
            Dim filepath As String = System.IO.Path.GetDirectoryName(filename)

            Dim pWorkspaceFactory As IWorkspaceFactory = DirectCast(pObjFactory.Create(typeCLsID), IWorkspaceFactory)
            Dim pFeatureWorkspace As IFeatureWorkspace = DirectCast(pWorkspaceFactory.OpenFromFile(filepath, 0), IFeatureWorkspace)

            Dim pFLayer As IFeatureLayer = DirectCast(pObjFactory.Create("esriCarto.FeatureLayer"), IFeatureLayer)
            pFLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(filename)
            pFLayer.Name = pFLayer.FeatureClass.AliasName

            Dim pBasicDocument As IBasicDocument = DirectCast(m_pApp.Document, IBasicDocument)
            pBasicDocument.AddLayer(pFLayer)
            pBasicDocument.UpdateContents()


        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub


Can you tell me if there is something that I'm missing?

Thanks!
0 Kudos
AdrianWelsh
MVP Honored Contributor
a hopeful bump!
0 Kudos
NeilClemmons
Honored Contributor
Sorry, ESRI's new forums suck and I don't get email notifications anymore.  The OpenFeatureClass method expects just the name of the feature class.  It looks like you're passing in a full path.  Strip off the directory path and it should be fine.
0 Kudos
AdrianWelsh
MVP Honored Contributor
Thanks Neil!  That works now.  I wish there was some kind of way to reward you or give you points.  I sure hope that ESRI is working on improving this forum site...
0 Kudos