1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "CollectionAPI_Field.h"
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Integer.h>
24 #include <ModelHighAPI_Selection.h>
25 #include <ModelHighAPI_Tools.h>
26 #include <ModelAPI_AttributeTables.h>
27 #include <ModelAPI_AttributeStringArray.h>
29 #include <algorithm> // for std::transform
31 //=================================================================================================
32 CollectionAPI_Field::CollectionAPI_Field(const std::shared_ptr<ModelAPI_Feature>& theFeature)
33 : ModelHighAPI_Interface(theFeature)
38 //=================================================================================================
39 CollectionAPI_Field::~CollectionAPI_Field()
43 //=================================================================================================
44 void CollectionAPI_Field::setSelection(const std::list<ModelHighAPI_Selection>& theFieldList)
46 fillAttribute(theFieldList, myselection);
50 //=================================================================================================
51 void CollectionAPI_Field::setComponentsNames(const std::list<std::string>& theNames)
53 fillAttribute(theNames, mycomponentsNames);
57 //=================================================================================================
58 void CollectionAPI_Field::setValuesType(const std::string& theType)
60 myvalues->setType(valueTypeByStr(theType));
64 //=================================================================================================
65 void CollectionAPI_Field::setStepsNum(const ModelHighAPI_Integer& theSteps)
67 mystamps->setSize(theSteps.intValue());
71 //=================================================================================================
72 void CollectionAPI_Field::setStamps(const std::list<ModelHighAPI_Integer>& theStamps)
74 fillAttribute(theStamps, mystamps);
78 //=================================================================================================
79 #define addStepImplementation(type, fieldType, type2, fieldType2, type3, fieldType3) \
80 void CollectionAPI_Field::addStep(const ModelHighAPI_Integer& theStepNum, \
81 const ModelHighAPI_Integer& theStamp, \
82 const std::list<std::list<type> >& theComponents) \
84 mystamps->setValue(theStepNum.intValue(), theStamp.intValue()); \
86 std::list<std::list<type> >::const_iterator \
87 aRowsIter = theComponents.begin(); \
88 for(; aRowsIter != theComponents.end(); aRowsIter++, aRowIndex++) { \
90 std::list<type>::const_iterator aColIter = aRowsIter->begin(); \
91 for(; aColIter != aRowsIter->end(); aColIter++, aColIndex++) { \
92 ModelAPI_AttributeTables::Value aVal; \
93 aVal.fieldType = *aColIter; \
94 aVal.fieldType2 = type2(*aColIter); \
95 aVal.fieldType3 = type3(*aColIter); \
96 myvalues->setValue(aVal, aRowIndex, aColIndex, theStepNum.intValue()); \
102 addStepImplementation(double, myDouble, int, myInt, bool, myBool);
103 addStepImplementation(int, myInt, double, myDouble, bool, myBool);
104 addStepImplementation(bool, myBool, int, myInt, double, myDouble);
105 addStepImplementation(std::string, myStr, std::string, myStr, std::string, myStr);
107 //=================================================================================================
108 void CollectionAPI_Field::dump(ModelHighAPI_Dumper& theDumper) const
110 FeaturePtr aBase = feature();
111 const std::string& aDocName = theDumper.name(aBase->document());
113 theDumper<<aBase<<" = model.addField("<<aDocName<<", "<<myvalues->tables()<<", \""
114 <<strByValueType(myvalues->type())<<"\", "<<mycomponentsNames->size()<<", ";
115 theDumper<<mycomponentsNames<<", ";
116 theDumper<<myselection<<")"<<std::endl;
117 // set values step by step
118 for(int aStep = 0; aStep < myvalues->tables(); aStep++) {
119 theDumper<<aBase<<".addStep("<<aStep<<", "<<mystamps->value(aStep)<<", [";
120 for(int aRow = 0; aRow < myvalues->rows(); aRow++) {
124 for(int aCol = 0; aCol < myvalues->columns(); aCol++) {
127 switch(myvalues->type()) {
128 case ModelAPI_AttributeTables::BOOLEAN:
129 theDumper<<myvalues->value(aRow, aCol, aStep).myBool;
131 case ModelAPI_AttributeTables::INTEGER:
132 theDumper<<myvalues->value(aRow, aCol, aStep).myInt;
134 case ModelAPI_AttributeTables::DOUBLE:
135 theDumper<<myvalues->value(aRow, aCol, aStep).myDouble;
137 case ModelAPI_AttributeTables::STRING:
138 theDumper<<'"'<<myvalues->value(aRow, aCol, aStep).myStr<<'"';
144 theDumper<<"])"<<std::endl;
148 std::shared_ptr<ModelAPI_AttributeTables> CollectionAPI_Field::tableValues()
153 //=================================================================================================
154 FieldPtr addField(const std::shared_ptr<ModelAPI_Document>& thePart,
155 const ModelHighAPI_Integer& theStepsNum,
156 const std::string& theComponentType,
157 const int theComponentsNum,
158 const std::list<std::string>& theComponentNames,
159 const std::list<ModelHighAPI_Selection>& theSelectionList)
161 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(CollectionAPI_Field::ID());
162 std::shared_ptr<CollectionAPI_Field> aField(new CollectionAPI_Field(aFeature));
163 aField->setStepsNum(theStepsNum);
164 aField->setValuesType(theComponentType);
165 aField->setComponentsNames(theComponentNames);
166 aField->setSelection(theSelectionList);
167 aField->tableValues()->setSize(
168 int(theSelectionList.size() + 1), theComponentsNum, theStepsNum.intValue());