Salome HOME
Remove not needed attributes from the field feature: they are duplicated in the Table...
[modules/shaper.git] / src / CollectionAPI / CollectionAPI_Field.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        CollectionAPI_Field.cpp
4 // Created:     16 Nov 2016
5 // Author:      Mikhail Ponikarov
6
7 #include "CollectionAPI_Field.h"
8
9 #include <ModelHighAPI_Dumper.h>
10 #include <ModelHighAPI_Integer.h>
11 #include <ModelHighAPI_Selection.h>
12 #include <ModelHighAPI_Tools.h>
13 #include <ModelAPI_AttributeTables.h>
14 #include <ModelAPI_AttributeStringArray.h>
15
16 #include <algorithm> // for std::transform
17
18 //=================================================================================================
19 CollectionAPI_Field::CollectionAPI_Field(const std::shared_ptr<ModelAPI_Feature>& theFeature)
20 : ModelHighAPI_Interface(theFeature)
21 {
22   initialize();
23 }
24
25 //=================================================================================================
26 CollectionAPI_Field::~CollectionAPI_Field()
27 {
28 }
29
30 //=================================================================================================
31 void CollectionAPI_Field::setSelection(const std::list<ModelHighAPI_Selection>& theFieldList)
32 {
33   fillAttribute(theFieldList, myselection);
34   execute();
35 }
36
37 //=================================================================================================
38 void CollectionAPI_Field::setComponentsNames(const std::list<std::string>& theNames)
39 {
40   fillAttribute(theNames, mycomponentsNames);
41   execute();
42 }
43
44 //=================================================================================================
45 void CollectionAPI_Field::setValuesType(const std::string& theType)
46 {
47   myvalues->setType(valueTypeByStr(theType));
48   execute();
49 }
50
51 //=================================================================================================
52 void CollectionAPI_Field::setStepsNum(const ModelHighAPI_Integer& theSteps)
53 {
54   mystamps->setSize(theSteps.intValue());
55   execute();
56 }
57
58 //=================================================================================================
59 void CollectionAPI_Field::setStamps(const std::list<ModelHighAPI_Integer>& theStamps)
60 {
61   fillAttribute(theStamps, mystamps);
62   execute();
63 }
64
65 //=================================================================================================
66 #define addStepImplementation(type, fieldType, type2, fieldType2, type3, fieldType3) \
67 void CollectionAPI_Field::addStep(const ModelHighAPI_Integer& theStepNum, \
68   const ModelHighAPI_Integer& theStamp, \
69   const std::list<std::list<type> >& theComponents) \
70 { \
71   mystamps->setValue(theStepNum.intValue(), theStamp.intValue()); \
72   int aRowIndex = 0; \
73   std::list<std::list<type> >::const_iterator \
74     aRowsIter = theComponents.begin(); \
75   for(; aRowsIter != theComponents.end(); aRowsIter++, aRowIndex++) { \
76     int aColIndex = 0; \
77     std::list<type>::const_iterator aColIter = aRowsIter->begin(); \
78     for(; aColIter != aRowsIter->end(); aColIter++, aColIndex++) { \
79       ModelAPI_AttributeTables::Value aVal; \
80       aVal.fieldType = *aColIter; \
81       aVal.fieldType2 = type2(*aColIter); \
82       aVal.fieldType3 = type3(*aColIter); \
83       myvalues->setValue(aVal, aRowIndex, aColIndex, theStepNum.intValue()); \
84     } \
85   } \
86   execute(); \
87 }
88
89 addStepImplementation(double, myDouble, int, myInt, bool, myBool);
90 addStepImplementation(int, myInt, double, myDouble, bool, myBool);
91 addStepImplementation(bool, myBool, int, myInt, double, myDouble);
92 addStepImplementation(std::string, myStr, std::string, myStr, std::string, myStr);
93
94 //=================================================================================================
95 void CollectionAPI_Field::dump(ModelHighAPI_Dumper& theDumper) const
96 {
97   FeaturePtr aBase = feature();
98   const std::string& aDocName = theDumper.name(aBase->document());
99
100   theDumper<<aBase<<" = model.addField("<<aDocName<<", "<<myvalues->tables()<<", \""
101     <<strByValueType(myvalues->type())<<"\", "<<mycomponentsNames->size()<<", ";
102   theDumper<<mycomponentsNames<<", ";
103   theDumper<<myselection<<")"<<std::endl;
104   // set values step by step
105   for(int aStep = 0; aStep < myvalues->tables(); aStep++) {
106     theDumper<<aBase<<".addStep("<<aStep<<", "<<mystamps->value(aStep)<<", [";
107     for(int aRow = 0; aRow < myvalues->rows(); aRow++) {
108       if (aRow != 0)
109         theDumper<<", ";
110       theDumper<<"[";
111       for(int aCol = 0; aCol < myvalues->columns(); aCol++) {
112         if (aCol != 0)
113           theDumper<<", ";
114         switch(myvalues->type()) {
115         case ModelAPI_AttributeTables::BOOLEAN:
116           theDumper<<myvalues->value(aRow, aCol, aStep).myBool;
117           break;
118         case ModelAPI_AttributeTables::INTEGER:
119           theDumper<<myvalues->value(aRow, aCol, aStep).myInt;
120           break;
121         case ModelAPI_AttributeTables::DOUBLE:
122           theDumper<<myvalues->value(aRow, aCol, aStep).myDouble;
123           break;
124         case ModelAPI_AttributeTables::STRING:
125           theDumper<<'"'<<myvalues->value(aRow, aCol, aStep).myStr<<'"';
126           break;
127         }
128       }
129       theDumper<<"]";
130     }
131     theDumper<<"])"<<std::endl;
132   }
133 }
134
135 std::shared_ptr<ModelAPI_AttributeTables> CollectionAPI_Field::tableValues()
136 {
137   return myvalues;
138 }
139
140 //=================================================================================================
141 FieldPtr addField(const std::shared_ptr<ModelAPI_Document>& thePart,
142                   const ModelHighAPI_Integer& theStepsNum,
143                   const std::string& theComponentType,
144                   const int theComponentsNum,
145                   const std::list<std::string>& theComponentNames,
146                   const std::list<ModelHighAPI_Selection>& theSelectionList)
147 {
148   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(CollectionAPI_Field::ID());
149   std::shared_ptr<CollectionAPI_Field> aField(new CollectionAPI_Field(aFeature));
150   aField->setStepsNum(theStepsNum);
151   aField->setValuesType(theComponentType);
152   aField->setComponentsNames(theComponentNames);
153   aField->setSelection(theSelectionList);
154   aField->tableValues()->setSize(
155     int(theSelectionList.size() + 1), theComponentsNum, theStepsNum.intValue());
156
157   return aField;
158 }