1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "CollectionAPI_Field.h"
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Integer.h>
25 #include <ModelHighAPI_Selection.h>
26 #include <ModelHighAPI_Tools.h>
27 #include <ModelAPI_AttributeTables.h>
28 #include <ModelAPI_AttributeStringArray.h>
30 #include <algorithm> // for std::transform
32 //=================================================================================================
33 CollectionAPI_Field::CollectionAPI_Field(const std::shared_ptr<ModelAPI_Feature>& theFeature)
34 : ModelHighAPI_Interface(theFeature)
39 //=================================================================================================
40 CollectionAPI_Field::~CollectionAPI_Field()
44 //=================================================================================================
45 void CollectionAPI_Field::setSelection(const std::list<ModelHighAPI_Selection>& theFieldList)
47 fillAttribute(theFieldList, myselection);
51 //=================================================================================================
52 void CollectionAPI_Field::setComponentsNames(const std::list<std::string>& theNames)
54 fillAttribute(theNames, mycomponentsNames);
58 //=================================================================================================
59 void CollectionAPI_Field::setValuesType(const std::string& theType)
61 myvalues->setType(valueTypeByStr(theType));
65 //=================================================================================================
66 void CollectionAPI_Field::setStepsNum(const ModelHighAPI_Integer& theSteps)
68 mystamps->setSize(theSteps.intValue());
72 //=================================================================================================
73 void CollectionAPI_Field::setStamps(const std::list<ModelHighAPI_Integer>& theStamps)
75 fillAttribute(theStamps, mystamps);
79 //=================================================================================================
80 #define addStepImplementation(type, fieldType, type2, fieldType2, type3, fieldType3) \
81 void CollectionAPI_Field::addStep(const ModelHighAPI_Integer& theStepNum, \
82 const ModelHighAPI_Integer& theStamp, \
83 const std::list<std::list<type> >& theComponents) \
85 mystamps->setValue(theStepNum.intValue(), theStamp.intValue()); \
87 std::list<std::list<type> >::const_iterator \
88 aRowsIter = theComponents.begin(); \
89 for(; aRowsIter != theComponents.end(); aRowsIter++, aRowIndex++) { \
91 std::list<type>::const_iterator aColIter = aRowsIter->begin(); \
92 for(; aColIter != aRowsIter->end(); aColIter++, aColIndex++) { \
93 ModelAPI_AttributeTables::Value aVal; \
94 aVal.fieldType = *aColIter; \
95 aVal.fieldType2 = type2(*aColIter); \
96 aVal.fieldType3 = type3(*aColIter); \
97 myvalues->setValue(aVal, aRowIndex, aColIndex, theStepNum.intValue()); \
103 addStepImplementation(double, myDouble, int, myInt, bool, myBool);
104 addStepImplementation(int, myInt, double, myDouble, bool, myBool);
105 addStepImplementation(bool, myBool, int, myInt, double, myDouble);
106 addStepImplementation(std::string, myStr, std::string, myStr, std::string, myStr);
108 //=================================================================================================
109 void CollectionAPI_Field::dump(ModelHighAPI_Dumper& theDumper) const
111 FeaturePtr aBase = feature();
112 const std::string& aDocName = theDumper.name(aBase->document());
114 theDumper<<aBase<<" = model.addField("<<aDocName<<", "<<myvalues->tables()<<", \""
115 <<strByValueType(myvalues->type())<<"\", "<<mycomponentsNames->size()<<", ";
116 theDumper<<mycomponentsNames<<", ";
117 theDumper<<myselection<<")"<<std::endl;
118 // set values step by step
119 for(int aStep = 0; aStep < myvalues->tables(); aStep++) {
120 theDumper<<aBase<<".addStep("<<aStep<<", "<<mystamps->value(aStep)<<", [";
121 for(int aRow = 0; aRow < myvalues->rows(); aRow++) {
125 for(int aCol = 0; aCol < myvalues->columns(); aCol++) {
128 switch(myvalues->type()) {
129 case ModelAPI_AttributeTables::BOOLEAN:
130 theDumper<<myvalues->value(aRow, aCol, aStep).myBool;
132 case ModelAPI_AttributeTables::INTEGER:
133 theDumper<<myvalues->value(aRow, aCol, aStep).myInt;
135 case ModelAPI_AttributeTables::DOUBLE:
136 theDumper<<myvalues->value(aRow, aCol, aStep).myDouble;
138 case ModelAPI_AttributeTables::STRING:
139 theDumper<<'"'<<myvalues->value(aRow, aCol, aStep).myStr<<'"';
145 theDumper<<"])"<<std::endl;
149 std::shared_ptr<ModelAPI_AttributeTables> CollectionAPI_Field::tableValues()
154 //=================================================================================================
155 FieldPtr addField(const std::shared_ptr<ModelAPI_Document>& thePart,
156 const ModelHighAPI_Integer& theStepsNum,
157 const std::string& theComponentType,
158 const int theComponentsNum,
159 const std::list<std::string>& theComponentNames,
160 const std::list<ModelHighAPI_Selection>& theSelectionList)
162 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(CollectionAPI_Field::ID());
163 std::shared_ptr<CollectionAPI_Field> aField(new CollectionAPI_Field(aFeature));
164 aField->setStepsNum(theStepsNum);
165 aField->setValuesType(theComponentType);
166 aField->setComponentsNames(theComponentNames);
167 aField->setSelection(theSelectionList);
168 aField->tableValues()->setSize(
169 int(theSelectionList.size() + 1), theComponentsNum, theStepsNum.intValue());