/// <summary> /// Import from text file to xns11 file /// </summary> /// <param name="txtFileName">Path and name of text file to import</param> /// <param name="xns11FileName">Path and name of xns11 file to create or update</param> /// <param name="xns11NewFileName">Path and name of xns11 file to write to</param> public static void Import(string txtFileName, string xns11FileName, string xns11NewFileName = null) { StreamReader reader = new StreamReader(txtFileName); CrossSectionDataFactory crossSectionDataFactory = new CrossSectionDataFactory(); CrossSectionData csData; if (File.Exists(xns11FileName)) { csData = crossSectionDataFactory.Open(Connection.Create(xns11FileName), null); } else { csData = new CrossSectionData(); } if (string.IsNullOrEmpty(xns11NewFileName)) { xns11NewFileName = xns11FileName; } string line; // Read cross section info-line while ((line = reader.ReadLine()) != null) { // Split cross section info-line string[] split = line.SplitQuoted(',', '"'); // extract info from info-line Location location = new Location(split[1], double.Parse(split[2], CultureInfo.InvariantCulture)); string topoId = split[3]; double datum = double.Parse(split[4], CultureInfo.InvariantCulture); int numRawPoints = int.Parse(split[5]); // Check if this cross section is to be processed if (split[0] == "1") { ICrossSection cs = csData.FindCrossSection(location, topoId); CrossSectionPointList points = new CrossSectionPointList(); // Read raw points for (int i = 0; i < numRawPoints; i++) { line = reader.ReadLine(); if (line == null) // end-of-file { throw new EndOfStreamException("File ended prematurely"); } string[] coords = line.Split(','); double x = double.Parse(coords[0], CultureInfo.InvariantCulture); double z = double.Parse(coords[1], CultureInfo.InvariantCulture); points.Add(new CrossSectionPoint(x, z)); } // Check if cross section already exists if (cs != null) { // Check if it is a cross section with raw points XSBaseRaw xsBaseRaw = cs.BaseCrossSection as XSBaseRaw; if (xsBaseRaw == null) { throw new Exception("Cannot modify raw points of a cross section without raw points: " + location.ToString() + ", " + topoId); } // replace datum (in case datum changes) cs.Location.Z = datum; // Replace points xsBaseRaw.Points = points; // Set default markers xsBaseRaw.UpdateMarkersToDefaults(true, true, true); // Recalculate processed data xsBaseRaw.ProcessingLevelsSpecs.NoOfLevels = 50; xsBaseRaw.CalculateProcessedData(); } else { // Create a new cross section CrossSectionFactory builder = new CrossSectionFactory(); builder.BuildOpen(""); // Defines the location of the current cross section. The Z-coordinate // for an open cross section with raw data is a Z-offset, and usually zero. builder.SetLocation(new ZLocation(location.ID, location.Chainage) { Z = datum }); // Set raw points and default markers builder.SetRawPoints(points); builder.SetDefaultMarkers(); // Define resistance properties as relative FlowResistance flowResistance = new FlowResistance(); flowResistance.Formulation = ResistanceFormulation.Relative; flowResistance.ResistanceDistribution = ResistanceDistribution.Uniform; flowResistance.ResistanceValue = 1; builder.SetFlowResistance(flowResistance); builder.SetRadiusType(RadiusType.ResistanceRadius); // Get the cross section CrossSectionLocated csLocated = builder.GetCrossSection(); // Set topo-id csLocated.TopoID = topoId; // now, calculate processed data csLocated.BaseCrossSection.ProcessingLevelsSpecs.Option = ProcessingOption.AutomaticLevels; csLocated.BaseCrossSection.ProcessingLevelsSpecs.NoOfLevels = 0; csLocated.BaseCrossSection.CalculateProcessedData(); // Store cross section in database csData.Add(csLocated); } } else // this cross section should not be processed { // Skip line containing raw points for (int i = 0; i < numRawPoints; i++) { line = reader.ReadLine(); if (line == null) // end-of-file { throw new EndOfStreamException("File ended prematurely"); } } } } csData.Connection = Connection.Create(xns11NewFileName); CrossSectionDataFactory.Save(csData); }