Select to view content in your preferred language

Start ArcMap programmatically, run code at startup in C#

2714
10
10-28-2010 06:01 AM
CarlosPiccirillo
Emerging Contributor
Hi everyone,

I have a dozen mxds that I run during the night from Windows Task Scheduler. They start ArcMap automatically, run code to process some data and then shut down. Now I need to convert these from VBA to C# since VBA will no longer be supported at ArcGIS 10 and I am lost on how to start ArcMap, run code and lastly shut down ArcMap programmatically. I've seen a few post both here and the old forms on how to start ArcMap automatically but nothing on how to run code at start up. In the VBA code, there is code in the MxDocument_OpenDocument function to run the entire thing including shutdown. But in a standalone C# application, there is no such thing as MxDocument_OpenDocument so how do I get my code to run at start up?

Thanks,
Carlos
0 Kudos
10 Replies
AlexanderGray
Honored Contributor
Well there is at least 3-4 more elegant ways of doing this sort of thing depending on the nature of the operations the code is doing.  However if you just want to migrate changing the least things to your design, there is a MxDocument_OpenDocument event in C#.  You need to create an ArcMap extension and create a listener in the oncreate event of the extension to the IDocumentEvents OpenDocument event and put your code in there.  Putting the code in the oncreate of the event is a possibility too but typically ArcMap is not finished loading at that time.
0 Kudos
NeilClemmons
Honored Contributor
If all you're doing is processing data then you don't need ArcMap at all.  Just write a standalone program that accesses the data directly and does what it needs to do.  If you need information from the map document, use IMapDocument to open it.
0 Kudos
CarlosPiccirillo
Emerging Contributor
Alexander,

Thanks for the reply! I've only done one extension before and it was quite a struggle to learn it. I was hoping to avoid having to create an extension for it. I don't mind changing the design if I can find a better way of doing it so if you have other ideas, I'm all ears. Thanks, Carlos!

Well there is at least 3-4 more elegant ways of doing this sort of thing depending on the nature of the operations the code is doing.  However if you just want to migrate changing the least things to your design, there is a MxDocument_OpenDocument event in C#.  You need to create an ArcMap extension and create a listener in the oncreate event of the extension to the IDocumentEvents OpenDocument event and put your code in there.  Putting the code in the oncreate of the event is a possibility too but typically ArcMap is not finished loading at that time.
0 Kudos
CarlosPiccirillo
Emerging Contributor
Neil,

Thanks for the reply. The processes the program does is merge several layers into one, create a new layer that buffers all the polygons in the merged layer and then does a spatial query between an area of concern layer with the merged layer as well as the buffered layer. Lastly, it gets lot information from Oracle tables, puts everything into an Access database and makes automatic exhibits for each polygon falling in an area of concern. I probably don't need ArcMap for some of the steps, but I'm pretty sure I need it for the spatial queries and exhibits.

Carlos

If all you're doing is processing data then you don't need ArcMap at all.  Just write a standalone program that accesses the data directly and does what it needs to do.  If you need information from the map document, use IMapDocument to open it.
0 Kudos
NeilClemmons
Honored Contributor
Neil,

Thanks for the reply. The processes the program does is merge several layers into one, create a new layer that buffers all the polygons in the merged layer and then does a spatial query between an area of concern layer with the merged layer as well as the buffered layer. Lastly, it gets lot information from Oracle tables, puts everything into an Access database and makes automatic exhibits for each polygon falling in an area of concern. I probably don't need ArcMap for some of the steps, but I'm pretty sure I need it for the spatial queries and exhibits.

Carlos


I'm not sure what you mean by exhibit but it's highly unlikely you need ArcMap.  The IMapDocument interface allows you to open a document and access the map, the layout, the map layers, etc.  You can make changes and save those changes.
0 Kudos
CarlosPiccirillo
Emerging Contributor
By exhibit I mean creating a layout with predetermined elements, (scale bar, tittle, etc.) and exporting it as a jpg.

You have me VERY intrigued. Do you have sample code in either C#, VB or VBA where you use IMapDocument to do some of things I mentioned without opening ArcMap?

