rs.ShowGrid() does not return correct state

Hi,

The rs.ShowGrid() function returns True when the grid is off. Should it not return False instead?

Hi @AMG,

The video doesn’t really help.

But, rs.ShowGrid() returns true if the grid is on in the active viewport, otherwise false is returned.

Just tested here and it seems to work as advertised.

– Dale

Does not work here for some reason, as you can see from the video (I don’t understand why the video does not help?). The grid is off, and I then run the following code:

#! python3

import rhinoscriptsyntax as rs

print(rs.ShowGrid())

The code prints True in the lower left corner of the viewport. My grid is off and should print False instead.

BUG: The code above only works when using the default Rhino display modes, but not when one is using custom display modes.

Steps to reproduce

  1. Use a custom display mode like TestDisplayMode.ini (7.6 KB).
  2. Turn of the grid with _Grid _ShowGrid=No _EnterEnd.
  3. Run the following script gridstate.py (65 Bytes) or copy the code below:
#! python3

import rhinoscriptsyntax as rs

print(rs.ShowGrid())

Observations

The script does not return the correct grid state when using a custom display mode. If you run the script with one of the default display modes (Wireframe etc) the script outputs the correct grid state.

Video

Here we can see how the script behaves with a default display mode and afterwards a custom display mode. See the lower left corner of the viewport for the output from the script.

Works correctly in one or two custom display modes I have here if the Grid display in that custom mode is set to “Use Document Settings”. The Grid setting in a display mode can be set to override the document default, but it looks like the rs method only checks the document default setting, so if you have set the display mode to “Mode Specific” for the grid, it no longer reports correctly.

Isn’t that a bug, though? The function should always report the state of the grid in the active view (in my opinion).

It’s a good question. I think there is a confusion between View and Viewport. The Help is also misleading. Despite what the Help says, the method reports the state of the grid in the active Viewport, not the View.

def ShowGrid(view=None, show=None):
    """Shows or hides a view's construction plane grid
    Parameters:
      view (str|guid, optional): title or id of the view. If omitted, the current active view is used
      show (bool, optional): The grid state to set. If omitted, the current grid display state is returned
    Returns:
      bool: If show is not specified, then the grid display state if successful
      bool: If show is specified, then the previous grid display state if successful
    Example:
      import rhinoscriptsyntax as rs
      view = rs.CurrentView()
      if rs.ShowGrid(view)==False:
          rs.ShowGrid( view, True )
    See Also:
      ShowGridAxes
      ShowWorldAxes
    """
    view = __viewhelper(view)
    viewport = view.ActiveViewport
    rc = viewport.ConstructionGridVisible
    if show is not None and rc!=show:
        viewport.ConstructionGridVisible = show
        view.Redraw()
    return rc

Notice the lines

    viewport = view.ActiveViewport
    rc = viewport.ConstructionGridVisible

It is still checking the viewport state (i.e. the document grid settings) and not the actual view grid settings with possible overrides. This was probably set up a long time before display modes with this option existed.

The proof is if you have a display mode set to “Use document settings” with the grid showing, and you switch to another display mode that has a custom grid override set to hide the grid. If you switch back and forth between the two display modes, the grid will appear and disappear.

So the method probably needs to be revised to check the current display mode in the active viewport and whether it is set to show the grid or not. I’m not actually certain that these settings are exposed in RhinoCommon.

The grid is either on or off. It should never report that it is on when it is clearly off and vice versa. It does not make sense. I agree with you that the function should be revised.

I created a Youtrack item for this, but as I said earlier, I’m not sure the display mode settings to detect this situation are exposed in RhinoCommon, so a Rhinoscriptsyntax correction might not currently be possible.

https://mcneel.myjetbrains.com/youtrack/issue/RH-82449/rs.ShowGrid-only-reports-the-document-setting-not-the-actual-view-setting

1 Like