1 // Copyright (C) 2014-2020 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 <ModelAPI_Events.h>
44 #include <GeomAPI_ShapeHierarchy.h>
45 #include <GeomAPI_ShapeIterator.h>
47 #define RECURSE_TOP_LEVEL 50
49 //#define DEBUG_REMOVE_FEATURES
50 //#define DEBUG_REMOVE_FEATURES_RECURSE
51 //#define DEBUG_CYCLING_1550
53 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
55 std::string getFeatureInfo(FeaturePtr theFeature)
57 if (!theFeature.get())
59 //std::ostringstream aPtrStr;
60 //aPtrStr << "[" << theFeature.get() << "] ";
61 std::string aFeatureInfo = /*aPtrStr.str() + */theFeature->name();
62 CompositeFeaturePtr aComposite = ModelAPI_Tools::compositeOwner(theFeature);
63 if (aComposite.get()) {
64 aFeatureInfo = aFeatureInfo + "[in " + aComposite->name() + "]";
70 #ifdef DEBUG_REMOVE_FEATURES
71 void printMapInfo(const std::map<FeaturePtr, std::set<FeaturePtr> >& theMainList,
72 const std::string& thePrefix)
74 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = theMainList.begin(),
75 aMainLast = theMainList.end();
77 for (; aMainIt != aMainLast; aMainIt++) {
78 FeaturePtr aMainListFeature = aMainIt->first;
79 std::set<FeaturePtr> aMainRefList = aMainIt->second;
80 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(), aLast = aMainRefList.end();
81 std::string aRefsInfo;
82 for (; anIt != aLast; anIt++) {
83 aRefsInfo += (*anIt)->name().c_str();
87 if (!aRefsInfo.empty()) {
88 anInfo = anInfo + aMainListFeature->name().c_str() + ": " + aRefsInfo + "\n";
91 std::cout << thePrefix.c_str() << " [feature: references to]: \n" << anInfo.c_str() << std::endl;
94 void printListInfo(const std::set<FeaturePtr>& theMainList,
95 const std::string& thePrefix)
97 std::set<FeaturePtr>::const_iterator aMainIt = theMainList.begin(),
98 aMainLast = theMainList.end();
100 for (; aMainIt != aMainLast; aMainIt++) {
101 FeaturePtr aRefFeature = *aMainIt;
102 anInfo += aRefFeature->name().c_str();
103 if (aMainIt != aMainLast)
106 std::cout << thePrefix.c_str() << ": " << anInfo.c_str() << std::endl;
110 namespace ModelAPI_Tools {
112 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
114 return theResult->shape();
118 const char* toString(ModelAPI_ExecState theExecState)
120 switch (theExecState) {
121 case ModelAPI_StateDone: return "Done";
122 case ModelAPI_StateMustBeUpdated: return "Must be updated";
123 case ModelAPI_StateExecFailed: return "Execution failed";
124 case ModelAPI_StateInvalidArgument: return "Invalid argument";
125 case ModelAPI_StateNothing: return "Empty state";
126 default: return "Unknown ExecState.";
130 std::string getFeatureError(const FeaturePtr& theFeature)
133 if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
136 // to be removed later, this error should be got from the feature
137 if (theFeature->data()->execState() == ModelAPI_StateDone ||
138 theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
141 // set error indication
142 anError = theFeature->error();
143 if (anError.empty()) {
144 bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
145 || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
147 anError = toString(theFeature->data()->execState());
148 // If the feature is Composite and error is StateInvalidArgument,
149 // error text should include error of first invalid sub-feature. Otherwise
150 // it is not clear what is the reason of the invalid argument.
151 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
152 CompositeFeaturePtr aComposite =
153 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
155 for (int i = 0, aSize = aComposite->numberOfSubs(); i < aSize; i++) {
156 FeaturePtr aSubFeature = aComposite->subFeature(i);
157 std::string aSubFeatureError = getFeatureError(aSubFeature);
158 if (!aSubFeatureError.empty()) {
159 anError = anError + " in " + aSubFeature->getKind() + ".\n" + aSubFeatureError;
172 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup,
173 const std::string& theName)
175 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
176 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
177 if (anObject->data()->name() == theName)
184 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
185 const std::string& theName, double& outValue, ResultParameterPtr& theParam)
187 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
188 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
191 // avoid usage of parameters created later than the initial parameter
193 if (theSearcher.get()) {
194 FeaturePtr aParamFeat = theDocument->feature(theParam);
195 if (aParamFeat == theSearcher || theDocument->isLater(aParamFeat, theSearcher))
198 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
199 outValue = aValueAttribute->value();
203 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue,
204 ResultParameterPtr& theParam, const DocumentPtr& theDocument)
206 SessionPtr aSession = ModelAPI_Session::get();
207 std::list<DocumentPtr> aDocList;
208 DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
209 if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
211 DocumentPtr aRootDocument = aSession->moduleDocument();
212 if (aDocument != aRootDocument) {
213 // any parameters in PartSet is okindependently on the Part position (issu #1504)
214 if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
220 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
222 // to optimize and avoid of crash on partset document close
223 // (don't touch the sub-document structure)
224 if (theMain != theSub) {
225 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
226 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
227 theMain->object(ModelAPI_ResultPart::group(), a));
228 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
236 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
238 // to optimize and avoid of crash on partset document close
239 // (don't touch the sub-document structure)
240 if (theMain != theSub) {
241 // iteration from top to bottom to avoid finding the movement documents before the original
242 int aSize = theMain->size(ModelAPI_Feature::group());
243 for (int a = 0; a < aSize; a++) {
244 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
245 theMain->object(ModelAPI_Feature::group(), a));
246 if (aPartFeat.get()) {
247 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
248 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
249 for(; aRes != aResList.end(); aRes++) {
250 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
252 if (aPart->isActivated() && aPart->partDoc() == theSub)
254 } else break; // if the first is not Part, others are also not
262 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
264 if (theFeature.get() && theFeature->data() && theFeature->data()->isValid()) {
265 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
266 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
267 for(; aRefIter != aRefs.end(); aRefIter++) {
268 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
269 ((*aRefIter)->owner());
270 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
274 return CompositeFeaturePtr(); // not found
277 ResultBodyPtr bodyOwner(const ResultPtr& theSub, const bool theRoot)
280 ObjectPtr aParent = theSub->document()->parent(theSub);
282 if (theRoot) { // try to find parent of parent
283 ResultPtr aResultParent = std::dynamic_pointer_cast<ModelAPI_Result>(aParent);
284 ResultBodyPtr aGrandParent = bodyOwner(aResultParent, true);
285 if (aGrandParent.get())
286 aParent = aGrandParent;
288 return std::dynamic_pointer_cast<ModelAPI_ResultBody>(aParent);
291 return ResultBodyPtr(); // not found
294 int bodyIndex(const ResultPtr& theSub)
297 ResultBodyPtr aParent = bodyOwner(theSub);
299 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
300 if (aBody.get() && aParent->isSub(aBody, anIndex))
303 return anIndex; // not found
306 bool hasSubResults(const ResultPtr& theResult)
308 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
309 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
312 void allSubs(const ResultBodyPtr& theResult, std::list<ResultPtr>& theResults,
313 const bool theLowerOnly) {
314 // iterate sub-bodies of compsolid
315 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
317 int aNumSub = aComp->numberOfSubs();
318 for (int a = 0; a < aNumSub; a++) {
319 ResultBodyPtr aSub = aComp->subResult(a);
320 if (!theLowerOnly || aSub->numberOfSubs() == 0)
321 theResults.push_back(aSub);
322 allSubs(aSub, theResults);
327 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
329 if (!theFeature.get()) // safety: for empty feature no results
331 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
332 std::list<ResultPtr>::const_iterator aRIter = aResults.begin();
333 for (; aRIter != aResults.cend(); aRIter++) {
334 theResults.push_back(*aRIter);
335 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRIter);
336 allSubs(aResult, theResults);
340 //******************************************************************
341 bool allDocumentsActivated(std::string& theNotActivatedNames)
343 theNotActivatedNames = "";
344 bool anAllPartActivated = true;
346 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
347 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
348 for (int i = 0; i < aSize; i++) {
349 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
350 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
351 if (!aPart->isActivated()) {
352 anAllPartActivated = false;
353 if (!theNotActivatedNames.empty())
354 theNotActivatedNames += ", ";
355 theNotActivatedNames += aObject->data()->name().c_str();
358 return anAllPartActivated;
361 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
362 const bool /*theFlushRedisplay*/,
363 const bool theUseComposite,
364 const bool theUseRecursion)
366 #ifdef DEBUG_REMOVE_FEATURES
367 printListInfo(theFeatures, "selection: ");
370 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
371 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
372 #ifdef DEBUG_REMOVE_FEATURES
373 printMapInfo(aReferences, "allDependencies: ");
376 std::set<FeaturePtr> aFeaturesRefsTo;
377 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
378 #ifdef DEBUG_REMOVE_FEATURES
379 printListInfo(aFeaturesRefsTo, "references: ");
382 std::set<FeaturePtr> aFeatures = theFeatures;
383 if (!aFeaturesRefsTo.empty())
384 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
385 #ifdef DEBUG_REMOVE_FEATURES
386 printListInfo(aFeatures, "removeFeatures: ");
389 return ModelAPI_Tools::removeFeatures(aFeatures, false);
392 //***********************************************************************
393 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
394 const bool theFlushRedisplay)
397 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
398 aLast = theFeatures.end();
399 for (; anIt != aLast; anIt++) {
400 FeaturePtr aFeature = *anIt;
401 if (aFeature.get()) {
402 DocumentPtr aDoc = aFeature->document();
403 // flush REDISPLAY signal after remove feature
404 aDoc->removeFeature(aFeature);
408 if (isDone && theFlushRedisplay) {
409 // the redisplay signal should be flushed in order to erase
410 // the feature presentation in the viewer
411 // if should be done after removeFeature() of document
412 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
417 //***********************************************************************
418 // Fills the references list by all references of the feature from the references map.
419 // This is a recusive method to find references by next found feature in the map of references.
420 // \param theFeature a feature to find references
421 // \param theReferencesMap a map of references
422 // \param theReferences an out container of references
423 void addRefsToFeature(const FeaturePtr& theFeature,
424 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
426 std::set<FeaturePtr>& theReferences)
428 if (theRecLevel > RECURSE_TOP_LEVEL)
432 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
433 return; // this feature is not in the selection list, so exists without references to it
434 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
436 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
437 aLast = aMainReferences.end();
438 for (; anIt != aLast; anIt++) {
439 FeaturePtr aRefFeature = *anIt;
440 if (theReferences.find(aRefFeature) == theReferences.end()) {
441 addRefsToFeature(aRefFeature, theReferencesMap, theRecLevel, theReferences);
442 theReferences.insert(aRefFeature);
447 // For each feature from the feature list it searches references to the feature and append them
448 // to the references map. This is a recusive method.
449 // \param theFeature a feature to find references
450 // \param theReferencesMap a map of references
451 // \param theReferences an out container of references
452 void findReferences(const std::set<FeaturePtr>& theFeatures,
453 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
454 const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
456 if (theRecLevel > RECURSE_TOP_LEVEL)
459 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
460 aLast = theFeatures.end();
461 for (; anIt != aLast; anIt++) {
462 FeaturePtr aFeature = *anIt;
463 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
464 DocumentPtr aSelFeatureDoc = aFeature->document();
465 std::set<FeaturePtr> aSelRefFeatures;
466 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
467 if (theUseComposite) { // do not filter selection
468 theReferences[aFeature] = aSelRefFeatures;
470 else { // filter references to skip composition features of the current feature
471 std::set<FeaturePtr> aFilteredFeatures;
472 std::set<FeaturePtr>::const_iterator aRefIt = aSelRefFeatures.begin(),
473 aRefLast = aSelRefFeatures.end();
474 for (; aRefIt != aRefLast; aRefIt++) {
475 FeaturePtr aCFeature = *aRefIt;
476 CompositeFeaturePtr aComposite =
477 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
478 if (aComposite.get() && aComposite->isSub(aFeature))
479 continue; /// composite of the current feature should be skipped
480 aFilteredFeatures.insert(aCFeature);
482 theReferences[aFeature] = aFilteredFeatures;
484 if (theUseRecursion) {
485 #ifdef DEBUG_CYCLING_1550
486 findReferences(aSelRefFeatures, theReferences, theUseComposite,
487 theUseRecursion, theRecLevel);
489 findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
497 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
498 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
499 const bool theUseComposite,
500 const bool theUseRecursion)
502 // For dependencies, find main_list:
503 // sk_1(ext_1, vertex_1)
504 // ext_1(bool_1, sk_3)
509 // Information: bool_1 is not selected, ext_2(bool_2) exists
510 // find all referenced features
511 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
513 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
515 #ifdef DEBUG_REMOVE_FEATURES
516 printMapInfo(aMainList, "firstDependencies");
518 // find all dependencies for each object:
519 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
520 // ext_1(bool_1, sk_3)
522 // sk_2(ext_2) + (bool_1)
525 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
526 aMainLast = aMainList.end();
527 for (; aMainIt != aMainLast; aMainIt++) {
528 FeaturePtr aMainListFeature = aMainIt->first;
530 //std::string aName = aMainListFeature->name();
531 std::set<FeaturePtr> aMainRefList = aMainIt->second;
533 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
535 int n = sprintf(aBuf, "%d", aMainRefList.size());
536 std::string aSize(aBuf);
537 std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
538 << ", references size = " << aSize << std::endl;
540 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
541 aLast = aMainRefList.end();
542 std::set<FeaturePtr> aResultRefList;
543 aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
544 for (; anIt != aLast; anIt++) {
545 FeaturePtr aFeature = *anIt;
547 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
548 std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
551 addRefsToFeature(aFeature, aMainList,
552 aRecLevel, aResultRefList/*aMainRefList*/);
554 theReferences[aMainListFeature] = aResultRefList;
556 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
557 std::cout << std::endl;
560 #ifdef DEBUG_REMOVE_FEATURES
561 printMapInfo(theReferences, "allDependencies");
565 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
566 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
567 std::set<FeaturePtr>& theFeaturesRefsTo)
569 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
570 aLast = theFeatures.end();
571 for (; anIt != aLast; anIt++) {
572 FeaturePtr aFeature = *anIt;
573 if (theReferences.find(aFeature) == theReferences.end())
575 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
576 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
577 for (; aRefIt != aRefLast; aRefIt++) {
578 FeaturePtr aRefFeature = *aRefIt;
579 CompositeFeaturePtr aComposite =
580 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
581 if (aComposite.get() && aComposite->isSub(aFeature))
582 continue; /// composite of the current feature should not be removed
584 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
585 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
586 theFeaturesRefsTo.insert(aRefFeature);
591 void getConcealedResults(const FeaturePtr& theFeature,
592 std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
594 SessionPtr aSession = ModelAPI_Session::get();
596 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
597 theFeature->data()->referencesToObjects(aRefs);
598 std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
599 anIt = aRefs.begin(), aLast = aRefs.end();
600 std::set<ResultPtr> alreadyThere; // to avoid duplications
601 for (; anIt != aLast; anIt++) {
602 if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
603 continue; // use only concealed attributes
604 std::list<ObjectPtr> anObjects = (*anIt).second;
605 std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
606 for (; anOIt != anOLast; anOIt++) {
607 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
608 if (aResult && aResult->isConcealed()) {
609 if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
610 alreadyThere.insert(aResult);
612 theResults.push_back(aResult);
618 std::pair<std::string, bool> getDefaultName(const std::shared_ptr<ModelAPI_Result>& theResult,
619 const bool theInherited)
621 typedef std::list< std::pair < std::string, std::list<ObjectPtr> > > ListOfReferences;
623 SessionPtr aSession = ModelAPI_Session::get();
625 ResultBodyPtr anOwnerRes = bodyOwner(theResult);
627 // names of sub-solids in CompSolid should be default (for example,
628 // result of boolean operation 'Boolean_1_1' is a CompSolid which is renamed to 'MyBOOL',
629 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1_1', 'Boolean_1_1_2' etc.)
630 std::ostringstream aDefaultName;
631 aDefaultName << getDefaultName(anOwnerRes).first;
632 aDefaultName << "_" << (bodyIndex(theResult) + 1);
633 return std::pair<std::string, bool>(aDefaultName.str(), false);
636 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult->data()->owner());
637 DataPtr aData = anOwner->data();
639 ListOfReferences aReferences;
640 // find first result with user-defined name
641 ListOfReferences::const_iterator aFoundRef = aReferences.end();
643 aData->referencesToObjects(aReferences);
645 for (ListOfReferences::const_iterator aRefIt = aReferences.begin();
646 aRefIt != aReferences.end(); ++aRefIt) {
647 bool isConcealed = aSession->validators()->isConcealed(anOwner->getKind(), aRefIt->first);
648 bool isMainArg = isConcealed &&
649 aSession->validators()->isMainArgument(anOwner->getKind(), aRefIt->first);
651 // check the referred object is a Body
652 // (for example, ExtrusionCut has a sketch as a first attribute which is concealing)
653 bool isBody = aRefIt->second.size() > 1 || (aRefIt->second.size() == 1 &&
654 aRefIt->second.front().get() &&
655 aRefIt->second.front()->groupName() == ModelAPI_ResultBody::group());
656 if (isBody && (isMainArg || aFoundRef == aReferences.end() ||
657 aData->isPrecedingAttribute(aRefIt->first, aFoundRef->first)))
665 // get the result number in the feature
666 int anIndexInOwner = 0;
667 const std::list<ResultPtr>& anOwnerResults = anOwner->results();
668 std::list<ResultPtr>::const_iterator aResIt = anOwnerResults.cbegin();
669 for(; aResIt != anOwnerResults.cend(); aResIt++) {
670 if(*aResIt == theResult)
675 // find an object which is concealed by theResult
676 if (aFoundRef != aReferences.end() && !aFoundRef->second.empty()) {
677 // store number of references for each object
678 std::map<ResultPtr, int> aNbRefToObject;
679 // search the object by result index
680 std::list<ObjectPtr>::const_iterator anObjIt = aFoundRef->second.begin();
681 int aResultIndex = anIndexInOwner;
682 while (--aResultIndex >= 0) {
683 ResultPtr aCurRes = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIt);
684 ResultBodyPtr aParentBody = ModelAPI_Tools::bodyOwner(aCurRes);
686 aCurRes = aParentBody;
687 if (aNbRefToObject.find(aCurRes) == aNbRefToObject.end())
688 aNbRefToObject[aCurRes] = 1;
690 aNbRefToObject[aCurRes] += 1;
693 if (anObjIt == aFoundRef->second.end()) {
694 anObjIt = aFoundRef->second.begin();
698 // check the result is a Body
699 if ((*anObjIt)->groupName() == ModelAPI_ResultBody::group()) {
700 // check the result is part of CompSolid
701 ResultPtr anObjRes = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIt);
702 ResultBodyPtr aParentBody = ModelAPI_Tools::bodyOwner(anObjRes);
704 anObjRes = aParentBody;
706 // return name of reference result only if it has been renamed by the user,
707 // in other case compose a default name
708 if (anObjRes->data()->hasUserDefinedName()) {
709 std::stringstream aName;
710 aName << anObjRes->data()->name();
711 std::map<ResultPtr, int>::iterator aFound = aNbRefToObject.find(anObjRes);
712 if (aFound != aNbRefToObject.end()) {
713 // to generate unique name, add suffix if there are several results
714 // referring to the same shape
715 aName << "_" << aFound->second + 1;
717 return std::pair<std::string, bool>(aName.str(), true);
722 // compose default name by the name of the feature and the index of result
723 std::stringstream aDefaultName;
724 aDefaultName << anOwner->name();
725 // if there are several results (issue #899: any number of result),
726 // add unique prefix starting from second
727 if (anIndexInOwner > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
728 aDefaultName << "_" << anIndexInOwner + 1;
729 return std::pair<std::string, bool>(aDefaultName.str(), false);
732 std::set<FeaturePtr> getParents(const FeaturePtr& theFeature)
734 std::set<FeaturePtr> aParents;
735 for (FeaturePtr aCurFeat = theFeature; aCurFeat; ) {
736 CompositeFeaturePtr aFoundComposite;
737 const std::set<AttributePtr>& aRefs = aCurFeat->data()->refsToMe();
738 for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
739 anIt != aRefs.end(); ++anIt) {
740 FeaturePtr aF = ModelAPI_Feature::feature((*anIt)->owner());
741 aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aF);
742 if (aFoundComposite && aFoundComposite->isSub(aCurFeat))
745 aFoundComposite = CompositeFeaturePtr();
748 if (aFoundComposite) {
749 aParents.insert(aFoundComposite);
750 aCurFeat = aFoundComposite;
753 // add the part containing high-level feature
754 SessionPtr aSession = ModelAPI_Session::get();
755 DocumentPtr aPartSetDoc = aSession->moduleDocument();
756 std::list<FeaturePtr> aPartSetFeatures = aPartSetDoc->allFeatures();
757 for (std::list<FeaturePtr>::const_iterator anIt = aPartSetFeatures.begin();
758 anIt != aPartSetFeatures.end(); ++anIt) {
759 aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
760 if (aFoundComposite && aFoundComposite->isSub(aCurFeat)) {
761 aParents.insert(aFoundComposite);
766 aCurFeat = FeaturePtr();
772 void fillShapeHierarchy(const GeomShapePtr& theShape,
773 const ResultPtr& theContext,
774 GeomAPI_ShapeHierarchy& theHierarchy)
776 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(theContext);
777 if (aResCompSolidPtr.get()) {
778 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
779 if (aContextShape->shapeType() <= GeomAPI_Shape::COMPSOLID) {
780 theHierarchy.addParent(theShape, aContextShape);
781 fillShapeHierarchy(aContextShape, aResCompSolidPtr, theHierarchy);
787 void removeResults(const std::list<ResultPtr>& theResults)
789 // collect all documents where the results must be removed
790 std::map<DocumentPtr, std::list<ResultPtr> > aDocs;
792 std::list<ResultPtr>::const_iterator aResIter = theResults.cbegin();
793 for(; aResIter != theResults.cend(); aResIter++) {
794 DocumentPtr aDoc = (*aResIter)->document();
795 if (!aDocs.count(aDoc))
796 aDocs[aDoc] = std::list<ResultPtr>();
797 aDocs[aDoc].push_back(*aResIter);
799 // create a "remove" feature in each doc
800 std::map<DocumentPtr, std::list<ResultPtr> >::iterator aDoc = aDocs.begin();
801 for(; aDoc != aDocs.end(); aDoc++) {
802 FeaturePtr aRemove = aDoc->first->addFeature("RemoveResults");
804 for(aResIter = aDoc->second.cbegin(); aResIter != aDoc->second.cend(); aResIter++)
805 aRemove->selectionList("results")->append(*aResIter, GeomShapePtr());
813 //**************************************************************
814 void setDeflection(ResultPtr theResult, const double theDeflection)
816 if (!theResult.get())
819 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
820 if (aDeflectionAttr.get() != NULL) {
821 aDeflectionAttr->setValue(theDeflection);
825 double getDeflection(const std::shared_ptr<ModelAPI_Result>& theResult)
827 double aDeflection = -1;
828 // get deflection from the attribute of the result
829 if (theResult.get() != NULL &&
830 theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
831 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
832 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
833 double aValue = aDoubleAttr->value();
834 if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
835 aDeflection = aDoubleAttr->value();
841 //******************************************************
842 void setColor(ResultPtr theResult, const std::vector<int>& theColor)
844 if (!theResult.get())
847 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
848 if (aColorAttr.get() != NULL) {
849 if (!aColorAttr->size()) {
850 aColorAttr->setSize(3);
852 aColorAttr->setValue(0, theColor[0]);
853 aColorAttr->setValue(1, theColor[1]);
854 aColorAttr->setValue(2, theColor[2]);
858 void getColor(const std::shared_ptr<ModelAPI_Result>& theResult, std::vector<int>& theColor)
861 // get color from the attribute of the result
862 if (theResult.get() != NULL &&
863 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
864 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
865 if (aColorAttr.get() && aColorAttr->size()) {
866 theColor.push_back(aColorAttr->value(0));
867 theColor.push_back(aColorAttr->value(1));
868 theColor.push_back(aColorAttr->value(2));
873 //******************************************************
874 void getIsoLines(const std::shared_ptr<ModelAPI_Result>& theResult,
875 bool& isVisible, std::vector<int>& theNbLines)
879 if (!theResult.get())
881 if (theResult->groupName() == ModelAPI_ResultConstruction::group()) {
882 theNbLines.push_back(0);
883 theNbLines.push_back(0);
886 // get color from the attribute of the result
887 AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
890 theNbLines.push_back(aAttr->value(0));
891 theNbLines.push_back(aAttr->value(1));
894 AttributeBooleanPtr aBoolAttr =
895 theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
896 if (aBoolAttr.get()) {
897 isVisible = aBoolAttr->value();
902 //******************************************************
903 void setIsoLines(ResultPtr theResult, const std::vector<int>& theIso)
905 if (!theResult.get())
908 AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
909 if (aAttr.get() != NULL) {
910 if (!aAttr->size()) {
913 aAttr->setValue(0, theIso[0]);
914 aAttr->setValue(1, theIso[1]);
918 //******************************************************
919 void showIsoLines(std::shared_ptr<ModelAPI_Result> theResult, bool theShow)
921 if (!theResult.get())
924 AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
925 if (aAttr.get() != NULL) {
926 aAttr->setValue(theShow);
930 //******************************************************
931 bool isShownIsoLines(std::shared_ptr<ModelAPI_Result> theResult)
933 if (!theResult.get())
936 AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
937 if (aAttr.get() != NULL) {
938 return aAttr->value();
943 //**************************************************************
944 void setTransparency(ResultPtr theResult, double theTransparency)
946 if (!theResult.get())
949 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
950 if (anAttribute.get() != NULL) {
951 anAttribute->setValue(theTransparency);
955 double getTransparency(const std::shared_ptr<ModelAPI_Result>& theResult)
957 double aTransparency = -1;
958 // get transparency from the attribute of the result
959 if (theResult.get() != NULL &&
960 theResult->data()->attribute(ModelAPI_Result::TRANSPARENCY_ID()).get() != NULL) {
961 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
962 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
963 aTransparency = aDoubleAttr->value();
966 return aTransparency;
969 void copyVisualizationAttrs(
970 std::shared_ptr<ModelAPI_Result> theSource, std::shared_ptr<ModelAPI_Result> theDest)
973 AttributeIntArrayPtr aSourceColor = theSource->data()->intArray(ModelAPI_Result::COLOR_ID());
974 if (aSourceColor.get() && aSourceColor->isInitialized() && aSourceColor->size()) {
975 AttributeIntArrayPtr aDestColor = theDest->data()->intArray(ModelAPI_Result::COLOR_ID());
976 if (aDestColor.get()) {
977 aDestColor->setSize(aSourceColor->size());
978 for(int a = 0; a < aSourceColor->size(); a++)
979 aDestColor->setValue(a, aSourceColor->value(a));
983 AttributeIntArrayPtr aSource = theSource->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
984 if (aSource.get() && aSource->isInitialized() && aSource->size()) {
985 AttributeIntArrayPtr aDest = theDest->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
987 aDest->setSize(aSource->size());
988 for(int a = 0; a < aSource->size(); a++)
989 aDest->setValue(a, aSource->value(a));
993 AttributeDoublePtr aSourceDefl = theSource->data()->real(ModelAPI_Result::DEFLECTION_ID());
994 if (aSourceDefl.get() && aSourceDefl->isInitialized()) {
995 AttributeDoublePtr aDestDefl = theDest->data()->real(ModelAPI_Result::DEFLECTION_ID());
996 if (aDestDefl.get()) {
997 aDestDefl->setValue(aSourceDefl->value());
1001 AttributeDoublePtr aSourceTransp = theSource->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1002 if (aSourceTransp.get() && aSourceTransp->isInitialized()) {
1003 AttributeDoublePtr aDestTransp = theDest->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1004 if (aDestTransp.get()) {
1005 aDestTransp->setValue(aSourceTransp->value());
1010 std::list<FeaturePtr> referencedFeatures(
1011 std::shared_ptr<ModelAPI_Result> theTarget, const std::string& theFeatureKind,
1012 const bool theSortResults)
1014 std::set<FeaturePtr> aResSet; // collect in the set initially to avoid duplicates
1015 std::list<ResultPtr> allSubRes;
1016 allSubRes.push_back(theTarget);
1017 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theTarget);
1019 allSubs(aBody, allSubRes);
1020 std::list<ResultPtr>::iterator aSub = allSubRes.begin();
1021 for(; aSub != allSubRes.end(); aSub++) {
1022 const std::set<AttributePtr>& aRefs = (*aSub)->data()->refsToMe();
1023 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1024 for(; aRef != aRefs.cend(); aRef++) {
1025 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1026 if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1027 aResSet.insert(aFeat);
1030 // add also feature of the target that may be referenced as a whole
1031 FeaturePtr aTargetFeature = theTarget->document()->feature(theTarget);
1032 const std::set<AttributePtr>& aRefs = aTargetFeature->data()->refsToMe();
1033 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1034 for(; aRef != aRefs.cend(); aRef++) {
1035 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1036 if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1037 aResSet.insert(aFeat);
1039 // check also Group-operations that may refer to groups - add them for theFeatureKind "Group"
1040 if (theFeatureKind == "Group") {
1041 std::set<FeaturePtr> aGroupOperations;
1042 for(bool aNeedIterate = true; aNeedIterate; ) {
1043 std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1044 for(; aResIter != aResSet.end(); aResIter++) {
1045 std::list<ResultPtr>::const_iterator aGroupRes = (*aResIter)->results().cbegin();
1046 for(; aGroupRes != (*aResIter)->results().cend(); aGroupRes++) {
1047 const std::set<AttributePtr>& aGroupRefs = (*aGroupRes)->data()->refsToMe();
1048 std::set<AttributePtr>::const_iterator aRefIt = aGroupRefs.cbegin();
1049 for(; aRefIt != aGroupRefs.cend(); aRefIt++) {
1050 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
1051 if (aFeat.get() && !aGroupOperations.count(aFeat) && !aFeat->results().empty() &&
1052 aFeat->firstResult()->groupName() == ModelAPI_ResultGroup::group()) {
1053 // iterate results of this group operation because it may be without theTarget shape
1054 GeomShapePtr aTargetShape = theTarget->shape();
1055 bool anIsIn = false;
1056 std::list<ResultPtr>::const_iterator anOpRes = aFeat->results().cbegin();
1057 for(; anOpRes != aFeat->results().cend() && !anIsIn; anOpRes++) {
1058 GeomShapePtr anOpShape = (*anOpRes)->shape();
1059 if (!anOpShape.get() || anOpShape->isNull())
1061 for(GeomAPI_ShapeIterator aSubIt(anOpShape); aSubIt.more(); aSubIt.next()) {
1062 if (aTargetShape->isSubShape(aSubIt.current(), false)) {
1069 aGroupOperations.insert(aFeat);
1074 // insert all new group operations into result and if they are, check for next dependencies
1075 aNeedIterate = false;
1076 std::set<FeaturePtr>::iterator aGroupOpIter = aGroupOperations.begin();
1077 for(; aGroupOpIter != aGroupOperations.end(); aGroupOpIter++) {
1078 if (aResSet.find(*aGroupOpIter) == aResSet.end()) {
1079 aResSet.insert(*aGroupOpIter);
1080 aNeedIterate = true;
1086 std::list<FeaturePtr> aResList;
1087 std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1088 for(; aResIter != aResSet.end(); aResIter++) {
1089 if (theSortResults) { // sort results by creation-order
1090 std::list<FeaturePtr>::iterator aListIter = aResList.begin();
1091 for(; aListIter != aResList.end(); aListIter++) {
1092 if ((*aResIter)->document()->isLater(*aListIter, *aResIter))
1095 if (aListIter == aResList.end()) // goes to the end
1096 aResList.push_back(*aResIter);
1098 aResList.insert(aListIter, *aResIter);
1099 } else //just push to the end unsorted
1100 aResList.push_back(*aResIter);
1107 } // namespace ModelAPI_Tools