Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.Drawing;
using System.Windows.Forms;
using DelftTools.Shell.Gui;
using DeltaShell.Plugins.VolumeModel.Importers;
namespace DeltaShell.Plugins.VolumeModel.Commands
{
    internal class AddInputDataToVolumeModelCommand : IGuiCommand
    {
Code Block
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.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>
        /// WhetherThe orname notof the gui command is checked
        /// </summary>
        public string Name
   /// <remarks>Not relevant in this tutorial</remarks>{
          public bool Checkedget { get; set return "Add input data"; }
        }
        /// <summary>
        /// AEnsures reference to the Delta Shell gui command (automaticallyis setenabled byfor Deltavolume Shellmodels logic)only
        /// </summary>
        public bool Enabled
   IGui Gui { get; set; }{

         /// <summary>
  get { return Gui !=  /// The action that should be performed while executing the gui commandnull && Gui.Selection is Models.VolumeModel; }
        }
        /// </summary><summary>
        public/// voidThe Execute(params object[] arguments)
        {image of the gui command
            /// Obtain the selected volume model
    </summary>
        varpublic volumeModelImage = (Models.VolumeModel) Gui.SelectedModel;

  Image { get; set; }
          /// Try<summary>
 to obtain a precipitation file via a file dialog
       /// 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 = "Choose precipitation time series",/// <summary>
        /// A reference to the Delta Shell gui (automatically set by Delta Filter = "WaterML2 files|*.XML",
Shell logic)
        /// </summary>
        public IGui Gui { Multiselectget; =set; false}
        /// <summary>
        };

            if (fileDialog.ShowDialog() != DialogResult.OK)/// The action that should be performed while executing the gui command
        /// </summary>
        public void Execute(params object[] {arguments)
        {
        return;
    // Obtain the selected volume model
   }

         var volumeModel  // Create a WaterML2 time series importer
= (Models.VolumeModel)Gui.SelectedModel;
            // Try to obtain a precipitation file varvia waterML2TimeSeriesImportera = new WaterML2TimeSeriesImporter();
file dialog
            //var ImportfileDialog the= datanew fromOpenFileDialog
 the precipitation file
         {
   waterML2TimeSeriesImporter.ImportItem(fileDialog.FileName, volumeModel.Precipitation);

            //Title Try to obtain a shape file via a file dialog
= "Choose precipitation time series",
                fileDialogFilter = new OpenFileDialog
"WaterML2 files|*.XML",
                Multiselect = {false
            };
        Title = "Choose basin shape file",
  if (fileDialog.ShowDialog() != DialogResult.OK)
             {
     Filter = "Shape files|*.shp",
        return;
            Multiselect}
 = false
          // Create a WaterML2 time series };importer

            if (fileDialog.ShowDialog() != DialogResult.OK)var waterML2TimeSeriesImporter = new WaterML2TimeSeriesImporter();
            {
// Import the data from the precipitation  file
        return;
    waterML2TimeSeriesImporter.ImportItem(fileDialog.FileName, volumeModel.Precipitation);
       }

     // Try to obtain a shape file //via Create a catchmentfile importerdialog
            var catchmentsImporterfileDialog = new CatchmentFromGisImporterOpenFileDialog
                {
                Title = "Choose basin FileBasedFeatureProviders = new List<IFileBasedFeatureProvider>
 shape file",
                Filter =  {"Shape files|*.shp",
                Multiselect = false
      new ShapeFile()
     };
            if (fileDialog.ShowDialog()  }!= DialogResult.OK)
                };
{
            // Configure the catchment importerreturn;
            var catchmentImporterSettings = catchmentsImporter.FeatureFromGisImporterSettings;
 }
            // Create catchmentImporterSettings.Patha = fileDialog.FileName;
DrainageBasinImporter importer
             catchmentImporterSettings.PropertiesMapping.First(propertyvar drainageBasinImporter => property.PropertyName == "Name").MappingColumn.ColumnName = "GM_NAAM";
new DrainageBasinImporter();
            // Import the data from the shape file
            catchmentsImporterdrainageBasinImporter.ImportItem(nullfileDialog.FileName, 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()
        {

        }
    }
}
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.

...