ArcMap Add-in development example

1.  Development environment

 Windows 7 SP1 64bit

 ArcGIS 10.2.2

 Microsoft Visual Studio 2010(C# 4.0)

 .NET Framework 4.0

 

Second,  introductory examples

1.  How to Create Simple Tools

1)  Create

Create a project in Visual Studio 2010 , select the template path as Visual C# -- ArcGIS – Desktop Add-Ins – ArcMap Add-in, customize the project name and code location , and then proceed to ArcGIS Add-Ins development wizard.

 

In the project wizard , set the basic information of the Add-in plug-in , and go to the next step .

 

Set the Add-in type , select Tool , and set the basic information to complete the setting.

 

2)  Compile

The wizard has automatically added some necessary ArcGIS Engine component references to the project, and automatically generated some code.

 

Execute the compilation and a .esriAddIn file will be generated .

 

3)  Installation

Double-click the .esriAddIn file to install it.

 

Prompt that the installation was successful.

 

4)  call

In ArcMap , open the Customize Mode menu , switch to the Commands tab, and you can see the function just installed in Add-In Controls .

 

Create a custom Toolbar .

 

Drag the created Command to the Toolbar to use it .

 

5)  Remove

Add-In plugins that are no longer used can be deleted in the Add-In Manager .

 

2.  How to carry out function development

1)  Understand virtual classes

Some common methods are defined in the virtual class Tool . We only need to implement some or all of the methods according to actual needs to realize the functions of the tool .

 

2)  Implement virtual methods

This example will implement a function that draws a polyline in MapControl and displays its coordinates.

 

3)  Functional testing and verification

After the development is completed, compile , install, open ArcMap , and execute the function.

 

The coordinates of the drawn polyline are displayed, and the function is correct .

 

 

 

 

appendix

The source code for the ShowCoordinates class in this example is as follows : 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.Display;

namespace ArcMapAddin1
{
    public class ShowCoordinates : ESRI.ArcGIS.Desktop.AddIns.Tool
    {
        private ESRI.ArcGIS.Display.INewLineFeedback m_pNewLineFeedback = null;
        public ShowCoordinates()
        {
        }

        protected override void OnUpdate()
        {
            try
            {
                Enabled = true;
            }
            catch { }
            finally
            {
                base.OnUpdate();
            }
            //Enabled = ArcMap.Application != null;
        }

        protected override void OnActivate()
        {
            base.OnActivate();
            Cursor = System.Windows.Forms.Cursors.Cross;
        }

        protected override bool OnDeactivate()
        {
            Cursor = System.Windows.Forms.Cursors.Arrow;
            return base.OnDeactivate();
        }

        protected override void OnDoubleClick()
        {
            Func<ESRI.ArcGIS.Geometry.IPolyline, string> PolylineToString = (aPolyline) =>
            {
                string sResult = "";
                if (null != aPolyline)
                {
                    ESRI.ArcGIS.Geometry.IPointCollection pPointCollection = aPolyline as ESRI.ArcGIS.Geometry.IPointCollection;
                    int nCount = pPointCollection.PointCount;
                    for (int n = 0; n < nCount; n++)
                    {
                        ESRI.ArcGIS.Geometry.IPoint aPoint = pPointCollection.get_Point(n);
                        sResult += string.Format("{0} {1}, ", aPoint.X, aPoint.Y);
                    }
                    if (1 < sResult.Length)
                        sResult = sResult.Substring(0, sResult.Length - 2);
                }
                return sResult;
            };

            if (m_pNewLineFeedback != null)
            {
                ESRI.ArcGIS.Geometry.IPolyline pPolyline = m_pNewLineFeedback.Stop();
                m_pNewLineFeedback = null;
                System.Windows.Forms.MessageBox.Show(PolylineToString(pPolyline));
            }
        }

        protected override void OnMouseDown(MouseEventArgs arg)
        {
            if (arg.Button == System.Windows.Forms.MouseButtons.Left)
            {
                ESRI.ArcGIS.Geometry.IPoint pCursorPoint = ArcMap.Document.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
                if (m_pNewLineFeedback == null)
                {
                    m_pNewLineFeedback = new NewLineFeedbackClass();
                    IRgbColor pRGB = new RgbColorClass();
                    ISimpleLineSymbol pSimpleLineSymbol = m_pNewLineFeedback.Symbol as ISimpleLineSymbol;
                    pRGB.Red = 255;
                    pSimpleLineSymbol.Color = pRGB;
                    pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
                    pSimpleLineSymbol.Width = 2;
                    m_pNewLineFeedback.Symbol = pSimpleLineSymbol as ISymbol;
                    m_pNewLineFeedback.Display = ArcMap.Document.ActiveView.ScreenDisplay;
                    m_pNewLineFeedback.Start(pCursorPoint);
                }
                else
                {
                    m_pNewLineFeedback.AddPoint(pCursorPoint);
                }
            }
        }

        protected override void OnMouseUp(MouseEventArgs arg)
        {
            if (arg.Button == System.Windows.Forms.MouseButtons.Right)
            {
                OnDoubleClick();
            }
        }

        protected override void OnMouseMove(MouseEventArgs arg)
        {
            if (m_pNewLineFeedback != null)
            {
                ESRI.ArcGIS.Geometry.IPoint pCursorPoint = ArcMap.Document.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
                m_pNewLineFeedback.MoveTo(pCursorPoint);
            }
        }
    }
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325547168&siteId=291194637
Recommended