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 <ModelHighAPI_ComponentValue.h>
14 #include <ModelAPI_AttributeTables.h>
15 #include <ModelAPI_AttributeStringArray.h>
17 #include <algorithm> // for std::transform
19 //=================================================================================================
20 CollectionAPI_Field::CollectionAPI_Field(const std::shared_ptr<ModelAPI_Feature>& theFeature)
21 : ModelHighAPI_Interface(theFeature)
26 //=================================================================================================
27 CollectionAPI_Field::~CollectionAPI_Field()
31 //=================================================================================================
32 void CollectionAPI_Field::setSelection(const std::list<ModelHighAPI_Selection>& theFieldList)
34 fillAttribute(theFieldList, myselection);
38 //=================================================================================================
39 void CollectionAPI_Field::setComponentsNum(const ModelHighAPI_Integer& theNum)
41 fillAttribute(theNum, mycomponentsNum);
45 //=================================================================================================
46 void CollectionAPI_Field::setComponentsNames(const std::list<std::string>& theNames)
48 fillAttribute(theNames, mycomponentsNames);
52 //=================================================================================================
53 void CollectionAPI_Field::setValuesType(const std::string& theType)
55 fillAttribute(int(valueTypeByStr(theType)), myvaluesType);
59 //=================================================================================================
60 void CollectionAPI_Field::setStepsNum(const ModelHighAPI_Integer& theSteps)
62 fillAttribute(theSteps, mystepsNum);
66 //=================================================================================================
67 void CollectionAPI_Field::setStamps(const std::list<ModelHighAPI_Integer>& theStamps)
69 fillAttribute(theStamps, mystamps);
73 //=================================================================================================
74 void CollectionAPI_Field::addStep(const ModelHighAPI_Integer& theStepNum,
75 const ModelHighAPI_Integer& theStamp,
76 const std::list<std::list<ModelHighAPI_ComponentValue> >& theComponents)
78 // set the table size to be sure the values are up to date
79 myvalues->setSize(myselection->size() + 1 /* with defaults */,
80 mycomponentsNum->value(), mystepsNum->value());
82 // set values one by one
84 std::list<std::list<ModelHighAPI_ComponentValue> >::const_iterator
85 aRowsIter = theComponents.begin();
86 for(; aRowsIter != theComponents.end(); aRowsIter++, aRowIndex++) {
88 std::list<ModelHighAPI_ComponentValue>::const_iterator aColIter = aRowsIter->begin();
89 for(; aColIter != aRowsIter->end(); aColIter++, aColIndex++) {
90 aColIter->fill(myvalues, theStepNum.intValue(), aColIndex, aRowIndex);
96 //=================================================================================================
97 void CollectionAPI_Field::dump(ModelHighAPI_Dumper& theDumper) const
99 FeaturePtr aBase = feature();
100 const std::string& aDocName = theDumper.name(aBase->document());
102 theDumper<<aBase<<" = model.addField("<<aDocName<<", "<<mystepsNum<<", "
103 <<strByValueType(ModelAPI_AttributeTables::ValueType(myvaluesType->value()))<<", "
104 <<mycomponentsNum->value()<<", ";
105 theDumper<<mycomponentsNames<<", ";
106 theDumper<<myselection<<")"<<std::endl;
107 // set values step by step
108 for(int aStep = 0; aStep < myvalues->tables(); aStep++) {
109 theDumper<<aBase<<".addStep("<<aStep<<", "<<mystamps->value(aStep)<<", [";
110 for(int aRow = 0; aRow < myvalues->rows(); aRow++) {
114 for(int aCol = 0; aCol < myvalues->columns(); aCol++) {
117 switch(myvalues->type()) {
118 case ModelAPI_AttributeTables::BOOLEAN:
119 theDumper<<myvalues->value(aRow, aCol, aStep).myBool;
121 case ModelAPI_AttributeTables::INTEGER:
122 theDumper<<myvalues->value(aRow, aCol, aStep).myInt;
124 case ModelAPI_AttributeTables::DOUBLE:
125 theDumper<<myvalues->value(aRow, aCol, aStep).myDouble;
127 case ModelAPI_AttributeTables::STRING:
128 theDumper<<myvalues->value(aRow, aCol, aStep).myStr;
134 theDumper<<")"<<std::endl;
138 //=================================================================================================
139 FieldPtr addField(const std::shared_ptr<ModelAPI_Document>& thePart,
140 const ModelHighAPI_Integer& theStepsNum,
141 std::string& theComponentType,
142 const int theComponentsNum,
143 const std::list<std::string>& theComponentNames,
144 const std::list<ModelHighAPI_Selection>& theSelectionList)
146 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(CollectionAPI_Field::ID());
147 std::shared_ptr<CollectionAPI_Field> aField(new CollectionAPI_Field(aFeature));
148 aField->setStepsNum(theStepsNum);
149 aField->setValuesType(theComponentType);
150 aField->setComponentsNum(theComponentsNum);
151 aField->setComponentsNames(theComponentNames);
152 aField->setSelection(theSelectionList);