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>
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::wstring& theName)
176 for (int anIndex = 0; anIndex < theDocument->size(theGroup); ++anIndex) {
177 ObjectPtr anObject = theDocument->object(theGroup, anIndex);
178 if (anObject->data()->name() == theName)
185 bool findVariable(const DocumentPtr& theDocument, FeaturePtr theSearcher,
186 const std::wstring& theName, double& outValue, ResultParameterPtr& theParam)
188 ObjectPtr aParamObj = objectByName(theDocument, ModelAPI_ResultParameter::group(), theName);
189 theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
192 // avoid usage of parameters created later than the initial parameter
194 if (theSearcher.get()) {
195 FeaturePtr aParamFeat = theDocument->feature(theParam);
196 if (aParamFeat == theSearcher || theDocument->isLater(aParamFeat, theSearcher))
199 AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
200 outValue = aValueAttribute->value();
204 bool findVariable(FeaturePtr theSearcher, const std::wstring& theName, double& outValue,
205 ResultParameterPtr& theParam, const DocumentPtr& theDocument)
207 SessionPtr aSession = ModelAPI_Session::get();
208 std::list<DocumentPtr> aDocList;
209 DocumentPtr aDocument = theDocument.get() ? theDocument : aSession->activeDocument();
210 if (findVariable(aDocument, theSearcher, theName, outValue, theParam))
212 DocumentPtr aRootDocument = aSession->moduleDocument();
213 if (aDocument != aRootDocument) {
214 // any parameters in PartSet is okindependently on the Part position (issu #1504)
215 if (findVariable(aRootDocument, FeaturePtr(), theName, outValue, theParam))
221 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
223 // to optimize and avoid of crash on partset document close
224 // (don't touch the sub-document structure)
225 if (theMain != theSub) {
226 for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
227 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
228 theMain->object(ModelAPI_ResultPart::group(), a));
229 if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
237 FeaturePtr findPartFeature(const DocumentPtr& theMain, const DocumentPtr& theSub)
239 // to optimize and avoid of crash on partset document close
240 // (don't touch the sub-document structure)
241 if (theMain != theSub) {
242 // iteration from top to bottom to avoid finding the movement documents before the original
243 int aSize = theMain->size(ModelAPI_Feature::group());
244 for (int a = 0; a < aSize; a++) {
245 FeaturePtr aPartFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(
246 theMain->object(ModelAPI_Feature::group(), a));
247 if (aPartFeat.get()) {
248 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aPartFeat->results();
249 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
250 for(; aRes != aResList.end(); aRes++) {
251 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
253 if (aPart->isActivated() && aPart->partDoc() == theSub)
255 } else break; // if the first is not Part, others are also not
263 CompositeFeaturePtr compositeOwner(const FeaturePtr& theFeature)
265 if (theFeature.get() && theFeature->data() && theFeature->data()->isValid()) {
266 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
267 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.begin();
268 for(; aRefIter != aRefs.end(); aRefIter++) {
269 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
270 ((*aRefIter)->owner());
271 if (aComp.get() && aComp->data()->isValid() && aComp->isSub(theFeature))
275 return CompositeFeaturePtr(); // not found
278 ResultBodyPtr bodyOwner(const ResultPtr& theSub, const bool theRoot)
281 ObjectPtr aParent = theSub->document()->parent(theSub);
283 if (theRoot) { // try to find parent of parent
284 ResultPtr aResultParent = std::dynamic_pointer_cast<ModelAPI_Result>(aParent);
285 ResultBodyPtr aGrandParent = bodyOwner(aResultParent, true);
286 if (aGrandParent.get())
287 aParent = aGrandParent;
289 return std::dynamic_pointer_cast<ModelAPI_ResultBody>(aParent);
292 return ResultBodyPtr(); // not found
295 int bodyIndex(const ResultPtr& theSub)
298 ResultBodyPtr aParent = bodyOwner(theSub);
300 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theSub);
301 if (aBody.get() && aParent->isSub(aBody, anIndex))
304 return anIndex; // not found
307 bool hasSubResults(const ResultPtr& theResult)
309 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
310 return aCompSolid.get() && aCompSolid->numberOfSubs() > 0;
313 void allSubs(const ResultBodyPtr& theResult, std::list<ResultPtr>& theResults,
314 const bool theLowerOnly) {
315 // iterate sub-bodies of compsolid
316 ResultBodyPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
318 int aNumSub = aComp->numberOfSubs();
319 for (int a = 0; a < aNumSub; a++) {
320 ResultBodyPtr aSub = aComp->subResult(a);
321 if (!theLowerOnly || aSub->numberOfSubs() == 0)
322 theResults.push_back(aSub);
323 allSubs(aSub, theResults);
328 void allResults(const FeaturePtr& theFeature, std::list<ResultPtr>& theResults)
330 if (!theFeature.get()) // safety: for empty feature no results
332 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
333 std::list<ResultPtr>::const_iterator aRIter = aResults.begin();
334 for (; aRIter != aResults.cend(); aRIter++) {
335 theResults.push_back(*aRIter);
336 ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRIter);
337 allSubs(aResult, theResults);
341 //******************************************************************
342 bool allDocumentsActivated(std::wstring& theNotActivatedNames)
344 theNotActivatedNames = L"";
345 bool anAllPartActivated = true;
347 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
348 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
349 for (int i = 0; i < aSize; i++) {
350 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
351 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
352 if (!aPart->isActivated()) {
353 anAllPartActivated = false;
354 if (!theNotActivatedNames.empty())
355 theNotActivatedNames += L", ";
356 theNotActivatedNames += aObject->data()->name().c_str();
359 return anAllPartActivated;
362 bool removeFeaturesAndReferences(const std::set<FeaturePtr>& theFeatures,
363 const bool /*theFlushRedisplay*/,
364 const bool theUseComposite,
365 const bool theUseRecursion)
367 #ifdef DEBUG_REMOVE_FEATURES
368 printListInfo(theFeatures, "selection: ");
371 std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
372 ModelAPI_Tools::findAllReferences(theFeatures, aReferences, theUseComposite, theUseRecursion);
373 #ifdef DEBUG_REMOVE_FEATURES
374 printMapInfo(aReferences, "allDependencies: ");
377 std::set<FeaturePtr> aFeaturesRefsTo;
378 ModelAPI_Tools::findRefsToFeatures(theFeatures, aReferences, aFeaturesRefsTo);
379 #ifdef DEBUG_REMOVE_FEATURES
380 printListInfo(aFeaturesRefsTo, "references: ");
383 std::set<FeaturePtr> aFeatures = theFeatures;
384 if (!aFeaturesRefsTo.empty())
385 aFeatures.insert(aFeaturesRefsTo.begin(), aFeaturesRefsTo.end());
386 #ifdef DEBUG_REMOVE_FEATURES
387 printListInfo(aFeatures, "removeFeatures: ");
390 return ModelAPI_Tools::removeFeatures(aFeatures, false);
393 //***********************************************************************
394 bool removeFeatures(const std::set<FeaturePtr>& theFeatures,
395 const bool theFlushRedisplay)
398 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
399 aLast = theFeatures.end();
400 for (; anIt != aLast; anIt++) {
401 FeaturePtr aFeature = *anIt;
402 if (aFeature.get()) {
403 DocumentPtr aDoc = aFeature->document();
404 // flush REDISPLAY signal after remove feature
405 aDoc->removeFeature(aFeature);
409 if (isDone && theFlushRedisplay) {
410 // the redisplay signal should be flushed in order to erase
411 // the feature presentation in the viewer
412 // if should be done after removeFeature() of document
413 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
418 //***********************************************************************
419 // Fills the references list by all references of the feature from the references map.
420 // This is a recusive method to find references by next found feature in the map of references.
421 // \param theFeature a feature to find references
422 // \param theReferencesMap a map of references
423 // \param theReferences an out container of references
424 void addRefsToFeature(const FeaturePtr& theFeature,
425 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferencesMap,
427 std::set<FeaturePtr>& theReferences)
429 if (theRecLevel > RECURSE_TOP_LEVEL)
433 if (theReferencesMap.find(theFeature) == theReferencesMap.end())
434 return; // this feature is not in the selection list, so exists without references to it
435 std::set<FeaturePtr> aMainReferences = theReferencesMap.at(theFeature);
437 std::set<FeaturePtr>::const_iterator anIt = aMainReferences.begin(),
438 aLast = aMainReferences.end();
439 for (; anIt != aLast; anIt++) {
440 FeaturePtr aRefFeature = *anIt;
441 if (theReferences.find(aRefFeature) == theReferences.end()) {
442 addRefsToFeature(aRefFeature, theReferencesMap, theRecLevel, theReferences);
443 theReferences.insert(aRefFeature);
448 // For each feature from the feature list it searches references to the feature and append them
449 // to the references map. This is a recusive method.
450 // \param theFeature a feature to find references
451 // \param theReferencesMap a map of references
452 // \param theReferences an out container of references
453 void findReferences(const std::set<FeaturePtr>& theFeatures,
454 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
455 const bool theUseComposite, const bool theUseRecursion, int theRecLevel)
457 if (theRecLevel > RECURSE_TOP_LEVEL)
460 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
461 aLast = theFeatures.end();
462 for (; anIt != aLast; anIt++) {
463 FeaturePtr aFeature = *anIt;
464 if (aFeature.get() && theReferences.find(aFeature) == theReferences.end()) {
465 DocumentPtr aSelFeatureDoc = aFeature->document();
466 std::set<FeaturePtr> aSelRefFeatures;
467 aSelFeatureDoc->refsToFeature(aFeature, aSelRefFeatures, false/*do not emit signals*/);
468 if (theUseComposite) { // do not filter selection
469 theReferences[aFeature] = aSelRefFeatures;
471 else { // filter references to skip composition features of the current feature
472 std::set<FeaturePtr> aFilteredFeatures;
473 std::set<FeaturePtr>::const_iterator aRefIt = aSelRefFeatures.begin(),
474 aRefLast = aSelRefFeatures.end();
475 for (; aRefIt != aRefLast; aRefIt++) {
476 FeaturePtr aCFeature = *aRefIt;
477 CompositeFeaturePtr aComposite =
478 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCFeature);
479 if (aComposite.get() && aComposite->isSub(aFeature))
480 continue; /// composite of the current feature should be skipped
481 aFilteredFeatures.insert(aCFeature);
483 theReferences[aFeature] = aFilteredFeatures;
485 if (theUseRecursion) {
486 #ifdef DEBUG_CYCLING_1550
487 findReferences(aSelRefFeatures, theReferences, theUseComposite,
488 theUseRecursion, theRecLevel);
490 findReferences(theReferences[aFeature], theReferences, theUseComposite, theUseRecursion,
498 void findAllReferences(const std::set<FeaturePtr>& theFeatures,
499 std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
500 const bool theUseComposite,
501 const bool theUseRecursion)
503 // For dependencies, find main_list:
504 // sk_1(ext_1, vertex_1)
505 // ext_1(bool_1, sk_3)
510 // Information: bool_1 is not selected, ext_2(bool_2) exists
511 // find all referenced features
512 std::map<FeaturePtr, std::set<FeaturePtr> > aMainList;
514 findReferences(theFeatures, aMainList, theUseComposite, theUseRecursion, aRecLevel);
516 #ifdef DEBUG_REMOVE_FEATURES
517 printMapInfo(aMainList, "firstDependencies");
519 // find all dependencies for each object:
520 // sk_1(ext_1, vertex_1) + (sk_3, bool_1)
521 // ext_1(bool_1, sk_3)
523 // sk_2(ext_2) + (bool_1)
526 std::map<FeaturePtr, std::set<FeaturePtr> >::const_iterator aMainIt = aMainList.begin(),
527 aMainLast = aMainList.end();
528 for (; aMainIt != aMainLast; aMainIt++) {
529 FeaturePtr aMainListFeature = aMainIt->first;
531 //std::string aName = aMainListFeature->name();
532 std::set<FeaturePtr> aMainRefList = aMainIt->second;
534 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
536 int n = sprintf(aBuf, "%d", aMainRefList.size());
537 std::string aSize(aBuf);
538 std::cout << "_findAllReferences for the Feature: " << getFeatureInfo(aMainListFeature)
539 << ", references size = " << aSize << std::endl;
541 std::set<FeaturePtr>::const_iterator anIt = aMainRefList.begin(),
542 aLast = aMainRefList.end();
543 std::set<FeaturePtr> aResultRefList;
544 aResultRefList.insert(aMainRefList.begin(), aMainRefList.end());
545 for (; anIt != aLast; anIt++) {
546 FeaturePtr aFeature = *anIt;
548 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
549 std::cout << " Ref: " << getFeatureInfo(aFeature) << std::endl;
552 addRefsToFeature(aFeature, aMainList,
553 aRecLevel, aResultRefList/*aMainRefList*/);
555 theReferences[aMainListFeature] = aResultRefList;
557 #ifdef DEBUG_REMOVE_FEATURES_RECURSE
558 std::cout << std::endl;
561 #ifdef DEBUG_REMOVE_FEATURES
562 printMapInfo(theReferences, "allDependencies");
566 void findRefsToFeatures(const std::set<FeaturePtr>& theFeatures,
567 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
568 std::set<FeaturePtr>& theFeaturesRefsTo)
570 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
571 aLast = theFeatures.end();
572 for (; anIt != aLast; anIt++) {
573 FeaturePtr aFeature = *anIt;
574 if (theReferences.find(aFeature) == theReferences.end())
576 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
577 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
578 for (; aRefIt != aRefLast; aRefIt++) {
579 FeaturePtr aRefFeature = *aRefIt;
580 CompositeFeaturePtr aComposite =
581 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aRefFeature);
582 if (aComposite.get() && aComposite->isSub(aFeature))
583 continue; /// composite of the current feature should not be removed
585 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
586 theFeaturesRefsTo.find(aRefFeature) == theFeaturesRefsTo.end()) // it is not added
587 theFeaturesRefsTo.insert(aRefFeature);
592 void getConcealedResults(const FeaturePtr& theFeature,
593 std::list<std::shared_ptr<ModelAPI_Result> >& theResults)
595 SessionPtr aSession = ModelAPI_Session::get();
597 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
598 theFeature->data()->referencesToObjects(aRefs);
599 std::list<std::pair<std::string, std::list<ObjectPtr> > >::const_iterator
600 anIt = aRefs.begin(), aLast = aRefs.end();
601 std::set<ResultPtr> alreadyThere; // to avoid duplications
602 for (; anIt != aLast; anIt++) {
603 if (!aSession->validators()->isConcealed(theFeature->getKind(), anIt->first))
604 continue; // use only concealed attributes
605 std::list<ObjectPtr> anObjects = (*anIt).second;
606 std::list<ObjectPtr>::const_iterator anOIt = anObjects.begin(), anOLast = anObjects.end();
607 for (; anOIt != anOLast; anOIt++) {
608 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anOIt);
609 if (aResult && aResult->isConcealed()) {
610 if (alreadyThere.find(aResult) == alreadyThere.end()) // issue 1712, avoid duplicates
611 alreadyThere.insert(aResult);
613 theResults.push_back(aResult);
619 std::pair<std::wstring, bool> getDefaultName(const std::shared_ptr<ModelAPI_Result>& theResult,
620 const bool theInherited,
621 const bool theRecursive)
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->get() && (*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 (theRecursive && anObjRes->data()->name() != getDefaultName(anObjRes).first)) {
712 std::wstringstream aName;
713 aName << anObjRes->data()->name();
714 std::map<ResultPtr, int>::iterator aFound = aNbRefToObject.find(anObjRes);
715 if (aFound != aNbRefToObject.end()) {
716 // to generate unique name, add suffix if there are several results
717 // referring to the same shape
718 aName << "_" << aFound->second + 1;
720 return std::pair<std::wstring, bool>(aName.str(), true);
725 // compose default name by the name of the feature and the index of result
726 std::wstringstream aDefaultName;
727 aDefaultName << anOwner->name();
728 // if there are several results (issue #899: any number of result),
729 // add unique prefix starting from second
730 if (anIndexInOwner > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
731 aDefaultName << "_" << anIndexInOwner + 1;
732 return std::pair<std::wstring, bool>(aDefaultName.str(), false);
735 std::set<FeaturePtr> getParents(const FeaturePtr& theFeature)
737 std::set<FeaturePtr> aParents;
738 for (FeaturePtr aCurFeat = theFeature; aCurFeat; ) {
739 CompositeFeaturePtr aFoundComposite;
740 const std::set<AttributePtr>& aRefs = aCurFeat->data()->refsToMe();
741 for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin();
742 anIt != aRefs.end(); ++anIt) {
743 FeaturePtr aF = ModelAPI_Feature::feature((*anIt)->owner());
744 aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aF);
745 if (aFoundComposite && aFoundComposite->isSub(aCurFeat))
748 aFoundComposite = CompositeFeaturePtr();
751 if (aFoundComposite) {
752 aParents.insert(aFoundComposite);
753 aCurFeat = aFoundComposite;
756 // add the part containing high-level feature
757 SessionPtr aSession = ModelAPI_Session::get();
758 DocumentPtr aPartSetDoc = aSession->moduleDocument();
759 std::list<FeaturePtr> aPartSetFeatures = aPartSetDoc->allFeatures();
760 for (std::list<FeaturePtr>::const_iterator anIt = aPartSetFeatures.begin();
761 anIt != aPartSetFeatures.end(); ++anIt) {
762 aFoundComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
763 if (aFoundComposite && aFoundComposite->isSub(aCurFeat)) {
764 aParents.insert(aFoundComposite);
769 aCurFeat = FeaturePtr();
775 void fillShapeHierarchy(const GeomShapePtr& theShape,
776 const ResultPtr& theContext,
777 GeomAPI_ShapeHierarchy& theHierarchy)
779 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(theContext);
780 if (aResCompSolidPtr.get()) {
781 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
782 if (aContextShape->shapeType() <= GeomAPI_Shape::COMPSOLID) {
783 theHierarchy.addParent(theShape, aContextShape);
784 fillShapeHierarchy(aContextShape, aResCompSolidPtr, theHierarchy);
790 void removeResults(const std::list<ResultPtr>& theResults)
792 // collect all documents where the results must be removed
793 std::map<DocumentPtr, std::list<ResultPtr> > aDocs;
795 std::list<ResultPtr>::const_iterator aResIter = theResults.cbegin();
796 for(; aResIter != theResults.cend(); aResIter++) {
797 DocumentPtr aDoc = (*aResIter)->document();
798 if (!aDocs.count(aDoc))
799 aDocs[aDoc] = std::list<ResultPtr>();
800 aDocs[aDoc].push_back(*aResIter);
802 // create a "remove" feature in each doc
803 std::map<DocumentPtr, std::list<ResultPtr> >::iterator aDoc = aDocs.begin();
804 for(; aDoc != aDocs.end(); aDoc++) {
805 FeaturePtr aRemove = aDoc->first->addFeature("RemoveResults");
807 for(aResIter = aDoc->second.cbegin(); aResIter != aDoc->second.cend(); aResIter++)
808 aRemove->selectionList("results")->append(*aResIter, GeomShapePtr());
816 //**************************************************************
817 void setDeflection(ResultPtr theResult, const double theDeflection)
819 if (!theResult.get())
822 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
823 if (aDeflectionAttr.get() != NULL) {
824 aDeflectionAttr->setValue(theDeflection);
828 double getDeflection(const std::shared_ptr<ModelAPI_Result>& theResult)
830 double aDeflection = -1;
831 // get deflection from the attribute of the result
832 if (theResult.get() != NULL &&
833 theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
834 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
835 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
836 double aValue = aDoubleAttr->value();
837 if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
838 aDeflection = aDoubleAttr->value();
844 //******************************************************
845 void setColor(ResultPtr theResult, const std::vector<int>& theColor)
847 if (!theResult.get())
850 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
851 if (aColorAttr.get() != NULL) {
852 if (!aColorAttr->size()) {
853 aColorAttr->setSize(3);
855 aColorAttr->setValue(0, theColor[0]);
856 aColorAttr->setValue(1, theColor[1]);
857 aColorAttr->setValue(2, theColor[2]);
861 void getColor(const std::shared_ptr<ModelAPI_Result>& theResult, std::vector<int>& theColor)
864 // get color from the attribute of the result
865 if (theResult.get() != NULL &&
866 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
867 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
868 if (aColorAttr.get() && aColorAttr->size()) {
869 theColor.push_back(aColorAttr->value(0));
870 theColor.push_back(aColorAttr->value(1));
871 theColor.push_back(aColorAttr->value(2));
876 //******************************************************
877 void getIsoLines(const std::shared_ptr<ModelAPI_Result>& theResult,
878 bool& isVisible, std::vector<int>& theNbLines)
882 if (!theResult.get())
884 if (theResult->groupName() == ModelAPI_ResultConstruction::group()) {
885 theNbLines.push_back(0);
886 theNbLines.push_back(0);
889 // get color from the attribute of the result
890 AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
893 theNbLines.push_back(aAttr->value(0));
894 theNbLines.push_back(aAttr->value(1));
897 AttributeBooleanPtr aBoolAttr =
898 theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
899 if (aBoolAttr.get()) {
900 isVisible = aBoolAttr->value();
905 //******************************************************
906 void setIsoLines(ResultPtr theResult, const std::vector<int>& theIso)
908 if (!theResult.get())
911 AttributeIntArrayPtr aAttr = theResult->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
912 if (aAttr.get() != NULL) {
913 if (!aAttr->size()) {
916 aAttr->setValue(0, theIso[0]);
917 aAttr->setValue(1, theIso[1]);
921 //******************************************************
922 void showIsoLines(std::shared_ptr<ModelAPI_Result> theResult, bool theShow)
924 if (!theResult.get())
927 AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
928 if (aAttr.get() != NULL) {
929 aAttr->setValue(theShow);
933 //******************************************************
934 bool isShownIsoLines(std::shared_ptr<ModelAPI_Result> theResult)
936 if (!theResult.get())
939 AttributeBooleanPtr aAttr = theResult->data()->boolean(ModelAPI_Result::SHOW_ISO_LINES_ID());
940 if (aAttr.get() != NULL) {
941 return aAttr->value();
946 //**************************************************************
947 void setTransparency(ResultPtr theResult, double theTransparency)
949 if (!theResult.get())
952 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
953 if (anAttribute.get() != NULL) {
954 anAttribute->setValue(theTransparency);
958 double getTransparency(const std::shared_ptr<ModelAPI_Result>& theResult)
960 double aTransparency = -1;
961 // get transparency from the attribute of the result
962 if (theResult.get() != NULL &&
963 theResult->data()->attribute(ModelAPI_Result::TRANSPARENCY_ID()).get() != NULL) {
964 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
965 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
966 aTransparency = aDoubleAttr->value();
969 return aTransparency;
972 void copyVisualizationAttrs(
973 std::shared_ptr<ModelAPI_Result> theSource, std::shared_ptr<ModelAPI_Result> theDest)
976 AttributeIntArrayPtr aSourceColor = theSource->data()->intArray(ModelAPI_Result::COLOR_ID());
977 if (aSourceColor.get() && aSourceColor->isInitialized() && aSourceColor->size()) {
978 AttributeIntArrayPtr aDestColor = theDest->data()->intArray(ModelAPI_Result::COLOR_ID());
979 if (aDestColor.get()) {
980 aDestColor->setSize(aSourceColor->size());
981 for(int a = 0; a < aSourceColor->size(); a++)
982 aDestColor->setValue(a, aSourceColor->value(a));
986 AttributeIntArrayPtr aSource = theSource->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
987 if (aSource.get() && aSource->isInitialized() && aSource->size()) {
988 AttributeIntArrayPtr aDest = theDest->data()->intArray(ModelAPI_Result::ISO_LINES_ID());
990 aDest->setSize(aSource->size());
991 for(int a = 0; a < aSource->size(); a++)
992 aDest->setValue(a, aSource->value(a));
996 AttributeDoublePtr aSourceDefl = theSource->data()->real(ModelAPI_Result::DEFLECTION_ID());
997 if (aSourceDefl.get() && aSourceDefl->isInitialized()) {
998 AttributeDoublePtr aDestDefl = theDest->data()->real(ModelAPI_Result::DEFLECTION_ID());
999 if (aDestDefl.get()) {
1000 aDestDefl->setValue(aSourceDefl->value());
1004 AttributeDoublePtr aSourceTransp = theSource->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1005 if (aSourceTransp.get() && aSourceTransp->isInitialized()) {
1006 AttributeDoublePtr aDestTransp = theDest->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
1007 if (aDestTransp.get()) {
1008 aDestTransp->setValue(aSourceTransp->value());
1013 std::list<FeaturePtr> referencedFeatures(
1014 std::shared_ptr<ModelAPI_Result> theTarget, const std::string& theFeatureKind,
1015 const bool theSortResults)
1017 std::set<FeaturePtr> aResSet; // collect in the set initially to avoid duplicates
1018 std::list<ResultPtr> allSubRes;
1019 allSubRes.push_back(theTarget);
1020 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theTarget);
1022 allSubs(aBody, allSubRes);
1023 std::list<ResultPtr>::iterator aSub = allSubRes.begin();
1024 for(; aSub != allSubRes.end(); aSub++) {
1025 const std::set<AttributePtr>& aRefs = (*aSub)->data()->refsToMe();
1026 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1027 for(; aRef != aRefs.cend(); aRef++) {
1028 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1029 if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1030 aResSet.insert(aFeat);
1033 // add also feature of the target that may be referenced as a whole
1034 FeaturePtr aTargetFeature = theTarget->document()->feature(theTarget);
1035 const std::set<AttributePtr>& aRefs = aTargetFeature->data()->refsToMe();
1036 std::set<AttributePtr>::const_iterator aRef = aRefs.cbegin();
1037 for(; aRef != aRefs.cend(); aRef++) {
1038 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRef)->owner());
1039 if (aFeat.get() && (theFeatureKind.empty() || aFeat->getKind() == theFeatureKind))
1040 aResSet.insert(aFeat);
1042 // check also Group-operations that may refer to groups - add them for theFeatureKind "Group"
1043 if (theFeatureKind == "Group") {
1044 std::set<FeaturePtr> aGroupOperations;
1045 for(bool aNeedIterate = true; aNeedIterate; ) {
1046 std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1047 for(; aResIter != aResSet.end(); aResIter++) {
1048 std::list<ResultPtr>::const_iterator aGroupRes = (*aResIter)->results().cbegin();
1049 for(; aGroupRes != (*aResIter)->results().cend(); aGroupRes++) {
1050 const std::set<AttributePtr>& aGroupRefs = (*aGroupRes)->data()->refsToMe();
1051 std::set<AttributePtr>::const_iterator aRefIt = aGroupRefs.cbegin();
1052 for(; aRefIt != aGroupRefs.cend(); aRefIt++) {
1053 FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
1054 if (aFeat.get() && !aGroupOperations.count(aFeat) && !aFeat->results().empty() &&
1055 aFeat->firstResult()->groupName() == ModelAPI_ResultGroup::group()) {
1056 // iterate results of this group operation because it may be without theTarget shape
1057 GeomShapePtr aTargetShape = theTarget->shape();
1058 bool anIsIn = false;
1059 std::list<ResultPtr>::const_iterator anOpRes = aFeat->results().cbegin();
1060 for(; anOpRes != aFeat->results().cend() && !anIsIn; anOpRes++) {
1061 GeomShapePtr anOpShape = (*anOpRes)->shape();
1062 if (!anOpShape.get() || anOpShape->isNull())
1064 for(GeomAPI_ShapeIterator aSubIt(anOpShape); aSubIt.more(); aSubIt.next()) {
1065 if (aTargetShape->isSubShape(aSubIt.current(), false)) {
1072 aGroupOperations.insert(aFeat);
1077 // insert all new group operations into result and if they are, check for next dependencies
1078 aNeedIterate = false;
1079 std::set<FeaturePtr>::iterator aGroupOpIter = aGroupOperations.begin();
1080 for(; aGroupOpIter != aGroupOperations.end(); aGroupOpIter++) {
1081 if (aResSet.find(*aGroupOpIter) == aResSet.end()) {
1082 aResSet.insert(*aGroupOpIter);
1083 aNeedIterate = true;
1089 std::list<FeaturePtr> aResList;
1090 std::set<FeaturePtr>::iterator aResIter = aResSet.begin();
1091 for(; aResIter != aResSet.end(); aResIter++) {
1092 if (theSortResults) { // sort results by creation-order
1093 std::list<FeaturePtr>::iterator aListIter = aResList.begin();
1094 for(; aListIter != aResList.end(); aListIter++) {
1095 if ((*aResIter)->document()->isLater(*aListIter, *aResIter))
1098 if (aListIter == aResList.end()) // goes to the end
1099 aResList.push_back(*aResIter);
1101 aResList.insert(aListIter, *aResIter);
1102 } else //just push to the end unsorted
1103 aResList.push_back(*aResIter);
1110 } // namespace ModelAPI_Tools