How to use Persistent settings globally

Hello everybody.

I want to save my plugin settings. I have seen in other threads that the best way to do it is to use Persistent Settings.

I am having problems implementing it.

I find this thread:

This was a point in the right direction. I implemented a similar code in my Plugin.cs file.
It worked well, but I noticed that the setting saved there is not possible to be accessed in the RunCommand classes. As if every class has its own isolated settings environment.

I am trying to implement a central place to store all the settings.

What I did:
I created a static Settings class:

public static class CWSettings
    {
        // General
        public static double tolerance;
        public static double angleTolerance;

        // Shrink Panels
        public static double spOffset;
        public static double planeCurveToleranceFactor;
    }

Than I added some code to the main plugin file. Want to create 2 methods: loading and saving.
What I noticed that even when you are in the main plugin class, a method created by me cannot access the Settings persistent settings. I need to pass it from somewhere. The only way I was able to solve the loading was:

protected override LoadReturnCode OnLoad(ref string errorMessage)
{
	LoadSettings(Settings);
	return base.OnLoad(ref errorMessage);
}


public static void LoadSettings(Settings)
{
	CWSettings.spOffset = Settings.GetDouble("spOffset", 0.5);
	CWSettings.planeCurveToleranceFactor = Settings.GetDouble("planeCurveToleranceFactor", 10);
}

This way I can load all my settings when the plugin loads.

Saving them causes the main challenge. I want to be able to call the save function from any RunCommand class, so I made it static. But if I don’t pass in the correct settings class, it will not save it to the correct location.

The bellow code will not work. I cannot grab directly the Settings property (it should be a public property… PlugIn.Settings Property) from the class. So I need to reference it but how?

public static void SaveSettings()
{
	// Shrink panels
	Settings.SetDouble("spOffset", CWSettings.spOffset);
	Settings.SetDouble("planeCurveToleranceFactor", CWSettings.planeCurveToleranceFactor);
}

Does somebody have any suggestion how to do it?
Or somebody has an idea how to do it a better way?

Thank you in advance,
Barna

remove the static keyword from both SaveSetting and LoadSetting. That way you get access to the Settings property. Since LoadSettings is static you need to pass in the Settings. The same is true for your current SaveSettings. But making them instance methods will give them access to the Settings property of the plug-in, and no need to pass in Settings to LoadSettings.

Thank you Nathan,

It was a quick and good response.
It shows that I have quite some holes in my C# knowledge… :sweat_smile:

In this case, how can I call the save function from a different class. We are not supposed to instantiate this class. It is instantiated by Rhino itself.

How can I initiate a saving procedure?

I have realized that I can use

MyPlugin.Instance.CustomMethod() to call my custom save method without needing to make it static.
This makes it possible to implement a custom save method as well.

You can always retrieve a plug-in instance by using either PlugIn.Find Method (Guid) or PlugIn.Find Method (Assembly).