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