]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultField.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / Model / Model_ResultField.cpp
1 // Copyright (C) 2014-2017  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<mailto: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 void Model_ResultField::colorConfigInfo(std::string& theSection, std::string& theName,
46                                        std::string& theDefault)
47 {
48   theSection = "Visualization";
49   theName = "result_field_color";
50   theDefault = DEFAULT_COLOR();
51 }
52
53 std::shared_ptr<GeomAPI_Shape> Model_ResultField::shape()
54 {
55   std::shared_ptr<GeomAPI_Shape> aResult;
56   if (myOwnerData) {
57     AttributeSelectionListPtr aList = myOwnerData->selectionList("selected");
58     if (aList) {
59       std::list<std::shared_ptr<GeomAPI_Shape> > aSubs;
60       if (aList->selectionType() == "part") { // all result bodies of the part
61         std::shared_ptr<Model_Document> aDoc =
62           std::dynamic_pointer_cast<Model_Document>(document());
63         if (!aDoc.get())
64           return aResult;
65         FeaturePtr aThisFeature = document()->feature(
66           std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner()));
67         int aResults = document()->size(ModelAPI_ResultBody::group());
68         for(int a = 0; a < aResults; a++) {
69           ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(
70             document()->object(ModelAPI_ResultBody::group(), a));
71           if (!aBody.get())
72             continue;
73           // check that only results that were created before this field are used
74           FeaturePtr aResultFeature = document()->feature(aBody);
75           if (!aResultFeature.get())
76             continue;
77           if (aDoc->isLater(aResultFeature, aThisFeature))
78             continue;
79           GeomShapePtr aShape = aBody->shape();
80           if (!aShape.get() || aShape->isNull())
81             continue;
82           aSubs.push_back(aShape);
83         }
84       } else {
85         for(int a = aList->size() - 1; a >= 0; a--) {
86           std::shared_ptr<GeomAPI_Shape> aSelection = aList->value(a)->value();
87           if (aSelection && !aSelection->isNull()) {
88             aSubs.push_back(aSelection);
89           }
90         }
91       }
92       if (!aSubs.empty()) {
93         aResult = GeomAlgoAPI_CompoundBuilder::compound(aSubs);
94       }
95     }
96     updateSteps();
97   }
98   return aResult;
99 }
100
101 void Model_ResultField::updateSteps()
102 {
103   // Update Array of steps
104   int aNbSteps = stepsSize();
105   if (mySteps.size() != aNbSteps) {
106     while(mySteps.size() > aNbSteps) {
107       delete mySteps.back();
108       mySteps.pop_back();
109     }
110     while(mySteps.size() < aNbSteps) {
111       mySteps.push_back(new Model_ResultField::Model_FieldStep(this, mySteps.size()));
112     }
113   }
114 }
115
116 int Model_ResultField::stepsSize() const
117 {
118   if (myOwnerData) {
119     AttributeIntArrayPtr aArray = myOwnerData->intArray("stamps");
120     if (aArray.get()) {
121       return aArray->size();
122     }
123   }
124   return 0;
125 }
126
127 std::string Model_ResultField::textLine(int theLine) const
128 {
129   if (myOwnerData) {
130     AttributeIntArrayPtr aArray = myOwnerData->intArray("stamps");
131     if (aArray.get()) {
132       if (theLine < aArray->size()) {
133         std::ostringstream aStream;
134         aStream << aArray->value(theLine);
135         return aStream.str();
136       }
137     }
138   }
139   return "";
140 }
141
142
143 ModelAPI_ResultField::ModelAPI_FieldStep* Model_ResultField::step(int theId) const
144 {
145   if (theId < mySteps.size()) {
146     return mySteps[theId];
147   }
148   return NULL;
149 }