|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.Linq; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | + |
| 7 | +namespace CodeCave.Revit.Toolkit |
| 8 | +{ |
| 9 | + public static class RevitFileInfoExtensions |
| 10 | + { |
| 11 | + #region Constants |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// List of known Revit file properties. |
| 15 | + /// </summary> |
| 16 | + internal struct KnownRevitInfoProps |
| 17 | + { |
| 18 | + public const string USERNAME = "Username"; |
| 19 | + public const string CENTRAL_MODEL_PATH = "Central Model Path"; |
| 20 | + public const string REVIT_BUILD = "Revit Build"; |
| 21 | + public const string LAST_SAVE_PATH = "Last Save Path"; |
| 22 | + public const string OPEN_WORKSET_DEFAULT = "Open Workset Default"; |
| 23 | + public const string PROJECT_SPARK_FILE = "Project Spark File"; |
| 24 | + public const string CENTRAL_MODEL_IDENTITY = "Central Model Identity"; |
| 25 | + public const string LOCALE = "Locale when saved"; |
| 26 | + public const string LOCAL_SAVED_TO_CENTRAL = "All Local Changes Saved To Central"; |
| 27 | + public const string CENTRAL_MODEL_VERSION = "Central model's version number corresponding to the last reload latest"; |
| 28 | + public const string CENTRAL_MODEL_GUID = "Central model's episode GUID corresponding to the last reload latest"; |
| 29 | + public const string UNIQUE_DOCUMENT_GUID = "Unique Document GUID"; |
| 30 | + public const string UNIQUE_DOCUMENT_INCREMENT = "Unique Document Increments"; |
| 31 | + } |
| 32 | + |
| 33 | + private static readonly Regex versionExtractor = |
| 34 | + new Regex(@"(?<vendor>\w*) (?<software>(\w|\s)*) (?<version>\d{4}) \(Build\: (?<build>\d*)_(?<revision>\d*)(\((?<arch>\w*)\))?\)", |
| 35 | + RegexOptions.Compiled | |
| 36 | + RegexOptions.IgnoreCase | |
| 37 | + RegexOptions.CultureInvariant); |
| 38 | + |
| 39 | + #endregion |
| 40 | + |
| 41 | + #region Methods |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Parses the username. |
| 45 | + /// </summary> |
| 46 | + /// <param name="revitFileInfo">The revit file information.</param> |
| 47 | + /// <param name="properties">The properties.</param> |
| 48 | + /// <exception cref="T:System.ArgumentNullException">properties</exception> |
| 49 | + public static void ParseUsername(this RevitFileInfo revitFileInfo, Dictionary<string, string> properties) |
| 50 | + { |
| 51 | + if (properties == null) |
| 52 | + throw new ArgumentNullException(nameof(properties)); |
| 53 | + |
| 54 | + // Parse username field |
| 55 | + if (!properties.ContainsKey(KnownRevitInfoProps.USERNAME)) |
| 56 | + return; |
| 57 | + |
| 58 | + revitFileInfo.Username = properties[KnownRevitInfoProps.USERNAME]; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Parses the revit. |
| 63 | + /// </summary> |
| 64 | + /// <param name="revitFileInfo">The revit file information.</param> |
| 65 | + /// <param name="properties">The properties.</param> |
| 66 | + /// <exception cref="T:System.ArgumentNullException">properties</exception> |
| 67 | + public static void ParseRevit(this RevitFileInfo revitFileInfo, Dictionary<string, string> properties) |
| 68 | + { |
| 69 | + if (properties == null) throw new ArgumentNullException(nameof(properties)); |
| 70 | + |
| 71 | + // Parse Revit Build string |
| 72 | + if (!properties.ContainsKey(KnownRevitInfoProps.REVIT_BUILD)) |
| 73 | + return; |
| 74 | + |
| 75 | + var versionObj = versionExtractor.Match(properties[KnownRevitInfoProps.REVIT_BUILD]); |
| 76 | + if (!versionObj.Success) |
| 77 | + return; |
| 78 | + |
| 79 | + revitFileInfo.Vendor = versionObj?.Groups["vendor"]?.Value; |
| 80 | + revitFileInfo.Name = versionObj?.Groups["software"]?.Value; |
| 81 | + revitFileInfo.Is64Bit = versionObj.Groups["arch"]?.Value?.Contains("x64") ?? false; |
| 82 | + |
| 83 | + var versionString = string.Format("{0}.{1}.{2}", |
| 84 | + versionObj.Groups["version"].Value, |
| 85 | + versionObj.Groups["build"].Value, |
| 86 | + versionObj.Groups["revision"].Value); |
| 87 | + revitFileInfo.Version = new Version(versionString); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Parses the locale. |
| 92 | + /// </summary> |
| 93 | + /// <param name="revitFileInfo">The revit file information.</param> |
| 94 | + /// <param name="properties">The properties.</param> |
| 95 | + /// <exception cref="T:System.ArgumentNullException">properties</exception> |
| 96 | + public static void ParseLocale(this RevitFileInfo revitFileInfo, Dictionary<string, string> properties) |
| 97 | + { |
| 98 | + if (properties == null) throw new ArgumentNullException(nameof(properties)); |
| 99 | + |
| 100 | + // Parse last saved locale |
| 101 | + if (!properties.ContainsKey(KnownRevitInfoProps.LOCALE)) |
| 102 | + return; |
| 103 | + |
| 104 | + var localeRaw = properties[KnownRevitInfoProps.LOCALE]; |
| 105 | + revitFileInfo.Locale = CultureInfo |
| 106 | + .GetCultures(CultureTypes.NeutralCultures) |
| 107 | + .FirstOrDefault(c => c.ThreeLetterWindowsLanguageName == localeRaw); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Parses the document information. |
| 112 | + /// </summary> |
| 113 | + /// <param name="revitFileInfo">The revit file information.</param> |
| 114 | + /// <param name="properties">The properties.</param> |
| 115 | + /// <exception cref="T:System.ArgumentNullException">properties</exception> |
| 116 | + public static void ParseDocumentInfo(this RevitFileInfo revitFileInfo, Dictionary<string, string> properties) |
| 117 | + { |
| 118 | + if (properties == null) throw new ArgumentNullException(nameof(properties)); |
| 119 | + |
| 120 | + // Parse document unique id |
| 121 | + if (!properties.ContainsKey(KnownRevitInfoProps.UNIQUE_DOCUMENT_GUID)) |
| 122 | + return; |
| 123 | + |
| 124 | + var guidRaw = properties[KnownRevitInfoProps.UNIQUE_DOCUMENT_GUID]; |
| 125 | + revitFileInfo.Guid = new Guid(guidRaw); |
| 126 | + } |
| 127 | + |
| 128 | + #endregion |
| 129 | + } |
| 130 | +} |
0 commit comments