forked from mvaerle/python-ifc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifcschemareader.py
87 lines (68 loc) · 3.04 KB
/
ifcschemareader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import re, copy
class IfcSchema:
SIMPLETYPES = ["INTEGER", "REAL", "STRING", "NUMBER", "LOGICAL", "BOOLEAN"]
NO_ATTR = ["WHERE", "INVERSE","WR2","WR3", "WR4", "WR5", "UNIQUE", "DERIVE"]
def __init__(self, filename):
self.filename = filename
self.file = open(self.filename)
self.data = self.file.read()
self.types = self.readTypes()
self.entities = self.readEntities()
print "Parsed from schema %s: %s entities and %s types" % (self.filename, len(self.entities), len(self.types))
def readTypes(self):
"""
Parse all the possible types from the schema,
returns a dictionary Name -> Type
"""
types = {}
for m in re.finditer("TYPE (.*) = (.*);", self.data):
typename, typetype = m.groups()
if typetype in self.SIMPLETYPES:
types[typename] = typetype
else:
types[typename] = "#" + typetype
return types
def readEntities(self):
"""
Parse all the possible entities from the schema,
returns a dictionary of the form:
{ name: {
"supertype": supertype,
"attributes": [{ key: value }, ..]
}}
"""
entities = {}
# Regexes must be greedy to prevent matching outer entity and end_entity strings
# Regexes have re.DOTALL to match newlines
for m in re.finditer("ENTITY (.*?)END_ENTITY;", self.data, re.DOTALL):
entity = {}
raw_entity_str = m.groups()[0]
entity["name"] = re.search("(.*?)[;|\s]", raw_entity_str).groups()[0].upper()
subtypeofmatch = re.search(".*SUBTYPE OF \((.*?)\);", raw_entity_str)
entity["supertype"] = subtypeofmatch.groups()[0].upper() if subtypeofmatch else None
# find the shortest string matched from the end of the entity type header to the
# first occurence of a NO_ATTR string (when it occurs on a new line)
inner_str = re.search(";(.*?)$", raw_entity_str, re.DOTALL).groups()[0]
attrs_str = min([inner_str.partition("\r\n "+a)[0] for a in self.NO_ATTR])
attrs = []
for am in re.finditer("(.*?) : (.*?);", attrs_str, re.DOTALL):
name, attr_type = [s.replace("\r\n\t","") for s in am.groups()]
attrs.append((name, attr_type))
entity["attributes"] = attrs
entities[entity["name"]] = entity
return entities
def getAttributes(self, name):
"""
Get all attributes af an entity, including supertypes
"""
ent = self.entities[name]
attrs = []
while ent != None:
this_ent_attrs = copy.copy(ent["attributes"])
this_ent_attrs.reverse()
attrs.extend(this_ent_attrs)
ent = self.entities.get(ent["supertype"], None)
attrs.reverse()
return attrs
if __name__ == "__main__":
schema = IfcSchema("IFC2X3_TC1.exp")