1 // Copyright (C) 2014-2021 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 email : webmaster.salome@opencascade.com
20 #include "ModelAPI_Tools.h"
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_CompositeFeature.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <ModelAPI_ResultParameter.h>
29 #include <ModelAPI_ResultPart.h>
30 #include <ModelAPI_ResultGroup.h>
31 #include <ModelAPI_AttributeDocRef.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_AttributeIntArray.h>
34 #include <ModelAPI_ResultConstruction.h>
35 #include <ModelAPI_AttributeBoolean.h>
41 #include <Events_Loop.h>
42 #include <Locale_Convert.h>
43 #include <ModelAPI_Events.h>
44 #include <Config_Translator.h>
46 #include <GeomAPI_ShapeHierarchy.h>
47 #include <GeomAPI_ShapeIterator.h>
49 #define RECURSE_TOP_LEVEL 50
51 //#define DEBUG_REMOVE_FEATURES
52 //#define DEBUG_REMOVE_FEATURES_RECURSE
53 //#define DEBUG_CYCLING_1550
55 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
57 std::string getFeatureInfo(FeaturePtr theFeature)
59 if (!theFeature.get())
61 //std::ostringstream aPtrStr;
62 //aPtrStr << "[" << theFeature.get() << "] ";
63 std::string aFeatureInfo = /*aPtrStr.str() + */theFeature->name();
64 CompositeFeaturePtr aComposite = ModelAPI_Tools::compositeOwner(theFeature);
65 if (aComposite.get()) {
66 aFeatureInfo = aFeatureInfo + "[in " + aComposite->name() + "]";
72 #ifdef DEBUG_REMOVE_FEATURES
73 void printMapInfo(const std::map<FeaturePtr, std::set<FeaturePtr> >& theMainList,
74 const std::string& thePrefix)
76 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = theMainList.begin(),
77 aMainLast = theMainList.end();
79 for (; aMainIt != aMainLast; aMainIt++) {
80 FeaturePtr aMainListFeature = aMainIt->first;
81 std::set<FeaturePtr> aMainRefList = aMainIt->second;
82 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(), aLast = aMainRefList.end();
83 std::string aRefsInfo;
84 for (; anIt != aLast; anIt++) {
85 aRefsInfo += (*anIt)->name().c_str();
89 if (!aRefsInfo.empty()) {
90 anInfo = anInfo + aMainListFeature->name().c_str() + ": " + aRefsInfo + "\n";
93 std::cout << thePrefix.c_str() << " [feature: references to]: \n" << anInfo.c_str() << std::endl;
96 void printListInfo(const std::set<FeaturePtr>& theMainList,
97 const std::string& thePrefix)
99 std::set<FeaturePtr>::const_iterator aMainIt = theMainList.begin(),
100 aMainLast = theMainList.end();
102 for (; aMainIt != aMainLast; aMainIt++) {
103 FeaturePtr aRefFeature = *aMainIt;
104 anInfo += aRefFeature->name().c_str();
105 if (aMainIt != aMainLast)
108 std::cout << thePrefix.c_str() << ": " << anInfo.c_str() << std::endl;
112 namespace ModelAPI_Tools {
114 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
116 return theResult->shape();
120 const char* toString(ModelAPI_ExecState theExecState)
122 switch (theExecState) {
123 case ModelAPI_StateDone: return "Done";
124 case ModelAPI_StateMustBeUpdated: return "Must be updated";
125 case ModelAPI_StateExecFailed: return "Execution failed";
126 case ModelAPI_StateInvalidArgument: return "Invalid argument";
127 case ModelAPI_StateNothing: return "Empty state";
128 default: return "Unknown ExecState.";
132 std::string getFeatureError(const FeaturePtr& theFeature)
135 if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
138 // to be removed later, this error should be got from the feature
139 if (theFeature->data()->execState() == ModelAPI_StateDone ||
140 theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
143 // set error indication
144 anError = theFeature->error();
145 if (anError.empty()) {
146 bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
147 || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
149 anError = toString(theFeature->data()->execState());
150 // If the feature is Composite and error is StateInvalidArgument,
151 // error text should include error of first invalid sub-feature. Otherwise
152 // it is not clear what is the reason of the invalid argument.
153 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
154 CompositeFeaturePtr aComposite =
155 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
157 bool aHasSubError = false;
158 for (int i = 0, aSize = aComposite->numberOfSubs(); i < aSize; i++) {
159 FeaturePtr aSubFeature = aComposite->subFeature(i);
160 std::string aSubFeatureError = getFeatureError(aSubFeature);
161 if (!aSubFeatureError.empty()) {
162 anError = anError + " in " + aSubFeature->getKind() + ".\n" + aSubFeatureError;
167 if (!aHasSubError) { // #24260: error not in the sub-features, but in the argument
168 if (aComposite->getKind() == "Sketch" &&
169 aComposite->selection("External")->isInvalid()) {
170 anError = Config_Translator::translate(aComposite->getKind(), "The sketch base plane \
171 is invalid, please push 'Change sketch plane' button to reselect it.");
183 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup,
184 const std::wstring& theName)
186 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
187 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
188 if (anObject->data()->name() == theName)
195 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
196 const std::wstring& theName, double& outValue, ResultParameterPtr& theParam)
198 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
199 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
202 // avoid usage of parameters created later than the initial parameter
204 if (theSearcher.get()) {
205 FeaturePtr aParamFeat = theDocument->feature(theParam);
206 if (aParamFeat == theSearcher || theDocument->isLater(aParamFeat, theSearcher))
209 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
210 outValue = aValueAttribute->value();
214 bool findVariable(FeaturePtr theSearcher, const std::wstring& theName, double& outValue,
215 ResultParameterPtr& theParam, const DocumentPtr& theDocument)
217 SessionPtr aSession = ModelAPI_Session::get();
218 std::list<DocumentPtr> aDocList;
219 DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
220 if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
222 DocumentPtr aRootDocument = aSession->moduleDocument();
223 if (aDocument != aRootDocument) {
224 // any parameters in PartSet is okindependently on the Part position (issu #1504)
225 if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
231 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
233 // to optimize and avoid of crash on partset document close
234 // (don't touch the sub-document structure)
235 if (theMain != theSub) {
236 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
237 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
238 theMain->object(ModelAPI_ResultPart::group(), a));
239 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
247 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
249 // to optimize and avoid of crash on partset document close
250 // (don't touch the sub-document structure)
251 if (theMain != theSub) {
252 // iteration from top to bottom to avoid finding the movement documents before the original
253 int aSize = theMain->size(ModelAPI_Feature::group());
254 for (int a = 0; a < aSize; a++) {
255 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
256 theMain->object(ModelAPI_Feature::group(), a));
257 if (aPartFeat.get()) {
258 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
259 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
260 for(; aRes != aResList.end(); aRes++) {
261 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
263 if (aPart->isActivated() && aPart->partDoc() == theSub)
265 } else break; // if the first is not Part, others are also not
273 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
275 if (theFeature.get() && theFeature->data() && theFeature->data()->isValid()) {
276 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
277 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
278 for(; aRefIter != aRefs.end(); aRefIter++) {
279 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
280 ((*aRefIter)->owner());
281 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
285 return CompositeFeaturePtr(); // not found
288 ResultBodyPtr bodyOwner(const ResultPtr& theSub, const bool theRoot)
291 ObjectPtr aParent = theSub->document()->parent(theSub);
293 if (theRoot) { // try to find parent of parent
294 ResultPtr aResultParent = std::dynamic_pointer_cast<ModelAPI_Result>(aParent);
295 ResultBodyPtr aGrandParent = bodyOwner(aResultParent, true);
296 if (aGrandParent.get())
297 aParent = aGrandParent;
299 return std::dynamic_pointer_cast<ModelAPI_ResultBody>(aParent);
302 return ResultBodyPtr(); // not found
305 int bodyIndex(const ResultPtr& theSub)
308 ResultBodyPtr aParent = bodyOwner(theSub);
310 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
311 if (aBody.get() && aParent->isSub(aBody, anIndex))
314 return anIndex; // not found
317 bool hasSubResults(const ResultPtr& theResult)
319 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
320 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
323 void allSubs(const ResultBodyPtr& theResult, std::list<ResultPtr>& theResults,
324 const bool theLowerOnly) {
325 // iterate sub-bodies of compsolid
326 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
328 int aNumSub = aComp->numberOfSubs();
329 for (int a = 0; a < aNumSub; a++) {
330 ResultBodyPtr aSub = aComp->subResult(a);
331 if (!theLowerOnly || aSub->numberOfSubs() == 0)
332 theResults.push_back(aSub);
333 allSubs(aSub, theResults);
338 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
340 if (!theFeature.get()) // safety: for empty feature no results
342 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
343 std::list<ResultPtr>::const_iterator aRIter = aResults.begin();
344 for (; aRIter != aResults.cend(); aRIter++) {
345 theResults.push_back(*aRIter);
346 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRIter);
347 allSubs(aResult, theResults);
351 //******************************************************************
352 bool allDocumentsActivated(std::wstring& theNotActivatedNames)
354 theNotActivatedNames = L"";
355 bool anAllPartActivated = true;
357 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
358 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
359 for (int i = 0; i < aSize; i++) {
360 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
361 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
362 if (!aPart->isActivated()) {
363 anAllPartActivated = false;
364 if (!theNotActivatedNames.empty())
365 theNotActivatedNames += L", ";
366 theNotActivatedNames += aObject->data()->name().c_str();
369 return anAllPartActivated;
372 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
373 const bool /*theFlushRedisplay*/,
374 const bool theUseComposite,
375 const bool theUseRecursion)
377 #ifdef DEBUG_REMOVE_FEATURES
378 printListInfo(theFeatures, "selection: ");
381 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
382 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
383 #ifdef DEBUG_REMOVE_FEATURES
384 printMapInfo(aReferences, "allDependencies: ");
387 std::set<FeaturePtr> aFeaturesRefsTo;
388 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
389 #ifdef DEBUG_REMOVE_FEATURES
390 printListInfo(aFeaturesRefsTo, "references: ");
393 std::set<FeaturePtr> aFeatures = theFeatures;
394 if (!aFeaturesRefsTo.empty())
395 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
396 #ifdef DEBUG_REMOVE_FEATURES
397 printListInfo(aFeatures, "removeFeatures: ");
400 return ModelAPI_Tools::removeFeatures(aFeatures, false);
403 //***********************************************************************
404 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
405 const bool theFlushRedisplay)
408 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
409 aLast = theFeatures.end();
410 for (; anIt != aLast; anIt++) {
411 FeaturePtr aFeature = *anIt;
412 if (aFeature.get()) {
413 DocumentPtr aDoc = aFeature->document();
414 // flush REDISPLAY signal after remove feature
415 aDoc->removeFeature(aFeature);
419 if (isDone && theFlushRedisplay) {
420 // the redisplay signal should be flushed in order to erase
421 // the feature presentation in the viewer
422 // if should be done after removeFeature() of document
423 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
428 //***********************************************************************
429 // Fills the references list by all references of the feature from the references map.
430 // This is a recusive method to find references by next found feature in the map of references.
431 // \param theFeature a feature to find references
432 // \param theReferencesMap a map of references
433 // \param theReferences an out container of references
434 void addRefsToFeature(const FeaturePtr& theFeature,
435 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
437 std::set<FeaturePtr>& theReferences)
439 if (theRecLevel > RECURSE_TOP_LEVEL)
443 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
444 return; // this feature is not in the selection list, so exists without references to it
445 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
447 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
448 aLast = aMainReferences.end();
449 for (; anIt != aLast; anIt++) {
450 FeaturePtr aRefFeature = *anIt;
451 if (theReferences.find(aRefFeature) == theReferences.end()) {
452 addRefsToFeature(aRefFeature, theReferencesMap, theRecLevel, theReferences);
453 theReferences.insert(aRefFeature);
458 // For each feature from the feature list it searches references to the feature and append them
459 // to the references map. This is a recusive method.
460 // \param theFeature a feature to find references
461 // \param theReferencesMap a map of references
462 // \param theReferences an out container of references
463 void findReferences(const std::set<FeaturePtr>& theFeatures,
464 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
465 const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
467 if (theRecLevel > RECURSE_TOP_LEVEL)
470 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
471 aLast = theFeatures.end();
472 for (; anIt != aLast; anIt++) {
473 FeaturePtr aFeature = *anIt;
474 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
475 DocumentPtr aSelFeatureDoc = aFeature->document();
476 std::set<FeaturePtr> aSelRefFeatures;
477 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
478 if (theUseComposite) { // do not filter selection
479 theReferences[aFeature] = aSelRefFeatures;
481 else { // filter references to skip composition features of the current feature
482 std::set<FeaturePtr> aFilteredFeatures;
483 std::set<FeaturePtr>::const_iterator aRefIt = aSelRefFeatures.begin(),
484 aRefLast = aSelRefFeatures.end();
485 for (; aRefIt != aRefLast; aRefIt++) {
486 FeaturePtr aCFeature = *aRefIt;
487 CompositeFeaturePtr aComposite =
488 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
489 if (aComposite.get() && aComposite->isSub(aFeature))
490 continue; /// composite of the current feature should be skipped
491 aFilteredFeatures.insert(aCFeature);
493 theReferences[aFeature] = aFilteredFeatures;
495 if (theUseRecursion) {
496 #ifdef DEBUG_CYCLING_1550
497 findReferences(aSelRefFeatures, theReferences, theUseComposite,
498 theUseRecursion, theRecLevel);
500 findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
508 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
509 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
510 const bool theUseComposite,
511 const bool theUseRecursion)
513 // For dependencies, find main_list:
514 // sk_1(ext_1, vertex_1)
515 // ext_1(bool_1, sk_3)
520 // Information: bool_1 is not selected, ext_2(bool_2) exists
521 // find all referenced features
522 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
524 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
526 #ifdef DEBUG_REMOVE_FEATURES
527 printMapInfo(aMainList, "firstDependencies");
529 // find all dependencies for each object:
530 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
531 // ext_1(bool_1, sk_3)
533 // sk_2(ext_2) + (bool_1)
536 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
537 aMainLast = aMainList.end();
538 for (; aMainIt != aMainLast; aMainIt++) {
539 FeaturePtr aMainListFeature = aMainIt->first;
541 //std::string aName = aMainListFeature->name();
542 std::set<FeaturePtr> aMainRefList = aMainIt->second;
544 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
546 int n = sprintf(aBuf, "%d", aMainRefList.size());
547 std::string aSize(aBuf);
548 std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
549 << ", references size = " << aSize << std::endl;
551 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
552 aLast = aMainRefList.end();
553 std::set<FeaturePtr> aResultRefList;
554 aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
555 for (; anIt != aLast; anIt++) {
556 FeaturePtr aFeature = *anIt;
558 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
559 std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
562 addRefsToFeature(aFeature, aMainList,
563 aRecLevel, aResultRefList/*aMainRefList*/);
565 theReferences[aMainListFeature] = aResultRefList;
567 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
568 std::cout << std::endl;
571 #ifdef DEBUG_REMOVE_FEATURES
572 printMapInfo(theReferences, "allDependencies");
576 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
577 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
578 std::set<FeaturePtr>& theFeaturesRefsTo)
580 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
581 aLast = theFeatures.end();
582 for (; anIt != aLast; anIt++) {
583 FeaturePtr aFeature = *anIt;
584 if (theReferences.find(aFeature) == theReferences.end())
586 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
587 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
588 for (; aRefIt != aRefLast; aRefIt++) {
589 FeaturePtr aRefFeature = *aRefIt;
590 CompositeFeaturePtr aComposite =
591 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
592 if (aComposite.get() && aComposite->isSub(aFeature))
593 continue; /// composite of the current feature should not be removed
595 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
596 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
597 theFeaturesRefsTo.insert(aRefFeature);
602 void getConcealedResults(const FeaturePtr& theFeature,
603 std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
605 SessionPtr aSession = ModelAPI_Session::get();
607 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
608 theFeature->data()->referencesToObjects(aRefs);
609 std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
610 anIt = aRefs.begin(), aLast = aRefs.end();
611 std::set<ResultPtr> alreadyThere; // to avoid duplications
612 for (; anIt != aLast; anIt++) {
613 if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
614 continue; // use only concealed attributes
615 std::list<ObjectPtr> anObjects = (*anIt).second;
616 std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
617 for (; anOIt != anOLast; anOIt++) {
618 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
619 if (aResult && aResult->isConcealed()) {
620 if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
621 alreadyThere.insert(aResult);
623 theResults.push_back(aResult);
629 std::pair<std::wstring, bool> getDefaultName(const std::shared_ptr<ModelAPI_Result>& theResult,
630 const bool theInherited,
631 const bool theRecursive)
633 typedef std::list< std::pair < std::string, std::list<ObjectPtr> > > ListOfReferences;
635 SessionPtr aSession = ModelAPI_Session::get();
637 ResultBodyPtr anOwnerRes = bodyOwner(theResult);
639 // names of sub-solids in CompSolid should be default (for example,
640 // result of boolean operation 'Boolean_1_1' is a CompSolid which is renamed to 'MyBOOL',
641 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1_1', 'Boolean_1_1_2' etc.)
642 std::wostringstream aDefaultName;
643 aDefaultName << getDefaultName(anOwnerRes).first;
644 aDefaultName << "_" << (bodyIndex(theResult) + 1);
645 return std::pair<std::wstring, bool>(aDefaultName.str(), false);
648 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult->data()->owner());
649 DataPtr aData = anOwner->data();
651 ListOfReferences aReferences;
652 // find first result with user-defined name
653 ListOfReferences::const_iterator aFoundRef = aReferences.end();
655 aData->referencesToObjects(aReferences);
657 for (ListOfReferences::const_iterator aRefIt = aReferences.begin();
658 aRefIt != aReferences.end(); ++aRefIt) {
659 bool isConcealed = aSession->validators()->isConcealed(anOwner->getKind(), aRefIt->first);
660 bool isMainArg = isConcealed &&
661 aSession->validators()->isMainArgument(anOwner->getKind(), aRefIt->first);
663 // check the referred object is a Body
664 // (for example, ExtrusionCut has a sketch as a first attribute which is concealing)
665 bool isBody = aRefIt->second.size() > 1 || (aRefIt->second.size() == 1 &&
666 aRefIt->second.front().get() &&
667 aRefIt->second.front()->groupName() == ModelAPI_ResultBody::group());
668 if (isBody && (isMainArg || aFoundRef == aReferences.end() ||
669 aData->isPrecedingAttribute(aRefIt->first, aFoundRef->first)))
677 // get the result number in the feature
678 int anIndexInOwner = 0;
679 const std::list<ResultPtr>& anOwnerResults = anOwner->results();
680 std::list<ResultPtr>::const_iterator aResIt = anOwnerResults.cbegin();
681 for(; aResIt != anOwnerResults.cend(); aResIt++) {
682 if(*aResIt == theResult)
687 // find an object which is concealed by theResult
688 if (aFoundRef != aReferences.end() && !aFoundRef->second.empty()) {
689 // store number of references for each object
690 std::map<ResultPtr, int> aNbRefToObject;
691 // search the object by result index
692 std::list<ObjectPtr>::const_iterator anObjIt = aFoundRef->second.begin();
693 int aResultIndex = anIndexInOwner;
694 while (--aResultIndex >= 0) {
695 ResultPtr aCurRes = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIt);
696 ResultBodyPtr aParentBody = ModelAPI_Tools::bodyOwner(aCurRes);
698 aCurRes = aParentBody;
699 if (aNbRefToObject.find(aCurRes) == aNbRefToObject.end())
700 aNbRefToObject[aCurRes] = 1;
702 aNbRefToObject[aCurRes] += 1;
705 if (anObjIt == aFoundRef->second.end()) {
706 anObjIt = aFoundRef->second.begin();
710 // check the result is a Body
711 if (anObjIt->get() && (*anObjIt)->groupName() == ModelAPI_ResultBody::group()) {
712 // check the result is part of CompSolid
713 ResultPtr anObjRes = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIt);
714 ResultBodyPtr aParentBody = ModelAPI_Tools::bodyOwner(anObjRes);
716 anObjRes = aParentBody;
718 // return name of reference result only if it has been renamed by the user,
719 // in other case compose a default name
720 if (anObjRes->data()->hasUserDefinedName() ||
721 (theRecursive && anObjRes->data()->name() != getDefaultName(anObjRes).first)) {
722 std::wstringstream aName;
723 aName << anObjRes->data()->name();
724 std::map<ResultPtr, int>::iterator aFound = aNbRefToObject.find(anObjRes);
725 if (aFound != aNbRefToObject.end()) {
726 // to generate unique name, add suffix if there are several results
727 // referring to the same shape
728 aName << "_" << aFound->second + 1;
730 return std::pair<std::wstring, bool>(aName.str(), true);
735 // compose default name by the name of the feature and the index of result
736 std::wstringstream aDefaultName;
737 aDefaultName << anOwner->name();
738 // if there are several results (issue #899: any number of result),
739 // add unique prefix starting from second
740 if (anIndexInOwner > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
741 aDefaultName << "_" << anIndexInOwner + 1;
742 return std::pair<std::wstring, bool>(aDefaultName.str(), false);
745 std::set<FeaturePtr> getParents(const FeaturePtr& theFeature)
747 std::set<FeaturePtr> aParents;
748 for (FeaturePtr aCurFeat = theFeature; aCurFeat; ) {
749 CompositeFeaturePtr aFoundComposite;
750 const std::set<AttributePtr>& aRefs = aCurFeat->data()->refsToMe();
751 for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
752 anIt != aRefs.end(); ++anIt) {
753 FeaturePtr aF = ModelAPI_Feature::feature((*anIt)->owner());
754 aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aF);
755 if (aFoundComposite && aFoundComposite->isSub(aCurFeat))
758 aFoundComposite = CompositeFeaturePtr();
761 if (aFoundComposite) {
762 aParents.insert(aFoundComposite);
763 aCurFeat = aFoundComposite;
766 // add the part containing high-level feature
767 SessionPtr aSession = ModelAPI_Session::get();
768 DocumentPtr aPartSetDoc = aSession->moduleDocument();
769 std::list<FeaturePtr> aPartSetFeatures = aPartSetDoc->allFeatures();
770 for (std::list<FeaturePtr>::const_iterator anIt = aPartSetFeatures.begin();
771 anIt != aPartSetFeatures.end(); ++anIt) {
772 aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
773 if (aFoundComposite && aFoundComposite->isSub(aCurFeat)) {
774 aParents.insert(aFoundComposite);
779 aCurFeat = FeaturePtr();
785 void fillShapeHierarchy(const GeomShapePtr& theShape,
786 const ResultPtr& theContext,
787 GeomAPI_ShapeHierarchy& theHierarchy)
789 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(theContext);
790 if (aResCompSolidPtr.get()) {
791 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
792 if (aContextShape->shapeType() <= GeomAPI_Shape::COMPSOLID) {
793 theHierarchy.addParent(theShape, aContextShape);
794 fillShapeHierarchy(aContextShape, aResCompSolidPtr, theHierarchy);
800 void removeResults(const std::list<ResultPtr>& theResults)
802 // collect all documents where the results must be removed
803 std::map<DocumentPtr, std::list<ResultPtr> > aDocs;
805 std::list<ResultPtr>::const_iterator aResIter = theResults.cbegin();
806 for(; aResIter != theResults.cend(); aResIter++) {
807 DocumentPtr aDoc = (*aResIter)->document();
808 if (!aDocs.count(aDoc))
809 aDocs[aDoc] = std::list<ResultPtr>();
810 aDocs[aDoc].push_back(*aResIter);
812 // create a "remove" feature in each doc
813 std::map<DocumentPtr, std::list<ResultPtr> >::iterator aDoc = aDocs.begin();
814 for(; aDoc != aDocs.end(); aDoc++) {
815 FeaturePtr aRemove = aDoc->first->addFeature("RemoveResults");
817 for(aResIter = aDoc->second.cbegin(); aResIter != aDoc->second.cend(); aResIter++)
818 aRemove->selectionList("results")->append(*aResIter, GeomShapePtr());
826 //**************************************************************
827 void setDeflection(ResultPtr theResult, const double theDeflection)
829 if (!theResult.get())
832 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
833 if (aDeflectionAttr.get() != NULL) {
834 aDeflectionAttr->setValue(theDeflection);
838 double getDeflection(const std::shared_ptr<ModelAPI_Result>& theResult)
840 double aDeflection = -1;
841 // get deflection from the attribute of the result
842 if (theResult.get() != NULL &&
843 theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
844 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
845 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
846 double aValue = aDoubleAttr->value();
847 if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
848 aDeflection = aDoubleAttr->value();
854 //******************************************************
855 void setColor(ResultPtr theResult, const std::vector<int>& theColor)
857 if (!theResult.get())
860 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
861 if (aColorAttr.get() != NULL) {
862 if (!aColorAttr->size()) {
863 aColorAttr->setSize(3);
865 aColorAttr->setValue(0, theColor[0]);
866 aColorAttr->setValue(1, theColor[1]);
867 aColorAttr->setValue(2, theColor[2]);
871 void getColor(const std::shared_ptr<ModelAPI_Result>& theResult, std::vector<int>& theColor)
874 // get color from the attribute of the result
875 if (theResult.get() != NULL &&
876 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
877 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
878 if (aColorAttr.get() && aColorAttr->size()) {
879 theColor.push_back(aColorAttr->value(0));
880 theColor.push_back(aColorAttr->value(1));
881 theColor.push_back(aColorAttr->value(2));
886 //******************************************************
887 void getIsoLines(const std::shared_ptr<ModelAPI_Result>& theResult,
888 bool& isVisible, std::vector<int>& theNbLines)
892 if (!theResult.get())
894 if (theResult->groupName() == ModelAPI_ResultConstruction::group()) {
895 theNbLines.push_back(0);
896 theNbLines.push_back(0);
899 // get color from the attribute of the result
900 AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
903 theNbLines.push_back(aAttr->value(0));
904 theNbLines.push_back(aAttr->value(1));
907 AttributeBooleanPtr aBoolAttr =
908 theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
909 if (aBoolAttr.get()) {
910 isVisible = aBoolAttr->value();
915 //******************************************************
916 void setIsoLines(ResultPtr theResult, const std::vector<int>& theIso)
918 if (!theResult.get())
921 AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
922 if (aAttr.get() != NULL) {
923 if (!aAttr->size()) {
926 aAttr->setValue(0, theIso[0]);
927 aAttr->setValue(1, theIso[1]);
931 //******************************************************
932 void showIsoLines(std::shared_ptr<ModelAPI_Result> theResult, bool theShow)
934 if (!theResult.get())
937 AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
938 if (aAttr.get() != NULL) {
939 aAttr->setValue(theShow);
943 //******************************************************
944 bool isShownIsoLines(std::shared_ptr<ModelAPI_Result> theResult)
946 if (!theResult.get())
949 AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
950 if (aAttr.get() != NULL) {
951 return aAttr->value();
956 //**************************************************************
957 void setTransparency(ResultPtr theResult, double theTransparency)
959 if (!theResult.get())
962 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
963 if (anAttribute.get() != NULL) {
964 anAttribute->setValue(theTransparency);
968 double getTransparency(const std::shared_ptr<ModelAPI_Result>& theResult)
970 double aTransparency = -1;
971 // get transparency from the attribute of the result
972 if (theResult.get() != NULL &&
973 theResult->data()->attribute(ModelAPI_Result::TRANSPARENCY_ID()).get() != NULL) {
974 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
975 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
976 aTransparency = aDoubleAttr->value();
979 return aTransparency;
982 void copyVisualizationAttrs(
983 std::shared_ptr<ModelAPI_Result> theSource, std::shared_ptr<ModelAPI_Result> theDest)
986 AttributeIntArrayPtr aSourceColor = theSource->data()->intArray(ModelAPI_Result::COLOR_ID());
987 if (aSourceColor.get() && aSourceColor->isInitialized() && aSourceColor->size()) {
988 AttributeIntArrayPtr aDestColor = theDest->data()->intArray(ModelAPI_Result::COLOR_ID());
989 if (aDestColor.get()) {
990 aDestColor->setSize(aSourceColor->size());
991 for(int a = 0; a < aSourceColor->size(); a++)
992 aDestColor->setValue(a, aSourceColor->value(a));
996 AttributeIntArrayPtr aSource = theSource->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
997 if (aSource.get() && aSource->isInitialized() && aSource->size()) {
998 AttributeIntArrayPtr aDest = theDest->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
1000 aDest->setSize(aSource->size());
1001 for(int a = 0; a < aSource->size(); a++)
1002 aDest->setValue(a, aSource->value(a));
1006 AttributeDoublePtr aSourceDefl = theSource->data()->real(ModelAPI_Result::DEFLECTION_ID());
1007 if (aSourceDefl.get() && aSourceDefl->isInitialized()) {
1008 AttributeDoublePtr aDestDefl = theDest->data()->real(ModelAPI_Result::DEFLECTION_ID());
1009 if (aDestDefl.get()) {
1010 aDestDefl->setValue(aSourceDefl->value());
1014 AttributeDoublePtr aSourceTransp = theSource->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1015 if (aSourceTransp.get() && aSourceTransp->isInitialized()) {
1016 AttributeDoublePtr aDestTransp = theDest->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1017 if (aDestTransp.get()) {
1018 aDestTransp->setValue(aSourceTransp->value());
1023 std::list<FeaturePtr> referencedFeatures(
1024 std::shared_ptr<ModelAPI_Result> theTarget, const std::string& theFeatureKind,
1025 const bool theSortResults)
1027 std::set<FeaturePtr> aResSet; // collect in the set initially to avoid duplicates
1028 std::list<ResultPtr> allSubRes;
1029 allSubRes.push_back(theTarget);
1030 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theTarget);
1032 allSubs(aBody, allSubRes);
1033 std::list<ResultPtr>::iterator aSub = allSubRes.begin();
1034 for(; aSub != allSubRes.end(); aSub++) {
1035 const std::set<AttributePtr>& aRefs = (*aSub)->data()->refsToMe();
1036 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1037 for(; aRef != aRefs.cend(); aRef++) {
1038 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1039 if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1040 aResSet.insert(aFeat);
1043 // add also feature of the target that may be referenced as a whole
1044 FeaturePtr aTargetFeature = theTarget->document()->feature(theTarget);
1045 const std::set<AttributePtr>& aRefs = aTargetFeature->data()->refsToMe();
1046 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1047 for(; aRef != aRefs.cend(); aRef++) {
1048 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1049 if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1050 aResSet.insert(aFeat);
1052 // check also Group-operations that may refer to groups - add them for theFeatureKind "Group"
1053 if (theFeatureKind == "Group") {
1054 std::set<FeaturePtr> aGroupOperations;
1055 for(bool aNeedIterate = true; aNeedIterate; ) {
1056 std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1057 for(; aResIter != aResSet.end(); aResIter++) {
1058 std::list<ResultPtr>::const_iterator aGroupRes = (*aResIter)->results().cbegin();
1059 for(; aGroupRes != (*aResIter)->results().cend(); aGroupRes++) {
1060 const std::set<AttributePtr>& aGroupRefs = (*aGroupRes)->data()->refsToMe();
1061 std::set<AttributePtr>::const_iterator aRefIt = aGroupRefs.cbegin();
1062 for(; aRefIt != aGroupRefs.cend(); aRefIt++) {
1063 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
1064 if (aFeat.get() && !aGroupOperations.count(aFeat) && !aFeat->results().empty() &&
1065 aFeat->firstResult()->groupName() == ModelAPI_ResultGroup::group()) {
1066 // iterate results of this group operation because it may be without theTarget shape
1067 GeomShapePtr aTargetShape = theTarget->shape();
1068 bool anIsIn = false;
1069 std::list<ResultPtr>::const_iterator anOpRes = aFeat->results().cbegin();
1070 for(; anOpRes != aFeat->results().cend() && !anIsIn; anOpRes++) {
1071 GeomShapePtr anOpShape = (*anOpRes)->shape();
1072 if (!anOpShape.get() || anOpShape->isNull())
1074 for(GeomAPI_ShapeIterator aSubIt(anOpShape); aSubIt.more(); aSubIt.next()) {
1075 if (aTargetShape->isSubShape(aSubIt.current(), false)) {
1082 aGroupOperations.insert(aFeat);
1087 // insert all new group operations into result and if they are, check for next dependencies
1088 aNeedIterate = false;
1089 std::set<FeaturePtr>::iterator aGroupOpIter = aGroupOperations.begin();
1090 for(; aGroupOpIter != aGroupOperations.end(); aGroupOpIter++) {
1091 if (aResSet.find(*aGroupOpIter) == aResSet.end()) {
1092 aResSet.insert(*aGroupOpIter);
1093 aNeedIterate = true;
1099 std::list<FeaturePtr> aResList;
1100 std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1101 for(; aResIter != aResSet.end(); aResIter++) {
1102 if (theSortResults) { // sort results by creation-order
1103 std::list<FeaturePtr>::iterator aListIter = aResList.begin();
1104 for(; aListIter != aResList.end(); aListIter++) {
1105 if ((*aResIter)->document()->isLater(*aListIter, *aResIter))
1108 if (aListIter == aResList.end()) // goes to the end
1109 aResList.push_back(*aResIter);
1111 aResList.insert(aListIter, *aResIter);
1112 } else //just push to the end unsorted
1113 aResList.push_back(*aResIter);
1120 } // namespace ModelAPI_Tools