|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Vim.Format.ObjectModel; |
| 4 | + |
| 5 | +// SOME BACKGROUND INFORMATION ABOUT ELEMENT IFC GUIDS |
| 6 | +// |
| 7 | +// by: Martin Ashton, August 25, 2025 |
| 8 | +// |
| 9 | +// - VIM Elements sourced from Revit may have the parameter IfcGUID, which defines the IFC GUID of the element (which is distinct from the value stored in Element.UniqueId) |
| 10 | +// - VIM Elements sourced from IFC files use the Element.UniqueId field to store the equivalent IfcGUID. |
| 11 | +// - This IfcGUID is a "compressed" 22 character case-sensitive string. |
| 12 | +// - This unfortunately does not play nicely with systems which merge records in a case-insensitive manner (ex: PowerBI) |
| 13 | +// - To resolve the casing issue, we expand the 22 character IfcGuid (if present) into its canonical GUID representation composed of 36 hexadecimal characters and dashes ('-'). |
| 14 | + |
| 15 | +namespace Vim.Format.ElementParameterInfo |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Convenience class which extracts IfcGuid from the Element's parameters or from its UniqueId. |
| 19 | + /// </summary> |
| 20 | + public class ElementIfcInfo : IElementIndex |
| 21 | + { |
| 22 | + public Element Element { get; } |
| 23 | + |
| 24 | + public int GetElementIndexOrNone() |
| 25 | + => Element.IndexOrDefault(); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// A 22 character IFC encoded GUID composed of case-sensitive characters. |
| 29 | + /// </summary> |
| 30 | + public string IfcGuid { get; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The built-in Revit parameters which contain the IFC GUID. |
| 34 | + /// </summary> |
| 35 | + public readonly HashSet<string> BuiltInIfcGuidParameterIds = new HashSet<string>() |
| 36 | + { |
| 37 | + "-1019000", //IFC_GUID, "IfcGUID" |
| 38 | + "-1019001", //IFC_TYPE_GUID, "Type IfcGUID" |
| 39 | + "-1019002", //IFC_PROJECT_GUID, "IfcProject GUID" |
| 40 | + "-1019003", //IFC_BUILDING_GUID, "IfcBuilding GUID" |
| 41 | + "-1019004", //IFC_SITE_GUID, "IfcSite GUID" |
| 42 | + }; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// The expanded canonical Guid based on the 22 character IfcGuid. |
| 46 | + /// </summary> |
| 47 | + public Guid? IfcGuidCanonical { get; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Constructor. |
| 51 | + /// </summary> |
| 52 | + public ElementIfcInfo( |
| 53 | + Element element, |
| 54 | + ParameterTable parameterTable, |
| 55 | + ElementIndexMaps elementIndexMaps) |
| 56 | + { |
| 57 | + Element = element; |
| 58 | + |
| 59 | + var elementIndex = GetElementIndexOrNone(); |
| 60 | + |
| 61 | + var elementParameterIndices = elementIndexMaps.GetParameterIndicesFromElementIndex(elementIndex); |
| 62 | + |
| 63 | + // 0. Initialize the properties to their default null values. |
| 64 | + IfcGuid = null; |
| 65 | + IfcGuidCanonical = null; |
| 66 | + |
| 67 | + // 1. Check if the unique ID can be parsed from the element.UniqueId field. |
| 68 | + var candidateIfcGuidFromUniqueId = element.UniqueId; |
| 69 | + if (TryParseIfcGuidAsCanonicalGuid(candidateIfcGuidFromUniqueId, out var guidFromUniqueId)) |
| 70 | + { |
| 71 | + IfcGuid = candidateIfcGuidFromUniqueId; |
| 72 | + IfcGuidCanonical = guidFromUniqueId; |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // 2. Look for the relevant parameters associated to this element. |
| 77 | + foreach (var parameterIndex in elementParameterIndices) |
| 78 | + { |
| 79 | + var p = parameterTable.Get(parameterIndex); |
| 80 | + var d = p.ParameterDescriptor; |
| 81 | + var builtInId = d.Guid; // This is the built-in ID of the parameter (not to be confused with the actual IFC GUID value we're looking for) |
| 82 | + |
| 83 | + if (!BuiltInIfcGuidParameterIds.Contains(builtInId) && |
| 84 | + !d.Name.Equals("IfcGUID", StringComparison.InvariantCultureIgnoreCase)) |
| 85 | + { |
| 86 | + // This is not the parameter you are looking for. |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + var (candidateIfcGuidFromParameter, _) = p.Values; // check the native value. |
| 91 | + |
| 92 | + if (TryParseIfcGuidAsCanonicalGuid(candidateIfcGuidFromParameter, out var ifcGuidCanonical)) |
| 93 | + { |
| 94 | + IfcGuid = candidateIfcGuidFromParameter; |
| 95 | + IfcGuidCanonical = ifcGuidCanonical; |
| 96 | + } |
| 97 | + |
| 98 | + // We have just visited a IfcGUID parameter, so we can end our search. |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Characters used in the 22-char encoding |
| 104 | + public const string Base64Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$"; |
| 105 | + public const uint IfcGuidLength = 22; |
| 106 | + public const uint IfcGuidCanonicalLength = 36; |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Converts a 22-character IFC GUID into a System.Guid |
| 110 | + /// </summary> |
| 111 | + public static bool TryParseIfcGuidAsCanonicalGuid(string ifcGuid, out Guid guid) |
| 112 | + { |
| 113 | + guid = Guid.Empty; |
| 114 | + if (string.IsNullOrEmpty(ifcGuid) || ifcGuid.Length != IfcGuidLength) |
| 115 | + return false; |
| 116 | + |
| 117 | + var bytes = new byte[16]; |
| 118 | + var bitPos = 0; |
| 119 | + var bytePos = 0; |
| 120 | + var value = 0; |
| 121 | + var bitsLeft = 0; |
| 122 | + |
| 123 | + foreach (var c in ifcGuid) |
| 124 | + { |
| 125 | + var index = Base64Chars.IndexOf(c); |
| 126 | + if (index < 0) |
| 127 | + return false; |
| 128 | + |
| 129 | + value = (value << 6) | index; |
| 130 | + bitsLeft += 6; |
| 131 | + |
| 132 | + if (bitsLeft >= 8) |
| 133 | + { |
| 134 | + bitsLeft -= 8; |
| 135 | + bytes[bytePos++] = (byte)((value >> bitsLeft) & 0xFF); |
| 136 | + if (bytePos == 16) |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + guid = new Guid(bytes); |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Converts a Guid into the 22-character IFC GUID format |
| 147 | + /// </summary> |
| 148 | + public static string ToIfcGuid(Guid guid) |
| 149 | + { |
| 150 | + if (guid == Guid.Empty) |
| 151 | + { |
| 152 | + return "0000000000000000000000"; // 22 characters of 0s |
| 153 | + } |
| 154 | + |
| 155 | + var bytes = guid.ToByteArray(); |
| 156 | + |
| 157 | + var value = 0; |
| 158 | + var bitsLeft = 0; |
| 159 | + var result = new char[IfcGuidLength]; |
| 160 | + var charPos = 0; |
| 161 | + |
| 162 | + foreach (var b in bytes) |
| 163 | + { |
| 164 | + value = (value << 8) | b; |
| 165 | + bitsLeft += 8; |
| 166 | + |
| 167 | + while (bitsLeft >= 6) |
| 168 | + { |
| 169 | + bitsLeft -= 6; |
| 170 | + result[charPos++] = Base64Chars[(value >> bitsLeft) & 0x3F]; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // handle remaining bits (pad if necessary) |
| 175 | + if (charPos < IfcGuidLength) |
| 176 | + { |
| 177 | + if (bitsLeft > 0) |
| 178 | + result[charPos++] = Base64Chars[(value << (6 - bitsLeft)) & 0x3F]; |
| 179 | + |
| 180 | + // pad with zeroes if still short (shouldn’t normally happen except for Guid.Empty) |
| 181 | + while (charPos < IfcGuidLength) |
| 182 | + result[charPos++] = Base64Chars[0]; |
| 183 | + } |
| 184 | + |
| 185 | + return new string(result); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments