|
I've got a method that zooms and centers the view on an object, based on Gile's contribution here.
My version of the code is as follows:
[ol]///
/// Zooms to and highlight the entity in question.
///
/// The ObjectID of the entity to zoom to.
/// factor to zoom out by, higher number is a wider zoom
public static void ZoomToAndHighlight(ObjectId id, double scale)
{
using (LockedTransaction lTr = Active.Document.TransactionManager.StartLockedTransaction())
{
Entity ent = lTr.GetObject(id, OpenMode.ForRead, false) as Entity;
if (ent!=null)
{
using (ViewTableRecord view = Active.Editor.GetCurrentView())
{
Active.WriteMessage($"Starting view centered on {view.CenterPoint}, height:{view.Height}, width:{view.Width}");
// get the extents
Extents3d ext = ent.GeometricExtents;
// transform extents
Matrix3d WCS2DCS = Matrix3d.PlaneToWorld(view.ViewDirection);
WCS2DCS = Matrix3d.Displacement(view.Target - Point3d.Origin)*WCS2DCS;
WCS2DCS = Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target);
WCS2DCS = WCS2DCS.Inverse();
ext.TransformBy(WCS2DCS);
double width = ext.MaxPoint.X - ext.MinPoint.X;
double height = ext.MaxPoint.Y - ext.MinPoint.Y;
// calculate center, width and height
Point2d center = new Point2d((ext.MaxPoint.X + ext.MinPoint.X)/2, (ext.MaxPoint.Y + ext.MinPoint.Y)/2);
view.Width = scale * width;
view.Height = scale * height;
view.CenterPoint = center;
Active.WriteMessage($"New view to be centered at {center}, height:{height}, width:{width}, WCS2DCS:{WCS2DCS}");
Active.Editor.SetCurrentView(view);
// this isn't working, this just verifies what the current view is
ViewTableRecord nView = Active.Editor.GetCurrentView();
Active.WriteMessage($"Moved view centered on {nView.CenterPoint}, height:{nView.Height}, width:{nView.Width}");
// TODO highlight the given entity...
}
}
lTr.Commit();
}
}
[/ol]
You can see that I'm writing the coordinates to the command line for troubleshooting purposes. The view in question should be centered on (3183274.93594949,1432379.113886) and is called out as so on the command line, but is instead centered on approximately (6365835,2864340) as shown in the cursor location. It's almost like the coordinates are getting doubled when the view is being set in SetCurrentView(view).
I double check the currentview after the move with the nView variable and it's showing the values as they were set, not whats shown on the screen.
Anyone got any ideas as to what I'm missing? UCS is set to world, but that shouldn't matter with the transformation. |
|