|
| 1 | +# IfcGloballyUniqueId.rb |
| 2 | +# |
| 3 | +# Copyright 2017 Jan Brouwer <[email protected]> |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation; either version 2 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | +# MA 02110-1301, USA. |
| 19 | +# |
| 20 | +# |
| 21 | + |
| 22 | +# (!) Note: securerandom takes very long to load |
| 23 | +require 'securerandom' |
| 24 | + |
| 25 | +module BimTools |
| 26 | + module IfcManager |
| 27 | + |
| 28 | + class IfcGloballyUniqueId |
| 29 | + |
| 30 | + # possible characters in GUID |
| 31 | + GUID64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$' |
| 32 | + |
| 33 | + def initialize( sketchup = nil ) |
| 34 | + @sketchup = sketchup |
| 35 | + |
| 36 | + # if sketchup object has a GUID, then use that, otherwise create new |
| 37 | + if @sketchup && defined?( @sketchup.guid ) |
| 38 | + @hex_guid = unformat_guid( @sketchup.guid ) |
| 39 | + else |
| 40 | + @hex_guid = new_guid |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + def set_parent_guid( parent_hex_guid ) |
| 45 | + @hex_guid = combined_guid( @hex_guid, parent_hex_guid ) |
| 46 | + end # def set_parent |
| 47 | + |
| 48 | + # return IfcGloballyUniqueId within quotes |
| 49 | + def step() |
| 50 | + return hex_to_ifc_guid( @hex_guid ) |
| 51 | + end # def step |
| 52 | + |
| 53 | + def to_s() |
| 54 | + return @hex_guid |
| 55 | + end # def to_s |
| 56 | + |
| 57 | + def to_json(arg=nil) |
| 58 | + return "#{@hex_guid[0..7]}-#{@hex_guid[8..11]}-#{@hex_guid[12..15]}-#{@hex_guid[16..19]}-#{@hex_guid[20..31]}" |
| 59 | + end # def to_s |
| 60 | + |
| 61 | + # recognize guid type and reformat to unformatted hex version |
| 62 | + def unformat_guid( guid ) |
| 63 | + |
| 64 | + # check if ifc_guid(length is 22) or uuid |
| 65 | + if guid.length == 22 |
| 66 | + guid = ifc_guid_to_hex( guid ) |
| 67 | + else |
| 68 | + |
| 69 | + # tr('-', ''): removes the dashes from the hex string |
| 70 | + guid.tr('-', '') |
| 71 | + end |
| 72 | + end # def unformat_guid |
| 73 | + |
| 74 | + # combine guid with parent guid |
| 75 | + def combined_guid( sketchup_guid, parent_guid ) |
| 76 | + guid = (sketchup_guid.to_i(16) ^ parent_guid.to_i(16)).to_s(16).rjust(32, '0') |
| 77 | + |
| 78 | + # The digit at position 1 above is always "4" |
| 79 | + # set the four most significant bits of the 7th byte to 0100'B, so the high nibble is "4" |
| 80 | + guid[12] = "4" |
| 81 | + |
| 82 | + # and the digit at position 2 is always one of "8", "9", "A" or "B". |
| 83 | + # set the two most significant bits of the 9th byte to 10'B, so the high nibble will be one of "8", "9", "A", or "B". |
| 84 | + h_val = guid[16] |
| 85 | + b_val = [h_val].pack('H*').unpack('B*')[0] |
| 86 | + b_val[0] = "1" |
| 87 | + b_val[1] = "0" |
| 88 | + guid[16] = [b_val].pack('B*').unpack('H*')[0] |
| 89 | + return guid |
| 90 | + end # def combined_guid |
| 91 | + |
| 92 | + # convert IfcGloballyUniqueId into unformatted hex number |
| 93 | + def ifc_guid_to_hex( ifc_guid ) |
| 94 | + bin = "" |
| 95 | + length = 2 |
| 96 | + ifc_guid.each_char do | char | |
| 97 | + n = GUID64.index( char.to_s ) |
| 98 | + bin = bin + n.to_s( 2 ).rjust( length, "0" ) |
| 99 | + length = 6 |
| 100 | + end |
| 101 | + return [bin].pack('B*').unpack('H*')[0] |
| 102 | + end # def ifc_guid_to_hex |
| 103 | + |
| 104 | + # # convert IfcGloballyUniqueId into UUID |
| 105 | + # def ifc_guid_to_uuid( ifc_guid ) |
| 106 | + # return ifc_guid_to_hex( ifc_guid ).insert(20, '-').insert(16, '-').insert(12, '-').insert(8, '-') |
| 107 | + # end # def ifc_guid_to_uuid |
| 108 | + |
| 109 | + # convert unformatted hex number into IfcGloballyUniqueId |
| 110 | + def hex_to_ifc_guid( hex_guid ) |
| 111 | + ifc_guid = "" |
| 112 | + |
| 113 | + # https://www.cryptosys.net/pki/uuid-rfc4122.html |
| 114 | + # pack('H*'): converts the hex string to a binary number (high nibble first) |
| 115 | + # unpack('B*'): converts the binary number to a bit string (128 0's and 1's) and places it into an array (Most Significant Block first) |
| 116 | + # [0]: gets the first (and only) value from the array |
| 117 | + bit_string = [hex_guid].pack('H*').unpack('B*')[0].to_s |
| 118 | + |
| 119 | + # take the number (0 - 63) and find the matching character in guid64, add the found character to the guid string |
| 120 | + # start with the 2 leftover bits |
| 121 | + char_num = bit_string[0,2].to_i(2) |
| 122 | + ifc_guid << GUID64[char_num] |
| 123 | + block_counter = 2 |
| 124 | + while block_counter < 128 do |
| 125 | + char_num = bit_string[ block_counter, 6 ].to_i( 2 ) |
| 126 | + ifc_guid << GUID64[char_num] |
| 127 | + block_counter += 6 |
| 128 | + end |
| 129 | + return "'#{ifc_guid}'" |
| 130 | + end # def hex_to_ifc_guid |
| 131 | + |
| 132 | + def new_guid |
| 133 | + |
| 134 | + # Old method: faster, but not a correct IfcGloballyUniqueId, just a random number |
| 135 | + # the leading 0 is added because the first character is only 1 bit and can only contain a 0,1,2 or 3. |
| 136 | + # while all other characters are 6 bits (64 possible values) |
| 137 | + #guid = '';21.times{|i|guid<<'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$'[rand(64)]} |
| 138 | + #first = rand(0...3).to_s |
| 139 | + #guid = "#{first}#{guid}" |
| 140 | + |
| 141 | + # SecureRandom.uuid: creates a 128 bit UUID hex string |
| 142 | + # convert to hex |
| 143 | + return unformat_guid( SecureRandom.uuid ) |
| 144 | + end |
| 145 | + end # class IfcGloballyUniqueId |
| 146 | + end # module IfcManager |
| 147 | +end # module BimTools |
0 commit comments