2022-01-19 12:18:33 +11:00
|
|
|
# IfcOpenShell - IFC toolkit and geometry engine
|
|
|
|
|
# Copyright (C) 2021 Thomas Krijnen <thomas@aecgeeks.com>
|
|
|
|
|
#
|
|
|
|
|
# This file is part of IfcOpenShell.
|
|
|
|
|
#
|
|
|
|
|
# IfcOpenShell is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# IfcOpenShell is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-05-07 17:32:47 +10:00
|
|
|
"""Reads and writes encoded GlobalIds
|
|
|
|
|
|
|
|
|
|
IFC entities may be identified using a unique ID (called a UUID or GUID). This
|
|
|
|
|
128-bit label is often represented in the form
|
|
|
|
|
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. However, in IFC, it is also usually
|
|
|
|
|
stored as a 22 character base 64 encoded string. This module lets you convert
|
|
|
|
|
between these representations and generate new UUIDs.
|
|
|
|
|
"""
|
2017-11-01 10:41:27 +01:00
|
|
|
|
2017-10-06 16:03:04 +02:00
|
|
|
import uuid
|
2015-01-05 14:11:42 +00:00
|
|
|
import string
|
|
|
|
|
|
2015-01-20 13:52:08 +00:00
|
|
|
from functools import reduce
|
|
|
|
|
|
2020-11-01 20:08:27 +07:00
|
|
|
chars = string.digits + string.ascii_uppercase + string.ascii_lowercase + "_$"
|
2015-01-05 14:11:42 +00:00
|
|
|
|
2017-11-06 09:10:28 +01:00
|
|
|
|
2015-01-05 14:11:42 +00:00
|
|
|
def compress(g):
|
2020-11-01 20:08:27 +07:00
|
|
|
bs = [int(g[i : i + 2], 16) for i in range(0, len(g), 2)]
|
2017-11-06 09:10:28 +01:00
|
|
|
|
2015-01-05 14:11:42 +00:00
|
|
|
def b64(v, l=4):
|
2024-07-26 12:00:28 +10:00
|
|
|
return "".join([chars[(v // (64**i)) % 64] for i in range(l)][::-1])
|
2017-11-06 11:06:40 +01:00
|
|
|
|
2020-11-01 20:08:27 +07:00
|
|
|
return "".join([b64(bs[0], 2)] + [b64((bs[i] << 16) + (bs[i + 1] << 8) + bs[i + 2]) for i in range(1, 16, 3)])
|
2017-11-06 09:10:28 +01:00
|
|
|
|
2015-01-05 14:11:42 +00:00
|
|
|
|
|
|
|
|
def expand(g):
|
|
|
|
|
def b64(v):
|
|
|
|
|
return reduce(lambda a, b: a * 64 + b, map(lambda c: chars.index(c), v))
|
2017-11-06 11:06:40 +01:00
|
|
|
|
2015-01-05 14:11:42 +00:00
|
|
|
bs = [b64(g[0:2])]
|
|
|
|
|
for i in range(5):
|
2020-11-01 20:08:27 +07:00
|
|
|
d = b64(g[2 + 4 * i : 6 + 4 * i])
|
2017-11-06 09:10:28 +01:00
|
|
|
bs += [(d >> (8 * (2 - j))) % 256 for j in range(3)]
|
2020-11-01 20:08:27 +07:00
|
|
|
return "".join(["%02x" % b for b in bs])
|
2017-11-06 09:10:28 +01:00
|
|
|
|
2015-01-05 14:11:42 +00:00
|
|
|
|
|
|
|
|
def split(g):
|
2020-11-01 20:08:27 +07:00
|
|
|
return "{%s-%s-%s-%s-%s}" % (g[:8], g[8:12], g[12:16], g[16:20], g[20:])
|
2017-11-06 09:10:28 +01:00
|
|
|
|
|
|
|
|
|
2017-10-06 16:03:04 +02:00
|
|
|
def new():
|
|
|
|
|
return compress(uuid.uuid4().hex)
|