Getting the coordinates of an entity base upon its UCS as a base point.

Getting the coordinates of an entity base upon its UCS as a base point.

gotMorris
Enthusiast Enthusiast
1,991 Views
2 Replies
Message 1 of 3

Getting the coordinates of an entity base upon its UCS as a base point.

gotMorris
Enthusiast
Enthusiast

I am trying to move a UCS to a particular point and then get the coordinates of an Entity ,in this case a circle, based upon the UCS I just moved as its base point. The results of the coordinates obtained from code are incorrect (X3.0,Y5.5,Z0) which makes no sense. They should be (X2.5,Y0.0,Z0.0). after my code runs I can check the coordinates manually in AutoCad and the are correct. Not sure what the problem is.Any help would be appreciated.

 

    private static void UcsBaseCoords(Circle circle, Point3d newBasePoint)
    {
        var acDoc = Active.Document;

        CoordinateSystem3d coordSystem = new CoordinateSystem3d(newBasePoint, new Vector3d(0, -1, 0),new Vector3d(1, 0, 0));

        CoordinateSystem3d cosy = acDoc.Editor.CurrentUserCoordinateSystem.CoordinateSystem3d;

        var ecs = Matrix3d.AlignCoordinateSystem(cosy.Origin, cosy.Xaxis, cosy.Yaxis, cosy.Zaxis, coordSystem.Origin, coordSystem.Xaxis, coordSystem.Yaxis, coordSystem.Zaxis);

        acDoc.Editor.CurrentUserCoordinateSystem = ecs;

        acDoc.Editor.UpdateTiledViewportsInDatabase();

        var coodrsBasedOnUcs = circle.Center.TransformBy(ecs);
        acDoc.Editor.WriteMessage(String.Format("X{0}, Y{1}, Z{2}", coodrsBasedOnUcs.X, coodrsBasedOnUcs.Y, coodrsBasedOnUcs.Z));
    }

 

This is after my program runs and it is correct. My UCS has been moved and If I check the circle's coordinates they are correct.(X2.5,Y0.0,Z0.0) 

p5.png

 

 

This is my dwg before I run my program.

p3.png

 

 

 

Accepted solutions (2)
1,992 Views
2 Replies
Replies (2)
Message 2 of 3

ActivistInvestor
Mentor
Mentor
Accepted solution

@Anonymous.mymail wrote:

I am trying to move a UCS to a particular point and then get the coordinates of an Entity ,in this case a circle, based upon the UCS I just moved as its base point. The results of the coordinates obtained from code are incorrect (X3.0,Y5.5,Z0) which makes no sense. They should be (X2.5,Y0.0,Z0.0). after my code runs I can check the coordinates manually in AutoCad and the are correct. Not sure what the problem is.Any help would be appreciated.

 

    private static void UcsBaseCoords(Circle circle, Point3d newBasePoint)
    {
        var acDoc = Active.Document;

        CoordinateSystem3d coordSystem = new CoordinateSystem3d(newBasePoint, new Vector3d(0, -1, 0),new Vector3d(1, 0, 0));

        CoordinateSystem3d cosy = acDoc.Editor.CurrentUserCoordinateSystem.CoordinateSystem3d;

        var ecs = Matrix3d.AlignCoordinateSystem(cosy.Origin, cosy.Xaxis, cosy.Yaxis, cosy.Zaxis, coordSystem.Origin, coordSystem.Xaxis, coordSystem.Yaxis, coordSystem.Zaxis);

        acDoc.Editor.CurrentUserCoordinateSystem = ecs;

        acDoc.Editor.UpdateTiledViewportsInDatabase();

        var coodrsBasedOnUcs = circle.Center.TransformBy(ecs);
        acDoc.Editor.WriteMessage(String.Format("X{0}, Y{1}, Z{2}", coodrsBasedOnUcs.X, coodrsBasedOnUcs.Y, coodrsBasedOnUcs.Z));
    }

  

To go from WCS to Current UCS, you use the inversion of the current UCS matrix, however your code as written will not work unless the current UCS is the WCS. To get the transformation matrix from the WCS to a UCS defined by a CoordinateSystem3d, you use Matrix3d.Identity.CoordinateSystem3d as the 'from' coordinate system in the call to AlignCoordinateSystem():

 

 

    private static void UcsBaseCoords(Circle circle, Point3d newBasePoint)
    {
        var acDoc = Active.Document;

        CoordinateSystem3d coordSystem = new CoordinateSystem3d(newBasePoint, new Vector3d(0, -1, 0),new Vector3d(1, 0, 0));

        CoordinateSystem3d cosy = Matrix3d.Identity.CoordinateSystem3d;
        var ecs = Matrix3d.AlignCoordinateSystem(cosy.Origin, cosy.Xaxis, cosy.Yaxis, cosy.Zaxis, coordSystem.Origin, coordSystem.Xaxis, coordSystem.Yaxis, coordSystem.Zaxis);

        acDoc.Editor.CurrentUserCoordinateSystem = ecs;

        acDoc.Editor.UpdateTiledViewportsInDatabase();
var coodrsBasedOnUcs = circle.Center.TransformBy(ecs.Inverse());
acDoc.Editor.WriteMessage(String.Format("X{0}, Y{1}, Z{2}", coodrsBasedOnUcs.X, coodrsBasedOnUcs.Y, coodrsBasedOnUcs.Z)); }

 


 

Message 3 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Try like this:

 

        private static void UcsBaseCoords(Circle circle, Point3d newBasePoint)
        {
            var acDoc = Active.Document;

            CoordinateSystem3d coordSystem = new CoordinateSystem3d(newBasePoint, new Vector3d(0, -1, 0), new Vector3d(1, 0, 0));

            CoordinateSystem3d cosy = acDoc.Editor.CurrentUserCoordinateSystem.CoordinateSystem3d;

            var ecs = Matrix3d.AlignCoordinateSystem(
                Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, 
                coordSystem.Origin, coordSystem.Xaxis, coordSystem.Yaxis, coordSystem.Zaxis);

            acDoc.Editor.CurrentUserCoordinateSystem = ecs;

            acDoc.Editor.UpdateTiledViewportsInDatabase();

            var coodrsBasedOnUcs = circle.Center.TransformBy(ecs.Inverse());
            acDoc.Editor.WriteMessage(String.Format("X{0}, Y{1}, Z{2}", coodrsBasedOnUcs.X, coodrsBasedOnUcs.Y, coodrsBasedOnUcs.Z));
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub