Problem with UCS vectors

Problem with UCS vectors

hericson
Advocate Advocate
857 Views
5 Replies
Message 1 of 6

Problem with UCS vectors

hericson
Advocate
Advocate
I can't figure out how the vectors and matrix works while changing UCS.

Let's say I have two points (could be start and end point on a line), now I want to create a UCS that have the X-axis on this line between the two points. I tried the following code but I can't create the correct vectors for the x,y and z axis.

Dim mat As Matrix3d = Matrix3d.AlignCoordinateSystem(Point3d.Origin, _
Vector3d.XAxis, _
Vector3d.YAxis, _
Vector3d.ZAxis, _
originPoint, _
xAxisVec, _
yAxisVec, _
zAxisVec)

ed.CurrentUserCoordinateSystem = mat

Please, help!
0 Likes
858 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant
Hi,

Two points aren't enough to define a coordinate system.
A third data is needed, as a third point or a vector.
The way AutoCAD works to "Accept" after two user inputs (points) in the UCS command is keeping the same ZAxis vector as prvious UCS.

The following routine works this way:

{code}private void SetUcsBy2Points(Point3d pt1, Point3d pt2)
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Vector3d zAxis = ed.CurrentUserCoordinateSystem.CoordinateSystem3d.Zaxis;
Vector3d xAxis = pt1.GetVectorTo(pt2).GetNormal();
Vector3d yAxis = zAxis.CrossProduct(xAxis).GetNormal();
Matrix3d mat = Matrix3d.AlignCoordinateSystem(
Point3d.Origin,
Vector3d.XAxis,
Vector3d.YAxis,
Vector3d.ZAxis,
pt1,
xAxis,
yAxis,
zAxis);
ed.CurrentUserCoordinateSystem = mat;
}{code}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

hericson
Advocate
Advocate
Thanks a lot, it worked like a charm!

I knew that a third point was needed but you solved it like I tried, with keeping the zAxis as in the previous UCS.

But now I have another question, how to "plan" the new UCS? (Like the PLAN command in AutoCAD). Is it to create a new view? Can you point me in some direction or even better give me a sample?
0 Likes
Message 4 of 6

_gile
Consultant
Consultant
IMO, the easyest way is to set UCSFOLLOW to 1 before changing UCS.


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 6

hericson
Advocate
Advocate
Smart solution! Thanks again.
0 Likes
Message 6 of 6

_gile
Consultant
Consultant
Here's a safer implementation which will work even if the pt1 to pt2 vector in colinear to the current UCS Z axis.
It also use anothe way to build the matrix.

{code}private void SetUcsBy2Points(Point3d pt1, Point3d pt2)
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
CoordinateSystem3d ucs = ed.CurrentUserCoordinateSystem.CoordinateSystem3d;
Vector3d zAxis = ucs.Zaxis;
Vector3d xAxis = pt1.GetVectorTo(pt2).GetNormal();
Vector3d yAxis;
if (xAxis.IsEqualTo(zAxis))
{
yAxis = ucs.Yaxis;
zAxis = xAxis.CrossProduct(yAxis).GetNormal();
}
else
{
yAxis = zAxis.CrossProduct(xAxis).GetNormal();
}
Matrix3d mat = new Matrix3d(new double[]{
xAxis.X, yAxis.X, zAxis.X, pt1.X,
xAxis.Y, yAxis.Y, zAxis.Y, pt1.Y,
xAxis.Z, yAxis.Z, zAxis.Z, pt1.Z,
0.0, 0.0, 0.0, 1.0});
ed.CurrentUserCoordinateSystem = mat;
}{code}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes