1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModelAPI_Tools.h"
22 #include <ModelAPI_Session.h>
23 #include <ModelAPI_CompositeFeature.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Object.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_ResultCompSolid.h>
28 #include <ModelAPI_ResultParameter.h>
29 #include <ModelAPI_ResultPart.h>
30 #include <ModelAPI_AttributeDocRef.h>
31 #include <ModelAPI_Validator.h>
36 #include <Events_Loop.h>
37 #include <ModelAPI_Events.h>
39 #define RECURSE_TOP_LEVEL 50
41 //#define DEBUG_REMOVE_FEATURES
42 //#define DEBUG_REMOVE_FEATURES_RECURSE
43 //#define DEBUG_CYCLING_1550
45 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
47 std::string getFeatureInfo(FeaturePtr theFeature)
49 if (!theFeature.get())
51 //std::ostringstream aPtrStr;
52 //aPtrStr << "[" << theFeature.get() << "] ";
53 std::string aFeatureInfo = /*aPtrStr.str() + */theFeature->name();
54 CompositeFeaturePtr aComposite = ModelAPI_Tools::compositeOwner(theFeature);
55 if (aComposite.get()) {
56 aFeatureInfo = aFeatureInfo + "[in " + aComposite->name() + "]";
62 #ifdef DEBUG_REMOVE_FEATURES
63 void printMapInfo(const std::map<FeaturePtr, std::set<FeaturePtr> >& theMainList,
64 const std::string& thePrefix)
66 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = theMainList.begin(),
67 aMainLast = theMainList.end();
69 for (; aMainIt != aMainLast; aMainIt++) {
70 FeaturePtr aMainListFeature = aMainIt->first;
71 std::set<FeaturePtr> aMainRefList = aMainIt->second;
72 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(), aLast = aMainRefList.end();
73 std::string aRefsInfo;
74 for (; anIt != aLast; anIt++) {
75 aRefsInfo += (*anIt)->name().c_str();
79 if (!aRefsInfo.empty()) {
80 anInfo = anInfo + aMainListFeature->name().c_str() + ": " + aRefsInfo + "\n";
83 std::cout << thePrefix.c_str() << " [feature: references to]: \n" << anInfo.c_str() << std::endl;
86 void printListInfo(const std::set<FeaturePtr>& theMainList,
87 const std::string& thePrefix)
89 std::set<FeaturePtr>::const_iterator aMainIt = theMainList.begin(),
90 aMainLast = theMainList.end();
92 for (; aMainIt != aMainLast; aMainIt++) {
93 FeaturePtr aRefFeature = *aMainIt;
94 anInfo += aRefFeature->name().c_str();
95 if (aMainIt != aMainLast)
98 std::cout << thePrefix.c_str() << ": " << anInfo.c_str() << std::endl;
102 namespace ModelAPI_Tools {
104 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
106 return theResult->shape();
109 const char* toString(ModelAPI_ExecState theExecState)
111 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
112 switch (theExecState) {
113 TO_STRING(ModelAPI_StateDone)
114 TO_STRING(ModelAPI_StateMustBeUpdated)
115 TO_STRING(ModelAPI_StateExecFailed)
116 TO_STRING(ModelAPI_StateInvalidArgument)
117 TO_STRING(ModelAPI_StateNothing)
118 default: return "Unknown ExecState.";
123 std::string getFeatureError(const FeaturePtr& theFeature)
126 if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
129 // to be removed later, this error should be got from the feature
130 if (theFeature->data()->execState() == ModelAPI_StateDone ||
131 theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
134 // set error indication
135 anError = theFeature->error();
136 if (anError.empty()) {
137 bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
138 || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
140 anError = toString(theFeature->data()->execState());
141 // If the feature is Composite and error is StateInvalidArgument,
142 // error text should include error of first invalid sub-feature. Otherwise
143 // it is not clear what is the reason of the invalid argument.
144 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
145 CompositeFeaturePtr aComposite =
146 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
148 for (int i = 0, aSize = aComposite->numberOfSubs(); i < aSize; i++) {
149 FeaturePtr aSubFeature = aComposite->subFeature(i);
150 std::string aSubFeatureError = getFeatureError(aSubFeature);
151 if (!aSubFeatureError.empty()) {
152 anError = anError + " in " + aSubFeature->getKind() + ".\n" + aSubFeatureError;
164 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup,
165 const std::string& theName)
167 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
168 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
169 if (anObject->data()->name() == theName)
176 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
177 const std::string& theName, double& outValue, ResultParameterPtr& theParam)
179 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
180 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
183 // avoid usage of parameters created later than the initial parameter
185 if (theSearcher.get()) {
186 FeaturePtr aParamFeat = theDocument->feature(theParam);
187 if (aParamFeat == theSearcher || theDocument->isLater(aParamFeat, theSearcher))
190 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
191 outValue = aValueAttribute->value();
195 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue,
196 ResultParameterPtr& theParam, const DocumentPtr& theDocument)
198 SessionPtr aSession = ModelAPI_Session::get();
199 std::list<DocumentPtr> aDocList;
200 DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
201 if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
203 DocumentPtr aRootDocument = aSession->moduleDocument();
204 if (aDocument != aRootDocument) {
205 // any parameters in PartSet is okindependently on the Part position (issu #1504)
206 if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
212 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
214 // to optimize and avoid of crash on partset document close
215 // (don't touch the sub-document structure)
216 if (theMain != theSub) {
217 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
218 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
219 theMain->object(ModelAPI_ResultPart::group(), a));
220 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
228 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
230 // to optimize and avoid of crash on partset document close
231 // (don't touch the sub-document structure)
232 if (theMain != theSub) {
233 // iteration from top to bottom to avoid finding the movement documents before the original
234 int aSize = theMain->size(ModelAPI_Feature::group());
235 for (int a = 0; a < aSize; a++) {
236 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
237 theMain->object(ModelAPI_Feature::group(), a));
238 if (aPartFeat.get()) {
239 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
240 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
241 for(; aRes != aResList.end(); aRes++) {
242 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
244 if (aPart->isActivated() && aPart->partDoc() == theSub)
246 } else break; // if the first is not Part, others are also not
254 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
256 if (theFeature.get() && theFeature->data()->isValid()) {
257 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
258 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
259 for(; aRefIter != aRefs.end(); aRefIter++) {
260 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
261 ((*aRefIter)->owner());
262 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
266 return CompositeFeaturePtr(); // not found
269 ResultCompSolidPtr compSolidOwner(const ResultPtr& theSub)
271 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
273 FeaturePtr aFeatureOwner = aBody->document()->feature(aBody);
274 if (aFeatureOwner.get()) {
275 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
276 aFeatureOwner->results().cbegin();
277 for(; aResIter != aFeatureOwner->results().cend(); aResIter++) {
278 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIter);
279 if (aComp && aComp->isSub(aBody))
284 return ResultCompSolidPtr(); // not found
287 bool hasSubResults(const ResultPtr& theResult)
289 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theResult);
290 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
293 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
295 if (!theFeature.get()) // safety: for empty feature no results
297 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
298 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
299 for (; aRIter != aResults.cend(); aRIter++) {
300 theResults.push_back(*aRIter);
301 // iterate sub-bodies of compsolid
302 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
304 int aNumSub = aComp->numberOfSubs();
305 for(int a = 0; a < aNumSub; a++) {
306 theResults.push_back(aComp->subResult(a));
312 //******************************************************************
313 bool allDocumentsActivated(std::string& theNotActivatedNames)
315 theNotActivatedNames = "";
316 bool anAllPartActivated = true;
318 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
319 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
320 for (int i = 0; i < aSize; i++) {
321 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
322 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
323 if (!aPart->isActivated()) {
324 anAllPartActivated = false;
325 if (!theNotActivatedNames.empty())
326 theNotActivatedNames += ", ";
327 theNotActivatedNames += aObject->data()->name().c_str();
330 return anAllPartActivated;
333 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
334 const bool theFlushRedisplay,
335 const bool theUseComposite,
336 const bool theUseRecursion)
338 #ifdef DEBUG_REMOVE_FEATURES
339 printListInfo(theFeatures, "selection: ");
342 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
343 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
344 #ifdef DEBUG_REMOVE_FEATURES
345 printMapInfo(aReferences, "allDependencies: ");
348 std::set<FeaturePtr> aFeaturesRefsTo;
349 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
350 #ifdef DEBUG_REMOVE_FEATURES
351 printListInfo(aFeaturesRefsTo, "references: ");
354 std::set<FeaturePtr> aFeatures = theFeatures;
355 if (!aFeaturesRefsTo.empty())
356 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
357 #ifdef DEBUG_REMOVE_FEATURES
358 printListInfo(aFeatures, "removeFeatures: ");
361 return ModelAPI_Tools::removeFeatures(aFeatures, false);
364 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
365 const bool theFlushRedisplay)
368 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
369 aLast = theFeatures.end();
370 for (; anIt != aLast; anIt++) {
371 FeaturePtr aFeature = *anIt;
372 if (aFeature.get()) {
373 DocumentPtr aDoc = aFeature->document();
374 // flush REDISPLAY signal after remove feature
375 aDoc->removeFeature(aFeature);
379 if (isDone && theFlushRedisplay) {
380 // the redisplay signal should be flushed in order to erase
381 // the feature presentation in the viewer
382 // if should be done after removeFeature() of document
383 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
388 // Fills the references list by all references of the feature from the references map.
389 // This is a recusive method to find references by next found feature in the map of references.
390 // \param theFeature a feature to find references
391 // \param theReferencesMap a map of references
392 // \param theReferences an out container of references
393 void addRefsToFeature(const FeaturePtr& theFeature,
394 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
395 std::map<FeaturePtr, std::set<FeaturePtr> >& theProcessedReferences,
397 std::set<FeaturePtr>& theReferences)
399 if (theRecLevel > RECURSE_TOP_LEVEL)
403 // if the feature is already processed, get the ready references from the map
404 if (theProcessedReferences.find(theFeature) != theProcessedReferences.end()) {
405 std::set<FeaturePtr> aReferences = theProcessedReferences.at(theFeature);
406 theReferences.insert(aReferences.begin(), aReferences.end());
410 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
411 return; // this feature is not in the selection list, so exists without references to it
412 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
414 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
415 aLast = aMainReferences.end();
416 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
417 std::string aSpacing;
418 for (int i = 0; i < theRecLevel; i++)
419 aSpacing.append(" ");
422 for (; anIt != aLast; anIt++) {
423 FeaturePtr aRefFeature = *anIt;
424 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
425 std::cout << aSpacing << " Ref: " << getFeatureInfo(aRefFeature) << std::endl;
427 if (theReferences.find(aRefFeature) == theReferences.end())
428 theReferences.insert(aRefFeature);
429 addRefsToFeature(aRefFeature, theReferencesMap, theProcessedReferences,
430 theRecLevel, theReferences);
434 // For each feature from the feature list it searches references to the feature and append them
435 // to the references map. This is a recusive method.
436 // \param theFeature a feature to find references
437 // \param theReferencesMap a map of references
438 // \param theReferences an out container of references
439 void findReferences(const std::set<FeaturePtr>& theFeatures,
440 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
441 const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
443 if (theRecLevel > RECURSE_TOP_LEVEL)
446 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
447 aLast = theFeatures.end();
448 for (; anIt != aLast; anIt++) {
449 FeaturePtr aFeature = *anIt;
450 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
451 DocumentPtr aSelFeatureDoc = aFeature->document();
452 std::set<FeaturePtr> aSelRefFeatures;
453 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
454 if (theUseComposite) { // do not filter selection
455 theReferences[aFeature] = aSelRefFeatures;
457 else { // filter references to skip composition features of the current feature
458 std::set<FeaturePtr> aFilteredFeatures;
459 std::set<FeaturePtr>::const_iterator anIt = aSelRefFeatures.begin(),
460 aLast = aSelRefFeatures.end();
461 for (; anIt != aLast; anIt++) {
462 FeaturePtr aCFeature = *anIt;
463 CompositeFeaturePtr aComposite =
464 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
465 if (aComposite.get() && aComposite->isSub(aFeature))
466 continue; /// composite of the current feature should be skipped
467 aFilteredFeatures.insert(aCFeature);
469 theReferences[aFeature] = aFilteredFeatures;
471 if (theUseRecursion) {
472 #ifdef DEBUG_CYCLING_1550
473 findReferences(aSelRefFeatures, theReferences, theUseComposite,
474 theUseRecursion, theRecLevel);
476 findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
484 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
485 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
486 const bool theUseComposite,
487 const bool theUseRecursion)
489 // For dependencies, find main_list:
490 // sk_1(ext_1, vertex_1)
491 // ext_1(bool_1, sk_3)
496 // Information: bool_1 is not selected, ext_2(bool_2) exists
497 // find all referenced features
498 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
500 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
502 #ifdef DEBUG_REMOVE_FEATURES
503 printMapInfo(aMainList, "firstDependencies");
505 // find all dependencies for each object:
506 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
507 // ext_1(bool_1, sk_3)
509 // sk_2(ext_2) + (bool_1)
512 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
513 aMainLast = aMainList.end();
514 for (; aMainIt != aMainLast; aMainIt++) {
515 FeaturePtr aMainListFeature = aMainIt->first;
517 //std::string aName = aMainListFeature->name();
518 std::set<FeaturePtr> aMainRefList = aMainIt->second;
520 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
522 int n = sprintf(aBuf, "%d", aMainRefList.size());
523 std::string aSize(aBuf);
524 std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
525 << ", references size = " << aSize << std::endl;
527 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
528 aLast = aMainRefList.end();
529 std::set<FeaturePtr> aResultRefList;
530 aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
531 for (; anIt != aLast; anIt++) {
532 FeaturePtr aFeature = *anIt;
534 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
535 std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
538 addRefsToFeature(aFeature, aMainList, theReferences,
539 aRecLevel, aResultRefList/*aMainRefList*/);
541 theReferences[aMainListFeature] = aResultRefList;
543 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
544 std::cout << std::endl;
547 #ifdef DEBUG_REMOVE_FEATURES
548 printMapInfo(theReferences, "allDependencies");
552 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
553 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
554 std::set<FeaturePtr>& theFeaturesRefsTo)
556 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
557 aLast = theFeatures.end();
558 for (; anIt != aLast; anIt++) {
559 FeaturePtr aFeature = *anIt;
560 if (theReferences.find(aFeature) == theReferences.end())
562 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
563 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
564 for (; aRefIt != aRefLast; aRefIt++) {
565 FeaturePtr aRefFeature = *aRefIt;
566 CompositeFeaturePtr aComposite =
567 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
568 if (aComposite.get() && aComposite->isSub(aFeature))
569 continue; /// composite of the current feature should not be removed
571 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
572 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
573 theFeaturesRefsTo.insert(aRefFeature);
578 void getConcealedResults(const FeaturePtr& theFeature,
579 std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
581 SessionPtr aSession = ModelAPI_Session::get();
583 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
584 theFeature->data()->referencesToObjects(aRefs);
585 std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
586 anIt = aRefs.begin(), aLast = aRefs.end();
587 std::set<ResultPtr> alreadyThere; // to avoid duplications
588 for (; anIt != aLast; anIt++) {
589 if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
590 continue; // use only concealed attributes
591 std::list<ObjectPtr> anObjects = (*anIt).second;
592 std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
593 for (; anOIt != anOLast; anOIt++) {
594 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
595 if (aResult && aResult->isConcealed()) {
596 if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
597 alreadyThere.insert(aResult);
599 theResults.push_back(aResult);
605 } // namespace ModelAPI_Tools