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::setComponentsNum(const ModelHighAPI_Integer& theNum)
40 fillAttribute(theNum, mycomponentsNum);
44 //=================================================================================================
45 void CollectionAPI_Field::setComponentsNames(const std::list<std::string>& theNames)
47 fillAttribute(theNames, mycomponentsNames);
51 //=================================================================================================
52 void CollectionAPI_Field::setValuesType(const std::string& theType)
54 fillAttribute(int(valueTypeByStr(theType)), myvaluesType);
55 myvalues->setType(valueTypeByStr(theType));
59 //=================================================================================================
60 void CollectionAPI_Field::setStepsNum(const ModelHighAPI_Integer& theSteps)
62 fillAttribute(theSteps, mystepsNum);
63 mystamps->setSize(theSteps.intValue());
67 //=================================================================================================
68 void CollectionAPI_Field::setStamps(const std::list<ModelHighAPI_Integer>& theStamps)
70 fillAttribute(theStamps, mystamps);
74 //=================================================================================================
75 #define addStepImplementation(type, fieldType, type2, fieldType2, type3, fieldType3) \
76 void CollectionAPI_Field::addStep(const ModelHighAPI_Integer& theStepNum, \
77 const ModelHighAPI_Integer& theStamp, \
78 const std::list<std::list<type> >& theComponents) \
80 myvalues->setSize(myselection->size() + 1, \
81 mycomponentsNum->value(), mystepsNum->value()); \
82 mystamps->setValue(theStepNum.intValue(), theStamp.intValue()); \
84 std::list<std::list<type> >::const_iterator \
85 aRowsIter = theComponents.begin(); \
86 for(; aRowsIter != theComponents.end(); aRowsIter++, aRowIndex++) { \
88 std::list<type>::const_iterator aColIter = aRowsIter->begin(); \
89 for(; aColIter != aRowsIter->end(); aColIter++, aColIndex++) { \
90 ModelAPI_AttributeTables::Value aVal; \
91 aVal.fieldType = *aColIter; \
92 aVal.fieldType2 = type2(*aColIter); \
93 aVal.fieldType3 = type3(*aColIter); \
94 myvalues->setValue(aVal, aRowIndex, aColIndex, theStepNum.intValue()); \
100 addStepImplementation(double, myDouble, int, myInt, bool, myBool);
101 addStepImplementation(int, myInt, double, myDouble, bool, myBool);
102 addStepImplementation(bool, myBool, int, myInt, double, myDouble);
103 addStepImplementation(std::string, myStr, std::string, myStr, std::string, myStr);
105 //=================================================================================================
106 void CollectionAPI_Field::dump(ModelHighAPI_Dumper& theDumper) const
108 FeaturePtr aBase = feature();
109 const std::string& aDocName = theDumper.name(aBase->document());
111 theDumper<<aBase<<" = model.addField("<<aDocName<<", "<<mystepsNum->value()<<", \""
112 <<strByValueType(ModelAPI_AttributeTables::ValueType(myvaluesType->value()))<<"\", "
113 <<mycomponentsNum->value()<<", ";
114 theDumper<<mycomponentsNames<<", ";
115 theDumper<<myselection<<")"<<std::endl;
116 // set values step by step
117 for(int aStep = 0; aStep < myvalues->tables(); aStep++) {
118 theDumper<<aBase<<".addStep("<<aStep<<", "<<mystamps->value(aStep)<<", [";
119 for(int aRow = 0; aRow < myvalues->rows(); aRow++) {
123 for(int aCol = 0; aCol < myvalues->columns(); aCol++) {
126 switch(myvaluesType->value()) {
127 case ModelAPI_AttributeTables::BOOLEAN:
128 theDumper<<myvalues->value(aRow, aCol, aStep).myBool;
130 case ModelAPI_AttributeTables::INTEGER:
131 theDumper<<myvalues->value(aRow, aCol, aStep).myInt;
133 case ModelAPI_AttributeTables::DOUBLE:
134 theDumper<<myvalues->value(aRow, aCol, aStep).myDouble;
136 case ModelAPI_AttributeTables::STRING:
137 theDumper<<'"'<<myvalues->value(aRow, aCol, aStep).myStr<<'"';
143 theDumper<<"])"<<std::endl;
147 //=================================================================================================
148 FieldPtr addField(const std::shared_ptr<ModelAPI_Document>& thePart,
149 const ModelHighAPI_Integer& theStepsNum,
150 const std::string& theComponentType,
151 const int theComponentsNum,
152 const std::list<std::string>& theComponentNames,
153 const std::list<ModelHighAPI_Selection>& theSelectionList)
155 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(CollectionAPI_Field::ID());
156 std::shared_ptr<CollectionAPI_Field> aField(new CollectionAPI_Field(aFeature));
157 aField->setStepsNum(theStepsNum);
158 aField->setValuesType(theComponentType);
159 aField->setComponentsNum(theComponentsNum);
160 aField->setComponentsNames(theComponentNames);
161 aField->setSelection(theSelectionList);