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 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup, const std::string& theName)
42 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
43 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
44 if (anObject->data()->name() == theName)
51 bool findVariable(const DocumentPtr& theDocument,
52 const std::string& theName, double& outValue, ResultParameterPtr& theParam)
54 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
55 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
58 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
59 outValue = aValueAttribute->value();
63 bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam,
64 const DocumentPtr& theDocument /*= DocumentPtr()*/)
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);
74 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
75 if (findVariable(*it, theName, outValue, theParam))
81 static std::map<int, std::vector<int> > myColorMap;
83 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
85 theRGB.push_back(theRed);
86 theRGB.push_back(theGreen);
87 theRGB.push_back(theBlue);
90 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
92 std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
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];
103 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
105 std::vector<int> aRGB;
106 if (theH < 0 || theH > 360 ||
107 theS < 0 || theS > 100 ||
108 theV < 0 || theV > 100)
111 int aHi = (int)theH/60;
114 double aVmin = (100 - theS)*theV/100;
116 double anA = (theV - aVmin)* (theH % 60) / 60;
118 double aVinc = aVmin + anA;
119 double aVdec = theV - anA;
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);
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;
142 if (!myColorMap.empty())
146 for (int s = 100; s > 0; s = s - 50)
148 for (int v = 100; v >= 40; v = v - 20)
150 for (int h = 0; h < 359 ; h = h + 60)
152 std::vector<int> aColor = HSVtoRGB(h, s, v);
153 if (containsValues(myColorMap, aColor))
155 myColorMap[i] = aColor;
162 void findRandomColor(std::vector<int>& theValues)
165 if (myColorMap.empty()) {
169 int aSize = myColorMap.size();
170 int anIndex = rand() % aSize;
171 if (myColorMap.find(anIndex) != myColorMap.end()) {
172 theValues = myColorMap.at(anIndex);
176 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
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) {
188 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
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))
200 return CompositeFeaturePtr(); // not found
203 } // namespace ModelAPI_Tools