AutoCAD 2020 Z-axis (ZAXIS) .net align polyline

AutoCAD 2020 Z-axis (ZAXIS) .net align polyline

Anonymous
Not applicable
718 Views
2 Replies
Message 1 of 3

AutoCAD 2020 Z-axis (ZAXIS) .net align polyline

Anonymous
Not applicable

Hello everybody!
In AutoCAD I can use the command "BKS" "ZA" "O" O for object
to align the UCS in the Z axis with an object (polyline).

Is there a way to do this in .NET or C # using code?

I click on a polyline and the UCS aligns with the Z-axis (ZAXIS)
on the polyline?

0 Likes
Accepted solutions (1)
719 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

The easy way is to call the _UCS command.

        [CommandMethod("TEST1")]
        public static void Test1()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var peo = new PromptEntityOptions("\nSelect Polyline: ");
            peo.SetRejectMessage("\nSelected object is not a Polyline.");
            peo.AddAllowedClass(typeof(Polyline), true);
            var per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) 
                return;
            ed.Command("_.ucs", "_object", per.ObjectId);
        }

But you can also mimic the native command by computing the Matrix3d.

        [CommandMethod("TEST2")]
        public static void Test2()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var peo = new PromptEntityOptions("\nSelect Polyline: ");
            peo.SetRejectMessage("\nSelected object is not a Polyline.");
            peo.AddAllowedClass(typeof(Polyline), true);
            var per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK)
                return;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                var origin = pline.StartPoint;
                var xAxis = pline.GetFirstDerivative(pline.StartParam);
                var zAxis = pline.Normal;
                var yAxis = zAxis.CrossProduct(xAxis).GetNormal();
                var ocs = Matrix3d.AlignCoordinateSystem(
                    Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
                    origin, xAxis, yAxis, zAxis);
                ed.CurrentUserCoordinateSystem = ocs;
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

Anonymous
Not applicable

... great, thank you, that helps me a lot.

0 Likes