Author Topic: Move selection to origin  (Read 1073 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Move selection to origin
« on: March 17, 2013, 07:39:16 AM »
      C#
   
Code: [Select]
        [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);
            }
        }

      VB.NET

      
Code: [Select]
<System.Security.SuppressUnmanagedCodeSecurity> _
<DllImport("acad.exe", EntryPoint := "acedCmd", CharSet := CharSet.Unicode, CallingConvention := CallingConvention.Cdecl)> _
Private Shared Function acedCmd(resbuf As IntPtr) As Integer
End Function

<CommandMethod("moveToOrig, mto", CommandFlags.UsePickSet Or CommandFlags.Redraw)> _
Public Shared Sub testMoveToOrigin()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim rb As New ResultBuffer()
Try

rb.Add(New TypedValue(5005, "_Zoom"))
rb.Add(New TypedValue(5005, "_Extents"))
acedCmd(rb.UnmanagedObject)

Dim ucs As Matrix3d = ed.CurrentUserCoordinateSystem
Dim ccos As CoordinateSystem3d = ucs.CoordinateSystem3d
Dim orig As Point3d = ccos.Origin.TransformBy(Matrix3d.Identity)
' select all objects
Dim sset As SelectionSet = ed.SelectAll().Value
If sset Is Nothing Then
Return
End If
Dim pts As New List(Of Point3d)()
Dim ents As New List(Of Entity)()
Dim mmx As New Matrix3d()
Using tr As Transaction = doc.TransactionManager.StartTransaction()
' iterate through selected objects
For Each id As ObjectId In sset.GetObjectIds()

Dim ent As Entity = DirectCast(tr.GetObject(id, OpenMode.ForRead, False), Entity)

Dim ext As Extents3d = ent.GeometricExtents
If ext IsNot Nothing Then
Dim minpt As Point3d = ext.MinPoint.TransformBy(Matrix3d.Identity)
' collect entities to List for the future work
ents.Add(ent)
pts.Add(minpt)
End If
Next
' Get most lower left point of screen
Dim xpt As Point3d = pts.OrderBy(Function(p) p.X).First()
' calculate minimal X value
Dim ypt As Point3d = pts.OrderBy(Function(p) p.Y).First()
' calculate minimal Y value
Dim mp As Point3d = New Point3d(xpt.X, ypt.Y, orig.Z).TransformBy(Matrix3d.Identity)
mmx = Matrix3d.Displacement(orig - mp)
' iterate through gathrered entities again
For Each e As Entity In ents
e.UpgradeOpen()
' apply transformation matrix
e.TransformBy(mmx)
Next

tr.Commit()
End Using
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage((vbLf + ex.Message & vbLf) + ex.StackTrace)
Finally
rb = New ResultBuffer()
rb.Add(New TypedValue(5005, "_Zoom"))
rb.Add(New TypedValue(5005, "_Extents"))
acedCmd(rb.UnmanagedObject)
End Try
End Sub