eddixon85

Midpoint in .NET 3.5 & ArcGIS Engine 10.0

Discussion created by eddixon85 on Oct 29, 2013
Hello evreyone:

I'm programming a tool similar to ArcMap's Midpoint (Feature Constructor in Arcmap).

I draw de line with a  INewLineFeedback object and works well, but de mid point is well calculated, but doesn't refresh properly.

i'm wondering if someone knows how i can resolve this....

I attacht my code and a screenshot with the trouble.


Thanks for your attention

[ATTACH=CONFIG]28702[/ATTACH]

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using System.Windows.Forms;


namespace Editor_Tools
{
    /// <summary>
    /// Summary description for Midpoint.
    /// </summary>
    [Guid("5a956a79-8872-4f48-aec9-5b048a307c31")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("EngineClassLibrary1.Midpoint")]
    public sealed class Midpoint : BaseTool
    {
        #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);
            ControlsCommands.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);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        #region private members
        private IHookHelper m_hookHelper;
        private IEngineEditor engineEditor ;
        private IEngineEditSketch engineEditSketch;
        private IPoint midPoint;
        private IPoint f;
        private IPoint s;
        private INewLineFeedback pFeedbackEnv;
        #endregion

        public Midpoint()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text 
            base.m_caption = "";  //localizable text 
            base.m_message = "";  //localizable text
            base.m_toolTip = "Mid point";  //localizable text
            base.m_name = "";   //unique id, non-localizable (e.g. "MyCategory_MyTool")
            try
            {
                //
                // TODO: change resource name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
                base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");

                engineEditor = new EngineEditorClass();
                engineEditSketch = engineEditor as IEngineEditSketch;
                midPoint = new PointClass();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this tool is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            // TODO:  Add Midpoint.OnCreate implementation
        }

        /// <summary>
        /// Occurs when this tool is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add Midpoint.OnClick implementation
        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add Midpoint.OnMouseDown implementation
            IActiveView pActiveView;
            IScreenDisplay pScreenDisplay;
            //
            pActiveView = m_hookHelper.ActiveView;//My.ArcMap.Document.FocusMap;
            pScreenDisplay = pActiveView.ScreenDisplay;

            IPoint pPoint = new PointClass();
            pPoint.PutCoords(X, Y);
            pPoint = Transforms.GetMapCoordinatesFromScreenCoordinates(pPoint, pActiveView);

            if (pFeedbackEnv == null)
            {
                pFeedbackEnv = new NewLineFeedbackClass();
                pFeedbackEnv.Display = pActiveView.ScreenDisplay;
                pFeedbackEnv.Start(pPoint);
                f = pPoint;
            }
            else
            {
                //pFeedbackEnv.AddPoint(pPoint);
                s = pPoint;
                double dx = (f.X + s.X) / 2;
                double dy = (f.Y + s.Y) / 2;
                midPoint.PutCoords(dx, dy);
                engineEditSketch.AddPoint(midPoint, true);
                pFeedbackEnv = null;
                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewAll, null, pActiveView.Extent);
            }
            //engineEditSketch.AddPoint(pPoint, true);
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add Midpoint.OnMouseMove implementation
            IActiveView pActiveView;
            IScreenDisplay pScreenDisplay;
            //
            pActiveView = m_hookHelper.ActiveView;//My.ArcMap.Document.FocusMap;
            pScreenDisplay = pActiveView.ScreenDisplay;
            IPoint pPoint = new PointClass();
            pPoint.PutCoords(X, Y);

            if (pFeedbackEnv != null)
            {
                pPoint = Transforms.GetMapCoordinatesFromScreenCoordinates(pPoint, pActiveView);
                pFeedbackEnv.MoveTo(pPoint);
                s = pPoint;
                
                double dx = (f.X + s.X)/2;
                double dy = (f.Y + s.Y) / 2;
                midPoint.PutCoords(dx,dy);
                DrawPoint(pActiveView, midPoint);
            }
        }

        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add Midpoint.OnMouseUp implementation
        }


        public override void OnDblClick()
        {
            base.OnDblClick();
            engineEditSketch.FinishSketch();
        }

        public override bool Enabled
        {
            get
            {
                if ((engineEditor.EditState == esriEngineEditState.esriEngineStateEditing))
                {
                    return base.Enabled == true;
                }
                return base.Enabled == false;
            }
        }


        #endregion


        public void DrawPoint(ESRI.ArcGIS.Carto.IActiveView activeView, IPoint point)
        {

            if (activeView == null)
            {
                return;
            }
            ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

            // Constant
            screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit Cast
            ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();

            ESRI.ArcGIS.Display.ISymbol symbol = simpleMarkerSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic Cast
            screenDisplay.SetSymbol(symbol);
            ESRI.ArcGIS.Display.IDisplayTransformation displayTransformation = screenDisplay.DisplayTransformation;

            // x and y are in device coordinates
            //ESRI.ArcGIS.Geometry.IPoint point = displayTransformation.ToMapPoint(x, y);
            

            screenDisplay.DrawPoint(point);
            screenDisplay.FinishDrawing();
            //activeView.PartialRefresh(esriViewDrawPhase.esriViewNone , null, activeView.Extent);
        }

    }
}

Attachments

Outcomes