This repository has been archived by the owner on Jul 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
896 lines (824 loc) · 29.5 KB
/
main.cpp
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/**
* Copyright (C) 2016 3D Repo Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ifcparse/IfcParse.h>
#include <ifcparse/IfcFile.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>
/**
* Process a given line from a csv file and returns a vector of string
* @param line the line to process
* @return return a vector of fields, delimited by commas
*/
std::vector<std::string> processLine(const std::string &line)
{
std::vector<std::string> fields;
int currentPos = 0, endPos = 0;
while (endPos != std::string::npos)
{
bool hasQuotes = line[currentPos] == '"';
if(hasQuotes) currentPos++;
endPos = line.find_first_of(hasQuotes? '"' : ',', currentPos);
auto substring = line.substr(currentPos, endPos - currentPos);
if(!substring.empty())fields.push_back(substring);
currentPos = hasQuotes ? endPos + 2 : endPos + 1;
}
return fields;
}
/**
* Process the CSV file and gather the patterns to match
* @param csvFile location of the file
* @return returns a map of {Metadata Field, {Metadata Value, Material name}}
*/
static std::map<std::string, std::map<std::string, std::string>> processCSVFile(const std::string &csvFile)
{
std::ifstream fs(csvFile);
std::string line;
std::map<std::string, std::map<std::string, std::string>> dataToMat;
if (std::getline(fs, line))
{
//skipping the first line which should be headers
while (std::getline(fs, line))
{
std::stringstream ss(line);
std::string matName, metaFieldName;
std::vector<std::string> metaFieldData;
auto fields = processLine(line);
if (fields.size() < 3)
{
//this line doesn't have enough fields to be valid
std::cerr << "Warning: Insufficient amount of entries on line: [" << line << "]. Skipping..." << std::endl;
continue;
}
for (int count = 0; count < fields.size(); ++count)
{
auto item = fields[count];
//1 is material name, 2 is field to match, 3 onwards are the matching names
switch (count)
{
case 0:
//Material name
matName = item;
break;
case 1:
metaFieldName = item;
if (!metaFieldName.empty() && dataToMat.find(metaFieldName) == dataToMat.end())
{
dataToMat[metaFieldName] = std::map<std::string, std::string>();
}
break;
default:
if (!metaFieldName.empty() && !item.empty())
{
if (dataToMat[metaFieldName].find(item) == dataToMat[metaFieldName].end())
dataToMat[metaFieldName][item] = matName;
else
std::cerr << "Warning: " << item << " is mapped to multiple material" << std::endl;
}
}
}
}
}
return dataToMat;
}
/**
* Get a mapping of Material name to it's IfcRelAssociatesMaterial and IfcSurfaceStyle
* @param ifcfile file to examin
* @return return a map of material name to it's IFC entities
*/
static std::map < std::string, std::pair<IfcSchema::IfcRelAssociatesMaterial*, IfcSchema::IfcSurfaceStyle*> >
getRelMatMap(IfcParse::IfcFile &ifcfile)
{
std::map < std::string, std::pair<IfcSchema::IfcRelAssociatesMaterial*, IfcSchema::IfcSurfaceStyle*> > matToIfcRelMat;
auto materialEntities = ifcfile.entitiesByType("IfcMaterial");
for (const auto &en : *materialEntities)
{
auto mat = dynamic_cast<const IfcSchema::IfcMaterial*>(en);
if (mat)
{
auto ref = ifcfile.entitiesByReference(mat->entity->id());
for (auto &r : *ref)
{
if (r->type() == IfcSchema::Type::Enum::IfcRelAssociatesMaterial)
{
matToIfcRelMat[mat->Name()] = { dynamic_cast<IfcSchema::IfcRelAssociatesMaterial*>(r), nullptr };
}
}
}
}
auto surfaceItems = ifcfile.entitiesByType("IfcSurfaceStyle");
for (auto &en : *surfaceItems)
{
auto mat = dynamic_cast<IfcSchema::IfcSurfaceStyle*>(en);
if (mat)
{
if (matToIfcRelMat.find(mat->Name()) != matToIfcRelMat.end())
matToIfcRelMat[mat->Name()].second = mat;
else
{
//Should already have an entry.
//std::cerr << "Failed to find Material entry for " << mat->Name() << std::endl;
matToIfcRelMat[mat->Name()] = { nullptr, mat};
}
}
}
return matToIfcRelMat;
}
/**
* Clones a IfcGeometricRepresentationItem
* @param org the item to clone
* @returns a new item identitical to org, without the Entity reference
*/
static IfcSchema::IfcGeometricRepresentationItem*
cloneGeoItem
(const IfcSchema::IfcGeometricRepresentationItem* org)
{
IfcSchema::IfcGeometricRepresentationItem* res = nullptr;
//FIXME: We have to do this as IFCOpenShell has no facility to clone an item...
switch (org->type())
{
case IfcSchema::Type::Enum::IfcGeometricSet:
{
auto ptr = dynamic_cast<const IfcSchema::IfcGeometricSet*>(org);
res = new IfcSchema::IfcGeometricSet(ptr->Elements());
}
break;
case IfcSchema::Type::Enum::IfcHalfSpaceSolid:
{
auto ptr = dynamic_cast<const IfcSchema::IfcHalfSpaceSolid*>(org);
res = new IfcSchema::IfcHalfSpaceSolid(ptr->BaseSurface(), ptr->AgreementFlag());
}
break;
case IfcSchema::Type::Enum::IfcLightSource:
{
auto ptr = dynamic_cast<const IfcSchema::IfcLightSource*>(org);
res = new IfcSchema::IfcLightSource(ptr->Name(), ptr->LightColour(), ptr->AmbientIntensity(), ptr->Intensity());
}
break;
case IfcSchema::Type::Enum::IfcOneDirectionRepeatFactor:
{
auto ptr = dynamic_cast<const IfcSchema::IfcOneDirectionRepeatFactor*>(org);
res = new IfcSchema::IfcOneDirectionRepeatFactor(ptr->RepeatFactor());
}
break;
case IfcSchema::Type::Enum::IfcPlacement:
{
auto ptr = dynamic_cast<const IfcSchema::IfcPlacement*>(org);
res = new IfcSchema::IfcPlacement(ptr->Location());
}
break;
case IfcSchema::Type::Enum::IfcPlanarExtent:
{
auto ptr = dynamic_cast<const IfcSchema::IfcPlanarExtent*>(org);
res = new IfcSchema::IfcPlanarExtent(ptr->SizeInX(), ptr->SizeInY());
}
break;
case IfcSchema::Type::Enum::IfcPointOnCurve:
{
auto ptr = dynamic_cast<const IfcSchema::IfcPointOnCurve*>(org);
res = new IfcSchema::IfcPointOnCurve(ptr->BasisCurve(), ptr->PointParameter());
}
break;
case IfcSchema::Type::Enum::IfcPointOnSurface:
{
auto ptr = dynamic_cast<const IfcSchema::IfcPointOnSurface*>(org);
res = new IfcSchema::IfcPointOnSurface(ptr->BasisSurface(), ptr->PointParameterU(), ptr->PointParameterV());
}
break;
case IfcSchema::Type::Enum::IfcCartesianPoint:
{
auto ptr = dynamic_cast<const IfcSchema::IfcCartesianPoint*>(org);
res = new IfcSchema::IfcCartesianPoint(ptr->Coordinates());
}
break;
case IfcSchema::Type::Enum::IfcSectionedSpine:
{
auto ptr = dynamic_cast<const IfcSchema::IfcSectionedSpine*>(org);
res = new IfcSchema::IfcSectionedSpine(ptr->SpineCurve(), ptr->CrossSections(), ptr->CrossSectionPositions());
}
break;
case IfcSchema::Type::Enum::IfcShellBasedSurfaceModel:
{
auto ptr = dynamic_cast<const IfcSchema::IfcShellBasedSurfaceModel*>(org);
res = new IfcSchema::IfcShellBasedSurfaceModel(ptr->SbsmBoundary());
}
break;
case IfcSchema::Type::Enum::IfcSweptDiskSolid:
{
auto ptr = dynamic_cast<const IfcSchema::IfcSweptDiskSolid*>(org);
res = new IfcSchema::IfcSweptDiskSolid(ptr->Directrix(), ptr->Radius(), ptr->InnerRadius(), ptr->StartParam(), ptr->EndParam());
}
break;
case IfcSchema::Type::Enum::IfcSweptAreaSolid:
{
auto ptr = dynamic_cast<const IfcSchema::IfcSweptAreaSolid*>(org);
res = new IfcSchema::IfcSweptAreaSolid(ptr->SweptArea(), ptr->Position());
}
break;
case IfcSchema::Type::Enum::IfcCsgSolid:
{
auto ptr = dynamic_cast<const IfcSchema::IfcCsgSolid*>(org);
res = new IfcSchema::IfcCsgSolid(ptr->TreeRootExpression());
}
break;
case IfcSchema::Type::Enum::IfcManifoldSolidBrep:
{
auto ptr = dynamic_cast<const IfcSchema::IfcManifoldSolidBrep*>(org);
res = new IfcSchema::IfcManifoldSolidBrep(ptr->Outer());
}
break;
case IfcSchema::Type::Enum::IfcSweptSurface:
{
auto ptr = dynamic_cast<const IfcSchema::IfcSweptSurface*>(org);
res = new IfcSchema::IfcSweptSurface(ptr->SweptCurve(), ptr->Position());
}
break;
case IfcSchema::Type::Enum::IfcCurveBoundedPlane:
{
auto ptr = dynamic_cast<const IfcSchema::IfcCurveBoundedPlane*>(org);
res = new IfcSchema::IfcCurveBoundedPlane(ptr->BasisSurface(), ptr->OuterBoundary(), ptr->InnerBoundaries());
}
break;
case IfcSchema::Type::Enum::IfcRectangularTrimmedSurface:
{
auto ptr = dynamic_cast<const IfcSchema::IfcRectangularTrimmedSurface*>(org);
res = new IfcSchema::IfcRectangularTrimmedSurface(ptr->BasisSurface(), ptr->U1(), ptr->V1(), ptr->U2(), ptr->V2(), ptr->Usense(), ptr->Vsense());
}
break;
case IfcSchema::Type::Enum::IfcElementarySurface:
{
auto ptr = dynamic_cast<const IfcSchema::IfcElementarySurface*>(org);
res = new IfcSchema::IfcElementarySurface(ptr->Position());
}
break;
case IfcSchema::Type::Enum::IfcTextLiteral:
{
auto ptr = dynamic_cast<const IfcSchema::IfcTextLiteral*>(org);
res = new IfcSchema::IfcTextLiteral(ptr->Literal(), ptr->Placement(), ptr->Path());
}
break;
case IfcSchema::Type::Enum::IfcVector:
{
auto ptr = dynamic_cast<const IfcSchema::IfcVector*>(org);
res = new IfcSchema::IfcVector(ptr->Orientation(), ptr->Magnitude());
}
break;
case IfcSchema::Type::Enum::IfcAnnotationFillArea:
{
auto ptr = dynamic_cast<const IfcSchema::IfcAnnotationFillArea*>(org);
res = new IfcSchema::IfcAnnotationFillArea(ptr->OuterBoundary(), ptr->InnerBoundaries());
}
break;
case IfcSchema::Type::Enum::IfcAnnotationSurface:
{
auto ptr = dynamic_cast<const IfcSchema::IfcAnnotationSurface*>(org);
res = new IfcSchema::IfcAnnotationSurface(ptr->Item(), ptr->TextureCoordinates());
}
break;
case IfcSchema::Type::Enum::IfcBooleanResult:
{
auto ptr = dynamic_cast<const IfcSchema::IfcBooleanResult*>(org);
res = new IfcSchema::IfcBooleanResult(ptr->Operator(), ptr->FirstOperand(), ptr->SecondOperand());
}
break;
case IfcSchema::Type::Enum::IfcBoundingBox:
{
auto ptr = dynamic_cast<const IfcSchema::IfcBoundingBox*>(org);
res = new IfcSchema::IfcBoundingBox(ptr->Corner(), ptr->XDim(), ptr->YDim(), ptr->ZDim());
}
break;
case IfcSchema::Type::Enum::IfcCartesianTransformationOperator:
{
auto ptr = dynamic_cast<const IfcSchema::IfcCartesianTransformationOperator*>(org);
res = new IfcSchema::IfcCartesianTransformationOperator(ptr->Axis1(), ptr->Axis2(), ptr->LocalOrigin(), ptr->Scale());
}
break;
case IfcSchema::Type::Enum::IfcCompositeCurveSegment:
{
auto ptr = dynamic_cast<const IfcSchema::IfcCompositeCurveSegment*>(org);
res = new IfcSchema::IfcCompositeCurveSegment(ptr->Transition(), ptr->SameSense(), ptr->ParentCurve());
}
break;
case IfcSchema::Type::Enum::IfcCsgPrimitive3D:
{
auto ptr = dynamic_cast<const IfcSchema::IfcCsgPrimitive3D*>(org);
res = new IfcSchema::IfcCsgPrimitive3D(ptr->Position());
}
break;
case IfcSchema::Type::Enum::IfcLine:
{
auto ptr = dynamic_cast<const IfcSchema::IfcLine*>(org);
res = new IfcSchema::IfcLine(ptr->Pnt(), ptr->Dir());
}
break;
case IfcSchema::Type::Enum::IfcOffsetCurve2D:
{
auto ptr = dynamic_cast<const IfcSchema::IfcOffsetCurve2D*>(org);
res = new IfcSchema::IfcOffsetCurve2D(ptr->BasisCurve(), ptr->Distance(), ptr->SelfIntersect());
}
break;
case IfcSchema::Type::Enum::IfcOffsetCurve3D:
{
auto ptr = dynamic_cast<const IfcSchema::IfcOffsetCurve3D*>(org);
res = new IfcSchema::IfcOffsetCurve3D(ptr->BasisCurve(), ptr->Distance(), ptr->SelfIntersect(), ptr->RefDirection());
}
break;
case IfcSchema::Type::Enum::IfcCompositeCurve:
{
auto ptr = dynamic_cast<const IfcSchema::IfcCompositeCurve*>(org);
res = new IfcSchema::IfcCompositeCurve(ptr->Segments(), ptr->SelfIntersect());
}
break;
case IfcSchema::Type::Enum::IfcPolyline:
{
auto ptr = dynamic_cast<const IfcSchema::IfcPolyline*>(org);
res = new IfcSchema::IfcPolyline(ptr->Points());
}
break;
case IfcSchema::Type::Enum::IfcTrimmedCurve:
{
auto ptr = dynamic_cast<const IfcSchema::IfcTrimmedCurve*>(org);
res = new IfcSchema::IfcTrimmedCurve(ptr->BasisCurve(), ptr->Trim1(), ptr->Trim2(), ptr->SenseAgreement(), ptr->MasterRepresentation());
}
break;
case IfcSchema::Type::Enum::IfcBSplineCurve:
{
auto ptr = dynamic_cast<const IfcSchema::IfcBSplineCurve*>(org);
res = new IfcSchema::IfcBSplineCurve(ptr->Degree(), ptr->ControlPointsList(), ptr->CurveForm(), ptr->ClosedCurve(), ptr->SelfIntersect());
}
break;
case IfcSchema::Type::Enum::IfcDefinedSymbol:
{
auto ptr = dynamic_cast<const IfcSchema::IfcDefinedSymbol*>(org);
res = new IfcSchema::IfcDefinedSymbol(ptr->Definition(), ptr->Target());
}
break;
case IfcSchema::Type::Enum::IfcConic:
{
auto ptr = dynamic_cast<const IfcSchema::IfcConic*>(org);
res = new IfcSchema::IfcConic(ptr->Position());
}
break;
case IfcSchema::Type::Enum::IfcDirection:
{
auto ptr = dynamic_cast<const IfcSchema::IfcDirection*>(org);
res = new IfcSchema::IfcDirection(ptr->DirectionRatios());
}
break;
case IfcSchema::Type::Enum::IfcDraughtingCallout:
{
auto ptr = dynamic_cast<const IfcSchema::IfcDraughtingCallout*>(org);
res = new IfcSchema::IfcDraughtingCallout(ptr->Contents());
}
break;
case IfcSchema::Type::Enum::IfcFaceBasedSurfaceModel:
{
auto ptr = dynamic_cast<const IfcSchema::IfcFaceBasedSurfaceModel*>(org);
res = new IfcSchema::IfcFaceBasedSurfaceModel(ptr->FbsmFaces());
}
break;
case IfcSchema::Type::Enum::IfcFillAreaStyleHatching:
{
auto ptr = dynamic_cast<const IfcSchema::IfcFillAreaStyleHatching*>(org);
res = new IfcSchema::IfcFillAreaStyleHatching(ptr->HatchLineAppearance(), ptr-> StartOfNextHatchLine(), ptr->PointOfReferenceHatchLine(), ptr->PatternStart(), ptr->HatchLineAngle());
}
break;
case IfcSchema::Type::Enum::IfcFillAreaStyleTileSymbolWithStyle:
{
auto ptr = dynamic_cast<const IfcSchema::IfcFillAreaStyleTileSymbolWithStyle*>(org);
res = new IfcSchema::IfcFillAreaStyleTileSymbolWithStyle(ptr->Symbol());
}
break;
case IfcSchema::Type::Enum::IfcFillAreaStyleTiles:
{
auto ptr = dynamic_cast<const IfcSchema::IfcFillAreaStyleTiles*>(org);
res = new IfcSchema::IfcFillAreaStyleTiles(ptr->TilingPattern(), ptr->Tiles(), ptr->TilingScale());
}
break;
case IfcSchema::Type::Enum::IfcFacetedBrep:
{
auto ptr = dynamic_cast<const IfcSchema::IfcFacetedBrep*>(org);
res = new IfcSchema::IfcFacetedBrep(ptr->Outer());
}
break;
case IfcSchema::Type::Enum::IfcFacetedBrepWithVoids:
{
auto ptr = dynamic_cast<const IfcSchema::IfcFacetedBrepWithVoids*>(org);
res = new IfcSchema::IfcFacetedBrepWithVoids(ptr->Outer(), ptr->Voids());
}
break;
case IfcSchema::Type::Enum::IfcExtrudedAreaSolid:
{
auto ptr = dynamic_cast<const IfcSchema::IfcExtrudedAreaSolid*>(org);
res = new IfcSchema::IfcExtrudedAreaSolid(ptr->SweptArea(), ptr->Position(), ptr-> ExtrudedDirection(), ptr->Depth());
}
break;
case IfcSchema::Type::Enum::IfcRevolvedAreaSolid:
{
auto ptr = dynamic_cast<const IfcSchema::IfcRevolvedAreaSolid*>(org);
res = new IfcSchema::IfcRevolvedAreaSolid(ptr->SweptArea(), ptr->Position(), ptr->Axis(), ptr->Angle());
}
break;
case IfcSchema::Type::Enum::IfcSurfaceCurveSweptAreaSolid:
{
auto ptr = dynamic_cast<const IfcSchema::IfcSurfaceCurveSweptAreaSolid*>(org);
res = new IfcSchema::IfcSurfaceCurveSweptAreaSolid(ptr->SweptArea(), ptr->Position(), ptr->Directrix(), ptr->StartParam(), ptr->EndParam(), ptr->ReferenceSurface());
}
break;
default:
std::cerr << "Unrecognised type for this entity: " << org->entity->toString(); exit(-1);
}
return res;
}
/**
* Extract IfcGeometricRepresentationItem items from IfcRepresentationItem
* This can be a recursive function if the Representation Item is actually a mappedItem
* User should pass in a nullptr for newItem param, and check if the pointer has changed
* after the function is called. if it has been changed, the user needs to updates the references to
* the new entity
* @param repItem the Representation item to extract from
* @param geoItems list of RepresentationItems that has been found from previous runs
* @param geoList A global list tracker to track the IFC Representation Items seen
* @param geoReps A global list tracker to track the IFC Representation Map seen
* @param geoList A global list tracker to track the IFC Representation seen
* @param ifcfile the current IFC File handler
* @param newItem returns a pointer to a new instance of the item if a new instance is instantiated
* @return returns a set of IfcGeometricRepresentationItem owned by this entity
*/
std::set<IfcSchema::IfcGeometricRepresentationItem*>
extractGeoRepItems(
IfcSchema::IfcRepresentationItem *repItem,
std::set<IfcSchema::IfcRepresentationItem*> &geoItems,
std::set<IfcSchema::IfcRepresentation*> &geoReps,
std::set<IfcSchema::IfcRepresentationMap*> &seenMaps,
IfcParse::IfcFile &ifcFile,
IfcSchema::IfcRepresentationItem* &newItem
)
{
std::set<IfcSchema::IfcGeometricRepresentationItem*> items;
if (auto geoItem = dynamic_cast<IfcSchema::IfcGeometricRepresentationItem*>(repItem))
{
if (geoItems.find(geoItem) != geoItems.end())
{
//This item already exists, clone it to avoid material clashing
newItem = cloneGeoItem(geoItem);
ifcFile.addEntity(newItem);
geoItem = (IfcSchema::IfcGeometricRepresentationItem*)newItem;
}
items.insert(geoItem);
}
else
{
//This must be a mapped item?
auto mappedItem = dynamic_cast<IfcSchema::IfcMappedItem*>(repItem);
//ensure we haven't seen this mapped Item before
if (geoItems.find(mappedItem) != geoItems.end())
{
//We've seen this mapped item before. clone it to avoid material clashing
mappedItem = new IfcSchema::IfcMappedItem(mappedItem->MappingSource(), mappedItem->MappingTarget());
ifcFile.addEntity(mappedItem);
newItem = mappedItem;
}
geoItems.insert(mappedItem);
//ensure we haven't seen this representation map before
auto repMap = mappedItem->MappingSource();
if (seenMaps.find(repMap) != seenMaps.end())
{
//We've seen this mapped item before. clone it to avoid material clashing
repMap = new IfcSchema::IfcRepresentationMap(repMap->MappingOrigin(), repMap->MappedRepresentation());
ifcFile.addEntity(repMap);
mappedItem->setMappingSource(repMap);
}
seenMaps.insert(repMap);
auto rep = mappedItem->MappingSource()->MappedRepresentation();
if (geoReps.find(rep) != geoReps.end())
{
rep = new IfcSchema::IfcRepresentation(rep->ContextOfItems(), rep->RepresentationIdentifier(), rep->RepresentationType(), rep->Items());
mappedItem->MappingSource()->setMappedRepresentation(rep);
ifcFile.addEntity(rep);
}
geoReps.insert(rep);
if (rep->Items()->size())
{
auto size = rep->Items()->size();
auto itemsr = rep->Items();
std::vector<std::pair<IfcSchema::IfcRepresentationItem *, IfcSchema::IfcRepresentationItem *>> changedChildren;
for (auto &subRepItem : *itemsr)
{
IfcSchema::IfcRepresentationItem * newPtr = nullptr;
auto childRes = extractGeoRepItems(subRepItem, geoItems, geoReps, seenMaps, ifcFile, newPtr);
items.insert(childRes.begin(), childRes.end());
if (newPtr)
{
changedChildren.push_back({ subRepItem, newPtr });
}
}
//New instances of these children were created, reflect them on the mapped item
for (auto &ptr : changedChildren)
{
itemsr->remove(ptr.first);
itemsr->push(ptr.second);
}
rep->setItems(itemsr);
}
}
return items;
}
/**
* Given a IfcProduct, find all of its IfcGeometricRepresentationItems
* Clone instanced entities if found in the process.
* @param relProd IfcProduct in question
* @param geoItems list of seen IfcRepresentationItem from previous processes
* @param geoReps list of seen IfcRepresentation from previous processes
* @param seenMaps list of seen IfcRepresentationMap from previous processes
* @param ifcFile ifcFile with the information
* @return returns a set of IfcGeometricRepresentationItems associated with the product
*/
static std::set<IfcSchema::IfcGeometricRepresentationItem*>
findGeoRepItems(
const IfcSchema::IfcProduct *relProd,
std::set<IfcSchema::IfcRepresentationItem*> &geoItems,
std::set<IfcSchema::IfcRepresentation*> &geoReps,
std::set<IfcSchema::IfcRepresentationMap*> &seenMaps,
IfcParse::IfcFile &ifcFile)
{
std::set<IfcSchema::IfcGeometricRepresentationItem*> res;
auto shapRep = dynamic_cast<const IfcSchema::IfcProductRepresentation*>(relProd->Representation());
if (shapRep)
{
auto reps = shapRep->Representations();
for (const auto rep : *reps)
{
auto shape = dynamic_cast<const IfcSchema::IfcShapeRepresentation*>(rep);
auto items = shape->Items();
std::vector<std::pair<IfcSchema::IfcRepresentationItem *, IfcSchema::IfcRepresentationItem *>> changedChildren;
for (auto item : *items)
{
IfcSchema::IfcRepresentationItem * newPtr = nullptr;
auto geoRepItems = extractGeoRepItems(item, geoItems, geoReps, seenMaps, ifcFile, newPtr);
if (newPtr)
{
changedChildren.push_back({ item, newPtr });
}
res.insert(geoRepItems.begin(), geoRepItems.end());
}
//New instances of these children were created, reflect them on the mapped item
for (auto &ptr : changedChildren)
{
items->remove(ptr.first);
items->push(ptr.second);
}
rep->setItems(items);
}
}
return res;
}
/**
* Generate a map of Representation Item to it's IfcStyled Item
* @param ifcfile ifcFile with all the information
* @return returns a map of representation item to styled Item
*/
std::map<IfcSchema::IfcRepresentationItem*, IfcSchema::IfcStyledItem*>
getStyleItemForGeoReps(IfcParse::IfcFile &ifcfile)
{
std::map<IfcSchema::IfcRepresentationItem*, IfcSchema::IfcStyledItem*> geoRepToStyle;
auto styledItem = ifcfile.entitiesByType("IfcStyledItem");
for (const auto &style : *styledItem)
{
auto s = dynamic_cast<IfcSchema::IfcStyledItem*>(style);
if (s->hasItem())
geoRepToStyle[s->Item()] = s;
}
return geoRepToStyle;
}
/**
* Given a metadata ID, update all its references with the given material
* @param ifcfile the current IFC File handler
* @param material IFCRelAssociatesMaterial tag that holds all the relationship to the material in question
* @param surfaceStyle IFCSurfaceStyle that has the details of this material
* @param metaId the IFC ID of the metadata
* @param geoList A global list tracker to track the IFC Representation Items seen
* @param geoReps A global list tracker to track the IFC Representation Map seen
* @param geoList A global list tracker to track the IFC Representation seen
* @param geoRepToStyle A mapping of representation items to its styled Item
* @param newEntities A list to keep track of new entities that needs to be added into the IFC file after
*/
void updateMaterial(
IfcParse::IfcFile &ifcfile,
IfcSchema::IfcRelAssociatesMaterial *material,
IfcSchema::IfcSurfaceStyle *surfaceStyle,
const int &metaId,
std::set<IfcSchema::IfcRepresentationItem*> &geoList,
std::set<IfcSchema::IfcRepresentationMap*> &seenMaps,
std::set<IfcSchema::IfcRepresentation*> &geoReps,
std::map<IfcSchema::IfcRepresentationItem*, IfcSchema::IfcStyledItem*> &geoRepToStyle,
IfcEntityList::ptr &newEntities
)
{
IfcTemplatedEntityList< IfcSchema::IfcRoot >::ptr relatingObjects;
std::set<IfcSchema::IfcGeometricRepresentationItem*> geoItems;
std::set<IfcSchema::IfcRoot*> objs;
if (material)
{
relatingObjects = material->RelatedObjects();
objs.insert(relatingObjects->begin(), relatingObjects->end());
}
auto refs = ifcfile.entitiesByReference(metaId);
if (refs)
{
for (const auto & r : *refs)
{
auto refs2 = ifcfile.entitiesByReference(r->entity->id());
if (!refs2)continue;
for (const auto & r2 : *refs2)
{
if (r2->type() == IfcSchema::Type::Enum::IfcRelDefinesByProperties)
{
auto relProp = dynamic_cast<const IfcSchema::IfcRelDefinesByProperties*>(r2);
auto relObjs = relProp->RelatedObjects();
for (const auto & r3 : *relObjs)
{
auto relProd = dynamic_cast<const IfcSchema::IfcProduct*>(r3);
std::set<IfcSchema::IfcGeometricRepresentationItem*> pGeoItems = findGeoRepItems(relProd, geoList, geoReps, seenMaps, ifcfile);
geoItems.insert(pGeoItems.begin(), pGeoItems.end());
geoList.insert(pGeoItems.begin(), pGeoItems.end());
if (objs.find(r3) == objs.end())
{
relatingObjects->push(r3);
}
}
}
else
{
std::cerr << "Unexpected type : " << r2->type() << std::endl;
std::cout << r2->entity->toString() << std::endl;
}
}
}
}
//Add objects to Material link
if (material)
material->setRelatedObjects(relatingObjects);
//Create surface items that references the geo items
if (surfaceStyle)
{
IfcEntityList::ptr surfaceList(new IfcEntityList);
surfaceList->push(surfaceStyle);
for (auto &geoItem : geoItems)
{
if (geoRepToStyle.find(geoItem) != geoRepToStyle.end())
{
geoRepToStyle[geoItem]->setItem(nullptr);
//It already has a surface item. does that mean it already has a material?
}
//No surface style
IfcTemplatedEntityList< IfcSchema::IfcPresentationStyleAssignment >::ptr list(new IfcTemplatedEntityList< IfcSchema::IfcPresentationStyleAssignment >);
auto styleAssignment = new IfcSchema::IfcPresentationStyleAssignment(surfaceList);
list->push(styleAssignment);
IfcSchema::IfcStyledItem* newStyleItem = new IfcSchema::IfcStyledItem(geoItem, list, std::string());
newEntities->push(newStyleItem);
}
}
}
/**
* Update the IFC with materials depicted from the given matMap
* This function will update the IFC and writes the results in outputFile
* @param inputFile input IFC file
* @param outputFile output IFC file
* @param matMap a map of {Metadata Field name , {Metadata Value, Material Name}}
*/
static void updateFile(const std::string &inputFile, const std::string &outputfile,
const std::map<std::string, std::map<std::string, std::string>> &matMap)
{
IfcParse::IfcFile ifcfile;
if (!ifcfile.Init(inputFile))
{
std::cerr << "Failed initialising " << inputFile << std::endl;
return;
}
auto geoRepToStyle = getStyleItemForGeoReps(ifcfile);
auto matToIfcRelMat = getRelMatMap(ifcfile);
//Entity tracker
std::set<IfcSchema::IfcRepresentationItem*> geoList;
std::set<IfcSchema::IfcRepresentationMap*> seenMaps;
std::set<IfcSchema::IfcRepresentation*> geoReps;
auto metadataEntities = ifcfile.entitiesByType("IfcPropertySingleValue");
IfcEntityList::ptr newEntities(new IfcEntityList());
//Loop through all Metadata entities to find matching metadata field
for (const auto &meta : *metadataEntities)
{
auto singleProp = dynamic_cast<const IfcSchema::IfcPropertySingleValue*>(meta);
if (singleProp->hasNominalValue())
{
auto it = matMap.find(singleProp->Name());
if (it != matMap.end())
{
auto &valueMap = it->second;
auto valueIt = valueMap.find(singleProp->NominalValue()->valueAsString());
if (valueIt != valueMap.end())
{
auto matIt = matToIfcRelMat.find(valueIt->second);
if (matIt != matToIfcRelMat.end())
{
//This Metadata Field/Value has a new material. Find all references and update them
updateMaterial(ifcfile, matIt->second.first, matIt->second.second, singleProp->entity->id(), geoList, seenMaps, geoReps, geoRepToStyle, newEntities);
}
else
{
std::cerr << "Failed to find material " << valueIt->second << std::endl;
}
}
}
}
else
{
std::cout << "no nominal value: " << meta->entity->toString() << std::endl;
}
}
//Add all the new entities into the ifc
ifcfile.addEntities(newEntities);
std::ofstream os(outputfile);
os << ifcfile;
os.close();
}
/**
* Check if file exists
* @param file location to check
* @return returns true if it exists, false otherwise
*/
static bool fileExists(const std::string &file)
{
std::ifstream ifs(file);
bool res = ifs.good();
ifs.close();
return res;
}
/**
* Process the IFC file based on the conditions within the CSV File
* the function will read from inputFile and csvFile and writes the resulting
* IFC file in outputFile
* @params inputFile location of input IFC file
* @params where to write the output file
* @params csvFile location of the CSV file
*/
static void processIFC(const std::string &inputFile, const std::string &outputFile, const std::string &csvFile)
{
auto matMap = processCSVFile(csvFile);
if (matMap.size())
{
for (const auto &item : matMap)
{
std::cout << item.first << ":" << std::endl;
for (const auto &pair : item.second)
{
std::cout << "\t" << pair.first << " : " << pair.second << std::endl;
}
}
updateFile(inputFile, outputFile, matMap);
}
else
{
std::cerr << "Cannot find mappings from csv file!" << std::endl;
}
}
int main(int argc, char* argv[])
{
if (argc < 4)
{
std::cerr << "Usage: " << argv[0] << " <input file> <output file> <csv file>" << std::endl;
return EXIT_FAILURE;
}
std::string inputFile = argv[1];
std::string outputFile = argv[2];
std::string csvFile = argv[3];
//Check input file exists
if (!fileExists(inputFile))
{
std::cerr << "Error: Cannot find file " << inputFile << std::endl;
return EXIT_FAILURE;
}
//Check csv file exists
if (!fileExists(csvFile))
{
std::cerr << "Error: Cannot find file " << csvFile << std::endl;
return EXIT_FAILURE;
}
processIFC(inputFile, outputFile, csvFile);
return EXIT_SUCCESS;
}