2

enter image description hereI am trying to open a dialog box which shows up only raster datasets so that I can select one and create Hill shade based on it. But I am facing a run time error for Dialog box when I click on the addin button. Could any one please help me out regarding this

Protected Overrides Sub OnClick()
    '
    '  TODO: Sample code showing how to access button host
    '
    My.ArcMap.Application.CurrentTool = Nothing

    Dim pMxDoc As IMxDocument = My.ArcMap.Document
    Dim pMap As IMap = pMxDoc.FocusMap
    Dim bObjectSelected As Boolean
    Dim pGxDialog As IGxDialog = New GxDialog
    'only show raster datasets
    Dim pRasterFilter As IGxObjectFilter = New GxFilterRasterDatasets
    'Layers and document declaration
    Dim pGxObjects As IEnumGxObject = Nothing
    Dim pGxDataset As IGxDataset
    Dim pRasterLayer As IRasterLayer

    Try
        With pGxDialog
            .AllowMultiSelect = False
            .StartingLocation = "C:\Users\rxm160030\Downloads\Lab12\Data"
            .Title = "Select Raster datasets to Add to Map"
            .ObjectFilter = pRasterFilter
            .DoModalOpen(My.ArcMap.Application.hWnd, pGxObjects)
        End With

        'If no file selected, exit
        If bObjectSelected = Nothing Then Exit Sub

        'Get first dataset
        pGxDataset = pGxObjects.Next
        'no dataset selected
        If pGxDataset Is Nothing Then Exit Sub
        'iterate through remaining datasets
        Do Until pGxDataset Is Nothing
            pRasterLayer = New RasterLayer
            'assign dataset to rasterLayer
            Dim pInputRaster As IRaster = pRasterLayer.Raster
            'assign dataset name to rasterLayer
            pRasterLayer.Name = pGxDataset.Dataset.Name
            ' Do something with the featureLayer
            Dim choice1 As String = InputBox("Enter Azimuth")
            Dim choice2 As String = InputBox("Enter Altitude")

            Dim pSurfaceOp As ISurfaceOp = New RasterSurfaceOp
            Dim pEnv As IRasterAnalysisEnvironment = pSurfaceOp
            Dim pWSF As IWorkspaceFactory = New RasterWorkspaceFactory
            Dim pWS As IWorkspace = pWSF.OpenFromFile("C:\Users\rxm160030\Downloads\Lab12\Data", 0)
            pEnv.OutWorkspace = pWS
            Dim pOutput As IGeoDataset = pSurfaceOp.HillShade(pInputRaster, choice1, choice2, True)

            Dim pOutLayer As IRasterLayer = New RasterLayer
            pOutLayer.CreateFromRaster(pOutput)
            pOutLayer.Name = "HillShade"
            pMap.AddLayer(pRasterLayer)
            pMap.AddLayer(pOutLayer)
            ' Go to next dataset
            pGxDataset = pGxObjects.Next
        Loop
    Catch ex As Exception
        MsgBox("BtnAdd_Click Exception: " & ex.Message)
    Finally
        ' Release ArcObjects objects
        pGxObjects = Nothing
        pRasterFilter = Nothing
        pGxDialog = Nothing
        pGxDataset = Nothing
        pRasterLayer = Nothing
        ' Call garbage collection
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Try
    pMxDoc.ActiveView.PartialRefresh(esriViewGeography, Nothing, Nothing)
End Sub
  • Please always provide error messages as text rather than pictures so that they are available to future searches. – PolyGeo Apr 14 '17 at 5:41
2

The code you have should work but it looks like the references are messed up. Instead of referencing ESRI.ArcGIS.CatalogUI it is setting your dialog object to a type from Lab12_Q2.dll. Try explicitly telling it to use ESRI.ArcGIS.CatalogUI by writing that line of code this way:

Dim pGxDialog As ESRI.ArcGIS.CatalogUI.IGxDialog = New ESRI.ArcGIS.CatalogUI.GxDialog
1

First, have you tried an explicit cast from (new GxDialog) to IGxDialog. I definitely encounter situations where the I have to explicitly cast to the interface to get things working.

Secondly, I would expect these types to reside in ESRI.ArcGIS.CatalogUI reference. Is that reference included in your current project?

  • Yes, its included – Rohit Apr 16 '17 at 19:50
  • 1
    Have you tried defining the variable like this: Dim pGxDialog As ESRI.ArcGIS.CatalogUI.IGxDialog = New ESRI.ArcGIS.CatalogUI.GxDialog – danielm Apr 18 '17 at 17:46
  • Yes, @danielm is right. Didn't see your comment until I submitted my answer. – Rich Wawrzonek Apr 18 '17 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.