Prod.: Engine, ver.: 6, ID: 60000701, HowTo : How can I calculate the position of objects that are shown in a viewport

HowTo : How can I calculate the position of objects that are shown in a viewport

Article60000701
TypeHowTo
ProductEngine
Version6
Date Added11/28/2008
Submitted byPushpak
Keywords

Subject

How can I calculate the position of objects that are shown in a viewport

Summary

How can I calculate the position in the layout of objects that are shown inside a viewport ?

Solution

In a new C# (Windows Forms) project add a vdFramed Control and 2 buttons. Then add this code :

public Form1()
{
                InitializeComponent();
}

ulong textHandle = 0;
ulong circHandle = 0;
ulong viewpHandle = 0;
short ColorInd = 40;

private void Form1_Load(object sender, EventArgs e)
{
//create a circle, a text (these points will be calculated) a Layout and a Viewport.

vdFramedControl1.BaseControl.ActiveDocument.New();
//create a circle
VectorDraw.Professional.vdFigures.vdCircle circ = new VectorDraw.Professional.vdFigures.vdCircle();
circ.SetUnRegisterDocument(vdFramedControl1.BaseControl.ActiveDocument);
circ.setDocumentDefaults();
circ.Center =
new VectorDraw.Geometry.gPoint(-3, -2, .5);
circ.Radius = (
double)3.3;
vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut.Entities.AddItem(circ);
circHandle = circ.Handle.Value;

//create a text
VectorDraw.Professional.vdFigures.vdText txt = new VectorDraw.Professional.vdFigures.vdText();
txt.SetUnRegisterDocument(vdFramedControl1.BaseControl.ActiveDocument);
txt.setDocumentDefaults();
txt.InsertionPoint =
new VectorDraw.Geometry.gPoint(3, 1, 3.5);
txt.TextString =
"Find WCoords in Layout";
vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut.Entities.AddItem(txt);
textHandle = txt.Handle.Value;

//create a Layout
VectorDraw.Professional.vdPrimaries.vdLayout lay = new VectorDraw.Professional.vdPrimaries.vdLayout();
lay.SetUnRegisterDocument(vdFramedControl1.BaseControl.ActiveDocument);
lay.setDocumentDefaults();
lay.Name =
"MyLayout1";
lay.ShowUCSAxis =
false;
vdFramedControl1.BaseControl.ActiveDocument.LayOuts.AddItem(lay);

//create a Viewport
VectorDraw.Professional.vdFigures.vdViewport view = new VectorDraw.Professional.vdFigures.vdViewport();
view.SetUnRegisterDocument(vdFramedControl1.BaseControl.ActiveDocument);
view.setDocumentDefaults();
view.ShowUCSAxis =
false;
view.Height = 100;
view.Width = 150;
view.Center =
new VectorDraw.Geometry.gPoint(100.0, 230.0);
view.ViewCenter =
new VectorDraw.Geometry.gPoint(4.4008, 1.8233);
view.ViewSize = 17.0;
view.PenColor.SystemColor =
Color.Red;
view.PenWidth = 1.0d;

//And add this viewport to the entities of the first layout.
lay = vdFramedControl1.BaseControl.ActiveDocument.LayOuts.FindName("MyLayout1");
if (lay != null) lay.Entities.AddItem(view);
viewpHandle = view.Handle.Value;
vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut = lay;
//change the view to the MyLayout1
vdFramedControl1.BaseControl.ActiveDocument.Redraw(false);

}

