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>
15 #include <ModelAPI_Validator.h>
20 #include <Events_Loop.h>
21 #include <ModelAPI_Events.h>
23 #define RECURSE_TOP_LEVEL 50
25 //#define DEBUG_REMOVE_FEATURES
26 //#define DEBUG_REMOVE_FEATURES_RECURSE
27 //#define DEBUG_CYCLING_1550
29 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
31 std::string getFeatureInfo(FeaturePtr theFeature)
33 if (!theFeature.get())
35 //std::ostringstream aPtrStr;
36 //aPtrStr << "[" << theFeature.get() << "] ";
37 std::string aFeatureInfo = /*aPtrStr.str() + */theFeature->name();
38 CompositeFeaturePtr aComposite = ModelAPI_Tools::compositeOwner(theFeature);
39 if (aComposite.get()) {
40 aFeatureInfo = aFeatureInfo + "[in " + aComposite->name() + "]";
46 #ifdef DEBUG_REMOVE_FEATURES
47 void printMapInfo(const std::map<FeaturePtr, std::set<FeaturePtr> >& theMainList,
48 const std::string& thePrefix)
50 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = theMainList.begin(),
51 aMainLast = theMainList.end();
53 for (; aMainIt != aMainLast; aMainIt++) {
54 FeaturePtr aMainListFeature = aMainIt->first;
55 std::set<FeaturePtr> aMainRefList = aMainIt->second;
56 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(), aLast = aMainRefList.end();
57 std::string aRefsInfo;
58 for (; anIt != aLast; anIt++) {
59 aRefsInfo += (*anIt)->name().c_str();
63 if (!aRefsInfo.empty()) {
64 anInfo = anInfo + aMainListFeature->name().c_str() + ": " + aRefsInfo + "\n";
67 std::cout << thePrefix.c_str() << " [feature: references to]: \n" << anInfo.c_str() << std::endl;
70 void printListInfo(const std::set<FeaturePtr>& theMainList,
71 const std::string& thePrefix)
73 std::set<FeaturePtr>::const_iterator aMainIt = theMainList.begin(),
74 aMainLast = theMainList.end();
76 for (; aMainIt != aMainLast; aMainIt++) {
77 FeaturePtr aRefFeature = *aMainIt;
78 anInfo += aRefFeature->name().c_str();
79 if (aMainIt != aMainLast)
82 std::cout << thePrefix.c_str() << ": " << anInfo.c_str() << std::endl;
86 namespace ModelAPI_Tools {
88 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
90 return theResult->shape();
93 void shapesOfType(const FeaturePtr& theFeature,
94 const GeomAPI_Shape::ShapeType& theType,
95 std::set<GeomShapePtr>& theShapes)
97 std::list<ResultPtr> aResults = theFeature->results();
98 std::list<ResultPtr>::const_iterator aRIter = aResults.cbegin();
99 for (; aRIter != aResults.cend(); aRIter++) {
100 ResultPtr aResult = *aRIter;
101 GeomShapePtr aShape = aResult->shape();
102 if (aShape.get() && aShape->shapeType() == theType)
103 theShapes.insert(aShape);
107 const char* toString(ModelAPI_ExecState theExecState)
109 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
110 switch (theExecState) {
111 TO_STRING(ModelAPI_StateDone)
112 TO_STRING(ModelAPI_StateMustBeUpdated)
113 TO_STRING(ModelAPI_StateExecFailed)
114 TO_STRING(ModelAPI_StateInvalidArgument)
115 TO_STRING(ModelAPI_StateNothing)
116 default: return "Unknown ExecState.";
121 std::string getFeatureError(const FeaturePtr& theFeature)
124 if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
127 // to be removed later, this error should be got from the feature
128 if (theFeature->data()->execState() == ModelAPI_StateDone ||
129 theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
132 // set error indication
133 anError = theFeature->error();
134 if (anError.empty()) {
135 bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
136 || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
138 anError = toString(theFeature->data()->execState());
139 // If the feature is Composite and error is StateInvalidArgument,
140 // error text should include error of first invalid sub-feature. Otherwise
141 // it is not clear what is the reason of the invalid argument.
142 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
143 CompositeFeaturePtr aComposite =
144 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
146 for (int i = 0, aSize = aComposite->numberOfSubs(); i < aSize; i++) {
147 FeaturePtr aSubFeature = aComposite->subFeature(i);
148 std::string aSubFeatureError = getFeatureError(aSubFeature);
149 if (!aSubFeatureError.empty()) {
150 anError = anError + " in " + aSubFeature->getKind() + ".\n" + aSubFeatureError;
162 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup,
163 const std::string& theName)
165 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
166 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
167 if (anObject->data()->name() == theName)
174 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
175 const std::string& theName, double& outValue, ResultParameterPtr& theParam)
177 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
178 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
181 // avoid usage of parameters created later than the initial parameter
182 if (theSearcher.get() && theDocument->isLater(theDocument->feature(theParam), theSearcher))
184 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
185 outValue = aValueAttribute->value();
189 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue,
190 ResultParameterPtr& theParam, const DocumentPtr& theDocument)
192 SessionPtr aSession = ModelAPI_Session::get();
193 std::list<DocumentPtr> aDocList;
194 DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
195 if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
197 DocumentPtr aRootDocument = aSession->moduleDocument();
198 if (aDocument != aRootDocument) {
199 // any parameters in PartSet is okindependently on the Part position (issu #1504)
200 if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
206 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
208 // to optimize and avoid of crash on partset document close
209 // (don't touch the sub-document structure)
210 if (theMain != theSub) {
211 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
212 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
213 theMain->object(ModelAPI_ResultPart::group(), a));
214 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
222 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
224 // to optimize and avoid of crash on partset document close
225 // (don't touch the sub-document structure)
226 if (theMain != theSub) {
227 for (int a = theMain->size(ModelAPI_Feature::group()) - 1; a >= 0; a--) {
228 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
229 theMain->object(ModelAPI_Feature::group(), a));
230 if (aPartFeat.get()) {
231 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
232 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
233 for(; aRes != aResList.end(); aRes++) {
234 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
236 if (aPart->isActivated() && aPart->partDoc() == theSub)
238 } else break; // if the first is not Part, others are also not
246 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
248 if (theFeature.get() && theFeature->data()->isValid()) {
249 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
250 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
251 for(; aRefIter != aRefs.end(); aRefIter++) {
252 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
253 ((*aRefIter)->owner());
254 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
258 return CompositeFeaturePtr(); // not found
261 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
263 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
265 FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
266 if (aFeatureOwner.get()) {
267 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
268 aFeatureOwner->results().cbegin();
269 for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
270 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
271 if (aComp && aComp->isSub(aBody))
276 return ResultCompSolidPtr(); // not found
279 bool hasSubResults(const ResultPtr& theResult)
281 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
282 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
285 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
287 if (!theFeature.get()) // safety: for empty feature no results
289 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
290 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
291 for (; aRIter != aResults.cend(); aRIter++) {
292 theResults.push_back(*aRIter);
293 // iterate sub-bodies of compsolid
294 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
296 int aNumSub = aComp->numberOfSubs();
297 for(int a = 0; a < aNumSub; a++) {
298 theResults.push_back(aComp->subResult(a));
304 //******************************************************************
305 bool allDocumentsActivated(std::string& theNotActivatedNames)
307 theNotActivatedNames = "";
308 bool anAllPartActivated = true;
310 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
311 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
312 for (int i = 0; i < aSize; i++) {
313 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
314 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
315 if (!aPart->isActivated()) {
316 anAllPartActivated = false;
317 if (!theNotActivatedNames.empty())
318 theNotActivatedNames += ", ";
319 theNotActivatedNames += aObject->data()->name().c_str();
322 return anAllPartActivated;
325 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
326 const bool theFlushRedisplay,
327 const bool theUseComposite,
328 const bool theUseRecursion)
330 #ifdef DEBUG_REMOVE_FEATURES
331 printListInfo(theFeatures, "selection: ");
334 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
335 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
336 #ifdef DEBUG_REMOVE_FEATURES
337 printMapInfo(aReferences, "allDependencies: ");
340 std::set<FeaturePtr> aFeaturesRefsTo;
341 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
342 #ifdef DEBUG_REMOVE_FEATURES
343 printListInfo(aFeaturesRefsTo, "references: ");
346 std::set<FeaturePtr> aFeatures = theFeatures;
347 if (!aFeaturesRefsTo.empty())
348 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
349 #ifdef DEBUG_REMOVE_FEATURES
350 printListInfo(aFeatures, "removeFeatures: ");
353 return ModelAPI_Tools::removeFeatures(aFeatures, false);
356 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
357 const bool theFlushRedisplay)
360 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
361 aLast = theFeatures.end();
362 for (; anIt != aLast; anIt++) {
363 FeaturePtr aFeature = *anIt;
364 if (aFeature.get()) {
365 DocumentPtr aDoc = aFeature->document();
366 // flush REDISPLAY signal after remove feature
367 aDoc->removeFeature(aFeature);
371 if (isDone && theFlushRedisplay) {
372 // the redisplay signal should be flushed in order to erase
373 // the feature presentation in the viewer
374 // if should be done after removeFeature() of document
375 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
380 // Fills the references list by all references of the feature from the references map.
381 // This is a recusive method to find references by next found feature in the map of references.
382 // \param theFeature a feature to find references
383 // \param theReferencesMap a map of references
384 // \param theReferences an out container of references
385 void addRefsToFeature(const FeaturePtr& theFeature,
386 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
387 std::map<FeaturePtr, std::set<FeaturePtr> >& theProcessedReferences,
389 std::set<FeaturePtr>& theReferences)
391 if (theRecLevel > RECURSE_TOP_LEVEL)
395 // if the feature is already processed, get the ready references from the map
396 if (theProcessedReferences.find(theFeature) != theProcessedReferences.end()) {
397 std::set<FeaturePtr> aReferences = theProcessedReferences.at(theFeature);
398 theReferences.insert(aReferences.begin(), aReferences.end());
402 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
403 return; // this feature is not in the selection list, so exists without references to it
404 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
406 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
407 aLast = aMainReferences.end();
408 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
409 std::string aSpacing;
410 for (int i = 0; i < theRecLevel; i++)
411 aSpacing.append(" ");
414 for (; anIt != aLast; anIt++) {
415 FeaturePtr aRefFeature = *anIt;
416 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
417 std::cout << aSpacing << " Ref: " << getFeatureInfo(aRefFeature) << std::endl;
419 if (theReferences.find(aRefFeature) == theReferences.end())
420 theReferences.insert(aRefFeature);
421 addRefsToFeature(aRefFeature, theReferencesMap, theProcessedReferences,
422 theRecLevel, theReferences);
426 // For each feature from the feature list it searches references to the feature and append them
427 // to the references map. This is a recusive method.
428 // \param theFeature a feature to find references
429 // \param theReferencesMap a map of references
430 // \param theReferences an out container of references
431 void findReferences(const std::set<FeaturePtr>& theFeatures,
432 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
433 const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
435 if (theRecLevel > RECURSE_TOP_LEVEL)
438 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
439 aLast = theFeatures.end();
440 for (; anIt != aLast; anIt++) {
441 FeaturePtr aFeature = *anIt;
442 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
443 DocumentPtr aSelFeatureDoc = aFeature->document();
444 std::set<FeaturePtr> aSelRefFeatures;
445 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
446 if (theUseComposite) { // do not filter selection
447 theReferences[aFeature] = aSelRefFeatures;
449 else { // filter references to skip composition features of the current feature
450 std::set<FeaturePtr> aFilteredFeatures;
451 std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
452 aLast = aSelRefFeatures.end();
453 for (; anIt != aLast; anIt++) {
454 FeaturePtr aCFeature = *anIt;
455 CompositeFeaturePtr aComposite =
456 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
457 if (aComposite.get() && aComposite->isSub(aFeature))
458 continue; /// composite of the current feature should be skipped
459 aFilteredFeatures.insert(aCFeature);
461 theReferences[aFeature] = aFilteredFeatures;
463 if (theUseRecursion) {
464 #ifdef DEBUG_CYCLING_1550
465 findReferences(aSelRefFeatures, theReferences, theUseComposite,
466 theUseRecursion, theRecLevel);
468 findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
476 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
477 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
478 const bool theUseComposite,
479 const bool theUseRecursion)
481 // For dependencies, find main_list:
482 // sk_1(ext_1, vertex_1)
483 // ext_1(bool_1, sk_3)
488 // Information: bool_1 is not selected, ext_2(bool_2) exists
489 // find all referenced features
490 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
492 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
494 #ifdef DEBUG_REMOVE_FEATURES
495 printMapInfo(aMainList, "firstDependencies");
497 // find all dependencies for each object:
498 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
499 // ext_1(bool_1, sk_3)
501 // sk_2(ext_2) + (bool_1)
504 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
505 aMainLast = aMainList.end();
506 for (; aMainIt != aMainLast; aMainIt++) {
507 FeaturePtr aMainListFeature = aMainIt->first;
509 //std::string aName = aMainListFeature->name();
510 std::set<FeaturePtr> aMainRefList = aMainIt->second;
512 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
514 int n = sprintf(aBuf, "%d", aMainRefList.size());
515 std::string aSize(aBuf);
516 std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
517 << ", references size = " << aSize << std::endl;
519 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
520 aLast = aMainRefList.end();
521 std::set<FeaturePtr> aResultRefList;
522 aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
523 for (; anIt != aLast; anIt++) {
524 FeaturePtr aFeature = *anIt;
526 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
527 std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
530 addRefsToFeature(aFeature, aMainList, theReferences,
531 aRecLevel, aResultRefList/*aMainRefList*/);
533 theReferences[aMainListFeature] = aResultRefList;
535 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
536 std::cout << std::endl;
539 #ifdef DEBUG_REMOVE_FEATURES
540 printMapInfo(theReferences, "allDependencies");
544 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
545 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
546 std::set<FeaturePtr>& theFeaturesRefsTo)
548 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
549 aLast = theFeatures.end();
550 for (; anIt != aLast; anIt++) {
551 FeaturePtr aFeature = *anIt;
552 if (theReferences.find(aFeature) == theReferences.end())
554 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
555 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
556 for (; aRefIt != aRefLast; aRefIt++) {
557 FeaturePtr aRefFeature = *aRefIt;
558 CompositeFeaturePtr aComposite =
559 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
560 if (aComposite.get() && aComposite->isSub(aFeature))
561 continue; /// composite of the current feature should not be removed
563 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
564 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
565 theFeaturesRefsTo.insert(aRefFeature);
570 void getConcealedResults(const FeaturePtr& theFeature,
571 std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
573 SessionPtr aSession = ModelAPI_Session::get();
575 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
576 theFeature->data()->referencesToObjects(aRefs);
577 std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
578 anIt = aRefs.begin(), aLast = aRefs.end();
579 std::set<ResultPtr> alreadyThere; // to avoid duplications
580 for (; anIt != aLast; anIt++) {
581 if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
582 continue; // use only concealed attributes
583 std::list<ObjectPtr> anObjects = (*anIt).second;
584 std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
585 for (; anOIt != anOLast; anOIt++) {
586 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
587 if (aResult && aResult->isConcealed()) {
588 if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
589 alreadyThere.insert(aResult);
591 theResults.push_back(aResult);
597 } // namespace ModelAPI_Tools