1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: CollectionAPI_Field.cpp
4 // Created: 16 Nov 2016
5 // Author: Mikhail Ponikarov
7 #include "CollectionAPI_Field.h"
9 #include <ModelHighAPI_Dumper.h>
10 #include <ModelHighAPI_Integer.h>
11 #include <ModelHighAPI_Selection.h>
12 #include <ModelHighAPI_Tools.h>
13 #include <ModelAPI_AttributeTables.h>
14 #include <ModelAPI_AttributeStringArray.h>
16 #include <algorithm> // for std::transform
18 //=================================================================================================
19 CollectionAPI_Field::CollectionAPI_Field(const std::shared_ptr<ModelAPI_Feature>& theFeature)
20 : ModelHighAPI_Interface(theFeature)
25 //=================================================================================================
26 CollectionAPI_Field::~CollectionAPI_Field()
30 //=================================================================================================
31 void CollectionAPI_Field::setSelection(const std::list<ModelHighAPI_Selection>& theFieldList)
33 fillAttribute(theFieldList, myselection);
37 //=================================================================================================
38 void CollectionAPI_Field::setComponentsNames(const std::list<std::string>& theNames)
40 fillAttribute(theNames, mycomponentsNames);
44 //=================================================================================================
45 void CollectionAPI_Field::setValuesType(const std::string& theType)
47 myvalues->setType(valueTypeByStr(theType));
51 //=================================================================================================
52 void CollectionAPI_Field::setStepsNum(const ModelHighAPI_Integer& theSteps)
54 mystamps->setSize(theSteps.intValue());
58 //=================================================================================================
59 void CollectionAPI_Field::setStamps(const std::list<ModelHighAPI_Integer>& theStamps)
61 fillAttribute(theStamps, mystamps);
65 //=================================================================================================
66 #define addStepImplementation(type, fieldType, type2, fieldType2, type3, fieldType3) \
67 void CollectionAPI_Field::addStep(const ModelHighAPI_Integer& theStepNum, \
68 const ModelHighAPI_Integer& theStamp, \
69 const std::list<std::list<type> >& theComponents) \
71 mystamps->setValue(theStepNum.intValue(), theStamp.intValue()); \
73 std::list<std::list<type> >::const_iterator \
74 aRowsIter = theComponents.begin(); \
75 for(; aRowsIter != theComponents.end(); aRowsIter++, aRowIndex++) { \
77 std::list<type>::const_iterator aColIter = aRowsIter->begin(); \
78 for(; aColIter != aRowsIter->end(); aColIter++, aColIndex++) { \
79 ModelAPI_AttributeTables::Value aVal; \
80 aVal.fieldType = *aColIter; \
81 aVal.fieldType2 = type2(*aColIter); \
82 aVal.fieldType3 = type3(*aColIter); \
83 myvalues->setValue(aVal, aRowIndex, aColIndex, theStepNum.intValue()); \
89 addStepImplementation(double, myDouble, int, myInt, bool, myBool);
90 addStepImplementation(int, myInt, double, myDouble, bool, myBool);
91 addStepImplementation(bool, myBool, int, myInt, double, myDouble);
92 addStepImplementation(std::string, myStr, std::string, myStr, std::string, myStr);
94 //=================================================================================================
95 void CollectionAPI_Field::dump(ModelHighAPI_Dumper& theDumper) const
97 FeaturePtr aBase = feature();
98 const std::string& aDocName = theDumper.name(aBase->document());
100 theDumper<<aBase<<" = model.addField("<<aDocName<<", "<<myvalues->tables()<<", \""
101 <<strByValueType(myvalues->type())<<"\", "<<mycomponentsNames->size()<<", ";
102 theDumper<<mycomponentsNames<<", ";
103 theDumper<<myselection<<")"<<std::endl;
104 // set values step by step
105 for(int aStep = 0; aStep < myvalues->tables(); aStep++) {
106 theDumper<<aBase<<".addStep("<<aStep<<", "<<mystamps->value(aStep)<<", [";
107 for(int aRow = 0; aRow < myvalues->rows(); aRow++) {
111 for(int aCol = 0; aCol < myvalues->columns(); aCol++) {
114 switch(myvalues->type()) {
115 case ModelAPI_AttributeTables::BOOLEAN:
116 theDumper<<myvalues->value(aRow, aCol, aStep).myBool;
118 case ModelAPI_AttributeTables::INTEGER:
119 theDumper<<myvalues->value(aRow, aCol, aStep).myInt;
121 case ModelAPI_AttributeTables::DOUBLE:
122 theDumper<<myvalues->value(aRow, aCol, aStep).myDouble;
124 case ModelAPI_AttributeTables::STRING:
125 theDumper<<'"'<<myvalues->value(aRow, aCol, aStep).myStr<<'"';
131 theDumper<<"])"<<std::endl;
135 std::shared_ptr<ModelAPI_AttributeTables> CollectionAPI_Field::tableValues()
140 //=================================================================================================
141 FieldPtr addField(const std::shared_ptr<ModelAPI_Document>& thePart,
142 const ModelHighAPI_Integer& theStepsNum,
143 const std::string& theComponentType,
144 const int theComponentsNum,
145 const std::list<std::string>& theComponentNames,
146 const std::list<ModelHighAPI_Selection>& theSelectionList)
148 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(CollectionAPI_Field::ID());
149 std::shared_ptr<CollectionAPI_Field> aField(new CollectionAPI_Field(aFeature));
150 aField->setStepsNum(theStepsNum);
151 aField->setValuesType(theComponentType);
152 aField->setComponentsNames(theComponentNames);
153 aField->setSelection(theSelectionList);
154 aField->tableValues()->setSize(
155 int(theSelectionList.size() + 1), theComponentsNum, theStepsNum.intValue());