| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text; |
| | 5 | | using System.Text.RegularExpressions; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using System.Xml; |
| | 8 | |
|
| | 9 | | namespace Morpho25.IO |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Library material class. |
| | 13 | | /// </summary> |
| | 14 | | public class Library |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Soil type (for soil). |
| | 18 | | /// </summary> |
| | 19 | | public const string SOIL = "SOIL"; |
| | 20 | | /// <summary> |
| | 21 | | /// Profile type (for soil). |
| | 22 | | /// </summary> |
| | 23 | | public const string PROFILE = "PROFILE"; |
| | 24 | | /// <summary> |
| | 25 | | /// Material type (for buildings). |
| | 26 | | /// </summary> |
| | 27 | | public const string MATERIAL = "MATERIAL"; |
| | 28 | | /// <summary> |
| | 29 | | /// Wall type (for buildings). |
| | 30 | | /// </summary> |
| | 31 | | public const string WALL = "WALL"; |
| | 32 | | /// <summary> |
| | 33 | | /// Source type (for source). |
| | 34 | | /// </summary> |
| | 35 | | public const string SOURCE = "SOURCE"; |
| | 36 | | /// <summary> |
| | 37 | | /// Plant type (for plant 2D). |
| | 38 | | /// </summary> |
| | 39 | | public const string PLANT = "PLANT"; |
| | 40 | | /// <summary> |
| | 41 | | /// Plant 3D type (for plant 3D). |
| | 42 | | /// </summary> |
| | 43 | | public const string PLANT3D = "PLANT3D"; |
| | 44 | | /// <summary> |
| | 45 | | /// Greening type (for building). |
| | 46 | | /// </summary> |
| | 47 | | public const string GREENING = "GREENING"; |
| | 48 | | /// <summary> |
| | 49 | | /// Collection of material's code. |
| | 50 | | /// </summary> |
| 0 | 51 | | public List<string> Code { get; private set; } |
| | 52 | | /// <summary> |
| | 53 | | /// Collection of material's description. |
| | 54 | | /// </summary> |
| 0 | 55 | | public List<string> Description { get; private set; } |
| | 56 | | /// <summary> |
| | 57 | | /// Collection of material's detail. |
| | 58 | | /// </summary> |
| 0 | 59 | | public List<string> Detail { get; private set; } |
| | 60 | | /// <summary> |
| | 61 | | /// Create a new library object. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="file">File path of the DB to read.</param> |
| | 64 | | /// <param name="type">Section of DB to read.</param> |
| | 65 | | /// <param name="keyword">Filter by keyword.</param> |
| 0 | 66 | | public Library(string file, |
| 0 | 67 | | string type, string keyword) |
| 0 | 68 | | { |
| 0 | 69 | | Code = new List<string>(); |
| 0 | 70 | | Description = new List<string>(); |
| 0 | 71 | | Detail = new List<string>(); |
| | 72 | |
|
| 0 | 73 | | SetLibrary(file, type, keyword); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | private string GetCompatibleText(string file) |
| 0 | 77 | | { |
| 0 | 78 | | string characters = @"[^\s()_<>/,\.A-Za-z0-9=""]+"; |
| | 79 | |
|
| 0 | 80 | | Encoding iso = Encoding.GetEncoding("ISO-8859-1"); |
| 0 | 81 | | if (!System.IO.File.Exists(file)) |
| 0 | 82 | | throw new Exception($"{file} not found."); |
| 0 | 83 | | string text = System.IO.File.ReadAllText(file, iso); |
| 0 | 84 | | string res = Regex.Replace(text, characters, ""); |
| | 85 | |
|
| 0 | 86 | | return res; |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private void SetLibrary(string file, string type, string keyword) |
| 0 | 90 | | { |
| 0 | 91 | | string innerText = GetCompatibleText(file); |
| | 92 | |
|
| 0 | 93 | | string word = (type != GREENING) ? "Description" : "Name"; |
| | 94 | |
|
| 0 | 95 | | XmlDocument xmlDcoument = new XmlDocument(); |
| 0 | 96 | | xmlDcoument.LoadXml(innerText); |
| 0 | 97 | | XmlNodeList data = xmlDcoument.DocumentElement.SelectNodes(type); |
| | 98 | |
|
| 0 | 99 | | var idContainer = new string[data.Count]; |
| 0 | 100 | | var descriptionContainer = new string[data.Count]; |
| 0 | 101 | | var dataContainer = new string[data.Count]; |
| | 102 | |
|
| 0 | 103 | | Parallel.For(0, data.Count, i => |
| 0 | 104 | | { |
| 0 | 105 | | var description = data[i].SelectSingleNode(word).InnerText; |
| 0 | 106 | | if (keyword != null) |
| 0 | 107 | | if (!description.ToUpper() |
| 0 | 108 | | .Contains(keyword?.ToUpper())) return; |
| 0 | 109 | |
|
| 0 | 110 | | dataContainer[i] = data[i].OuterXml; |
| 0 | 111 | | descriptionContainer[i]= description; |
| 0 | 112 | | var id = data[i].SelectSingleNode("ID").InnerText; |
| 0 | 113 | | idContainer[i] = id.Replace(" ", ""); |
| 0 | 114 | | }); |
| | 115 | |
|
| 0 | 116 | | Code.AddRange(idContainer.Where(_ => _ != null)); |
| 0 | 117 | | Description.AddRange(descriptionContainer.Where(_ => _ != null)); |
| 0 | 118 | | Detail.AddRange(dataContainer.Where(_ => _ != null)); |
| 0 | 119 | | } |
| | 120 | | } |
| | 121 | | } |