Salome HOME
Issue #1865 : initial implementation of fields results and high level API
[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 <ModelHighAPI_ComponentValue.h>
14 #include <ModelAPI_AttributeTables.h>
15 #include <ModelAPI_AttributeStringArray.h>
16
17 #include <algorithm> // for std::transform
18
19 //=================================================================================================
20 CollectionAPI_Field::CollectionAPI_Field(const std::shared_ptr<ModelAPI_Feature>& theFeature)
21 : ModelHighAPI_Interface(theFeature)
22 {
23   initialize();
24 }
25
26 //=================================================================================================
27 CollectionAPI_Field::~CollectionAPI_Field()
28 {
29 }
30
31 //=================================================================================================
32 void CollectionAPI_Field::setSelection(const std::list<ModelHighAPI_Selection>& theFieldList)
33 {
34   fillAttribute(theFieldList, myselection);
35   execute();
36 }
37
38 //=================================================================================================
39 void CollectionAPI_Field::setComponentsNum(const ModelHighAPI_Integer& theNum)
40 {
41   fillAttribute(theNum, mycomponentsNum);
42   execute();
43 }
44
45 //=================================================================================================
46 void CollectionAPI_Field::setComponentsNames(const std::list<std::string>& theNames)
47 {
48   fillAttribute(theNames, mycomponentsNames);
49   execute();
50 }
51
52 //=================================================================================================
53 void CollectionAPI_Field::setValuesType(const std::string& theType)
54 {
55   fillAttribute(int(valueTypeByStr(theType)), myvaluesType);
56   execute();
57 }
58
59 //=================================================================================================
60 void CollectionAPI_Field::setStepsNum(const ModelHighAPI_Integer& theSteps)
61 {
62   fillAttribute(theSteps, mystepsNum);
63   execute();
64 }
65
66 //=================================================================================================
67 void CollectionAPI_Field::setStamps(const std::list<ModelHighAPI_Integer>& theStamps)
68 {
69   fillAttribute(theStamps, mystamps);
70   execute();
71 }
72
73 //=================================================================================================
74 void CollectionAPI_Field::addStep(const ModelHighAPI_Integer& theStepNum,
75   const ModelHighAPI_Integer& theStamp,
76   const std::list<std::list<ModelHighAPI_ComponentValue> >& theComponents)
77 {
78   // set the table size to be sure the values are up to date
79   myvalues->setSize(myselection->size() + 1 /* with defaults */,
80     mycomponentsNum->value(), mystepsNum->value());
81
82   // set values one by one
83   int aRowIndex = 0;
84   std::list<std::list<ModelHighAPI_ComponentValue> >::const_iterator 
85     aRowsIter = theComponents.begin();
86   for(; aRowsIter != theComponents.end(); aRowsIter++, aRowIndex++) {
87     int aColIndex = 0;
88     std::list<ModelHighAPI_ComponentValue>::const_iterator aColIter = aRowsIter->begin();
89     for(; aColIter != aRowsIter->end(); aColIter++, aColIndex++) {
90       aColIter->fill(myvalues, theStepNum.intValue(), aColIndex, aRowIndex);
91     }
92   }
93   execute();
94 }
95
96 //=================================================================================================
97 void CollectionAPI_Field::dump(ModelHighAPI_Dumper& theDumper) const
98 {
99   FeaturePtr aBase = feature();
100   const std::string& aDocName = theDumper.name(aBase->document());
101
102   theDumper<<aBase<<" = model.addField("<<aDocName<<", "<<mystepsNum<<", "
103     <<strByValueType(ModelAPI_AttributeTables::ValueType(myvaluesType->value()))<<", "
104     <<mycomponentsNum->value()<<", ";
105   theDumper<<mycomponentsNames<<", ";
106   theDumper<<myselection<<")"<<std::endl;
107   // set values step by step
108   for(int aStep = 0; aStep < myvalues->tables(); aStep++) {
109     theDumper<<aBase<<".addStep("<<aStep<<", "<<mystamps->value(aStep)<<", [";
110     for(int aRow = 0; aRow < myvalues->rows(); aRow++) {
111       if (aRow != 0)
112         theDumper<<", ";
113       theDumper<<"[";
114       for(int aCol = 0; aCol < myvalues->columns(); aCol++) {
115         if (aCol != 0)
116           theDumper<<", ";
117         switch(myvalues->type()) {
118         case ModelAPI_AttributeTables::BOOLEAN:
119           theDumper<<myvalues->value(aRow, aCol, aStep).myBool;
120           break;
121         case ModelAPI_AttributeTables::INTEGER:
122           theDumper<<myvalues->value(aRow, aCol, aStep).myInt;
123           break;
124         case ModelAPI_AttributeTables::DOUBLE:
125           theDumper<<myvalues->value(aRow, aCol, aStep).myDouble;
126           break;
127         case ModelAPI_AttributeTables::STRING:
128           theDumper<<myvalues->value(aRow, aCol, aStep).myStr;
129           break;
130         }
131       }
132       theDumper<<"]";
133     }
134     theDumper<<")"<<std::endl;
135   }
136 }
137
138 //=================================================================================================
139 FieldPtr addField(const std::shared_ptr<ModelAPI_Document>& thePart,
140                   const ModelHighAPI_Integer& theStepsNum,
141                   std::string& theComponentType,
142                   const int theComponentsNum,
143                   const std::list<std::string>& theComponentNames,
144                   const std::list<ModelHighAPI_Selection>& theSelectionList)
145 {
146   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(CollectionAPI_Field::ID());
147   std::shared_ptr<CollectionAPI_Field> aField(new CollectionAPI_Field(aFeature));
148   aField->setStepsNum(theStepsNum);
149   aField->setValuesType(theComponentType);
150   aField->setComponentsNum(theComponentsNum);
151   aField->setComponentsNames(theComponentNames);
152   aField->setSelection(theSelectionList);
153
154   return aField;
155 }