ArcObjects: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
mNo edit summary
Brian Wilson (talk | contribs)
 
(16 intermediate revisions by the same user not shown)
Line 3: Line 3:
== Getting off the ground ==
== Getting off the ground ==


The first time is the hardest...  even just knowing where to start.
I started this page in 2008 and this statement is still just as true:
"Just knowing where to start with ArcObjects is a pain."


I am using Visual Studio 2005 + ArcGIS Desktop 9.2 and programming in C#.
What's changed since then? There is now a "Community" (free) version of [https://visualstudio.com/vs/ Visual Studio] 2017. This is the real deal not just an editor.


For someone conversant in C# and Visual Studio, it's hard because the only book I can find is "Getting To Know ArcObjects" which spends the first 1/2 of its content telling you how to drop an icon into a toolbar. Then it spends the second 1/2 on Visual Basic examples that don't interest me.
This is also still true: there is a dearth of written materials on ArcObjects or ArcGIS+C# programming.
In 2008 I said: "The only book I can find is "Getting To Know ArcObjects" which spends the first 1/2 of its content telling you how to drop an icon into a toolbar. Then it spends the second 1/2 on Visual Basic examples. In other words, it's useless."
There are several other (outdated) books now, all written for Visual Basic and therefore useless.


What I was looking for was a wedge to drive into ArcObjects to split it open. "Getting To Know ArcObjects" was a major disappointment. Good thing I borrowed it from the library so I did not have to waste my own money on it.
This one holds promise, I am ordering a copy: "Beginning ArcGIS for Desktop Development using .NET"
 
Stackexchange recommends online resources. Warning: ESRI offers 99% Javascript and Python classes. Dig dig dig and you will eventually find a free video and a $1700 instructor-led class (online), both geared for ArcGIS Pro. Come prepared with advanced knowledge of Visual Studio and C#. Let me know how that goes for you.
 
Another place to look on Youtube for IGeometry. I have a hard time sitting still for videos but many people don't, apparently.
 
It's just not a hot topic.


=== Developer samples are helpful ===
=== Developer samples are helpful ===
Line 16: Line 25:
There are lots of C# samples. Too many in fact - they are all sort of just dropped into the directory with long names like "NAServerGeocodeRouteWebService"  and little documentation. Dear ESRI: a table of contents would have been '''SO USEFUL'''.
There are lots of C# samples. Too many in fact - they are all sort of just dropped into the directory with long names like "NAServerGeocodeRouteWebService"  and little documentation. Dear ESRI: a table of contents would have been '''SO USEFUL'''.


The Developer Samples are not a wedge either. More of a deluge. You still have to know something to get started.  
The samples are a good place to start but the EDN site was better.  


=== EDN works for me ===
=== EDN worked for me ===


