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 <Locale_Convert.h>
43 #include <ModelAPI_Events.h>
45 #include <GeomAPI_ShapeHierarchy.h>
46 #include <GeomAPI_ShapeIterator.h>
48 #define RECURSE_TOP_LEVEL 50
50 //#define DEBUG_REMOVE_FEATURES
51 //#define DEBUG_REMOVE_FEATURES_RECURSE
52 //#define DEBUG_CYCLING_1550
54 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
56 std::string getFeatureInfo(FeaturePtr theFeature)
58 if (!theFeature.get())
60 //std::ostringstream aPtrStr;
61 //aPtrStr << "[" << theFeature.get() << "] ";
62 std::string aFeatureInfo = /*aPtrStr.str() + */theFeature->name();
63 CompositeFeaturePtr aComposite = ModelAPI_Tools::compositeOwner(theFeature);
64 if (aComposite.get()) {
65 aFeatureInfo = aFeatureInfo + "[in " + aComposite->name() + "]";
71 #ifdef DEBUG_REMOVE_FEATURES
72 void printMapInfo(const std::map<FeaturePtr, std::set<FeaturePtr> >& theMainList,
73 const std::string& thePrefix)
75 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = theMainList.begin(),
76 aMainLast = theMainList.end();
78 for (; aMainIt != aMainLast; aMainIt++) {
79 FeaturePtr aMainListFeature = aMainIt->first;
80 std::set<FeaturePtr> aMainRefList = aMainIt->second;
81 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(), aLast = aMainRefList.end();
82 std::string aRefsInfo;
83 for (; anIt != aLast; anIt++) {
84 aRefsInfo += (*anIt)->name().c_str();
88 if (!aRefsInfo.empty()) {
89 anInfo = anInfo + aMainListFeature->name().c_str() + ": " + aRefsInfo + "\n";
92 std::cout << thePrefix.c_str() << " [feature: references to]: \n" << anInfo.c_str() << std::endl;
95 void printListInfo(const std::set<FeaturePtr>& theMainList,
96 const std::string& thePrefix)
98 std::set<FeaturePtr>::const_iterator aMainIt = theMainList.begin(),
99 aMainLast = theMainList.end();
101 for (; aMainIt != aMainLast; aMainIt++) {
102 FeaturePtr aRefFeature = *aMainIt;
103 anInfo += aRefFeature->name().c_str();
104 if (aMainIt != aMainLast)
107 std::cout << thePrefix.c_str() << ": " << anInfo.c_str() << std::endl;
111 namespace ModelAPI_Tools {
113 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
115 return theResult->shape();
119 const char* toString(ModelAPI_ExecState theExecState)
121 switch (theExecState) {
122 case ModelAPI_StateDone: return "Done";
123 case ModelAPI_StateMustBeUpdated: return "Must be updated";
124 case ModelAPI_StateExecFailed: return "Execution failed";
125 case ModelAPI_StateInvalidArgument: return "Invalid argument";
126 case ModelAPI_StateNothing: return "Empty state";
127 default: return "Unknown ExecState.";
131 std::string getFeatureError(const FeaturePtr& theFeature)
134 if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
137 // to be removed later, this error should be got from the feature
138 if (theFeature->data()->execState() == ModelAPI_StateDone ||
139 theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
142 // set error indication
143 anError = theFeature->error();
144 if (anError.empty()) {
145 bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
146 || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
148 anError = toString(theFeature->data()->execState());
149 // If the feature is Composite and error is StateInvalidArgument,
150 // error text should include error of first invalid sub-feature. Otherwise
151 // it is not clear what is the reason of the invalid argument.
152 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
153 CompositeFeaturePtr aComposite =
154 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
156 for (int i = 0, aSize = aComposite->numberOfSubs(); i < aSize; i++) {
157 FeaturePtr aSubFeature = aComposite->subFeature(i);
158 std::string aSubFeatureError = getFeatureError(aSubFeature);
159 if (!aSubFeatureError.empty()) {
160 anError = anError + " in " + aSubFeature->getKind() + ".\n" + aSubFeatureError;
173 ObjectPtr objectByName(const DocumentPtr& theDocument, const std::string& theGroup,
174 const std::string& theName)
176 std::wstring aName = Locale::Convert::toWString(theName);
177 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
178 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
179 if (anObject->data()->name() == aName)
186 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
187 const std::string& theName, double& outValue, ResultParameterPtr& theParam)
189 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
190 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
193 // avoid usage of parameters created later than the initial parameter
195 if (theSearcher.get()) {
196 FeaturePtr aParamFeat = theDocument->feature(theParam);
197 if (aParamFeat == theSearcher || theDocument->isLater(aParamFeat, theSearcher))
200 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
201 outValue = aValueAttribute->value();
205 bool findVariable(FeaturePtr theSearcher, const std::string& theName, double& outValue,
206 ResultParameterPtr& theParam, const DocumentPtr& theDocument)
208 SessionPtr aSession = ModelAPI_Session::get();
209 std::list<DocumentPtr> aDocList;
210 DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
211 if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
213 DocumentPtr aRootDocument = aSession->moduleDocument();
214 if (aDocument != aRootDocument) {
215 // any parameters in PartSet is okindependently on the Part position (issu #1504)
216 if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
222 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
224 // to optimize and avoid of crash on partset document close
225 // (don't touch the sub-document structure)
226 if (theMain != theSub) {
227 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
228 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
229 theMain->object(ModelAPI_ResultPart::group(), a));
230 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
238 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
240 // to optimize and avoid of crash on partset document close
241 // (don't touch the sub-document structure)
242 if (theMain != theSub) {
243 // iteration from top to bottom to avoid finding the movement documents before the original
244 int aSize = theMain->size(ModelAPI_Feature::group());
245 for (int a = 0; a < aSize; a++) {
246 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
247 theMain->object(ModelAPI_Feature::group(), a));
248 if (aPartFeat.get()) {
249 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
250 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
251 for(; aRes != aResList.end(); aRes++) {
252 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
254 if (aPart->isActivated() && aPart->partDoc() == theSub)
256 } else break; // if the first is not Part, others are also not
264 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
266 if (theFeature.get() && theFeature->data() && theFeature->data()->isValid()) {
267 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
268 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
269 for(; aRefIter != aRefs.end(); aRefIter++) {
270 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
271 ((*aRefIter)->owner());
272 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
276 return CompositeFeaturePtr(); // not found
279 ResultBodyPtr bodyOwner(const ResultPtr& theSub, const bool theRoot)
282 ObjectPtr aParent = theSub->document()->parent(theSub);
284 if (theRoot) { // try to find parent of parent
285 ResultPtr aResultParent = std::dynamic_pointer_cast<ModelAPI_Result>(aParent);
286 ResultBodyPtr aGrandParent = bodyOwner(aResultParent, true);
287 if (aGrandParent.get())
288 aParent = aGrandParent;
290 return std::dynamic_pointer_cast<ModelAPI_ResultBody>(aParent);
293 return ResultBodyPtr(); // not found
296 int bodyIndex(const ResultPtr& theSub)
299 ResultBodyPtr aParent = bodyOwner(theSub);
301 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
302 if (aBody.get() && aParent->isSub(aBody, anIndex))
305 return anIndex; // not found
308 bool hasSubResults(const ResultPtr& theResult)
310 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
311 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
314 void allSubs(const ResultBodyPtr& theResult, std::list<ResultPtr>& theResults,
315 const bool theLowerOnly) {
316 // iterate sub-bodies of compsolid
317 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
319 int aNumSub = aComp->numberOfSubs();
320 for (int a = 0; a < aNumSub; a++) {
321 ResultBodyPtr aSub = aComp->subResult(a);
322 if (!theLowerOnly || aSub->numberOfSubs() == 0)
323 theResults.push_back(aSub);
324 allSubs(aSub, theResults);
329 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
331 if (!theFeature.get()) // safety: for empty feature no results
333 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
334 std::list<ResultPtr>::const_iterator aRIter = aResults.begin();
335 for (; aRIter != aResults.cend(); aRIter++) {
336 theResults.push_back(*aRIter);
337 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRIter);
338 allSubs(aResult, theResults);
342 //******************************************************************
343 bool allDocumentsActivated(std::wstring& theNotActivatedNames)
345 theNotActivatedNames = L"";
346 bool anAllPartActivated = true;
348 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
349 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
350 for (int i = 0; i < aSize; i++) {
351 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
352 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
353 if (!aPart->isActivated()) {
354 anAllPartActivated = false;
355 if (!theNotActivatedNames.empty())
356 theNotActivatedNames += L", ";
357 theNotActivatedNames += aObject->data()->name().c_str();
360 return anAllPartActivated;
363 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
364 const bool /*theFlushRedisplay*/,
365 const bool theUseComposite,
366 const bool theUseRecursion)
368 #ifdef DEBUG_REMOVE_FEATURES
369 printListInfo(theFeatures, "selection: ");
372 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
373 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
374 #ifdef DEBUG_REMOVE_FEATURES
375 printMapInfo(aReferences, "allDependencies: ");
378 std::set<FeaturePtr> aFeaturesRefsTo;
379 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
380 #ifdef DEBUG_REMOVE_FEATURES
381 printListInfo(aFeaturesRefsTo, "references: ");
384 std::set<FeaturePtr> aFeatures = theFeatures;
385 if (!aFeaturesRefsTo.empty())
386 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
387 #ifdef DEBUG_REMOVE_FEATURES
388 printListInfo(aFeatures, "removeFeatures: ");
391 return ModelAPI_Tools::removeFeatures(aFeatures, false);
394 //***********************************************************************
395 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
396 const bool theFlushRedisplay)
399 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
400 aLast = theFeatures.end();
401 for (; anIt != aLast; anIt++) {
402 FeaturePtr aFeature = *anIt;
403 if (aFeature.get()) {
404 DocumentPtr aDoc = aFeature->document();
405 // flush REDISPLAY signal after remove feature
406 aDoc->removeFeature(aFeature);
410 if (isDone && theFlushRedisplay) {
411 // the redisplay signal should be flushed in order to erase
412 // the feature presentation in the viewer
413 // if should be done after removeFeature() of document
414 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
419 //***********************************************************************
420 // Fills the references list by all references of the feature from the references map.
421 // This is a recusive method to find references by next found feature in the map of references.
422 // \param theFeature a feature to find references
423 // \param theReferencesMap a map of references
424 // \param theReferences an out container of references
425 void addRefsToFeature(const FeaturePtr& theFeature,
426 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
428 std::set<FeaturePtr>& theReferences)
430 if (theRecLevel > RECURSE_TOP_LEVEL)
434 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
435 return; // this feature is not in the selection list, so exists without references to it
436 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
438 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
439 aLast = aMainReferences.end();
440 for (; anIt != aLast; anIt++) {
441 FeaturePtr aRefFeature = *anIt;
442 if (theReferences.find(aRefFeature) == theReferences.end()) {
443 addRefsToFeature(aRefFeature, theReferencesMap, theRecLevel, theReferences);
444 theReferences.insert(aRefFeature);
449 // For each feature from the feature list it searches references to the feature and append them
450 // to the references map. This is a recusive method.
451 // \param theFeature a feature to find references
452 // \param theReferencesMap a map of references
453 // \param theReferences an out container of references
454 void findReferences(const std::set<FeaturePtr>& theFeatures,
455 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
456 const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
458 if (theRecLevel > RECURSE_TOP_LEVEL)
461 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
462 aLast = theFeatures.end();
463 for (; anIt != aLast; anIt++) {
464 FeaturePtr aFeature = *anIt;
465 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
466 DocumentPtr aSelFeatureDoc = aFeature->document();
467 std::set<FeaturePtr> aSelRefFeatures;
468 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
469 if (theUseComposite) { // do not filter selection
470 theReferences[aFeature] = aSelRefFeatures;
472 else { // filter references to skip composition features of the current feature
473 std::set<FeaturePtr> aFilteredFeatures;
474 std::set<FeaturePtr>::const_iterator aRefIt = aSelRefFeatures.begin(),
475 aRefLast = aSelRefFeatures.end();
476 for (; aRefIt != aRefLast; aRefIt++) {
477 FeaturePtr aCFeature = *aRefIt;
478 CompositeFeaturePtr aComposite =
479 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
480 if (aComposite.get() && aComposite->isSub(aFeature))
481 continue; /// composite of the current feature should be skipped
482 aFilteredFeatures.insert(aCFeature);
484 theReferences[aFeature] = aFilteredFeatures;
486 if (theUseRecursion) {
487 #ifdef DEBUG_CYCLING_1550
488 findReferences(aSelRefFeatures, theReferences, theUseComposite,
489 theUseRecursion, theRecLevel);
491 findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
499 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
500 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
501 const bool theUseComposite,
502 const bool theUseRecursion)
504 // For dependencies, find main_list:
505 // sk_1(ext_1, vertex_1)
506 // ext_1(bool_1, sk_3)
511 // Information: bool_1 is not selected, ext_2(bool_2) exists
512 // find all referenced features
513 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
515 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
517 #ifdef DEBUG_REMOVE_FEATURES
518 printMapInfo(aMainList, "firstDependencies");
520 // find all dependencies for each object:
521 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
522 // ext_1(bool_1, sk_3)
524 // sk_2(ext_2) + (bool_1)
527 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
528 aMainLast = aMainList.end();
529 for (; aMainIt != aMainLast; aMainIt++) {
530 FeaturePtr aMainListFeature = aMainIt->first;
532 //std::string aName = aMainListFeature->name();
533 std::set<FeaturePtr> aMainRefList = aMainIt->second;
535 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
537 int n = sprintf(aBuf, "%d", aMainRefList.size());
538 std::string aSize(aBuf);
539 std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
540 << ", references size = " << aSize << std::endl;
542 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
543 aLast = aMainRefList.end();
544 std::set<FeaturePtr> aResultRefList;
545 aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
546 for (; anIt != aLast; anIt++) {
547 FeaturePtr aFeature = *anIt;
549 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
550 std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
553 addRefsToFeature(aFeature, aMainList,
554 aRecLevel, aResultRefList/*aMainRefList*/);
556 theReferences[aMainListFeature] = aResultRefList;
558 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
559 std::cout << std::endl;
562 #ifdef DEBUG_REMOVE_FEATURES
563 printMapInfo(theReferences, "allDependencies");
567 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
568 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
569 std::set<FeaturePtr>& theFeaturesRefsTo)
571 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
572 aLast = theFeatures.end();
573 for (; anIt != aLast; anIt++) {
574 FeaturePtr aFeature = *anIt;
575 if (theReferences.find(aFeature) == theReferences.end())
577 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
578 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
579 for (; aRefIt != aRefLast; aRefIt++) {
580 FeaturePtr aRefFeature = *aRefIt;
581 CompositeFeaturePtr aComposite =
582 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
583 if (aComposite.get() && aComposite->isSub(aFeature))
584 continue; /// composite of the current feature should not be removed
586 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
587 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
588 theFeaturesRefsTo.insert(aRefFeature);
593 void getConcealedResults(const FeaturePtr& theFeature,
594 std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
596 SessionPtr aSession = ModelAPI_Session::get();
598 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
599 theFeature->data()->referencesToObjects(aRefs);
600 std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
601 anIt = aRefs.begin(), aLast = aRefs.end();
602 std::set<ResultPtr> alreadyThere; // to avoid duplications
603 for (; anIt != aLast; anIt++) {
604 if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
605 continue; // use only concealed attributes
606 std::list<ObjectPtr> anObjects = (*anIt).second;
607 std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
608 for (; anOIt != anOLast; anOIt++) {
609 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
610 if (aResult && aResult->isConcealed()) {
611 if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
612 alreadyThere.insert(aResult);
614 theResults.push_back(aResult);
620 std::pair<std::wstring, bool> getDefaultName(const std::shared_ptr<ModelAPI_Result>& theResult,
621 const bool theInherited)
623 typedef std::list< std::pair < std::string, std::list<ObjectPtr> > > ListOfReferences;
625 SessionPtr aSession = ModelAPI_Session::get();
627 ResultBodyPtr anOwnerRes = bodyOwner(theResult);
629 // names of sub-solids in CompSolid should be default (for example,
630 // result of boolean operation 'Boolean_1_1' is a CompSolid which is renamed to 'MyBOOL',
631 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1_1', 'Boolean_1_1_2' etc.)
632 std::wostringstream aDefaultName;
633 aDefaultName << getDefaultName(anOwnerRes).first;
634 aDefaultName << "_" << (bodyIndex(theResult) + 1);
635 return std::pair<std::wstring, bool>(aDefaultName.str(), false);
638 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult->data()->owner());
639 DataPtr aData = anOwner->data();
641 ListOfReferences aReferences;
642 // find first result with user-defined name
643 ListOfReferences::const_iterator aFoundRef = aReferences.end();
645 aData->referencesToObjects(aReferences);
647 for (ListOfReferences::const_iterator aRefIt = aReferences.begin();
648 aRefIt != aReferences.end(); ++aRefIt) {
649 bool isConcealed = aSession->validators()->isConcealed(anOwner->getKind(), aRefIt->first);
650 bool isMainArg = isConcealed &&
651 aSession->validators()->isMainArgument(anOwner->getKind(), aRefIt->first);
653 // check the referred object is a Body
654 // (for example, ExtrusionCut has a sketch as a first attribute which is concealing)
655 bool isBody = aRefIt->second.size() > 1 || (aRefIt->second.size() == 1 &&
656 aRefIt->second.front().get() &&
657 aRefIt->second.front()->groupName() == ModelAPI_ResultBody::group());
658 if (isBody && (isMainArg || aFoundRef == aReferences.end() ||
659 aData->isPrecedingAttribute(aRefIt->first, aFoundRef->first)))
667 // get the result number in the feature
668 int anIndexInOwner = 0;
669 const std::list<ResultPtr>& anOwnerResults = anOwner->results();
670 std::list<ResultPtr>::const_iterator aResIt = anOwnerResults.cbegin();
671 for(; aResIt != anOwnerResults.cend(); aResIt++) {
672 if(*aResIt == theResult)
677 // find an object which is concealed by theResult
678 if (aFoundRef != aReferences.end() && !aFoundRef->second.empty()) {
679 // store number of references for each object
680 std::map<ResultPtr, int> aNbRefToObject;
681 // search the object by result index
682 std::list<ObjectPtr>::const_iterator anObjIt = aFoundRef->second.begin();
683 int aResultIndex = anIndexInOwner;
684 while (--aResultIndex >= 0) {
685 ResultPtr aCurRes = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIt);
686 ResultBodyPtr aParentBody = ModelAPI_Tools::bodyOwner(aCurRes);
688 aCurRes = aParentBody;
689 if (aNbRefToObject.find(aCurRes) == aNbRefToObject.end())
690 aNbRefToObject[aCurRes] = 1;
692 aNbRefToObject[aCurRes] += 1;
695 if (anObjIt == aFoundRef->second.end()) {
696 anObjIt = aFoundRef->second.begin();
700 // check the result is a Body
701 if ((*anObjIt)->groupName() == ModelAPI_ResultBody::group()) {
702 // check the result is part of CompSolid
703 ResultPtr anObjRes = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIt);
704 ResultBodyPtr aParentBody = ModelAPI_Tools::bodyOwner(anObjRes);
706 anObjRes = aParentBody;
708 // return name of reference result only if it has been renamed by the user,
709 // in other case compose a default name
710 if (anObjRes->data()->hasUserDefinedName()) {
711 std::wstringstream aName;
712 aName << anObjRes->data()->name();
713 std::map<ResultPtr, int>::iterator aFound = aNbRefToObject.find(anObjRes);
714 if (aFound != aNbRefToObject.end()) {
715 // to generate unique name, add suffix if there are several results
716 // referring to the same shape
717 aName << "_" << aFound->second + 1;
719 return std::pair<std::wstring, bool>(aName.str(), true);
724 // compose default name by the name of the feature and the index of result
725 std::wstringstream aDefaultName;
726 aDefaultName << anOwner->name();
727 // if there are several results (issue #899: any number of result),
728 // add unique prefix starting from second
729 if (anIndexInOwner > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
730 aDefaultName << "_" << anIndexInOwner + 1;
731 return std::pair<std::wstring, bool>(aDefaultName.str(), false);
734 std::set<FeaturePtr> getParents(const FeaturePtr& theFeature)
736 std::set<FeaturePtr> aParents;
737 for (FeaturePtr aCurFeat = theFeature; aCurFeat; ) {
738 CompositeFeaturePtr aFoundComposite;
739 const std::set<AttributePtr>& aRefs = aCurFeat->data()->refsToMe();
740 for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
741 anIt != aRefs.end(); ++anIt) {
742 FeaturePtr aF = ModelAPI_Feature::feature((*anIt)->owner());
743 aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aF);
744 if (aFoundComposite && aFoundComposite->isSub(aCurFeat))
747 aFoundComposite = CompositeFeaturePtr();
750 if (aFoundComposite) {
751 aParents.insert(aFoundComposite);
752 aCurFeat = aFoundComposite;
755 // add the part containing high-level feature
756 SessionPtr aSession = ModelAPI_Session::get();
757 DocumentPtr aPartSetDoc = aSession->moduleDocument();
758 std::list<FeaturePtr> aPartSetFeatures = aPartSetDoc->allFeatures();
759 for (std::list<FeaturePtr>::const_iterator anIt = aPartSetFeatures.begin();
760 anIt != aPartSetFeatures.end(); ++anIt) {
761 aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
762 if (aFoundComposite && aFoundComposite->isSub(aCurFeat)) {
763 aParents.insert(aFoundComposite);
768 aCurFeat = FeaturePtr();
774 void fillShapeHierarchy(const GeomShapePtr& theShape,
775 const ResultPtr& theContext,
776 GeomAPI_ShapeHierarchy& theHierarchy)
778 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(theContext);
779 if (aResCompSolidPtr.get()) {
780 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
781 if (aContextShape->shapeType() <= GeomAPI_Shape::COMPSOLID) {
782 theHierarchy.addParent(theShape, aContextShape);
783 fillShapeHierarchy(aContextShape, aResCompSolidPtr, theHierarchy);
789 void removeResults(const std::list<ResultPtr>& theResults)
791 // collect all documents where the results must be removed
792 std::map<DocumentPtr, std::list<ResultPtr> > aDocs;
794 std::list<ResultPtr>::const_iterator aResIter = theResults.cbegin();
795 for(; aResIter != theResults.cend(); aResIter++) {
796 DocumentPtr aDoc = (*aResIter)->document();
797 if (!aDocs.count(aDoc))
798 aDocs[aDoc] = std::list<ResultPtr>();
799 aDocs[aDoc].push_back(*aResIter);
801 // create a "remove" feature in each doc
802 std::map<DocumentPtr, std::list<ResultPtr> >::iterator aDoc = aDocs.begin();
803 for(; aDoc != aDocs.end(); aDoc++) {
804 FeaturePtr aRemove = aDoc->first->addFeature("RemoveResults");
806 for(aResIter = aDoc->second.cbegin(); aResIter != aDoc->second.cend(); aResIter++)
807 aRemove->selectionList("results")->append(*aResIter, GeomShapePtr());
815 //**************************************************************
816 void setDeflection(ResultPtr theResult, const double theDeflection)
818 if (!theResult.get())
821 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
822 if (aDeflectionAttr.get() != NULL) {
823 aDeflectionAttr->setValue(theDeflection);
827 double getDeflection(const std::shared_ptr<ModelAPI_Result>& theResult)
829 double aDeflection = -1;
830 // get deflection from the attribute of the result
831 if (theResult.get() != NULL &&
832 theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
833 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
834 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
835 double aValue = aDoubleAttr->value();
836 if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
837 aDeflection = aDoubleAttr->value();
843 //******************************************************
844 void setColor(ResultPtr theResult, const std::vector<int>& theColor)
846 if (!theResult.get())
849 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
850 if (aColorAttr.get() != NULL) {
851 if (!aColorAttr->size()) {
852 aColorAttr->setSize(3);
854 aColorAttr->setValue(0, theColor[0]);
855 aColorAttr->setValue(1, theColor[1]);
856 aColorAttr->setValue(2, theColor[2]);
860 void getColor(const std::shared_ptr<ModelAPI_Result>& theResult, std::vector<int>& theColor)
863 // get color from the attribute of the result
864 if (theResult.get() != NULL &&
865 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
866 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
867 if (aColorAttr.get() && aColorAttr->size()) {
868 theColor.push_back(aColorAttr->value(0));
869 theColor.push_back(aColorAttr->value(1));
870 theColor.push_back(aColorAttr->value(2));
875 //******************************************************
876 void getIsoLines(const std::shared_ptr<ModelAPI_Result>& theResult,
877 bool& isVisible, std::vector<int>& theNbLines)
881 if (!theResult.get())
883 if (theResult->groupName() == ModelAPI_ResultConstruction::group()) {
884 theNbLines.push_back(0);
885 theNbLines.push_back(0);
888 // get color from the attribute of the result
889 AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
892 theNbLines.push_back(aAttr->value(0));
893 theNbLines.push_back(aAttr->value(1));
896 AttributeBooleanPtr aBoolAttr =
897 theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
898 if (aBoolAttr.get()) {
899 isVisible = aBoolAttr->value();
904 //******************************************************
905 void setIsoLines(ResultPtr theResult, const std::vector<int>& theIso)
907 if (!theResult.get())
910 AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
911 if (aAttr.get() != NULL) {
912 if (!aAttr->size()) {
915 aAttr->setValue(0, theIso[0]);
916 aAttr->setValue(1, theIso[1]);
920 //******************************************************
921 void showIsoLines(std::shared_ptr<ModelAPI_Result> theResult, bool theShow)
923 if (!theResult.get())
926 AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
927 if (aAttr.get() != NULL) {
928 aAttr->setValue(theShow);
932 //******************************************************
933 bool isShownIsoLines(std::shared_ptr<ModelAPI_Result> theResult)
935 if (!theResult.get())
938 AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
939 if (aAttr.get() != NULL) {
940 return aAttr->value();
945 //**************************************************************
946 void setTransparency(ResultPtr theResult, double theTransparency)
948 if (!theResult.get())
951 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
952 if (anAttribute.get() != NULL) {
953 anAttribute->setValue(theTransparency);
957 double getTransparency(const std::shared_ptr<ModelAPI_Result>& theResult)
959 double aTransparency = -1;
960 // get transparency from the attribute of the result
961 if (theResult.get() != NULL &&
962 theResult->data()->attribute(ModelAPI_Result::TRANSPARENCY_ID()).get() != NULL) {
963 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
964 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
965 aTransparency = aDoubleAttr->value();
968 return aTransparency;
971 void copyVisualizationAttrs(
972 std::shared_ptr<ModelAPI_Result> theSource, std::shared_ptr<ModelAPI_Result> theDest)
975 AttributeIntArrayPtr aSourceColor = theSource->data()->intArray(ModelAPI_Result::COLOR_ID());
976 if (aSourceColor.get() && aSourceColor->isInitialized() && aSourceColor->size()) {
977 AttributeIntArrayPtr aDestColor = theDest->data()->intArray(ModelAPI_Result::COLOR_ID());
978 if (aDestColor.get()) {
979 aDestColor->setSize(aSourceColor->size());
980 for(int a = 0; a < aSourceColor->size(); a++)
981 aDestColor->setValue(a, aSourceColor->value(a));
985 AttributeIntArrayPtr aSource = theSource->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
986 if (aSource.get() && aSource->isInitialized() && aSource->size()) {
987 AttributeIntArrayPtr aDest = theDest->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
989 aDest->setSize(aSource->size());
990 for(int a = 0; a < aSource->size(); a++)
991 aDest->setValue(a, aSource->value(a));
995 AttributeDoublePtr aSourceDefl = theSource->data()->real(ModelAPI_Result::DEFLECTION_ID());
996 if (aSourceDefl.get() && aSourceDefl->isInitialized()) {
997 AttributeDoublePtr aDestDefl = theDest->data()->real(ModelAPI_Result::DEFLECTION_ID());
998 if (aDestDefl.get()) {
999 aDestDefl->setValue(aSourceDefl->value());
1003 AttributeDoublePtr aSourceTransp = theSource->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1004 if (aSourceTransp.get() && aSourceTransp->isInitialized()) {
1005 AttributeDoublePtr aDestTransp = theDest->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1006 if (aDestTransp.get()) {
1007 aDestTransp->setValue(aSourceTransp->value());
1012 std::list<FeaturePtr> referencedFeatures(
1013 std::shared_ptr<ModelAPI_Result> theTarget, const std::string& theFeatureKind,
1014 const bool theSortResults)
1016 std::set<FeaturePtr> aResSet; // collect in the set initially to avoid duplicates
1017 std::list<ResultPtr> allSubRes;
1018 allSubRes.push_back(theTarget);
1019 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theTarget);
1021 allSubs(aBody, allSubRes);
1022 std::list<ResultPtr>::iterator aSub = allSubRes.begin();
1023 for(; aSub != allSubRes.end(); aSub++) {
1024 const std::set<AttributePtr>& aRefs = (*aSub)->data()->refsToMe();
1025 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1026 for(; aRef != aRefs.cend(); aRef++) {
1027 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1028 if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1029 aResSet.insert(aFeat);
1032 // add also feature of the target that may be referenced as a whole
1033 FeaturePtr aTargetFeature = theTarget->document()->feature(theTarget);
1034 const std::set<AttributePtr>& aRefs = aTargetFeature->data()->refsToMe();
1035 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1036 for(; aRef != aRefs.cend(); aRef++) {
1037 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1038 if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1039 aResSet.insert(aFeat);
1041 // check also Group-operations that may refer to groups - add them for theFeatureKind "Group"
1042 if (theFeatureKind == "Group") {
1043 std::set<FeaturePtr> aGroupOperations;
1044 for(bool aNeedIterate = true; aNeedIterate; ) {
1045 std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1046 for(; aResIter != aResSet.end(); aResIter++) {
1047 std::list<ResultPtr>::const_iterator aGroupRes = (*aResIter)->results().cbegin();
1048 for(; aGroupRes != (*aResIter)->results().cend(); aGroupRes++) {
1049 const std::set<AttributePtr>& aGroupRefs = (*aGroupRes)->data()->refsToMe();
1050 std::set<AttributePtr>::const_iterator aRefIt = aGroupRefs.cbegin();
1051 for(; aRefIt != aGroupRefs.cend(); aRefIt++) {
1052 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
1053 if (aFeat.get() && !aGroupOperations.count(aFeat) && !aFeat->results().empty() &&
1054 aFeat->firstResult()->groupName() == ModelAPI_ResultGroup::group()) {
1055 // iterate results of this group operation because it may be without theTarget shape
1056 GeomShapePtr aTargetShape = theTarget->shape();
1057 bool anIsIn = false;
1058 std::list<ResultPtr>::const_iterator anOpRes = aFeat->results().cbegin();
1059 for(; anOpRes != aFeat->results().cend() && !anIsIn; anOpRes++) {
1060 GeomShapePtr anOpShape = (*anOpRes)->shape();
1061 if (!anOpShape.get() || anOpShape->isNull())
1063 for(GeomAPI_ShapeIterator aSubIt(anOpShape); aSubIt.more(); aSubIt.next()) {
1064 if (aTargetShape->isSubShape(aSubIt.current(), false)) {
1071 aGroupOperations.insert(aFeat);
1076 // insert all new group operations into result and if they are, check for next dependencies
1077 aNeedIterate = false;
1078 std::set<FeaturePtr>::iterator aGroupOpIter = aGroupOperations.begin();
1079 for(; aGroupOpIter != aGroupOperations.end(); aGroupOpIter++) {
1080 if (aResSet.find(*aGroupOpIter) == aResSet.end()) {
1081 aResSet.insert(*aGroupOpIter);
1082 aNeedIterate = true;
1088 std::list<FeaturePtr> aResList;
1089 std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1090 for(; aResIter != aResSet.end(); aResIter++) {
1091 if (theSortResults) { // sort results by creation-order
1092 std::list<FeaturePtr>::iterator aListIter = aResList.begin();
1093 for(; aListIter != aResList.end(); aListIter++) {
1094 if ((*aResIter)->document()->isLater(*aListIter, *aResIter))
1097 if (aListIter == aResList.end()) // goes to the end
1098 aResList.push_back(*aResIter);
1100 aResList.insert(aListIter, *aResIter);
1101 } else //just push to the end unsorted
1102 aResList.push_back(*aResIter);
1109 } // namespace ModelAPI_Tools