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