.NET

Reply
Active Contributor
pva75
Posts: 48
Registered: ‎07-22-2012
Message 1 of 6 (667 Views)
Accepted Solution

Set screen center to my point

667 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.

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(); } }

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 6 (657 Views)

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
Active Contributor
pva75
Posts: 48
Registered: ‎07-22-2012
Message 3 of 6 (636 Views)

Re: Set screen center to my point

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

No changes :smileysad:

Active Contributor
pva75
Posts: 48
Registered: ‎07-22-2012
Message 4 of 6 (635 Views)

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.

*Expert Elite*
_gile
Posts: 2,133
Registered: ‎04-29-2006
Message 5 of 6 (628 Views)

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
Active Contributor
pva75
Posts: 48
Registered: ‎07-22-2012
Message 6 of 6 (619 Views)

Re: Set screen center to my point

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

Wow! It works! Thanks, gile!

 

Pavel.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.