When and how to use InteropServices Guid attribute

I am not sure I understand use of [System.Runtime.InteropServices.Guid(“00000000-0000-0000-0000-000000000000
”)] attrubute. Should i use it before every command?

Would appreciate if someone could explain when and why should one use this attribute. ( I am not planning to interface with COM. )

Here is typical structure i have used for multiple commands.


    [System.Runtime.InteropServices.Guid("259CB426-2F44-4B03-A818-DAA7413C432D")]
    public class MyProject_Command1 : Command
    {
        public MyProject_Command1()
        {
            Instance = this;
        }

        public static MyProject_Command1 Instance
        {
            get;
            private set;
        }

        public override string EnglishName
        {
            get { return "MyProject_Command1"; }
        }

        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {

            return Result.Success;
        }
    }

    
    [System.Runtime.InteropServices.Guid("dd6c90d3-5fc7-48bb-9776-1e34886cc954")]
    public class MyProject_Command2 : Command
    {
        public MyProject_Command2()
        {
            Instance = this;
        }

        public static MyProject_Command2 Instance
        {
            get;
            private set;
        }

        public override string EnglishName
        {
            get { return "MyProject_Command2"; }
        }

        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
           
            return Result.Success;
        }
    }

Each command has a unique ID, so that Rhino can identify it between sessions to for example store settings. That is why the Guid should be unique.
In Visual Studio you can use Tools > Create Guid to generat a new one easily. There are also online services that can generate them.

1 Like

If you have installed the RhinoCommon Templates for V5, then you can right-click on your project (in Solution Explorer) and pick Add -> New Item and then select “Empty RhinoCommon Command.” This will add a new file to your project and add a new command class - GUID included.

Can you elaborate bit more on store settings part. How would interface would look like? I have used registry to store value over session.

Thank you. That’s a good hint.

https://github.com/dalefugier/SampleCsCommands/blob/master/SampleCsPersistentSettings.cs

If you ever need to generate a new guid you can use PowerShell (Windows->Run->PowerShell), and use:
[guid]::NewGuid();

Comes in handy.

Or, if you happen to use the ReSharper tool, you can use the nguid TAB-expansion.

That is very useful example. Will use that in future.
As a side question how do you integrate github code into you post.

Copy paste the direct link of a github example and it shows up automaticly:

https://github.com/dalefugier/SampleCsCommands/blob/master/SampleCsPersistentSettings.cs

without the quotes will show:

https://github.com/dalefugier/SampleCsCommands/blob/master/SampleCsPersistentSettings.cs

alright. I tried gist link and that didn’t work.