Salome HOME
9023cb2d2d89893c4fb9bf343d2fc6ab248571a1
[modules/shaper.git] / src / ModelAPI / ModelAPI_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Tools.cpp
4 // Created:     06 Aug 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ModelAPI_Tools.h"
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Document.h>
10 #include <ModelAPI_Object.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_ResultParameter.h>
13 #include <ModelAPI_ResultPart.h>
14 #include <ModelAPI_AttributeDocRef.h>
15 #include <list>
16 #include <map>
17
18 namespace ModelAPI_Tools {
19
20 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
21 {
22   return theResult->shape();
23 }
24
25 std::string getFeatureError(const FeaturePtr& theFeature)
26 {
27   std::string anError;
28   if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
29     return anError;
30
31   // to be removed later, this error should be got from the feature
32   if (theFeature->data()->execState() == ModelAPI_StateDone ||
33       theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
34     return anError;
35
36   // set error indication
37   anError = theFeature->error();
38   if (anError.empty()) {
39     bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
40                  || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
41     if (!isDone)
42       anError = theFeature->data()->execState();
43   }
44
45   return anError;
46 }
47
48 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup, const std::string& theName)
49 {
50   for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
51     ObjectPtr anObject = theDocument->object(theGroup, anIndex);
52     if (anObject->data()->name() == theName)
53       return anObject;
54   }
55   // not found
56   return ObjectPtr();
57 }
58
59 bool findVariable(const DocumentPtr& theDocument, 
60                   const std::string& theName, double& outValue, ResultParameterPtr& theParam)
61 {
62   ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
63   theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
64   if (!theParam.get())
65     return false;
66   AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
67   outValue = aValueAttribute->value();
68   return true;
69 }
70
71 bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam,
72                   const DocumentPtr& theDocument /*= DocumentPtr()*/)
73 {
74   SessionPtr aSession = ModelAPI_Session::get();
75   std::list<DocumentPtr> aDocList;
76   DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
77   DocumentPtr aRootDocument = aSession->moduleDocument();
78   aDocList.push_back(aDocument);
79   if (aDocument != aRootDocument) {
80     aDocList.push_back(aRootDocument);
81   }
82   for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
83     if (findVariable(*it, theName, outValue, theParam))
84       return true;
85   }
86   return false;
87 }
88
89 static std::map<int, std::vector<int> > myColorMap;
90
91 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
92 {
93   theRGB.push_back(theRed);
94   theRGB.push_back(theGreen);
95   theRGB.push_back(theBlue);
96 }
97
98 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
99 {
100   std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
101   bool isFound = false;
102   for (; anIt != aLast && !isFound; anIt++) {
103     std::vector<int> aValues = anIt->second;
104     isFound = aValues[0] == theValues[0] &&
105               aValues[1] == theValues[1] &&
106               aValues[2] == theValues[2];
107   }
108   return isFound;
109 }
110
111 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
112 {
113   std::vector<int> aRGB;
114   if (theH < 0 || theH > 360 ||
115       theS < 0 || theS > 100 ||
116       theV < 0 || theV > 100)
117     return aRGB;
118
119   int aHi = (int)theH/60;
120
121   double aV = theV;
122   double aVmin = (100 - theS)*theV/100;
123
124   double anA = (theV - aVmin)* (theH % 60) / 60;
125
126   double aVinc = aVmin + anA;
127   double aVdec = theV - anA;
128
129   double aPercentToValue = 255./100;
130   int aV_int    = (int)(aV*aPercentToValue);
131   int aVinc_int = (int)(aVinc*aPercentToValue);
132   int aVmin_int = (int)(aVmin*aPercentToValue);
133   int aVdec_int = (int)(aVdec*aPercentToValue);
134
135   switch(aHi) {
136     case 0: appendValues(aRGB, aV_int,    aVinc_int, aVmin_int); break;
137     case 1: appendValues(aRGB, aVdec_int, aV_int,    aVmin_int); break;
138     case 2: appendValues(aRGB, aVmin_int, aV_int,    aVinc_int); break;
139     case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
140     case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
141     case 5: appendValues(aRGB, aV_int,    aVmin_int, aVdec_int); break;
142     default: break;
143   }
144   return aRGB;
145 }
146
147
148 void fillColorMap()
149 {
150   if (!myColorMap.empty())
151     return;
152
153   int i = 0;
154   for (int s = 100; s > 0; s = s - 50)
155   {
156     for (int v = 100; v >= 40; v = v - 20)
157     {
158       for (int h = 0; h < 359 ; h = h + 60)
159       {
160         std::vector<int> aColor = HSVtoRGB(h, s, v);
161         if (containsValues(myColorMap, aColor))
162           continue;
163         myColorMap[i] = aColor;
164         i++;
165       }
166     }
167   }
168 }
169
170 void findRandomColor(std::vector<int>& theValues)
171 {
172   theValues.clear();
173   if (myColorMap.empty()) {
174     fillColorMap();
175   }
176
177   int aSize = myColorMap.size();
178   int anIndex = rand() % aSize;
179   if (myColorMap.find(anIndex) != myColorMap.end()) {
180     theValues = myColorMap.at(anIndex);
181   }
182 }
183
184 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
185 {
186   if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
187     for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
188       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
189           theMain->object(ModelAPI_ResultPart::group(), a));
190       if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
191         return aPart;
192       }
193     }
194   }
195   return ResultPtr();
196 }
197
198 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
199 {
200   if (theFeature.get() && theFeature->data()->isValid()) {
201     const std::set<std::shared_ptr<ModelAPI_Attribute> > aRefs = theFeature->data()->refsToMe();
202     std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
203     for(; aRefIter != aRefs.end(); aRefIter++) {
204       CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
205         ((*aRefIter)->owner());
206       if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
207         return aComp;
208     }
209   }
210   return CompositeFeaturePtr(); // not found
211 }
212
213 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
214 {
215   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
216   if (aBody.get()) {
217     FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
218     if (aFeatureOwner.get()) {
219       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = 
220         aFeatureOwner->results().cbegin();
221       for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
222         ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
223         if (aComp && aComp->isSub(aBody))
224           return aComp;
225       }
226     }
227   }
228   return ResultCompSolidPtr(); // not found
229 }
230
231 bool hasSubResults(const ResultPtr& theResult)
232 {
233   ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
234   return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
235 }
236
237 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
238 {
239   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
240   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
241   for (; aRIter != aResults.cend(); aRIter++) {
242     theResults.push_back(*aRIter);
243     // iterate sub-bodies of compsolid
244     ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
245     if (aComp.get()) {
246       int aNumSub = aComp->numberOfSubs();
247       for(int a = 0; a < aNumSub; a++) {
248         theResults.push_back(aComp->subResult(a));
249       }
250     }
251   }
252 }
253
254 } // namespace ModelAPI_Tools
255
256