Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / CollectionAPI / CollectionAPI_Field.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "CollectionAPI_Field.h"
22
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>
29
30 #include <algorithm> // for std::transform
31
32 //=================================================================================================
33 CollectionAPI_Field::CollectionAPI_Field(const std::shared_ptr<ModelAPI_Feature>& theFeature)
34 : ModelHighAPI_Interface(theFeature)
35 {
36   initialize();
37 }
38
39 //=================================================================================================
40 CollectionAPI_Field::~CollectionAPI_Field()
41 {
42 }
43
44 //=================================================================================================
45 void CollectionAPI_Field::setSelection(const std::list<ModelHighAPI_Selection>& theFieldList)
46 {
47   fillAttribute(theFieldList, myselection);
48   execute();
49 }
50
51 //=================================================================================================
52 void CollectionAPI_Field::setComponentsNames(const std::list<std::string>& theNames)
53 {
54   fillAttribute(theNames, mycomponentsNames);
55   execute();
56 }
57
58 //=================================================================================================
59 void CollectionAPI_Field::setValuesType(const std::string& theType)
60 {
61   myvalues->setType(valueTypeByStr(theType));
62   execute();
63 }
64
65 //=================================================================================================
66 void CollectionAPI_Field::setStepsNum(const ModelHighAPI_Integer& theSteps)
67 {
68   mystamps->setSize(theSteps.intValue());
69   execute();
70 }
71
72 //=================================================================================================
73 void CollectionAPI_Field::setStamps(const std::list<ModelHighAPI_Integer>& theStamps)
74 {
75   fillAttribute(theStamps, mystamps);
76   execute();
77 }
78
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) \
84 { \
85   mystamps->setValue(theStepNum.intValue(), theStamp.intValue()); \
86   int aRowIndex = 0; \
87   std::list<std::list<type> >::const_iterator \
88     aRowsIter = theComponents.begin(); \
89   for(; aRowsIter != theComponents.end(); aRowsIter++, aRowIndex++) { \
90     int aColIndex = 0; \
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()); \
98     } \
99   } \
100   execute(); \
101 }
102
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);
107
108 //=================================================================================================
109 void CollectionAPI_Field::dump(ModelHighAPI_Dumper& theDumper) const
110 {
111   FeaturePtr aBase = feature();
112   const std::string& aDocName = theDumper.name(aBase->document());
113
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++) {
122       if (aRow != 0)
123         theDumper<<", ";
124       theDumper<<"[";
125       for(int aCol = 0; aCol < myvalues->columns(); aCol++) {
126         if (aCol != 0)
127           theDumper<<", ";
128         switch(myvalues->type()) {
129         case ModelAPI_AttributeTables::BOOLEAN:
130           theDumper<<myvalues->value(aRow, aCol, aStep).myBool;
131           break;
132         case ModelAPI_AttributeTables::INTEGER:
133           theDumper<<myvalues->value(aRow, aCol, aStep).myInt;
134           break;
135         case ModelAPI_AttributeTables::DOUBLE:
136           theDumper<<myvalues->value(aRow, aCol, aStep).myDouble;
137           break;
138         case ModelAPI_AttributeTables::STRING:
139           theDumper<<'"'<<myvalues->value(aRow, aCol, aStep).myStr<<'"';
140           break;
141         }
142       }
143       theDumper<<"]";
144     }
145     theDumper<<"])"<<std::endl;
146   }
147 }
148
149 std::shared_ptr<ModelAPI_AttributeTables> CollectionAPI_Field::tableValues()
150 {
151   return myvalues;
152 }
153
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)
161 {
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());
170
171   return aField;
172 }