Click or drag to resize

Data Manipulation 2 Example

Below, an example implementation of a RelaxISPlugin_DataManipulation2 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 DataManipulation2 plugin.

Executing the plugin changes the DataSource property of the input spectra and adds a new spectrum to the current project.

It also illustrates running the plugin asynchronously for longer running tasks.

Example
C#
// <copyright file="MyDataManipulation2.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
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using RelaxIS_SDK.Common;
    using RelaxIS_SDK.Plugins;

    /***
     * The DataManipulation2 plugin type can freely modify a collection of spectra that are selected by
     * the user. It is called via a button in the main menu that can be defined in the ButtonInformation
     * property. Alternatively it may also be called from non-userinterface functions, as indicated by
     * the ExecutionMode argument on the main function (see below).
     */

    /// <summary>
    /// Defines an example <see cref="RelaxISPlugin_DataManipulation2"/> class.
    /// </summary>
    public class MyDataManipulation2
        : RelaxISPlugin_DataManipulation2
    {
        /***
         * First, implement the default plugin properties Name and Description that describe the plugin.
         * This is mainly used for display purposes.
         * The name is used as the button title if ButtonInformation is not overridden.
         */

        /// <inheritdoc/>
        public override string Name
        {
            get
            {
                return "MyDataManipulationPlugin2";
            }
        }

        /// <inheritdoc/>
        public override string Description
        {
            get
            {
                return "An example data manipulation v2 plugin that adds the count of the spectra to the data source and also adds a new spectrum.";
            }
        }

        /***
         * Override this property to define what the button for this plugin looks like.
         * It is possible to define the tab and panel titles to display the button in
         * one of the existing panels in the main menu instead of on the 'Plugins' tab.
         */

        /// <inheritdoc/>
        public override ButtonInformation ButtonInformation
        {
            get
            {
                var res = new ButtonInformation()
                {
                    Active = true,
                    ButtonTitle = "Run MyDataManipulation2",
                    Image = Convert.FromBase64String(@"iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAH6SURBVHhe7ZiBjsMgCIbrPcbe/9nuNXplgYQSEPVs5wpfYtgoU/xFZ1r2fd8i84M2LOEFMLdAKeVRe+OYZ8GPJ3ILoA3LaQs8rewt+HbICvAqwDo8vgVvTnkIog1LCoA2LCkA2rDcJkB5bTs0/LoMTfcAK/H9d2u+I1AfPb+x0PqSOdKzS+8Bq6yolkdrbl0CgKrU0LUUI/lNPQNAdd7QrSJjebz8Dmi+Gq0idAlASVAifBAtuZ6EAdkvfdfG04A4auhyGa4AKxnwe4kCFNcaX0P7fasIQ2cAfLYG6FkBitXi5TieSJSbFyeZegYAPJFaMnxiWpycuCYSUXvmMSRALSnw8YZuk9Y4j54xOdMqoLbaGrV4mgTFeFWg9dWaT74RQhuWFABtWFIAtGFJAdCG5RIBRm5kn6L7ItRyG5M3uRmM9nnbRehbVlwyLACsBDV0XcpVY91yCEJ1yAqp+WTjz96BB9oz/ryVZf4FKHm50t6qy0n3ijAsACneO+BsSDBPKItpFTCawKeZcgjOmDz1watqRr8et54BtW3DJz1L1BZuEUBORpsc+bhImlCzWeaVGK+At+NA8/WSr8QclnopqpX8f1Yf8OaUb4XRhiUFQBuWFABtWFIAtGFx7wFPJO8BjFMFcJ5WDdaNNg9BtGExt0AUcgugDcq2/QEL5WcAvUXq8gAAAABJRU5ErkJggg=="),
                    PanelTitle = "Data Manipulation Plugins",
                    TabTitle = "Example Plugins",
                };

                return res;
            }
        }

        /***
         * This defines which spectra are used for running the plugin.
         * This can be used to override the default RelaxIS method as configured in
         * the RelaxIS setup.
         */

        /// <inheritdoc/>
        public override SpectraSelectionMethod SpectraSelectionMethod
        {
            get
            {
                return SpectraSelectionMethod.CurrentlySelected;
            }
        }

        /***
         * This is the main function that manipulates the list of spectra.
         * The spectra selected by the user as given in the 'spectra' argument.
         * 'mode' defines if the plugin was called via the user interface by clicking the button, or from
         * another non-interface function.
         * 'parent' is a handle to a parent form. This can be used if 'mode' is 'FromUserInterface' as a
         * parent for displaying a custom user interface dialog modally.
         * 'cancellation' is a token that should be watched during long-running tasks. The user can cancel
         * the execution of the plugin which is signalled via this token.
         * In general the function is intended as an async/await function and it is recommended to run
         * long-running tasks asynchronously via await Task.Run(...).
         *
         * Every change in the source list is written back to RelaxIS. The spectra in the list are linked
         * to the source spectra via their GUID properties. If a spectra with a certain GUID is no longer
         * present in the list when the function returns it is removed from the source project.
         * New GUIDs are added to the source project.
         * Changes to existing GUIDs are written back to the respective source spectrum.
         *
         * This applies to every aspect of the spectrum: Data, metadata, Analysis details (models, parameters...).
         */

        /// <inheritdoc/>
        public override async Task ManipulateSpectraAsync(IList<ImpedanceSpectrum> spectra, ExecutionMode mode, IWin32Window parent, CancellationToken cancellation)
        {
            var count = spectra.Count;
            var idx = 0;
            foreach (var s in spectra)
            {
                s.Datasource = string.Format("({0} / {1}) {2}", ++idx, count, s.Datasource);
            }

            var spec = spectra.FirstOrDefault();
            if (spec == null)
            {
                return;
            }

            var newSpec = new ImpedanceSpectrum
            {
                Datasource = string.Format("{0} multiplied by {1}", spec.Datasource, count),
            };

            // For the data, use the first spectrum data but multiplied by the total number of selected spectra.
            // This is not useful, but can serve as an example.

            // Simulate a long running task that runs asyncronously.
            await Task.Run(
                () =>
                {
                    foreach (var p in spec.Data)
                    {
                        newSpec.Data.Add(new EISDatapoint(p.Impedance.Frequency, p.Impedance.Real * count, p.Impedance.Imaginary * count));
                        Thread.Sleep(50);
                        cancellation.ThrowIfCancellationRequested();
                    }
                },
                cancellation);

            // Just adding the new spectrum to the input list adds it to the RelaxIS project.
            spectra.Add(newSpec);
        }
    }
}
See Also