Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin
h2.
scrollbar

Exercise outline

The goal of this exercise is to add a Ribbon button for volume models. As a result, it should be possible to configure the input data of volume models in just a couple of clicks.

Create a new gui command

Add a new folder to the plugin project named Commands. In this folder, create a new class named AddInputDataToVolumeModelCommand.cs and adapt the contents as shown below.

Note

A reference to DeltaShell.Plugins.NetworkEditor needs to be added in order to successfully build the code below (right click the References folder of the project in the Solution Explorer | Add Reference... | Browse... | Select D:\VolumeModel\packages\DeltaShell.1.0.0\delta-shell\plugins\DeltaShell.Plugins.NetworkEditor\DeltaShell.Plugins.NetworkEditor.dll).

Code Block

using System.Collections.Generic;
using System.Drawing;
using System. outline

{color:#ff0000}*\[TODO\]*{color} {color:#ff0000}Description of the exercise outline{color}

h2. Fixme

{color:#ff0000}*\[TODO\]*{color} {color:#ff0000}Fixme{color}

h2. Fixme

{color:#ff0000}*\[TODO\]*{color} {color:#ff0000}Fixme{color}

h2. Exercise results

{color:#ff0000}*\[TODO\]*{color} {color:#ff0000}Description of the exercise results{color}\\
\\
\\
\\
\\
1. Add a new folder to the project named "Commands"

2. Create a new class named "AddDemoDataToVolumeModelCommand" 

3. Add the following contents to this class:

{code}
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DelftTools.Shell.Gui;
using DeltaShell.PluginPlugins.VolumeModel.Importers;
using DeltaShell.Plugins.NetworkEditor.Import;
using SharpMap.Api;
using SharpMap.Data.Providers;

namespace DeltaShell.PluginPlugins.VolumeModel.Commands
{
    internal class AddDemoDataToVolumeModelCommandAddInputDataToVolumeModelCommand : IGuiCommand
    {
        public string Name/// <summary>
        {
/// The name of the gui command
        /// </summary>
    get { return "Add demopublic data";string }Name
        }
{
        public bool Enabled
  get { return "Add input data"; {}
        }

    get { return Gui != null && Gui.SelectedModel is Models.VolumeModel; }
        }

        public Image Image { get; set; }
 /// <summary>
        /// Ensures the gui command is enabled for volume models only
        /// </summary>
        public bool CheckedEnabled
 { get; set; }

    {
    public IGui Gui { get; set; }

  get { return Gui != null public void Execute(params object[] arguments)&& Gui.Selection is Models.VolumeModel; }
        {}

            /// Obtain<summary>
 the selected volume model
    /// The image of the gui command
  var volumeModel = (Models.VolumeModel)Gui.SelectedModel;

   /// </summary>
        //public CreateImage aImage WaterML2{ timeget; seriesset; importer}

        /// <summary>
    var waterMl2TimeSeriesImporter = new WaterML2TimeSeriesImporter();

        /// Whether or not the gui command is checked
     var fileDialog = new OpenFileDialog/// </summary>
        /// <remarks>Not relevant in this tutorial</remarks>
   {
     public bool Checked { get; set; }

        /// Title<summary>
 = "Choose   precipitation time series",
               /// A reference to the Delta Shell gui (automatically set by Delta Shell logic)
      Filter = "WaterML2 files|*.XML",/// </summary>
        public IGui Gui { get; set; }

      Multiselect = false/// <summary>
        /// The action that should be performed while };

executing the gui command
         if (fileDialog.ShowDialog() != DialogResult.OK)
/// </summary>
        public void Execute(params object[] arguments)
   {
     {
           return;
 // Obtain the selected volume model
            var volumeModel  }= (Models.VolumeModel) Gui.SelectedModel;

            // Try to Importobtain a WaterML2precipitation file tovia thea precipitationfile timedialog
 series of the volume model
       var fileDialog =   waterMl2TimeSeriesImporter.ImportItem(fileDialog.FileName, volumeModel.Precipitation);
new OpenFileDialog
            // Create a catchment importer{
            var  catchmentsImporter = new CatchmentFromGisImporter
   Title = "Choose precipitation time series",
    {
                FileBasedFeatureProvidersFilter = new List<IFileBasedFeatureProvider>"WaterML2 files|*.XML",
                {
    Multiselect = false
               new ShapeFile() };

            if (fileDialog.ShowDialog()   }!= DialogResult.OK)
            };{

             fileDialog = new OpenFileDialogreturn;
            {}

            // Create a WaterML2 Titletime =series "Chooseimporter
 basin shape file",
         var waterML2TimeSeriesImporter = new WaterML2TimeSeriesImporter();

   Filter = "Shape files|*.shp",
      // Import the data from the precipitation file
   Multiselect = false
       waterML2TimeSeriesImporter.ImportItem(fileDialog.FileName, volumeModel.Precipitation);

    };

        // Try to obtain if (fileDialog.ShowDialog() != DialogResult.OK)a shape file via a file dialog
            {
fileDialog = new OpenFileDialog
             return;
   {
         }

           Title //= Configure"Choose thebasin catchmentshape importerfile",
            var catchmentImporterSettings = catchmentsImporter.FeatureFromGisImporterSettings;
     Filter = "Shape files|*.shp",
    catchmentImporterSettings.Path = fileDialog.FileName;
            catchmentImporterSettings.PropertiesMapping.First(property => property.PropertyNameMultiselect == "Name").MappingColumn.ColumnName = "GM_NAAM";
false
            // Import a shape file};

 to the basin of the volume model
            catchmentsImporter.ImportItem(null, volumeModel.Basin);if (fileDialog.ShowDialog() != DialogResult.OK)
        }

    {
    public   void Unexecute()
        {
return;
        }
    }
}
{code}

4.             // Create a catchment newimporter
 WPF user control named "VolumeModelRibbon"
\\
\\
!NewWPFControl .png!
\\

5. Add the following xaml code:

{code}
<UserControl x:Class="DeltaShell.Plugin.VolumeModel.VolumeModelRibbon"
 var catchmentsImporter = new CatchmentFromGisImporter
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
{
                    FileBasedFeatureProviders xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
= new List<IFileBasedFeatureProvider>
                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 {
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    new ShapeFile()
             xmlns:fluent="clr-namespace:Fluent;assembly=Fluent"
       }
       mc:Ignorable="d" Height="145" Width="632">
    <!--Create a ribbon control-->
  };

   <fluent:Ribbon Name="VolumeModelRibbonControl" x:FieldModifier="private">
        <!--Create a ribbon tab-->
// Configure the catchment importer
         <fluent:RibbonTabItem Header="Volume model" fluent:KeyTip.Keys="E"> var catchmentImporterSettings = catchmentsImporter.FeatureFromGisImporterSettings;
            <!--Create a ribbon group box-->catchmentImporterSettings.Path = fileDialog.FileName;
            <fluent:RibbonGroupBox Header="Demo">
    catchmentImporterSettings.PropertiesMapping.First(property => property.PropertyName == "Name").MappingColumn.ColumnName = "GM_NAAM";

            <!--Create a ribbon button-->
    // Import the data from the shape file
            <fluent:Button x:Name="ButtonAddDemoDataToVolumeModel"catchmentsImporter.ImportItem(null, volumeModel.Basin);
        }

        /// <summary>
        /// The action that should be Header="Add demo data"
        performed in order to undo execute actions of the gui command
        /// </summary>
        /// <remarks>Not relevant in this  ToolTip="Add demo data to the selected volume model"
  tutorial</remarks>
        public void Unexecute()
        {

        }
             Click="ButtonAddDemoDataToVolumeModel_OnClick"
             }
}
Info

The command is derived from the IGuiCommand interface in order to obtain a reference to the Delta Shell gui (which is automatically set by the Delta Shell framework).

The comments in the code explain the different parts of the gui command implementation.

Create a new Ribbon control

Add a new folder to the plugin project named Ribbon. In this folder, create a new WPF user control named VolumeModelRibbon.xaml and adapt the contents (in the designer) as shown below:

Code Block

<UserControl x:Class="DeltaShell.Plugins.VolumeModel.Ribbon.VolumeModelRibbon"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:fluent="clr-namespace:Fluent;assembly=Fluent"
    mc:Ignorable="d" Height="145" Width="632">
    <!--Create a ribbon control-->
    <fluent:Ribbon Name="VolumeModelRibbonControl" x:FieldModifier="private">
        <!--Create a ribbon tab-->
        <fluent:RibbonTabItem Header="Volume model" fluent:KeyTip.Keys="E">
            <!--Create a ribbon group box-->
            <fluent:RibbonGroupBox Header="Input">
                <!--Create a ribbon button-->
                <fluent:Button x:Name="ButtonAddInputDataToVolumeModel"
                               Header="Add input data"
                               ToolTip="Add input data to the selected volume model"
                               Click="ButtonAddInputDataToVolumeModel_OnClick"
                               Size="Middle"
                               SizeDefinition="Middle,Small,Small"/>
            </fluent:RibbonGroupBox>
        </fluent:RibbonTabItem>
    </fluent:Ribbon>
</UserControl>

Additionally, adapt the contents of VolumeModelRibbon.xaml.cs as shown below (right click the class | View Code).

Note

A reference to System.Xaml needs to be added in order to successfully build the code below (right click the References folder of the project in the Solution Explorer | Add Reference... | Assemblies | Framework | Select System.Xaml).

Code Block

using System.Collections.Generic;
using System.Windows;
using DelftTools.Controls;
using DelftTools.Shell.Gui;
using DelftTools.Shell.Gui.Forms;
using DeltaShell.Plugins.VolumeModel.Commands;

namespace DeltaShell.Plugins.VolumeModel.Ribbon
{
    /// <summary>
    /// Interaction logic for VolumeModelRibbon.xaml
    /// </summary>
    public partial class VolumeModelRibbon : IRibbonCommandHandler
    {
        private readonly IGuiCommand addInputDataToVolumeModelCommand = new AddInputDataToVolumeModelCommand(); // Instance of the implemented gui command

        /// <summary>
        /// Creates the Ribbon control
        /// </summary>
        public VolumeModelRibbon()
        {
            // Initialize the control (standard user control logic)
            InitializeComponent();
        }

        /// <summary>
        /// Returns the volume model Ribbon control
        /// </summary>
        public object GetRibbonControl()
        {
            return VolumeModelRibbonControl;
        Size="Middle"}

        /// <summary>
        /// Enabling/disabling actions to be performed while validating the Ribbon items (triggered by Delta  SizeDefinition="Middle,Small,Small"/>Shell logic)
         ///   </fluent:RibbonGroupBox>summary>
        </fluent:RibbonTabItem>
    </fluent:Ribbon>
</UserControl>
{code}

6. Additionally, add the following interaction logic in DemoAppRibbon.xaml.cs:

{code}
using System.Collections.Generic;
using System.Windows;
using DelftTools.Controls;
using DelftTools.Shell.Gui;
using DelftTools.Shell.Gui.Forms;
using DeltaShell.Plugin.DemoApp.Commands;

namespace DeltaShell.Plugin.DemoApp
{
public void ValidateItems()
        {
            ButtonAddInputDataToVolumeModel.IsEnabled = addInputDataToVolumeModelCommand.Enabled;
        }

        /// <summary>
     /// Interaction logic for DemoAppRibbon.xaml
   /// Whether or not the contextual tab should be visible
        /// </summary>
        public partialbool class DemoAppRibbon : IRibbonCommandHandler
IsConextualTabVisible(string tabGroupName, string tabName)
        {
        private readonly IGuiCommand addDemoDataCommand = new AddDemoDataToVolumeModelCommand();

return false;
        }

    public DemoAppRibbon()
   /// <summary>
    {
    /// The (gui) commands of the Ribbon  InitializeComponent();control
        }
/// </summary>
        public objectIEnumerable<ICommand> GetRibbonControl()Commands
        {
            get { yield return DemoAppRibbonControladdInputDataToVolumeModelCommand; }
        }

        public void ValidateItems()
    /// <summary>
    {
    /// Actions to be performed after clicking the ButtonAddDemoDataToVolumeModel.IsEnabled = addDemoDataCommand.Enabled;
AddInputDataToVolumeModel button
         }
/// </summary>
        publicprivate boolvoid IsConextualTabVisibleButtonAddInputDataToVolumeModel_OnClick(stringobject tabGroupNamesender, stringRoutedEventArgs tabNamee)
        {
            addInputDataToVolumeModelCommand.Execute();
   return false;
    }
    }

    }
Info

The Ribbon control is derived from the IRibbonCommandHandler interface so that it can be registered in the gui plugin.

The comments in the code should explain the different parts of the Ribbon control implementation.

Register the Ribbon control in the gui plugin class

Register the Ribbon control in the gui plugin by adding the following code to VolumeModelGuiPlugin.cs:

Code Block

using DelftTools.Shell.Gui.Forms;
using DeltaShell.Plugins.VolumeModel.Ribbon;

and

Code Block
    public IEnumerable<ICommand> Commands
        {
            get { yield return addDemoDataCommand; }
        }

public override IRibbonCommandHandler RibbonCommandHandler
     private void ButtonAddDemoDataToVolumeModel_OnClick(object sender, RoutedEventArgs e) {
        {
    get { return      addDemoDataCommand.Executenew VolumeModelRibbon(); }
         }
    }
}

{code}

7. Run the application, create a volume model and click the newly created Ribbon button: ensure input data is added correctly to the volume model by opening the data views or by running the model \[TODO: Image\]
\\
\\
\\
 }

Delta Shell should now automatically add the new Ribbon control to its Ribbon bar during the application startup.

Exercise results

First of all, download the following WaterML2 XML file: WaterML2_precipitation_data.XML. Also download and unzip the shape files contained in the following archive: Gemeenten.zip. You will use all these data along the exercise.

Next, run the application and check that a volume Ribbon button has been added to the Ribbon bar:

Image Added

Then create a new volume model item (right click on project | Add | New Model ...), click the volume Ribbon button and, sequentially, select the previously downloaded WaterML2 XML file and shape files in the file selection dialogs.

Afterwards, verify that the input data has been correctly added to the volume model by opening the data views or by running the model:

Image Added


scrollbar