Page 1 of 1

Difference between mouse position click value and selected entities position value

Posted: 26 Feb 2023, 18:23
by Masoud
i get mouse position from cadpicturebox with this code:

this.CADPictureBox.PictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CadPictBox_MouseDown);

private void CadPictBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
}

And I get the position of the selected line with this code:

var LineSelected = (CADLine)CADPictureBox.Image.SelectedEntities[0];
int xx=LineSelected.Point.X;
iny yy=LineSelected.Point.Y;

When I measure the coordinates of the line in AutoCAD software, it is exactly the same as the value obtained from the line selection method, but it is completely different from the value obtained by selecting the two ends of the line with the mouse.

In my software, I want the user to find the coordinates by clicking on cadpicturebox, but it gives me wrong numbers, thank you for your help.

Re: Difference between mouse position click value and selected entities position value

Posted: 28 Feb 2023, 09:52
by support
Masoud wrote:
26 Feb 2023, 18:23
i get mouse position from cadpicturebox with this code:

this.CADPictureBox.PictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CadPictBox_MouseDown);

private void CadPictBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
}

And I get the position of the selected line with this code:

var LineSelected = (CADLine)CADPictureBox.Image.SelectedEntities[0];
int xx=LineSelected.Point.X;
iny yy=LineSelected.Point.Y;

When I measure the coordinates of the line in AutoCAD software, it is exactly the same as the value obtained from the line selection method, but it is completely different from the value obtained by selecting the two ends of the line with the mouse.

In my software, I want the user to find the coordinates by clicking on cadpicturebox, but it gives me wrong numbers, thank you for your help.
Hi,
The thing is that mouse coordinates are screen coordinates. Please, have a look at the Editor demo, where you can see how the cooordinates are recalculated on the onmouse event:

Code: Select all

#region Help
  /// <summary>
  /// Gets a three-dimensional point of the current CAD image from the specified screen point.
  /// </summary>
  /// <param name="x">An X coordinate of the screen point.</param>
  /// <param name="y">A Y coordinate of the screen point.</param>
        /// <param name="useGrid">Defines if the point will be reduced to a grid node.</param>
  /// <returns>A three-dimensional point.</returns>
#endregion Help
  public DPoint GetRealPoint(int x, int y, bool useGrid)
  {
            return GetRealPoint(x, y, useGrid, true);
  }

#region Help
        /// <summary>
        /// Gets a three-dimensional point of the current CAD image from the specified screen point.
        /// </summary>
        /// <param name="x">An X coordinate of the screen point.</param>
        /// <param name="y">A Y coordinate of the screen point.</param>
        /// <param name="useGrid">Defines if the point will be reduced to a grid node.</param>
        /// <param name="rounding">Defines if rounding will be applied.</param>
        /// <returns>A three-dimensional point.</returns>
#endregion Help
        public DPoint GetRealPoint(int x, int y, bool useGrid, bool rounding)
        {
            RectangleF tmpRect = GetDrawingRect();
            DPoint realPt = CADConst.GetRealPoint(x, y, this.cadImage, tmpRect, rounding);
            if (useGrid)
                this.cadImage.CorrectByGrid(ref realPt);
            return realPt;
        }

You should multiply the matrix (Painter.DrawMatrix) of the visible drawing area (current.Layout.Box) ("return DRect.GetRealBox(cadImage.CurrentLayout.Box, cadImage.Painter.DrawMatrix);") by the box coordinates GetRealBox(DRect dRect, CADMatrix matrix).

Catherine.

Re: Difference between mouse position click value and selected entities position value

Posted: 28 Feb 2023, 13:35
by Masoud
Thank you so much Catherine,To complete the code you sent, I am sending this code

private DRect GetDrawingRect()
{
if (this.CadImage != null)
return DRect.GetRealBox(this.CadImage.CurrentLayout.Box, this.CadImage.Painter.DrawMatrix);
else
return CadEditorControl_cec.ClientRectangle;
}