-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifcReader.py
executable file
·447 lines (388 loc) · 16.6 KB
/
ifcReader.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#***************************************************************************
#* *
#* Copyright (c) 2011 *
#* Yorik van Havre, Marijn van Aerle *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU General Public License (GPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program 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 Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
import os, re, copy
__title__="FreeCAD IFC parser"
__author__ = "Yorik van Havre, Marijn van Aerle"
__url__ = "http://free-cad.sourceforge.net"
'''
FreeCAD IFC parser, by Yorik van Havre, based on work by Marijn van Aerle
Usage:
import ifcReader
ifcdoc = ifcReader.IfcDocument("path/to/file.ifc")
print ifcdoc.Entities
myent = ifcdoc.Entities[20] # returns one entity
myent = ifcdoc.getEnt(20) # alternative way
polylines = ifcdoc.getEnt("IFCPOLYLINE") # returns a list
print myent.attributes
The ifc document contains a list of entities, that can be retrieved
by iterating the list (indices corresponds to the entities ids)
or by using the getEnt() method. All entities have id, type
and attributes. Attributes can have values such as text or number,
or a link to another entity.
Important note:
1) For this reader to function, you need an IFC Schema Express file (.exp)
available here:
http://www.steptools.com/support/stdev_docs/express/ifc2x3/ifc2x3_tc1.exp
For licensing reasons we are not allowed to ship that file with FreeCAD.
Just place the .exp file together with this script.
2) IFC files can have ordered content (ordered list, no entity number missing)
or be much messier (entity numbers missing, etc). The performance of the reader
will be drastically different.
'''
IFCLINE_RE = re.compile("#(\d+)[ ]?=[ ]?(.*?)\((.*)\);[\\r]?$")
DEBUG = False
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
if not os.path.exists(filename):
raise ImportError("no IFCSchema file found!")
else:
self.file = open(self.filename)
self.data = self.file.read()
self.types = self.readTypes()
self.entities = self.readEntities()
if DEBUG: 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
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 = IfcSchema(schema)
self.file = open(self.filename)
self.entById, self.entsByName, self.header = self.read()
self.file.close()
if DEBUG: print "Parsed from file %s: %s entities" % (self.filename, len(self.entById))
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 = {}
header = 'HEADER '
readheader = False
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))
elif 'HEADER' in line:
readheader = True
elif readheader:
if 'ENDSEC' in line:
readheader = False
else:
header += line
return [entById, entsByName, header]
def parseLine(self, line):
"""
Parse a line
"""
m = IFCLINE_RE.search(line) # id,name,attrs
if m:
id, name, attrs = m.groups()
id = id.strip()
name = name.strip()
attrs = attrs.strip()
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
class IfcEntity:
"a container for an IFC entity"
def __init__(self,ent,doc=None):
self.data = ent
self.id = int(ent['id'])
self.type = ent['name'].upper().strip(",[]()")
self.attributes = ent['attributes']
self.doc = doc
def __repr__(self):
return str(self.id) + ' : ' + self.type + ' ' + str(self.attributes)
def getProperty(self,propName):
"finds the value of the given property or quantity in this object, if exists"
propsets = self.doc.find('IFCRELDEFINESBYPROPERTIES','RelatedObjects',self)
if not propsets: return None
propset = []
for p in propsets:
if hasattr(p.RelatingPropertyDefinition,"HasProperties"):
propset.extend(p.RelatingPropertyDefinition.HasProperties)
elif hasattr(p.RelatingPropertyDefinition,"Quantities"):
propset.extend(p.RelatingPropertyDefinition.Quantities)
for prop in propset:
if prop.Name == propName:
print "found valid",prop
if hasattr(prop,"LengthValue"):
return prop.LengthValue
elif hasattr(prop,"AreaValue"):
return prop.AreaValue
elif hasattr(prop,"VolumeValue"):
return prop.VolumeValue
elif hasattr(prop,"NominalValue"):
return prop.NominalValue
return None
def getAttribute(self,attr):
"returns the value of the given attribute, if exists"
if hasattr(self,attr):
return self.__dict__[attr]
return None
class IfcDocument:
"an object representing an IFC document"
def __init__(self,filename,schema="IFC2X3_TC1.exp",debug=False):
DEBUG = debug
f = IfcFile(filename,schema)
self.filename = filename
self.data = f.entById
self.Entities = {0:f.header}
for k,e in self.data.iteritems():
eid = int(e['id'])
self.Entities[eid] = IfcEntity(e,self)
if DEBUG: print len(self.Entities),"entities created. Creating attributes..."
for k,ent in self.Entities.iteritems():
if DEBUG: print "attributing entity ",ent
if hasattr(ent,"attributes"):
for k,v in ent.attributes.iteritems():
if DEBUG: print "parsing attribute: ",k," value ",v
if isinstance(v,str):
val = self.__clean__(v)
elif isinstance(v,list):
val = []
for item in v:
if isinstance(item,str):
val.append(self.__clean__(item))
else:
val.append(item)
else:
val = v
setattr(ent,k.strip(),val)
if DEBUG: print "Document successfully created"
def __clean__(self,value):
"turns an attribute value into something usable"
try:
val = value.strip(" ()'")
if val[:3].upper() == "IFC":
if "IFCTEXT" in val.upper():
l = val.split("'")
if len(l) == 3: val = l[1]
elif "IFCBOOLEAN" in value.upper():
l = val.split(".")
if len(l) == 3: val = l[1]
if val.upper() == "F": val = False
elif val.upper() == "T": val = True
elif "IFCREAL" in val.upper():
l = val.split("(")
if len(l) == 2: val = float(l[1].strip(")"))
else:
if '#' in val:
if "," in val:
val = val.split(",")
l = []
for subval in val:
if '#' in subval:
s = subval.strip(" #")
if DEBUG: print "referencing ",s," : ",self.getEnt(int(s))
l.append(self.getEnt(int(s)))
val = l
else:
val = val.strip()
val = val.replace("#","")
if DEBUG: print "referencing ",val," : ",self.getEnt(int(val))
val = self.getEnt(int(val))
if not val:
val = value
except:
if DEBUG: print "error parsing attribute",value
val = value
return val
def __repr__(self):
return "IFC Document: " + self.filename + ', ' + str(len(self.Entities)) + " entities "
def getEnt(self,ref):
"gets an entity by id number, or a list of entities by type"
if isinstance(ref,int):
if ref in self.Entities:
return self.Entities[ref]
elif isinstance(ref,str):
l = []
ref = ref.upper()
for k,ob in self.Entities.iteritems():
if hasattr(ob,"type"):
if ob.type == ref:
l.append(ob)
return l
return None
def search(self,pat):
"searches entities types for partial match"
l = []
pat = pat.upper()
for k,ob in self.Entities.iteritems():
if hasattr(ob,"type"):
if pat in ob.type:
if not ob.type in l:
l.append(ob.type)
return l
def find(self,pat1,pat2=None,pat3=None):
'''finds objects in the current IFC document.
arguments can be of the following form:
- (pattern): returns object types matching the given pattern (same as search)
- (type,property,value): finds, in all objects of type "type", those whose
property "property" has the given value
'''
if pat3:
bobs = self.getEnt(pat1)
obs = []
for bob in bobs:
if hasattr(bob,pat2):
if bob.getAttribute(pat2) == pat3:
obs.append(bob)
return obs
elif pat1:
ll = self.search(pat1)
obs = []
for l in ll:
obs.extend(self.getEnt(l))
return obs
return None
if __name__ == "__main__":
print __doc__