create feature class in an add-in

859
5
03-07-2012 01:29 PM
ChrisBradberry
Occasional Contributor
Hey,

I think that I figured out the problem that I am having, but not the solution.  I have an add-in that creates a feature class if it does not exist.  Sometimes it works and sometimes it gives a com error. I finally came to the conclusion that when the default.gdb (or the outpath) has a path in the name (as in windows xp default location) the createfeatureclass_management call has a com error.  When you work on a windows 7 computer without a space in the default.gdb it works fine.  Of course the developer computer was windows 7 and always worked, but the users computers had failures. 

My question is how do you submit the CreateFeatureClass_Management geoprocess when the outpath path has a space in it?  I know you can do it from ArcMap, but how does it work in the add-in?

Thanks, Chris
0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor
This is how I create a new feature class using the Geoprocessor in my add-in

    Friend Function CreateFeatureClass(ByVal FCLocation As String, ByVal FCName As String, ByVal pSR As ESRI.ArcGIS.Geometry.ISpatialReference3, ByVal GeometryType As String) As ESRI.ArcGIS.Geodatabase.IFeatureClass

        Dim CreateFClass As New ESRI.ArcGIS.DataManagementTools.CreateFeatureclass
        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
                releaser.ManageLifetime(CreateFClass)

                CreateFClass.out_path = FCLocation
                CreateFClass.out_name = FCName
                CreateFClass.spatial_reference = pSR
                CreateFClass.geometry_type = GeometryType

                Result = RunTool(CreateFClass, Nothing, True)
                If Result Is Nothing Then Return Nothing

                Return ReturnObjectfromResult(Result, "Feature Class")

            End Using
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Create Feature Class")
            Return Nothing
        End Try

    End Function

    Friend Function ReturnObjectfromResult(ByVal result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Type As String) As Object

        Dim GPVal As ESRI.ArcGIS.Geodatabase.IGPValue
        Dim InMemFC As String
        Dim GPUtil As ESRI.ArcGIS.Geoprocessing.IGPUtilities3 = New ESRI.ArcGIS.Geoprocessing.GPUtilities

        Try
            GPVal = result.GetOutput(0)
            InMemFC = GPVal.GetAsText()

            Select Case Type
                Case "Feature Class"
                    Return GPUtil.OpenFeatureClassFromString(InMemFC)
                Case "Table"
                    Return GPUtil.OpenTableFromString(InMemFC)
                Case "Feature Layer"
                    Return GPUtil.OpenFeatureLayerFromString(InMemFC)
            End Select

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Return FeatureClass error")
            Return Nothing
        End Try

    End Function

    Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel, Optional ByVal AddOutput As Boolean = False) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult

        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult

        Try
            System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor

            Dim GP as New ESRI.ArcGIS.Geoprocessor.Geoprocessor

            GP.OverwriteOutput = True
            GP.AddOutputsToMap = AddOutput

            Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult)
            If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then
                Return Nothing
            End If
            Return Result
        Catch ex As Exception
            Return Nothing
        End Try

    End Function
0 Kudos
ChrisBradberry
Occasional Contributor
Ken,

Thanks, I will try that out and let you know.

Chris
0 Kudos
ChrisBradberry
Occasional Contributor
Ken,

The code works for a feature class path that does not have a space in it, but still throws a com error when the feature class path has a space in it.  There should be a way around this because the tool in ArcMap will work when the path has a space in it.

Any other ideas?

Chris
0 Kudos
KenBuja
MVP Esteemed Contributor
I just ran a test with the application that uses this code and I didn't receive an error when I created a new dataset in the directory C:\temp\New Folder\. This is on a Win 7 machine.

Where are you getting the error?
0 Kudos
ChrisBradberry
Occasional Contributor
Ken,

I am using a win 7 machine too, and get the error when I try to create the feature class. I have elevated this to tech support.  I imagine that I have used some bad coding practices.  Thanks for the help.

Chris
0 Kudos