View.SetActive Event

I am trying to start some action every time a newly created view (named “Testview” below) is activated.
But the event is fired every time any viewport is selected.

import Rhino as rc
import scriptcontext
import System

def new_view_activated(sender, e):
    print "Activated view: {}".format(e.View)

def add_floating_viewport(name, x, y, w, h):
    view_table = rc.RhinoDoc.ActiveDoc.Views
    new_view = view_table.Add(name, rc.Display.DefinedViewportProjection.Perspective, System.Drawing.Rectangle(x,y,w,h), True)
    print "New view: {}".format(new_view)
    return new_view

new_view = add_floating_viewport("Testview", 100,100, 100,100)

func = new_view_activated
scriptcontext.sticky["new_view_activated"] = func
new_view.SetActive += func

I guess I am missing the exact difference between a view and a viewport? But a viewport does not have a setActive event.

Thanks!
Philip

@powerpp,

maybe below helps to differentiate between the current viewport name ?

import Rhino as rc
import scriptcontext
import System

def new_view_activated(sender, e):
    if e.View.ActiveViewport.Name == "Testview":
        print "Testview activated !"
    else:
        print "Activated view: {}".format(e.View.ActiveViewport.Name)


def add_floating_viewport(name, x, y, w, h):
    view_table = rc.RhinoDoc.ActiveDoc.Views
    projection = rc.Display.DefinedViewportProjection.Perspective
    rectangle = System.Drawing.Rectangle(x,y,w,h)
    new_view = view_table.Add(name, projection, rectangle, True)
    print "New view: {}".format(new_view)
    return new_view

new_view = add_floating_viewport("Testview", 100,100, 100,100)

func = new_view_activated
scriptcontext.sticky["new_view_activated"] = func
new_view.SetActive += func

c.

1 Like

Yes that does the trick. Thanks a lot! So if I got it right, a named view can be created and this view can be displayed in different viewports? And does a viewport allways have the same name as the view that is beeing displayed using this viewport?

Philip

I guess so. But note that “Testview” is not a named view, it is just the viewport title of the new Rhino view added to the table. So you cannot switch to the top view and set it to “Testview”. Btw. You can also print the id of an active viewport using

e.View.ActiveViewportID

This might be more reliable in comparison to viewport titles.

c.

1 Like

Thanks for that hint. Helps me to sort out things a bit.