-
-
Notifications
You must be signed in to change notification settings - Fork 901
Expand file tree
/
Copy pathparse_ifcxml.cpp
More file actions
725 lines (643 loc) · 30.2 KB
/
parse_ifcxml.cpp
File metadata and controls
725 lines (643 loc) · 30.2 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/********************************************************************************
* *
* 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/>. *
* *
********************************************************************************/
#define BOOST_RESULT_OF_USE_DECLTYPE
#ifdef WITH_IFCXML
#include "IfcFile.h"
#include "IfcLogger.h"
#include <boost/algorithm/string.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/any.hpp>
#include <libxml/parser.h>
namespace {
// Base case: when there are no more types left to check.
template <typename Fn>
void visit_any_impl(Fn& fn, const boost::any& a) {
}
// Recursive case: Check the first type in the pack.
template <typename Fn, typename T, typename... Types>
void visit_any_impl(Fn& fn, const boost::any& a) {
if (a.type() == typeid(T)) {
Fn(boost::any_cast<T>(a));
} else {
visit_any_impl<Types...>(a);
}
}
// Helper to prepend a type to a tuple
template <typename Fn, typename U>
void visit_any(Fn fn, const boost::any& a);
template <typename Fn, typename... Types>
void visit_any(Fn fn, const boost::any & a) {
visit_any_impl<Fn, Types...>(fn, a);
};
}
// For debug printing on release builds
// #undef NDEBUG
// ifcXML is quite radically different for ifc2x3 and ifc4. ifc2x3 follows
// iso 10303 part 28 and puts all attribute values in XML text nodes. ifc4
// has attributes in actual XML attributes and may include inverse attributes
// to make trees more compact.
enum ifcxml_dialect {
ifcxml_dialect_ifc2x3,
ifcxml_dialect_ifc4,
ifcxml_dialect_unknown
};
// "Dereferences" a named_attribute. For example:
// IfcCompoundPlaneAngleMeasure -> LIST [3:4] OF INTEGER
void follow_named(const IfcParse::parameter_type*& pt) {
while (pt->as_named_type() != nullptr) {
if (pt->as_named_type()->declared_type()->as_type_declaration() != nullptr) {
pt = pt->as_named_type()->declared_type()->as_type_declaration()->declared_type();
} else {
break;
}
}
}
// The ifcXML parser uses SAX so we need to keep a stack of where we are in
// the file. These different kinds of nodes could be subclasses but aren't
// for ease in pushing to std::vector.
class stack_node {
public:
enum node_type {
stack_empty,
node_instance,
node_instance_attribute,
node_aggregate,
node_aggregate_element,
node_inverse,
node_select,
node_header,
node_header_entry
};
// to be coerced into the correct type later on
std::vector<boost::any> aggregate_elements;
protected:
node_type type_;
IfcUtil::IfcBaseClass* inst_;
int idx_;
const IfcParse::inverse_attribute* inv_;
std::string tagname_;
std::string id_in_file_;
const IfcParse::parameter_type* aggregate_elem_type_;
stack_node()
: type_(stack_empty),
inst_(nullptr),
idx_(-1),
inv_(nullptr),
aggregate_elem_type_(nullptr) {}
public:
static stack_node instance(const std::string& id, IfcUtil::IfcBaseClass* inst) {
stack_node node;
node.type_ = node_instance;
node.inst_ = inst;
node.id_in_file_ = id;
return node;
}
static stack_node instance_attribute(IfcUtil::IfcBaseClass* inst, int idx) {
stack_node node;
node.type_ = node_instance_attribute;
node.inst_ = inst;
node.idx_ = idx;
return node;
}
static stack_node aggregate(IfcUtil::IfcBaseClass* inst, int idx) {
stack_node node;
node.type_ = node_aggregate;
node.inst_ = inst;
node.idx_ = idx;
return node;
}
static stack_node aggregate_element(const IfcParse::parameter_type* aggregate_elem_type, int idx) {
stack_node node;
node.type_ = node_aggregate_element;
node.idx_ = idx;
node.aggregate_elem_type_ = aggregate_elem_type;
return node;
}
static stack_node inverse(IfcUtil::IfcBaseClass* inst, const IfcParse::inverse_attribute* inv) {
stack_node node;
node.type_ = node_inverse;
node.inst_ = inst;
node.inv_ = inv;
return node;
}
static stack_node select(IfcUtil::IfcBaseClass* inst, int idx) {
stack_node node;
node.type_ = node_select;
node.inst_ = inst;
node.idx_ = idx;
return node;
}
static stack_node header() {
stack_node node;
node.type_ = node_header;
return node;
};
static stack_node header_entry(const std::string& tagname) {
stack_node node;
node.type_ = node_header_entry;
node.tagname_ = tagname;
return node;
};
node_type ntype() const { return type_; }
IfcUtil::IfcBaseClass* inst() const { return inst_; }
int idx() const { return idx_; }
const IfcParse::inverse_attribute* inv_attr() const { return inv_; }
const std::string& tagname() const { return tagname_; }
const std::string& id() const { return id_in_file_; }
const IfcParse::parameter_type* aggregate_elem_type() const { return aggregate_elem_type_; }
std::string repr() const {
std::stringstream stream;
static const char* const node_type_names[] = {"empty", "inst", "attr", "aggr", "agelem", "inv", "sel", "head", "hdentry"};
stream << "[" << node_type_names[type_] << "] ";
if (inst_ != nullptr) {
stream << inst_->declaration().name() << " ";
}
if (type_ == node_aggregate) {
stream << "{" << aggregate_elements.size() << " elems} ";
}
if (idx_ != -1) {
stream << idx_ << " ";
}
return stream.str();
}
};
struct ifcxml_parse_state {
IfcParse::IfcFile* file;
std::vector<stack_node> stack;
std::map<std::string, int> idmap;
std::vector<std::tuple<IfcUtil::IfcBaseEntity*, size_t, std::string>> forward_references;
ifcxml_dialect dialect;
};
// ifc4 allows for aggregates to be concatenated using whitespace.
template <typename T>
std::vector<T> split(const std::string& value) {
std::vector<std::string> strs;
boost::split(
strs, value, [](char character) { return character == ' '; }, boost::token_compress_on);
std::vector<T> r(strs.size());
boost::copy(strs | boost::adaptors::transformed([](const std::string& s) {
return boost::lexical_cast<T>(s);
}),
r.begin());
return r;
}
boost::any parse_attribute_value(const IfcParse::parameter_type* ty, const std::string& value) {
boost::any any;
auto cpp_type = IfcUtil::from_parameter_type(ty);
if (cpp_type == IfcUtil::Argument_STRING) {
any = value;
} else if (cpp_type == IfcUtil::Argument_ENUMERATION) {
const auto* enum_type = ty->as_named_type()->declared_type()->as_enumeration_type();
std::vector<std::string>::const_iterator iter = std::find(
enum_type->enumeration_items().begin(),
enum_type->enumeration_items().end(),
boost::to_upper_copy(value));
any = EnumerationReference(enum_type, std::distance(enum_type->enumeration_items().begin(), iter));
} else if (cpp_type == IfcUtil::Argument_INT) {
any = boost::lexical_cast<int>(value);
} else if (cpp_type == IfcUtil::Argument_DOUBLE) {
any = boost::lexical_cast<double>(value);
} else if (cpp_type == IfcUtil::Argument_BOOL) {
any = boost::to_lower_copy(value) == "true";
} else if (cpp_type == IfcUtil::Argument_AGGREGATE_OF_INT) {
any = split<int>(value);
} else if (cpp_type == IfcUtil::Argument_AGGREGATE_OF_DOUBLE) {
any = split<double>(value);
}
if (any.empty()) {
Logger::Error("Attribute '" + value + "' not successfully parsed");
}
return any;
}
static void end_element(void* user, const xmlChar* tag) {
ifcxml_parse_state* state = (ifcxml_parse_state*)user;
if (state->file == nullptr) {
return;
}
if (!state->stack.empty() && state->stack.back().ntype() == stack_node::node_aggregate) {
const auto& back = state->stack.back();
auto& elems = state->stack.back().aggregate_elements;
/*
auto* list = new IfcParse::ArgumentList(elems.size());
size_t i = 0;
for (auto& elem : elems) {
list->arguments()[i++] = elem;
}
*/
// @todo
// back.inst()->set_attribute_value(back.idx(), elems);
}
if (state->dialect == ifcxml_dialect_ifc2x3 && state->stack.back().ntype() == stack_node::node_instance) {
if (state->stack.back().inst() != nullptr) {
state->idmap[state->stack.back().id()] = state->file->addEntity(state->stack.back().inst())->id();
}
}
const std::string tagname = (char*)tag;
// ignore uos ex:iso_10303_28 (ifc2x3) and ifc:ifcXML (ifc4)
if (tagname != "uos" && tagname != "ex:iso_10303_28" && tagname != "ifc:ifcXML" && tagname != "ifcXML") {
if (state->stack.empty()) {
Logger::Error("Mismatch in parse stack due to previous errors");
} else {
state->stack.pop_back();
}
}
}
static void process_characters(void* user, const xmlChar* character, int len) {
ifcxml_parse_state* state = (ifcxml_parse_state*)user;
if (state->file == nullptr) {
return;
}
std::string txt((char*)character, len);
stack_node::node_type state_type = stack_node::stack_empty;
if (!state->stack.empty()) {
state_type = state->stack.back().ntype();
}
if (!state->stack.empty() && state->stack.back().inst() != nullptr && (state->stack.back().inst()->declaration().as_type_declaration() != nullptr)) {
const auto* pt = state->stack.back().inst()->declaration().as_type_declaration()->declared_type();
boost::any val;
try {
val = parse_attribute_value(pt, txt);
} catch (const std::exception& e) {
Logger::Error(e, state->stack.back().inst());
}
if (!val.empty()) {
// type declaration always at idx 0
visit_any([&state](auto& v) {
state->stack.back().inst()->set_attribute_value(0, v);
}, val);
}
} else if (state_type == stack_node::node_header_entry) {
const std::string tagname = boost::replace_all_copy(state->stack.back().tagname(), "ex:", "");
auto& header = state->file->header();
if (tagname == "name") {
header.file_name()->setname(txt);
} else if (tagname == "time_stamp") {
header.file_name()->settime_stamp(txt);
} else if (tagname == "author") {
header.file_name()->setauthor({txt});
} else if (tagname == "organization") {
header.file_name()->setorganization({txt});
} else if (tagname == "preprocessor_version") {
header.file_name()->setpreprocessor_version(txt);
} else if (tagname == "originating_system") {
header.file_name()->setoriginating_system(txt);
} else if (tagname == "authorization") {
header.file_name()->setauthorization(txt);
} else if (tagname == "documentation") {
header.file_description()->setdescription({txt});
} else {
Logger::Error("Unrecognized header entry " + tagname);
}
} else if (state_type == stack_node::node_instance_attribute) {
const auto* pt = state->stack.back().inst()->declaration().as_entity()->attribute_by_index(state->stack.back().idx())->type_of_attribute();
auto cpp_type = IfcUtil::from_parameter_type(pt);
if (cpp_type != IfcUtil::Argument_ENTITY_INSTANCE) {
auto val = parse_attribute_value(pt, txt);
if (!val.empty()) {
visit_any([&state](auto& v) {
state->stack.back().inst()->set_attribute_value(state->stack.back().idx(), v);
}, val);
}
}
} else if (state_type == stack_node::node_aggregate_element) {
const auto* pt = state->stack.back().aggregate_elem_type();
auto val = parse_attribute_value(pt, txt);
if (!val.empty()) {
(*(state->stack.rbegin() + 1)).aggregate_elements.push_back(val);
}
}
}
static void start_element(void* user, const xmlChar* tag, const xmlChar** attrs) {
ifcxml_parse_state* state = (ifcxml_parse_state*)user;
std::string tagname = (char*)tag;
#ifndef NDEBUG
std::cout << "stack:" << std::endl;
{
int i = 1;
for (auto& node : state->stack) {
std::cout << " " << (i++) << ":" << node.repr() << std::endl;
}
}
std::cout << std::string(state->stack.size(), ' ') << "<" << tagname << ">";
#endif
std::vector<std::pair<std::string, std::string>> attributes;
if (attrs != nullptr) {
std::string attrname;
int i = 0;
while (attrs[i] != NULL) {
if ((i % 2) != 0) {
const std::string value = (char*)attrs[i];
#ifndef NDEBUG
std::cout << " " << attrname << "='" << value << "'";
#endif
attributes.push_back(std::make_pair(attrname, value));
if ((tagname == "ifc:ifcXML" || tagname == "ifcXML") && attrname == "xsi:schemaLocation" &&
(boost::starts_with(value, "http://www.buildingsmart-tech.org/ifcXML/IFC4") ||
boost::starts_with(value, "http://www.buildingsmart-tech.org/ifc/IFC4"))) {
// We're expecting a schemaLocation like "http://www.buildingsmart-tech.org/ifcXML/IFC4/Add2 IFC4_ADD2_TC1.xsd"
// With token compression this is split into:
// [0] http:
// [1] www.buildingsmart-tech.org
// [2] ifcXML
// [3] IFC4
// [4] Add2
// [5] IFC4_ADD2_TC1.xsd
// The hosstname will likely change though.
auto it = boost::algorithm::make_split_iterator(value, boost::algorithm::token_finder(boost::algorithm::is_any_of("/ "), boost::algorithm::token_compress_on));
decltype(it) end;
for (int tok = 0; it != end && tok < 3; ++it, ++tok) {
}
if (it != end) {
std::string schema_name(&it->front(), it->size());
boost::to_upper(schema_name);
state->file = new IfcParse::IfcFile(IfcParse::schema_by_name(schema_name));
state->dialect = ifcxml_dialect_ifc4;
}
goto end;
} else if (tagname == "ex:iso_10303_28" && attrname == "xsi:schemaLocation" && boost::starts_with(value, "http://www.iai-tech.org/ifcXML/IFC2x3")) {
state->file = new IfcParse::IfcFile(IfcParse::schema_by_name("IFC2X3"));
state->dialect = ifcxml_dialect_ifc2x3;
goto end;
}
} else {
attrname = (char*)attrs[i];
}
i++;
}
}
if (state->file == nullptr) {
return;
}
{
// ifcXML id attributes are commonly numeric identifiers prefixed with 'i' (as
// XML identifiers need to start with a alphabetic character). This convention
// is not always followed, so a mapping is kept from XML string attribute to
// numeric index into the IfcParse::IfcFile.
std::string id;
// Create an attribute value from an instance. Potentially NULL in case it is a
// forward reference to an instance not yet encountered.
auto instance_to_attribute = [&state](const std::variant<std::string, IfcUtil::IfcBaseClass*>& inst_or_ref, size_t attribute_index, IfcUtil::IfcBaseClass*& inst) {
if (inst_or_ref.index() == 0) {
inst = nullptr;
// This attribute is NULL initially and after parsing the complete
// file populated in a subsequent step.
state->forward_references.push_back(std::make_tuple(inst->as<IfcUtil::IfcBaseEntity>(), attribute_index, std::get<std::string>(inst_or_ref)));
} else {
inst = std::get<IfcUtil::IfcBaseClass*>(inst_or_ref);
inst->set_attribute_value(attribute_index, inst);
}
};
// Create or reference an instance from the file and set attributes based on XML attributes.
auto create_instance = [&state, &attributes](const IfcParse::declaration* decl) {
boost::optional<std::string> id;
std::variant<std::string, IfcUtil::IfcBaseClass*> rv;
for (auto& pair : attributes) {
if (pair.first == "id" || pair.first == "href" || pair.first == "ref") {
id = id = pair.second;
if (pair.first == "href" || pair.first == "ref") {
if (state->idmap.find(pair.second) == state->idmap.end()) {
rv = pair.second;
return rv;
}
rv = state->file->instance_by_id(state->idmap[pair.second]);
return rv;
}
} else if (pair.first == "xsi:type") {
decl = state->file->schema()->declaration_by_name(pair.second)->as_entity();
}
}
auto untyped = IfcEntityInstanceData(in_memory_attribute_storage(decl->as_entity() != nullptr ? decl->as_entity()->attribute_count() : 1));
const IfcParse::entity* entity = decl->as_entity();
if (entity != nullptr) {
for (auto& pair : attributes) {
if (pair.first == "id" || pair.first == "xsi:type" || pair.first == "pos") {
continue;
}
auto idx = entity->attribute_index(pair.first);
if (idx != -1) {
const auto* attr = entity->attribute_by_index(idx);
auto val = parse_attribute_value(attr->type_of_attribute(), pair.second);
if (!val.empty()) {
visit_any([&untyped, idx](auto& v) {
untyped.set_attribute_value(idx, v);
}, val);
}
} else {
Logger::Error("Unknown attribute '" + pair.first + "' on entity '" + entity->name() + "' with value '" + pair.second + "'");
}
}
}
IfcUtil::IfcBaseClass* newinst = state->file->schema()->instantiate(decl, std::move(untyped));
if (state->dialect == ifcxml_dialect_ifc4) {
// In IFC2X3 not added directly because attrs such as GlobalId are in
// subsequent child nodes
newinst = state->file->addEntity(newinst);
if (id) {
state->idmap[*id] = newinst->id();
}
}
rv = newinst;
return rv;
};
stack_node::node_type state_type = stack_node::stack_empty;
if (!state->stack.empty()) {
state_type = state->stack.back().ntype();
}
const std::string tagname_copy = boost::replace_all_copy(tagname, "-wrapper", "");
if (state_type == stack_node::node_select) {
const IfcParse::declaration* decl = state->file->schema()->declaration_by_name(tagname_copy);
// Argument* attr;
IfcUtil::IfcBaseClass* inst;
auto inst_ = create_instance(decl);
instance_to_attribute(inst_, state->stack.back().idx(), inst);
// state->stack.back().inst()->set_attribute_value(state->stack.back().idx(), attr);
state->stack.push_back(stack_node::instance(id, inst));
} else if (state_type == stack_node::node_aggregate) {
const IfcParse::parameter_type* attribute_type = state->stack.back().inst()->declaration().as_entity()->attribute_by_index(state->stack.back().idx())->type_of_attribute();
follow_named(attribute_type);
const IfcParse::parameter_type* element_type = attribute_type->as_aggregation_type()->type_of_element();
follow_named(element_type);
int aggrpos = -1;
/*
auto it = std::find_if(attributes.begin(), attributes.end(), [](const std::pair<std::string, std::string>& p) {
return p.first == "pos";
});
boost::lexical_cast<int>(it->second);
*/
if (element_type->as_simple_type() != nullptr) {
state->stack.push_back(stack_node::aggregate_element(element_type, aggrpos));
} else {
const IfcParse::declaration* decl = nullptr;
try {
decl = state->file->schema()->declaration_by_name(tagname_copy);
} catch (const std::exception& e) {
Logger::Error(e);
}
if (decl != nullptr) {
auto inst_or_ref = create_instance(decl);
IfcUtil::IfcBaseClass* inst;
// Argument* attr;
// @todo
// instance_to_attribute(inst_or_ref, attr, inst);
state->stack.back().aggregate_elements.push_back(boost::any{});
state->stack.push_back(stack_node::instance(id, inst));
}
}
} else if (state_type == stack_node::node_instance) {
const IfcParse::entity* current = state->stack.back().inst()->declaration().as_entity();
if (current == nullptr) {
Logger::Error("'" + state->stack.back().inst()->declaration().name() + "' is not an entity, unable to set attribute '" + tagname + "'");
// We need to push something on the stack. Likely there has been some extra indirection that is not understood.
state->stack.push_back(state->stack.back());
} else {
auto idx = current->attribute_index(tagname);
if (idx == -1) {
auto inverses = current->all_inverse_attributes();
auto found = std::find_if(inverses.begin(), inverses.end(), [&tagname](const IfcParse::inverse_attribute* attr) {
return attr->name() == tagname;
});
if (found == inverses.end()) {
Logger::Error("Unknown attribute " + tagname);
state->stack.push_back(state->stack.back());
} else {
if ((*found)->bound1() == 0 && (*found)->bound2() == 1) {
auto inst_or_ref = create_instance((*found)->entity_reference());
IfcUtil::IfcBaseClass* inst;
instance_to_attribute(inst_or_ref, 0, inst);
if (inst != nullptr) {
int idx = (*found)->entity_reference()->attribute_index(
(*found)->attribute_reference());
inst->set_attribute_value(idx, state->stack.back().inst());
state->stack.push_back(stack_node::instance(id, inst));
} else {
Logger::Error("Unknown attribute " + tagname);
state->stack.push_back(state->stack.back());
}
} else {
state->stack.push_back(stack_node::inverse(state->stack.back().inst(), *found));
}
}
} else {
const IfcParse::parameter_type* attribute_type = current->attribute_by_index(idx)->type_of_attribute();
if (state->dialect == ifcxml_dialect_ifc2x3) {
follow_named(attribute_type);
if (attribute_type->as_aggregation_type() != nullptr) {
state->stack.push_back(stack_node::aggregate(state->stack.back().inst(), idx));
} else {
state->stack.push_back(stack_node::instance_attribute(state->stack.back().inst(), idx));
}
} else {
if (IfcUtil::from_parameter_type(attribute_type) == IfcUtil::Argument_ENTITY_INSTANCE) {
if (const auto* entity = attribute_type->as_named_type()->declared_type()->as_entity()) {
auto inst_or_reference = create_instance(entity);
IfcUtil::IfcBaseClass* inst;
instance_to_attribute(inst_or_reference, idx, inst);
// @todo
state->stack.back().inst();
state->stack.push_back(stack_node::instance(id, std::get<IfcUtil::IfcBaseClass*>(inst_or_reference)));
} else if (attribute_type->as_named_type()->declared_type()->as_select_type() != nullptr) {
// Select types cause an additional indirection, so the current stack node is simply repeated
state->stack.push_back(stack_node::select(state->stack.back().inst(), idx));
}
} else if (attribute_type->as_aggregation_type() != nullptr) {
state->stack.push_back(stack_node::aggregate(state->stack.back().inst(), idx));
}
}
}
}
} else if (state->file != nullptr) {
if (state_type == stack_node::node_header) {
state->stack.push_back(stack_node::header_entry(tagname));
} else if (tagname == "ex:iso_10303_28_header" || tagname == "header") {
state->stack.push_back(stack_node::header());
} else if (tagname == "uos") {
// intentially empty, ignored in end_element() as well
} else {
const IfcParse::declaration* decl = nullptr;
try {
decl = state->file->schema()->declaration_by_name(tagname);
} catch (const std::exception& e) {
Logger::Error(e);
}
if (decl == nullptr) {
goto end;
}
const IfcParse::entity* entity = decl->as_entity();
if ((entity == nullptr) && state_type != stack_node::node_instance_attribute) {
Logger::Error("Not an entity definition " + tagname);
goto end;
}
auto inst_or_ref = create_instance(decl);
IfcUtil::IfcBaseClass* inst;
instance_to_attribute(inst_or_ref, state->stack.back().idx(), inst);
if (state_type == stack_node::node_inverse) {
int idx = state->stack.back().inv_attr()->entity_reference()->attribute_index(
state->stack.back().inv_attr()->attribute_reference());
if (inst != nullptr) {
inst->set_attribute_value(idx, state->stack.back().inst());
} else {
Logger::Error("Internal error, inverse attribute not processed");
}
} else if (state_type == stack_node::node_instance_attribute) {
state->stack.back().inst()->set_attribute_value(state->stack.back().idx(), inst);
}
if (entity == nullptr) {
// Type declaration, immediately populate attr 0
state->stack.push_back(stack_node::instance_attribute(inst, 0));
} else {
state->stack.push_back(stack_node::instance(id, inst));
}
}
}
}
end:
#ifndef NDEBUG
std::cout << std::endl;
#endif
return;
}
IFC_PARSE_API IfcParse::IfcFile* IfcParse::parse_ifcxml(const std::string& filename) {
throw std::runtime_error("IFC-XML import temporarily disabled");
ifcxml_parse_state state;
state.file = nullptr;
state.dialect = ifcxml_dialect_unknown;
xmlSAXHandler handler;
memset(&handler, 0, sizeof(xmlSAXHandler));
handler.startElement = start_element;
handler.endElement = end_element;
handler.characters = process_characters;
xmlSAXUserParseFile(&handler, &state, filename.c_str());
for (const auto& pair : state.forward_references) {
/*
auto it = state.idmap.find(pair.second);
if (it == state.idmap.end()) {
Logger::Error("Instance with id '" + pair.second + "' not encountered");
} else {
pair.first->set(state.file->instance_by_id(it->second));
}
*/
}
if (state.file != nullptr) {
// state.file->parsing_complete() = true;
state.file->build_inverses();
}
return state.file;
}
#endif // WITH_IFCXML