Running ImportLayout deleting custom display modes (C#)

Hi,
I’m writing a command which imports a page and display mode, but I’m finding the custom display modes are getting deleted by ImportLayout. If I import the display mode and then close and reopen Rhino or open the Options window before running ImportLayout, the display modes don’t get deleted.

Here is some code to recreate the issue:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
var displayModeId = DisplayModeDescription.AddDisplayMode(“TestDisplayMode”);
var displayMode = DisplayModeDescription.GetDisplayMode(displayModeId);
RhinoApp.WriteLine($“{displayMode.LocalName} has been created.”);

// RhinoApp.RunScript($"Options", true); // Uncomment to prevent the display mode from getting deleted

RhinoApp.RunScript("ImportLayout", true);
return Result.Success;

}

I have looked into the bug some more and found that display modes are stored in Rhino’s setting file (C:\Users\%User%\AppData\Roaming\McNeel\Rhinoceros\8.0\settings\settings-Scheme__Default.xml). Using DisplayModeDescription.AddDisplayMode will create the display mode but not save it to file. Opening and closing the Options window will though. My guess is ImportLayouts is reverting the display modes to what is saved in the settings file, which, given the file hasn’t been saved to include the display modes, is causing the display modes to get deleted.

Is there a method to get Rhino to save to the settings file?

Hi @Stephen15,

Try this:

#! python 3
import Rhino

# Adds a new display mode.
dm_id = Rhino.Display.DisplayModeDescription.AddDisplayMode("TestDisplayMode")

# Get the new display mode
dm = Rhino.Display.DisplayModeDescription.GetDisplayMode(dm_id)

# TODO: make any changes here

# Update it and save it to Rhino's settings
Rhino.Display.DisplayModeDescription.UpdateDisplayMode(dm)

– Dale

It works, thanks Dale!

Here is the final working code:

var dm_id = DisplayModeDescription.ImportFromFile("example.ini");
var dm = DisplayModeDescription.GetDisplayMode(dm_id);
dm.InMenu = !dm.InMenu; // Rhino will only save if it detects a change
DisplayModeDescription.UpdateDisplayMode(dm);
dm.InMenu = !dm.InMenu; // Revert back
DisplayModeDescription.UpdateDisplayMode(dm);
// imported display mode is now saved