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.Linq; using System.Windows.Forms; using DelftTools.Shell.Gui; using outline {color:#000000}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.{color} h2. 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: {code} using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; using DelftTools.Shell.Gui; using DeltaShell.Plugins.VolumeModel.Importers; using DeltaShell.Plugins.NetworkEditor.Import; using SharpMap.Api; using SharpMap.Data.Providers; namespace DeltaShell.Plugins.VolumeModel.CommandsImporters; { using DeltaShell.Plugins.NetworkEditor.Import; using SharpMap.Api; using SharpMap.Data.Providers; namespace DeltaShell.Plugins.VolumeModel.Commands { internal class AddInputDataToVolumeModelCommand : IGuiCommand { /// <summary> /// The name of the gui command /// </summary> public string Name { get { return "Add input data"; } } /// <summary> /// Ensures the gui command is enabled for volume models only /// </summary> public bool Enabled { get { return Gui != null && Gui.Selection is Models.VolumeModel; } } /// <summary> /// The image of the gui command /// </summary> public Image Image { get; set; } /// <summary> /// Whether or not the gui command is checked /// </summary> /// <remarks>Not relevant in this tutorial</remarks> public bool Checked { get; set; } /// <summary> /// A reference to the Delta Shell gui (automatically set by Delta Shell logic) /// </summary> public IGui Gui { get; set; } /// <summary> /// The action that should be performed while executing the gui command /// </summary> public void Execute(params object[] arguments) { // Obtain the selected volume model var volumeModel = (Models.VolumeModel) Gui.SelectedModel; // Try to obtain a precipitation file via a file dialog var fileDialog = new OpenFileDialog { Title = "Choose precipitation time series", Filter = "WaterML2 files|*.XML", Multiselect = false }; if (fileDialog.ShowDialog() != DialogResult.OK) { return; } // Create a WaterML2 time series importer var waterML2TimeSeriesImporter = new WaterML2TimeSeriesImporter(); // Import the data from the precipitation file waterML2TimeSeriesImporter.ImportItem(fileDialog.FileName, volumeModel.Precipitation); // Try to obtain a shape file via a file dialog fileDialog = new OpenFileDialog { Title = "Choose basin shape file", Filter = "Shape files|*.shp", Multiselect = false }; if (fileDialog.ShowDialog() != DialogResult.OK) { return; } // Create a catchment importer var catchmentsImporter = new CatchmentFromGisImporter { FileBasedFeatureProviders = new List<IFileBasedFeatureProvider> { new ShapeFile() } }; // Configure the catchment importer var catchmentImporterSettings = catchmentsImporter.FeatureFromGisImporterSettings; catchmentImporterSettings.Path = fileDialog.FileName; catchmentImporterSettings.PropertiesMapping.First(property => property.PropertyName == "Name").MappingColumn.ColumnName = "GM_NAAM"; // Import the data from the shape file catchmentsImporter.ImportItem(null, volumeModel.Basin); } /// <summary> /// The action that should be performed in order to undo execute actions of the gui command /// </summary> /// <remarks>Not relevant in this tutorial</remarks> public void Unexecute() { } } } {code} {note} A reference to _DeltaShell.Plugins.NetworkEditor_ needs to be added in order to successfully build the code (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_). {note} {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. {info} h2. 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 as shown below: {code} <UserControl x:Class="DeltaShell.Plugins.VolumeModel.Ribbon.VolumeModelRibbon" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
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"> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<!--Create a ribbon group box--> xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" <fluent:RibbonGroupBox Header="Input"> xmlns:d="http://schemas.microsoft.com/expression/blend/2008" <!--Create a ribbon button--> xmlns:fluent="clr-namespace:Fluent;assembly=Fluent<fluent:Button x:Name="ButtonAddInputDataToVolumeModel" mc:Ignorable="d" Height="145" Width="632"> <!--Create a ribbon control--> <fluent:Ribbon NameHeader="VolumeModelRibbonControl" x:FieldModifier="private">Add input data" <!--Create a ribbon tab--> <fluent:RibbonTabItem Header="Volume model" fluent:KeyTip.Keys="E"> ToolTip="Add input data <!--Create a ribbon group box-->to the selected volume model" <fluent:RibbonGroupBox Header="Input"> <!--Create a ribbon button--> Click="ButtonAddInputDataToVolumeModel_OnClick" <fluent:Button x:Name="ButtonAddInputDataToVolumeModel" Header="Add input dataSize="Middle" ToolTipSizeDefinition="Add input data to the selected volume model" Middle,Small,Small"/> </fluent:RibbonGroupBox> </fluent:RibbonTabItem> Click="ButtonAddInputDataToVolumeModel_OnClick" Size="Middle" SizeDefinition="Middle,Small,Small"/> </fluent:RibbonGroupBox> </fluent:RibbonTabItem> </fluent:</fluent:Ribbon> </UserControl> {code} |
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 { : {code} 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> /// CreatesInteraction thelogic Ribbonfor control VolumeModelRibbon.xaml /// </summary> public partial class VolumeModelRibbon public: VolumeModelRibbon() IRibbonCommandHandler { private readonly IGuiCommand // Initialize the control (standard user control logic) InitializeComponentaddInputDataToVolumeModelCommand = new AddInputDataToVolumeModelCommand(); // Instance of the implemented gui }command /// <summary> /// ReturnsCreates the volume model Ribbon control /// </summary> public object GetRibbonControlVolumeModelRibbon() { return VolumeModelRibbonControl; // Initialize the control (standard user control logic) } InitializeComponent(); /// <summary>} /// Enabling/disabling<summary> actions to be performed while validating the Ribbon/// itemsReturns (triggeredthe byvolume Deltamodel ShellRibbon logic)control /// </summary> public voidobject ValidateItemsGetRibbonControl() { ButtonAddInputDataToVolumeModel.IsEnabled = addInputDataToVolumeModelCommand.Enabledreturn VolumeModelRibbonControl; } /// <summary> /// Whether or not the contextual tab should be visibleEnabling/disabling actions to be performed while validating the Ribbon items (triggered by Delta Shell logic) /// </summary> public boolvoid IsConextualTabVisible(string tabGroupName, string tabNameValidateItems() { ButtonAddInputDataToVolumeModel.IsEnabled return= falseaddInputDataToVolumeModelCommand.Enabled; } /// <summary> /// The (gui) commands of the Ribbon controlWhether or not the contextual tab should be visible /// </summary> public IEnumerable<ICommand> Commands bool IsConextualTabVisible(string tabGroupName, string tabName) { get { yield return addInputDataToVolumeModelCommandfalse; } } /// <summary> /// ActionsThe to(gui) be performed after clickingcommands of the AddInputDataToVolumeModelRibbon buttoncontrol /// </summary> private void ButtonAddInputDataToVolumeModel_OnClick(object sender, RoutedEventArgs e) public IEnumerable<ICommand> Commands { get { yield return addInputDataToVolumeModelCommand; } } /// {<summary> /// Actions to addInputDataToVolumeModelCommand.Execute(); be performed after clicking the AddInputDataToVolumeModel button } } } {code} {note} A reference to _System.Xaml_ needs to be added in order to successfully build the code (right click the References folder of the project in the Solution Explorer \| Add Reference... \| Assemblies \| Framework \| Select _System.Xaml_). {note} {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. {info} h2. 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} /// </summary> private void ButtonAddInputDataToVolumeModel_OnClick(object sender, RoutedEventArgs e) { addInputDataToVolumeModelCommand.Execute(); } } } |
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 |
---|
{code} and {code} public override IRibbonCommandHandler RibbonCommandHandler { get { return new VolumeModelRibbon(); } } {code} |
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:
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:
scrollbar | ||
---|---|---|
Panel | ||
| ||
Align | center | center | The code results up until the end of this exercise can be downloaded here