Files
IfcOpenShell/src/blenderbim/get_description.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.7 KiB
Python
Raw Normal View History

import os
import re
import html
import json
from pathlib import Path
import ifcopenshell
2020-11-01 20:08:48 +07:00
class Describer:
def describe(self):
# BuildingSMART does not provide a computer interpretable set of
# descriptions. They provide HTML docs, which contained malformed /
# invalid HTML. Therefore, this dodgy hack was written.
2020-11-01 20:08:48 +07:00
schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name("IFC4")
self.html_sources = {}
self.get_html_sources()
self.entity_descriptions = {}
self.enum_descriptions = {}
for entity in schema.entities():
name = entity.name()
self.get_entity_description(name)
for attribute in entity.attributes():
try:
attribute = attribute.type_of_attribute().declared_type()
except:
continue
if isinstance(attribute, str):
continue
2020-11-01 20:08:48 +07:00
if "Enum" in attribute.name() and "Enumeration" not in attribute.name():
self.get_enum_descriptions(attribute)
2020-11-01 20:08:48 +07:00
with open("entity_descriptions.json", "w") as f:
f.write(json.dumps(self.entity_descriptions, indent=4))
2020-11-01 20:08:48 +07:00
with open("enum_descriptions.json", "w") as f:
f.write(json.dumps(self.enum_descriptions, indent=4))
def get_html_sources(self):
2021-03-26 20:49:57 +11:00
html_dir = "/home/dion/Projects/IfcOpenShell/src/blenderbim/descriptions/IFC4_3/RC1/HTML"
2020-11-01 20:08:48 +07:00
for filename in Path(html_dir).rglob("*.htm"):
if "lexical" not in str(filename):
continue
name = os.path.basename(filename)[0:-4]
self.html_sources[name] = filename
def get_entity_description(self, name):
if name.lower() not in self.html_sources:
return
with open(self.html_sources[name.lower()]) as f:
for line in f:
2020-11-01 20:08:48 +07:00
if "Entity definition" in line:
self.entity_descriptions[name] = html.unescape(
re.sub("<.*?>", "", line.strip().replace("Entity definition", ""))
)
def get_enum_descriptions(self, enum):
if enum.name().lower() not in self.html_sources:
return
print(enum.name())
print(dir(enum))
for item in enum.enumeration_items():
with open(self.html_sources[enum.name().lower()]) as f:
for line in f:
2020-11-01 20:08:48 +07:00
if "<td>" + item + "</td>" in line:
self.enum_descriptions.setdefault(enum.name(), {})[item] = html.unescape(
re.sub("<.*?>", "", line.strip().replace(item, ""))
)
describer = Describer()
describer.describe()