Click or drag to resize

Generic Plugin Example

Below, an example implementation of a RelaxISPlugin_Generic is shown.

The function of the plugin is chosen for illustrative purposes.

Requirements
  • Reference to the RelaxIS_SDK.dll

  • .NET Framework 4.7.2 target (class library)

  • Recommended: Development environment with compiler e.g. RelaxIS SDK Code Editor, Microsoft Visual Studio

Demonstrates

This examples illustrates the implementation of a Generic plugin.

It illustrates how a button is created to allow the user to call a function that changes metadata on spectra.

Example
C#
// <copyright file="MyGenericPlugin.cs" company="rhd instruments GmbH and Co. KG">
// Copyright (c) rhd instruments GmbH and Co. KG. All rights reserved.
// Licensed under the MIT No Attribution (MIT-0) license. See section 'License' in the 'SDK Examples / Tutorials' topic for full license information.
// </copyright>

namespace RelaxIS_SDK_Examples.Plugins
{
    /***
     * Generic plugins do not contain any inherent functionality. However,
     * they have access to the RelaxISProgramInterface and RelaxISWCFInterface
     * functions that allow implementation of custom functionality.
     * This makes them very flexible with regards to application areas.
     */

    using System;
    using System.Windows.Forms;
    using RelaxIS_SDK.Plugins;

    /// <summary>
    /// Defines an example <see cref="RelaxISPlugin_Generic"/> class.
    /// </summary>
    public class MyGenericPlugin
        : RelaxISPlugin_Generic
    {
        private string buttonGuid;
        private int tag;

        /***
         * First, implement the default plugin properties Name and Description that describe the plugin.
         * This is mainly used for display purposes in the case of AxisValue plugins, e.g. the name appears in the list of available axis values.
         */

        /// <inheritdoc/>
        public override string Name
        {
            get { return "My Generic Plugin"; }
        }

        /// <inheritdoc/>
        public override string Description
        {
            get { return "A generic plugin that adds a ribbon button."; }
        }

        /***
         * In order for the plugin to do something, the user must be able to access functions in it.
         * For this one or multiple ribbon buttons can be defined. Each button defines a callback, that
         * is executed when the user clicks on the button.
         * The button information contain information about button title and location (tab, panel),
         * an optional image, whether the button is active.
         * The RegisterRibbonButton returns an identifier. Using this, the button can later be changed or removed.
         */

        /// <inheritdoc/>
        public override void AfterStartupInitialization()
        {
            this.tag = 0;
            var button = GetButtonInformation(0);

            // Register the callback, so that the HandleButtonClick function is called when the user clicks the button.
            button.Callback = this.HandleButtonClick;

            // Add the button to the RelaxIS main menu, keep the GUID around to later update the button.
            this.buttonGuid = this.RelaxISProgramInterface.RegisterRibbonButton(button);
        }

        /***
         * The button information contain information about button title and location (tab, panel),
         * an optional image, whether the button is active.
         * If Callback is left null, the existing callback will not be changed when the ChangeRibbonButton
         * function is called.
         */

        private static ButtonInformation GetButtonInformation(int tag)
        {
            var button = new ButtonInformation()
            {
                Active = true,
                ButtonTitle = string.Format("Call Generic Plugin ({0})", tag),
                PanelTitle = "Example Plugins",
                TabTitle = "Main",
            };

            return button;
        }

        /***
         * The button information contain information about button title and location (tab, panel),
         * an optional image, whether the button is active.
         * If Callback is left null, the existing callback will not be changed when the ChangeRibbonButton
         * function is called.
         */

        private void HandleButtonClick(object sender, EventArgs e)
        {
            // Let the user select some spectra
            var spectra = this.RelaxISProgramInterface.SelectSpectra(SpectraSelectionMethod.RelaxISDefault);

            // Get a parent window in order to show some messages.
            var parent = this.RelaxISProgramInterface.GetParentWindow();
            if (spectra.Count > 0)
            {
                // As an example, we change the metadata on the spectra
                for (var i = 0; i < spectra.Count; i++)
                {
                    spectra[i].Metadata.SetOrCreate(RelaxIS_SDK.Common.MetadataNames.FREEVARIABLE, i);
                }

                // Apply the changes back to RelaxIS. The spectra will be updated based on their internal GUIDs.
                this.RelaxISProgramInterface.ApplySpectra(spectra, Array.Empty<string>());

                MessageBox.Show(parent, string.Format("Changed the metadata of {0} spectra!", spectra.Count), "Spectra changed", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(parent, "No spectra were selected", "No spectra changed", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            // Update the button - this adds an incrementing number each time the function is called.
            this.tag++;
            this.RelaxISProgramInterface.ChangeRibbonButton(this.buttonGuid, GetButtonInformation(this.tag));
        }
    }
}
See Also