private void buttonCalculatePoints_Click(object sender, EventArgs e)
{
// this code will calculate (in WorldCS) the points of the two entities that are shown inside the viewport

//get the entities from their handles
VectorDraw.Professional.vdFigures.vdCircle onecircle = vdFramedControl1.BaseControl.ActiveDocument.FindFromHandle(new VectorDraw.Professional.vdObjects.vdHandle(circHandle), typeof(VectorDraw.Professional.vdPrimaries.vdFigure)) as VectorDraw.Professional.vdFigures.vdCircle;
VectorDraw.Professional.vdFigures.
vdText onetext = vdFramedControl1.BaseControl.ActiveDocument.FindFromHandle(new VectorDraw.Professional.vdObjects.vdHandle(textHandle), typeof(VectorDraw.Professional.vdPrimaries.vdFigure)) as VectorDraw.Professional.vdFigures.vdText;

//get the points to calculate (center point of the circle and insertion point of the text)
VectorDraw.Geometry.
gPoint CircPt = new VectorDraw.Geometry.gPoint(onecircle.Center); // circle's center point in WCS
VectorDraw.Geometry.gPoint TextPt = new VectorDraw.Geometry.gPoint(onetext.InsertionPoint);// text's insertion point in WCS
vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut = vdFramedControl1.BaseControl.ActiveDocument.LayOuts[0];
VectorDraw.Professional.vdFigures.
vdViewport viewport = new VectorDraw.Professional.vdFigures.vdViewport();

//get the viewport and set it to active in order to get the transformation matrix
viewport = vdFramedControl1.BaseControl.ActiveDocument.FindFromHandle(
new VectorDraw.Professional.vdObjects.vdHandle(viewpHandle), typeof(VectorDraw.Professional.vdPrimaries.vdFigure)) as VectorDraw.Professional.vdFigures.vdViewport;

vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut.ActiveViewPort = viewport;
vdFramedControl1.BaseControl.ActiveDocument.Redraw(
true);

//do the transformations  and calculate the points
VectorDraw.Geometry.gPoint ptC = vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut.ActiveViewPort.Render.WorldToView(CircPt); // circle's center in Viewport's view coordinates
VectorDraw.Geometry.gPoint ptT = vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut.ActiveViewPort.Render.WorldToView(TextPt);// text's insertion point in Viewport's view coordinates
VectorDraw.Geometry.gPoint ptT1 = new VectorDraw.Geometry.gPoint(); // in world coordinates
VectorDraw.Geometry.gPoint ptC1 = new VectorDraw.Geometry.gPoint();//in world coordinates

vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut.ActiveViewPort = null;
VectorDraw.Geometry.
Matrix mat = new VectorDraw.Geometry.Matrix();
mat.ScaleMatrix(1 / viewport.ViewportScale, 1 / viewport.ViewportScale, 1 / viewport.ViewportScale);
mat.TranslateMatrix(viewport.Center);
mat.TransformPt(ptT - viewport.ViewCenter, ptT1);
mat.TransformPt(ptC - viewport.ViewCenter, ptC1);

// Create a line to connect the two calculated points
VectorDraw.Professional.vdFigures.
vdLine lin = new VectorDraw.Professional.vdFigures.vdLine();
lin.SetUnRegisterDocument(vdFramedControl1.BaseControl.ActiveDocument);
lin.setDocumentDefaults();
lin.StartPoint =
new VectorDraw.Geometry.gPoint(ptT1.x,ptT1.y,(double)0.0); // the insertion point of the text in World coordinates in the Layout
lin.EndPoint = new VectorDraw.Geometry.gPoint(ptC1.x, ptC1.y, (double)0.0); // the center point of the circle in World coordinates in the Layout
// z is set to zero as it is not needed in Paper spaces
lin.PenColor.ColorIndex = ColorInd;
ColorInd += 1;
lin.Invalidate();
vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut.Entities.AddItem(lin);

}

private void buttonChangeViewport_Click(object sender, EventArgs e)
{ // Change the viewport so the circle and text are shown in different position, then press the calculate button.

VectorDraw.Professional.vdFigures.vdViewport viewport = new VectorDraw.Professional.vdFigures.vdViewport();
viewport = vdFramedControl1.BaseControl.ActiveDocument.FindFromHandle(
new VectorDraw.Professional.vdObjects.vdHandle(viewpHandle), typeof(VectorDraw.Professional.vdPrimaries.vdFigure)) as VectorDraw.Professional.vdFigures.vdViewport;
vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut.ActiveViewPort = viewport;
vdFramedControl1.BaseControl.ActiveDocument.CommandAction.View3D(
"vise");
viewport.ViewCenter =
new VectorDraw.Geometry.gPoint(5, -4);
viewport.ViewSize = 30;
vdFramedControl1.BaseControl.ActiveDocument.ActiveLayOut.ActiveViewPort =
null;
viewport.Update();
viewport.Invalidate();

}