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>
19 #include <Events_Loop.h>
20 #include <ModelAPI_Events.h>
22 //#define DEBUG_REMOVE_FEATURES
24 #ifdef DEBUG_REMOVE_FEATURES
25 void printMapInfo(const std::map<FeaturePtr, std::set<FeaturePtr> >& theMainList,
26 const std::string& thePrefix)
28 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = theMainList.begin(),
29 aMainLast = theMainList.end();
31 for (; aMainIt != aMainLast; aMainIt++) {
32 FeaturePtr aMainListFeature = aMainIt->first;
33 std::set<FeaturePtr> aMainRefList = aMainIt->second;
34 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(), aLast = aMainRefList.end();
35 std::string aRefsInfo;
36 for (; anIt != aLast; anIt++) {
37 aRefsInfo += (*anIt)->name().c_str();
41 if (!aRefsInfo.empty()) {
42 anInfo = anInfo + aMainListFeature->name().c_str() + ": " + aRefsInfo + "\n";
45 std::cout << thePrefix.c_str() << ": " << anInfo.c_str() << std::endl;
48 void printListInfo(const std::set<FeaturePtr>& theMainList,
49 const std::string& thePrefix)
51 std::set<FeaturePtr>::const_iterator aMainIt = theMainList.begin(),
52 aMainLast = theMainList.end();
54 for (; aMainIt != aMainLast; aMainIt++) {
55 FeaturePtr aRefFeature = *aMainIt;
56 anInfo += aRefFeature->name().c_str();
57 if (aMainIt != aMainLast)
60 std::cout << thePrefix.c_str() << ": " << anInfo.c_str() << std::endl;
64 namespace ModelAPI_Tools {
66 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
68 return theResult->shape();
71 const char* toString(ModelAPI_ExecState theExecState)
73 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
74 switch (theExecState) {
75 TO_STRING(ModelAPI_StateDone)
76 TO_STRING(ModelAPI_StateMustBeUpdated)
77 TO_STRING(ModelAPI_StateExecFailed)
78 TO_STRING(ModelAPI_StateInvalidArgument)
79 TO_STRING(ModelAPI_StateNothing)
80 default: return "Unknown ExecState.";
85 std::string getFeatureError(const FeaturePtr& theFeature)
88 if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
91 // to be removed later, this error should be got from the feature
92 if (theFeature->data()->execState() == ModelAPI_StateDone ||
93 theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
96 // set error indication
97 anError = theFeature->error();
98 if (anError.empty()) {
99 bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
100 || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
102 anError = toString(theFeature->data()->execState());
103 // If the feature is Composite and error is StateInvalidArgument,
104 // error text should include error of first invalid sub-feature. Otherwise
105 // it is not clear what is the reason of the invalid argument.
106 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
107 CompositeFeaturePtr aComposite =
108 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
110 for (int i = 0, aSize = aComposite->numberOfSubs(); i < aSize; i++) {
111 FeaturePtr aSubFeature = aComposite->subFeature(i);
112 std::string aSubFeatureError = getFeatureError(aSubFeature);
113 if (!aSubFeatureError.empty()) {
114 anError = anError + " in " + aSubFeature->getKind() + ".\n" + aSubFeatureError;
126 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup, const std::string& theName)
128 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
129 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
130 if (anObject->data()->name() == theName)
137 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
138 const std::string& theName, double& outValue, ResultParameterPtr& theParam)
140 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
141 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
144 // avoid usage of parameters created later than the initial parameter
145 if (theSearcher.get() && theDocument->isLater(theDocument->feature(theParam), theSearcher))
147 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
148 outValue = aValueAttribute->value();
152 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue, ResultParameterPtr& theParam,
153 const DocumentPtr& theDocument)
155 SessionPtr aSession = ModelAPI_Session::get();
156 std::list<DocumentPtr> aDocList;
157 DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
158 if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
160 DocumentPtr aRootDocument = aSession->moduleDocument();
161 if (aDocument != aRootDocument) {
162 // any parameters in PartSet is okindependently on the Part position (issu #1504)
163 if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
169 static std::map<int, std::vector<int> > myColorMap;
171 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
173 theRGB.push_back(theRed);
174 theRGB.push_back(theGreen);
175 theRGB.push_back(theBlue);
178 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
180 std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
181 bool isFound = false;
182 for (; anIt != aLast && !isFound; anIt++) {
183 std::vector<int> aValues = anIt->second;
184 isFound = aValues[0] == theValues[0] &&
185 aValues[1] == theValues[1] &&
186 aValues[2] == theValues[2];
191 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
193 std::vector<int> aRGB;
194 if (theH < 0 || theH > 360 ||
195 theS < 0 || theS > 100 ||
196 theV < 0 || theV > 100)
199 int aHi = (int)theH/60;
202 double aVmin = (100 - theS)*theV/100;
204 double anA = (theV - aVmin)* (theH % 60) / 60;
206 double aVinc = aVmin + anA;
207 double aVdec = theV - anA;
209 double aPercentToValue = 255./100;
210 int aV_int = (int)(aV*aPercentToValue);
211 int aVinc_int = (int)(aVinc*aPercentToValue);
212 int aVmin_int = (int)(aVmin*aPercentToValue);
213 int aVdec_int = (int)(aVdec*aPercentToValue);
216 case 0: appendValues(aRGB, aV_int, aVinc_int, aVmin_int); break;
217 case 1: appendValues(aRGB, aVdec_int, aV_int, aVmin_int); break;
218 case 2: appendValues(aRGB, aVmin_int, aV_int, aVinc_int); break;
219 case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
220 case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
221 case 5: appendValues(aRGB, aV_int, aVmin_int, aVdec_int); break;
230 if (!myColorMap.empty())
234 for (int s = 100; s > 0; s = s - 50)
236 for (int v = 100; v >= 40; v = v - 20)
238 for (int h = 0; h < 359 ; h = h + 60)
240 std::vector<int> aColor = HSVtoRGB(h, s, v);
241 if (containsValues(myColorMap, aColor))
243 myColorMap[i] = aColor;
250 void findRandomColor(std::vector<int>& theValues)
253 if (myColorMap.empty()) {
257 size_t aSize = myColorMap.size();
258 int anIndex = rand() % aSize;
259 if (myColorMap.find(anIndex) != myColorMap.end()) {
260 theValues = myColorMap.at(anIndex);
264 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
266 if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
267 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
268 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
269 theMain->object(ModelAPI_ResultPart::group(), a));
270 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
278 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
280 if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
281 for (int a = theMain->size(ModelAPI_Feature::group()) - 1; a >= 0; a--) {
282 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
283 theMain->object(ModelAPI_Feature::group(), a));
284 if (aPartFeat.get()) {
285 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
286 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
287 for(; aRes != aResList.end(); aRes++) {
288 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
290 if (aPart->isActivated() && aPart->partDoc() == theSub)
292 } else break; // if the first is not Part, others are also not
300 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
302 if (theFeature.get() && theFeature->data()->isValid()) {
303 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
304 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
305 for(; aRefIter != aRefs.end(); aRefIter++) {
306 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
307 ((*aRefIter)->owner());
308 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
312 return CompositeFeaturePtr(); // not found
315 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
317 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
319 FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
320 if (aFeatureOwner.get()) {
321 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
322 aFeatureOwner->results().cbegin();
323 for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
324 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
325 if (aComp && aComp->isSub(aBody))
330 return ResultCompSolidPtr(); // not found
333 bool hasSubResults(const ResultPtr& theResult)
335 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
336 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
339 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
341 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
342 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
343 for (; aRIter != aResults.cend(); aRIter++) {
344 theResults.push_back(*aRIter);
345 // iterate sub-bodies of compsolid
346 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
348 int aNumSub = aComp->numberOfSubs();
349 for(int a = 0; a < aNumSub; a++) {
350 theResults.push_back(aComp->subResult(a));
356 //******************************************************************
357 bool allDocumentsActivated(std::string& theNotActivatedNames)
359 theNotActivatedNames = "";
360 bool anAllPartActivated = true;
362 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
363 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
364 for (int i = 0; i < aSize; i++) {
365 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
366 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
367 if (!aPart->isActivated()) {
368 anAllPartActivated = false;
369 if (!theNotActivatedNames.empty())
370 theNotActivatedNames += ", ";
371 theNotActivatedNames += aObject->data()->name().c_str();
374 return anAllPartActivated;
377 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
378 const bool theFlushRedisplay,
379 const bool theUseComposite,
380 const bool theUseRecursion)
382 #ifdef DEBUG_REMOVE_FEATURES
383 printListInfo(theFeatures, "selection: ");
386 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
387 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
388 #ifdef DEBUG_REMOVE_FEATURES
389 printMapInfo(aReferences, "allDependencies: ");
392 std::set<FeaturePtr> aFeaturesRefsTo;
393 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
394 #ifdef DEBUG_REMOVE_FEATURES
395 printListInfo(aFeaturesRefsTo, "references: ");
398 std::set<FeaturePtr> aFeatures = theFeatures;
399 if (!aFeaturesRefsTo.empty())
400 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
401 #ifdef DEBUG_REMOVE_FEATURES
402 printListInfo(aFeatures, "removeFeatures: ");
405 return ModelAPI_Tools::removeFeatures(aFeatures, false);
408 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
409 const bool theFlushRedisplay)
412 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
413 aLast = theFeatures.end();
414 for (; anIt != aLast; anIt++) {
415 FeaturePtr aFeature = *anIt;
416 if (aFeature.get()) {
417 DocumentPtr aDoc = aFeature->document();
418 // flush REDISPLAY signal after remove feature
419 aDoc->removeFeature(aFeature);
423 if (isDone && theFlushRedisplay) {
424 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
425 // if should be done after removeFeature() of document
426 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
431 // Fills the references list by all references of the feature from the references map.
432 // This is a recusive method to find references by next found feature in the map of references.
433 // \param theFeature a feature to find references
434 // \param theReferencesMap a map of references
435 // \param theReferences an out container of references
436 void addRefsToFeature(const FeaturePtr& theFeature,
437 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
438 std::set<FeaturePtr>& theReferences)
440 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
441 return; // this feature is not in the selection list, so exists without references to it
442 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
444 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
445 aLast = aMainReferences.end();
446 for (; anIt != aLast; anIt++) {
447 FeaturePtr aRefFeature = *anIt;
448 if (theReferences.find(aRefFeature) == theReferences.end())
449 theReferences.insert(aRefFeature);
450 addRefsToFeature(aRefFeature, theReferencesMap, theReferences);
454 // For each feature from the feature list it searches references to the feature and append them
455 // to the references map. This is a recusive method.
456 // \param theFeature a feature to find references
457 // \param theReferencesMap a map of references
458 // \param theReferences an out container of references
459 void findReferences(const std::set<FeaturePtr>& theFeatures,
460 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
461 const bool theUseComposite, const bool theUseRecursion)
463 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
464 aLast = theFeatures.end();
465 for (; anIt != aLast; anIt++) {
466 FeaturePtr aFeature = *anIt;
467 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
468 DocumentPtr aSelFeatureDoc = aFeature->document();
469 std::set<FeaturePtr> aSelRefFeatures;
470 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
471 if (theUseComposite) { // do not filter selection
472 theReferences[aFeature] = aSelRefFeatures;
474 else { // filter references to skip composition features of the current feature
475 std::set<FeaturePtr> aFilteredFeatures;
476 std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
477 aLast = aSelRefFeatures.end();
478 for (; anIt != aLast; anIt++) {
479 FeaturePtr aCFeature = *anIt;
480 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
481 if (aComposite.get() && aComposite->isSub(aFeature))
482 continue; /// composite of the current feature should be skipped
483 aFilteredFeatures.insert(aCFeature);
485 theReferences[aFeature] = aFilteredFeatures;
488 findReferences(aSelRefFeatures, theReferences, theUseComposite, theUseRecursion);
493 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
494 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
495 const bool theUseComposite,
496 const bool theUseRecursion)
498 // For dependencies, find main_list:
499 // sk_1(ext_1, vertex_1)
500 // ext_1(bool_1, sk_3)
505 // Information: bool_1 is not selected, ext_2(bool_2) exists
506 // find all referenced features
507 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
508 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion);
510 #ifdef DEBUG_REMOVE_FEATURES
511 printMapInfo(aMainList, "firstDependencies");
513 // find all dependencies for each object:
514 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
515 // ext_1(bool_1, sk_3)
517 // sk_2(ext_2) + (bool_1)
520 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
521 aMainLast = aMainList.end();
522 for (; aMainIt != aMainLast; aMainIt++) {
523 FeaturePtr aMainListFeature = aMainIt->first;
524 //std::string aName = aMainListFeature->name();
525 std::set<FeaturePtr> aMainRefList = aMainIt->second;
526 std::set<FeaturePtr> anAddRefFeatures;
528 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
529 aLast = aMainRefList.end();
530 for (; anIt != aLast; anIt++) {
531 FeaturePtr aFeature = *anIt;
532 addRefsToFeature(aFeature, aMainList, aMainRefList);
534 theReferences[aMainListFeature] = aMainRefList;
537 #ifdef DEBUG_REMOVE_FEATURES
538 printMapInfo(theReferences, "allDependencies");
542 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
543 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
544 std::set<FeaturePtr>& theFeaturesRefsTo)
546 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
547 aLast = theFeatures.end();
548 for (; anIt != aLast; anIt++) {
549 FeaturePtr aFeature = *anIt;
550 if (theReferences.find(aFeature) == theReferences.end())
552 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
553 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
554 for (; aRefIt != aRefLast; aRefIt++) {
555 FeaturePtr aRefFeature = *aRefIt;
556 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
557 if (aComposite.get() && aComposite->isSub(aFeature))
558 continue; /// composite of the current feature should not be removed
560 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
561 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
562 theFeaturesRefsTo.insert(aRefFeature);
567 } // namespace ModelAPI_Tools