Get viewport name as String with c#

How do I get the name of the viewport in c#?
I am working on a grasshopper plugin and would like to write it in c#.


GetViewNames.gh (11.3 KB)

In the case of python

import rhinoscriptsyntax as rs

a = rs.ViewNames()
b = rs.NamedViews()

In c#, I wrote the following.

var activeView = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport.Name;
var view = Rhino.RhinoDoc.ActiveDoc.Views;
var namedViews = Rhino.RhinoDoc.ActiveDoc.NamedViews;

A = activeView;
B = view;
C = namedViews;

The active viewport of A is output as a String, but B, C are output as Rhino.DocObjects.Tables.ViewTable.

Can this be changed to String?

I would like to get a list of default views and NamedViews.

Thank you very much.

Iterate over the view table for named views to access the Name of each named view.

List<string> namedViews = new List<string>();

foreach(var nv in Rhino.RhinoDoc.ActiveDoc.NamedViews)
{
    namedViews.Add(nv.Name);
}

A = namedViews;

1 Like
Rhino.DocObjects.Tables.ViewTable viewTable = Rhino.RhinoDoc.ActiveDoc.Views;
Rhino.Display.RhinoView[] views = viewTable.GetViewList(true, true);
List<string> viewsAsString = new List<string>();
foreach(Rhino.Display.RhinoView view in views)
{
    viewsAsString.Add(view.ActiveViewport.Name);
}
B = viewsAsString;

Something like this?

1 Like

@nathanletwory @Ayoub
Thank you for answering my question!

I combined the two replies and wrote the following.

List<string> viewAsString = new List<string>();

var views = Rhino.RhinoDoc.ActiveDoc.Views.GetViewList(true, false);
var namedViews = Rhino.RhinoDoc.ActiveDoc.NamedViews;

foreach(var v in views)
{
    viewAsString.Add(v.ActiveViewport.Name);
}

foreach(var nv in namedViews)
{
    viewAsString.Add(nv.Name);
}

A = viewAsString;

I was able to get a list of Views and NamedViews.
Thank you very much!