Create a point using a TOC template C#

770
0
03-31-2013 03:10 PM
AnthonyStevens
New Contributor II
I cobbled together some C# code from a couple of developer samples to make a tool that will create a new point leveraging a point template selected in the TOC.  The tool works (creating a new point and storing it in the template layer), but the new point isn't visible until I zoom in/zoom out or hit the refresh button.  The point also isn't selected after it is created.  I've tried refresh and partial refresh from IActiveView, but that didn't work or I didn't do it right.  Any help would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;



namespace Point
{
    [Guid("d0bfb058-7f77-4948-a57f-807cb6847a33")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Point.Class1")]
    public class CreatePoint : BaseTool, IShapeConstructorTool
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);
            FeatureConstructionPointTools.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);
            FeatureConstructionPointTools.Unregister(regKey);

        }

        #endregion
        #endregion


        private IApplication m_application;
        private IEditor3 m_editor;
        private IEditEvents_Event m_editEvents;
        private IEditEvents5_Event m_editEvents5;
        private IEditSketch3 m_edSketch;
        private IShapeConstructor m_csc;
        //private IActiveView activeView = null;

        public CreatePoint()
        {
          base.m_category = "Tool"; //localizable text 
          base.m_caption = "Dot";  //localizable text 
          base.m_message = "Dot";  //localizable text
          base.m_toolTip = "Dot";  //localizable text
          base.m_name = "Point_CreatePoint";   //unique id, non-localizable (e.g. "MyCategory_ArcMapTool")
          try
          {
            string bitmapResourceName = GetType().Name + ".bmp";
            base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            //base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");
          }
          catch (Exception ex)
          {
            System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
          }
        }

        public void AddPoint(IPoint point, bool Clone, bool allowUndo)
        {
            m_csc.AddPoint(point, Clone, allowUndo);
        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            m_csc.OnMouseDown(Button, Shift, X, Y);
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            m_csc.OnMouseMove(Button, Shift, X, Y);
        }

        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            m_csc.OnMouseUp(Button, Shift, X, Y);
        }

        public override bool OnContextMenu(int X, int Y)
        {
            return m_csc.OnContextMenu(X, Y);
        }

        public override void OnKeyDown(int keyCode, int Shift)
        {
            m_csc.OnKeyDown(keyCode, Shift);
        }

        public override void OnKeyUp(int keyCode, int Shift)
        {
            m_csc.OnKeyUp(keyCode, Shift);
        }

        public override void Refresh(int hDC)
        {
            m_csc.Refresh(hDC);
        }

        public override int Cursor
        {
            get { return m_csc.Cursor; }
        }

        public override void OnDblClick()
        {
            if (Control.ModifierKeys == Keys.Shift)
            {
                ISketchOperation pso = new SketchOperation();
                pso.MenuString_2 = "Finish Sketch Part";
                pso.Start(m_editor);
                m_edSketch.FinishSketchPart();
                pso.Finish(null);
            }
            else
                m_edSketch.FinishSketch();
        }
        
        public override bool Deactivate()
        {
            //unsubscribe events
            m_editEvents.OnSketchModified -= m_editEvents_OnSketchModified;
            m_editEvents5.OnShapeConstructorChanged -= m_editEvents5_OnShapeConstructorChanged;
            m_editEvents.OnSketchFinished -= m_editEvents_OnSketchFinished;
            return base.Deactivate();
        }

        public override void OnCreate(object hook)
        {
            m_application = hook as IApplication;

            //get the editor
            UID editorUid = new UID();
            editorUid.Value = "esriEditor.Editor";
            m_editor = m_application.FindExtensionByCLSID(editorUid) as IEditor3;
            m_editEvents = m_editor as IEditEvents_Event;
            m_editEvents5 = m_editor as IEditEvents5_Event;
        }

        /// <summary>
        /// Occurs when this tool is clicked
        /// </summary>
        public override void OnClick()
        {
            m_edSketch = m_editor as IEditSketch3;

            //Restrict to line constructors (for this tool)
            //m_edSketch.GeometryType = esriGeometryType.esriGeometryPolyline;

            //Activate a shape constructor based on the current sketch geometry
            if (m_edSketch.GeometryType == esriGeometryType.esriGeometryPoint)
                m_csc = new PointConstructorClass();
            else
                m_csc = new StraightConstructorClass();
            m_csc.Initialize(m_editor);
            m_edSketch.ShapeConstructor = m_csc;
            m_csc.Activate();

            //set the current task to null
            m_editor.CurrentTask = null;

            //setup events
            m_editEvents.OnSketchModified += new IEditEvents_OnSketchModifiedEventHandler(m_editEvents_OnSketchModified);
            m_editEvents5.OnShapeConstructorChanged += new IEditEvents5_OnShapeConstructorChangedEventHandler(m_editEvents5_OnShapeConstructorChanged);
            m_editEvents.OnSketchFinished += new IEditEvents_OnSketchFinishedEventHandler(m_editEvents_OnSketchFinished);
             
        }

        public void m_editEvents_OnSketchFinished()
        {
            //Create a point at the end of the sketch for the current point template.
            m_editor.StartOperation();
            IPoint point = m_edSketch.LastPoint;
            IEditTemplate editTemplate = m_editor.CurrentTemplate;

            IFeatureLayer featLayer = editTemplate.Layer as IFeatureLayer;
            IFeatureClass featClass = featLayer.FeatureClass;
            IFeature newFeature = featClass.CreateFeature();
            newFeature.Shape = point;

            //Apply default values as stored in the template properties.
            editTemplate.SetDefaultValues(newFeature);
            newFeature.Store();
            m_editor.StopOperation("Create Point");

            //Invalidate the area around the new feature.
            m_editor.Display.Invalidate(newFeature.Extent, true, (short)esriScreenCache.esriAllScreenCaches);


        }

        private void m_editEvents_OnSketchModified()
        {
            m_csc.SketchModified();
        }

        private void m_editEvents5_OnShapeConstructorChanged()
        {
            //activate new constructor
            m_csc.Deactivate();
            m_csc = null;
            m_csc = m_edSketch.ShapeConstructor;
            m_csc.Activate();
        }


//-------------------------------------------------------------------------------
//...............................................................................
    }
}


Thanks!
0 Kudos
0 Replies