//NOTE: I think the axManifoldMap.NativeToScreen() method does the same as this.
        public static Manifold.Interop.Point convertPointCoordinates(AxManifold.Interop.AxComponentControl p_Target, Manifold.Interop.Drawing p_Source, Manifold.Interop.Point p_objPoint)
        {
            Manifold.Interop.CoordinateSystem csSource = null;
            Manifold.Interop.CoordinateSystem csTarget = null;
            csTarget = p_Target.Application.DefaultCoordinateSystemLatLon;

            if (p_Source.CoordinateSystemVerified == true)
            {
                csSource = p_Source.CoordinateSystem;
            }
            else
            {
                p_Source.CoordinateSystem = p_Target.Application.Application.NewCoordinateSystem("Irish Grid");
                csSource = p_Target.Application.Application.NewCoordinateSystem("Irish Grid");
                p_Source.CoordinateSystemVerified = true;
            }

            Manifold.Interop.CoordinateConverter objConverter = p_Target.Application.NewCoordinateConverter();
            objConverter.Prepare((Manifold.Interop.Base)csTarget, (Manifold.Interop.Base)csSource);
            objConverter.Convert((Manifold.Interop.Base)p_objPoint, null);
            return p_objPoint;
        }
 private void axComponentControl1_EndTrack(object sender, AxManifold.Interop.IComponentControlEvents_EndTrackEvent e)
 {
     try
     {
         switch (axComponentControl1.MouseMode)
         {
             case Manifold.Interop.ControlMouseMode.ControlMouseModeGenericBoxCenter:
                 break;
             case Manifold.Interop.ControlMouseMode.ControlMouseModeGenericPoint:
                 double x, y;
                 x = e.pArgs.GeomLatLon.Box.XMax;
                 y = e.pArgs.GeomLatLon.Box.YMax;
                 DrawPoint(x, y);
                 break;
             case Manifold.Interop.ControlMouseMode.ControlMouseModeGenericLine:
                 DrawLine(e.pArgs.GeomNative);
                 break;
             case Manifold.Interop.ControlMouseMode.ControlMouseModeGenericArea:
                 DrawArea(e.pArgs.GeomNative);
                 break;
             default:
                 break;
         }
     }
     catch (Exception err)
     {
         MessageBox.Show("ERROR:" + err.Message);
     }
     finally
     {
         EventHandler_onEditModeChanged(this, new ModeTypeEventArgs(MODETYPE_OPERATION.SelectMode));
     }
 }
        private void axComponentControl1_ClickEvent(object sender, AxManifold.Interop.IComponentControlEvents_ClickEvent e)
        {
            try
            {
                double x, y;
                x = e.pArgs.LocationLatLon.X;
                y = e.pArgs.LocationLatLon.Y;

                if (axComponentControl1.MouseMode == Manifold.Interop.ControlMouseMode.ControlMouseModeNone)
                {
                    switch (e.pArgs.Button)
                    {
                        //HighLight is default selection
                        case Manifold.Interop.ControlMouseButton.MouseButtonLeft:
                            //HACK: adjust vertical location for the control's tool bar
                            Manifold.Interop.Point objPointScreen = e.pArgs.LocationScreen;
                            objPointScreen.Y += 27;
                            mapObjectSelection(objPointScreen);
                            break;
                        case Manifold.Interop.ControlMouseButton.MouseButtonMiddle:
                            break;
                        case Manifold.Interop.ControlMouseButton.MouseButtonRight:
                            this._IsDrawing = false;
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("ERROR:" + err.Message);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_MapControl">Reference to the Manifold ActiveX control.</param>
        /// <param name="p_DrawingOriginalEditObject">The Drawing layer which contains the original Line\Area to edit.</param>
        /// <param name="p_DrawingEdit">A temporary Drawing layer - used only to display inflection Points of the Line\area to edit.</param>
        /// <param name="p_Draw">Internal class used to draw ojects on Drawings.</param>
        /// <param name="p_MODETYPE_OPERATION"></param>
        public EditDrawingFactorySQL(AxManifold.Interop.AxComponentControl p_MapControl, Manifold.Interop.Drawing p_DrawingOriginalEditObject, Manifold.Interop.Drawing p_DrawingEdit, MODETYPE_OPERATION p_MODETYPE_OPERATION)
        {
            try
            {
                //Map events
                this._MapControl = p_MapControl;

                this._MapControl.MouseDownEvent += new AxManifold.Interop.IComponentControlEvents_MouseDownEventHandler(this.MapControl_MouseDownEvent);
                this._MapControl.MouseUpEvent += new AxManifold.Interop.IComponentControlEvents_MouseUpEventHandler(this.MapControl_MouseUpEvent);
                this._MapControl.MouseMoveEvent += new AxManifold.Interop.IComponentControlEvents_MouseMoveEventHandler(this.MapControl_MouseMoveEvent);

                if (p_DrawingOriginalEditObject == null )
                {
                    throw new Exception("The Original Edit objects Drawing was null in the EditDrawingFactory constructor. Assign an initialised Manifold.Interop.Drawing to it before calling this class.");
                }
                else
                {
                    this._DrawingOriginalEditObject = p_DrawingOriginalEditObject;
                }

                if (p_DrawingEdit == null)
                {
                    throw new Exception("The Edit Drawing layer was null in the EditDrawingFactory constructor. Assign an initialised Manifold.Interop.Drawing to it before calling this class.");
                }
                else
                {
                    this._DrawingEdit = p_DrawingEdit;
                }
                this._EditModeOperation = p_MODETYPE_OPERATION;
                clearEditLayer();
            }
            catch (Exception objEx)
            {
                throw;
            }
        }
        private void MapControl_MouseUpEvent(object sender, AxManifold.Interop.IComponentControlEvents_MouseUpEvent e)
        {
            try
            {
                bool bPrepareEdit = false;

                if (this._MouseIsClickedDown == true &&
                  (Utility.isModeTypeEditEnabled(EditModeOperation) == true))
                {
                    //Need to set this here because it gets updated in the 'prepareEditObject()' methods
                    //and the GEOMChanged Event needs the original value before it was Edited.
                    //string sSelectedManifoldObjectOriginalGEOM = this._SelectedManifoldObjectOriginalGEOM;

                    switch (EditModeOperation)
                    {
                        case MODETYPE_OPERATION.EditDeleteCoordinate:
                            this._DrawingEdit.SelectNone();
                            break;
                        case MODETYPE_OPERATION.EditAddCoordinate:
                            this._DrawingEdit.SelectNone();
                            break;
                        case MODETYPE_OPERATION.EditModeEnabled:
                            if (this._SelectedEditManifoldID > 0 && this._UserMovedCoordinate == true)
                            {
                                Manifold.Interop.Point objPointScreen = e.pArgs.LocationScreen;
                                objPointScreen.Y += this._YCoordMapClickOffset;
                                objPointScreen = this._MapControl.ScreenToNative(objPointScreen);
                                moveOriginalPointCoordinate(objPointScreen.X, objPointScreen.Y);
                                bPrepareEdit = true;

                                //After MouseUp Event - remove selection for move so that it dosnt move anymore
                                this._SelectedEditManifoldID = -1;
                            }
                            break;
                        case MODETYPE_OPERATION.EditMoveGeom:
                            if (this._SelectedEditManifoldID > 0 && this._UserMovedGeom == true)
                            {
                                this._SelectedEditManifoldPointEnd = e.pArgs.LocationNative;
                                if (this._SelectedEditManifoldPointStart != null && this._SelectedEditManifoldPointEnd != null)
                                {
                                    //Move Geom the difference in meters Horizontally and Vertically
                                    moveGeomWithSQL();
                                    bPrepareEdit = true;
                                }
                                //After MouseUp Event - remove selection for move so that it dosnt move anymore
                                this._SelectedEditManifoldID = -1;
                            }
                            break;
                        default:
                            break;
                        //}
                    }
                    if (this._DrawingOriginalEditObject.Selection.Count < 1 || Convert.ToInt32(this._DrawingOriginalEditObject.Selection[0].Record.get_Data("ID").ToString()) != this._SelectedManifoldID)
                    {
                        Utility.setSelection(this._MapControl, this._DrawingOriginalEditObject, this._SelectedManifoldID);
                    }

                    if (bPrepareEdit == true)
                    {
                        prepareEditObject();
                    }
                }
            }
            catch (Exception objEx)
            {
                throw;
            }
            finally
            {
                this._UserMovedCoordinate = false;
                this._UserAddedCoordinate = false;
                this._UserDeletedCoordinate = false;
                this._UserMovedGeom = false;
                this._MouseIsClickedDown = false;
                this._SelectedEditManifoldPointEnd = null;
                this._SelectedEditManifoldPointStart = null;

                this._SelectedPointBranchIndex = -1;
                this._SelectedPointIndex = -1;
            }
        }
        private void MapControl_MouseMoveEvent(object sender, AxManifold.Interop.IComponentControlEvents_MouseMoveEvent e)
        {
            try
            {
                //Move Coordinate
                if (EditModeOperation == MODETYPE_OPERATION.EditModeEnabled && this._MouseIsClickedDown == true)
                {
                    if (this._DrawingOriginalEditObject.Selection.Count < 1 || Convert.ToInt32(this._DrawingOriginalEditObject.Selection[0].Record.get_Data("ID").ToString()) != this._SelectedManifoldID)
                    {
                        Utility.setSelection(this._MapControl, this._DrawingOriginalEditObject, this._SelectedManifoldID);
                    }

                    if (this._SelectedEditManifoldID > 0)
                    {
                        Manifold.Interop.Point objPointScreen = e.pArgs.LocationScreen;
                        objPointScreen.Y += this._YCoordMapClickOffset;
                        objPointScreen = this._MapControl.ScreenToNative(objPointScreen);
                        moveEditPointCoordinate(objPointScreen.X, objPointScreen.Y);
                    }

                    if (this._DrawingOriginalEditObject.Selection.Count < 1 || Convert.ToInt32(this._DrawingOriginalEditObject.Selection[0].Record.get_Data("ID").ToString()) != this._SelectedManifoldID)
                    {
                        Utility.setSelection(this._MapControl, this._DrawingOriginalEditObject, this._SelectedManifoldID);
                    }
                }

                //Move Geom
                if (EditModeOperation == MODETYPE_OPERATION.EditMoveGeom && this._MouseIsClickedDown == true)
                {
                    if (this._DrawingOriginalEditObject.Selection.Count < 1 || Convert.ToInt32(this._DrawingOriginalEditObject.Selection[0].Record.get_Data("ID").ToString()) != this._SelectedManifoldID)
                    {
                        Utility.setSelection(this._MapControl, this._DrawingOriginalEditObject, this._SelectedManifoldID);
                    }

                    if (this._SelectedEditManifoldID > 0)
                    {
                        Manifold.Interop.Point objPointScreen = e.pArgs.LocationScreen;
                        objPointScreen.Y += this._YCoordMapClickOffset;
                        objPointScreen = this._MapControl.ScreenToNative(objPointScreen);
                        moveEditPointCoordinate(objPointScreen.X, objPointScreen.Y);
                    }

                    if (this._DrawingOriginalEditObject.Selection.Count < 1 || Convert.ToInt32(this._DrawingOriginalEditObject.Selection[0].Record.get_Data("ID").ToString()) != this._SelectedManifoldID)
                    {
                        Utility.setSelection(this._MapControl, this._DrawingOriginalEditObject, this._SelectedManifoldID);
                    }
                }
            }
            catch (Exception objEx)
            {
                throw;
            }
        }
        private void MapControl_MouseDownEvent(object sender, AxManifold.Interop.IComponentControlEvents_MouseDownEvent e)
        {
            try
            {
                if (Utility.isModeTypeEditEnabled(EditModeOperation) == true)
                {
                    this._DrawingEdit.SelectNone();
                    if (this._DrawingOriginalEditObject.Selection.Count < 1 || Convert.ToInt32(this._DrawingOriginalEditObject.Selection[0].Record.get_Data("ID").ToString()) != this._SelectedManifoldID)
                    {
                        Utility.setSelection(this._MapControl, this._DrawingOriginalEditObject, this._SelectedManifoldID);
                    }

                    if (this._DrawingEdit.Selection.Count < 1)
                    {
                        //Save the Edit object GEOM string before any EDITING is performed on it.
                        this._SelectedManifoldObjectOriginalGEOM = this._DrawingOriginalEditObject.Selection[0].get_Geom().ToTextWKT();

                        //SET SELECTION
                        Manifold.Interop.Point objPointScreen = e.pArgs.LocationScreen;
                        objPointScreen.Y += this._YCoordMapClickOffset;
                        objPointScreen = this._MapControl.ScreenToNative(objPointScreen);

                        switch (EditModeOperation)
                        {
                            case MODETYPE_OPERATION.EditDeleteCoordinate:
                                this._MouseIsClickedDown = true;
                                deleteCoordinate(objPointScreen);
                                break;
                            case MODETYPE_OPERATION.EditAddCoordinate:
                                this._MouseIsClickedDown = true;
                                if (this._AddingCoordinate == false)
                                {
                                    addCoordinate(objPointScreen);
                                }
                                break;
                            case MODETYPE_OPERATION.EditModeEnabled:
                                Utility.setNearestSelection(this._MapControl, this._DrawingEdit, objPointScreen, _SelectionTolerance);

                                if (this._DrawingEdit.Selection.Count > 0)
                                {
                                    //Record selected Point object
                                    this._MouseIsClickedDown = true;
                                    this._SelectedEditManifoldID = Convert.ToInt32(this._DrawingEdit.Selection[0].Record.get_Data("ID").ToString());

                                    this._SelectedPointBranchIndex = Convert.ToInt32(this._DrawingEdit.Selection[0].Record.get_Data("BranchIndex").ToString());
                                    this._SelectedPointIndex = Convert.ToInt32(this._DrawingEdit.Selection[0].Record.get_Data("PointIndex").ToString());
                                }
                                else
                                {
                                    this._SelectedEditManifoldID = -1;
                                }
                                break;
                            case MODETYPE_OPERATION.EditMoveGeom:
                                Utility.setNearestSelection(this._MapControl, this._DrawingEdit, objPointScreen, _SelectionTolerance);

                                if (this._DrawingEdit.Selection.Count > 0)
                                {
                                    this._GeomMovedDetails = new GeomMovedDetails();
                                    this._UserMovedGeom = false;
                                    //Record selected Point object
                                    this._MouseIsClickedDown = true;
                                    this._SelectedEditManifoldID = Convert.ToInt32(this._DrawingEdit.Selection[0].Record.get_Data("ID").ToString());
                                    this._SelectedEditManifoldPointStart = e.pArgs.LocationNative;
                                }
                                else
                                {
                                    this._SelectedEditManifoldID = -1;
                                }
                                break;
                            default:
                                this._MouseIsClickedDown = false;
                                break;
                        }
                    }
                }
            }
            catch (Exception objEx)
            {
                throw;
            }
        }
 public static void unsetSelection(AxManifold.Interop.AxComponentControl p_MapControl, Manifold.Interop.Drawing p_Drawing)
 {
     try
     {
         string sSQL = string.Format(@"UPDATE [{0}] SET [Selection (I)] = False ", p_Drawing.Name);
         executeSQL(sSQL);
         p_MapControl.Refresh();
     }
     catch (Exception objEx)
     {
         throw;
     }
 }
        public static void setSelection(AxManifold.Interop.AxComponentControl p_MapControl, Manifold.Interop.Drawing p_Drawing, Manifold.Interop.Point p_ptClickedLocation, double p_dblSelectionTolerance)
        {
            Manifold.Interop.Query tempQuery = p_MapControl.get_Document().NewQuery("TempQuery", false);
            try
            {
                //Clear any Previous selection
                unsetSelection(p_MapControl, p_Drawing);

                StringBuilder sbSQL = new StringBuilder();
                #region Method1
                //Set current selection
                //sbSQL.AppendLine(@"UPDATE [" + p_Drawing.Name + "] SET [Selection (I)] = True WHERE [ID] IN (");
                //sbSQL.AppendLine(@" SELECT tblDistance.[ID] FROM [" + p_Drawing.Name + "] tblDistance WHERE ");
                //sbSQL.AppendLine(@" DISTANCE(GEOM([ID]),AssignCoordSys(CGeom(CGeomWKB(""POINT (" + p_ptClickedLocation.X.ToString() + " " + p_ptClickedLocation.Y.ToString() + @")"")), CoordSys(""" + p_Drawing.Name + @""" AS COMPONENT))) = ");
                //sbSQL.AppendLine(@" (");
                //sbSQL.AppendLine(@"     SELECT min (DISTANCE(GEOM([ID]),AssignCoordSys(CGeom(CGeomWKB(""POINT (" + p_ptClickedLocation.X.ToString() + " " + p_ptClickedLocation.Y.ToString() + @")"")), CoordSys(""" + p_Drawing.Name + @""" AS COMPONENT))))");
                //sbSQL.AppendLine(@"     FROM [" + p_Drawing.Name + "] tblDistance");
                //sbSQL.AppendLine(@"     WHERE DISTANCE(GEOM([ID]),AssignCoordSys(CGeom(CGeomWKB(""POINT (" + p_ptClickedLocation.X.ToString() + " " + p_ptClickedLocation.Y.ToString() + @")"")), CoordSys(""" + p_Drawing.Name + @""" AS COMPONENT))) < " + p_dblSelectionTolerance);
                //sbSQL.AppendLine(@" ))");
                #endregion

                #region Method2
                //string wkt = String.Format("POINT ({0} {1})", p_ptClickedLocation.X.ToString(), p_ptClickedLocation.Y.ToString());
                //string sql = String.Format(@"UPDATE [{0}] SET [Selection (I)] = True " +
                //                           @"WHERE TOUCHES([ID],BUFFER(AssignCoordSys(CGeom(CGeomWKB(""{1}"")), CoordSys(""{0}"" AS COMPONENT)),{2}))",
                //                           p_Drawing.Name, wkt, p_dblSelectionTolerance);
                #endregion

                //Create selection Rectangle around clicked Point
                Manifold.Interop.PointSet points = p_MapControl.Application.NewPointSet();
                points.Add(p_MapControl.Application.NewPoint(p_ptClickedLocation.X - p_dblSelectionTolerance, p_ptClickedLocation.Y - p_dblSelectionTolerance));
                points.Add(p_MapControl.Application.NewPoint(p_ptClickedLocation.X + p_dblSelectionTolerance, p_ptClickedLocation.Y - p_dblSelectionTolerance));
                points.Add(p_MapControl.Application.NewPoint(p_ptClickedLocation.X + p_dblSelectionTolerance, p_ptClickedLocation.Y + p_dblSelectionTolerance));
                points.Add(p_MapControl.Application.NewPoint(p_ptClickedLocation.X - p_dblSelectionTolerance, p_ptClickedLocation.Y + p_dblSelectionTolerance));
                points.Add(VBManifoldWrapper.ManifoldObjectWrapper.getPointFromPointSet(points, 0));

                //Create the geom to get WKT string to use in search
                Manifold.Interop.Geom geom = p_MapControl.Application.NewGeom(Manifold.Interop.GeomType.GeomArea, null);
                Manifold.Interop.BranchSet geomBranchSet = geom.get_BranchSet();
                VBManifoldWrapper.ManifoldObjectWrapper.setPointSetInBranchSet(points, geomBranchSet);
                string wkt = geom.ToTextWKT();

                sbSQL.Length = 0;
                sbSQL.AppendLine(@"UPDATE [" + p_Drawing.Name + "] SET [Selection (I)] = True ");
                sbSQL.AppendLine("WHERE Touches(AssignCoordSys(CGeom(CGeomWKB(\"" + wkt + "\")), CoordSys(\"" + p_Drawing.Name + "\" AS COMPONENT)), [ID])");

                tempQuery.Text = sbSQL.ToString();
                tempQuery.Run();
                p_MapControl.Refresh();
            }
            catch (Exception objEx)
            {
                throw;
            }
            finally
            {
                p_MapControl.get_Document().ComponentSet.Remove(tempQuery);
            }
        }
        public static void setSelection(AxManifold.Interop.AxComponentControl p_MapControl, Manifold.Interop.Drawing p_Drawing, Int32 p_iManifoldObjectID)
        {
            try
            {
                //Clear any Previous selection
                unsetSelection(p_MapControl, p_Drawing);

                //Set selection
                string sSQL = String.Format(@"UPDATE [{0}] SET [Selection (I)] = True WHERE [ID]= {1}", p_Drawing.Name, p_iManifoldObjectID.ToString());
                //Manifold.Interop.Query tempQuery = p_MapControl.get_Document().NewQuery("TempQuery", false);

                //p_MapControl.get_Document().ComponentSet.Remove(tempQuery);
                executeSQL(sSQL);
                p_MapControl.Refresh();
            }
            catch (Exception objEx)
            {
                throw;
            }
        }
 public static void setNearestSelection(AxManifold.Interop.AxComponentControl p_MapControl, Manifold.Interop.Drawing p_Drawing, Manifold.Interop.Point p_ptClickedLocation, double p_dblSelectionTolerance)
 {
     try
     {
         executeSQL(getSQL_SetNearestSelection(p_Drawing, p_ptClickedLocation, p_dblSelectionTolerance));
     }
     catch (Exception objEx)
     {
         throw;
     }
 }
        public static Manifold.Interop.Map loadManifoldMap(AxManifold.Interop.AxComponentControl p_axManifoldMap, string p_sMapPath, string p_sMapName)
        {
            Manifold.Interop.Map objReturn = null;
            bool bMapExists = false;

            try
            {
                p_axManifoldMap.set_Document(p_sMapPath);
                Manifold.Interop.ComponentSet m_ComponentSet = p_axManifoldMap.get_Document().ComponentSet;

                //Disaply _Map_Inspection if exists
                foreach (Manifold.Interop.Component objComponent in m_ComponentSet)
                {
                    switch (objComponent.Type)
                    {
                        case Manifold.Interop.ComponentType.ComponentMap:
                            if (p_sMapName.Equals(objComponent.Name))
                            {
                                //Get Map Name
                                objReturn = (Manifold.Interop.Map)objComponent;
                                p_axManifoldMap.ComponentName = objComponent.Name;
                                bMapExists = true;
                                break;
                            }
                            break;
                        default:
                            break;
                    }
                }

                //Create new Map if required
                if (bMapExists == false)
                {
                    Manifold.Interop.ComponentSet objNewComponentSet = p_axManifoldMap.get_Document().NewComponentSet();
                    foreach (Manifold.Interop.Component objComponent in m_ComponentSet)
                    {
                        switch (objComponent.Type)
                        {
                            case Manifold.Interop.ComponentType.ComponentDrawing:
                                objNewComponentSet.Add(objComponent);
                                break;
                            case Manifold.Interop.ComponentType.ComponentTable:
                                break;
                            default:
                                break;
                        }
                    }

                    //objReturn = p_axManifoldMap.get_Document().NewMap(p_sMapName, objNewComponentSet, p_axManifoldMap.Application.DefaultCoordinateSystem, true);
                    objReturn = p_axManifoldMap.get_Document().NewMap(p_sMapName, objNewComponentSet, p_axManifoldMap.Application.Application.NewCoordinateSystem("Irish Grid"), true);
                    p_axManifoldMap.ComponentName = p_sMapName;
                    p_axManifoldMap.ZoomToFit();
                }
            }
            catch (Exception objEx)
            {
                throw;
            }

            return objReturn;
        }