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_CompositeFeature.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Object.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_ResultCompSolid.h>
14 #include <ModelAPI_ResultParameter.h>
15 #include <ModelAPI_ResultPart.h>
16 #include <ModelAPI_AttributeDocRef.h>
17 #include <ModelAPI_Validator.h>
22 #include <Events_Loop.h>
23 #include <ModelAPI_Events.h>
25 #define RECURSE_TOP_LEVEL 50
27 //#define DEBUG_REMOVE_FEATURES
28 //#define DEBUG_REMOVE_FEATURES_RECURSE
29 //#define DEBUG_CYCLING_1550
31 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
33 std::string getFeatureInfo(FeaturePtr theFeature)
35 if (!theFeature.get())
37 //std::ostringstream aPtrStr;
38 //aPtrStr << "[" << theFeature.get() << "] ";
39 std::string aFeatureInfo = /*aPtrStr.str() + */theFeature->name();
40 CompositeFeaturePtr aComposite = ModelAPI_Tools::compositeOwner(theFeature);
41 if (aComposite.get()) {
42 aFeatureInfo = aFeatureInfo + "[in " + aComposite->name() + "]";
48 #ifdef DEBUG_REMOVE_FEATURES
49 void printMapInfo(const std::map<FeaturePtr, std::set<FeaturePtr> >& theMainList,
50 const std::string& thePrefix)
52 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = theMainList.begin(),
53 aMainLast = theMainList.end();
55 for (; aMainIt != aMainLast; aMainIt++) {
56 FeaturePtr aMainListFeature = aMainIt->first;
57 std::set<FeaturePtr> aMainRefList = aMainIt->second;
58 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(), aLast = aMainRefList.end();
59 std::string aRefsInfo;
60 for (; anIt != aLast; anIt++) {
61 aRefsInfo += (*anIt)->name().c_str();
65 if (!aRefsInfo.empty()) {
66 anInfo = anInfo + aMainListFeature->name().c_str() + ": " + aRefsInfo + "\n";
69 std::cout << thePrefix.c_str() << " [feature: references to]: \n" << anInfo.c_str() << std::endl;
72 void printListInfo(const std::set<FeaturePtr>& theMainList,
73 const std::string& thePrefix)
75 std::set<FeaturePtr>::const_iterator aMainIt = theMainList.begin(),
76 aMainLast = theMainList.end();
78 for (; aMainIt != aMainLast; aMainIt++) {
79 FeaturePtr aRefFeature = *aMainIt;
80 anInfo += aRefFeature->name().c_str();
81 if (aMainIt != aMainLast)
84 std::cout << thePrefix.c_str() << ": " << anInfo.c_str() << std::endl;
88 namespace ModelAPI_Tools {
90 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
92 return theResult->shape();
95 const char* toString(ModelAPI_ExecState theExecState)
97 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
98 switch (theExecState) {
99 TO_STRING(ModelAPI_StateDone)
100 TO_STRING(ModelAPI_StateMustBeUpdated)
101 TO_STRING(ModelAPI_StateExecFailed)
102 TO_STRING(ModelAPI_StateInvalidArgument)
103 TO_STRING(ModelAPI_StateNothing)
104 default: return "Unknown ExecState.";
109 std::string getFeatureError(const FeaturePtr& theFeature)
112 if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
115 // to be removed later, this error should be got from the feature
116 if (theFeature->data()->execState() == ModelAPI_StateDone ||
117 theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
120 // set error indication
121 anError = theFeature->error();
122 if (anError.empty()) {
123 bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
124 || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
126 anError = toString(theFeature->data()->execState());
127 // If the feature is Composite and error is StateInvalidArgument,
128 // error text should include error of first invalid sub-feature. Otherwise
129 // it is not clear what is the reason of the invalid argument.
130 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
131 CompositeFeaturePtr aComposite =
132 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
134 for (int i = 0, aSize = aComposite->numberOfSubs(); i < aSize; i++) {
135 FeaturePtr aSubFeature = aComposite->subFeature(i);
136 std::string aSubFeatureError = getFeatureError(aSubFeature);
137 if (!aSubFeatureError.empty()) {
138 anError = anError + " in " + aSubFeature->getKind() + ".\n" + aSubFeatureError;
150 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup,
151 const std::string& theName)
153 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
154 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
155 if (anObject->data()->name() == theName)
162 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
163 const std::string& theName, double& outValue, ResultParameterPtr& theParam)
165 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
166 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
169 // avoid usage of parameters created later than the initial parameter
170 if (theSearcher.get() && theDocument->isLater(theDocument->feature(theParam), theSearcher))
172 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
173 outValue = aValueAttribute->value();
177 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue,
178 ResultParameterPtr& theParam, const DocumentPtr& theDocument)
180 SessionPtr aSession = ModelAPI_Session::get();
181 std::list<DocumentPtr> aDocList;
182 DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
183 if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
185 DocumentPtr aRootDocument = aSession->moduleDocument();
186 if (aDocument != aRootDocument) {
187 // any parameters in PartSet is okindependently on the Part position (issu #1504)
188 if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
194 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
196 // to optimize and avoid of crash on partset document close
197 // (don't touch the sub-document structure)
198 if (theMain != theSub) {
199 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
200 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
201 theMain->object(ModelAPI_ResultPart::group(), a));
202 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
210 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
212 // to optimize and avoid of crash on partset document close
213 // (don't touch the sub-document structure)
214 if (theMain != theSub) {
215 // iteration from top to bottom to avoid finding the movement documents before the original
216 int aSize = theMain->size(ModelAPI_Feature::group());
217 for (int a = 0; a < aSize; a++) {
218 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
219 theMain->object(ModelAPI_Feature::group(), a));
220 if (aPartFeat.get()) {
221 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
222 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
223 for(; aRes != aResList.end(); aRes++) {
224 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
226 if (aPart->isActivated() && aPart->partDoc() == theSub)
228 } else break; // if the first is not Part, others are also not
236 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
238 if (theFeature.get() && theFeature->data()->isValid()) {
239 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
240 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
241 for(; aRefIter != aRefs.end(); aRefIter++) {
242 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
243 ((*aRefIter)->owner());
244 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
248 return CompositeFeaturePtr(); // not found
251 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
253 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
255 FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
256 if (aFeatureOwner.get()) {
257 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
258 aFeatureOwner->results().cbegin();
259 for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
260 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
261 if (aComp && aComp->isSub(aBody))
266 return ResultCompSolidPtr(); // not found
269 bool hasSubResults(const ResultPtr& theResult)
271 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
272 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
275 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
277 if (!theFeature.get()) // safety: for empty feature no results
279 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
280 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
281 for (; aRIter != aResults.cend(); aRIter++) {
282 theResults.push_back(*aRIter);
283 // iterate sub-bodies of compsolid
284 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
286 int aNumSub = aComp->numberOfSubs();
287 for(int a = 0; a < aNumSub; a++) {
288 theResults.push_back(aComp->subResult(a));
294 //******************************************************************
295 bool allDocumentsActivated(std::string& theNotActivatedNames)
297 theNotActivatedNames = "";
298 bool anAllPartActivated = true;
300 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
301 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
302 for (int i = 0; i < aSize; i++) {
303 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
304 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
305 if (!aPart->isActivated()) {
306 anAllPartActivated = false;
307 if (!theNotActivatedNames.empty())
308 theNotActivatedNames += ", ";
309 theNotActivatedNames += aObject->data()->name().c_str();
312 return anAllPartActivated;
315 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
316 const bool theFlushRedisplay,
317 const bool theUseComposite,
318 const bool theUseRecursion)
320 #ifdef DEBUG_REMOVE_FEATURES
321 printListInfo(theFeatures, "selection: ");
324 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
325 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
326 #ifdef DEBUG_REMOVE_FEATURES
327 printMapInfo(aReferences, "allDependencies: ");
330 std::set<FeaturePtr> aFeaturesRefsTo;
331 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
332 #ifdef DEBUG_REMOVE_FEATURES
333 printListInfo(aFeaturesRefsTo, "references: ");
336 std::set<FeaturePtr> aFeatures = theFeatures;
337 if (!aFeaturesRefsTo.empty())
338 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
339 #ifdef DEBUG_REMOVE_FEATURES
340 printListInfo(aFeatures, "removeFeatures: ");
343 return ModelAPI_Tools::removeFeatures(aFeatures, false);
346 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
347 const bool theFlushRedisplay)
350 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
351 aLast = theFeatures.end();
352 for (; anIt != aLast; anIt++) {
353 FeaturePtr aFeature = *anIt;
354 if (aFeature.get()) {
355 DocumentPtr aDoc = aFeature->document();
356 // flush REDISPLAY signal after remove feature
357 aDoc->removeFeature(aFeature);
361 if (isDone && theFlushRedisplay) {
362 // the redisplay signal should be flushed in order to erase
363 // the feature presentation in the viewer
364 // if should be done after removeFeature() of document
365 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
370 // Fills the references list by all references of the feature from the references map.
371 // This is a recusive method to find references by next found feature in the map of references.
372 // \param theFeature a feature to find references
373 // \param theReferencesMap a map of references
374 // \param theReferences an out container of references
375 void addRefsToFeature(const FeaturePtr& theFeature,
376 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
377 std::map<FeaturePtr, std::set<FeaturePtr> >& theProcessedReferences,
379 std::set<FeaturePtr>& theReferences)
381 if (theRecLevel > RECURSE_TOP_LEVEL)
385 // if the feature is already processed, get the ready references from the map
386 if (theProcessedReferences.find(theFeature) != theProcessedReferences.end()) {
387 std::set<FeaturePtr> aReferences = theProcessedReferences.at(theFeature);
388 theReferences.insert(aReferences.begin(), aReferences.end());
392 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
393 return; // this feature is not in the selection list, so exists without references to it
394 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
396 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
397 aLast = aMainReferences.end();
398 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
399 std::string aSpacing;
400 for (int i = 0; i < theRecLevel; i++)
401 aSpacing.append(" ");
404 for (; anIt != aLast; anIt++) {
405 FeaturePtr aRefFeature = *anIt;
406 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
407 std::cout << aSpacing << " Ref: " << getFeatureInfo(aRefFeature) << std::endl;
409 if (theReferences.find(aRefFeature) == theReferences.end())
410 theReferences.insert(aRefFeature);
411 addRefsToFeature(aRefFeature, theReferencesMap, theProcessedReferences,
412 theRecLevel, theReferences);
416 // For each feature from the feature list it searches references to the feature and append them
417 // to the references map. This is a recusive method.
418 // \param theFeature a feature to find references
419 // \param theReferencesMap a map of references
420 // \param theReferences an out container of references
421 void findReferences(const std::set<FeaturePtr>& theFeatures,
422 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
423 const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
425 if (theRecLevel > RECURSE_TOP_LEVEL)
428 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
429 aLast = theFeatures.end();
430 for (; anIt != aLast; anIt++) {
431 FeaturePtr aFeature = *anIt;
432 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
433 DocumentPtr aSelFeatureDoc = aFeature->document();
434 std::set<FeaturePtr> aSelRefFeatures;
435 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
436 if (theUseComposite) { // do not filter selection
437 theReferences[aFeature] = aSelRefFeatures;
439 else { // filter references to skip composition features of the current feature
440 std::set<FeaturePtr> aFilteredFeatures;
441 std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
442 aLast = aSelRefFeatures.end();
443 for (; anIt != aLast; anIt++) {
444 FeaturePtr aCFeature = *anIt;
445 CompositeFeaturePtr aComposite =
446 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
447 if (aComposite.get() && aComposite->isSub(aFeature))
448 continue; /// composite of the current feature should be skipped
449 aFilteredFeatures.insert(aCFeature);
451 theReferences[aFeature] = aFilteredFeatures;
453 if (theUseRecursion) {
454 #ifdef DEBUG_CYCLING_1550
455 findReferences(aSelRefFeatures, theReferences, theUseComposite,
456 theUseRecursion, theRecLevel);
458 findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
466 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
467 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
468 const bool theUseComposite,
469 const bool theUseRecursion)
471 // For dependencies, find main_list:
472 // sk_1(ext_1, vertex_1)
473 // ext_1(bool_1, sk_3)
478 // Information: bool_1 is not selected, ext_2(bool_2) exists
479 // find all referenced features
480 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
482 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
484 #ifdef DEBUG_REMOVE_FEATURES
485 printMapInfo(aMainList, "firstDependencies");
487 // find all dependencies for each object:
488 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
489 // ext_1(bool_1, sk_3)
491 // sk_2(ext_2) + (bool_1)
494 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
495 aMainLast = aMainList.end();
496 for (; aMainIt != aMainLast; aMainIt++) {
497 FeaturePtr aMainListFeature = aMainIt->first;
499 //std::string aName = aMainListFeature->name();
500 std::set<FeaturePtr> aMainRefList = aMainIt->second;
502 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
504 int n = sprintf(aBuf, "%d", aMainRefList.size());
505 std::string aSize(aBuf);
506 std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
507 << ", references size = " << aSize << std::endl;
509 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
510 aLast = aMainRefList.end();
511 std::set<FeaturePtr> aResultRefList;
512 aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
513 for (; anIt != aLast; anIt++) {
514 FeaturePtr aFeature = *anIt;
516 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
517 std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
520 addRefsToFeature(aFeature, aMainList, theReferences,
521 aRecLevel, aResultRefList/*aMainRefList*/);
523 theReferences[aMainListFeature] = aResultRefList;
525 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
526 std::cout << std::endl;
529 #ifdef DEBUG_REMOVE_FEATURES
530 printMapInfo(theReferences, "allDependencies");
534 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
535 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
536 std::set<FeaturePtr>& theFeaturesRefsTo)
538 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
539 aLast = theFeatures.end();
540 for (; anIt != aLast; anIt++) {
541 FeaturePtr aFeature = *anIt;
542 if (theReferences.find(aFeature) == theReferences.end())
544 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
545 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
546 for (; aRefIt != aRefLast; aRefIt++) {
547 FeaturePtr aRefFeature = *aRefIt;
548 CompositeFeaturePtr aComposite =
549 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
550 if (aComposite.get() && aComposite->isSub(aFeature))
551 continue; /// composite of the current feature should not be removed
553 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
554 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
555 theFeaturesRefsTo.insert(aRefFeature);
560 void getConcealedResults(const FeaturePtr& theFeature,
561 std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
563 SessionPtr aSession = ModelAPI_Session::get();
565 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
566 theFeature->data()->referencesToObjects(aRefs);
567 std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
568 anIt = aRefs.begin(), aLast = aRefs.end();
569 std::set<ResultPtr> alreadyThere; // to avoid duplications
570 for (; anIt != aLast; anIt++) {
571 if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
572 continue; // use only concealed attributes
573 std::list<ObjectPtr> anObjects = (*anIt).second;
574 std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
575 for (; anOIt != anOLast; anOIt++) {
576 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
577 if (aResult && aResult->isConcealed()) {
578 if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
579 alreadyThere.insert(aResult);
581 theResults.push_back(aResult);
587 } // namespace ModelAPI_Tools