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>
13 #include <ModelAPI_ResultPart.h>
14 #include <ModelAPI_AttributeDocRef.h>
18 namespace ModelAPI_Tools {
20 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
23 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
25 return aBody->shape();
27 ResultConstructionPtr aConstruct = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
30 return aConstruct->shape();
32 ResultGroupPtr aGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(theResult);
34 return aGroup->shape();
35 return std::shared_ptr<GeomAPI_Shape>();
37 return theResult->shape();
40 bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam)
42 SessionPtr aSession = ModelAPI_Session::get();
43 std::list<DocumentPtr> aDocList;
44 DocumentPtr aDocument = aSession->activeDocument();
45 DocumentPtr aRootDocument = aSession->moduleDocument();
46 aDocList.push_back(aDocument);
47 if (aDocument != aRootDocument) {
48 aDocList.push_back(aRootDocument);
50 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
51 ObjectPtr aParamObj = (*it)->objectByName(ModelAPI_ResultParameter::group(), theName);
52 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
55 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
56 outValue = aValueAttribute->value();
62 static std::map<int, std::vector<int> > myColorMap;
64 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
66 theRGB.push_back(theRed);
67 theRGB.push_back(theGreen);
68 theRGB.push_back(theBlue);
71 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
73 std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
75 for (; anIt != aLast && !isFound; anIt++) {
76 std::vector<int> aValues = anIt->second;
77 isFound = aValues[0] == theValues[0] &&
78 aValues[1] == theValues[1] &&
79 aValues[2] == theValues[2];
84 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
86 std::vector<int> aRGB;
87 if (theH < 0 || theH > 360 ||
88 theS < 0 || theS > 100 ||
89 theV < 0 || theV > 100)
92 int aHi = (int)theH/60;
95 double aVmin = (100 - theS)*theV/100;
97 double anA = (theV - aVmin)* (theH % 60) / 60;
99 double aVinc = aVmin + anA;
100 double aVdec = theV - anA;
102 double aPercentToValue = 255./100;
103 int aV_int = (int)(aV*aPercentToValue);
104 int aVinc_int = (int)(aVinc*aPercentToValue);
105 int aVmin_int = (int)(aVmin*aPercentToValue);
106 int aVdec_int = (int)(aVdec*aPercentToValue);
109 case 0: appendValues(aRGB, aV_int, aVinc_int, aVmin_int); break;
110 case 1: appendValues(aRGB, aVdec_int, aV_int, aVmin_int); break;
111 case 2: appendValues(aRGB, aVmin_int, aV_int, aVinc_int); break;
112 case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
113 case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
114 case 5: appendValues(aRGB, aV_int, aVmin_int, aVdec_int); break;
123 if (!myColorMap.empty())
127 for (int s = 100; s > 0; s = s - 50)
129 for (int v = 100; v >= 40; v = v - 20)
131 for (int h = 0; h < 359 ; h = h + 60)
133 std::vector<int> aColor = HSVtoRGB(h, s, v);
134 if (containsValues(myColorMap, aColor))
136 myColorMap[i] = aColor;
143 void findRandomColor(std::vector<int>& theValues)
146 if (myColorMap.empty()) {
150 int aSize = myColorMap.size();
151 int anIndex = rand() % aSize;
152 if (myColorMap.find(anIndex) != myColorMap.end()) {
153 theValues = myColorMap.at(anIndex);
157 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
159 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
160 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
161 theMain->object(ModelAPI_ResultPart::group(), a));
162 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
169 } // namespace ModelAPI_Tools