Salome HOME
56a18c43efaa8a8f4e2d11de96888f2e0632e089
[modules/shaper.git] / src / Model / Model_ResultField.cpp
1 // Copyright (C) 2014-2019  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 <Model_ResultField.h>
21 #include <Model_Document.h>
22
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_Feature.h>
25 #include <ModelAPI_AttributeIntArray.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29
30 #include <Config_PropManager.h>
31
32 Model_ResultField::Model_ResultField(std::shared_ptr<ModelAPI_Data> theOwnerData)
33 {
34   myOwnerData = theOwnerData;
35 }
36
37 Model_ResultField::~Model_ResultField()
38 {
39   while(mySteps.size() > 0) {
40     //delete mySteps.back();
41     mySteps.pop_back();
42   }
43 }
44
45 // LCOV_EXCL_START
46 void Model_ResultField::colorConfigInfo(std::string& theSection, std::string& theName,
47                                        std::string& theDefault)
48 {
49   theSection = "Visualization";
50   theName = "result_field_color";
51   theDefault = DEFAULT_COLOR();
52 }
53 // LCOV_EXCL_STOP
54
55 std::shared_ptr<GeomAPI_Shape> Model_ResultField::shape()
56 {
57   std::shared_ptr<GeomAPI_Shape> aResult;
58   if (myOwnerData) {
59     AttributeSelectionListPtr aList = myOwnerData->selectionList("selected");
60     if (aList) {
61       std::list<std::shared_ptr<GeomAPI_Shape> > aSubs;
62       if (aList->selectionType() == "part") { // all result bodies of the part
63         std::shared_ptr<Model_Document> aDoc =
64           std::dynamic_pointer_cast<Model_Document>(document());
65         if (!aDoc.get())
66           return aResult;
67         FeaturePtr aThisFeature = document()->feature(
68           std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner()));
69         int aResults = document()->size(ModelAPI_ResultBody::group());
70         for(int a = 0; a < aResults; a++) {
71           ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(
72             document()->object(ModelAPI_ResultBody::group(), a));
73           if (!aBody.get())
74             continue;
75           // check that only results that were created before this field are used
76           FeaturePtr aResultFeature = document()->feature(aBody);
77           if (!aResultFeature.get())
78             continue;
79           if (aDoc->isLater(aResultFeature, aThisFeature))
80             continue;
81           GeomShapePtr aShape = aBody->shape();
82           if (!aShape.get() || aShape->isNull())
83             continue;
84           aSubs.push_back(aShape);
85         }
86       } else {
87         for(int a = aList->size() - 1; a >= 0; a--) {
88           std::shared_ptr<GeomAPI_Shape> aSelection = aList->value(a)->value();
89           if (aSelection && !aSelection->isNull()) {
90             aSubs.push_back(aSelection);
91           }
92         }
93       }
94       if (!aSubs.empty()) {
95         aResult = GeomAlgoAPI_CompoundBuilder::compound(aSubs);
96       }
97     }
98     updateSteps();
99   }
100   return aResult;
101 }
102
103 void Model_ResultField::updateSteps()
104 {
105   // Update Array of steps
106   int aNbSteps = stepsSize();
107   if (mySteps.size() != aNbSteps) {
108     while(mySteps.size() > aNbSteps) {
109       //delete mySteps.back();
110       mySteps.pop_back();
111     }
112     while(mySteps.size() < aNbSteps) {
113       mySteps.push_back(FieldStepPtr(new Model_ResultField::Model_FieldStep(this,
114         int(mySteps.size()))));
115     }
116   }
117 }
118
119 int Model_ResultField::stepsSize() const
120 {
121   if (myOwnerData) {
122     AttributeIntArrayPtr aArray = myOwnerData->intArray("stamps");
123     if (aArray.get()) {
124       return aArray->size();
125     }
126   }
127   return 0;
128 }
129
130 std::string Model_ResultField::textLine(int theLine) const
131 {
132   if (myOwnerData) {
133     AttributeIntArrayPtr aArray = myOwnerData->intArray("stamps");
134     if (aArray.get()) {
135       if (theLine < aArray->size()) {
136         std::ostringstream aStream;
137         aStream << aArray->value(theLine);
138         return aStream.str();
139       }
140     }
141   }
142   return "";
143 }
144
145 // used by GUI only
146 // LCOV_EXCL_START
147 std::shared_ptr<ModelAPI_ResultField::ModelAPI_FieldStep> Model_ResultField::step(int theId) const
148 {
149   if (theId < mySteps.size()) {
150     return mySteps[theId];
151   }
152   return NULL;
153 }
154
155 std::string Model_ResultField::Model_FieldStep::name() {
156   std::ostringstream aStream;
157   aStream<<myParent->data()->name()<<std::endl;
158   aStream<<"Step "<<(myId + 1)<<" "<<myParent->textLine(myId);
159   return aStream.str();
160 }
161 // LCOV_EXCL_STOP