Hello,
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(); ent.GetStretchPoints(pts); 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); }
Utilities.Command:
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)); break; case "ObjectIdCollection": foreach (ObjectId id in (ObjectIdCollection)obj) resbuf.Add(new TypedValue((int)LispDataType.ObjectId, id)); break; case "SelectionSetDelayMarshalled": case "SelectionSetFullyMarshalled": resbuf.Add(new TypedValue((int)LispDataType.SelectionSet, obj)); break; default: throw new InvalidOperationException("Unsupported type in Command() method"); } } acedCmd(resbuf.UnmanagedObject); }
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?
Hi,
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.
http://adndevblog.typepad.com/autocad/2012/07/align-the-ucs-to-an-entity.html
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.
Regards,