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 std::string aMsg = "The sketch base plane is invalid, ";
171 aMsg += "please push 'Change sketch plane' button to reselect it.";
172 anError = Config_Translator::translate(aComposite->getKind(), aMsg);
184 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup,
185 const std::wstring& theName)
187 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
188 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
189 if (anObject->data()->name() == theName)
196 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
197 const std::wstring& theName, double& outValue, ResultParameterPtr& theParam)
199 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
200 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
203 // avoid usage of parameters created later than the initial parameter
205 if (theSearcher.get()) {
206 FeaturePtr aParamFeat = theDocument->feature(theParam);
207 if (aParamFeat == theSearcher || theDocument->isLater(aParamFeat, theSearcher))
210 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
211 outValue = aValueAttribute->value();
215 bool findVariable(FeaturePtr theSearcher, const std::wstring& theName, double& outValue,
216 ResultParameterPtr& theParam, const DocumentPtr& theDocument)
218 SessionPtr aSession = ModelAPI_Session::get();
219 std::list<DocumentPtr> aDocList;
220 DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
221 if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
223 DocumentPtr aRootDocument = aSession->moduleDocument();
224 if (aDocument != aRootDocument) {
225 // any parameters in PartSet is okindependently on the Part position (issu #1504)
226 if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
232 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
234 // to optimize and avoid of crash on partset document close
235 // (don't touch the sub-document structure)
236 if (theMain != theSub) {
237 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
238 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
239 theMain->object(ModelAPI_ResultPart::group(), a));
240 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
248 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
250 // to optimize and avoid of crash on partset document close
251 // (don't touch the sub-document structure)
252 if (theMain != theSub) {
253 // iteration from top to bottom to avoid finding the movement documents before the original
254 int aSize = theMain->size(ModelAPI_Feature::group());
255 for (int a = 0; a < aSize; a++) {
256 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
257 theMain->object(ModelAPI_Feature::group(), a));
258 if (aPartFeat.get()) {
259 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
260 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
261 for(; aRes != aResList.end(); aRes++) {
262 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
264 if (aPart->isActivated() && aPart->partDoc() == theSub)
266 } else break; // if the first is not Part, others are also not
274 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
276 if (theFeature.get() && theFeature->data() && theFeature->data()->isValid()) {
277 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
278 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
279 for(; aRefIter != aRefs.end(); aRefIter++) {
280 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
281 ((*aRefIter)->owner());
282 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
286 return CompositeFeaturePtr(); // not found
289 ResultBodyPtr bodyOwner(const ResultPtr& theSub, const bool theRoot)
292 ObjectPtr aParent = theSub->document()->parent(theSub);
294 if (theRoot) { // try to find parent of parent
295 ResultPtr aResultParent = std::dynamic_pointer_cast<ModelAPI_Result>(aParent);
296 ResultBodyPtr aGrandParent = bodyOwner(aResultParent, true);
297 if (aGrandParent.get())
298 aParent = aGrandParent;
300 return std::dynamic_pointer_cast<ModelAPI_ResultBody>(aParent);
303 return ResultBodyPtr(); // not found
306 int bodyIndex(const ResultPtr& theSub)
309 ResultBodyPtr aParent = bodyOwner(theSub);
311 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
312 if (aBody.get() && aParent->isSub(aBody, anIndex))
315 return anIndex; // not found
318 bool hasSubResults(const ResultPtr& theResult)
320 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
321 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
324 void allSubs(const ResultBodyPtr& theResult, std::list<ResultPtr>& theResults,
325 const bool theLowerOnly) {
326 // iterate sub-bodies of compsolid
327 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
329 int aNumSub = aComp->numberOfSubs();
330 for (int a = 0; a < aNumSub; a++) {
331 ResultBodyPtr aSub = aComp->subResult(a);
332 if (!theLowerOnly || aSub->numberOfSubs() == 0)
333 theResults.push_back(aSub);
334 allSubs(aSub, theResults);
339 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
341 if (!theFeature.get()) // safety: for empty feature no results
343 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
344 std::list<ResultPtr>::const_iterator aRIter = aResults.begin();
345 for (; aRIter != aResults.cend(); aRIter++) {
346 theResults.push_back(*aRIter);
347 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRIter);
348 allSubs(aResult, theResults);
352 //******************************************************************
353 bool allDocumentsActivated(std::wstring& theNotActivatedNames)
355 theNotActivatedNames = L"";
356 bool anAllPartActivated = true;
358 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
359 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
360 for (int i = 0; i < aSize; i++) {
361 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
362 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
363 if (!aPart->isActivated()) {
364 anAllPartActivated = false;
365 if (!theNotActivatedNames.empty())
366 theNotActivatedNames += L", ";
367 theNotActivatedNames += aObject->data()->name().c_str();
370 return anAllPartActivated;
373 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
374 const bool /*theFlushRedisplay*/,
375 const bool theUseComposite,
376 const bool theUseRecursion)
378 #ifdef DEBUG_REMOVE_FEATURES
379 printListInfo(theFeatures, "selection: ");
382 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
383 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
384 #ifdef DEBUG_REMOVE_FEATURES
385 printMapInfo(aReferences, "allDependencies: ");
388 std::set<FeaturePtr> aFeaturesRefsTo;
389 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
390 #ifdef DEBUG_REMOVE_FEATURES
391 printListInfo(aFeaturesRefsTo, "references: ");
394 std::set<FeaturePtr> aFeatures = theFeatures;
395 if (!aFeaturesRefsTo.empty())
396 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
397 #ifdef DEBUG_REMOVE_FEATURES
398 printListInfo(aFeatures, "removeFeatures: ");
401 return ModelAPI_Tools::removeFeatures(aFeatures, false);
404 //***********************************************************************
405 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
406 const bool theFlushRedisplay)
409 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
410 aLast = theFeatures.end();
411 for (; anIt != aLast; anIt++) {
412 FeaturePtr aFeature = *anIt;
413 if (aFeature.get()) {
414 DocumentPtr aDoc = aFeature->document();
415 // flush REDISPLAY signal after remove feature
416 aDoc->removeFeature(aFeature);
420 if (isDone && theFlushRedisplay) {
421 // the redisplay signal should be flushed in order to erase
422 // the feature presentation in the viewer
423 // if should be done after removeFeature() of document
424 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
429 //***********************************************************************
430 // Fills the references list by all references of the feature from the references map.
431 // This is a recusive method to find references by next found feature in the map of references.
432 // \param theFeature a feature to find references
433 // \param theReferencesMap a map of references
434 // \param theReferences an out container of references
435 void addRefsToFeature(const FeaturePtr& theFeature,
436 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
438 std::set<FeaturePtr>& theReferences)
440 if (theRecLevel > RECURSE_TOP_LEVEL)
444 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
445 return; // this feature is not in the selection list, so exists without references to it
446 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
448 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
449 aLast = aMainReferences.end();
450 for (; anIt != aLast; anIt++) {
451 FeaturePtr aRefFeature = *anIt;
452 if (theReferences.find(aRefFeature) == theReferences.end()) {
453 addRefsToFeature(aRefFeature, theReferencesMap, theRecLevel, theReferences);
454 theReferences.insert(aRefFeature);
459 // For each feature from the feature list it searches references to the feature and append them
460 // to the references map. This is a recusive method.
461 // \param theFeature a feature to find references
462 // \param theReferencesMap a map of references
463 // \param theReferences an out container of references
464 void findReferences(const std::set<FeaturePtr>& theFeatures,
465 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
466 const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
468 if (theRecLevel > RECURSE_TOP_LEVEL)
471 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
472 aLast = theFeatures.end();
473 for (; anIt != aLast; anIt++) {
474 FeaturePtr aFeature = *anIt;
475 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
476 DocumentPtr aSelFeatureDoc = aFeature->document();
477 std::set<FeaturePtr> aSelRefFeatures;
478 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
479 if (theUseComposite) { // do not filter selection
480 theReferences[aFeature] = aSelRefFeatures;
482 else { // filter references to skip composition features of the current feature
483 std::set<FeaturePtr> aFilteredFeatures;
484 std::set<FeaturePtr>::const_iterator aRefIt = aSelRefFeatures.begin(),
485 aRefLast = aSelRefFeatures.end();
486 for (; aRefIt != aRefLast; aRefIt++) {
487 FeaturePtr aCFeature = *aRefIt;
488 CompositeFeaturePtr aComposite =
489 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
490 if (aComposite.get() && aComposite->isSub(aFeature))
491 continue; /// composite of the current feature should be skipped
492 aFilteredFeatures.insert(aCFeature);
494 theReferences[aFeature] = aFilteredFeatures;
496 if (theUseRecursion) {
497 #ifdef DEBUG_CYCLING_1550
498 findReferences(aSelRefFeatures, theReferences, theUseComposite,
499 theUseRecursion, theRecLevel);
501 findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
509 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
510 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
511 const bool theUseComposite,
512 const bool theUseRecursion)
514 // For dependencies, find main_list:
515 // sk_1(ext_1, vertex_1)
516 // ext_1(bool_1, sk_3)
521 // Information: bool_1 is not selected, ext_2(bool_2) exists
522 // find all referenced features
523 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
525 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
527 #ifdef DEBUG_REMOVE_FEATURES
528 printMapInfo(aMainList, "firstDependencies");
530 // find all dependencies for each object:
531 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
532 // ext_1(bool_1, sk_3)
534 // sk_2(ext_2) + (bool_1)
537 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
538 aMainLast = aMainList.end();
539 for (; aMainIt != aMainLast; aMainIt++) {
540 FeaturePtr aMainListFeature = aMainIt->first;
542 //std::string aName = aMainListFeature->name();
543 std::set<FeaturePtr> aMainRefList = aMainIt->second;
545 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
547 int n = sprintf(aBuf, "%d", aMainRefList.size());
548 std::string aSize(aBuf);
549 std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
550 << ", references size = " << aSize << std::endl;
552 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
553 aLast = aMainRefList.end();
554 std::set<FeaturePtr> aResultRefList;
555 aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
556 for (; anIt != aLast; anIt++) {
557 FeaturePtr aFeature = *anIt;
559 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
560 std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
563 addRefsToFeature(aFeature, aMainList,
564 aRecLevel, aResultRefList/*aMainRefList*/);
566 theReferences[aMainListFeature] = aResultRefList;
568 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
569 std::cout << std::endl;
572 #ifdef DEBUG_REMOVE_FEATURES
573 printMapInfo(theReferences, "allDependencies");
577 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
578 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
579 std::set<FeaturePtr>& theFeaturesRefsTo)
581 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
582 aLast = theFeatures.end();
583 for (; anIt != aLast; anIt++) {
584 FeaturePtr aFeature = *anIt;
585 if (theReferences.find(aFeature) == theReferences.end())
587 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
588 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
589 for (; aRefIt != aRefLast; aRefIt++) {
590 FeaturePtr aRefFeature = *aRefIt;
591 CompositeFeaturePtr aComposite =
592 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
593 if (aComposite.get() && aComposite->isSub(aFeature))
594 continue; /// composite of the current feature should not be removed
596 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
597 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
598 theFeaturesRefsTo.insert(aRefFeature);
603 void getConcealedResults(const FeaturePtr& theFeature,
604 std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
606 SessionPtr aSession = ModelAPI_Session::get();
608 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
609 theFeature->data()->referencesToObjects(aRefs);
610 std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
611 anIt = aRefs.begin(), aLast = aRefs.end();
612 std::set<ResultPtr> alreadyThere; // to avoid duplications
613 for (; anIt != aLast; anIt++) {
614 if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
615 continue; // use only concealed attributes
616 std::list<ObjectPtr> anObjects = (*anIt).second;
617 std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
618 for (; anOIt != anOLast; anOIt++) {
619 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
620 if (aResult && aResult->isConcealed()) {
621 if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
622 alreadyThere.insert(aResult);
624 theResults.push_back(aResult);
630 std::pair<std::wstring, bool> getDefaultName(const std::shared_ptr<ModelAPI_Result>& theResult,
631 const bool theInherited,
632 const bool theRecursive)
634 typedef std::list< std::pair < std::string, std::list<ObjectPtr> > > ListOfReferences;
636 SessionPtr aSession = ModelAPI_Session::get();
638 ResultBodyPtr anOwnerRes = bodyOwner(theResult);
640 // names of sub-solids in CompSolid should be default (for example,
641 // result of boolean operation 'Boolean_1_1' is a CompSolid which is renamed to 'MyBOOL',
642 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1_1', 'Boolean_1_1_2' etc.)
643 std::wostringstream aDefaultName;
644 aDefaultName << getDefaultName(anOwnerRes).first;
645 aDefaultName << "_" << (bodyIndex(theResult) + 1);
646 return std::pair<std::wstring, bool>(aDefaultName.str(), false);
649 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult->data()->owner());
650 DataPtr aData = anOwner->data();
652 ListOfReferences aReferences;
653 // find first result with user-defined name
654 ListOfReferences::const_iterator aFoundRef = aReferences.end();
656 aData->referencesToObjects(aReferences);
658 for (ListOfReferences::const_iterator aRefIt = aReferences.begin();
659 aRefIt != aReferences.end(); ++aRefIt) {
660 bool isConcealed = aSession->validators()->isConcealed(anOwner->getKind(), aRefIt->first);
661 bool isMainArg = isConcealed &&
662 aSession->validators()->isMainArgument(anOwner->getKind(), aRefIt->first);
664 // check the referred object is a Body
665 // (for example, ExtrusionCut has a sketch as a first attribute which is concealing)
666 bool isBody = aRefIt->second.size() > 1 || (aRefIt->second.size() == 1 &&
667 aRefIt->second.front().get() &&
668 aRefIt->second.front()->groupName() == ModelAPI_ResultBody::group());
669 if (isBody && (isMainArg || aFoundRef == aReferences.end() ||
670 aData->isPrecedingAttribute(aRefIt->first, aFoundRef->first)))
678 // get the result number in the feature
679 int anIndexInOwner = 0;
680 const std::list<ResultPtr>& anOwnerResults = anOwner->results();
681 std::list<ResultPtr>::const_iterator aResIt = anOwnerResults.cbegin();
682 for(; aResIt != anOwnerResults.cend(); aResIt++) {
683 if(*aResIt == theResult)
688 // find an object which is concealed by theResult
689 if (aFoundRef != aReferences.end() && !aFoundRef->second.empty()) {
690 // store number of references for each object
691 std::map<ResultPtr, int> aNbRefToObject;
692 // search the object by result index
693 std::list<ObjectPtr>::const_iterator anObjIt = aFoundRef->second.begin();
694 int aResultIndex = anIndexInOwner;
695 while (--aResultIndex >= 0) {
696 ResultPtr aCurRes = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIt);
697 ResultBodyPtr aParentBody = ModelAPI_Tools::bodyOwner(aCurRes);
699 aCurRes = aParentBody;
700 if (aNbRefToObject.find(aCurRes) == aNbRefToObject.end())
701 aNbRefToObject[aCurRes] = 1;
703 aNbRefToObject[aCurRes] += 1;
706 if (anObjIt == aFoundRef->second.end()) {
707 anObjIt = aFoundRef->second.begin();
711 // check the result is a Body
712 if (anObjIt->get() && (*anObjIt)->groupName() == ModelAPI_ResultBody::group()) {
713 // check the result is part of CompSolid
714 ResultPtr anObjRes = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIt);
715 ResultBodyPtr aParentBody = ModelAPI_Tools::bodyOwner(anObjRes);
717 anObjRes = aParentBody;
719 // return name of reference result only if it has been renamed by the user,
720 // in other case compose a default name
721 if (anObjRes->data()->hasUserDefinedName() ||
722 (theRecursive && anObjRes->data()->name() != getDefaultName(anObjRes).first)) {
723 std::wstringstream aName;
724 aName << anObjRes->data()->name();
725 std::map<ResultPtr, int>::iterator aFound = aNbRefToObject.find(anObjRes);
726 if (aFound != aNbRefToObject.end()) {
727 // to generate unique name, add suffix if there are several results
728 // referring to the same shape
729 aName << "_" << aFound->second + 1;
731 return std::pair<std::wstring, bool>(aName.str(), true);
736 // compose default name by the name of the feature and the index of result
737 std::wstringstream aDefaultName;
738 aDefaultName << anOwner->name();
739 // if there are several results (issue #899: any number of result),
740 // add unique prefix starting from second
741 if (anIndexInOwner > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
742 aDefaultName << "_" << anIndexInOwner + 1;
743 return std::pair<std::wstring, bool>(aDefaultName.str(), false);
746 std::set<FeaturePtr> getParents(const FeaturePtr& theFeature)
748 std::set<FeaturePtr> aParents;
749 for (FeaturePtr aCurFeat = theFeature; aCurFeat; ) {
750 CompositeFeaturePtr aFoundComposite;
751 const std::set<AttributePtr>& aRefs = aCurFeat->data()->refsToMe();
752 for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
753 anIt != aRefs.end(); ++anIt) {
754 FeaturePtr aF = ModelAPI_Feature::feature((*anIt)->owner());
755 aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aF);
756 if (aFoundComposite && aFoundComposite->isSub(aCurFeat))
759 aFoundComposite = CompositeFeaturePtr();
762 if (aFoundComposite) {
763 aParents.insert(aFoundComposite);
764 aCurFeat = aFoundComposite;
767 // add the part containing high-level feature
768 SessionPtr aSession = ModelAPI_Session::get();
769 DocumentPtr aPartSetDoc = aSession->moduleDocument();
770 std::list<FeaturePtr> aPartSetFeatures = aPartSetDoc->allFeatures();
771 for (std::list<FeaturePtr>::const_iterator anIt = aPartSetFeatures.begin();
772 anIt != aPartSetFeatures.end(); ++anIt) {
773 aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
774 if (aFoundComposite && aFoundComposite->isSub(aCurFeat)) {
775 aParents.insert(aFoundComposite);
780 aCurFeat = FeaturePtr();
786 void fillShapeHierarchy(const GeomShapePtr& theShape,
787 const ResultPtr& theContext,
788 GeomAPI_ShapeHierarchy& theHierarchy)
790 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(theContext);
791 if (aResCompSolidPtr.get()) {
792 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
793 if (aContextShape->shapeType() <= GeomAPI_Shape::COMPSOLID) {
794 theHierarchy.addParent(theShape, aContextShape);
795 fillShapeHierarchy(aContextShape, aResCompSolidPtr, theHierarchy);
801 void removeResults(const std::list<ResultPtr>& theResults)
803 // collect all documents where the results must be removed
804 std::map<DocumentPtr, std::list<ResultPtr> > aDocs;
806 std::list<ResultPtr>::const_iterator aResIter = theResults.cbegin();
807 for(; aResIter != theResults.cend(); aResIter++) {
808 DocumentPtr aDoc = (*aResIter)->document();
809 if (!aDocs.count(aDoc))
810 aDocs[aDoc] = std::list<ResultPtr>();
811 aDocs[aDoc].push_back(*aResIter);
813 // create a "remove" feature in each doc
814 std::map<DocumentPtr, std::list<ResultPtr> >::iterator aDoc = aDocs.begin();
815 for(; aDoc != aDocs.end(); aDoc++) {
816 FeaturePtr aRemove = aDoc->first->addFeature("RemoveResults");
818 for(aResIter = aDoc->second.cbegin(); aResIter != aDoc->second.cend(); aResIter++)
819 aRemove->selectionList("results")->append(*aResIter, GeomShapePtr());
827 //**************************************************************
828 void setDeflection(ResultPtr theResult, const double theDeflection)
830 if (!theResult.get())
833 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
834 if (aDeflectionAttr.get() != NULL) {
835 aDeflectionAttr->setValue(theDeflection);
839 double getDeflection(const std::shared_ptr<ModelAPI_Result>& theResult)
841 double aDeflection = -1;
842 // get deflection from the attribute of the result
843 if (theResult.get() != NULL &&
844 theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
845 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
846 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
847 double aValue = aDoubleAttr->value();
848 if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
849 aDeflection = aDoubleAttr->value();
855 //******************************************************
856 void setColor(ResultPtr theResult, const std::vector<int>& theColor)
858 if (!theResult.get())
861 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
862 if (aColorAttr.get() != NULL) {
863 if (!aColorAttr->size()) {
864 aColorAttr->setSize(3);
866 aColorAttr->setValue(0, theColor[0]);
867 aColorAttr->setValue(1, theColor[1]);
868 aColorAttr->setValue(2, theColor[2]);
872 void getColor(const std::shared_ptr<ModelAPI_Result>& theResult, std::vector<int>& theColor)
875 // get color from the attribute of the result
876 if (theResult.get() != NULL &&
877 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
878 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
879 if (aColorAttr.get() && aColorAttr->size()) {
880 theColor.push_back(aColorAttr->value(0));
881 theColor.push_back(aColorAttr->value(1));
882 theColor.push_back(aColorAttr->value(2));
887 //******************************************************
888 void getIsoLines(const std::shared_ptr<ModelAPI_Result>& theResult,
889 bool& isVisible, std::vector<int>& theNbLines)
893 if (!theResult.get())
895 if (theResult->groupName() == ModelAPI_ResultConstruction::group()) {
896 theNbLines.push_back(0);
897 theNbLines.push_back(0);
900 // get color from the attribute of the result
901 AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
904 theNbLines.push_back(aAttr->value(0));
905 theNbLines.push_back(aAttr->value(1));
908 AttributeBooleanPtr aBoolAttr =
909 theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
910 if (aBoolAttr.get()) {
911 isVisible = aBoolAttr->value();
916 //******************************************************
917 void setIsoLines(ResultPtr theResult, const std::vector<int>& theIso)
919 if (!theResult.get())
922 AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
923 if (aAttr.get() != NULL) {
924 if (!aAttr->size()) {
927 aAttr->setValue(0, theIso[0]);
928 aAttr->setValue(1, theIso[1]);
932 //******************************************************
933 void showIsoLines(std::shared_ptr<ModelAPI_Result> theResult, bool theShow)
935 if (!theResult.get())
938 AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
939 if (aAttr.get() != NULL) {
940 aAttr->setValue(theShow);
944 //******************************************************
945 bool isShownIsoLines(std::shared_ptr<ModelAPI_Result> theResult)
947 if (!theResult.get())
950 AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
951 if (aAttr.get() != NULL) {
952 return aAttr->value();
957 //**************************************************************
958 void setTransparency(ResultPtr theResult, double theTransparency)
960 if (!theResult.get())
963 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
964 if (anAttribute.get() != NULL) {
965 anAttribute->setValue(theTransparency);
969 double getTransparency(const std::shared_ptr<ModelAPI_Result>& theResult)
971 double aTransparency = -1;
972 // get transparency from the attribute of the result
973 if (theResult.get() != NULL &&
974 theResult->data()->attribute(ModelAPI_Result::TRANSPARENCY_ID()).get() != NULL) {
975 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
976 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
977 aTransparency = aDoubleAttr->value();
980 return aTransparency;
983 void copyVisualizationAttrs(
984 std::shared_ptr<ModelAPI_Result> theSource, std::shared_ptr<ModelAPI_Result> theDest)
987 AttributeIntArrayPtr aSourceColor = theSource->data()->intArray(ModelAPI_Result::COLOR_ID());
988 if (aSourceColor.get() && aSourceColor->isInitialized() && aSourceColor->size()) {
989 AttributeIntArrayPtr aDestColor = theDest->data()->intArray(ModelAPI_Result::COLOR_ID());
990 if (aDestColor.get()) {
991 aDestColor->setSize(aSourceColor->size());
992 for(int a = 0; a < aSourceColor->size(); a++)
993 aDestColor->setValue(a, aSourceColor->value(a));
997 AttributeIntArrayPtr aSource = theSource->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
998 if (aSource.get() && aSource->isInitialized() && aSource->size()) {
999 AttributeIntArrayPtr aDest = theDest->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
1001 aDest->setSize(aSource->size());
1002 for(int a = 0; a < aSource->size(); a++)
1003 aDest->setValue(a, aSource->value(a));
1007 AttributeDoublePtr aSourceDefl = theSource->data()->real(ModelAPI_Result::DEFLECTION_ID());
1008 if (aSourceDefl.get() && aSourceDefl->isInitialized()) {
1009 AttributeDoublePtr aDestDefl = theDest->data()->real(ModelAPI_Result::DEFLECTION_ID());
1010 if (aDestDefl.get()) {
1011 aDestDefl->setValue(aSourceDefl->value());
1015 AttributeDoublePtr aSourceTransp = theSource->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1016 if (aSourceTransp.get() && aSourceTransp->isInitialized()) {
1017 AttributeDoublePtr aDestTransp = theDest->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1018 if (aDestTransp.get()) {
1019 aDestTransp->setValue(aSourceTransp->value());
1024 std::list<FeaturePtr> referencedFeatures(
1025 std::shared_ptr<ModelAPI_Result> theTarget, const std::string& theFeatureKind,
1026 const bool theSortResults)
1028 std::set<FeaturePtr> aResSet; // collect in the set initially to avoid duplicates
1029 std::list<ResultPtr> allSubRes;
1030 allSubRes.push_back(theTarget);
1031 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theTarget);
1033 allSubs(aBody, allSubRes);
1034 std::list<ResultPtr>::iterator aSub = allSubRes.begin();
1035 for(; aSub != allSubRes.end(); aSub++) {
1036 const std::set<AttributePtr>& aRefs = (*aSub)->data()->refsToMe();
1037 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1038 for(; aRef != aRefs.cend(); aRef++) {
1039 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1040 if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1041 aResSet.insert(aFeat);
1044 // add also feature of the target that may be referenced as a whole
1045 FeaturePtr aTargetFeature = theTarget->document()->feature(theTarget);
1046 const std::set<AttributePtr>& aRefs = aTargetFeature->data()->refsToMe();
1047 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1048 for(; aRef != aRefs.cend(); aRef++) {
1049 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1050 if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1051 aResSet.insert(aFeat);
1053 // check also Group-operations that may refer to groups - add them for theFeatureKind "Group"
1054 if (theFeatureKind == "Group") {
1055 std::set<FeaturePtr> aGroupOperations;
1056 for(bool aNeedIterate = true; aNeedIterate; ) {
1057 std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1058 for(; aResIter != aResSet.end(); aResIter++) {
1059 std::list<ResultPtr>::const_iterator aGroupRes = (*aResIter)->results().cbegin();
1060 for(; aGroupRes != (*aResIter)->results().cend(); aGroupRes++) {
1061 const std::set<AttributePtr>& aGroupRefs = (*aGroupRes)->data()->refsToMe();
1062 std::set<AttributePtr>::const_iterator aRefIt = aGroupRefs.cbegin();
1063 for(; aRefIt != aGroupRefs.cend(); aRefIt++) {
1064 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
1065 if (aFeat.get() && !aGroupOperations.count(aFeat) && !aFeat->results().empty() &&
1066 aFeat->firstResult()->groupName() == ModelAPI_ResultGroup::group()) {
1067 // iterate results of this group operation because it may be without theTarget shape
1068 GeomShapePtr aTargetShape = theTarget->shape();
1069 bool anIsIn = false;
1070 std::list<ResultPtr>::const_iterator anOpRes = aFeat->results().cbegin();
1071 for(; anOpRes != aFeat->results().cend() && !anIsIn; anOpRes++) {
1072 GeomShapePtr anOpShape = (*anOpRes)->shape();
1073 if (!anOpShape.get() || anOpShape->isNull())
1075 for(GeomAPI_ShapeIterator aSubIt(anOpShape); aSubIt.more(); aSubIt.next()) {
1076 if (aTargetShape->isSubShape(aSubIt.current(), false)) {
1083 aGroupOperations.insert(aFeat);
1088 // insert all new group operations into result and if they are, check for next dependencies
1089 aNeedIterate = false;
1090 std::set<FeaturePtr>::iterator aGroupOpIter = aGroupOperations.begin();
1091 for(; aGroupOpIter != aGroupOperations.end(); aGroupOpIter++) {
1092 if (aResSet.find(*aGroupOpIter) == aResSet.end()) {
1093 aResSet.insert(*aGroupOpIter);
1094 aNeedIterate = true;
1100 std::list<FeaturePtr> aResList;
1101 std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1102 for(; aResIter != aResSet.end(); aResIter++) {
1103 if (theSortResults) { // sort results by creation-order
1104 std::list<FeaturePtr>::iterator aListIter = aResList.begin();
1105 for(; aListIter != aResList.end(); aListIter++) {
1106 if ((*aResIter)->document()->isLater(*aListIter, *aResIter))
1109 if (aListIter == aResList.end()) // goes to the end
1110 aResList.push_back(*aResIter);
1112 aResList.insert(aListIter, *aResIter);
1113 } else //just push to the end unsorted
1114 aResList.push_back(*aResIter);
1121 } // namespace ModelAPI_Tools