Data Manipulation Example |
Below, an example implementation of a RelaxISPlugin_DataManipulation is shown.
The function of the plugin is chosen for illustrative purposes.
Please note that it is recommended to use the RelaxISPlugin_DataManipulation2 plugin type. It offers the same (and more) function with a modernized API.
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
This examples illustrates the implementation of a DataManipulation plugin.
Executing the plugin changes the DataSource property of the input spectra and adds a new spectrum to the current project.
// <copyright file="MyDataManipulation.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.Drawing; using System.Linq; using RelaxIS_SDK.Common; using RelaxIS_SDK.Plugins; /*** * DataManipulation plugins are called by the user by pressing a button in the main menu. Each DataManipulation plugin gets one dedicated button. * When the button is clicked, the user may be prompted to select spectra, and then the PerformSetup() function is called. After this returns, * for each selected spectrum the ManipulateSpectrum() function is called. * This function can change the data of the spectrum, which is then written back to the source spectrum afterwards. * The plugin can also create new spectra by adding them to the NewSpectra property either in either of the two functions. * It is not possible to delete spectra in this plugin. */ /// <summary> /// Defines an example <see cref="RelaxISPlugin_DataManipulation"/> class. /// </summary> /// <remarks>It is recommended to use <see cref="RelaxISPlugin_DataManipulation2"/> instead.</remarks> [Obsolete("You should use RelaxISPlugin_DataManipulation2 instead.")] public class MyDataManipulation : RelaxISPlugin_DataManipulation { private readonly Bitmap buttonImage; private int spectrumCount = 0; /*** * The constructor creates a button image from the base64 string and stores it for later use. That way the image is not recreated every time. */ /// <summary> /// Initializes a new instance of the <see cref="MyDataManipulation"/> class. /// </summary> public MyDataManipulation() { // Read image data from base64 data and store it for the ButtomImage property. var data = @"iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAH6SURBVHhe7ZiBjsMgCIbrPcbe/9nuNXplgYQSEPVs5wpfYtgoU/xFZ1r2fd8i84M2LOEFMLdAKeVRe+OYZ8GPJ3ILoA3LaQs8rewt+HbICvAqwDo8vgVvTnkIog1LCoA2LCkA2rDcJkB5bTs0/LoMTfcAK/H9d2u+I1AfPb+x0PqSOdKzS+8Bq6yolkdrbl0CgKrU0LUUI/lNPQNAdd7QrSJjebz8Dmi+Gq0idAlASVAifBAtuZ6EAdkvfdfG04A4auhyGa4AKxnwe4kCFNcaX0P7fasIQ2cAfLYG6FkBitXi5TieSJSbFyeZegYAPJFaMnxiWpycuCYSUXvmMSRALSnw8YZuk9Y4j54xOdMqoLbaGrV4mgTFeFWg9dWaT74RQhuWFABtWFIAtGFJAdCG5RIBRm5kn6L7ItRyG5M3uRmM9nnbRehbVlwyLACsBDV0XcpVY91yCEJ1yAqp+WTjz96BB9oz/ryVZf4FKHm50t6qy0n3ijAsACneO+BsSDBPKItpFTCawKeZcgjOmDz1watqRr8et54BtW3DJz1L1BZuEUBORpsc+bhImlCzWeaVGK+At+NA8/WSr8QclnopqpX8f1Yf8OaUb4XRhiUFQBuWFABtWFIAtGFx7wFPJO8BjFMFcJ5WDdaNNg9BtGExt0AUcgugDcq2/QEL5WcAvUXq8gAAAABJRU5ErkJggg=="; var bytes = Convert.FromBase64String(data); using (var ms = new System.IO.MemoryStream(bytes)) { this.buttonImage = new Bitmap(ms); } } /*** * Implement the default plugin properties Name and Description that describe the plugin. * This is mainly used for display purposes. If no ButtonTitle is used, the Name property value is * used on the button. */ /// <inheritdoc/> public override string Name { get { return "MyDataManipulationPlugin"; } } /// <inheritdoc/> public override string Description { get { return "An example data manipulation plugin that adds the count of the spectra to the data source and also adds a new spectrum."; } } /*** * Define the button title and image. */ /// <inheritdoc/> public override string ButtonTitle { get { return "Run MyDataManipulation2"; } } /// <inheritdoc/> public override Bitmap ButtonImage { get { return this.buttonImage; } } /*** * These are properties that change some behavior when the plugin is executed. * It is not required to override them, but can be necessary to achieve certain requirements. * DoesChangeData: Per default, the Fit-Up-To-Date status of spectra are set to 'false' after */ /*** * Per default, the Fit-Up-To-Date status of spectra are set to 'false' after ManipulateSpectrum() returns. * If this property is 'false' the status will not be changed as the plugin indicates that the spectrum data is not changed. */ /// <inheritdoc/> public override bool DoesChangeData { get { return base.DoesChangeData; } } /*** * If 'true', RelaxIS will select the newly created spectra (if there are any) in the Spectra Explorer. Otherwise, * this doesn't happen. */ /// <inheritdoc/> public override bool ActivateNewSpectra { get { return base.ActivateNewSpectra; } } /*** * Per default, or if 'false', RelaxIS will prompt the user to select spectra (depending on RelaxIS configuration). * If 'true', executing the plugin will always use the currently selected spectra in the Spectra Explorer window. */ /// <inheritdoc/> public override bool AlwaysUseSelectedSpectra { get { return base.AlwaysUseSelectedSpectra; } } /*** * This function is called after the user selected the spectra. All selected spectra are found in the Spectra argument. * Note that changes to the spectra are not written back to the source spectra after this function and will be lost. * If you set Cancel to true here, the plugin will stop after the function returns and not call the ManipulateSpectrum function. * If you use the plugin for e.g. custom export or import functions or the like, it may be sufficient to perform all work here * and ignore the ManipulateSpectra() function. */ /// <inheritdoc/> public override void PerformSetup(IEnumerable<ImpedanceSpectrum_Fitted> Spectra, ref bool Cancel) { this.spectrumCount = Spectra.Count(); var spec = Spectra.FirstOrDefault(); if (spec == null) { return; } var newSpec = new ImpedanceSpectrum_Fitted { Datasource = string.Format("{0} multiplied by {1}", spec.Datasource, this.spectrumCount), }; // For the data, use the first spectrum data but with Z'' multiplied by the total number of selected spectra. // This is not useful, but can serve as an example. newSpec.Frequencies.AddRange(spec.Frequencies); newSpec.ZReal.AddRange(spec.ZReal); newSpec.ZImag.AddRange(spec.ZImag.Select(v => v * this.spectrumCount)); this.NewSpectra.Add(newSpec); } /*** * If the process is not cancelled in the PerformSetup() function, this function is called once for each of the input spectra. * The index will be the same as the spectra indizes in the list handed to the PerformSetup() function. * The changes made to the spectrum will be written back to the source spectrum afterwards. */ /// <inheritdoc/> public override void ManipulateSpectrum(int Index, ref ImpedanceSpectrum_Fitted Spectrum) { Spectrum.Datasource = string.Format("({0} / {1}) {2}", Index + 1, this.spectrumCount, Spectrum.Datasource); } } }