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
171 if (theSearcher.get()) {
172 FeaturePtr aParamFeat = theDocument->feature(theParam);
173 if (aParamFeat == theSearcher || theDocument->isLater(aParamFeat, theSearcher))
176 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
177 outValue = aValueAttribute->value();
181 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue,
182 ResultParameterPtr& theParam, const DocumentPtr& theDocument)
184 SessionPtr aSession = ModelAPI_Session::get();
185 std::list<DocumentPtr> aDocList;
186 DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
187 if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
189 DocumentPtr aRootDocument = aSession->moduleDocument();
190 if (aDocument != aRootDocument) {
191 // any parameters in PartSet is okindependently on the Part position (issu #1504)
192 if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
198 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
200 // to optimize and avoid of crash on partset document close
201 // (don't touch the sub-document structure)
202 if (theMain != theSub) {
203 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
204 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
205 theMain->object(ModelAPI_ResultPart::group(), a));
206 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
214 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
216 // to optimize and avoid of crash on partset document close
217 // (don't touch the sub-document structure)
218 if (theMain != theSub) {
219 // iteration from top to bottom to avoid finding the movement documents before the original
220 int aSize = theMain->size(ModelAPI_Feature::group());
221 for (int a = 0; a < aSize; a++) {
222 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
223 theMain->object(ModelAPI_Feature::group(), a));
224 if (aPartFeat.get()) {
225 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
226 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
227 for(; aRes != aResList.end(); aRes++) {
228 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
230 if (aPart->isActivated() && aPart->partDoc() == theSub)
232 } else break; // if the first is not Part, others are also not
240 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
242 if (theFeature.get() && theFeature->data()->isValid()) {
243 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
244 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
245 for(; aRefIter != aRefs.end(); aRefIter++) {
246 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
247 ((*aRefIter)->owner());
248 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
252 return CompositeFeaturePtr(); // not found
255 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
257 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
259 FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
260 if (aFeatureOwner.get()) {
261 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
262 aFeatureOwner->results().cbegin();
263 for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
264 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
265 if (aComp && aComp->isSub(aBody))
270 return ResultCompSolidPtr(); // not found
273 bool hasSubResults(const ResultPtr& theResult)
275 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
276 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
279 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
281 if (!theFeature.get()) // safety: for empty feature no results
283 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
284 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
285 for (; aRIter != aResults.cend(); aRIter++) {
286 theResults.push_back(*aRIter);
287 // iterate sub-bodies of compsolid
288 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
290 int aNumSub = aComp->numberOfSubs();
291 for(int a = 0; a < aNumSub; a++) {
292 theResults.push_back(aComp->subResult(a));
298 //******************************************************************
299 bool allDocumentsActivated(std::string& theNotActivatedNames)
301 theNotActivatedNames = "";
302 bool anAllPartActivated = true;
304 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
305 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
306 for (int i = 0; i < aSize; i++) {
307 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
308 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
309 if (!aPart->isActivated()) {
310 anAllPartActivated = false;
311 if (!theNotActivatedNames.empty())
312 theNotActivatedNames += ", ";
313 theNotActivatedNames += aObject->data()->name().c_str();
316 return anAllPartActivated;
319 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
320 const bool theFlushRedisplay,
321 const bool theUseComposite,
322 const bool theUseRecursion)
324 #ifdef DEBUG_REMOVE_FEATURES
325 printListInfo(theFeatures, "selection: ");
328 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
329 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
330 #ifdef DEBUG_REMOVE_FEATURES
331 printMapInfo(aReferences, "allDependencies: ");
334 std::set<FeaturePtr> aFeaturesRefsTo;
335 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
336 #ifdef DEBUG_REMOVE_FEATURES
337 printListInfo(aFeaturesRefsTo, "references: ");
340 std::set<FeaturePtr> aFeatures = theFeatures;
341 if (!aFeaturesRefsTo.empty())
342 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
343 #ifdef DEBUG_REMOVE_FEATURES
344 printListInfo(aFeatures, "removeFeatures: ");
347 return ModelAPI_Tools::removeFeatures(aFeatures, false);
350 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
351 const bool theFlushRedisplay)
354 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
355 aLast = theFeatures.end();
356 for (; anIt != aLast; anIt++) {
357 FeaturePtr aFeature = *anIt;
358 if (aFeature.get()) {
359 DocumentPtr aDoc = aFeature->document();
360 // flush REDISPLAY signal after remove feature
361 aDoc->removeFeature(aFeature);
365 if (isDone && theFlushRedisplay) {
366 // the redisplay signal should be flushed in order to erase
367 // the feature presentation in the viewer
368 // if should be done after removeFeature() of document
369 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
374 // Fills the references list by all references of the feature from the references map.
375 // This is a recusive method to find references by next found feature in the map of references.
376 // \param theFeature a feature to find references
377 // \param theReferencesMap a map of references
378 // \param theReferences an out container of references
379 void addRefsToFeature(const FeaturePtr& theFeature,
380 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
381 std::map<FeaturePtr, std::set<FeaturePtr> >& theProcessedReferences,
383 std::set<FeaturePtr>& theReferences)
385 if (theRecLevel > RECURSE_TOP_LEVEL)
389 // if the feature is already processed, get the ready references from the map
390 if (theProcessedReferences.find(theFeature) != theProcessedReferences.end()) {
391 std::set<FeaturePtr> aReferences = theProcessedReferences.at(theFeature);
392 theReferences.insert(aReferences.begin(), aReferences.end());
396 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
397 return; // this feature is not in the selection list, so exists without references to it
398 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
400 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
401 aLast = aMainReferences.end();
402 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
403 std::string aSpacing;
404 for (int i = 0; i < theRecLevel; i++)
405 aSpacing.append(" ");
408 for (; anIt != aLast; anIt++) {
409 FeaturePtr aRefFeature = *anIt;
410 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
411 std::cout << aSpacing << " Ref: " << getFeatureInfo(aRefFeature) << std::endl;
413 if (theReferences.find(aRefFeature) == theReferences.end())
414 theReferences.insert(aRefFeature);
415 addRefsToFeature(aRefFeature, theReferencesMap, theProcessedReferences,
416 theRecLevel, theReferences);
420 // For each feature from the feature list it searches references to the feature and append them
421 // to the references map. This is a recusive method.
422 // \param theFeature a feature to find references
423 // \param theReferencesMap a map of references
424 // \param theReferences an out container of references
425 void findReferences(const std::set<FeaturePtr>& theFeatures,
426 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
427 const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
429 if (theRecLevel > RECURSE_TOP_LEVEL)
432 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
433 aLast = theFeatures.end();
434 for (; anIt != aLast; anIt++) {
435 FeaturePtr aFeature = *anIt;
436 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
437 DocumentPtr aSelFeatureDoc = aFeature->document();
438 std::set<FeaturePtr> aSelRefFeatures;
439 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
440 if (theUseComposite) { // do not filter selection
441 theReferences[aFeature] = aSelRefFeatures;
443 else { // filter references to skip composition features of the current feature
444 std::set<FeaturePtr> aFilteredFeatures;
445 std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
446 aLast = aSelRefFeatures.end();
447 for (; anIt != aLast; anIt++) {
448 FeaturePtr aCFeature = *anIt;
449 CompositeFeaturePtr aComposite =
450 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
451 if (aComposite.get() && aComposite->isSub(aFeature))
452 continue; /// composite of the current feature should be skipped
453 aFilteredFeatures.insert(aCFeature);
455 theReferences[aFeature] = aFilteredFeatures;
457 if (theUseRecursion) {
458 #ifdef DEBUG_CYCLING_1550
459 findReferences(aSelRefFeatures, theReferences, theUseComposite,
460 theUseRecursion, theRecLevel);
462 findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
470 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
471 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
472 const bool theUseComposite,
473 const bool theUseRecursion)
475 // For dependencies, find main_list:
476 // sk_1(ext_1, vertex_1)
477 // ext_1(bool_1, sk_3)
482 // Information: bool_1 is not selected, ext_2(bool_2) exists
483 // find all referenced features
484 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
486 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
488 #ifdef DEBUG_REMOVE_FEATURES
489 printMapInfo(aMainList, "firstDependencies");
491 // find all dependencies for each object:
492 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
493 // ext_1(bool_1, sk_3)
495 // sk_2(ext_2) + (bool_1)
498 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
499 aMainLast = aMainList.end();
500 for (; aMainIt != aMainLast; aMainIt++) {
501 FeaturePtr aMainListFeature = aMainIt->first;
503 //std::string aName = aMainListFeature->name();
504 std::set<FeaturePtr> aMainRefList = aMainIt->second;
506 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
508 int n = sprintf(aBuf, "%d", aMainRefList.size());
509 std::string aSize(aBuf);
510 std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
511 << ", references size = " << aSize << std::endl;
513 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
514 aLast = aMainRefList.end();
515 std::set<FeaturePtr> aResultRefList;
516 aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
517 for (; anIt != aLast; anIt++) {
518 FeaturePtr aFeature = *anIt;
520 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
521 std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
524 addRefsToFeature(aFeature, aMainList, theReferences,
525 aRecLevel, aResultRefList/*aMainRefList*/);
527 theReferences[aMainListFeature] = aResultRefList;
529 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
530 std::cout << std::endl;
533 #ifdef DEBUG_REMOVE_FEATURES
534 printMapInfo(theReferences, "allDependencies");
538 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
539 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
540 std::set<FeaturePtr>& theFeaturesRefsTo)
542 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
543 aLast = theFeatures.end();
544 for (; anIt != aLast; anIt++) {
545 FeaturePtr aFeature = *anIt;
546 if (theReferences.find(aFeature) == theReferences.end())
548 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
549 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
550 for (; aRefIt != aRefLast; aRefIt++) {
551 FeaturePtr aRefFeature = *aRefIt;
552 CompositeFeaturePtr aComposite =
553 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
554 if (aComposite.get() && aComposite->isSub(aFeature))
555 continue; /// composite of the current feature should not be removed
557 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
558 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
559 theFeaturesRefsTo.insert(aRefFeature);
564 void getConcealedResults(const FeaturePtr& theFeature,
565 std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
567 SessionPtr aSession = ModelAPI_Session::get();
569 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
570 theFeature->data()->referencesToObjects(aRefs);
571 std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
572 anIt = aRefs.begin(), aLast = aRefs.end();
573 std::set<ResultPtr> alreadyThere; // to avoid duplications
574 for (; anIt != aLast; anIt++) {
575 if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
576 continue; // use only concealed attributes
577 std::list<ObjectPtr> anObjects = (*anIt).second;
578 std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
579 for (; anOIt != anOLast; anOIt++) {
580 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
581 if (aResult && aResult->isConcealed()) {
582 if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
583 alreadyThere.insert(aResult);
585 theResults.push_back(aResult);
591 } // namespace ModelAPI_Tools