1 // Copyright (C) 2014-2019 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 <Model_Objects.h>
21 #include <Model_Data.h>
22 #include <Model_Document.h>
23 #include <Model_Events.h>
24 #include <Model_Session.h>
25 #include <Model_ResultPart.h>
26 #include <Model_ResultConstruction.h>
27 #include <Model_ResultBody.h>
28 #include <Model_ResultGroup.h>
29 #include <Model_ResultField.h>
30 #include <Model_ResultParameter.h>
31 #include <Model_AttributeRefList.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_CompositeFeature.h>
34 #include <ModelAPI_Tools.h>
35 #include <ModelAPI_Filter.h>
38 #include <Events_Loop.h>
39 #include <Events_InfoMessage.h>
41 #include <TDataStd_Integer.hxx>
42 #include <TDataStd_Comment.hxx>
43 #include <TDF_ChildIDIterator.hxx>
44 #include <TDataStd_ReferenceArray.hxx>
45 #include <TDataStd_HLabelArray1.hxx>
46 #include <TDF_Reference.hxx>
47 #include <TDF_ChildIDIterator.hxx>
48 #include <TDF_LabelMapHasher.hxx>
49 #include <TDF_LabelMap.hxx>
50 #include <TDF_ListIteratorOfLabelList.hxx>
52 int kUNDEFINED_FEATURE_INDEX = -1;
54 static const std::string& groupNameFoldering(const std::string& theGroupID,
55 const bool theAllowFolder)
58 static const std::string anOutOfFolderName = std::string("__") + ModelAPI_Feature::group();
59 static const std::string aDummyName;
60 return theGroupID == ModelAPI_Feature::group() ? anOutOfFolderName : aDummyName;
65 // Check theFeature is a first or last feature in folder and return this folder
66 static FolderPtr inFolder(const FeaturePtr& theFeature, const std::string& theFolderAttr)
68 const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
69 for (std::set<AttributePtr>::iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt) {
70 if ((*anIt)->id() != theFolderAttr)
73 ObjectPtr anOwner = (*anIt)->owner();
74 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(anOwner);
82 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
85 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
86 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
89 /// 0:1:2 - where features are located
90 /// 0:1:2:N:1 - data of the feature N
91 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
93 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
97 void Model_Objects::setOwner(DocumentPtr theDoc)
100 // update all fields and recreate features and result objects if needed
101 TDF_LabelList aNoUpdated;
102 synchronizeFeatures(aNoUpdated, true, false, true, true);
106 Model_Objects::~Model_Objects()
108 // delete all features of this document
109 Events_Loop* aLoop = Events_Loop::loop();
110 // erase one by one to avoid access from the feature destructor itself from he map
111 // blocks the flush signals to avoid the temporary objects visualization in the viewer
112 // they should not be shown in order to do not lose highlight by erasing them
113 bool isActive = aLoop->activateFlushes(false);
115 while(!myFeatures.IsEmpty()) {
116 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
117 FeaturePtr aFeature = aFeaturesIter.Value();
118 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
119 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
120 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
121 aFeature->removeResults(0, false);
123 myFeatures.UnBind(aFeaturesIter.Key());
125 while (!myFolders.IsEmpty()) {
126 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFoldersIter(myFolders);
127 ObjectPtr aFolder = aFoldersIter.Value();
128 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
129 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
130 ModelAPI_EventCreator::get()->sendUpdated(aFolder, EVENT_DISP);
132 myFolders.UnBind(aFoldersIter.Key());
135 aLoop->activateFlushes(isActive);
136 // erase update, because features are destroyed and update should not performed for them anywhere
137 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
138 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
139 // deleted and redisplayed is correctly performed: they know that features are destroyed
140 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
141 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
145 /// Appends to the array of references a new referenced label
146 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
148 Handle(TDataStd_ReferenceArray) aRefs;
149 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
150 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
151 aRefs->SetValue(0, theReferenced);
152 } else { // extend array by one more element
153 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
155 int aPassedPrev = 0; // previous feature is found and passed
156 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
157 aNewArray->SetValue(aRefs->Lower(), theReferenced);
160 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
161 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
162 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
164 aNewArray->SetValue(a + 1, theReferenced);
167 if (!aPassedPrev) // not found: unknown situation
168 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
169 aRefs->SetInternalArray(aNewArray);
173 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
175 if (!theFeature->isAction()) { // do not add action to the data model
176 TDF_Label aFeaturesLab = featuresLabel();
177 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
178 // store feature in the features array: before "initData" because in macro features
179 // in initData it creates new features, appeared later than this
180 TDF_Label aPrevFeateureLab;
181 FolderPtr aParentFolder;
182 if (theAfterThis.get()) { // searching for the previous feature label
183 std::shared_ptr<Model_Data> aPrevData =
184 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
185 if (aPrevData.get()) {
186 aPrevFeateureLab = aPrevData->label().Father();
188 // Check if the previous feature is the last feature in a folder,
189 // then the folder should be updated to contain additional feature.
190 // Macro features are not stored in folder.
191 if (!theFeature->isMacro()) {
192 // If the last feature is a sub-feature of composite, use parent feature
193 // to check belonging to a folder.
194 FeaturePtr afterThis = ModelAPI_Tools::compositeOwner(theAfterThis);
196 afterThis = theAfterThis;
197 aParentFolder = inFolder(afterThis, ModelAPI_Folder::LAST_FEATURE_ID());
200 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
202 // keep the feature ID to restore document later correctly
203 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
204 myFeatures.Bind(aFeatureLab, theFeature);
205 // must be before the event sending: for OB the feature is already added
206 updateHistory(ModelAPI_Feature::group());
207 // do not change the order:
210 // during python script with fillet constraint feature data should be
211 // initialized before using it in GUI
213 // must be after binding to the map because of "Box" macro feature that
214 // creates other features in "initData"
215 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
216 // put feature to the end of folder if it is added while
217 // the history line is set to the last feature from the folder
219 aParentFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(theFeature);
220 updateHistory(ModelAPI_Folder::group());
222 // event: feature is added, mist be before "initData" to update OB correctly on Duplicate:
223 // first new part, then the content
224 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
225 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
226 } else { // make feature has not-null data anyway
227 theFeature->setData(Model_Data::invalidData());
228 theFeature->setDoc(myDoc);
232 /// Appends to the array of references a new referenced label.
233 /// If theIndex is not -1, removes element at this index, not theReferenced.
234 /// \returns the index of removed element
235 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
236 const int theIndex = -1)
238 int aResult = -1; // no returned
239 Handle(TDataStd_ReferenceArray) aRefs;
240 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
241 if (aRefs->Length() == 1) { // just erase an array
242 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
243 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
246 } else { // reduce the array
247 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
249 int aCount = aRefs->Lower();
250 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
251 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
255 aNewArray->SetValue(aCount, aRefs->Value(a));
258 aRefs->SetInternalArray(aNewArray);
264 void Model_Objects::refsToFeature(FeaturePtr theFeature,
265 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
267 // check the feature: it must have no depended objects on it
268 // the dependencies can be in the feature results
269 std::list<ResultPtr> aResults;
270 ModelAPI_Tools::allResults(theFeature, aResults);
271 std::list<ResultPtr>::const_iterator aResIter = aResults.cbegin();
272 for (; aResIter != aResults.cend(); aResIter++) {
273 ResultPtr aResult = (*aResIter);
274 std::shared_ptr<Model_Data> aData =
275 std::dynamic_pointer_cast<Model_Data>(aResult->data());
276 if (aData.get() != NULL) {
277 const std::set<AttributePtr>& aRefs = aData->refsToMe();
278 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
279 for (; aRefIt != aRefLast; aRefIt++) {
280 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
281 if (aFeature.get() != NULL)
282 theRefs.insert(aFeature);
286 // the dependencies can be in the feature itself
287 std::shared_ptr<Model_Data> aData =
288 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
289 if (aData.get() && !aData->refsToMe().empty()) {
290 const std::set<AttributePtr>& aRefs = aData->refsToMe();
291 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
292 for (; aRefIt != aRefLast; aRefIt++) {
293 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
294 if (aFeature.get() != NULL)
295 theRefs.insert(aFeature);
299 if (!theRefs.empty() && isSendError) {
300 Events_InfoMessage("Model_Objects",
301 "Feature '%1' is used and can not be deleted").arg(theFeature->data()->name()).send();
305 void Model_Objects::removeFeature(FeaturePtr theFeature)
307 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
308 if (aData.get() && aData->isValid()) {
309 // checking that the sub-element of composite feature is removed: if yes, inform the owner
310 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
311 refsToFeature(theFeature, aRefs, false);
312 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
313 for(; aRefIter != aRefs.end(); aRefIter++) {
314 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
315 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
316 if (aComposite.get() && aComposite->data()->isValid() && aComposite->isSub(theFeature)) {
317 aComposite->removeFeature(theFeature);
320 // remove feature from folder
321 removeFromFolder(std::list<FeaturePtr>(1, theFeature));
322 // this must be before erase since theFeature erasing removes all information about
323 // the feature results and groups of results
324 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
325 clearHistory(theFeature);
329 TDF_Label aFeatureLabel = aData->label().Father();
330 if (myFeatures.IsBound(aFeatureLabel))
331 myFeatures.UnBind(aFeatureLabel);
333 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
334 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
335 // erase all attributes under the label of feature
336 aFeatureLabel.ForgetAllAttributes();
337 // remove it from the references array
338 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
339 // event: feature is deleted
340 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
341 updateHistory(ModelAPI_Feature::group());
345 void Model_Objects::eraseAllFeatures()
347 static Events_ID kDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
348 static const ModelAPI_EventCreator* kCreator = ModelAPI_EventCreator::get();
349 // make all features invalid (like deleted)
350 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
351 for(; aFIter.More(); aFIter.Next()) {
352 FeaturePtr aFeature = aFIter.Value();
353 std::list<ResultPtr> aResList;
354 ModelAPI_Tools::allResults(aFeature, aResList);
355 std::list<ResultPtr>::iterator aRIter = aResList.begin();
356 for(; aRIter != aResList.end(); aRIter++) {
357 ResultPtr aRes = *aRIter;
358 if (aRes && aRes->data()->isValid()) {
359 kCreator->sendDeleted(myDoc, aRes->groupName());
360 kCreator->sendUpdated(aRes, kDispEvent);
361 aRes->setData(aRes->data()->invalidPtr());
365 kCreator->sendUpdated(aFeature, kDispEvent);
366 aFeature->setData(aFeature->data()->invalidPtr());
368 kCreator->sendDeleted(myDoc, ModelAPI_Feature::group());
369 myFeatures.Clear(); // just remove features without modification of DS
373 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
375 TDF_Label aFeaturesLab = featuresLabel();
376 Handle(TDataStd_ReferenceArray) aRefs;
377 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
379 TDF_Label anAfterLab, aMovedLab =
380 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
381 if (theAfterThis.get())
382 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
384 // check whether some folder refers to the moved feature by start or end: if yes, remove from it
385 removeFromFolder(std::list<FeaturePtr>(1, theMoved));
387 Handle(TDataStd_HLabelArray1) aNewArray =
388 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
389 int aPassedMovedFrom = 0; // the previous feature location is found and passed
390 int aPassedMovedTo = 0; // the feature is added and this location is passed
391 if (!theAfterThis.get()) { // null means that inserted feature must be the first
392 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
395 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
396 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
398 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
399 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
400 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
401 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
403 } else { // just copy one
404 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
405 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
408 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
409 if (!aPassedMovedFrom) {
410 static std::string aMovedFromError("The moved feature is not found");
411 Events_InfoMessage("Model_Objects", aMovedFromError).send();
413 static std::string aMovedToError("The 'after' feature for movement is not found");
414 Events_InfoMessage("Model_Objects", aMovedToError).send();
418 // store the new array
419 aRefs->SetInternalArray(aNewArray);
420 // update the feature and the history
421 clearHistory(theMoved);
422 // make sure all (selection) attributes of moved feature will be updated
423 static Events_ID kUpdateSelection = Events_Loop::loop()->eventByName(EVENT_UPDATE_SELECTION);
424 ModelAPI_EventCreator::get()->sendUpdated(theMoved, kUpdateSelection, false);
425 ModelAPI_EventCreator::get()->sendReordered(theMoved);
428 void Model_Objects::clearHistory(ObjectPtr theObj)
431 const std::string aGroup = theObj->groupName();
432 updateHistory(aGroup);
434 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
435 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
436 std::string aResultGroup = featureResultGroup(aFeature);
437 if (!aResultGroup.empty()) {
438 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
439 myHistory.find(aResultGroup);
440 if (aHIter != myHistory.end())
441 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
447 void Model_Objects::createHistory(const std::string& theGroupID)
449 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
450 if (aHIter == myHistory.end()) {
451 std::vector<ObjectPtr> aResult;
452 std::vector<ObjectPtr> aResultOutOfFolder;
453 FeaturePtr aLastFeatureInFolder;
454 // iterate the array of references and get feature by feature from the array
455 bool isFeature = theGroupID == ModelAPI_Feature::group();
456 bool isFolder = theGroupID == ModelAPI_Folder::group();
457 Handle(TDataStd_ReferenceArray) aRefs;
458 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
459 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
460 FeaturePtr aFeature = feature(aRefs->Value(a));
461 if (aFeature.get()) {
462 // if feature is in sub-component, remove it from history:
463 // it is in sub-tree of sub-component
464 bool isSub = ModelAPI_Tools::compositeOwner(aFeature).get() != NULL;
465 if (isFeature) { // here may be also disabled features
466 if (!isSub && aFeature->isInHistory()) {
467 aResult.push_back(aFeature);
468 // the feature is out of the folders
469 if (aLastFeatureInFolder.get() == NULL)
470 aResultOutOfFolder.push_back(aFeature);
472 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
473 // construction results of sub-features should not be in the tree
474 if (!isSub || theGroupID != ModelAPI_ResultConstruction::group()) {
475 // do not use reference to the list here since results can be changed by "isConcealed"
476 const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
477 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator
478 aRIter = aResults.begin();
479 for (; aRIter != aResults.cend(); aRIter++) {
480 ResultPtr aRes = *aRIter;
481 if (aRes->groupName() != theGroupID) break; // feature have only same group results
482 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
483 aResult.push_back(*aRIter);
489 // the feature closes the folder, so the next features will be treated as out-of-folder
490 if (aLastFeatureInFolder.get() && aLastFeatureInFolder == aFeature)
491 aLastFeatureInFolder = FeaturePtr();
494 // it may be a folder
495 const ObjectPtr& aFolder = folder(aRefs->Value(a));
497 // store folder information for the Features group only
498 if (isFeature || isFolder) {
499 aResult.push_back(aFolder);
501 aResultOutOfFolder.push_back(aFolder);
504 // get the last feature in the folder
505 AttributeReferencePtr aLastFeatAttr =
506 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
508 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
513 // to be sure that isConcealed did not update the history (issue 1089) during the iteration
514 if (myHistory.find(theGroupID) == myHistory.end()) {
515 myHistory[theGroupID] = aResult;
517 // store the features placed out of any folder
518 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroupID, true);
519 if (!anOutOfFolderGroupID.empty())
520 myHistory[anOutOfFolderGroupID] = aResultOutOfFolder;
525 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
527 clearHistory(theObject);
530 void Model_Objects::updateHistory(const std::string theGroup)
532 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
533 if (aHIter != myHistory.end()) {
534 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
536 // erase history for the group of objects placed out of any folder
537 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroup, true);
538 if (!anOutOfFolderGroupID.empty())
539 myHistory.erase(anOutOfFolderGroupID);
543 const ObjectPtr& Model_Objects::folder(TDF_Label theLabel) const
545 if (myFolders.IsBound(theLabel))
546 return myFolders.Find(theLabel);
547 static ObjectPtr anEmptyResult;
548 return anEmptyResult;
551 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
553 if (myFeatures.IsBound(theLabel))
554 return myFeatures.Find(theLabel);
555 return FeaturePtr(); // not found
558 ObjectPtr Model_Objects::object(TDF_Label theLabel)
560 // try feature by label
561 FeaturePtr aFeature = feature(theLabel);
563 return feature(theLabel);
564 TDF_Label aFeatureLabel = theLabel; // let's suppose it is result of this feature
565 TDF_LabelList aSubLabs; // sub - labels from higher level to lower level of result
566 while(!aFeature.get() && aFeatureLabel.Depth() > 1) {
567 aSubLabs.Prepend(aFeatureLabel);
568 aFeatureLabel = aFeatureLabel.Father().Father();
569 aFeature = feature(aFeatureLabel);
571 if (aFeature.get()) {
572 ResultPtr aCurrentResult;
573 // searching for results then sub-results label by label
574 for(TDF_ListIteratorOfLabelList aSubLab(aSubLabs); aSubLab.More(); aSubLab.Next()) {
575 if (aCurrentResult.get()) { // iterate sub-results of result
576 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCurrentResult);
578 return ObjectPtr(); // only Body can have sub-results
579 int a, aNumSubs = anOwner->numberOfSubs();
580 for(a = 0; a < aNumSubs; a++) {
581 ResultPtr aSub = anOwner->subResult(a);
583 std::shared_ptr<Model_Data> aSubData = std::dynamic_pointer_cast<Model_Data>(
585 const TDF_Label& aSubLabVal = aSubLab.ChangeValue();
586 if (aSubData->label().Father().IsEqual(aSubLabVal)) {
587 aCurrentResult = aSub;
592 if (a == aNumSubs) // not found an appropriate sub-result of result
594 } else { // iterate results of feature
595 const std::list<ResultPtr>& aResults = aFeature->results();
596 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
597 for(; aRIter != aResults.cend(); aRIter++) {
598 std::shared_ptr<Model_Data> aResData =
599 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
600 if (aResData->label().Father().IsEqual(aSubLab.ChangeValue())) {
601 aCurrentResult = *aRIter;
605 if (aRIter == aResults.cend()) // not found an appropriate result of feature
609 return aCurrentResult;
611 return ObjectPtr(); // not found
614 ObjectPtr Model_Objects::object(const std::string& theGroupID,
616 const bool theAllowFolder)
620 createHistory(theGroupID);
621 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
622 const std::vector<ObjectPtr>& aVec = myHistory[theGroupID];
623 //if (aVec.size() <= theIndex)
624 // return aVec[aVec.size() - 1]; // too high index requested (to avoid crash in #2360)
625 return aGroupID.empty() ? myHistory[theGroupID][theIndex] : myHistory[aGroupID][theIndex];
628 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
629 const std::string& theGroupID, const std::string& theName)
631 createHistory(theGroupID);
632 if (theGroupID == ModelAPI_Feature::group()) { // searching among features (in history or not)
633 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
634 // from the end to find the latest result with such name
635 std::list<std::shared_ptr<ModelAPI_Feature> >::reverse_iterator anObjIter = allObjs.rbegin();
636 for(; anObjIter != allObjs.rend(); anObjIter++) {
637 if ((*anObjIter)->data()->name() == theName)
640 } else { // searching among results (concealed or not)
641 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
642 // from the end to find the latest result with such name
643 std::list<std::shared_ptr<ModelAPI_Feature> >::reverse_iterator anObjIter = allObjs.rbegin();
644 for(; anObjIter != allObjs.rend(); anObjIter++) {
645 std::list<ResultPtr> allRes;
646 ModelAPI_Tools::allResults(*anObjIter, allRes);
647 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
648 if (aRes->get() && (*aRes)->groupName() == theGroupID) {
649 if ((*aRes)->data()->name() == theName)
659 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject,
660 const bool theAllowFolder)
662 std::string aGroup = theObject->groupName();
663 // treat folder as feature
664 if (aGroup == ModelAPI_Folder::group())
665 aGroup = ModelAPI_Feature::group();
666 createHistory(aGroup);
668 // get the group of features out of folder (if enabled)
669 if (theAllowFolder && !groupNameFoldering(aGroup, theAllowFolder).empty())
670 aGroup = groupNameFoldering(aGroup, theAllowFolder);
672 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
673 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
674 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
675 if ((*anObjIter) == theObject)
682 int Model_Objects::size(const std::string& theGroupID, const bool theAllowFolder)
684 createHistory(theGroupID);
685 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
686 return aGroupID.empty() ? int(myHistory[theGroupID].size()) : int(myHistory[aGroupID].size());
689 std::shared_ptr<ModelAPI_Object> Model_Objects::parent(
690 const std::shared_ptr<ModelAPI_Object> theChild)
692 if (theChild.get()) {
693 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theChild->data());
694 TDF_Label aLab = aData->label();
695 if (!aLab.IsNull() && aLab.Depth() > 2) {
696 ObjectPtr anObj = object(aLab.Father().Father().Father());
704 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
706 // iterate the array of references and get feature by feature from the array
707 Handle(TDataStd_ReferenceArray) aRefs;
708 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
709 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
710 FeaturePtr aFeature = feature(aRefs->Value(a));
711 if (aFeature.get()) {
712 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
713 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
714 for (; aRIter != aResults.cend(); aRIter++) {
715 ResultPtr aRes = *aRIter;
716 if (aRes->groupName() != theGroupID) break; // feature have only same group results
717 // iterate also concealed: ALL RESULTS (for translation parts undo/redo management)
718 //if (aRes->isInHistory() && !aRes->isConcealed()) {
719 theResults.push_back(*aRIter);
728 TDF_Label Model_Objects::featuresLabel() const
730 return myMain.FindChild(TAG_OBJECTS);
733 static std::string composeName(const std::string& theFeatureKind, const int theIndex)
735 std::stringstream aNameStream;
736 aNameStream << theFeatureKind << "_" << theIndex;
737 return aNameStream.str();
740 void Model_Objects::setUniqueName(FeaturePtr theFeature)
742 if (!theFeature->data()->name().empty())
743 return; // not needed, name is already defined
744 std::string aName; // result
745 // first count all features of such kind to start with index = count + 1
746 int aNumObjects = -1; // this feature is already in this map
747 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
748 std::set<std::string> allNames;
749 for (; aFIter.More(); aFIter.Next()) {
750 if (aFIter.Value()->getKind() == theFeature->getKind())
752 allNames.insert(aFIter.Value()->data()->name());
754 // generate candidate name
755 aName = composeName(theFeature->getKind(), aNumObjects + 1);
756 // check this is unique, if not, increase index by 1
757 for(aNumObjects++; allNames.find(aName) != allNames.end(); aNumObjects++) {
758 aName = composeName(theFeature->getKind(), aNumObjects + 1);
760 theFeature->data()->setName(aName);
763 void Model_Objects::setUniqueName(FolderPtr theFolder)
765 if (!theFolder->name().empty())
766 return; // name is already defined
768 int aNbFolders = myFolders.Size();
769 std::string aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
771 // check the uniqueness of the name
772 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator anIt(myFolders);
773 while (anIt.More()) {
774 if (anIt.Value()->data()->name() == aName) {
775 aName = composeName(ModelAPI_Folder::ID(), ++aNbFolders);
776 // reinitialize iterator to make sure a new name is unique
777 anIt.Initialize(myFolders);
782 theFolder->data()->setName(aName);
785 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
787 std::shared_ptr<Model_Data> aData(new Model_Data);
788 aData->setLabel(theLab.FindChild(theTag));
789 aData->setObject(theObj);
790 theObj->setDoc(myDoc);
791 theObj->setData(aData);
792 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
793 if (aFeature.get()) {
794 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
795 } else { // is it a folder?
796 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theObj);
798 setUniqueName(aFolder);
800 theObj->initAttributes();
803 void Model_Objects::synchronizeFeatures(
804 const TDF_LabelList& theUpdated, const bool theUpdateReferences,
805 const bool theExecuteFeatures, const bool theOpen, const bool theFlush)
807 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
808 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
810 // after all updates, sends a message that groups of features were created or updated
811 Events_Loop* aLoop = Events_Loop::loop();
812 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
813 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
814 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
815 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
816 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
817 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
818 bool isActive = aLoop->activateFlushes(false);
820 // collect all updated labels map
821 TDF_LabelMap anUpdatedMap;
822 TDF_ListIteratorOfLabelList anUpdatedIter(theUpdated);
823 for(; anUpdatedIter.More(); anUpdatedIter.Next()) {
824 TDF_Label& aFeatureLab = anUpdatedIter.Value();
825 while(aFeatureLab.Depth() > 3)
826 aFeatureLab = aFeatureLab.Father();
827 if (myFeatures.IsBound(aFeatureLab) || myFolders.IsBound(aFeatureLab))
828 anUpdatedMap.Add(aFeatureLab);
831 // update all objects by checking are they on labels or not
832 std::set<ObjectPtr> aNewFeatures, aKeptFeatures;
833 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
834 for (; aLabIter.More(); aLabIter.Next()) {
835 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
836 if (!myFeatures.IsBound(aFeatureLabel) && !myFolders.IsBound(aFeatureLabel)) {
837 // a new feature or folder is inserted
839 std::string aFeatureID = TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
840 aLabIter.Value())->Get()).ToCString();
841 bool isFolder = aFeatureID == ModelAPI_Folder::ID();
843 std::shared_ptr<Model_Session> aSession =
844 std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get());
847 ObjectPtr aFeature = isFolder ? ObjectPtr(new ModelAPI_Folder)
848 : ObjectPtr(aSession->createFeature(aFeatureID, anOwner));
849 if (!aFeature.get()) {
850 // something is wrong, most probably, the opened document has invalid structure
851 Events_InfoMessage("Model_Objects", "Invalid type of object in the document").send();
852 aLabIter.Value()->Label().ForgetAllAttributes();
856 // this must be before "setData" to redo the sketch line correctly
858 myFolders.Bind(aFeatureLabel, aFeature);
860 myFeatures.Bind(aFeatureLabel, std::dynamic_pointer_cast<ModelAPI_Feature>(aFeature));
861 aNewFeatures.insert(aFeature);
862 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
863 updateHistory(aFeature);
865 // event: model is updated
866 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
867 } else { // nothing is changed, both iterators are incremented
870 if (myFeatures.Find(aFeatureLabel, aFeature)) {
871 aKeptFeatures.insert(aFeature);
874 if (myFolders.Find(aFeatureLabel, anObject))
875 aKeptFeatures.insert(anObject);
877 if (anUpdatedMap.Contains(aFeatureLabel)) {
878 if (!theOpen) { // on abort/undo/redo reinitialize attributes if something is changed
879 FiltersFeaturePtr aFilter = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(anObject);
880 if (aFilter.get()) { // for filters attributes may be added/removed on undo/redo
881 std::dynamic_pointer_cast<Model_Data>(aFilter->data())->clearAttributes();
882 aFilter->initAttributes();
884 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
885 anObject->data()->attributes("");
886 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
887 for(; anAttr != anAttrs.end(); anAttr++)
889 // if feature contains results, re-init them too
890 if (aFeature.get()) {
891 std::list<ResultPtr> aResults;
892 ModelAPI_Tools::allResults(aFeature, aResults);
893 std::list<ResultPtr>::iterator aResIter = aResults.begin();
894 for(; aResIter != aResults.end(); aResIter++) {
895 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
896 (*aResIter)->data()->attributes("");
897 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
898 for(; anAttr != anAttrs.end(); anAttr++)
904 ModelAPI_EventCreator::get()->sendUpdated(anObject, anUpdateEvent);
905 if (aFeature && aFeature->getKind() == "Parameter") {
906 // if parameters are changed, update the results (issue 937)
907 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
908 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
909 for (; aRIter != aResults.cend(); aRIter++) {
910 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
911 if (aRes->data()->isValid() && !aRes->isDisabled()) {
912 ModelAPI_EventCreator::get()->sendUpdated(aRes, anUpdateEvent);
920 // check all features are checked: if not => it was removed
921 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
922 while (aFIter.More()) {
923 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
924 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
925 FeaturePtr aFeature = aFIter.Value();
926 // event: model is updated
927 //if (aFeature->isInHistory()) {
928 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
930 // results of this feature must be redisplayed (hided)
931 // redisplay also removed feature (used for sketch and AISObject)
932 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
933 updateHistory(aFeature);
936 // unbind after the "erase" call: on abort sketch
937 // is removes sub-objects that corrupts aFIter
938 myFeatures.UnBind(aFIter.Key());
939 // reinitialize iterator because unbind may corrupt the previous order in the map
940 aFIter.Initialize(myFeatures);
944 // verify folders are checked: if not => is was removed
945 for (NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFldIt(myFolders);
946 aFldIt.More(); aFldIt.Next()) {
947 ObjectPtr aCurObj = aFldIt.Value();
948 if (aKeptFeatures.find(aCurObj) == aKeptFeatures.end() &&
949 aNewFeatures.find(aCurObj) == aNewFeatures.end()) {
950 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
951 // results of this feature must be redisplayed (hided)
952 // redisplay also removed feature (used for sketch and AISObject)
953 ModelAPI_EventCreator::get()->sendUpdated(aCurObj, aRedispEvent);
954 updateHistory(aCurObj);
957 // unbind after the "erase" call: on abort sketch
958 // is removes sub-objects that corrupts aFIter
959 myFolders.UnBind(aFldIt.Key());
960 // reinitialize iterator because unbind may corrupt the previous order in the map
961 aFldIt.Initialize(myFolders);
965 if (theUpdateReferences) {
966 synchronizeBackRefs();
968 // update results of the features (after features created because
969 // they may be connected, like sketch and sub elements)
970 // After synchronization of back references because sketch
971 // must be set in sub-elements before "execute" by updateResults
972 std::set<FeaturePtr> aProcessed; // composites must be updated after their subs (issue 360)
973 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
974 for (; aLabIter2.More(); aLabIter2.Next()) {
975 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
976 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
977 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
978 updateResults(aFeature, aProcessed);
981 // the synchronize should be done after updateResults
982 // in order to correct back references of updated results
983 if (theUpdateReferences) {
984 synchronizeBackRefs();
986 if (!theUpdated.IsEmpty()) {
987 // this means there is no control what was modified => remove history cash
991 if (!theExecuteFeatures)
992 anOwner->setExecuteFeatures(false);
993 aLoop->activateFlushes(isActive);
996 aLoop->flush(aDeleteEvent);
997 // delete should be emitted before create to reacts to aborted feature
998 aLoop->flush(aCreateEvent);
999 aLoop->flush(anUpdateEvent);
1000 aLoop->flush(aCreateEvent); // after update of features, there could be results created
1001 aLoop->flush(aDeleteEvent); // or deleted
1002 aLoop->flush(aRedispEvent);
1003 aLoop->flush(aToHideEvent);
1005 if (!theExecuteFeatures)
1006 anOwner->setExecuteFeatures(true);
1009 /// synchronizes back references for the given object basing on the collected data
1010 void Model_Objects::synchronizeBackRefsForObject(const std::set<AttributePtr>& theNewRefs,
1011 ObjectPtr theObject)
1013 if (!theObject.get() || !theObject->data()->isValid())
1015 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
1016 // iterate new list to compare with current
1017 std::set<AttributePtr>::iterator aNewIter = theNewRefs.begin();
1018 for(; aNewIter != theNewRefs.end(); aNewIter++) {
1019 // for the Model_AttributeRefList erase cash (issue #2819)
1020 std::shared_ptr<Model_AttributeRefList> aRefList =
1021 std::dynamic_pointer_cast<Model_AttributeRefList>(*aNewIter);
1023 aRefList->eraseHash();
1025 if (aData->refsToMe().find(*aNewIter) == aData->refsToMe().end()) {
1026 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aNewIter)->owner());
1028 aData->addBackReference(aRefFeat, (*aNewIter)->id());
1029 else // add back reference to a folder
1030 aData->addBackReference((*aNewIter)->owner(), (*aNewIter)->id());
1033 if (theNewRefs.size() != aData->refsToMe().size()) { // some back ref must be removed
1034 std::set<AttributePtr>::iterator aCurrentIter = aData->refsToMe().begin();
1035 while(aCurrentIter != aData->refsToMe().end()) {
1036 if (theNewRefs.find(*aCurrentIter) == theNewRefs.end()) {
1037 // for external references from other documents this system
1038 // is not working: refs are collected from
1039 // different Model_Objects, so before remove check this
1040 // external object exists and still referenced
1041 bool aLeaveIt = false;
1042 if ((*aCurrentIter)->owner().get() && (*aCurrentIter)->owner()->document() != myDoc &&
1043 (*aCurrentIter)->owner()->data().get() && (*aCurrentIter)->owner()->data()->isValid()) {
1044 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
1045 (*aCurrentIter)->owner()->data()->referencesToObjects(aRefs);
1046 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> >>>::iterator
1047 aRefIter = aRefs.begin();
1048 for(; aRefIter != aRefs.end(); aRefIter++) {
1049 if ((*aCurrentIter)->id() == aRefIter->first) {
1050 std::list<std::shared_ptr<ModelAPI_Object> >::iterator anOIt;
1051 for(anOIt = aRefIter->second.begin(); anOIt != aRefIter->second.end(); anOIt++) {
1052 if (*anOIt == theObject) {
1060 aData->removeBackReference(*aCurrentIter);
1061 aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
1062 } else aCurrentIter++;
1063 } else aCurrentIter++;
1066 // for the last feature in the folder, check if it is a sub-feature,
1067 // then refer the folder to a top-level parent composite feature
1068 const std::set<AttributePtr>& aRefs = aData->refsToMe();
1069 std::set<AttributePtr>::iterator anIt = aRefs.begin();
1070 for (; anIt != aRefs.end(); ++anIt)
1071 if ((*anIt)->id() == ModelAPI_Folder::LAST_FEATURE_ID())
1073 if (anIt != aRefs.end()) {
1074 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1076 CompositeFeaturePtr aParent;
1077 CompositeFeaturePtr aGrandParent = ModelAPI_Tools::compositeOwner(aFeature);
1079 aParent = aGrandParent;
1081 aGrandParent = ModelAPI_Tools::compositeOwner(aParent);
1082 } while (aGrandParent.get());
1084 ObjectPtr aFolder = (*anIt)->owner();
1085 // remove reference from the current feature
1086 aData->removeBackReference(aFolder, ModelAPI_Folder::LAST_FEATURE_ID());
1087 // set reference to a top-level parent
1088 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aParent);
1089 std::shared_ptr<Model_Data> aParentData =
1090 std::dynamic_pointer_cast<Model_Data>(aParent->data());
1091 aParentData->addBackReference(aFolder, ModelAPI_Folder::LAST_FEATURE_ID());
1095 aData->updateConcealmentFlag();
1098 static void collectReferences(std::shared_ptr<ModelAPI_Data> theData,
1099 std::map<ObjectPtr, std::set<AttributePtr> >& theRefs)
1101 if (theData.get()) {
1102 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1103 theData->referencesToObjects(aRefs);
1104 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIt = aRefs.begin();
1105 for(; aRefsIt != aRefs.end(); aRefsIt++) {
1106 std::list<ObjectPtr>::iterator aRefTo = aRefsIt->second.begin();
1107 for(; aRefTo != aRefsIt->second.end(); aRefTo++) {
1109 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = theRefs.find(*aRefTo);
1110 if (aFound == theRefs.end()) {
1111 theRefs[*aRefTo] = std::set<AttributePtr>();
1112 aFound = theRefs.find(*aRefTo);
1114 aFound->second.insert(theData->attribute(aRefsIt->first));
1121 void Model_Objects::synchronizeBackRefs()
1123 // collect all back references in the separated container: to update everything at once,
1124 // without additional Concealment switching on and off: only the final modification
1126 // referenced (slave) objects to referencing attributes
1127 std::map<ObjectPtr, std::set<AttributePtr> > allRefs;
1128 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
1129 for(; aFeatures.More(); aFeatures.Next()) {
1130 FeaturePtr aFeature = aFeatures.Value();
1131 collectReferences(aFeature->data(), allRefs);
1133 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFolders(myFolders);
1134 for(; aFolders.More(); aFolders.Next()) {
1135 ObjectPtr aFolder = aFolders.Value();
1136 collectReferences(aFolder->data(), allRefs);
1138 // second iteration: just compare back-references with existing in features and results
1139 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1140 FeaturePtr aFeature = aFeatures.Value();
1141 static std::set<AttributePtr> anEmpty;
1142 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(aFeature);
1143 if (aFound == allRefs.end()) { // not found => erase all back references
1144 synchronizeBackRefsForObject(anEmpty, aFeature);
1146 synchronizeBackRefsForObject(aFound->second, aFeature);
1147 allRefs.erase(aFound); // to check that all refs are counted
1150 std::list<ResultPtr> aResults;
1151 ModelAPI_Tools::allResults(aFeature, aResults);
1152 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1153 for(; aRIter != aResults.cend(); aRIter++) {
1154 aFound = allRefs.find(*aRIter);
1155 if (aFound == allRefs.end()) { // not found => erase all back references
1156 synchronizeBackRefsForObject(anEmpty, *aRIter);
1158 synchronizeBackRefsForObject(aFound->second, *aRIter);
1159 allRefs.erase(aFound); // to check that all refs are counted
1163 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1164 FeaturePtr aFeature = aFeatures.Value();
1165 std::list<ResultPtr> aResults;
1166 ModelAPI_Tools::allResults(aFeature, aResults);
1167 // update the concealment status for display in isConcealed of ResultBody
1168 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1169 for(; aRIter != aResults.cend(); aRIter++) {
1170 (*aRIter)->isConcealed();
1173 // the rest all refs means that feature references to the external document feature:
1174 // process also them
1175 std::map<ObjectPtr, std::set<AttributePtr> >::iterator anExtIter = allRefs.begin();
1176 for(; anExtIter != allRefs.end(); anExtIter++) {
1177 synchronizeBackRefsForObject(anExtIter->second, anExtIter->first);
1181 TDF_Label Model_Objects::resultLabel(
1182 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1184 const std::shared_ptr<Model_Data>& aData =
1185 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1186 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1189 bool Model_Objects::hasCustomName(DataPtr theFeatureData,
1190 ResultPtr theResult,
1192 std::string& theParentName) const
1194 ResultBodyPtr aBodyRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theFeatureData->owner());
1196 // only for top-results (works for the cases when results are not yet added to the feature)
1197 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult);
1199 // names of sub-solids in CompSolid should be default (for example,
1200 // result of boolean operation 'Boolean_1' is a CompSolid which is renamed to 'MyBOOL',
1201 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1', 'Boolean_1_2' etc.)
1202 if (std::dynamic_pointer_cast<Model_Data>(aBodyRes->data())->label().Depth() == 6) {
1203 std::ostringstream aDefaultName;
1204 // compute default name of CompSolid (name of feature + index of CompSolid's result)
1205 int aBodyResultIndex = 0;
1206 const std::list<ResultPtr>& aResults = anOwner->results();
1207 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
1208 for (; anIt != aResults.end(); ++anIt, ++aBodyResultIndex)
1209 if (aBodyRes == *anIt)
1211 aDefaultName << anOwner->name();
1212 aDefaultName << "_" << (aBodyResultIndex + 1);
1213 theParentName = aDefaultName.str();
1214 } else { // just name of the parent result if it is deeper than just a sub-result
1215 theParentName = aBodyRes->data()->name();
1220 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(theResult);
1222 theParentName = aName.first;
1223 return aName.second;
1226 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1227 std::shared_ptr<ModelAPI_Result> theResult,
1228 const int theResultIndex)
1231 theResult->setDoc(myDoc);
1232 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1233 if (theResult->data()->name().empty()) {
1234 // if was not initialized, generate event and set a name
1235 std::string aNewName = theFeatureData->name();
1236 if (hasCustomName(theFeatureData, theResult, theResultIndex, aNewName)) {
1237 // if the name of result is user-defined, then, at first time, assign name of the result
1238 // by empty string to be sure that corresponding flag in the data model is set
1239 theResult->data()->setName("");
1241 std::stringstream aName;
1243 // if there are several results (issue #899: any number of result),
1244 // add unique prefix starting from second
1245 if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
1246 aName << "_" << theResultIndex + 1;
1247 aNewName = aName.str();
1249 theResult->data()->setName(aNewName);
1253 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
1254 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1256 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1257 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1258 ObjectPtr anOldObject = object(aLab);
1259 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1260 if (anOldObject.get()) {
1261 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1263 if (!aResult.get()) {
1264 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1265 storeResult(theFeatureData, aResult, theIndex);
1270 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
1271 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1273 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1274 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1275 ObjectPtr anOldObject = object(aLab);
1276 std::shared_ptr<ModelAPI_ResultBody> aResult;
1277 if (anOldObject.get()) {
1278 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1280 if (!aResult.get()) {
1281 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1282 storeResult(theFeatureData, aResult, theIndex);
1287 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
1288 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1290 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1291 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1292 ObjectPtr anOldObject = object(aLab);
1293 std::shared_ptr<ModelAPI_ResultPart> aResult;
1294 if (anOldObject.get()) {
1295 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1297 if (!aResult.get()) {
1298 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1299 storeResult(theFeatureData, aResult, theIndex);
1304 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
1305 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
1306 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1308 std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
1309 aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
1313 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
1314 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1316 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1317 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1318 ObjectPtr anOldObject = object(aLab);
1319 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1320 if (anOldObject.get()) {
1321 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1323 if (!aResult.get()) {
1324 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1325 storeResult(theFeatureData, aResult, theIndex);
1330 std::shared_ptr<ModelAPI_ResultField> Model_Objects::createField(
1331 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1333 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1334 TDataStd_Comment::Set(aLab, ModelAPI_ResultField::group().c_str());
1335 ObjectPtr anOldObject = object(aLab);
1336 std::shared_ptr<ModelAPI_ResultField> aResult;
1337 if (anOldObject.get()) {
1338 aResult = std::dynamic_pointer_cast<ModelAPI_ResultField>(anOldObject);
1340 if (!aResult.get()) {
1341 aResult = std::shared_ptr<ModelAPI_ResultField>(new Model_ResultField(theFeatureData));
1342 storeResult(theFeatureData, aResult, theIndex);
1347 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
1348 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1350 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1351 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1352 ObjectPtr anOldObject = object(aLab);
1353 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1354 if (anOldObject.get()) {
1355 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1357 if (!aResult.get()) {
1358 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1359 storeResult(theFeatureData, aResult, theIndex);
1364 std::shared_ptr<ModelAPI_Folder> Model_Objects::createFolder(
1365 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis)
1367 FolderPtr aFolder(new ModelAPI_Folder);
1371 TDF_Label aFeaturesLab = featuresLabel();
1372 TDF_Label aFolderLab = aFeaturesLab.NewChild();
1373 // store feature in the features array: before "initData" because in macro features
1374 // in initData it creates new features, appeared later than this
1375 TDF_Label aPrevFeatureLab;
1376 if (theBeforeThis.get()) { // searching for the previous feature label
1377 std::shared_ptr<Model_Data> aPrevData =
1378 std::dynamic_pointer_cast<Model_Data>(theBeforeThis->data());
1379 if (aPrevData.get()) {
1380 int anIndex = kUNDEFINED_FEATURE_INDEX;
1381 aPrevFeatureLab = nextLabel(aPrevData->label().Father(), anIndex, true);
1383 } else { // find the label of the last feature
1384 Handle(TDataStd_ReferenceArray) aRefs;
1385 if (aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1386 aPrevFeatureLab = aRefs->Value(aRefs->Upper());
1388 AddToRefArray(aFeaturesLab, aFolderLab, aPrevFeatureLab);
1390 // keep the feature ID to restore document later correctly
1391 TDataStd_Comment::Set(aFolderLab, ModelAPI_Folder::ID().c_str());
1392 myFolders.Bind(aFolderLab, aFolder);
1393 // must be before the event sending: for OB the feature is already added
1394 updateHistory(ModelAPI_Folder::group());
1395 updateHistory(ModelAPI_Feature::group());
1397 // must be after binding to the map because of "Box" macro feature that
1398 // creates other features in "initData"
1399 initData(aFolder, aFolderLab, TAG_FEATURE_ARGUMENTS);
1400 // event: folder is added, must be before "initData" to update OB correctly on Duplicate:
1401 // first new part, then the content
1402 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1403 ModelAPI_EventCreator::get()->sendUpdated(aFolder, anEvent);
1408 void Model_Objects::removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder)
1410 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFolder->data());
1411 if (!aData.get() || !aData->isValid())
1414 // this must be before erase since theFolder erasing removes all information about it
1415 clearHistory(theFolder);
1419 TDF_Label aFolderLabel = aData->label().Father();
1420 if (myFolders.IsBound(aFolderLabel))
1421 myFolders.UnBind(aFolderLabel);
1423 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1424 ModelAPI_EventCreator::get()->sendUpdated(theFolder, EVENT_DISP);
1425 // erase all attributes under the label of feature
1426 aFolderLabel.ForgetAllAttributes();
1427 // remove it from the references array
1428 RemoveFromRefArray(featuresLabel(), aFolderLabel);
1429 // event: feature is deleted
1430 ModelAPI_EventCreator::get()->sendDeleted(theFolder->document(), ModelAPI_Folder::group());
1431 updateHistory(ModelAPI_Folder::group());
1432 updateHistory(ModelAPI_Feature::group());
1435 // Returns one of the limiting features of the list
1436 static FeaturePtr limitingFeature(std::list<FeaturePtr>& theFeatures, const bool isLast)
1438 FeaturePtr aFeature;
1440 aFeature = theFeatures.back();
1441 theFeatures.pop_back();
1443 aFeature = theFeatures.front();
1444 theFeatures.pop_front();
1449 // Verify the feature is sub-element in composite feature or it is not used in the history
1450 static bool isSkippedFeature(FeaturePtr theFeature)
1452 bool isSub = ModelAPI_Tools::compositeOwner(theFeature).get() != NULL;
1453 return isSub || (theFeature && !theFeature->isInHistory());
1456 std::shared_ptr<ModelAPI_Folder> Model_Objects::findFolder(
1457 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1458 const bool theBelow)
1460 if (theFeatures.empty())
1461 return FolderPtr(); // nothing to move
1463 TDF_Label aFeaturesLab = featuresLabel();
1464 Handle(TDataStd_ReferenceArray) aRefs;
1465 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1466 return FolderPtr(); // no reference array (something is wrong)
1468 std::list<std::shared_ptr<ModelAPI_Feature> > aFeatures = theFeatures;
1469 std::shared_ptr<ModelAPI_Feature> aLimitingFeature = limitingFeature(aFeatures, theBelow);
1471 std::shared_ptr<Model_Data> aData =
1472 std::static_pointer_cast<Model_Data>(aLimitingFeature->data());
1473 if (!aData || !aData->isValid())
1474 return FolderPtr(); // invalid feature
1476 // label of the first feature in the list for fast searching
1477 TDF_Label aFirstFeatureLabel = aData->label().Father();
1479 // find a folder above the features and
1480 // check the given features represent a sequential list of objects following the folder
1481 FolderPtr aFoundFolder;
1482 TDF_Label aLastFeatureInFolder;
1483 int aRefIndex = aRefs->Lower();
1484 for(; aRefIndex <= aRefs->Upper(); ++aRefIndex) { // iterate all existing features
1485 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1486 if (IsEqual(aCurLabel, aFirstFeatureLabel))
1487 break; // no need to continue searching
1489 // searching the folder below, just continue to search last feature from the list
1493 // if feature is in sub-component, skip it
1494 FeaturePtr aCurFeature = feature(aCurLabel);
1495 if (isSkippedFeature(aCurFeature))
1498 if (!aLastFeatureInFolder.IsNull()) {
1499 if (IsEqual(aCurLabel, aLastFeatureInFolder))
1500 aLastFeatureInFolder.Nullify(); // the last feature in the folder is achieved
1504 const ObjectPtr& aFolderObj = folder(aCurLabel);
1505 if (aFolderObj.get()) {
1506 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aFolderObj);
1507 AttributeReferencePtr aLastFeatAttr =
1508 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1509 if (aLastFeatAttr) {
1510 // setup iterating inside a folder to find last feature
1511 ObjectPtr aLastFeature = aLastFeatAttr->value();
1513 aData = std::static_pointer_cast<Model_Data>(aLastFeature->data());
1514 if (aData && aData->isValid())
1515 aLastFeatureInFolder = aData->label().Father();
1521 if (theBelow && aRefIndex < aRefs->Upper()) {
1523 // skip following features which are sub-components or not in history
1524 for (int anIndex = aRefIndex + 1; anIndex <= aRefs->Upper(); ++anIndex) {
1525 aLabel = aRefs->Value(anIndex);
1526 FeaturePtr aCurFeature = feature(aLabel);
1527 if (!isSkippedFeature(aCurFeature))
1530 // check the next object is a folder
1531 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(folder(aLabel));
1534 if (!aLastFeatureInFolder.IsNull() || // the last feature of the folder above is not found
1538 // check the given features are sequential list
1539 int aStep = theBelow ? -1 : 1;
1540 for (aRefIndex += aStep;
1541 !aFeatures.empty() && aRefIndex >= aRefs->Lower() && aRefIndex <= aRefs->Upper();
1542 aRefIndex += aStep) {
1543 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1544 // if feature is in sub-component, skip it
1545 FeaturePtr aCurFeature = feature(aCurLabel);
1546 if (isSkippedFeature(aCurFeature))
1549 aLimitingFeature = limitingFeature(aFeatures, theBelow);
1550 if (!aCurFeature->data()->isEqual(aLimitingFeature->data()))
1551 return FolderPtr(); // not a sequential list
1554 return aFoundFolder;
1557 bool Model_Objects::moveToFolder(
1558 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1559 const std::shared_ptr<ModelAPI_Folder>& theFolder)
1561 if (theFeatures.empty() || !theFolder)
1564 // labels for the folder and last feature in the list
1565 TDF_Label aFolderLabel, aLastFeatureLabel;
1566 std::shared_ptr<Model_Data> aData =
1567 std::static_pointer_cast<Model_Data>(theFolder->data());
1568 if (aData && aData->isValid())
1569 aFolderLabel = aData->label().Father();
1570 aData = std::static_pointer_cast<Model_Data>(theFeatures.back()->data());
1571 if (aData && aData->isValid())
1572 aLastFeatureLabel = aData->label().Father();
1574 if (aFolderLabel.IsNull() || aLastFeatureLabel.IsNull())
1577 AttributeReferencePtr aFirstFeatAttr =
1578 theFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1579 AttributeReferencePtr aLastFeatAttr =
1580 theFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1581 bool initFirstAttr = !aFirstFeatAttr->value().get();
1582 bool initLastAttr = !aLastFeatAttr->value().get();
1584 // check the folder is below the list of features
1585 bool isFolderBelow = false;
1586 TDF_Label aFeaturesLab = featuresLabel();
1587 Handle(TDataStd_ReferenceArray) aRefs;
1588 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1589 return false; // no reference array (something is wrong)
1590 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1591 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1592 if (aCurLabel == aFolderLabel)
1593 break; // folder is above the features
1594 else if (aCurLabel == aLastFeatureLabel) {
1595 isFolderBelow = true;
1600 if (isFolderBelow) {
1601 aData = std::static_pointer_cast<Model_Data>(theFeatures.front()->data());
1602 if (!aData || !aData->isValid())
1604 TDF_Label aPrevFeatureLabel = aData->label().Father();
1605 // label of the feature before the first feature in the list
1606 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex)
1607 if (aPrevFeatureLabel == aRefs->Value(aRefIndex)) {
1608 if (aRefIndex == aRefs->Lower())
1609 aPrevFeatureLabel.Nullify();
1611 aPrevFeatureLabel = aRefs->Value(aRefIndex - 1);
1615 // move the folder in the list of references before the first feature
1616 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1617 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1618 // update first feature of the folder
1619 initFirstAttr = true;
1621 // update last feature of the folder
1622 initLastAttr = true;
1626 aFirstFeatAttr->setValue(theFeatures.front());
1628 aLastFeatAttr->setValue(theFeatures.back());
1630 updateHistory(ModelAPI_Feature::group());
1634 static FolderPtr isExtractionCorrect(const FolderPtr& theFirstFeatureFolder,
1635 const FolderPtr& theLastFeatureFolder,
1636 bool& isExtractBefore)
1638 if (theFirstFeatureFolder.get()) {
1639 if (theLastFeatureFolder.get())
1640 return theFirstFeatureFolder == theLastFeatureFolder ? theFirstFeatureFolder : FolderPtr();
1642 isExtractBefore = true;
1643 return theFirstFeatureFolder;
1644 } else if (theLastFeatureFolder.get()) {
1645 isExtractBefore = false;
1646 return theLastFeatureFolder;
1652 bool Model_Objects::removeFromFolder(
1653 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1654 const bool theBefore)
1656 if (theFeatures.empty())
1659 FolderPtr aFirstFeatureFolder =
1660 inFolder(theFeatures.front(), ModelAPI_Folder::FIRST_FEATURE_ID());
1661 FolderPtr aLastFeatureFolder =
1662 inFolder(theFeatures.back(), ModelAPI_Folder::LAST_FEATURE_ID());
1664 bool isExtractBeforeFolder = theBefore;
1665 FolderPtr aFoundFolder =
1666 isExtractionCorrect(aFirstFeatureFolder, aLastFeatureFolder, isExtractBeforeFolder);
1668 return false; // list of features cannot be extracted
1670 // references of the current folder
1671 ObjectPtr aFolderStartFeature;
1672 ObjectPtr aFolderEndFeature;
1673 if (aFirstFeatureFolder != aLastFeatureFolder) {
1674 aFolderStartFeature = aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->value();
1675 aFolderEndFeature = aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->value();
1678 FeaturePtr aFeatureToFind = isExtractBeforeFolder ? theFeatures.back() : theFeatures.front();
1679 std::shared_ptr<Model_Data> aData =
1680 std::static_pointer_cast<Model_Data>(aFeatureToFind->data());
1681 if (!aData || !aData->isValid())
1683 TDF_Label aLabelToFind = aData->label().Father();
1685 // search the label in the list of references
1686 TDF_Label aFeaturesLab = featuresLabel();
1687 Handle(TDataStd_ReferenceArray) aRefs;
1688 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1689 return false; // no reference array (something is wrong)
1690 int aRefIndex = aRefs->Lower();
1691 for (; aRefIndex <= aRefs->Upper(); ++aRefIndex)
1692 if (aRefs->Value(aRefIndex) == aLabelToFind)
1695 // update folder position
1696 if (isExtractBeforeFolder) {
1697 aData = std::dynamic_pointer_cast<Model_Data>(aFoundFolder->data());
1698 TDF_Label aFolderLabel = aData->label().Father();
1699 TDF_Label aPrevFeatureLabel = aRefs->Value(aRefIndex);
1700 // update start reference of the folder
1701 if (aFolderStartFeature.get()) {
1702 FeaturePtr aNewStartFeature;
1703 do { // skip all features placed in the composite features
1704 aPrevFeatureLabel = aRefs->Value(aRefIndex++);
1706 aRefIndex <= aRefs->Upper() ? feature(aRefs->Value(aRefIndex)) : FeaturePtr();
1707 } while (aNewStartFeature && isSkippedFeature(aNewStartFeature));
1708 aFolderStartFeature = aNewStartFeature;
1710 // move the folder in the list of references after the last feature from the list
1711 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1712 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1714 // update end reference of the folder
1715 if (aFolderEndFeature.get()) {
1716 FeaturePtr aNewEndFeature;
1717 do { // skip all features placed in the composite features
1720 aRefIndex >= aRefs->Lower() ? feature(aRefs->Value(aRefIndex)) : FeaturePtr();
1721 } while (aNewEndFeature && isSkippedFeature(aNewEndFeature));
1722 aFolderEndFeature = aNewEndFeature;
1726 // update folder references
1727 aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->setValue(aFolderStartFeature);
1728 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aFolderEndFeature);
1730 updateHistory(ModelAPI_Feature::group());
1734 FolderPtr Model_Objects::findContainingFolder(const FeaturePtr& theFeature, int& theIndexInFolder)
1736 // search the label in the list of references
1737 TDF_Label aFeaturesLab = featuresLabel();
1738 Handle(TDataStd_ReferenceArray) aRefs;
1739 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1740 return FolderPtr(); // no reference array (something is wrong)
1742 std::shared_ptr<Model_Data> aData =
1743 std::static_pointer_cast<Model_Data>(theFeature->data());
1744 if (!aData || !aData->isValid())
1746 TDF_Label aLabelToFind = aData->label().Father();
1748 theIndexInFolder = -1;
1749 FolderPtr aFoundFolder;
1750 TDF_Label aLastFeatureLabel;
1752 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1753 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1758 if (aCurLabel == aLabelToFind) { // the feature is reached
1760 if (isSkippedFeature(theFeature)) {
1761 theIndexInFolder = -1;
1764 // decrease the index of the feature in the folder by the number of skipped features
1765 for (int anIndex = theIndexInFolder - 1; anIndex > 0; anIndex--) {
1766 aCurLabel = aRefs->Value(aRefIndex - anIndex);
1767 if (isSkippedFeature(feature(aCurLabel)))
1771 return aFoundFolder;
1774 if (!aFoundFolder) {
1775 // if the current label refers to a folder, feel all necessary data
1776 const ObjectPtr& aFolderObj = folder(aCurLabel);
1777 if (aFolderObj.get()) {
1778 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aFolderObj);
1779 theIndexInFolder = -1;
1781 AttributeReferencePtr aLastRef =
1782 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1783 if (aLastRef->value()) {
1784 aData = std::static_pointer_cast<Model_Data>(aLastRef->value()->data());
1785 if (aData && aData->isValid())
1786 aLastFeatureLabel = aData->label().Father();
1787 } else // folder is empty
1788 aFoundFolder = FolderPtr();
1790 } else if (aLastFeatureLabel == aCurLabel) {
1791 // folder is finished, clear all stored data
1792 theIndexInFolder = -1;
1793 aFoundFolder = FolderPtr();
1797 // folder is not found
1798 theIndexInFolder = -1;
1803 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
1804 const std::shared_ptr<ModelAPI_Result>& theResult)
1806 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1807 if (aData.get() && aData->isValid()) {
1808 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1809 FeaturePtr aFeature = feature(aFeatureLab);
1810 while(!aFeature.get() && aFeatureLab.Depth() > 1) { // this may be sub-result of result
1811 aFeatureLab = aFeatureLab.Father().Father();
1812 aFeature = feature(aFeatureLab);
1816 return FeaturePtr();
1819 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
1821 if (theFeature->data()->isValid()) {
1822 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1823 if (aLabIter.More()) {
1824 TDF_Label anArgLab = aLabIter.Value();
1825 Handle(TDataStd_Comment) aGroup;
1826 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1827 return TCollection_AsciiString(aGroup->Get()).ToCString();
1831 static std::string anEmpty;
1832 return anEmpty; // not found
1835 void Model_Objects::updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
1837 if (theProcessed.find(theFeature) != theProcessed.end())
1839 theProcessed.insert(theFeature);
1840 // for composites update subs recursively (sketch elements results are needed for the sketch)
1841 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
1842 if (aComp.get() && aComp->getKind() != "Part") { // don't go inside of parts sub-features
1843 // update subs of composites first
1844 int aSubNum = aComp->numberOfSubs();
1845 for(int a = 0; a < aSubNum; a++) {
1846 FeaturePtr aSub = aComp->subFeature(a);
1847 updateResults(aComp->subFeature(a), theProcessed);
1851 // it may be on undo
1852 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
1855 // check the existing results and remove them if there is nothing on the label
1856 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1857 while(aResIter != theFeature->results().cend()) {
1858 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1860 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(aBody->data());
1861 if (!aData.get() || !aData->isValid() || (!aBody->isDisabled() && aData->isDeleted())) {
1862 // found a disappeared result => remove it
1863 theFeature->eraseResultFromList(aBody);
1864 // start iterate from beginning because iterator is corrupted by removing
1865 aResIter = theFeature->results().cbegin();
1871 // check that results are presented on all labels
1872 int aResSize = int(theFeature->results().size());
1873 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1874 for(; aLabIter.More(); aLabIter.Next()) {
1875 // here must be GUID of the feature
1876 int aResIndex = aLabIter.Value().Tag() - 1;
1878 if (aResSize <= aResIndex) {
1879 TDF_Label anArgLab = aLabIter.Value();
1880 Handle(TDataStd_Comment) aGroup;
1881 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1882 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1883 aNewBody = createBody(theFeature->data(), aResIndex);
1884 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1885 if (aResIndex <= theFeature->results().size()) { // to avoid crash if previous execute
1886 // for index = 0 erases result
1887 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
1888 theFeature->setResult(aNewP, aResIndex);
1889 if (!aNewP->partDoc().get())
1890 // create the part result: it is better to restore the previous result if possible
1891 theFeature->execute();
1893 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1894 ResultConstructionPtr aConstr = createConstruction(theFeature->data(), aResIndex);
1895 if (!aConstr->updateShape())
1896 theFeature->execute(); // not stored shape in the data structure, execute to have it
1898 theFeature->setResult(aConstr, aResIndex); // result is ready without execution
1899 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1900 aNewBody = createGroup(theFeature->data(), aResIndex);
1901 } else if (aGroup->Get() == ModelAPI_ResultField::group().c_str()) {
1902 ResultFieldPtr aField = createField(theFeature->data(), aResIndex);
1903 aField->updateSteps(); // to refresh the internal data
1905 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1906 theFeature->attributeChanged("expression"); // just produce a value
1908 Events_InfoMessage("Model_Objects", "Unknown type of result is found in the document:")
1909 .arg(TCollection_AsciiString(aGroup->Get()).ToCString()).send();
1912 if (aNewBody && !aNewBody->data()->isDeleted()) {
1913 theFeature->setResult(aNewBody, aResIndex);
1917 if (aResSize > 0) { // check there exist a body that must be updated
1918 std::list<ResultPtr>::const_iterator aRes = theFeature->results().cbegin();
1919 for (; aResSize && aRes != theFeature->results().cend(); aRes++, aResSize++) {
1920 if ((*aRes)->data()->isValid()) {
1921 if ((*aRes)->groupName() == ModelAPI_ResultBody::group()) {
1922 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRes);
1923 aBody->updateSubs(aBody->shape(), false);
1924 } else if ((*aRes)->groupName() == ModelAPI_ResultConstruction::group()) {
1925 // update the cashed myShape presented in construction
1926 ResultConstructionPtr aConstr =
1927 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
1928 aConstr->updateShape();
1935 ResultPtr Model_Objects::findByName(const std::string theName)
1938 FeaturePtr aResFeature; // keep feature to return the latest one
1939 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
1940 for(; anObjIter.More(); anObjIter.Next()) {
1941 FeaturePtr& aFeature = anObjIter.ChangeValue();
1942 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
1944 std::list<ResultPtr> allResults;
1945 ModelAPI_Tools::allResults(aFeature, allResults);
1946 std::list<ResultPtr>::iterator aRIter = allResults.begin();
1947 for (; aRIter != allResults.cend(); aRIter++) {
1948 ResultPtr aRes = *aRIter;
1949 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
1950 aRes->data()->name() == theName)
1952 if (!aResult.get() || isLater(aFeature, aResFeature)) { // select the latest
1954 aResFeature = aFeature;
1962 TDF_Label Model_Objects::nextLabel(TDF_Label theCurrent, int& theIndex, const bool theReverse)
1964 Handle(TDataStd_ReferenceArray) aRefs;
1965 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1966 int aStart = theIndex == kUNDEFINED_FEATURE_INDEX ? aRefs->Lower() : theIndex;
1967 for(int a = aStart; a <= aRefs->Upper(); a++) { // iterate all existing features
1968 TDF_Label aCurLab = aRefs->Value(a);
1969 if (aCurLab.IsEqual(theCurrent)) {
1970 a += theReverse ? -1 : 1;
1971 if (a >= aRefs->Lower() && a <= aRefs->Upper()) {
1973 return aRefs->Value(a);
1975 break; // finish iteration: it's last feature
1982 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, int& theIndex, const bool theReverse)
1984 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
1985 if (aData.get() && aData->isValid()) {
1986 TDF_Label aFeatureLabel = aData->label().Father();
1988 TDF_Label aNextLabel = nextLabel(aFeatureLabel, theIndex, theReverse);
1989 if (aNextLabel.IsNull())
1990 break; // the last or something is wrong
1991 FeaturePtr aFound = feature(aNextLabel);
1993 return aFound; // the feature is found
1994 // if the next label is a folder, skip it
1995 aFeatureLabel = folder(aNextLabel).get() ? aNextLabel : TDF_Label();
1996 } while (!aFeatureLabel.IsNull());
1998 return FeaturePtr(); // not found, last, or something is wrong
2001 FeaturePtr Model_Objects::firstFeature()
2003 Handle(TDataStd_ReferenceArray) aRefs;
2004 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2005 return feature(aRefs->Value(aRefs->Lower()));
2007 return FeaturePtr(); // no features at all
2010 FeaturePtr Model_Objects::lastFeature()
2012 Handle(TDataStd_ReferenceArray) aRefs;
2013 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2014 // comment this because of #2674 - features are removed from array on deactivation of Part
2015 /*FeaturePtr aLast = feature(aRefs->Value(aRefs->Upper()));
2016 if (!aLast.get() && aRefs->Length() != 0) { // erase the invalid feature from the array
2017 RemoveFromRefArray(featuresLabel(), aRefs->Value(aRefs->Upper()));
2018 return lastFeature(); // try once again, after the last was removed
2020 return feature(aRefs->Value(aRefs->Upper()));
2022 return FeaturePtr(); // no features at all
2025 bool Model_Objects::isLater(FeaturePtr theLater, FeaturePtr theCurrent) const
2027 std::shared_ptr<Model_Data> aLaterD = std::static_pointer_cast<Model_Data>(theLater->data());
2028 std::shared_ptr<Model_Data> aCurrentD = std::static_pointer_cast<Model_Data>(theCurrent->data());
2029 if (aLaterD.get() && aLaterD->isValid() && aCurrentD.get() && aCurrentD->isValid()) {
2030 TDF_Label aLaterL = aLaterD->label().Father();
2031 TDF_Label aCurrentL = aCurrentD->label().Father();
2032 int aLaterI = -1, aCurentI = -1; // not found yet state
2033 Handle(TDataStd_ReferenceArray) aRefs;
2034 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2035 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
2036 TDF_Label aCurLab = aRefs->Value(a);
2037 if (aCurLab.IsEqual(aLaterL)) {
2039 } else if (aCurLab.IsEqual(aCurrentL)) {
2042 if (aLaterI != -1 && aCurentI != -1) // both are found
2043 return aLaterI > aCurentI;
2047 return false; // not found, or something is wrong
2050 std::list<std::shared_ptr<ModelAPI_Object> > Model_Objects::allObjects()
2052 std::list<std::shared_ptr<ModelAPI_Object> > aResult;
2053 Handle(TDataStd_ReferenceArray) aRefs;
2054 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2055 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
2056 ObjectPtr anObject = object(aRefs->Value(a));
2057 if (!anObject.get()) // is it a folder?
2058 anObject = folder(aRefs->Value(a));
2060 aResult.push_back(anObject);
2066 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
2068 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
2069 Handle(TDataStd_ReferenceArray) aRefs;
2070 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2071 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
2072 FeaturePtr aFeature = feature(aRefs->Value(a));
2074 aResult.push_back(aFeature);
2080 int Model_Objects::numInternalFeatures()
2082 Handle(TDataStd_ReferenceArray) aRefs;
2083 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2084 return aRefs->Upper() - aRefs->Lower() + 1;
2086 return 0; // invalid
2089 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
2091 Handle(TDataStd_ReferenceArray) aRefs;
2092 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2093 return feature(aRefs->Value(aRefs->Lower() + theIndex));
2095 return FeaturePtr(); // invalid
2098 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
2100 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
2103 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
2105 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);