Viewport in the form

Hello ,

I need create a windows form with Rhino viewport, Any idea how I can make this ??

Thanks

Hi @MatrixRatrix,

RhinoCommon does not provide a way of embedding a full-featured viewport into a form or dialog.

However, Rhino.UI has a simple view-only control - 'Rhino.UI.Controls.ViewportControl`. Here is a simple example of how to use it.

– Dale

4 Likes

Hi @dale , and add layout viewport ? is possible ?

Thanks

Hi @MatrixRatrix,

Sure. To add a layout, you can either script the -Layout command, or use the ViewTable.AddPageView RhinoCommon method.

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_DocObjects_Tables_ViewTable_AddPageView_1.htm

– Dale

@dale ,

This Add layout on form?? if so, how?

No, this adds a layout to the document.

– Dale

Dale Fugier

Can I Shade or Render this viewport in the form?
How I do?

Thanks

Another feature that would solve the problem would be to be able to orbit the model in the main viewport.

Thanks

Sure, just do something like this:

m_viewport_control.Viewport.DisplayMode = mode;
m_viewport_control.Refresh();

where:

m_viewport_control = Rhino.UI.Controls.ViewportControl
mode = Rhino.Display.DisplayModeDescription

– Dale

Dale Fugier

I need the ETO dialog box to lose focus and let me zoom and rotate in Rhino’s main viewport.
It would be better than having the viewport inside the ETO.

Why the viewport control embed in plugin dialog display the same as model in Rhino Viewport?,is this a bug?

Hi @dale, is it possible to set the projection in the viewport, say Top?

I’m trying with

mode = Rhino.Display.DisplayModeDescription.FindByName('Shaded')
proj = Rhino.Display.DefinedViewportProjection.Top
        
viewport0 = Rhino.UI.Controls.ViewportControl(Size = drawing.Size(400, 400))
viewport0.Viewport.DisplayMode = mode
Rhino.UI.Controls.ViewportControl.Viewport.PropertyType.SetProjection(viewport0, proj,'viewname', False)
viewport0.Refresh()

to set the projection, but ViewportControl doesn’t like that, so I’m guessing if there is another way to set projection for ViewportControl

And I couldn’t find Rhino.UI.Controls.ViewportContrls in the namespace window to the left of the python editor screen

Thanks for the advice.

Actually, I think I stumbled across the solution…

SetProjection works for a Rhino Viewport, not ViewportControl

So I changed the code to this: (I used the 2 print statements to tell me the type of each)

        mode = Rhino.Display.DisplayModeDescription.FindByName('Shaded')
        proj = Rhino.Display.DefinedViewportProjection.Top
        
        viewport0 = Rhino.UI.Controls.ViewportControl(Size = drawing.Size(400, 400))
        viewport0.Viewport.DisplayMode = mode
        
        #print viewport0.Viewport.DisplayMode
        #print viewport0.Viewport
        viewport0.Viewport.SetProjection( proj, '', False)
2 Likes

I tried this code, even modified it a bit wit success, but every time I run it it adds two viewports to the lisf of viewports in my Rhino document. I didn’t find a way to delete these viewports as I close the Eto window. As you might imagine the mess quickly piles up, because each script run adds 2 unnecessary viewports.

How can I delete these viewports (or even better - do not create them at all) as I close the Eto window?

1 Like

Hi @moby-dk ,
Same issue here on my end using a Rhino.UI.Controls.Viewport Control in R8 with Python 3 and same issue reported here as well:

I can’t say that I’m glad you have the same problem, but maybe it will gain more developers’ attention since it affects more users. Thanks for the links, I didn’t know about these posts.

1 Like

haha right, agreed.

The irony for me is that the ā€œpopup viewportsā€ we don’t want to see actually have the full featured capability of a Rhino viewport that I WANT to embed into Eto.

The Rhino.UI.Control.ViewportControl that does get embedded into Eto really acts more like a viewer and not a full featured Rhino.Viewport.

While I think this is a relevant thing to have, a full featured, embeddable Rhino.Viewport within an Eto.Form is really what I would like to have for my developments.

When you’re form is closing, do something like this (note - I have not tried this):

var viewport = myViewportControl.Viewport
if (null != viewport)
{
  var parentView = viewport.ParentView;
  if (null != parentView)
    parentView.Close();

– Dale

1 Like

Hi @dale,

I attempted to utilize that idea here but it gives None as a Parent:

    def OnFormClosed(self, sender, e):
        # Close Rogue Viewports
        try:
            if self.viewport_control is not None:
                vpc_viewport = self.viewport_control.Viewport
                # Get the parent view of the viewport
                vpc_parent = vpc_viewport.ParentView

                # Print the parent view
                if vpc_parent:
                    vpc_parent.Close()
                    Rhino.RhinoApp.WriteLine("Parent View: " + vpc_parent.MainViewport.Name)
                else:
                    Rhino.RhinoApp.WriteLine("No parent view found.")

        except Exception as ex:
            Rhino.RhinoApp.WriteLine(f"Close Viewports: {ex}")

When executing a script that creates a viewport control and running this statement to print the active viewports in the model you can see that Custom view shows up:

if __name__ == '__main__':
    CreateMainForm()
    # Get all open views in the document
    views = Rhino.RhinoDoc.ActiveDoc.Views

    # Iterate through each view and print the viewport names
    for view in views:
        viewport = view.ActiveViewport
        Rhino.RhinoApp.WriteLine("Viewport Name: " + viewport.Name)

Print Statement:
Viewport Name: Perspective
Viewport Name: Top
Viewport Name: Front
Viewport Name: Right
Viewport Name: Custom view

This code does seem to properly close the ā€œextra duplicateā€ viewport on form close (though this would also close a ā€œvalidā€ Custom view if applicable outside of the Eto.Form as well (if someone added a custom floating view from Rhino for instance):

# Form Class Initialization Code Above This ^

    def OnFormClosed(self, sender, e):
        # Close Rogue Viewports
        try:
            if extra_views:
                for view in extra_views:
                    view.Close()

        except Exception as ex:
            Rhino.RhinoApp.WriteLine(f"Close Extra Viewports: {ex}")

def CheckViewports():
    extra_views = []
    # Get all open views in the document
    views = Rhino.RhinoDoc.ActiveDoc.Views

    # Iterate through each view and print the viewport names
    for view in views:
        viewport = view.ActiveViewport
        if viewport.Name == "Custom view":
            extra_views.append(view)

        Rhino.RhinoApp.WriteLine("Viewport Name: " + viewport.Name)
    return extra_views

if __name__ == '__main__':
    extra_views = CheckViewports()