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)
22 return theResult->shape();
25 const char* toString(ModelAPI_ExecState theExecState)
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.";
39 std::string getFeatureError(const FeaturePtr& theFeature)
42 if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
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)
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 );
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);
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;
80 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup, const std::string& theName)
82 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
83 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
84 if (anObject->data()->name() == theName)
91 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
92 const std::string& theName, double& outValue, ResultParameterPtr& theParam)
94 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
95 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
98 // avoid usage of parameters created later than the initial parameter
99 if (theSearcher.get() && theDocument->isLater(theDocument->feature(theParam), theSearcher))
101 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
102 outValue = aValueAttribute->value();
106 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue, ResultParameterPtr& theParam,
107 const DocumentPtr& theDocument)
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))
114 DocumentPtr aRootDocument = aSession->moduleDocument();
115 if (aDocument != aRootDocument) {
116 ResultPtr aPartResult = findPartResult(aRootDocument, aDocument);
117 if (aPartResult.get()) {
118 FeaturePtr aPartFeature = aRootDocument->feature(aPartResult);
119 if (findVariable(aRootDocument, aPartFeature, theName, outValue, theParam))
126 static std::map<int, std::vector<int> > myColorMap;
128 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
130 theRGB.push_back(theRed);
131 theRGB.push_back(theGreen);
132 theRGB.push_back(theBlue);
135 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
137 std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
138 bool isFound = false;
139 for (; anIt != aLast && !isFound; anIt++) {
140 std::vector<int> aValues = anIt->second;
141 isFound = aValues[0] == theValues[0] &&
142 aValues[1] == theValues[1] &&
143 aValues[2] == theValues[2];
148 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
150 std::vector<int> aRGB;
151 if (theH < 0 || theH > 360 ||
152 theS < 0 || theS > 100 ||
153 theV < 0 || theV > 100)
156 int aHi = (int)theH/60;
159 double aVmin = (100 - theS)*theV/100;
161 double anA = (theV - aVmin)* (theH % 60) / 60;
163 double aVinc = aVmin + anA;
164 double aVdec = theV - anA;
166 double aPercentToValue = 255./100;
167 int aV_int = (int)(aV*aPercentToValue);
168 int aVinc_int = (int)(aVinc*aPercentToValue);
169 int aVmin_int = (int)(aVmin*aPercentToValue);
170 int aVdec_int = (int)(aVdec*aPercentToValue);
173 case 0: appendValues(aRGB, aV_int, aVinc_int, aVmin_int); break;
174 case 1: appendValues(aRGB, aVdec_int, aV_int, aVmin_int); break;
175 case 2: appendValues(aRGB, aVmin_int, aV_int, aVinc_int); break;
176 case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
177 case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
178 case 5: appendValues(aRGB, aV_int, aVmin_int, aVdec_int); break;
187 if (!myColorMap.empty())
191 for (int s = 100; s > 0; s = s - 50)
193 for (int v = 100; v >= 40; v = v - 20)
195 for (int h = 0; h < 359 ; h = h + 60)
197 std::vector<int> aColor = HSVtoRGB(h, s, v);
198 if (containsValues(myColorMap, aColor))
200 myColorMap[i] = aColor;
207 void findRandomColor(std::vector<int>& theValues)
210 if (myColorMap.empty()) {
214 size_t aSize = myColorMap.size();
215 int anIndex = rand() % aSize;
216 if (myColorMap.find(anIndex) != myColorMap.end()) {
217 theValues = myColorMap.at(anIndex);
221 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
223 if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
224 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
225 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
226 theMain->object(ModelAPI_ResultPart::group(), a));
227 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
235 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
237 if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
238 for (int a = theMain->size(ModelAPI_Feature::group()) - 1; a >= 0; a--) {
239 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
240 theMain->object(ModelAPI_Feature::group(), a));
241 if (aPartFeat.get()) {
242 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
243 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
244 for(; aRes != aResList.end(); aRes++) {
245 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
247 if (aPart->isActivated() && aPart->partDoc() == theSub)
249 } else break; // if the first is not Part, others are also not
257 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
259 if (theFeature.get() && theFeature->data()->isValid()) {
260 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
261 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
262 for(; aRefIter != aRefs.end(); aRefIter++) {
263 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
264 ((*aRefIter)->owner());
265 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
269 return CompositeFeaturePtr(); // not found
272 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
274 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
276 FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
277 if (aFeatureOwner.get()) {
278 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
279 aFeatureOwner->results().cbegin();
280 for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
281 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
282 if (aComp && aComp->isSub(aBody))
287 return ResultCompSolidPtr(); // not found
290 bool hasSubResults(const ResultPtr& theResult)
292 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
293 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
296 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
298 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
299 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
300 for (; aRIter != aResults.cend(); aRIter++) {
301 theResults.push_back(*aRIter);
302 // iterate sub-bodies of compsolid
303 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
305 int aNumSub = aComp->numberOfSubs();
306 for(int a = 0; a < aNumSub; a++) {
307 theResults.push_back(aComp->subResult(a));
313 } // namespace ModelAPI_Tools