Showing posts with label ArcObjects. Show all posts
Showing posts with label ArcObjects. Show all posts

Friday, November 25, 2011

Get Selected Layer From TOC (ArcObjects)

Getting the selected layer a user is using can be an easy way to have the user interact with ArcMap without having them mull through some form.  This is be achieved in a couple of line of code:

IContentsView contentsView = mapDocument.CurrentContentsView;
object selectedItem = contentsView.SelectedItem;
if (!(selectedItem is ILayer))
{
   return;
}


First the code assumes you know how to get the current map document's reference and second it not only gets the selected item, I provided a simple check that ensures it's a layer object.

Enjoy

Friday, June 17, 2011

AddIn Extension - Update Document Properties

A way to keep track of who accesses map documents is to create an extension that logs who last used the map document.  To do this, you need to wire events to the OpenDocument event.


        private void WireDocumentEvents()
        {
            // Named event handler
            ArcMap.Events.OpenDocument += new IDocumentEvents_OpenDocumentEventHandler(Events_OpenDocument);
         
        }

Call this event OnStartup(), so override it in the extension:

        protected override void OnStartup()
        {
            // Wire the events
             WireDocumentEvents();
        }

Next stub out the Events_OpenDocument()


        void Events_OpenDocument()
        {
            IMxDocument mxDoc = ArcMap.Document as IMxDocument;
            IDocumentInfo2 docInfo = mxDoc as IDocumentInfo2;
            docInfo.Comments += Environment.NewLine + "Opened On: " + DateTime.Now.ToString() + " by " + Environment.UserName;
            if (docInfo.RelativePaths == false)
            {
                docInfo.RelativePaths = true;
            }
        }

Now if you look at the Map Documents properties in ArcMap you'll see something like:
"Opened On: 06/17/2011 3:02 AM by Bob1234"

An additional option would be to tack in a Save() to save the document right away so the information doesn't get lost if the user exits ArcMap.


        private void Save()
        {
            ArcMap.Application.SaveDocument();
        }

Thursday, October 7, 2010

Searching Esri ArcObject Help With Google

Here is a great tip I've picked up over the last couple of months.  When working with the online help system for ArcObjects, you can sometimes get tons of search results for unrelated topics.  For example, say you search Point or Polygon, you will get about 10,000 results from the forums, other help topics.  It's basicly information overload!

Let's say you need to search the .NET ArcObjects help, so to streamline the search process, you can utilize Google's search engine by doing the following:
  1. In order for google to work, you need the site.  For this example, we will use http://help.arcgis.com/en/sdk/10.0/arcobjects_net/ao_home.html. You can to remove the http:// and the ao_home.html for your site reference.
    • The result should be this: help.arcgis.com/en/sdk/10.0/arcobjects_net
  2. With our site in hand, navigate to google (http://www.google.com/) and create a new search entry as follows: "site:help.arcgis.com/en/sdk/10.0/arcobjects_net TEST" 
  3. Copy the resulting URL and ignore the search results
For IE:
  1. Go to: http://www.microsoft.com/windows/ie/searchguide/en-en/default.mspx
  2. Paste the resulting Google Search URL into the URL box
  3. Give it a descriptive name. Ex: Search .NET ArcObjects
  4. Click install
Enjoy