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 ResultPtr aPartResult = findPartResult(aRootDocument, aDocument);
163 if (aPartResult.get()) {
164 FeaturePtr aPartFeature;
165 if (theSearcher.get()) // only if the relative search is needed
166 aPartFeature = aRootDocument->feature(aPartResult);
167 if (findVariable(aRootDocument, aPartFeature, theName, outValue, theParam))
174 static std::map<int, std::vector<int> > myColorMap;
176 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
178 theRGB.push_back(theRed);
179 theRGB.push_back(theGreen);
180 theRGB.push_back(theBlue);
183 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
185 std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
186 bool isFound = false;
187 for (; anIt != aLast && !isFound; anIt++) {
188 std::vector<int> aValues = anIt->second;
189 isFound = aValues[0] == theValues[0] &&
190 aValues[1] == theValues[1] &&
191 aValues[2] == theValues[2];
196 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
198 std::vector<int> aRGB;
199 if (theH < 0 || theH > 360 ||
200 theS < 0 || theS > 100 ||
201 theV < 0 || theV > 100)
204 int aHi = (int)theH/60;
207 double aVmin = (100 - theS)*theV/100;
209 double anA = (theV - aVmin)* (theH % 60) / 60;
211 double aVinc = aVmin + anA;
212 double aVdec = theV - anA;
214 double aPercentToValue = 255./100;
215 int aV_int = (int)(aV*aPercentToValue);
216 int aVinc_int = (int)(aVinc*aPercentToValue);
217 int aVmin_int = (int)(aVmin*aPercentToValue);
218 int aVdec_int = (int)(aVdec*aPercentToValue);
221 case 0: appendValues(aRGB, aV_int, aVinc_int, aVmin_int); break;
222 case 1: appendValues(aRGB, aVdec_int, aV_int, aVmin_int); break;
223 case 2: appendValues(aRGB, aVmin_int, aV_int, aVinc_int); break;
224 case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
225 case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
226 case 5: appendValues(aRGB, aV_int, aVmin_int, aVdec_int); break;
235 if (!myColorMap.empty())
239 for (int s = 100; s > 0; s = s - 50)
241 for (int v = 100; v >= 40; v = v - 20)
243 for (int h = 0; h < 359 ; h = h + 60)
245 std::vector<int> aColor = HSVtoRGB(h, s, v);
246 if (containsValues(myColorMap, aColor))
248 myColorMap[i] = aColor;
255 void findRandomColor(std::vector<int>& theValues)
258 if (myColorMap.empty()) {
262 size_t aSize = myColorMap.size();
263 int anIndex = rand() % aSize;
264 if (myColorMap.find(anIndex) != myColorMap.end()) {
265 theValues = myColorMap.at(anIndex);
269 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
271 if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
272 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
273 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
274 theMain->object(ModelAPI_ResultPart::group(), a));
275 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
283 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
285 if (theMain != theSub) { // to optimize and avoid of crash on partset document close (don't touch the sub-document structure)
286 for (int a = theMain->size(ModelAPI_Feature::group()) - 1; a >= 0; a--) {
287 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
288 theMain->object(ModelAPI_Feature::group(), a));
289 if (aPartFeat.get()) {
290 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
291 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
292 for(; aRes != aResList.end(); aRes++) {
293 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
295 if (aPart->isActivated() && aPart->partDoc() == theSub)
297 } else break; // if the first is not Part, others are also not
305 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
307 if (theFeature.get() && theFeature->data()->isValid()) {
308 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
309 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
310 for(; aRefIter != aRefs.end(); aRefIter++) {
311 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
312 ((*aRefIter)->owner());
313 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
317 return CompositeFeaturePtr(); // not found
320 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
322 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
324 FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
325 if (aFeatureOwner.get()) {
326 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
327 aFeatureOwner->results().cbegin();
328 for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
329 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
330 if (aComp && aComp->isSub(aBody))
335 return ResultCompSolidPtr(); // not found
338 bool hasSubResults(const ResultPtr& theResult)
340 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
341 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
344 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
346 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
347 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
348 for (; aRIter != aResults.cend(); aRIter++) {
349 theResults.push_back(*aRIter);
350 // iterate sub-bodies of compsolid
351 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
353 int aNumSub = aComp->numberOfSubs();
354 for(int a = 0; a < aNumSub; a++) {
355 theResults.push_back(aComp->subResult(a));
361 //******************************************************************
362 bool allDocumentsActivated(std::string& theNotActivatedNames)
364 theNotActivatedNames = "";
365 bool anAllPartActivated = true;
367 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
368 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
369 for (int i = 0; i < aSize; i++) {
370 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
371 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
372 if (!aPart->isActivated()) {
373 anAllPartActivated = false;
374 if (!theNotActivatedNames.empty())
375 theNotActivatedNames += ", ";
376 theNotActivatedNames += aObject->data()->name().c_str();
379 return anAllPartActivated;
382 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
383 const bool theFlushRedisplay,
384 const bool theUseComposite,
385 const bool theUseRecursion)
387 #ifdef DEBUG_REMOVE_FEATURES
388 printListInfo(theFeatures, "selection: ");
391 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
392 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
393 #ifdef DEBUG_REMOVE_FEATURES
394 printMapInfo(aReferences, "allDependencies: ");
397 std::set<FeaturePtr> aFeaturesRefsTo;
398 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
399 #ifdef DEBUG_REMOVE_FEATURES
400 printListInfo(aFeaturesRefsTo, "references: ");
403 std::set<FeaturePtr> aFeatures = theFeatures;
404 if (!aFeaturesRefsTo.empty())
405 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
406 #ifdef DEBUG_REMOVE_FEATURES
407 printListInfo(aFeatures, "removeFeatures: ");
410 return ModelAPI_Tools::removeFeatures(aFeatures, false);
413 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
414 const bool theFlushRedisplay)
417 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
418 aLast = theFeatures.end();
419 for (; anIt != aLast; anIt++) {
420 FeaturePtr aFeature = *anIt;
421 if (aFeature.get()) {
422 DocumentPtr aDoc = aFeature->document();
423 // flush REDISPLAY signal after remove feature
424 aDoc->removeFeature(aFeature);
428 if (isDone && theFlushRedisplay) {
429 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
430 // if should be done after removeFeature() of document
431 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
436 // Fills the references list by all references of the feature from the references map.
437 // This is a recusive method to find references by next found feature in the map of references.
438 // \param theFeature a feature to find references
439 // \param theReferencesMap a map of references
440 // \param theReferences an out container of references
441 void addRefsToFeature(const FeaturePtr& theFeature,
442 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
443 std::set<FeaturePtr>& theReferences)
445 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
446 return; // this feature is not in the selection list, so exists without references to it
447 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
449 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
450 aLast = aMainReferences.end();
451 for (; anIt != aLast; anIt++) {
452 FeaturePtr aRefFeature = *anIt;
453 if (theReferences.find(aRefFeature) == theReferences.end())
454 theReferences.insert(aRefFeature);
455 addRefsToFeature(aRefFeature, theReferencesMap, theReferences);
459 // For each feature from the feature list it searches references to the feature and append them
460 // to the references map. This is a recusive method.
461 // \param theFeature a feature to find references
462 // \param theReferencesMap a map of references
463 // \param theReferences an out container of references
464 void findReferences(const std::set<FeaturePtr>& theFeatures,
465 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
466 const bool theUseComposite, const bool theUseRecursion)
468 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
469 aLast = theFeatures.end();
470 for (; anIt != aLast; anIt++) {
471 FeaturePtr aFeature = *anIt;
472 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
473 DocumentPtr aSelFeatureDoc = aFeature->document();
474 std::set<FeaturePtr> aSelRefFeatures;
475 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
476 if (theUseComposite) { // do not filter selection
477 theReferences[aFeature] = aSelRefFeatures;
479 else { // filter references to skip composition features of the current feature
480 std::set<FeaturePtr> aFilteredFeatures;
481 std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
482 aLast = aSelRefFeatures.end();
483 for (; anIt != aLast; anIt++) {
484 FeaturePtr aCFeature = *anIt;
485 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
486 if (aComposite.get() && aComposite->isSub(aFeature))
487 continue; /// composite of the current feature should be skipped
488 aFilteredFeatures.insert(aCFeature);
490 theReferences[aFeature] = aFilteredFeatures;
493 findReferences(aSelRefFeatures, theReferences, theUseComposite, theUseRecursion);
498 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
499 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
500 const bool theUseComposite,
501 const bool theUseRecursion)
503 // For dependencies, find main_list:
504 // sk_1(ext_1, vertex_1)
505 // ext_1(bool_1, sk_3)
510 // Information: bool_1 is not selected, ext_2(bool_2) exists
511 // find all referenced features
512 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
513 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion);
515 #ifdef DEBUG_REMOVE_FEATURES
516 printMapInfo(aMainList, "firstDependencies");
518 // find all dependencies for each object:
519 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
520 // ext_1(bool_1, sk_3)
522 // sk_2(ext_2) + (bool_1)
525 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
526 aMainLast = aMainList.end();
527 for (; aMainIt != aMainLast; aMainIt++) {
528 FeaturePtr aMainListFeature = aMainIt->first;
529 //std::string aName = aMainListFeature->name();
530 std::set<FeaturePtr> aMainRefList = aMainIt->second;
531 std::set<FeaturePtr> anAddRefFeatures;
533 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
534 aLast = aMainRefList.end();
535 for (; anIt != aLast; anIt++) {
536 FeaturePtr aFeature = *anIt;
537 addRefsToFeature(aFeature, aMainList, aMainRefList);
539 theReferences[aMainListFeature] = aMainRefList;
542 #ifdef DEBUG_REMOVE_FEATURES
543 printMapInfo(theReferences, "allDependencies");
547 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
548 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
549 std::set<FeaturePtr>& theFeaturesRefsTo)
551 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
552 aLast = theFeatures.end();
553 for (; anIt != aLast; anIt++) {
554 FeaturePtr aFeature = *anIt;
555 if (theReferences.find(aFeature) == theReferences.end())
557 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
558 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
559 for (; aRefIt != aRefLast; aRefIt++) {
560 FeaturePtr aRefFeature = *aRefIt;
561 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
562 if (aComposite.get() && aComposite->isSub(aFeature))
563 continue; /// composite of the current feature should not be removed
565 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
566 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
567 theFeaturesRefsTo.insert(aRefFeature);
572 } // namespace ModelAPI_Tools