Restoring a viewport after modification

Hi all,

I try to restore a viewport after i made some temporary modifications on it.

ON_3dmView savedView = RhinoApp().ActiveView()->ActiveViewport().View();
    if (savedView.IsValid())
    {

        ... Modifications are done here

        RhinoApp().ActiveView()->ActiveViewport().SetView(savedView);
    }

This doesn’t work… What i am doing wrong ?

The modifications i want to restore are:

  • Viewport size (_-ViewportProperties _Size …)
  • Camera target (RhinoApp().ActiveView()->ActiveViewport().SetCameraDirection(direction); )

Probably i am confusing Views, Viewports etc…
I have tried several Get/Set with no success…so i prefer ask :wink:

You should make a new instance of the view (copy). Also, if that is relevant in your case, the view may have been resized by the user. If that can happen, you need to set the size and the frustum aspect of the view after restoring it.

// to store the view make a new instance
const ON_3dmView& currentView = RhinoApp().ActiveView()->ActiveViewport().View();
ON_3dmView* copy = new ON_3dmView(currentView);

// to restore the view first
// get the size of the viewport as it currently is
int right, left, top, bottom;
RhinoApp().ActiveView()->ActiveViewport().VP().GetScreenPort(&left, &right, &bottom, &top);
double frustumAspect;
RhinoApp().ActiveView()->ActiveViewport().VP().GetFrustumAspect(frustumAspect);
                        
// restore the previously saved viewport
RhinoApp().ActiveView()->ActiveViewport().SetView(*copy);

// the current viewport may not have the same size anymore
// so set the screenport size of the current viewport.
// otherwise, the view is restored with the old size and 
// that just look weird (parts of the screenport are not updated).

const ON_Viewport& vp = RhinoApp().ActiveView()->ActiveViewport().VP();
ON_Viewport& nonConst = const_cast<ON_Viewport&>(vp);
nonConst.SetScreenPort(left, right, bottom, top);
nonConst.SetFrustumAspect(frustumAspect);    

// be sure to delete the copy after use
delete copy;

Thank you very much for your answer.
But it doesn’t work for me.

I’ll try to play around with your code by changing some parts.
I’ll tell you more if it works.

Hi Georges,

You can get the size of the active view as follows:

CRhinoView* view = RhinoApp().ActiveView();
if (0 != view)
{
  CRect rect;
  view->GetClientRect(rect);
  int width = rect.Width();
  int height = rect.Height();
}

You can feed these values into the -ViewportProperties command that you are scripting.

Since you are scripting command, you might as well (also) script the ``’-NamedView``` command, which save and restores view projections. See the help file for details.

Thanks Dale.
This helped a lot.