For a while I worked on a Revit plugin which searches for analytical nodes that are so close to each other but not connected. These double nodes may create problems, resulting in an incoherent analytical model and hinder the structural analysis procedure.
So after finding and listing such nodes I wanted to be able to pan the view to get that node at the center of the view and than zoom in. Below is a sample method that would help you understand the general idea.
Keep in mind that while zooming with ZoomAndCenterRectangle() method, especially if you are dealing with very small numbers, do not use arithmetic operators + and – to calculate coordinates, use * and / instead.
private void zoompan(UIDocument adoc, ElementId nodeId)
{
Document doc = adoc.Document;
View view = doc.ActiveView;
UIView uiview = null;
IList<UIView> uiviews = adoc.GetOpenUIViews();
foreach (UIView uv in uiviews) // loop to determine current view
{
if (uv.ViewId.Equals(view.Id))
{
uiview = uv;
break;
}
}
//PAN CENTER
double panstep = 50; //pan step, this determines the speed of the operation. higher the slower
ReferencePoint node = (ReferencePoint)doc.GetElement(nodeId); // cast the returning element into a ReferencePoint
IList<XYZ> corners = uiview.GetZoomCorners(); // current view corner coordinates
XYZ p0 = corners[0]; // left bottom
XYZ q0 = corners[1]; // right top
XYZ vd0 = (q0 - p0) / 2; // half the screen size
XYZ center = node.Position; // this will be our new center
XYZ pc = center - vd0; // new left bottom
XYZ qc = center + vd0; // new right top
XYZ dp = (p0 - pc); // vector from old left bottom to new
XYZ dq = (q0 - qc); // vector from old right top to new
for (double i = 0.0; i <= panstep; i++)
{
//y=(x-1)^3+1 I find this is as a suitable curve to pan smoothly
double x = i / panstep;
double y = (Math.Pow((x - 1), 3) + 1);
pc = p0 - dp * y;
qc = q0 - dq * y;
uiview.ZoomAndCenterRectangle(pc, qc);
}
//ZOOM IN
double zoomstep = 100; //zoom step
corners = uiview.GetZoomCorners(); // current view corner coordinates
p0 = corners[0]; // left bottom
q0 = corners[1]; // right top
vd0 = q0 - p0; // screen size
pc = center - vd0 * 0.1;// new left bottom, here you can determine zoom level such as 0.1 => 5x zoom
qc = center + vd0 * 0.1;// new right top
dp = (p0 - pc); // vector from old left bottom to new
dq = (q0 - qc); // vector from old right top to new
for (double i = 0.0; i <= zoomstep; i++)
{
//y=(x-1)^3+1 Same curve as above, you can try out different equations
double x = i / zoomstep;
double y = (Math.Pow((x - 1), 3) + 1);
pc = p0 - dp * y;
qc = q0 - dq * y;
uiview.ZoomAndCenterRectangle(pc, qc);
}
}