1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_Tools.cpp
4 // Created: 06 Aug 2014
5 // Author: Vitaly Smetannikov
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>
17 namespace ModelAPI_Tools {
19 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
22 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
24 return aBody->shape();
26 ResultConstructionPtr aConstruct = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
29 return aConstruct->shape();
31 ResultGroupPtr aGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(theResult);
33 return aGroup->shape();
34 return std::shared_ptr<GeomAPI_Shape>();
36 return theResult->shape();
39 bool findVariable(const std::string& theName, double& outValue)
41 SessionPtr aSession = ModelAPI_Session::get();
42 std::list<DocumentPtr> aDocList;
43 DocumentPtr aDocument = aSession->activeDocument();
44 DocumentPtr aRootDocument = aSession->moduleDocument();
45 aDocList.push_back(aDocument);
46 if (aDocument != aRootDocument) {
47 aDocList.push_back(aRootDocument);
49 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
50 ObjectPtr aParamObj = (*it)->objectByName(ModelAPI_ResultParameter::group(), theName);
51 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
54 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
55 outValue = aValueAttribute->value();
61 static std::map<int, std::vector<int> > myColorMap;
63 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
65 theRGB.push_back(theRed);
66 theRGB.push_back(theGreen);
67 theRGB.push_back(theBlue);
70 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
72 std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
74 for (; anIt != aLast && !isFound; anIt++) {
75 std::vector<int> aValues = anIt->second;
76 isFound = aValues[0] == theValues[0] &&
77 aValues[1] == theValues[1] &&
78 aValues[2] == theValues[2];
83 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
85 std::vector<int> aRGB;
86 if (theH < 0 || theH > 360 ||
87 theS < 0 || theS > 100 ||
88 theV < 0 || theV > 100)
91 int aHi = (int)theH/60;
94 double aVmin = (100 - theS)*theV/100;
96 double anA = (theV - aVmin)* (theH % 60) / 60;
98 double aVinc = aVmin + anA;
99 double aVdec = theV - anA;
101 double aPercentToValue = 255./100;
102 aV *= aPercentToValue;
103 aVinc *= aPercentToValue;
104 aVmin *= aPercentToValue;
105 aVdec *= aPercentToValue;
108 case 0: appendValues(aRGB, aV, aVinc, aVmin); break;
109 case 1: appendValues(aRGB, aVdec, aV, aVmin); break;
110 case 2: appendValues(aRGB, aVmin, aV, aVinc); break;
111 case 3: appendValues(aRGB, aVmin, aVdec, aV); break;
112 case 4: appendValues(aRGB, aVinc, aVmin, aV); break;
113 case 5: appendValues(aRGB, aV, aVmin, aVdec); break;
122 if (!myColorMap.empty())
126 for (int s = 100; s > 0; s = s - 50)
128 for (int v = 100; v >= 40; v = v - 20)
130 for (int h = 0; h < 359 ; h = h + 60)
132 std::vector<int> aColor = HSVtoRGB(h, s, v);
133 if (containsValues(myColorMap, aColor))
135 myColorMap[i] = aColor;
142 void findRandomColor(std::vector<int>& theValues)
145 if (myColorMap.empty()) {
149 int aSize = myColorMap.size();
150 int anIndex = rand() % aSize;
151 if (myColorMap.find(anIndex) != myColorMap.end()) {
152 theValues = myColorMap.at(anIndex);
156 } // namespace ModelAPI_Tools