I'm not sure what you mean by exhibit but it's highly unlikely you need ArcMap.  The IMapDocument interface allows you to open a document and access the map, the layout, the map layers, etc.  You can make changes and save those changes.
0 Kudos
SteveFang
Deactivated User
The code is in VB .Net but you can easily port it to c#.  It's a console application that accepts a parameter (MXD name) and exports the layout of the MXD to PDF.  Good luck.

Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Output
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Geometry
Imports System.io

Module ExtractPdf

    Sub Main()
        Dim ao As IAoInitialize = New AoInitialize
        Dim pMapDoc As IMapDocument = New MapDocument
        Dim pParameter As String() = Environment.GetCommandLineArgs
        Dim theMap As String = pParameter(1)
        Dim thePath As String
        Dim theFileName As String
        Dim theWidth As Double
        Dim theHeight As Double

        Dim pExportPDF As IExportPDF = New ExportPDF
        Dim pExport As IExport = pExportPDF
        Dim pExportVectorOptionEx As IExportVectorOptionsEx
        Dim pPixEnv As IEnvelope = New Envelope
        Dim pRect As tagRECT
        Dim hDc As Integer
        Dim pLayoutView As IActiveView

        Dim writer As StreamWriter

        Try
            If theMap Is Nothing Then
                Exit Sub
            End If

            'Get output path and output file name
            thePath = System.IO.Path.GetDirectoryName(theMap)
            theFileName = System.IO.Path.GetFileNameWithoutExtension(theMap)

            If File.Exists(thePath & "\tilecache_error.log") Then
                writer = File.AppendText(thePath & "\tilecache_error.log")
            Else
                writer = New StreamWriter(thePath & "\tilecache_error.log")
            End If

            If ao.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcInfo) = esriLicenseStatus.esriLicenseAvailable Then
                ao.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo)

                If pMapDoc.IsMapDocument(theMap) Then
                    'Open map and obtain layout
                    pMapDoc.Open(theMap)
                    pLayoutView = pMapDoc.PageLayout

                    'Get page width and height
                    pMapDoc.PageLayout.Page.QuerySize(theWidth, theHeight)

                    'Set up export parameters
                    pExport.ExportFileName = thePath & "\" & theFileName & ".pdf"
                    pExport.Resolution = 100

                    pExportVectorOptionEx = pExportPDF
                    pExportVectorOptionEx.ExportPictureSymbolOptions = esriPictureSymbolOptions.esriPSOVectorize

                    pRect.left = 0
                    pRect.top = 0
                    pRect.right = theWidth * pExport.Resolution
                    pRect.bottom = theHeight * pExport.Resolution

                    pPixEnv.PutCoords(pRect.left, pRect.top, pRect.right, pRect.bottom)
                    pExport.PixelBounds = pPixEnv

                    'Start export of layout
                    hDc = pExport.StartExporting
                    pLayoutView.Output(hDc, pExport.Resolution, pRect, Nothing, Nothing)
                    pExport.FinishExporting()
                Else
                    writer.WriteLine("Export PDF (" & Now & ") - " & theFileName & " - Input map parameter is not an ArcGIS map document.")
                    writer.Close()
                End If
            Else
                writer.WriteLine("Export PDF (" & Now & ") - " & theFileName & " - Can not obtain an ArcInfo license.")
                writer.Close()
                Exit Sub
            End If

            ao.Shutdown()
        Catch ex As Exception
            ao.Shutdown()
            writer.WriteLine("Export PDF (" & Now & ") - " & theFileName & " - " & ex.Message)
        End Try
    End Sub

End Module
0 Kudos
CarlosPiccirillo
Emerging Contributor
Steve,

Thank you VERY much for the sample code, this should help a lot! I have VB.Net on this machine too so I'll get it working there first and then port it to C#.

Carlos
0 Kudos
CarlosPiccirillo
Emerging Contributor
Thanks everyone for your help! I think I am on the right track. I have been able to create a stand alone C# executeable that grabs a bunch of SDE layers, joins them to an Oracle table and creates new layers with the joined attributes. All without having to call up ArcMap! Now I have to expand it to do all the other things the VBA code did but with all of your help I've gotten this far and I REALLY appreciate everyones help!

Thanks,
Carlos
0 Kudos