Valued Contributor
Posts: 67
Registered: ‎02-03-2012
Message 1 of 2 (393 Views)

Change WCS to Entity's UCS - problem.

393 Views, 1 Replies
09-09-2013 12:33 AM



I would like to change current WCS to the object's UCS.

In AutoCAD I manually do this:

a) WCS -> OBject.

b) I'm selecting object;

c) PLAN.


I wrote this code:


     if (sourceObject is Entity)
                            Entity ent = (Entity)sourceObject as Entity;
                            Point3dCollection pts = new Point3dCollection();
                            Point3d pt = pts[0];

                            double viewsize = (double)Application.GetSystemVariable("VIEWSIZE");
                            Utilities.Command("UCS", "W");
                            Utilities.Command("UCS", "OB", pt);
                            Point3d center = (Point3d)Application.GetSystemVariable("VIEWCTR");
                            Utilities.Command("PLAN", "C");
                            Utilities.Command("ZOOM", "C", new Point3d(0, 0, 0), viewsize);


        public static void Command(params object[] args)
            ResultBuffer resbuf = new ResultBuffer();
            foreach (object obj in args)
                switch (obj.GetType().Name)
                    case "String":
                        resbuf.Add(new TypedValue((int)LispDataType.Text, obj)); break;
                    case "Int16":
                        resbuf.Add(new TypedValue((int)LispDataType.Int16, obj)); break;
                    case "Int32":
                        resbuf.Add(new TypedValue((int)LispDataType.Int32, obj)); break;
                    case "Double":
                        resbuf.Add(new TypedValue((int)LispDataType.Double, obj)); break;
                    case "Point2d":
                        resbuf.Add(new TypedValue((int)LispDataType.Point2d, obj)); break;
                    case "Point3d":
                        resbuf.Add(new TypedValue((int)LispDataType.Point3d, obj)); break;
                    case "ObjectId":
                        resbuf.Add(new TypedValue((int)LispDataType.ObjectId, obj)); break;
                    case "ObjectId[]":
                        foreach (ObjectId id in (ObjectId[])obj)
                            resbuf.Add(new TypedValue((int)LispDataType.ObjectId, id));
                    case "ObjectIdCollection":
                        foreach (ObjectId id in (ObjectIdCollection)obj)
                            resbuf.Add(new TypedValue((int)LispDataType.ObjectId, id));
                    case "SelectionSetDelayMarshalled":
                    case "SelectionSetFullyMarshalled":
                        resbuf.Add(new TypedValue((int)LispDataType.SelectionSet, obj)); break;
                        throw new InvalidOperationException("Unsupported type in Command() method");

 But it doesn't work well - for the polyline in attachment.


I tried also this way:


 Matrix3d cur = ed.CurrentUserCoordinateSystem;
                            Vector3d x = new Vector3d(1, 0, 0);
                            Vector3d y = new Vector3d(0, 1, 0);
                            Point3d o = new Point3d(0, 0, 0);
                            Point3d center = (Point3d)Application.GetSystemVariable("VIEWCTR");
                            //double viewsize = (double)Application.GetSystemVariable("VIEWSIZE");
                            Matrix3d newUcsMat = Matrix3d.AlignCoordinateSystem(new Point3d(0, 0, 0), new Vector3d(1, 0, 0), new Vector3d(0, 1, 0), new Vector3d(0, 0, 1),
                               ent.Ecs.CoordinateSystem3d.Origin, ent.Ecs.CoordinateSystem3d.Xaxis, ent.Ecs.CoordinateSystem3d.Yaxis, ent.Ecs.CoordinateSystem3d.Zaxis);

                            ed.CurrentUserCoordinateSystem = newUcsMat;

 But still doesn't work.


Can anyone help me how to set current coordinate system to the object's UCS?


ADN Support Specialist
Posts: 714
Registered: ‎03-21-2011
Message 2 of 2 (341 Views)

Re: Change WCS to Entity's UCS - problem.

09-24-2013 11:09 PM in reply to: GrzesiekGP



Depending on which segment of the polyline you choose, the ucs is computed based on start point and end point of that segment.


Here is a blog post to set the WCS to ECS for a selected line. On similar lines, the UCS can be set to align with any segment of the polyline.




The command in the blog post might work only when the UCS is initially set to world as it uses the previous UCS in the "AlignCoordinateSystem" method. If change this behavior, you can use the WCS coordinate system as the reference in the call to "AlignCoordinateSystem" method.



Developer Technical Services
Autodesk Developer Network

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.