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