马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
- [System.Security.SuppressUnmanagedCodeSecurity]
- [DllImport("acad.exe", EntryPoint = "acedCmd", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
- extern static private int acedCmd(IntPtr resbuf);
- [CommandMethod("moveToOrig, mto", CommandFlags.UsePickSet | CommandFlags.Redraw)]
- static public void testMoveToOrigin()
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- ResultBuffer rb = new ResultBuffer();
- try
- {
- rb.Add(new TypedValue(5005, "_Zoom"));
- rb.Add(new TypedValue(5005, "_Extents"));
- acedCmd(rb.UnmanagedObject);
- Matrix3d ucs = ed.CurrentUserCoordinateSystem;
- CoordinateSystem3d ccos = ucs.CoordinateSystem3d;
- Point3d orig = ccos.Origin.TransformBy(Matrix3d.Identity);
- // select all objects
- SelectionSet sset = ed.SelectAll().Value;
- if (sset == null) return;
- List<Point3d> pts = new List<Point3d>();
- List<Entity> ents = new List<Entity>();
- Matrix3d mmx = new Matrix3d();
- using (Transaction tr = doc.TransactionManager.StartTransaction())
- {
- // iterate through selected objects
- foreach (ObjectId id in sset.GetObjectIds())
- {
- Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead, false);
- Extents3d ext = ent.GeometricExtents;
- if (ext != null)
- {
- Point3d minpt = ext.MinPoint.TransformBy(Matrix3d.Identity);
- // collect entities to List for the future work
- ents.Add(ent);
- pts.Add(minpt);
- }
- }
- // Get most lower left point of screen
- Point3d xpt = pts.OrderBy(p => p.X).First();// calculate minimal X value
- Point3d ypt = pts.OrderBy(p => p.Y).First();// calculate minimal Y value
- Point3d mp = new Point3d(xpt.X, ypt.Y, orig.Z).TransformBy(Matrix3d.Identity);
- mmx = Matrix3d.Displacement(orig-mp );
- // iterate through gathrered entities again
- foreach (Entity e in ents)
- {
- e.UpgradeOpen();
- // apply transformation matrix
- e.TransformBy(mmx);
- }
- tr.Commit();
- }
- }
- catch (Autodesk.AutoCAD.Runtime.Exception ex)
- {
- ed.WriteMessage("\n" + ex.Message + "\n" + ex.StackTrace);
- }
- finally
- {
- rb = new ResultBuffer();
- rb.Add(new TypedValue(5005, "_Zoom"));
- rb.Add(new TypedValue(5005, "_Extents"));
- acedCmd(rb.UnmanagedObject);
- }
- }
|