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