Set screen center to my point

Set screen center to my point

Anonymous
Not applicable
2,512 Views
5 Replies
Message 1 of 6

Set screen center to my point

Anonymous
Not applicable

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.

0 Likes
Accepted solutions (1)
2,513 Views
5 Replies
Replies (5)
Message 2 of 6

Hallex
Advisor
Advisor

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
0 Likes
Message 3 of 6

Anonymous
Not applicable

No changes 😞

0 Likes
Message 4 of 6

Anonymous
Not applicable

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.

0 Likes
Message 5 of 6

_gile
Consultant
Consultant
Accepted solution

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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 6

Anonymous
Not applicable

Wow! It works! Thanks, gile!

 

Pavel.

0 Likes