Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / Model / Model_ResultField.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_ResultField.cpp
4 // Created:     08 Jul 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_ResultField.h>
8 #include <Model_Document.h>
9
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_AttributeIntArray.h>
13 #include <ModelAPI_AttributeSelectionList.h>
14
15 #include <GeomAlgoAPI_CompoundBuilder.h>
16
17 #include <Config_PropManager.h>
18
19 Model_ResultField::Model_ResultField(std::shared_ptr<ModelAPI_Data> theOwnerData)
20 {
21   myOwnerData = theOwnerData;
22 }
23
24 Model_ResultField::~Model_ResultField()
25 {
26   while(mySteps.size() > 0) {
27     delete mySteps.back();
28     mySteps.pop_back();
29   }
30 }
31
32 void Model_ResultField::colorConfigInfo(std::string& theSection, std::string& theName,
33                                        std::string& theDefault)
34 {
35   theSection = "Visualization";
36   theName = "result_field_color";
37   theDefault = DEFAULT_COLOR();
38 }
39
40 std::shared_ptr<GeomAPI_Shape> Model_ResultField::shape()
41 {
42   std::shared_ptr<GeomAPI_Shape> aResult;
43   if (myOwnerData) {
44     AttributeSelectionListPtr aList = myOwnerData->selectionList("selected");
45     if (aList) {
46       std::list<std::shared_ptr<GeomAPI_Shape> > aSubs;
47       if (aList->selectionType() == "part") { // all result bodies of the part
48         std::shared_ptr<Model_Document> aDoc =
49           std::dynamic_pointer_cast<Model_Document>(document());
50         if (!aDoc.get())
51           return aResult;
52         FeaturePtr aThisFeature = document()->feature(
53           std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner()));
54         int aResults = document()->size(ModelAPI_ResultBody::group());
55         for(int a = 0; a < aResults; a++) {
56           ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(
57             document()->object(ModelAPI_ResultBody::group(), a));
58           if (!aBody.get())
59             continue;
60           // check that only results that were created before this field are used
61           FeaturePtr aResultFeature = document()->feature(aBody);
62           if (!aResultFeature.get())
63             continue;
64           if (aDoc->isLater(aResultFeature, aThisFeature))
65             continue;
66           GeomShapePtr aShape = aBody->shape();
67           if (!aShape.get() || aShape->isNull())
68             continue;
69           aSubs.push_back(aShape);
70         }
71       } else {
72         for(int a = aList->size() - 1; a >= 0; a--) {
73           std::shared_ptr<GeomAPI_Shape> aSelection = aList->value(a)->value();
74           if (aSelection && !aSelection->isNull()) {
75             aSubs.push_back(aSelection);
76           }
77         }
78       }
79       if (!aSubs.empty()) {
80         aResult = GeomAlgoAPI_CompoundBuilder::compound(aSubs);
81       }
82     }
83     updateSteps();
84   }
85   return aResult;
86 }
87
88 void Model_ResultField::updateSteps()
89 {
90   // Update Array of steps
91   int aNbSteps = stepsSize();
92   if (mySteps.size() != aNbSteps) {
93     while(mySteps.size() > aNbSteps) {
94       delete mySteps.back();
95       mySteps.pop_back();
96     }
97     while(mySteps.size() < aNbSteps) {
98       mySteps.push_back(new Model_ResultField::Model_FieldStep(this, mySteps.size()));
99     }
100   }
101 }
102
103 int Model_ResultField::stepsSize() const
104 {
105   if (myOwnerData) {
106     AttributeIntArrayPtr aArray = myOwnerData->intArray("stamps");
107     if (aArray.get()) {
108       return aArray->size();
109     }
110   }
111   return 0;
112 }
113
114 std::string Model_ResultField::textLine(int theLine) const
115 {
116   if (myOwnerData) {
117     AttributeIntArrayPtr aArray = myOwnerData->intArray("stamps");
118     if (aArray.get()) {
119       if (theLine < aArray->size()) {
120         std::ostringstream aStream;
121         aStream << aArray->value(theLine);
122         return aStream.str();
123       }
124     }
125   }
126   return "";
127 }
128
129
130 ModelAPI_ResultField::ModelAPI_FieldStep* Model_ResultField::step(int theId) const
131 {
132   if (theId < mySteps.size()) {
133     return mySteps[theId];
134   }
135   return NULL;
136 }