Search code examples
c#winformseyeshot

PointToScreen not returning screen coordinates


I am using EyeShot 12. I am creating a rectangle using EyeShot Line Entity, it has 2 dimensions along length and breadth.

My functionality involves changing the Dimension Text by using the action->SelectByPick, then picking anyone of the dimension and changing its value by bringing up a TextBox so that user can add the value. Here the TextBox pops-up on the location of mouse pointer.

Going further I click on Tab (keypad button) to switch to next dimension and also making sure that particular Dimension gets highlighted. But my concern is I am unable to locate the TextBox next to that highlighted dimension.

I am able to locate the position of existing Line(corresponding to the selected dimension) in Eyeshot coordinates but TextBox requires screen coordinates value for Positioning it exactly.

So I am using control.PointToScreen to convert eyeshot coordinates into screen but it return a Point which is same as to the Eyeshot coordinates.

code:

foreach (Entity ent in model1.Entities)      
{
    if (ent.Selected)
    {
        Line lin = (Line)ent;

        Point3D midpt = lin.MidPoint;

        string newpt1X = midpt.X.ToString();
        string newpt1Y = midpt.Y.ToString();

        System.Drawing.Point startPtX = model1.PointToScreen(new 
        System.Drawing.Point(int.Parse(newpt1X) + 20, int.Parse(newpt1Y) + 20));

        TextBox tb = new TextBox();
        tb.Text = "some text";
        tb.Width = 50;
        tb.Location = startPtX;
        model1.Controls.Add(tb);
    }

I looked for other results but everyone triggers to PointToScreen to get this convertion.

Hoping somebody can point what I am doing.

Thanks in advance

Suraj


Solution

  • You made your object (TextBox) a child of the ViewportLayout therefore you need the point relative to it. But the controls are not in the world coordinate but screen coordinate based on their parent.

    What you actually need is two (2) conversion.

    // first grab the entity point you want
    // this is a world point in 3D. I used your line entity
    // of your loop here
    var entityPoint = ((Line)ent).MidPoint;
    
    // now use your Viewport to transform the world point to a screen point
    // this screen point is actually a point on your real physical monitor(s)
    // so it is very generic, it need further conversion to be local to the control
    var screenPoint = model1.WorldToScreen(entityPoint);
    
    // now create a window 2d point
    var window2Dpoint = new System.Drawing.Point(screenPoint.X, screenPoint.Y);
    
    // now the point is on the complete screen but you want to know
    // relative to your viewport where that is window-wise
    var pointLocalToViewport = model1.PointToClient(window2Dpoint);
    
    // now you can setup the textbox position with this point as it's local
    // in X, Y relative to the model control.
    tb.Left = pointLocalToViewport.X;
    tb.Top = pointLocalToViewport.Y;
    
    // then you can add the textbox to the model1.Controls