forked from IfcOpenShell/IfcOpenShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIfcParseWrapper.i
More file actions
465 lines (412 loc) · 18.3 KB
/
IfcParseWrapper.i
File metadata and controls
465 lines (412 loc) · 18.3 KB
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 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 *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
// A class declaration to silence SWIG warning about base classes being
// undefined, the constructor is private so that SWIG does not wrap them
class IfcEntityInstanceData {
private:
IfcEntityInstanceData();
};
#define invalid_argument(i, arg_name) { throw IfcParse::IfcException(IfcSchema::Type::GetAttributeName(self->entity->type(), (unsigned char)i) + " is not a valid type for '" + arg_name + "'"); }
%ignore IfcParse::IfcFile::Init;
%ignore IfcParse::IfcFile::entityByGuid;
%ignore IfcParse::IfcFile::register_inverse;
%ignore IfcParse::IfcFile::unregister_inverse;
%ignore operator<<;
%ignore IfcParse::FileDescription::FileDescription;
%ignore IfcParse::FileName::FileName;
%ignore IfcParse::FileSchema::FileSchema;
%ignore IfcParse::IfcFile::tokens;
%ignore IfcParse::IfcSpfHeader::IfcSpfHeader(IfcSpfLexer*);
%ignore IfcParse::IfcSpfHeader::lexer;
%ignore IfcParse::IfcSpfHeader::stream;
%ignore IfcParse::HeaderEntity::is;
%ignore IfcParse::IfcFile::type_iterator;
%ignore IfcUtil::IfcBaseClass::is;
%rename("by_id") entityById;
%rename("by_type") entitiesByType;
%rename("__len__") getArgumentCount;
%rename("get_argument_type") getArgumentType;
%rename("get_argument_name") getArgumentName;
%rename("get_argument_index") getArgumentIndex;
%rename("get_argument_optionality") getArgumentOptionality;
%rename("get_attribute_names") getAttributeNames;
%rename("get_inverse_attribute_names") getInverseAttributeNames;
%rename("entity_instance") IfcBaseClass;
%rename("file") IfcFile;
%rename("add") addEntity;
%rename("remove") removeEntity;
%extend IfcParse::IfcFile {
IfcUtil::IfcBaseClass* by_guid(const std::string& guid) {
return $self->entityByGuid(guid);
}
IfcEntityList::ptr get_inverse(IfcUtil::IfcBaseClass* e) {
return $self->getInverse(e->entity->id(), IfcSchema::Type::UNDEFINED, -1);
}
void write(const std::string& fn) {
std::ofstream f(fn.c_str());
f << (*$self);
}
std::vector<unsigned> entity_names() const {
std::vector<unsigned> keys;
keys.reserve(std::distance($self->begin(), $self->end()));
for (IfcParse::IfcFile::entity_by_id_t::const_iterator it = $self->begin(); it != $self->end(); ++ it) {
keys.push_back(it->first);
}
return keys;
}
std::vector<std::string> types() const {
const size_t n = std::distance($self->types_begin(), $self->types_end());
std::vector<std::string> ts;
ts.reserve(n);
std::transform($self->types_begin(), $self->types_end(), std::back_inserter(ts), IfcSchema::Type::ToString);
return ts;
}
std::vector<std::string> types_with_super() const {
const size_t n = std::distance($self->types_incl_super_begin(), $self->types_incl_super_end());
std::vector<std::string> ts;
ts.reserve(n);
std::transform($self->types_incl_super_begin(), $self->types_incl_super_end(), std::back_inserter(ts), IfcSchema::Type::ToString);
return ts;
}
%pythoncode %{
# Hide the getters with read-only property implementations
header = property(header)
%}
}
%extend IfcUtil::IfcBaseClass {
int get_attribute_category(const std::string& name) const {
if (IfcSchema::Type::IsSimple($self->type())) {
return name == "wrappedValue";
}
IfcUtil::IfcBaseEntity* self_ = (IfcUtil::IfcBaseEntity*) self;
const std::vector<std::string> names = self_->getAttributeNames();
if (std::find(names.begin(), names.end(), name) != names.end()) {
return 1;
} else {
const std::vector<std::string> names = self_->getInverseAttributeNames();
if (std::find(names.begin(), names.end(), name) != names.end()) {
return 2;
} else {
return 0;
}
}
}
// id() is defined on IfcBaseEntity and not on IfcBaseClass, in order
// to expose it to the Python wrapper it is simply duplicated here.
// Same applies to the two methods reimplemented below.
int id() const {
return $self->entity->id();
}
std::vector<std::string> getAttributeNames() const {
if (IfcSchema::Type::IsSimple($self->type())) {
return std::vector<std::string>(1, "wrappedValue");
}
IfcUtil::IfcBaseEntity* self_ = (IfcUtil::IfcBaseEntity*) self;
return self_->getAttributeNames();
}
std::vector<std::string> getInverseAttributeNames() const {
if (IfcSchema::Type::IsSimple($self->type())) {
return std::vector<std::string>(0);
}
IfcUtil::IfcBaseEntity* self_ = (IfcUtil::IfcBaseEntity*) self;
return self_->getInverseAttributeNames();
}
bool is_a(const std::string& s) {
return self->is(IfcSchema::Type::FromString(boost::to_upper_copy(s)));
}
std::string is_a() const {
return IfcSchema::Type::ToString(self->entity->type());
}
std::pair<IfcUtil::ArgumentType,Argument*> get_argument(unsigned i) {
return std::pair<IfcUtil::ArgumentType,Argument*>($self->getArgumentType(i), $self->getArgument(i));
}
std::pair<IfcUtil::ArgumentType,Argument*> get_argument(const std::string& a) {
unsigned i = IfcSchema::Type::GetAttributeIndex($self->type(), a);
return std::pair<IfcUtil::ArgumentType,Argument*>($self->getArgumentType(i), $self->getArgument(i));
}
bool __eq__(IfcUtil::IfcBaseClass* other) const {
if ($self == other) {
return true;
}
if (IfcSchema::Type::IsSimple($self->type()) || IfcSchema::Type::IsSimple(other->type())) {
/// @todo
return false;
} else {
IfcUtil::IfcBaseEntity* self_ = (IfcUtil::IfcBaseEntity*) self;
IfcUtil::IfcBaseEntity* other_ = (IfcUtil::IfcBaseEntity*) other;
return self_->id() == other_->id() && self_->entity->file == other_->entity->file;
}
}
std::string __repr__() const {
return $self->entity->toString();
}
// Just something to have a somewhat sensible value to hash
size_t file_pointer() const {
return reinterpret_cast<size_t>($self->entity->file);
}
unsigned get_argument_index(const std::string& a) const {
return IfcSchema::Type::GetAttributeIndex(self->entity->type(), a);
}
IfcEntityList::ptr get_inverse(const std::string& a) {
std::pair<IfcSchema::Type::Enum, unsigned> inv = IfcSchema::Type::GetInverseAttribute(self->entity->type(), a);
return self->entity->getInverse(inv.first, inv.second);
}
void setArgumentAsNull(unsigned int i) {
bool is_optional = IfcSchema::Type::GetAttributeOptional(self->entity->type(), (unsigned char)i);
if (is_optional) {
self->entity->setArgument(i, new IfcWrite::IfcWriteArgument());
} else invalid_argument(i,"NULL");
}
void setArgumentAsInt(unsigned int i, int v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_INT) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else if ( (arg_type == IfcUtil::Argument_BOOL) && ( (v == 0) || (v == 1) ) ) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v == 1);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"INTEGER");
}
void setArgumentAsBool(unsigned int i, bool v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_BOOL) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"BOOLEAN");
}
void setArgumentAsDouble(unsigned int i, double v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_DOUBLE) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"REAL");
}
void setArgumentAsString(unsigned int i, const std::string& a) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_STRING) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(a);
self->entity->setArgument(i, arg);
} else if (arg_type == IfcUtil::Argument_ENUMERATION) {
std::pair<const char*, int> enum_data = IfcSchema::Type::GetEnumerationIndex(IfcSchema::Type::GetAttributeEntity(self->entity->type(), (unsigned char)i), a);
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(IfcWrite::IfcWriteArgument::EnumerationReference(enum_data.second, enum_data.first));
self->entity->setArgument(i, arg);
} else if (arg_type == IfcUtil::Argument_BINARY) {
if (IfcUtil::valid_binary_string(a)) {
boost::dynamic_bitset<> bits(a);
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(bits);
self->entity->setArgument(i, arg);
} else {
throw IfcParse::IfcException("String not a valid binary representation");
}
} else invalid_argument(i,"STRING");
}
void setArgumentAsAggregateOfInt(unsigned int i, const std::vector<int>& v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_AGGREGATE_OF_INT) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"AGGREGATE OF INT");
}
void setArgumentAsAggregateOfDouble(unsigned int i, const std::vector<double>& v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_AGGREGATE_OF_DOUBLE) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"AGGREGATE OF DOUBLE");
}
void setArgumentAsAggregateOfString(unsigned int i, const std::vector<std::string>& v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_AGGREGATE_OF_STRING) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else if (arg_type == IfcUtil::Argument_AGGREGATE_OF_BINARY) {
std::vector< boost::dynamic_bitset<> > bits;
bits.reserve(v.size());
for (std::vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it) {
if (IfcUtil::valid_binary_string(*it)) {
bits.push_back(boost::dynamic_bitset<>(*it));
} else {
throw IfcParse::IfcException("String not a valid binary representation");
}
}
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(bits);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"AGGREGATE OF STRING");
}
void setArgumentAsEntityInstance(unsigned int i, IfcUtil::IfcBaseClass* v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_ENTITY_INSTANCE) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"ENTITY INSTANCE");
}
void setArgumentAsAggregateOfEntityInstance(unsigned int i, IfcEntityList::ptr v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_AGGREGATE_OF_ENTITY_INSTANCE) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"AGGREGATE OF ENTITY INSTANCE");
}
void setArgumentAsAggregateOfAggregateOfInt(unsigned int i, const std::vector< std::vector<int> >& v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_AGGREGATE_OF_AGGREGATE_OF_INT) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"AGGREGATE OF AGGREGATE OF INT");
}
void setArgumentAsAggregateOfAggregateOfDouble(unsigned int i, const std::vector< std::vector<double> >& v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_AGGREGATE_OF_AGGREGATE_OF_DOUBLE) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"AGGREGATE OF AGGREGATE OF DOUBLE");
}
void setArgumentAsAggregateOfAggregateOfEntityInstance(unsigned int i, IfcEntityListList::ptr v) {
IfcUtil::ArgumentType arg_type = IfcSchema::Type::GetAttributeType(self->entity->type(), (unsigned char)i);
if (arg_type == IfcUtil::Argument_AGGREGATE_OF_AGGREGATE_OF_ENTITY_INSTANCE) {
IfcWrite::IfcWriteArgument* arg = new IfcWrite::IfcWriteArgument();
arg->set(v);
self->entity->setArgument(i, arg);
} else invalid_argument(i,"AGGREGATE OF AGGREGATE OF ENTITY INSTANCE");
}
}
%extend IfcParse::IfcSpfHeader {
%pythoncode %{
# Hide the getters with read-only property implementations
file_description = property(file_description)
file_name = property(file_name)
file_schema = property(file_schema)
%}
};
%extend IfcParse::FileDescription {
%pythoncode %{
# Hide the getters with read-write property implementations
__swig_getmethods__["description"] = description
__swig_setmethods__["description"] = description
description = property(description, description)
__swig_getmethods__["implementation_level"] = implementation_level
__swig_setmethods__["implementation_level"] = implementation_level
implementation_level = property(implementation_level, implementation_level)
%}
};
%extend IfcParse::FileName {
%pythoncode %{
# Hide the getters with read-write property implementations
__swig_getmethods__["name"] = name
__swig_setmethods__["name"] = name
name = property(name, name)
__swig_getmethods__["time_stamp"] = time_stamp
__swig_setmethods__["time_stamp"] = time_stamp
time_stamp = property(time_stamp, time_stamp)
__swig_getmethods__["author"] = author
__swig_setmethods__["author"] = author
author = property(author, author)
__swig_getmethods__["organization"] = organization
__swig_setmethods__["organization"] = organization
organization = property(organization, organization)
__swig_getmethods__["preprocessor_version"] = preprocessor_version
__swig_setmethods__["preprocessor_version"] = preprocessor_version
preprocessor_version = property(preprocessor_version, preprocessor_version)
__swig_getmethods__["originating_system"] = originating_system
__swig_setmethods__["originating_system"] = originating_system
originating_system = property(originating_system, originating_system)
__swig_getmethods__["authorization"] = authorization
__swig_setmethods__["authorization"] = authorization
authorization = property(authorization, authorization)
%}
};
%extend IfcParse::FileSchema {
%pythoncode %{
# Hide the getters with read-write property implementations
__swig_getmethods__["schema_identifiers"] = schema_identifiers
__swig_setmethods__["schema_identifiers"] = schema_identifiers
schema_identifiers = property(schema_identifiers, schema_identifiers)
%}
};
%include "../ifcparse/ifc_parse_api.h"
%include "../ifcparse/IfcSpfHeader.h"
%include "../ifcparse/IfcFile.h"
%include "../ifcparse/IfcBaseClass.h"
// The IfcFile* returned by open() is to be freed by SWIG/Python
%newobject open;
%newobject read;
%inline %{
IfcParse::IfcFile* open(const std::string& fn) {
IfcParse::IfcFile* f = new IfcParse::IfcFile();
f->Init(fn);
return f;
}
IfcParse::IfcFile* read(const std::string& data) {
char* copiedData = new char[data.length()];
memcpy(copiedData, data.c_str(), data.length());
IfcParse::IfcFile* f = new IfcParse::IfcFile();
f->Init((void *)copiedData, data.length());
return f;
}
const char* schema_identifier() {
return IfcSchema::Identifier;
}
const char* version() {
return IFCOPENSHELL_VERSION;
}
std::string get_supertype(std::string n) {
boost::to_upper(n);
IfcSchema::Type::Enum t = IfcSchema::Type::FromString(n);
if (IfcSchema::Type::Parent(t)) {
return IfcSchema::Type::ToString(*IfcSchema::Type::Parent(t));
} else {
return "";
}
}
IfcUtil::IfcBaseClass* new_IfcBaseClass(const std::string& s) {
IfcSchema::Type::Enum ty = IfcSchema::Type::FromString(boost::to_upper_copy(s));
IfcEntityInstanceData* data = new IfcEntityInstanceData(ty);
data->setArgument(IfcSchema::Type::GetAttributeCount(ty) - 1, new IfcWrite::IfcWriteArgument());
IfcSchema::Type::PopulateDerivedFields(data);
return IfcSchema::SchemaEntity(data);
}
%}
%{
static std::stringstream ifcopenshell_log_stream;
%}
%init %{
Logger::SetOutput(0, &ifcopenshell_log_stream);
%}
%inline %{
std::string get_log() {
std::string log = ifcopenshell_log_stream.str();
ifcopenshell_log_stream.str("");
return log;
}
%}