TitlePages

Showing posts with label addin. Show all posts
Showing posts with label addin. Show all posts

Friday, 12 December 2014

How to create a simple python add-in tool for ArcGIS Desktop

How to create a simple python add-in for ArcGIS Desktop


TOOL USED:  Python Add-in Assistant

WORKING OF THE TOOL:
Creating a Simple python add-in assistant for ArcGIS Desktop to add a Fishnet table to the ArcMap using the python
Type of Python Add-in: Tool Type
Function: At the end of drawing a rectangle on the ArcMap a fishnet with specified rows and columns should appear on the Screen
A python add-in tool is very similar to a button. But, a tool requires user to interact with the map application based on that interaction some business logic will get executed. The Zoom In tool Description: Zoom In in ArcMap will be a great example, when the user draws a rectangle on the ArcMap ,then the map will be redrawn with that rectangle as an extent.
Below steps will help you to create both Toolbox as well as a Tool on it

PROCEDURE:
Steps to Create a Toolbar for your Tool:
1.     To create your own toolbar. Open the Add-n assistant and Go to Add-in Contents tab and give the information about the add-in which you are going to create.
Note: A tool can reside on a Toolbar but cannot sits on a Menu
2.     Click the Add-In Contents tab and begin declaring your add-in customizations. A tool can reside on a toolbar but not on a menu. See creating an add-in toolbar for more detailed procedure.
3.     To Create the tool on your new Toolbar
Right-click the new toolbar and choose New Tool on the right click Menu.
Python addin assistant window

At once you have finished giving out the information and details about the add-in. Click on Save button at the bottom of the wizard. All the necessary files and folders will get created within the working folder of your add-in assistant.
Now it is the time to work on the main code. Go to the python script located on the install folder and open it using your Python idle. Code will appear as shown below with the class name same as your button name.
Note: Do not change the button name as it is directly linked with the button and registered in the your Config.xml file

 
Onclick() methof of the addin button class
The Onclick () method shown on idle will get executed on when the user click on the tool . But I our case the fishnet should get created whenever the user draw the tool is selected and draw an rectangle on the Map. So to do that OnRectangle () method of the python add-in will comes in to Picture. Replace on click method with OnRectangle () method as shown below:


Fishnet content added to On Rectangle method


·         The code will check for any preexistence of fishnet on the memory using the Exist() Method and deletes if anyone is already there using the Delete_Management() function.
·         CreateFishnet_management is the core function which is going to do the actual work after supply all the necessary parameters to it
·         Refresh the active view to redraw the Map and return the path of the fishnet to the calling function.







Wednesday, 19 November 2014

Create a Display Flow Arrows Addin


ArcMAP ESRI Add-in for Display flow arrows for Utility Geometric Network


Software Required:
Microsoft Visual Studio 2010 +
ArcMap 10.2 or more
ArcObjects SDK for .Net Installed

Procedure:

  • Open Visual Studio 2010
  • Create a New Project > Visual C#> ArcGIS>Desktop Addins >ArcMap Add In
  • ArcMap Addin Wizard will appear on the Screen.Give a desired name for your Addin and click Next
  • In the Addin Types Enable Button checkbox and provide description for it.This will appear as a button tooltip in your ArcMap application.Click finish to end the wizard.
  • Now go the Solution Explorer(View>Soln Explorer) and open the Addinname.cs file if it doesn't appear in your screen.
  • Replace the content of the file with the following data.
  • Add the References(Right click references in Solution Explorer and select Add References) based on your classes usage


//Add the references in to the current namespace
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.EditorExt;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.NetworkAnalystTools;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;



namespace Flow
{
    public class FlowArrow : ESRI.ArcGIS.Desktop.AddIns.Button
    {
//Declare the interface Variable
        public IUtilityNetworkAnalysisExtFlow utilarr;
        public IUtilityNetworkAnalysisExt utilflow;
        public INetworkAnalysisExt netExt;

        public FlowArrow()
        {
//Get the reference to the Utility Analyst Toolbar using the UID Reference
            UID pUID = new UIDClass();
            pUID.Value = "esriEditorExt.UtilityNetworkAnalysisExt";
            netExt = ArcMap.Application.FindExtensionByCLSID(pUID) as INetworkAnalysisExt;

            UID uidutilflow = new UIDClass();
            uidutilflow.Value = "esriEditorExt.UtilityNetworkAnalysisExt";
            utilflow = ArcMap.Application.FindExtensionByCLSID(uidutilflow) as IUtilityNetworkAnalysisExt;

            
            
        }

        protected override void OnClick()
        {
            message m = new message();
    
            IGeometricNetwork gn = null;
            
            // get the current network

            IMxDocument mxd=ArcMap.Application.Document as IMxDocument;
            IMap map=mxd.FocusMap;
            IActiveView av = map as IActiveView;
            gn = netExt.CurrentNetwork;

            if (gn != null && utilflow!= null)
            {
                utilflow = netExt as IUtilityNetworkAnalysisExt;
                utilarr = utilflow as IUtilityNetworkAnalysisExtFlow;
                utilarr.ShowFlow = !(utilarr.ShowFlow);
                av.Refresh();
            }

       
            
            // refresh the display to update the flow direction arrows
            IActiveView mapView = ArcMap.Document.ActiveView;
            mapView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

        }
        protected override void OnUpdate()
        {
//Enable the Button once the operation is Successful
           //Enabled=True;
        }

        
   
    }

}

Code to get flow arrow using ArcObjects
Display flow arrow using Arcobjects

  • Save your Current project .Don't run your Code.
  • Go to Properties>Addinname Properties>Debug option.Set your ArcMap as the External program for debugging the code
  • Run the Code.
  • ArcMap Application will open.Now go to Customize>Customize mode>Command.Right now you will see your Addin name in the category list click on it.After that the button will get displayed on the commands window.
  • Drag the Button and put it in the Menu bar.
  • You get the ESRI Addin file bin>Debug folder of your project location
  • Now its party time :) Share your work with other through the Addin file