forked from mvaerle/python-ifc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathifcfilereader.py
146 lines (116 loc) · 4.21 KB
/
ifcfilereader.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: UTF-8 -*-
import re
from ifcschemareader import IfcSchema
IFCLINE_RE = re.compile("#(\d+)[ ]?=[ ]?(.*?)\((.*)\);[\\r]?$")
class IfcFile:
"""
Parses an ifc file given by filename, entities can be retrieved by name and id
The whole file is stored in a dictionary (in memory)
"""
entsById = {}
entsByName = {}
def __init__(self, filename, schema):
self.filename = filename
self.schema = schema
self.file = open(self.filename)
self.entById, self.entsByName = self.read()
print "Parsed from file %s: %s entities" % (self.filename, len(self.entById))
self.file.close()
def getEntityById(self, id):
return self.entById.get(id, None)
def getEntitiesByName(self, name):
return self.entsByName.get(name, None)
def read(self):
"""
Returns 2 dictionaries, entById and entsByName
"""
entById = {}
entsByName = {}
for line in self.file:
e = self.parseLine(line)
if e:
entById[int(e["id"])] = e
ids = e.get(e["name"],[])
ids.append(e["id"])
entsByName[e["name"]] = list(set(ids))
return [entById, entsByName]
def parseLine(self, line):
"""
Parse a line
"""
m = IFCLINE_RE.search(line) # id,name,attrs
if m:
id, name, attrs = m.groups()
else:
return False
return {"id": id, "name": name, "attributes": self.parseAttributes(name, attrs)}
def parseAttributes(self, ent_name, attrs_str):
"""
Parse the attributes of a line
"""
parts = []
lastpos = 0
while lastpos < len(attrs_str):
newpos = self.nextString(attrs_str, lastpos)
parts.extend(self.parseAttribute(attrs_str[lastpos:newpos-1]))
lastpos = newpos
schema_attributes = self.schema.getAttributes(ent_name)
# assert len(schema_attributes) == len(parts), \
# "Expected %s attributes, got %s (entity: %s" % \
# (len(schema_attributes), len(parts), ent_name)
attribute_names = [a[0] for a in schema_attributes]
return dict(zip(attribute_names, parts))
def parseAttribute(self, attr_str):
"""
Map a single attribute to a python type (recursively)
"""
parts = []
lastpos = 0
while lastpos < len(attr_str):
newpos = self.nextString(attr_str, lastpos)
s = attr_str[lastpos:newpos-1]
if (s[0] == "(" and s[-1] == ")"): # list, recurse
parts.append(self.parseAttribute(s[1:-1]))
else:
try:
parts.append(float(s)) # number, any kind
except ValueError:
if s[0] == "'" and s[-1] == "'": # string
parts.append(s[1:-1])
elif s == "$":
parts.append(None)
else:
parts.append(s) # ref, enum or other
lastpos = newpos
return parts
def nextString(self, s, start):
"""
Parse the data part of a line
"""
parens = 0
quotes = 0
for pos in range(start,len(s)):
c = s[pos]
if c == "," and parens == 0 and quotes == 0:
return pos+1
elif c == "(" and quotes == 0:
parens += 1
elif c == ")" and quotes == 0:
parens -= 1
elif c == "\'" and quotes == 0:
quotes = 1
elif c =="\'" and quotes == 1:
quotes = 0
return len(s)+1
if __name__ == "__main__":
import time
t1 = time.time()
schema = IfcSchema("IFC2X3_TC1.exp")
t2 = time.time()
print "Loading schema took: %s s \n" % ((t2-t1))
t1 = time.time()
ifcfile = IfcFile("testdata/AC11-FZK-Haus-IFC.ifc", schema)
t2 = time.time()
print "Loading file took: %s s \n" % ((t2-t1))
print ifcfile.getEntityById(233), "\n"
print ifcfile.getEntityById(216), "\n"