...
Add a new folder to the plugin project named Importers. In this folder, create a new class named DemoAppApplicationPlugin.cs and add the following code:
Code Block |
---|
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Xml.Linq; using DelftTools.Functions; using DelftTools.Functions.Generic; using DelftTools.Shell.Core; using log4net; namespace DeltaShell.Plugin.DemoApp.Importers { public class WaterML2TimeSeriesImporter : IFileImporter { private static readonly ILog log = LogManager.GetLogger(typeof(WaterML2TimeSeriesImporter)); /// <summary> /// The name of the importer /// </summary> public string Name { get { return "WaterML2 time series importer"; } } /// <summary> /// The category of the importer /// </summary> public string Category { get { return "DemoApp importers"; } } /// <summary> /// The image of the importer /// </summary> public Bitmap Image { get { return new Bitmap(16, 16);} } /// <summary> /// The data types supported by the importer /// </summary> public IEnumerable<Type> SupportedItemTypes { get { yield return typeof(TimeSeries); } } /// <summary> /// Indicates whether or not the importer can import at root level (folder/project). /// If true, the importer will always show up in the project->import list. If /// false this importer can only be retrieved by supported type, eg, in code. /// Use false for partial/composite importers and importers called from map tools /// etc. If true, the importer is assumed to support both "new" and "into" modes. /// </summary> public bool CanImportOnRootLevel { get { return true; } } /// <summary> /// The file filter of the importer /// </summary> /// <example>"My file format1 (*.ext1)|*.ext1|My file format2 (*.ext2)|*.ext2"</example> public string publicFileFilter class WaterML2TimeSeriesImporter : IFileImporter { private static readonlyget ILog{ logreturn = LogManager.GetLogger(typeof(WaterML2TimeSeriesImporter));"WaterML2 files|*.XML"; } } public string Name/// <summary> { get { return "WaterML2 time series importer"; }/// Path where external data files can be copied into /// </summary> } public string Category { /// <remarks>Optional, used only when external files need to be copied into the project "as is"</remarks> public string getTargetDataDirectory { return "DemoApp importers"get; set; } } /// <summary> public/// BitmapWhether Image or not an import task should be {cancelled get { return new Bitmap(16, 16);}/// </summary> } public IEnumerable<Type> SupportedItemTypes { /// <remarks>This property must be observed by the importer (thread-safe); when it is true the importer must stop current import task</remarks> public bool getShouldCancel { yield return typeof(TimeSeries)get; set; } } /// <summary> public bool CanImportOnRootLevel /// Fired when progress has been {changed /// </summary> get { return true; } public ImportProgressChangedDelegate ProgressChanged { get; set; } public string FileFilter/// <summary> { get { return "WaterML2 files|*.XML"; } /// Imports data from the file with path <paramref name="path"/> to target <paramref name="target"/> } /// </summary> public/// string<remarks>The TargetDataDirectorytarget {parameter get; set; } public bool ShouldCancel { get; set; } public ImportProgressChangedDelegate ProgressChanged { get; set; } is optional. If a target is specified, the importer should import new data into the target. If no target is specified, the importer should import the data into a new object.</remarks> public object ImportItem(string path, object target = null) { // Check the file path if (!File.Exists(path)) { log.Error("File does not exist"); return null; } // Obtain a new time series or check the provided target for being a time series var timeSeries = target == null ? new TimeSeries { Name = Path.GetFileNameWithoutExtension(path), Components = { new Variable<double>() } } : target as TimeSeries; if (timeSeries == null) { log.Error("Target is of the wrong type (should be time series)"); return null; } // Load the XML document var doc = XDocument.Load(path); // Obtain the document elements var xElements = doc.Descendants(); // Obtain the measurement TVP tags var measurements = xElements.Where(element => element.Name.LocalName == "MeasurementTVP"); // Get the corresponding time and value for each measurement tag foreach (var measurement in measurements) { var time = DateTime.Parse(measurement.Elements().First(e => e.Name.LocalName == "time").Value); var value = double.Parse(measurement.Elements().First(e => e.Name.LocalName == "value").Value); timeSeries[time] = value; } // Return the time series return timeSeries; } } } |
Info |
---|
Fixme: Info on the code above.
...