Salome HOME
Issue #763 - It is possible to remove parameter which is used in features -- Fix...
[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 /*
23   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
24   if (aBody)
25     return aBody->shape();
26
27   ResultConstructionPtr aConstruct = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
28     theResult);
29   if (aConstruct)
30     return aConstruct->shape();
31
32   ResultGroupPtr aGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(theResult);
33   if (aGroup)
34     return aGroup->shape();
35   return std::shared_ptr<GeomAPI_Shape>();
36   */
37   return theResult->shape();
38 }
39
40 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup, const std::string& theName)
41 {
42   for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
43     ObjectPtr anObject = theDocument->object(theGroup, anIndex);
44     if (anObject->data()->name() == theName)
45       return anObject;
46   }
47   // not found
48   return ObjectPtr();
49 }
50
51 bool findVariable(const DocumentPtr& theDocument, 
52                   const std::string& theName, double& outValue, ResultParameterPtr& theParam)
53 {
54   ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
55   theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
56   if (!theParam.get())
57     return false;
58   AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
59   outValue = aValueAttribute->value();
60   return true;
61 }
62
63 bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam,
64                   const DocumentPtr& theDocument /*= DocumentPtr()*/)
65 {
66   SessionPtr aSession = ModelAPI_Session::get();
67   std::list<DocumentPtr> aDocList;
68   DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
69   DocumentPtr aRootDocument = aSession->moduleDocument();
70   aDocList.push_back(aDocument);
71   if (aDocument != aRootDocument) {
72     aDocList.push_back(aRootDocument);
73   }
74   for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
75     if (findVariable(*it, theName, outValue, theParam))
76       return true;
77   }
78   return false;
79 }
80
81 static std::map<int, std::vector<int> > myColorMap;
82
83 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
84 {
85   theRGB.push_back(theRed);
86   theRGB.push_back(theGreen);
87   theRGB.push_back(theBlue);
88 }
89
90 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
91 {
92   std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
93   bool isFound = false;
94   for (; anIt != aLast && !isFound; anIt++) {
95     std::vector<int> aValues = anIt->second;
96     isFound = aValues[0] == theValues[0] &&
97               aValues[1] == theValues[1] &&
98               aValues[2] == theValues[2];
99   }
100   return isFound;
101 }
102
103 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
104 {
105   std::vector<int> aRGB;
106   if (theH < 0 || theH > 360 ||
107       theS < 0 || theS > 100 ||
108       theV < 0 || theV > 100)
109     return aRGB;
110
111   int aHi = (int)theH/60;
112
113   double aV = theV;
114   double aVmin = (100 - theS)*theV/100;
115
116   double anA = (theV - aVmin)* (theH % 60) / 60;
117
118   double aVinc = aVmin + anA;
119   double aVdec = theV - anA;
120
121   double aPercentToValue = 255./100;
122   int aV_int    = (int)(aV*aPercentToValue);
123   int aVinc_int = (int)(aVinc*aPercentToValue);
124   int aVmin_int = (int)(aVmin*aPercentToValue);
125   int aVdec_int = (int)(aVdec*aPercentToValue);
126
127   switch(aHi) {
128     case 0: appendValues(aRGB, aV_int,    aVinc_int, aVmin_int); break;
129     case 1: appendValues(aRGB, aVdec_int, aV_int,    aVmin_int); break;
130     case 2: appendValues(aRGB, aVmin_int, aV_int,    aVinc_int); break;
131     case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
132     case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
133     case 5: appendValues(aRGB, aV_int,    aVmin_int, aVdec_int); break;
134     default: break;
135   }
136   return aRGB;
137 }
138
139
140 void fillColorMap()
141 {
142   if (!myColorMap.empty())
143     return;
144
145   int i = 0;
146   for (int s = 100; s > 0; s = s - 50)
147   {
148     for (int v = 100; v >= 40; v = v - 20)
149     {
150       for (int h = 0; h < 359 ; h = h + 60)
151       {
152         std::vector<int> aColor = HSVtoRGB(h, s, v);
153         if (containsValues(myColorMap, aColor))
154           continue;
155         myColorMap[i] = aColor;
156         i++;
157       }
158     }
159   }
160 }
161
162 void findRandomColor(std::vector<int>& theValues)
163 {
164   theValues.clear();
165   if (myColorMap.empty()) {
166     fillColorMap();
167   }
168
169   int aSize = myColorMap.size();
170   int anIndex = rand() % aSize;
171   if (myColorMap.find(anIndex) != myColorMap.end()) {
172     theValues = myColorMap.at(anIndex);
173   }
174 }
175
176 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
177 {
178   for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
179     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
180         theMain->object(ModelAPI_ResultPart::group(), a));
181     if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
182       return aPart;
183     }
184   }
185   return ResultPtr();
186 }
187
188 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
189 {
190   if (theFeature.get() && theFeature->data()->isValid()) {
191     const std::set<std::shared_ptr<ModelAPI_Attribute> > aRefs = theFeature->data()->refsToMe();
192     std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
193     for(; aRefIter != aRefs.end(); aRefIter++) {
194       CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
195         ((*aRefIter)->owner());
196       if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
197         return aComp;
198     }
199   }
200   return CompositeFeaturePtr(); // not found
201 }
202
203 } // namespace ModelAPI_Tools