/// <summary> /// This is basically the same example as the SaveNetworkData above, though /// this time the data is stored as a part of the MIKE 1D setup in the /// standard .m1dx file. /// </summary> /// <param name="vidaaFilepath">Path and name of the Vidaa example</param> public static void SaveDoubleNetworkDataWithSetup(string vidaaFilepath) { { // Creates a new Mike 1D controller and load the setup Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); Diagnostics diagnostics = new Diagnostics("My Diagnostics"); IMike1DController controller = controllerFactory.OpenAndCreate(Connection.Create(vidaaFilepath), diagnostics); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception("Loading errors, aborting"); } // Create a NetworkData that can store a user defined type, and add some data at some locations NetworkDataDouble myDoubleData = new NetworkDataDouble() { CanInterpolate = false }; myDoubleData.AddValue(new Location("SonderAa", 1034), 1.034); myDoubleData.AddValue(new Location("LindSkov", 2860), 2.86); myDoubleData.AddValue("MyNodeId", -42); // Set the NetworkData as additional data, using the string "UserData" as key controller.Mike1DData.SetAdditionalData("UserData", myDoubleData); // Saving the file again (same filename, new extension) controller.Mike1DData.Connection.BridgeName = "m1dx"; controller.Mike1DData.Connection.FilePath.Extension = ".m1dx"; Mike1DBridge.Save(controller.Mike1DData); controller.Finish(); } { // Load the setup again, changing the extension to .m1dx FilePath vidaaFile = new FilePath(vidaaFilepath); vidaaFile.Extension = ".m1dx"; // Load the setup Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); Diagnostics diagnostics = new Diagnostics("My Diagnostics"); IMike1DController controller = controllerFactory.OpenAndCreate(Connection.Create(vidaaFile.Path), diagnostics); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception("Loading errors, aborting"); } // Get the additional data using the string "UserData" as key, which we know is a NetworkData with MyData NetworkDataDouble myComplexData = (NetworkDataDouble)controller.Mike1DData.GetAdditionalData("UserData"); // Now we can extract our data again double v1034; bool ok1034 = myComplexData.GetValue(new Location("SonderAa", 1034), out v1034); double v2860; bool ok2860 = myComplexData.GetValue(new Location("LindSkov", 2860), out v2860); double vNode; bool okNode = myComplexData.GetValue("MyNodeId", out vNode); controller.Finish(); } }
public void UserStructureSaveData() { string setupFilePath = Path.Combine(ExampleBase.ExampleRoot, @"UnitFlow\UnitFlowStruc.sim11"); Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); Diagnostics diagnostics = new Diagnostics("My Diagnostics"); IMike1DController controller = controllerFactory.OpenAndCreate(Connection.Create(setupFilePath), diagnostics); // Add custom assembly and type - required for the standalong engine to load it again. controller.Mike1DData.CustomTypes.AssemblyFiles.Add(@"..\..\DHI.Mike1D.Examples\bin\Debug\DHI.Mike1D.Examples.dll"); controller.Mike1DData.CustomTypes.Add(typeof(MyStructure)); // Save to .m1dx file controller.Mike1DData.Connection.FilePath.Extension = ".m1dx"; controller.Mike1DData.Connection.FilePath.FileNameWithoutExtension = "UnitFlowStruc-2"; controller.Mike1DData.Connection.BridgeName = "m1dx"; controller.Mike1DData.ResultSpecifications.ForEach(rs => rs.Connection.FilePath.FileNameWithoutExtension += "-2"); Mike1DBridge.Save(controller.Mike1DData); }
/// <summary> /// This is basically the same example as the SaveNetworkData above, though /// this time the data is stored as a part of the MIKE 1D setup in the /// standard .m1dx file. /// <para> /// This is ONLY recommended for advanced users and use: /// The disadvantage of this approach is that the resulting .m1dx file can only be /// read again by a similar approach, i.e. it WILL ONLY run using the default engine /// when the custom assembly is available, hence it CAN NOT be read by others unless /// they are also provided with the custom assembly. /// </para> /// </summary> /// <param name="vidaaFilepath">Path and name of the Vidaa example</param> /// <param name="customAssemblyFile"></param> public static void SaveComplexNetworkDataWithSetup(string vidaaFilepath, string customAssemblyFile) { { // Creates a new Mike 1D controller and load the setup Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); Diagnostics diagnostics = new Diagnostics("My Diagnostics"); IMike1DController controller = controllerFactory.OpenAndCreate(Connection.Create(vidaaFilepath), diagnostics); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception("Loading errors, aborting"); } // Create a NetworkData that can store a user defined type, and add some data at some locations NetworkData <MyData> myComplexData = new NetworkData <MyData>() { CanInterpolate = false }; myComplexData.AddValue(new Location("SonderAa", 1034), new MyData() { SValue = "SA", DValue = 1.034 }); myComplexData.AddValue(new Location("LindSkov", 2860), new MyData() { SValue = "LS", DValue = 2.86 }); myComplexData.AddValue("MyNodeId", new MyData() { SValue = "Node", DValue = -42 }); // Set the NetworkData as additional data, using the string "UserData" as key controller.Mike1DData.SetAdditionalData("UserData", myComplexData); controller.Mike1DData.SetAdditionalData("UserData2", new MyData() { SValue = "Upstream", DValue = 15.03 }); // Saving the file again (same filename, new extension) controller.Mike1DData.Connection.BridgeName = "m1dx"; controller.Mike1DData.Connection.FilePath.Extension = ".m1dx"; // We need to add two custom types, in order to store data to .m1dx // The types are found in the DHI.Mike1D.Examples.dll, which cannot be found by the MIKE 1D engine unless an explicit path is provided // Make the path relative to the setup. string relativeAssemblyFile = FilePath.GetRelativeFilePath(customAssemblyFile, Path.GetDirectoryName(Path.GetFullPath(vidaaFilepath))); controller.Mike1DData.CustomTypes.AssemblyFiles.Add(relativeAssemblyFile); controller.Mike1DData.CustomTypes.Add(myComplexData.GetType()); controller.Mike1DData.CustomTypes.Add(typeof(MyData)); Mike1DBridge.Save(controller.Mike1DData); // Close any log files controller.Finish(); } { // Load the setup again, changing the extension to .m1dx FilePath vidaaFile = new FilePath(vidaaFilepath); vidaaFile.Extension = ".m1dx"; Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory(); Diagnostics diagnostics = new Diagnostics("My Diagnostics"); IMike1DController controller = controllerFactory.OpenAndCreate(Connection.Create(vidaaFile), diagnostics); Mike1DData mike1DData = controller.Mike1DData; // Get the additional data using the string "UserData" as key, which we know is a NetworkData with MyData NetworkData <MyData> myComplexData = (NetworkData <MyData>)mike1DData.GetAdditionalData("UserData"); MyData myComplexData2 = (MyData)mike1DData.GetAdditionalData("UserData2"); // Now we can extract our data again MyData v1034; bool ok1034 = myComplexData.GetValue(new Location("SonderAa", 1034), out v1034); MyData v2860; bool ok2860 = myComplexData.GetValue(new Location("LindSkov", 2860), out v2860); MyData vNode; bool okNode = myComplexData.GetValue("MyNodeId", out vNode); controller.Finish(); } }