Salome HOME
1b618562d6e8c10dc3b3c6daa61a59574a8fbc0e
[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 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
213 {
214   if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
215     for (int a = theMain->size(ModelAPI_Feature::group()) - 1; a >= 0; a--) {
216       FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
217           theMain->object(ModelAPI_Feature::group(), a));
218       if (aPartFeat.get()) {
219         const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
220         std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
221         for(; aRes != aResList.end(); aRes++) {
222           ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
223           if (aPart.get()) {
224             if (aPart->isActivated() && aPart->partDoc() == theSub)
225               return aPartFeat;
226           } else break; // if the first is not Part, others are also not
227         }
228       }
229     }
230   }
231   return FeaturePtr();
232 }
233
234 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
235 {
236   if (theFeature.get() && theFeature->data()->isValid()) {
237     const std::set<std::shared_ptr<ModelAPI_Attribute> > aRefs = theFeature->data()->refsToMe();
238     std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
239     for(; aRefIter != aRefs.end(); aRefIter++) {
240       CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
241         ((*aRefIter)->owner());
242       if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
243         return aComp;
244     }
245   }
246   return CompositeFeaturePtr(); // not found
247 }
248
249 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
250 {
251   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
252   if (aBody.get()) {
253     FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
254     if (aFeatureOwner.get()) {
255       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = 
256         aFeatureOwner->results().cbegin();
257       for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
258         ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
259         if (aComp && aComp->isSub(aBody))
260           return aComp;
261       }
262     }
263   }
264   return ResultCompSolidPtr(); // not found
265 }
266
267 bool hasSubResults(const ResultPtr& theResult)
268 {
269   ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
270   return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
271 }
272
273 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
274 {
275   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
276   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
277   for (; aRIter != aResults.cend(); aRIter++) {
278     theResults.push_back(*aRIter);
279     // iterate sub-bodies of compsolid
280     ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
281     if (aComp.get()) {
282       int aNumSub = aComp->numberOfSubs();
283       for(int a = 0; a < aNumSub; a++) {
284         theResults.push_back(aComp->subResult(a));
285       }
286     }
287   }
288 }
289
290 } // namespace ModelAPI_Tools
291
292