.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Set screen center to my point

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
pva75
1558 Views, 5 Replies

Set screen center to my point

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.

5 REPLIES 5
Message 2 of 6
Hallex
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
Message 3 of 6
pva75
in reply to: Hallex

No changes 😞

Message 4 of 6
pva75
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.

Message 5 of 6
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 6
pva75
in reply to: _gile

Wow! It works! Thanks, gile!

 

Pavel.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost