• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Contributor
    Posts: 48
    Registered: ‎07-22-2012
    Accepted Solution

    Set screen center to my point

    211 Views, 5 Replies
    08-27-2012 12:02 PM

    At this time I tried to set screen center. I have some object and I want to set screen position to this object.

     

    What I did:

     

     AcadApplication app = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;

     using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
     {
          Entity en = (Entity)tr.GetObject(objectId, OpenMode.ForRead); // get my obj by Id

          Extents3d ext = en.GeometricExtents;

      

           Point3d p_center = new Point3d( // get center
                            (ext.MinPoint.X + ext.MaxPoint.X) / 2,
                            (ext.MinPoint.Y + ext.MaxPoint.Y) / 2,
                            (ext.MinPoint.Z + ext.MaxPoint.Z) / 2
           );
                        

         // set center point of current view


           ed.UpdateTiledViewportsInDatabase();

     

          ViewportTableRecord viewportTableRec = tr.GetObject(ed.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord;
          viewportTableRec.CenterPoint = new Point2d(p_center.X, p_center.Y);

           ed.UpdateTiledViewportsFromDatabase();

       // Ok!

           tr.Commit();
       }

     

      // set object as selected - it works

      doc.Editor.SetImpliedSelection(new ObjectId[] { objectId });

     

     

    I see that SetImpliedSelection works fine (as I see current object) but screen center  is moved to unknown point.

     

    How I can set screen center to my point?

     

    Thanks,

    Pavel.

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,338
    Registered: ‎10-08-2008

    Re: Set screen center to my point

    08-27-2012 01:31 PM in reply to: pva75

    Something like this will do your work

         

          Matrix3d ucs = ed.CurrentUserCoordinateSystem;
           Point3d p_center = new Point3d( // get center
                            (ext.MinPoint.X + ext.MaxPoint.X) / 2,
                            (ext.MinPoint.Y + ext.MaxPoint.Y) / 2,
                            (ext.MinPoint.Z + ext.MaxPoint.Z) / 2
           ).TransformBy(ucs);

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 48
    Registered: ‎07-22-2012

    Re: Set screen center to my point

    08-27-2012 11:04 PM in reply to: Hallex

    No changes :smileysad:

    Please use plain text.
    Active Contributor
    Posts: 48
    Registered: ‎07-22-2012

    Re: Set screen center to my point

    08-27-2012 11:11 PM in reply to: pva75

    I had created new schema and this code works, but still doesn't work in old, stored file.

    It's very strange.

    It seems in old file I have very specific view but I have no idea what it is.

     

    Thanks,

    Pavel.

    Please use plain text.
    *Expert Elite*
    Posts: 1,640
    Registered: ‎04-29-2006

    Re: Set screen center to my point

    08-28-2012 01:56 AM in reply to: pva75

    Hi,

     

    You need to transform the Extents3d from World Coordinates System to the viewport Display Coordinates System before calculating the center point.

     

            private void SetViewCenterToObject(ObjectId id)
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                using (Transaction tr = doc.TransactionManager.StartTransaction())
                using (ViewTableRecord view = ed.GetCurrentView())
                {
                    Matrix3d DCS2WCS =
                        Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) *
                        Matrix3d.Displacement(view.Target - Point3d.Origin) *
                        Matrix3d.PlaneToWorld(view.ViewDirection);
                    Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                    Extents3d ext = ent.GeometricExtents;
                    ext.TransformBy(DCS2WCS.Inverse());
                    view.CenterPoint = new Point2d(
    (ext.MaxPoint.X + ext.MinPoint.X) / 2.0,
    (ext.MaxPoint.Y + ext.MinPoint.Y) / 2.0); ed.SetCurrentView(view); tr.Commit(); } }

     

    Gilles Chanteau
    Please use plain text.
    Active Contributor
    Posts: 48
    Registered: ‎07-22-2012

    Re: Set screen center to my point

    08-28-2012 03:52 AM in reply to: _gile

    Wow! It works! Thanks, gile!

     

    Pavel.

    Please use plain text.