Finally I found a useful document on [http://edn.esri.com/ EDN], the ESRI Developer Network site.
Finally I found a useful document on [http://edn.esri.com/ EDN], the ESRI Developer Network site. The entire site is gone now, so it's no longer available at the link I had here. ESRI does that. They use file names like
01c01659-cdf8-4579-9c87-2b965e872d84.htm so you can't even search for the doc once they move it. Sorry.


The page that got me running was this one: (Great name, huh?) http://edndoc.esri.com/arcobjects/9.2/NET/01c01659-cdf8-4579-9c87-2b965e872d84.htm
Try this one instead.
[http://desktop.arcgis.com/en/arcobjects/latest/net/webframe.htm#5326e840-a76e-48c7-809a-90191f5bf3bb.htm Walkthrough: Building custom UI elements using add-ins (ArcObjects .NET 10.6 SDK)]


I have successfully built a sample command and run it in the debugger. It just lists out the names of every layer in the current map in a Windows Form Listbox. I am now well on my way with my first project, which will be a "project publisher" for ArcReader.
I have successfully built a sample command and run it in the debugger. It just lists out the names of every layer in the current map in a Windows Form Listbox.


=== Code snippets are really handy ===
=== Code snippets are really handy ===
Line 30: Line 41:
The best tip from the above-mentioned EDN doc is to use the "code snippets" feature in Visual Studio to tap into the code provided with the developer kit.
The best tip from the above-mentioned EDN doc is to use the "code snippets" feature in Visual Studio to tap into the code provided with the developer kit.
They are a big help!
They are a big help!
== Finding the fields in a layer ==
=== Finding fields using a workspace ===
Here is a sample windows app that gets a list of fields from a layer.
I dislike the fact that it wants a Windows form handle, makes it hard to write a command line version!
<pre>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesFile;
namespace EngineWindowsApplication1
{
    public partial class Form1 : Form
    {
        private BindingSource bindingSource1 = new BindingSource();
        public Form1()
        {
            InitializeComponent();
            FeatureWorkspace fw = new FeatureWorkspace(@"D:\AGIProducts\IncidentViewIII\SampleData\IncidentView.mdb", this.Handle);
            List<IField> fields = fw.GetFields("Addresses");
        }
    }
    public class FeatureWorkspace
    {
        IFeatureWorkspace featureworkspace;
        /// <summary>
        ///
        /// </summary>
        /// <param name="workspace">Path to geodatabase</param>
        /// <param name="handle">Handle to parent form</param>
        public FeatureWorkspace(string workspace, IntPtr handle)
        {
            IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)new AccessWorkspaceFactory();
            featureworkspace = workspaceFactory.OpenFromFile(workspace, handle.ToInt32()) as IFeatureWorkspace;
        }
        public List<IField> GetFields(string layer)
        {
            List<IField> list = new List<IField>();
            // Add support for other workspace types here??
            ITable foundITable = featureworkspace.OpenFeatureClass(layer) as ITable;
            IFields fields = foundITable.Fields;
            for (int fieldcount = 0; fieldcount < fields.FieldCount; fieldcount++)
            {
                IField f = fields.get_Field(fieldcount);
                list.Add(f);
            }
            return list;
        }
    }
}
</pre>
=== Finding fields using a map object ===
add code here...


== ArcReader Project publisher ==
== ArcReader Project publisher ==
This is a project idea I had a while ago...
=== As a tool within ArcMap ===


Read the currently open MXD file and publish the map as a PMF file. Then  
Read the currently open MXD file and publish the map as a PMF file. Then  
Line 42: Line 131:
It might be interesting to embed the Python in the output so the manifest is runnable.
It might be interesting to embed the Python in the output so the manifest is runnable.


== Other relevant pages ==
=== As a command (runnable from Python scripts) ===
 
http://edndoc.esri.com/arcobjects/9.2/NET/0D300185-2025-4FA9-A4FF-FEBD0FE4C298.htm
 
== Related pages ==


[[Windows programming]]
[[Windows programming]]


== Mapbook ==
[[Category: GIS]]
 
The client wants to be able to export a series of maps to PDF's using a column of data from a table on each map; if you change the extent of the map and click print it creates a new series.

Latest revision as of 16:02, 13 April 2018

Some ArcObjects projects that I am developing.

Getting off the ground

I started this page in 2008 and this statement is still just as true: "Just knowing where to start with ArcObjects is a pain."

What's changed since then? There is now a "Community" (free) version of Visual Studio 2017. This is the real deal not just an editor.

This is also still true: there is a dearth of written materials on ArcObjects or ArcGIS+C# programming. In 2008 I said: "The only book I can find is "Getting To Know ArcObjects" which spends the first 1/2 of its content telling you how to drop an icon into a toolbar. Then it spends the second 1/2 on Visual Basic examples. In other words, it's useless." There are several other (outdated) books now, all written for Visual Basic and therefore useless.

This one holds promise, I am ordering a copy: "Beginning ArcGIS for Desktop Development using .NET"

Stackexchange recommends online resources. Warning: ESRI offers 99% Javascript and Python classes. Dig dig dig and you will eventually find a free video and a $1700 instructor-led class (online), both geared for ArcGIS Pro. Come prepared with advanced knowledge of Visual Studio and C#. Let me know how that goes for you.

Another place to look on Youtube for IGeometry. I have a hard time sitting still for videos but many people don't, apparently.

It's just not a hot topic.

Developer samples are helpful

Next I spent some time exploring the sample code buried in my development system at C:/Program Files/ArcGIS/DeveloperKit/SamplesNET/Desktop There are lots of C# samples. Too many in fact - they are all sort of just dropped into the directory with long names like "NAServerGeocodeRouteWebService" and little documentation. Dear ESRI: a table of contents would have been SO USEFUL.

The samples are a good place to start but the EDN site was better.

EDN worked for me

Finally I found a useful document on EDN, the ESRI Developer Network site. The entire site is gone now, so it's no longer available at the link I had here. ESRI does that. They use file names like 01c01659-cdf8-4579-9c87-2b965e872d84.htm so you can't even search for the doc once they move it. Sorry.

Try this one instead. Walkthrough: Building custom UI elements using add-ins (ArcObjects .NET 10.6 SDK)

I have successfully built a sample command and run it in the debugger. It just lists out the names of every layer in the current map in a Windows Form Listbox.

Code snippets are really handy

The best tip from the above-mentioned EDN doc is to use the "code snippets" feature in Visual Studio to tap into the code provided with the developer kit. They are a big help!

Finding the fields in a layer

Finding fields using a workspace

Here is a sample windows app that gets a list of fields from a layer. I dislike the fact that it wants a Windows form handle, makes it hard to write a command line version!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesFile;

namespace EngineWindowsApplication1
{
    public partial class Form1 : Form
    {
        private BindingSource bindingSource1 = new BindingSource();

        public Form1()
        {
            InitializeComponent();

            FeatureWorkspace fw = new FeatureWorkspace(@"D:\AGIProducts\IncidentViewIII\SampleData\IncidentView.mdb", this.Handle);
            List<IField> fields = fw.GetFields("Addresses");
        }
    }

    public class FeatureWorkspace
    {
        IFeatureWorkspace featureworkspace;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="workspace">Path to geodatabase</param>
        /// <param name="handle">Handle to parent form</param>
        public FeatureWorkspace(string workspace, IntPtr handle)
        {
            IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)new AccessWorkspaceFactory();
            featureworkspace = workspaceFactory.OpenFromFile(workspace, handle.ToInt32()) as IFeatureWorkspace;
        }

        public List<IField> GetFields(string layer)
        {
            List<IField> list = new List<IField>();

            // Add support for other workspace types here??
            ITable foundITable = featureworkspace.OpenFeatureClass(layer) as ITable;
            IFields fields = foundITable.Fields;
            for (int fieldcount = 0; fieldcount < fields.FieldCount; fieldcount++)
            {
                IField f = fields.get_Field(fieldcount);
                list.Add(f);
            }

            return list;
        }
    }
}

Finding fields using a map object

add code here...

ArcReader Project publisher

This is a project idea I had a while ago...

As a tool within ArcMap

Read the currently open MXD file and publish the map as a PMF file. Then generate a manifest for a map containing a listing of all files required to reproduce it. This file should be usable by an external Python script to copy the files to a distributable cdrom as an ArcReader project.

If there are any unmanaged raster catalogs then include the list of rasters.

Report any problems such as layers that will not be reachable (outside the current directory tree.)

It might be interesting to embed the Python in the output so the manifest is runnable.

As a command (runnable from Python scripts)

http://edndoc.esri.com/arcobjects/9.2/NET/0D300185-2025-4FA9-A4FF-FEBD0FE4C298.htm

Related pages

Windows programming