]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_Tools.cpp
Salome HOME
20073da7c1a3242720678a5ede3933833a6fe404
[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       // If the feature is Composite and error is StateInvalidArgument,
58       // error text should include error of first invalid sub-feature. Otherwise
59       // it is not clear what is the reason of the invalid argument.
60       if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
61         CompositeFeaturePtr aComposite =
62                     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
63         if (aComposite) {
64           for (int i = 0, aSize = aComposite->numberOfSubs(); i < aSize; i++) {
65             FeaturePtr aSubFeature = aComposite->subFeature(i);
66             std::string aSubFeatureError = getFeatureError(aSubFeature);
67             if (!aSubFeatureError.empty()) {
68               anError = anError + " in " + aSubFeature->getKind() + ".\n" + aSubFeatureError;
69               break;
70             }
71           }
72         }
73       }
74     }
75   }
76
77   return anError;
78 }
79
80 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup, const std::string& theName)
81 {
82   for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
83     ObjectPtr anObject = theDocument->object(theGroup, anIndex);
84     if (anObject->data()->name() == theName)
85       return anObject;
86   }
87   // not found
88   return ObjectPtr();
89 }
90
91 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
92                   const std::string& theName, double& outValue, ResultParameterPtr& theParam)
93 {
94   ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
95   theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
96   if (!theParam.get())
97     return false;
98   // avoid usage of parameters created later than the initial parameter
99   if (theSearcher.get() && theDocument->isLater(theDocument->feature(theParam), theSearcher))
100     return false;
101   AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
102   outValue = aValueAttribute->value();
103   return true;
104 }
105
106 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue, ResultParameterPtr& theParam,
107                   const DocumentPtr& theDocument)
108 {
109   SessionPtr aSession = ModelAPI_Session::get();
110   std::list<DocumentPtr> aDocList;
111   DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
112   if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
113     return true;
114   DocumentPtr aRootDocument = aSession->moduleDocument();
115   if (aDocument != aRootDocument) {
116     ResultPtr aPartResult = findPartResult(aRootDocument, aDocument);
117     if (aPartResult.get()) {
118       FeaturePtr aPartFeature;
119       if (theSearcher.get()) // only if the relative search is needed
120         aPartFeature = aRootDocument->feature(aPartResult);
121       if (findVariable(aRootDocument, aPartFeature, theName, outValue, theParam))
122         return true;
123     }
124   }
125   return false;
126 }
127
128 static std::map<int, std::vector<int> > myColorMap;
129
130 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
131 {
132   theRGB.push_back(theRed);
133   theRGB.push_back(theGreen);
134   theRGB.push_back(theBlue);
135 }
136
137 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
138 {
139   std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
140   bool isFound = false;
141   for (; anIt != aLast && !isFound; anIt++) {
142     std::vector<int> aValues = anIt->second;
143     isFound = aValues[0] == theValues[0] &&
144               aValues[1] == theValues[1] &&
145               aValues[2] == theValues[2];
146   }
147   return isFound;
148 }
149
150 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
151 {
152   std::vector<int> aRGB;
153   if (theH < 0 || theH > 360 ||
154       theS < 0 || theS > 100 ||
155       theV < 0 || theV > 100)
156     return aRGB;
157
158   int aHi = (int)theH/60;
159
160   double aV = theV;
161   double aVmin = (100 - theS)*theV/100;
162
163   double anA = (theV - aVmin)* (theH % 60) / 60;
164
165   double aVinc = aVmin + anA;
166   double aVdec = theV - anA;
167
168   double aPercentToValue = 255./100;
169   int aV_int    = (int)(aV*aPercentToValue);
170   int aVinc_int = (int)(aVinc*aPercentToValue);
171   int aVmin_int = (int)(aVmin*aPercentToValue);
172   int aVdec_int = (int)(aVdec*aPercentToValue);
173
174   switch(aHi) {
175     case 0: appendValues(aRGB, aV_int,    aVinc_int, aVmin_int); break;
176     case 1: appendValues(aRGB, aVdec_int, aV_int,    aVmin_int); break;
177     case 2: appendValues(aRGB, aVmin_int, aV_int,    aVinc_int); break;
178     case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
179     case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
180     case 5: appendValues(aRGB, aV_int,    aVmin_int, aVdec_int); break;
181     default: break;
182   }
183   return aRGB;
184 }
185
186
187 void fillColorMap()
188 {
189   if (!myColorMap.empty())
190     return;
191
192   int i = 0;
193   for (int s = 100; s > 0; s = s - 50)
194   {
195     for (int v = 100; v >= 40; v = v - 20)
196     {
197       for (int h = 0; h < 359 ; h = h + 60)
198       {
199         std::vector<int> aColor = HSVtoRGB(h, s, v);
200         if (containsValues(myColorMap, aColor))
201           continue;
202         myColorMap[i] = aColor;
203         i++;
204       }
205     }
206   }
207 }
208
209 void findRandomColor(std::vector<int>& theValues)
210 {
211   theValues.clear();
212   if (myColorMap.empty()) {
213     fillColorMap();
214   }
215
216   size_t aSize = myColorMap.size();
217   int anIndex = rand() % aSize;
218   if (myColorMap.find(anIndex) != myColorMap.end()) {
219     theValues = myColorMap.at(anIndex);
220   }
221 }
222
223 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
224 {
225   if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
226     for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
227       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
228           theMain->object(ModelAPI_ResultPart::group(), a));
229       if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
230         return aPart;
231       }
232     }
233   }
234   return ResultPtr();
235 }
236
237 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
238 {
239   if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
240     for (int a = theMain->size(ModelAPI_Feature::group()) - 1; a >= 0; a--) {
241       FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
242           theMain->object(ModelAPI_Feature::group(), a));
243       if (aPartFeat.get()) {
244         const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
245         std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
246         for(; aRes != aResList.end(); aRes++) {
247           ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
248           if (aPart.get()) {
249             if (aPart->isActivated() && aPart->partDoc() == theSub)
250               return aPartFeat;
251           } else break; // if the first is not Part, others are also not
252         }
253       }
254     }
255   }
256   return FeaturePtr();
257 }
258
259 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
260 {
261   if (theFeature.get() && theFeature->data()->isValid()) {
262     const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
263     std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
264     for(; aRefIter != aRefs.end(); aRefIter++) {
265       CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
266         ((*aRefIter)->owner());
267       if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
268         return aComp;
269     }
270   }
271   return CompositeFeaturePtr(); // not found
272 }
273
274 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
275 {
276   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
277   if (aBody.get()) {
278     FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
279     if (aFeatureOwner.get()) {
280       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = 
281         aFeatureOwner->results().cbegin();
282       for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
283         ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
284         if (aComp && aComp->isSub(aBody))
285           return aComp;
286       }
287     }
288   }
289   return ResultCompSolidPtr(); // not found
290 }
291
292 bool hasSubResults(const ResultPtr& theResult)
293 {
294   ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
295   return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
296 }
297
298 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
299 {
300   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
301   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
302   for (; aRIter != aResults.cend(); aRIter++) {
303     theResults.push_back(*aRIter);
304     // iterate sub-bodies of compsolid
305     ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
306     if (aComp.get()) {
307       int aNumSub = aComp->numberOfSubs();
308       for(int a = 0; a < aNumSub; a++) {
309         theResults.push_back(aComp->subResult(a));
310       }
311     }
312   }
313 }
314
315 } // namespace ModelAPI_Tools
316
317