1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Model_Objects.h>
22 #include <Model_Data.h>
23 #include <Model_Document.h>
24 #include <Model_Events.h>
25 #include <Model_Session.h>
26 #include <Model_ResultPart.h>
27 #include <Model_ResultConstruction.h>
28 #include <Model_ResultBody.h>
29 #include <Model_ResultGroup.h>
30 #include <Model_ResultField.h>
31 #include <Model_ResultParameter.h>
32 #include <Model_AttributeRefList.h>
33 #include <ModelAPI_Validator.h>
34 #include <ModelAPI_CompositeFeature.h>
35 #include <ModelAPI_Tools.h>
37 #include <Events_Loop.h>
38 #include <Events_InfoMessage.h>
40 #include <TDataStd_Integer.hxx>
41 #include <TDataStd_Comment.hxx>
42 #include <TDF_ChildIDIterator.hxx>
43 #include <TDataStd_ReferenceArray.hxx>
44 #include <TDataStd_HLabelArray1.hxx>
45 #include <TDF_Reference.hxx>
46 #include <TDF_ChildIDIterator.hxx>
47 #include <TDF_LabelMapHasher.hxx>
48 #include <TDF_LabelMap.hxx>
49 #include <TDF_ListIteratorOfLabelList.hxx>
51 int kUNDEFINED_FEATURE_INDEX = -1;
53 static const std::string& groupNameFoldering(const std::string& theGroupID,
54 const bool theAllowFolder)
57 static const std::string anOutOfFolderName = std::string("__") + ModelAPI_Feature::group();
58 static const std::string aDummyName;
59 return theGroupID == ModelAPI_Feature::group() ? anOutOfFolderName : aDummyName;
64 // Check theFeature is a first or last feature in folder and return this folder
65 static FolderPtr inFolder(const FeaturePtr& theFeature, const std::string& theFolderAttr)
67 const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
68 for (std::set<AttributePtr>::iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt) {
69 if ((*anIt)->id() != theFolderAttr)
72 ObjectPtr anOwner = (*anIt)->owner();
73 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(anOwner);
81 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
84 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
85 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
88 /// 0:1:2 - where features are located
89 /// 0:1:2:N:1 - data of the feature N
90 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
92 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
96 void Model_Objects::setOwner(DocumentPtr theDoc)
99 // update all fields and recreate features and result objects if needed
100 TDF_LabelList aNoUpdated;
101 synchronizeFeatures(aNoUpdated, true, false, true, true);
105 Model_Objects::~Model_Objects()
107 // delete all features of this document
108 Events_Loop* aLoop = Events_Loop::loop();
109 // erase one by one to avoid access from the feature destructor itself from he map
110 // blocks the flush signals to avoid the temporary objects visualization in the viewer
111 // they should not be shown in order to do not lose highlight by erasing them
112 bool isActive = aLoop->activateFlushes(false);
114 while(!myFeatures.IsEmpty()) {
115 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
116 FeaturePtr aFeature = aFeaturesIter.Value();
117 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
118 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
119 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
120 aFeature->removeResults(0, false);
122 myFeatures.UnBind(aFeaturesIter.Key());
124 while (!myFolders.IsEmpty()) {
125 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFoldersIter(myFolders);
126 ObjectPtr aFolder = aFoldersIter.Value();
127 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
128 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
129 ModelAPI_EventCreator::get()->sendUpdated(aFolder, EVENT_DISP);
131 myFolders.UnBind(aFoldersIter.Key());
134 aLoop->activateFlushes(isActive);
135 // erase update, because features are destroyed and update should not performed for them anywhere
136 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
137 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
138 // deleted and redisplayed is correctly performed: they know that features are destroyed
139 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
140 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
144 /// Appends to the array of references a new referenced label
145 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
147 Handle(TDataStd_ReferenceArray) aRefs;
148 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
149 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
150 aRefs->SetValue(0, theReferenced);
151 } else { // extend array by one more element
152 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
154 int aPassedPrev = 0; // previous feature is found and passed
155 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
156 aNewArray->SetValue(aRefs->Lower(), theReferenced);
159 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
160 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
161 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
163 aNewArray->SetValue(a + 1, theReferenced);
166 if (!aPassedPrev) // not found: unknown situation
167 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
168 aRefs->SetInternalArray(aNewArray);
172 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
174 if (!theFeature->isAction()) { // do not add action to the data model
175 TDF_Label aFeaturesLab = featuresLabel();
176 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
177 // store feature in the features array: before "initData" because in macro features
178 // in initData it creates new features, appeared later than this
179 TDF_Label aPrevFeateureLab;
180 FolderPtr aParentFolder;
181 if (theAfterThis.get()) { // searching for the previous feature label
182 std::shared_ptr<Model_Data> aPrevData =
183 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
184 if (aPrevData.get()) {
185 aPrevFeateureLab = aPrevData->label().Father();
187 // Check if the previous feature is the last feature in a folder,
188 // then the folder should be updated to contain additional feature.
189 // Macro features are not stored in folder.
190 if (!theFeature->isMacro()) {
191 // If the last feature is a sub-feature of composite, use parent feature
192 // to check belonging to a folder.
193 FeaturePtr afterThis = ModelAPI_Tools::compositeOwner(theAfterThis);
195 afterThis = theAfterThis;
196 aParentFolder = inFolder(afterThis, ModelAPI_Folder::LAST_FEATURE_ID());
199 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
201 // keep the feature ID to restore document later correctly
202 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
203 myFeatures.Bind(aFeatureLab, theFeature);
204 // must be before the event sending: for OB the feature is already added
205 updateHistory(ModelAPI_Feature::group());
206 // do not change the order:
209 // during python script with fillet constraint feature data should be
210 // initialized before using it in GUI
212 // must be after binding to the map because of "Box" macro feature that
213 // creates other features in "initData"
214 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
215 // put feature to the end of folder if it is added while
216 // the history line is set to the last feature from the folder
218 aParentFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(theFeature);
219 updateHistory(ModelAPI_Folder::group());
221 // event: feature is added, mist be before "initData" to update OB correctly on Duplicate:
222 // first new part, then the content
223 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
224 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
225 } else { // make feature has not-null data anyway
226 theFeature->setData(Model_Data::invalidData());
227 theFeature->setDoc(myDoc);
231 /// Appends to the array of references a new referenced label.
232 /// If theIndex is not -1, removes element at this index, not theReferenced.
233 /// \returns the index of removed element
234 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
235 const int theIndex = -1)
237 int aResult = -1; // no returned
238 Handle(TDataStd_ReferenceArray) aRefs;
239 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
240 if (aRefs->Length() == 1) { // just erase an array
241 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
242 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
245 } else { // reduce the array
246 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
248 int aCount = aRefs->Lower();
249 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
250 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
254 aNewArray->SetValue(aCount, aRefs->Value(a));
257 aRefs->SetInternalArray(aNewArray);
263 void Model_Objects::refsToFeature(FeaturePtr theFeature,
264 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
266 // check the feature: it must have no depended objects on it
267 // the dependencies can be in the feature results
268 std::list<ResultPtr> aResults;
269 ModelAPI_Tools::allResults(theFeature, aResults);
270 std::list<ResultPtr>::const_iterator aResIter = aResults.cbegin();
271 for (; aResIter != aResults.cend(); aResIter++) {
272 ResultPtr aResult = (*aResIter);
273 std::shared_ptr<Model_Data> aData =
274 std::dynamic_pointer_cast<Model_Data>(aResult->data());
275 if (aData.get() != NULL) {
276 const std::set<AttributePtr>& aRefs = aData->refsToMe();
277 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
278 for (; aRefIt != aRefLast; aRefIt++) {
279 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
280 if (aFeature.get() != NULL)
281 theRefs.insert(aFeature);
285 // the dependencies can be in the feature itself
286 std::shared_ptr<Model_Data> aData =
287 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
288 if (aData.get() && !aData->refsToMe().empty()) {
289 const std::set<AttributePtr>& aRefs = aData->refsToMe();
290 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
291 for (; aRefIt != aRefLast; aRefIt++) {
292 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
293 if (aFeature.get() != NULL)
294 theRefs.insert(aFeature);
298 if (!theRefs.empty() && isSendError) {
299 Events_InfoMessage("Model_Objects",
300 "Feature '%1' is used and can not be deleted").arg(theFeature->data()->name()).send();
304 void Model_Objects::removeFeature(FeaturePtr theFeature)
306 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
307 if (aData.get() && aData->isValid()) {
308 // checking that the sub-element of composite feature is removed: if yes, inform the owner
309 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
310 refsToFeature(theFeature, aRefs, false);
311 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
312 for(; aRefIter != aRefs.end(); aRefIter++) {
313 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
314 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
315 if (aComposite.get() && aComposite->data()->isValid() && aComposite->isSub(theFeature)) {
316 aComposite->removeFeature(theFeature);
319 // remove feature from folder
320 removeFromFolder(std::list<FeaturePtr>(1, theFeature));
321 // this must be before erase since theFeature erasing removes all information about
322 // the feature results and groups of results
323 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
324 clearHistory(theFeature);
328 TDF_Label aFeatureLabel = aData->label().Father();
329 if (myFeatures.IsBound(aFeatureLabel))
330 myFeatures.UnBind(aFeatureLabel);
332 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
333 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
334 // erase all attributes under the label of feature
335 aFeatureLabel.ForgetAllAttributes();
336 // remove it from the references array
337 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
338 // event: feature is deleted
339 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
340 updateHistory(ModelAPI_Feature::group());
344 void Model_Objects::eraseAllFeatures()
346 static Events_ID kDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
347 static const ModelAPI_EventCreator* kCreator = ModelAPI_EventCreator::get();
348 // make all features invalid (like deleted)
349 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
350 for(; aFIter.More(); aFIter.Next()) {
351 FeaturePtr aFeature = aFIter.Value();
352 std::list<ResultPtr> aResList;
353 ModelAPI_Tools::allResults(aFeature, aResList);
354 std::list<ResultPtr>::iterator aRIter = aResList.begin();
355 for(; aRIter != aResList.end(); aRIter++) {
356 ResultPtr aRes = *aRIter;
357 if (aRes && aRes->data()->isValid()) {
358 kCreator->sendDeleted(myDoc, aRes->groupName());
359 kCreator->sendUpdated(aRes, kDispEvent);
360 aRes->setData(aRes->data()->invalidPtr());
364 kCreator->sendUpdated(aFeature, kDispEvent);
365 aFeature->setData(aFeature->data()->invalidPtr());
367 kCreator->sendDeleted(myDoc, ModelAPI_Feature::group());
368 myFeatures.Clear(); // just remove features without modification of DS
372 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
374 TDF_Label aFeaturesLab = featuresLabel();
375 Handle(TDataStd_ReferenceArray) aRefs;
376 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
378 TDF_Label anAfterLab, aMovedLab =
379 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
380 if (theAfterThis.get())
381 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
383 // check whether some folder refers to the moved feature by start or end: if yes, remove from it
384 removeFromFolder(std::list<FeaturePtr>(1, theMoved));
386 Handle(TDataStd_HLabelArray1) aNewArray =
387 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
388 int aPassedMovedFrom = 0; // the previous feature location is found and passed
389 int aPassedMovedTo = 0; // the feature is added and this location is passed
390 if (!theAfterThis.get()) { // null means that inserted feature must be the first
391 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
394 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
395 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
397 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
398 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
399 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
400 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
402 } else { // just copy one
403 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
404 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
407 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
408 if (!aPassedMovedFrom) {
409 static std::string aMovedFromError("The moved feature is not found");
410 Events_InfoMessage("Model_Objects", aMovedFromError).send();
412 static std::string aMovedToError("The 'after' feature for movement is not found");
413 Events_InfoMessage("Model_Objects", aMovedToError).send();
417 // store the new array
418 aRefs->SetInternalArray(aNewArray);
419 // update the feature and the history
420 clearHistory(theMoved);
421 // make sure all (selection) attributes of moved feature will be updated
422 static Events_ID kUpdateSelection = Events_Loop::loop()->eventByName(EVENT_UPDATE_SELECTION);
423 ModelAPI_EventCreator::get()->sendUpdated(theMoved, kUpdateSelection, false);
424 ModelAPI_EventCreator::get()->sendReordered(theMoved);
427 void Model_Objects::clearHistory(ObjectPtr theObj)
430 const std::string aGroup = theObj->groupName();
431 updateHistory(aGroup);
433 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
434 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
435 std::string aResultGroup = featureResultGroup(aFeature);
436 if (!aResultGroup.empty()) {
437 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
438 myHistory.find(aResultGroup);
439 if (aHIter != myHistory.end())
440 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
446 void Model_Objects::createHistory(const std::string& theGroupID)
448 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
449 if (aHIter == myHistory.end()) {
450 std::vector<ObjectPtr> aResult;
451 std::vector<ObjectPtr> aResultOutOfFolder;
452 FeaturePtr aLastFeatureInFolder;
453 // iterate the array of references and get feature by feature from the array
454 bool isFeature = theGroupID == ModelAPI_Feature::group();
455 bool isFolder = theGroupID == ModelAPI_Folder::group();
456 Handle(TDataStd_ReferenceArray) aRefs;
457 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
458 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
459 FeaturePtr aFeature = feature(aRefs->Value(a));
460 if (aFeature.get()) {
461 // if feature is in sub-component, remove it from history:
462 // it is in sub-tree of sub-component
463 bool isSub = ModelAPI_Tools::compositeOwner(aFeature).get() != NULL;
464 if (isFeature) { // here may be also disabled features
465 if (!isSub && aFeature->isInHistory()) {
466 aResult.push_back(aFeature);
467 // the feature is out of the folders
468 if (aLastFeatureInFolder.get() == NULL)
469 aResultOutOfFolder.push_back(aFeature);
471 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
472 // construction results of sub-features should not be in the tree
473 if (!isSub || theGroupID != ModelAPI_ResultConstruction::group()) {
474 // do not use reference to the list here since results can be changed by "isConcealed"
475 const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
476 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator
477 aRIter = aResults.begin();
478 for (; aRIter != aResults.cend(); aRIter++) {
479 ResultPtr aRes = *aRIter;
480 if (aRes->groupName() != theGroupID) break; // feature have only same group results
481 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
482 aResult.push_back(*aRIter);
488 // the feature closes the folder, so the next features will be treated as out-of-folder
489 if (aLastFeatureInFolder.get() && aLastFeatureInFolder == aFeature)
490 aLastFeatureInFolder = FeaturePtr();
493 // it may be a folder
494 const ObjectPtr& aFolder = folder(aRefs->Value(a));
496 // store folder information for the Features group only
497 if (isFeature || isFolder) {
498 aResult.push_back(aFolder);
500 aResultOutOfFolder.push_back(aFolder);
503 // get the last feature in the folder
504 AttributeReferencePtr aLastFeatAttr =
505 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
507 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
512 // to be sure that isConcealed did not update the history (issue 1089) during the iteration
513 if (myHistory.find(theGroupID) == myHistory.end()) {
514 myHistory[theGroupID] = aResult;
516 // store the features placed out of any folder
517 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroupID, true);
518 if (!anOutOfFolderGroupID.empty())
519 myHistory[anOutOfFolderGroupID] = aResultOutOfFolder;
524 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
526 clearHistory(theObject);
529 void Model_Objects::updateHistory(const std::string theGroup)
531 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
532 if (aHIter != myHistory.end()) {
533 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
535 // erase history for the group of objects placed out of any folder
536 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroup, true);
537 if (!anOutOfFolderGroupID.empty())
538 myHistory.erase(anOutOfFolderGroupID);
542 const ObjectPtr& Model_Objects::folder(TDF_Label theLabel) const
544 if (myFolders.IsBound(theLabel))
545 return myFolders.Find(theLabel);
546 static ObjectPtr anEmptyResult;
547 return anEmptyResult;
550 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
552 if (myFeatures.IsBound(theLabel))
553 return myFeatures.Find(theLabel);
554 return FeaturePtr(); // not found
557 ObjectPtr Model_Objects::object(TDF_Label theLabel)
559 // try feature by label
560 FeaturePtr aFeature = feature(theLabel);
562 return feature(theLabel);
563 TDF_Label aFeatureLabel = theLabel; // let's suppose it is result of this feature
564 TDF_LabelList aSubLabs; // sub - labels from higher level to lower level of result
565 while(!aFeature.get() && aFeatureLabel.Depth() > 1) {
566 aSubLabs.Prepend(aFeatureLabel);
567 aFeatureLabel = aFeatureLabel.Father().Father();
568 aFeature = feature(aFeatureLabel);
570 if (aFeature.get()) {
571 ResultPtr aCurrentResult;
572 // searching for results then sub-results label by label
573 for(TDF_ListIteratorOfLabelList aSubLab(aSubLabs); aSubLab.More(); aSubLab.Next()) {
574 if (aCurrentResult.get()) { // iterate sub-results of result
575 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCurrentResult);
577 return ObjectPtr(); // only Body can have sub-results
578 int a, aNumSubs = anOwner->numberOfSubs();
579 for(a = 0; a < aNumSubs; a++) {
580 ResultPtr aSub = anOwner->subResult(a);
582 std::shared_ptr<Model_Data> aSubData = std::dynamic_pointer_cast<Model_Data>(
584 const TDF_Label& aSubLabVal = aSubLab.ChangeValue();
585 if (aSubData->label().Father().IsEqual(aSubLabVal)) {
586 aCurrentResult = aSub;
591 if (a == aNumSubs) // not found an appropriate sub-result of result
593 } else { // iterate results of feature
594 const std::list<ResultPtr>& aResults = aFeature->results();
595 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
596 for(; aRIter != aResults.cend(); aRIter++) {
597 std::shared_ptr<Model_Data> aResData =
598 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
599 if (aResData->label().Father().IsEqual(aSubLab.ChangeValue())) {
600 aCurrentResult = *aRIter;
604 if (aRIter == aResults.cend()) // not found an appropriate result of feature
608 return aCurrentResult;
610 return ObjectPtr(); // not found
613 ObjectPtr Model_Objects::object(const std::string& theGroupID,
615 const bool theAllowFolder)
619 createHistory(theGroupID);
620 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
621 const std::vector<ObjectPtr>& aVec = myHistory[theGroupID];
622 //if (aVec.size() <= theIndex)
623 // return aVec[aVec.size() - 1]; // too high index requested (to avoid crash in #2360)
624 return aGroupID.empty() ? myHistory[theGroupID][theIndex] : myHistory[aGroupID][theIndex];
627 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
628 const std::string& theGroupID, const std::string& theName)
630 createHistory(theGroupID);
631 if (theGroupID == ModelAPI_Feature::group()) { // searching among features (in history or not)
632 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
633 // from the end to find the latest result with such name
634 std::list<std::shared_ptr<ModelAPI_Feature> >::reverse_iterator anObjIter = allObjs.rbegin();
635 for(; anObjIter != allObjs.rend(); anObjIter++) {
636 if ((*anObjIter)->data()->name() == theName)
639 } else { // searching among results (concealed or not)
640 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
641 // from the end to find the latest result with such name
642 std::list<std::shared_ptr<ModelAPI_Feature> >::reverse_iterator anObjIter = allObjs.rbegin();
643 for(; anObjIter != allObjs.rend(); anObjIter++) {
644 std::list<ResultPtr> allRes;
645 ModelAPI_Tools::allResults(*anObjIter, allRes);
646 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
647 if (aRes->get() && (*aRes)->groupName() == theGroupID) {
648 if ((*aRes)->data()->name() == theName)
658 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject,
659 const bool theAllowFolder)
661 std::string aGroup = theObject->groupName();
662 // treat folder as feature
663 if (aGroup == ModelAPI_Folder::group())
664 aGroup = ModelAPI_Feature::group();
665 createHistory(aGroup);
667 // get the group of features out of folder (if enabled)
668 if (theAllowFolder && !groupNameFoldering(aGroup, theAllowFolder).empty())
669 aGroup = groupNameFoldering(aGroup, theAllowFolder);
671 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
672 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
673 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
674 if ((*anObjIter) == theObject)
681 int Model_Objects::size(const std::string& theGroupID, const bool theAllowFolder)
683 createHistory(theGroupID);
684 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
685 return aGroupID.empty() ? int(myHistory[theGroupID].size()) : int(myHistory[aGroupID].size());
688 std::shared_ptr<ModelAPI_Object> Model_Objects::parent(
689 const std::shared_ptr<ModelAPI_Object> theChild)
691 if (theChild.get()) {
692 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theChild->data());
693 TDF_Label aLab = aData->label();
694 if (!aLab.IsNull() && aLab.Depth() > 2) {
695 ObjectPtr anObj = object(aLab.Father().Father().Father());
703 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
705 // iterate the array of references and get feature by feature from the array
706 Handle(TDataStd_ReferenceArray) aRefs;
707 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
708 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
709 FeaturePtr aFeature = feature(aRefs->Value(a));
710 if (aFeature.get()) {
711 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
712 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
713 for (; aRIter != aResults.cend(); aRIter++) {
714 ResultPtr aRes = *aRIter;
715 if (aRes->groupName() != theGroupID) break; // feature have only same group results
716 // iterate also concealed: ALL RESULTS (for translation parts undo/redo management)
717 //if (aRes->isInHistory() && !aRes->isConcealed()) {
718 theResults.push_back(*aRIter);
727 TDF_Label Model_Objects::featuresLabel() const
729 return myMain.FindChild(TAG_OBJECTS);
732 static std::string composeName(const std::string& theFeatureKind, const int theIndex)
734 std::stringstream aNameStream;
735 aNameStream << theFeatureKind << "_" << theIndex;
736 return aNameStream.str();
739 void Model_Objects::setUniqueName(FeaturePtr theFeature)
741 if (!theFeature->data()->name().empty())
742 return; // not needed, name is already defined
743 std::string aName; // result
744 // first count all features of such kind to start with index = count + 1
745 int aNumObjects = -1; // this feature is already in this map
746 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
747 std::set<std::string> allNames;
748 for (; aFIter.More(); aFIter.Next()) {
749 if (aFIter.Value()->getKind() == theFeature->getKind())
751 allNames.insert(aFIter.Value()->data()->name());
753 // generate candidate name
754 aName = composeName(theFeature->getKind(), aNumObjects + 1);
755 // check this is unique, if not, increase index by 1
756 for(aNumObjects++; allNames.find(aName) != allNames.end(); aNumObjects++) {
757 aName = composeName(theFeature->getKind(), aNumObjects + 1);
759 theFeature->data()->setName(aName);
762 void Model_Objects::setUniqueName(FolderPtr theFolder)
764 if (!theFolder->name().empty())
765 return; // name is already defined
767 int aNbFolders = myFolders.Size();
768 std::string aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
770 // check the uniqueness of the name
771 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator anIt(myFolders);
772 while (anIt.More()) {
773 if (anIt.Value()->data()->name() == aName) {
774 aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
775 // reinitialize iterator to make sure a new name is unique
776 anIt.Initialize(myFolders);
781 theFolder->data()->setName(aName);
784 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
786 std::shared_ptr<Model_Data> aData(new Model_Data);
787 aData->setLabel(theLab.FindChild(theTag));
788 aData->setObject(theObj);
789 theObj->setDoc(myDoc);
790 theObj->setData(aData);
791 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
792 if (aFeature.get()) {
793 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
794 } else { // is it a folder?
795 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theObj);
797 setUniqueName(aFolder);
799 theObj->initAttributes();
802 void Model_Objects::synchronizeFeatures(
803 const TDF_LabelList& theUpdated, const bool theUpdateReferences,
804 const bool theExecuteFeatures, const bool theOpen, const bool theFlush)
806 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
807 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
809 // after all updates, sends a message that groups of features were created or updated
810 Events_Loop* aLoop = Events_Loop::loop();
811 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
812 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
813 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
814 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
815 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
816 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
817 bool isActive = aLoop->activateFlushes(false);
819 // collect all updated labels map
820 TDF_LabelMap anUpdatedMap;
821 TDF_ListIteratorOfLabelList anUpdatedIter(theUpdated);
822 for(; anUpdatedIter.More(); anUpdatedIter.Next()) {
823 TDF_Label& aFeatureLab = anUpdatedIter.Value();
824 while(aFeatureLab.Depth() > 3)
825 aFeatureLab = aFeatureLab.Father();
826 if (myFeatures.IsBound(aFeatureLab) || myFolders.IsBound(aFeatureLab))
827 anUpdatedMap.Add(aFeatureLab);
830 // update all objects by checking are they on labels or not
831 std::set<ObjectPtr> aNewFeatures, aKeptFeatures;
832 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
833 for (; aLabIter.More(); aLabIter.Next()) {
834 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
835 if (!myFeatures.IsBound(aFeatureLabel) && !myFolders.IsBound(aFeatureLabel)) {
836 // a new feature or folder is inserted
838 std::string aFeatureID = TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
839 aLabIter.Value())->Get()).ToCString();
840 bool isFolder = aFeatureID == ModelAPI_Folder::ID();
842 std::shared_ptr<Model_Session> aSession =
843 std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get());
846 ObjectPtr aFeature = isFolder ? ObjectPtr(new ModelAPI_Folder)
847 : ObjectPtr(aSession->createFeature(aFeatureID, anOwner));
848 if (!aFeature.get()) {
849 // something is wrong, most probably, the opened document has invalid structure
850 Events_InfoMessage("Model_Objects", "Invalid type of object in the document").send();
851 aLabIter.Value()->Label().ForgetAllAttributes();
855 // this must be before "setData" to redo the sketch line correctly
857 myFolders.Bind(aFeatureLabel, aFeature);
859 myFeatures.Bind(aFeatureLabel, std::dynamic_pointer_cast<ModelAPI_Feature>(aFeature));
860 aNewFeatures.insert(aFeature);
861 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
862 updateHistory(aFeature);
864 // event: model is updated
865 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
866 } else { // nothing is changed, both iterators are incremented
869 if (myFeatures.Find(aFeatureLabel, aFeature)) {
870 aKeptFeatures.insert(aFeature);
873 if (myFolders.Find(aFeatureLabel, anObject))
874 aKeptFeatures.insert(anObject);
876 if (anUpdatedMap.Contains(aFeatureLabel)) {
877 if (!theOpen) { // on abort/undo/redo reinitialize attributes if something is changed
878 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
879 anObject->data()->attributes("");
880 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
881 for(; anAttr != anAttrs.end(); anAttr++)
884 ModelAPI_EventCreator::get()->sendUpdated(anObject, anUpdateEvent);
885 if (aFeature && aFeature->getKind() == "Parameter") {
886 // if parameters are changed, update the results (issue 937)
887 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
888 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
889 for (; aRIter != aResults.cend(); aRIter++) {
890 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
891 if (aRes->data()->isValid() && !aRes->isDisabled()) {
892 ModelAPI_EventCreator::get()->sendUpdated(aRes, anUpdateEvent);
900 // check all features are checked: if not => it was removed
901 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
902 while (aFIter.More()) {
903 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
904 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
905 FeaturePtr aFeature = aFIter.Value();
906 // event: model is updated
907 //if (aFeature->isInHistory()) {
908 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
910 // results of this feature must be redisplayed (hided)
911 // redisplay also removed feature (used for sketch and AISObject)
912 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
913 updateHistory(aFeature);
916 // unbind after the "erase" call: on abort sketch
917 // is removes sub-objects that corrupts aFIter
918 myFeatures.UnBind(aFIter.Key());
919 // reinitialize iterator because unbind may corrupt the previous order in the map
920 aFIter.Initialize(myFeatures);
924 // verify folders are checked: if not => is was removed
925 for (NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFldIt(myFolders);
926 aFldIt.More(); aFldIt.Next()) {
927 ObjectPtr aCurObj = aFldIt.Value();
928 if (aKeptFeatures.find(aCurObj) == aKeptFeatures.end() &&
929 aNewFeatures.find(aCurObj) == aNewFeatures.end()) {
930 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
931 // results of this feature must be redisplayed (hided)
932 // redisplay also removed feature (used for sketch and AISObject)
933 ModelAPI_EventCreator::get()->sendUpdated(aCurObj, aRedispEvent);
934 updateHistory(aCurObj);
937 // unbind after the "erase" call: on abort sketch
938 // is removes sub-objects that corrupts aFIter
939 myFolders.UnBind(aFldIt.Key());
940 // reinitialize iterator because unbind may corrupt the previous order in the map
941 aFldIt.Initialize(myFolders);
945 if (theUpdateReferences) {
946 synchronizeBackRefs();
948 // update results of the features (after features created because
949 // they may be connected, like sketch and sub elements)
950 // After synchronization of back references because sketch
951 // must be set in sub-elements before "execute" by updateResults
952 std::set<FeaturePtr> aProcessed; // composites must be updated after their subs (issue 360)
953 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
954 for (; aLabIter2.More(); aLabIter2.Next()) {
955 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
956 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
957 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
958 updateResults(aFeature, aProcessed);
961 // the synchronize should be done after updateResults
962 // in order to correct back references of updated results
963 if (theUpdateReferences) {
964 synchronizeBackRefs();
966 if (!theUpdated.IsEmpty()) {
967 // this means there is no control what was modified => remove history cash
971 if (!theExecuteFeatures)
972 anOwner->setExecuteFeatures(false);
973 aLoop->activateFlushes(isActive);
976 aLoop->flush(aDeleteEvent);
977 // delete should be emitted before create to reacts to aborted feature
978 aLoop->flush(aCreateEvent);
979 aLoop->flush(anUpdateEvent);
980 aLoop->flush(aCreateEvent); // after update of features, there could be results created
981 aLoop->flush(aDeleteEvent); // or deleted
982 aLoop->flush(aRedispEvent);
983 aLoop->flush(aToHideEvent);
985 if (!theExecuteFeatures)
986 anOwner->setExecuteFeatures(true);
989 /// synchronizes back references for the given object basing on the collected data
990 void Model_Objects::synchronizeBackRefsForObject(const std::set<AttributePtr>& theNewRefs,
993 if (!theObject.get() || !theObject->data()->isValid())
995 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
996 // iterate new list to compare with current
997 std::set<AttributePtr>::iterator aNewIter = theNewRefs.begin();
998 for(; aNewIter != theNewRefs.end(); aNewIter++) {
999 // for the Model_AttributeRefList erase cash (issue #2819)
1000 std::shared_ptr<Model_AttributeRefList> aRefList =
1001 std::dynamic_pointer_cast<Model_AttributeRefList>(*aNewIter);
1003 aRefList->eraseHash();
1005 if (aData->refsToMe().find(*aNewIter) == aData->refsToMe().end()) {
1006 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aNewIter)->owner());
1008 aData->addBackReference(aRefFeat, (*aNewIter)->id());
1009 else // add back reference to a folder
1010 aData->addBackReference((*aNewIter)->owner(), (*aNewIter)->id());
1013 if (theNewRefs.size() != aData->refsToMe().size()) { // some back ref must be removed
1014 std::set<AttributePtr>::iterator aCurrentIter = aData->refsToMe().begin();
1015 while(aCurrentIter != aData->refsToMe().end()) {
1016 if (theNewRefs.find(*aCurrentIter) == theNewRefs.end()) {
1017 // for external references from other documents this system
1018 // is not working: refs are collected from
1019 // different Model_Objects, so before remove check this
1020 // external object exists and still referenced
1021 bool aLeaveIt = false;
1022 if ((*aCurrentIter)->owner().get() && (*aCurrentIter)->owner()->document() != myDoc &&
1023 (*aCurrentIter)->owner()->data().get() && (*aCurrentIter)->owner()->data()->isValid()) {
1024 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
1025 (*aCurrentIter)->owner()->data()->referencesToObjects(aRefs);
1026 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> >>>::iterator
1027 aRefIter = aRefs.begin();
1028 for(; aRefIter != aRefs.end(); aRefIter++) {
1029 if ((*aCurrentIter)->id() == aRefIter->first) {
1030 std::list<std::shared_ptr<ModelAPI_Object> >::iterator anOIt;
1031 for(anOIt = aRefIter->second.begin(); anOIt != aRefIter->second.end(); anOIt++) {
1032 if (*anOIt == theObject) {
1040 aData->removeBackReference(*aCurrentIter);
1041 aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
1042 } else aCurrentIter++;
1043 } else aCurrentIter++;
1046 // for the last feature in the folder, check if it is a sub-feature,
1047 // then refer the folder to a top-level parent composite feature
1048 const std::set<AttributePtr>& aRefs = aData->refsToMe();
1049 std::set<AttributePtr>::iterator anIt = aRefs.begin();
1050 for (; anIt != aRefs.end(); ++anIt)
1051 if ((*anIt)->id() == ModelAPI_Folder::LAST_FEATURE_ID())
1053 if (anIt != aRefs.end()) {
1054 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1056 CompositeFeaturePtr aParent;
1057 CompositeFeaturePtr aGrandParent = ModelAPI_Tools::compositeOwner(aFeature);
1059 aParent = aGrandParent;
1061 aGrandParent = ModelAPI_Tools::compositeOwner(aParent);
1062 } while (aGrandParent.get());
1064 ObjectPtr aFolder = (*anIt)->owner();
1065 // remove reference from the current feature
1066 aData->removeBackReference(aFolder, ModelAPI_Folder::LAST_FEATURE_ID());
1067 // set reference to a top-level parent
1068 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aParent);
1069 std::shared_ptr<Model_Data> aParentData =
1070 std::dynamic_pointer_cast<Model_Data>(aParent->data());
1071 aParentData->addBackReference(aFolder, ModelAPI_Folder::LAST_FEATURE_ID());
1075 aData->updateConcealmentFlag();
1078 static void collectReferences(std::shared_ptr<ModelAPI_Data> theData,
1079 std::map<ObjectPtr, std::set<AttributePtr> >& theRefs)
1081 if (theData.get()) {
1082 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1083 theData->referencesToObjects(aRefs);
1084 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIt = aRefs.begin();
1085 for(; aRefsIt != aRefs.end(); aRefsIt++) {
1086 std::list<ObjectPtr>::iterator aRefTo = aRefsIt->second.begin();
1087 for(; aRefTo != aRefsIt->second.end(); aRefTo++) {
1089 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = theRefs.find(*aRefTo);
1090 if (aFound == theRefs.end()) {
1091 theRefs[*aRefTo] = std::set<AttributePtr>();
1092 aFound = theRefs.find(*aRefTo);
1094 aFound->second.insert(theData->attribute(aRefsIt->first));
1101 void Model_Objects::synchronizeBackRefs()
1103 // collect all back references in the separated container: to update everything at once,
1104 // without additional Concealment switching on and off: only the final modification
1106 // referenced (slave) objects to referencing attributes
1107 std::map<ObjectPtr, std::set<AttributePtr> > allRefs;
1108 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
1109 for(; aFeatures.More(); aFeatures.Next()) {
1110 FeaturePtr aFeature = aFeatures.Value();
1111 collectReferences(aFeature->data(), allRefs);
1113 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFolders(myFolders);
1114 for(; aFolders.More(); aFolders.Next()) {
1115 ObjectPtr aFolder = aFolders.Value();
1116 collectReferences(aFolder->data(), allRefs);
1118 // second iteration: just compare back-references with existing in features and results
1119 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1120 FeaturePtr aFeature = aFeatures.Value();
1121 static std::set<AttributePtr> anEmpty;
1122 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(aFeature);
1123 if (aFound == allRefs.end()) { // not found => erase all back references
1124 synchronizeBackRefsForObject(anEmpty, aFeature);
1126 synchronizeBackRefsForObject(aFound->second, aFeature);
1127 allRefs.erase(aFound); // to check that all refs are counted
1130 std::list<ResultPtr> aResults;
1131 ModelAPI_Tools::allResults(aFeature, aResults);
1132 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1133 for(; aRIter != aResults.cend(); aRIter++) {
1134 aFound = allRefs.find(*aRIter);
1135 if (aFound == allRefs.end()) { // not found => erase all back references
1136 synchronizeBackRefsForObject(anEmpty, *aRIter);
1138 synchronizeBackRefsForObject(aFound->second, *aRIter);
1139 allRefs.erase(aFound); // to check that all refs are counted
1143 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1144 FeaturePtr aFeature = aFeatures.Value();
1145 std::list<ResultPtr> aResults;
1146 ModelAPI_Tools::allResults(aFeature, aResults);
1147 // update the concealment status for display in isConcealed of ResultBody
1148 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1149 for(; aRIter != aResults.cend(); aRIter++) {
1150 (*aRIter)->isConcealed();
1153 // the rest all refs means that feature references to the external document feature:
1154 // process also them
1155 std::map<ObjectPtr, std::set<AttributePtr> >::iterator anExtIter = allRefs.begin();
1156 for(; anExtIter != allRefs.end(); anExtIter++) {
1157 synchronizeBackRefsForObject(anExtIter->second, anExtIter->first);
1161 TDF_Label Model_Objects::resultLabel(
1162 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1164 const std::shared_ptr<Model_Data>& aData =
1165 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1166 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1169 bool Model_Objects::hasCustomName(DataPtr theFeatureData,
1170 ResultPtr theResult,
1172 std::string& theParentName) const
1174 ResultBodyPtr aBodyRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theFeatureData->owner());
1176 // only for top-results (works for the cases when results are not yet added to the feature)
1177 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult);
1179 // names of sub-solids in CompSolid should be default (for example,
1180 // result of boolean operation 'Boolean_1' is a CompSolid which is renamed to 'MyBOOL',
1181 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1', 'Boolean_1_2' etc.)
1182 if (std::dynamic_pointer_cast<Model_Data>(aBodyRes->data())->label().Depth() == 6) {
1183 std::ostringstream aDefaultName;
1184 // compute default name of CompSolid (name of feature + index of CompSolid's result)
1185 int aBodyResultIndex = 0;
1186 const std::list<ResultPtr>& aResults = anOwner->results();
1187 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
1188 for (; anIt != aResults.end(); ++anIt, ++aBodyResultIndex)
1189 if (aBodyRes == *anIt)
1191 aDefaultName << anOwner->name();
1192 aDefaultName << "_" << (aBodyResultIndex + 1);
1193 theParentName = aDefaultName.str();
1194 } else { // just name of the parent result if it is deeper than just a sub-result
1195 theParentName = aBodyRes->data()->name();
1200 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(theResult);
1202 theParentName = aName.first;
1203 return aName.second;
1206 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1207 std::shared_ptr<ModelAPI_Result> theResult,
1208 const int theResultIndex)
1211 theResult->setDoc(myDoc);
1212 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1213 if (theResult->data()->name().empty()) {
1214 // if was not initialized, generate event and set a name
1215 std::string aNewName = theFeatureData->name();
1216 if (hasCustomName(theFeatureData, theResult, theResultIndex, aNewName)) {
1217 // if the name of result is user-defined, then, at first time, assign name of the result
1218 // by empty string to be sure that corresponding flag in the data model is set
1219 theResult->data()->setName("");
1221 std::stringstream aName;
1223 // if there are several results (issue #899: any number of result),
1224 // add unique prefix starting from second
1225 if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
1226 aName << "_" << theResultIndex + 1;
1227 aNewName = aName.str();
1229 theResult->data()->setName(aNewName);
1233 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
1234 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1236 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1237 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1238 ObjectPtr anOldObject = object(aLab);
1239 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1240 if (anOldObject.get()) {
1241 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1243 if (!aResult.get()) {
1244 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1245 storeResult(theFeatureData, aResult, theIndex);
1250 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
1251 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1253 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1254 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1255 ObjectPtr anOldObject = object(aLab);
1256 std::shared_ptr<ModelAPI_ResultBody> aResult;
1257 if (anOldObject.get()) {
1258 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1260 if (!aResult.get()) {
1261 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1262 storeResult(theFeatureData, aResult, theIndex);
1267 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
1268 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1270 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1271 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1272 ObjectPtr anOldObject = object(aLab);
1273 std::shared_ptr<ModelAPI_ResultPart> aResult;
1274 if (anOldObject.get()) {
1275 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1277 if (!aResult.get()) {
1278 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1279 storeResult(theFeatureData, aResult, theIndex);
1284 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
1285 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
1286 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1288 std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
1289 aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
1293 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
1294 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1296 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1297 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1298 ObjectPtr anOldObject = object(aLab);
1299 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1300 if (anOldObject.get()) {
1301 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1303 if (!aResult.get()) {
1304 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1305 storeResult(theFeatureData, aResult, theIndex);
1310 std::shared_ptr<ModelAPI_ResultField> Model_Objects::createField(
1311 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1313 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1314 TDataStd_Comment::Set(aLab, ModelAPI_ResultField::group().c_str());
1315 ObjectPtr anOldObject = object(aLab);
1316 std::shared_ptr<ModelAPI_ResultField> aResult;
1317 if (anOldObject.get()) {
1318 aResult = std::dynamic_pointer_cast<ModelAPI_ResultField>(anOldObject);
1320 if (!aResult.get()) {
1321 aResult = std::shared_ptr<ModelAPI_ResultField>(new Model_ResultField(theFeatureData));
1322 storeResult(theFeatureData, aResult, theIndex);
1327 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
1328 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1330 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1331 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1332 ObjectPtr anOldObject = object(aLab);
1333 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1334 if (anOldObject.get()) {
1335 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1337 if (!aResult.get()) {
1338 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1339 storeResult(theFeatureData, aResult, theIndex);
1344 std::shared_ptr<ModelAPI_Folder> Model_Objects::createFolder(
1345 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis)
1347 FolderPtr aFolder(new ModelAPI_Folder);
1351 TDF_Label aFeaturesLab = featuresLabel();
1352 TDF_Label aFolderLab = aFeaturesLab.NewChild();
1353 // store feature in the features array: before "initData" because in macro features
1354 // in initData it creates new features, appeared later than this
1355 TDF_Label aPrevFeatureLab;
1356 if (theBeforeThis.get()) { // searching for the previous feature label
1357 std::shared_ptr<Model_Data> aPrevData =
1358 std::dynamic_pointer_cast<Model_Data>(theBeforeThis->data());
1359 if (aPrevData.get()) {
1360 int anIndex = kUNDEFINED_FEATURE_INDEX;
1361 aPrevFeatureLab = nextLabel(aPrevData->label().Father(), anIndex, true);
1363 } else { // find the label of the last feature
1364 Handle(TDataStd_ReferenceArray) aRefs;
1365 if (aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1366 aPrevFeatureLab = aRefs->Value(aRefs->Upper());
1368 AddToRefArray(aFeaturesLab, aFolderLab, aPrevFeatureLab);
1370 // keep the feature ID to restore document later correctly
1371 TDataStd_Comment::Set(aFolderLab, ModelAPI_Folder::ID().c_str());
1372 myFolders.Bind(aFolderLab, aFolder);
1373 // must be before the event sending: for OB the feature is already added
1374 updateHistory(ModelAPI_Folder::group());
1375 updateHistory(ModelAPI_Feature::group());
1377 // must be after binding to the map because of "Box" macro feature that
1378 // creates other features in "initData"
1379 initData(aFolder, aFolderLab, TAG_FEATURE_ARGUMENTS);
1380 // event: folder is added, must be before "initData" to update OB correctly on Duplicate:
1381 // first new part, then the content
1382 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1383 ModelAPI_EventCreator::get()->sendUpdated(aFolder, anEvent);
1388 void Model_Objects::removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder)
1390 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFolder->data());
1391 if (!aData.get() || !aData->isValid())
1394 // this must be before erase since theFolder erasing removes all information about it
1395 clearHistory(theFolder);
1399 TDF_Label aFolderLabel = aData->label().Father();
1400 if (myFolders.IsBound(aFolderLabel))
1401 myFolders.UnBind(aFolderLabel);
1403 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1404 ModelAPI_EventCreator::get()->sendUpdated(theFolder, EVENT_DISP);
1405 // erase all attributes under the label of feature
1406 aFolderLabel.ForgetAllAttributes();
1407 // remove it from the references array
1408 RemoveFromRefArray(featuresLabel(), aFolderLabel);
1409 // event: feature is deleted
1410 ModelAPI_EventCreator::get()->sendDeleted(theFolder->document(), ModelAPI_Folder::group());
1411 updateHistory(ModelAPI_Folder::group());
1412 updateHistory(ModelAPI_Feature::group());
1415 // Returns one of the limiting features of the list
1416 static FeaturePtr limitingFeature(std::list<FeaturePtr>& theFeatures, const bool isLast)
1418 FeaturePtr aFeature;
1420 aFeature = theFeatures.back();
1421 theFeatures.pop_back();
1423 aFeature = theFeatures.front();
1424 theFeatures.pop_front();
1429 // Verify the feature is sub-element in composite feature or it is not used in the history
1430 static bool isSkippedFeature(FeaturePtr theFeature)
1432 bool isSub = ModelAPI_Tools::compositeOwner(theFeature).get() != NULL;
1433 return isSub || (theFeature && !theFeature->isInHistory());
1436 std::shared_ptr<ModelAPI_Folder> Model_Objects::findFolder(
1437 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1438 const bool theBelow)
1440 if (theFeatures.empty())
1441 return FolderPtr(); // nothing to move
1443 TDF_Label aFeaturesLab = featuresLabel();
1444 Handle(TDataStd_ReferenceArray) aRefs;
1445 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1446 return FolderPtr(); // no reference array (something is wrong)
1448 std::list<std::shared_ptr<ModelAPI_Feature> > aFeatures = theFeatures;
1449 std::shared_ptr<ModelAPI_Feature> aLimitingFeature = limitingFeature(aFeatures, theBelow);
1451 std::shared_ptr<Model_Data> aData =
1452 std::static_pointer_cast<Model_Data>(aLimitingFeature->data());
1453 if (!aData || !aData->isValid())
1454 return FolderPtr(); // invalid feature
1456 // label of the first feature in the list for fast searching
1457 TDF_Label aFirstFeatureLabel = aData->label().Father();
1459 // find a folder above the features and
1460 // check the given features represent a sequential list of objects following the folder
1461 FolderPtr aFoundFolder;
1462 TDF_Label aLastFeatureInFolder;
1463 int aRefIndex = aRefs->Lower();
1464 for(; aRefIndex <= aRefs->Upper(); ++aRefIndex) { // iterate all existing features
1465 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1466 if (IsEqual(aCurLabel, aFirstFeatureLabel))
1467 break; // no need to continue searching
1469 // searching the folder below, just continue to search last feature from the list
1473 // if feature is in sub-component, skip it
1474 FeaturePtr aCurFeature = feature(aCurLabel);
1475 if (isSkippedFeature(aCurFeature))
1478 if (!aLastFeatureInFolder.IsNull()) {
1479 if (IsEqual(aCurLabel, aLastFeatureInFolder))
1480 aLastFeatureInFolder.Nullify(); // the last feature in the folder is achieved
1484 const ObjectPtr& aFolderObj = folder(aCurLabel);
1485 if (aFolderObj.get()) {
1486 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aFolderObj);
1487 AttributeReferencePtr aLastFeatAttr =
1488 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1489 if (aLastFeatAttr) {
1490 // setup iterating inside a folder to find last feature
1491 ObjectPtr aLastFeature = aLastFeatAttr->value();
1493 aData = std::static_pointer_cast<Model_Data>(aLastFeature->data());
1494 if (aData && aData->isValid())
1495 aLastFeatureInFolder = aData->label().Father();
1501 if (theBelow && aRefIndex < aRefs->Upper()) {
1503 // skip following features which are sub-components or not in history
1504 for (int anIndex = aRefIndex + 1; anIndex <= aRefs->Upper(); ++anIndex) {
1505 aLabel = aRefs->Value(anIndex);
1506 FeaturePtr aCurFeature = feature(aLabel);
1507 if (!isSkippedFeature(aCurFeature))
1510 // check the next object is a folder
1511 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(folder(aLabel));
1514 if (!aLastFeatureInFolder.IsNull() || // the last feature of the folder above is not found
1518 // check the given features are sequential list
1519 int aStep = theBelow ? -1 : 1;
1520 for (aRefIndex += aStep;
1521 !aFeatures.empty() && aRefIndex >= aRefs->Lower() && aRefIndex <= aRefs->Upper();
1522 aRefIndex += aStep) {
1523 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1524 // if feature is in sub-component, skip it
1525 FeaturePtr aCurFeature = feature(aCurLabel);
1526 if (isSkippedFeature(aCurFeature))
1529 aLimitingFeature = limitingFeature(aFeatures, theBelow);
1530 if (!aCurFeature->data()->isEqual(aLimitingFeature->data()))
1531 return FolderPtr(); // not a sequential list
1534 return aFoundFolder;
1537 bool Model_Objects::moveToFolder(
1538 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1539 const std::shared_ptr<ModelAPI_Folder>& theFolder)
1541 if (theFeatures.empty() || !theFolder)
1544 // labels for the folder and last feature in the list
1545 TDF_Label aFolderLabel, aLastFeatureLabel;
1546 std::shared_ptr<Model_Data> aData =
1547 std::static_pointer_cast<Model_Data>(theFolder->data());
1548 if (aData && aData->isValid())
1549 aFolderLabel = aData->label().Father();
1550 aData = std::static_pointer_cast<Model_Data>(theFeatures.back()->data());
1551 if (aData && aData->isValid())
1552 aLastFeatureLabel = aData->label().Father();
1554 if (aFolderLabel.IsNull() || aLastFeatureLabel.IsNull())
1557 AttributeReferencePtr aFirstFeatAttr =
1558 theFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1559 AttributeReferencePtr aLastFeatAttr =
1560 theFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1561 bool initFirstAttr = !aFirstFeatAttr->value().get();
1562 bool initLastAttr = !aLastFeatAttr->value().get();
1564 // check the folder is below the list of features
1565 bool isFolderBelow = false;
1566 TDF_Label aFeaturesLab = featuresLabel();
1567 Handle(TDataStd_ReferenceArray) aRefs;
1568 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1569 return false; // no reference array (something is wrong)
1570 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1571 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1572 if (aCurLabel == aFolderLabel)
1573 break; // folder is above the features
1574 else if (aCurLabel == aLastFeatureLabel) {
1575 isFolderBelow = true;
1580 if (isFolderBelow) {
1581 aData = std::static_pointer_cast<Model_Data>(theFeatures.front()->data());
1582 if (!aData || !aData->isValid())
1584 TDF_Label aPrevFeatureLabel = aData->label().Father();
1585 // label of the feature before the first feature in the list
1586 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex)
1587 if (aPrevFeatureLabel == aRefs->Value(aRefIndex)) {
1588 if (aRefIndex == aRefs->Lower())
1589 aPrevFeatureLabel.Nullify();
1591 aPrevFeatureLabel = aRefs->Value(aRefIndex - 1);
1595 // move the folder in the list of references before the first feature
1596 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1597 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1598 // update first feature of the folder
1599 initFirstAttr = true;
1601 // update last feature of the folder
1602 initLastAttr = true;
1606 aFirstFeatAttr->setValue(theFeatures.front());
1608 aLastFeatAttr->setValue(theFeatures.back());
1610 updateHistory(ModelAPI_Feature::group());
1614 static FolderPtr isExtractionCorrect(const FolderPtr& theFirstFeatureFolder,
1615 const FolderPtr& theLastFeatureFolder,
1616 bool& isExtractBefore)
1618 if (theFirstFeatureFolder.get()) {
1619 if (theLastFeatureFolder.get())
1620 return theFirstFeatureFolder == theLastFeatureFolder ? theFirstFeatureFolder : FolderPtr();
1622 isExtractBefore = true;
1623 return theFirstFeatureFolder;
1624 } else if (theLastFeatureFolder.get()) {
1625 isExtractBefore = false;
1626 return theLastFeatureFolder;
1632 bool Model_Objects::removeFromFolder(
1633 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1634 const bool theBefore)
1636 if (theFeatures.empty())
1639 FolderPtr aFirstFeatureFolder =
1640 inFolder(theFeatures.front(), ModelAPI_Folder::FIRST_FEATURE_ID());
1641 FolderPtr aLastFeatureFolder =
1642 inFolder(theFeatures.back(), ModelAPI_Folder::LAST_FEATURE_ID());
1644 bool isExtractBeforeFolder = theBefore;
1645 FolderPtr aFoundFolder =
1646 isExtractionCorrect(aFirstFeatureFolder, aLastFeatureFolder, isExtractBeforeFolder);
1648 return false; // list of features cannot be extracted
1650 // references of the current folder
1651 ObjectPtr aFolderStartFeature;
1652 ObjectPtr aFolderEndFeature;
1653 if (aFirstFeatureFolder != aLastFeatureFolder) {
1654 aFolderStartFeature = aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->value();
1655 aFolderEndFeature = aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->value();
1658 FeaturePtr aFeatureToFind = isExtractBeforeFolder ? theFeatures.back() : theFeatures.front();
1659 std::shared_ptr<Model_Data> aData =
1660 std::static_pointer_cast<Model_Data>(aFeatureToFind->data());
1661 if (!aData || !aData->isValid())
1663 TDF_Label aLabelToFind = aData->label().Father();
1665 // search the label in the list of references
1666 TDF_Label aFeaturesLab = featuresLabel();
1667 Handle(TDataStd_ReferenceArray) aRefs;
1668 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1669 return false; // no reference array (something is wrong)
1670 int aRefIndex = aRefs->Lower();
1671 for (; aRefIndex <= aRefs->Upper(); ++aRefIndex)
1672 if (aRefs->Value(aRefIndex) == aLabelToFind)
1675 // update folder position
1676 if (isExtractBeforeFolder) {
1677 aData = std::dynamic_pointer_cast<Model_Data>(aFoundFolder->data());
1678 TDF_Label aFolderLabel = aData->label().Father();
1679 TDF_Label aPrevFeatureLabel = aRefs->Value(aRefIndex);
1680 // update start reference of the folder
1681 if (aFolderStartFeature.get()) {
1682 FeaturePtr aNewStartFeature;
1683 do { // skip all features placed in the composite features
1684 aPrevFeatureLabel = aRefs->Value(aRefIndex++);
1686 aRefIndex <= aRefs->Upper() ? feature(aRefs->Value(aRefIndex)) : FeaturePtr();
1687 } while (aNewStartFeature && isSkippedFeature(aNewStartFeature));
1688 aFolderStartFeature = aNewStartFeature;
1690 // move the folder in the list of references after the last feature from the list
1691 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1692 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1694 // update end reference of the folder
1695 if (aFolderEndFeature.get()) {
1696 FeaturePtr aNewEndFeature;
1697 do { // skip all features placed in the composite features
1700 aRefIndex >= aRefs->Lower() ? feature(aRefs->Value(aRefIndex)) : FeaturePtr();
1701 } while (aNewEndFeature && isSkippedFeature(aNewEndFeature));
1702 aFolderEndFeature = aNewEndFeature;
1706 // update folder references
1707 aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->setValue(aFolderStartFeature);
1708 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aFolderEndFeature);
1710 updateHistory(ModelAPI_Feature::group());
1714 FolderPtr Model_Objects::findContainingFolder(const FeaturePtr& theFeature, int& theIndexInFolder)
1716 // search the label in the list of references
1717 TDF_Label aFeaturesLab = featuresLabel();
1718 Handle(TDataStd_ReferenceArray) aRefs;
1719 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1720 return FolderPtr(); // no reference array (something is wrong)
1722 std::shared_ptr<Model_Data> aData =
1723 std::static_pointer_cast<Model_Data>(theFeature->data());
1724 if (!aData || !aData->isValid())
1726 TDF_Label aLabelToFind = aData->label().Father();
1728 theIndexInFolder = -1;
1729 FolderPtr aFoundFolder;
1730 TDF_Label aLastFeatureLabel;
1732 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1733 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1738 if (aCurLabel == aLabelToFind) { // the feature is reached
1740 if (isSkippedFeature(theFeature)) {
1741 theIndexInFolder = -1;
1744 // decrease the index of the feature in the folder by the number of skipped features
1745 for (int anIndex = theIndexInFolder - 1; anIndex > 0; anIndex--) {
1746 aCurLabel = aRefs->Value(aRefIndex - anIndex);
1747 if (isSkippedFeature(feature(aCurLabel)))
1751 return aFoundFolder;
1754 if (!aFoundFolder) {
1755 // if the current label refers to a folder, feel all necessary data
1756 const ObjectPtr& aFolderObj = folder(aCurLabel);
1757 if (aFolderObj.get()) {
1758 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aFolderObj);
1759 theIndexInFolder = -1;
1761 AttributeReferencePtr aLastRef =
1762 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1763 if (aLastRef->value()) {
1764 aData = std::static_pointer_cast<Model_Data>(aLastRef->value()->data());
1765 if (aData && aData->isValid())
1766 aLastFeatureLabel = aData->label().Father();
1767 } else // folder is empty
1768 aFoundFolder = FolderPtr();
1770 } else if (aLastFeatureLabel == aCurLabel) {
1771 // folder is finished, clear all stored data
1772 theIndexInFolder = -1;
1773 aFoundFolder = FolderPtr();
1777 // folder is not found
1778 theIndexInFolder = -1;
1783 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
1784 const std::shared_ptr<ModelAPI_Result>& theResult)
1786 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1787 if (aData.get() && aData->isValid()) {
1788 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1789 FeaturePtr aFeature = feature(aFeatureLab);
1790 while(!aFeature.get() && aFeatureLab.Depth() > 1) { // this may be sub-result of result
1791 aFeatureLab = aFeatureLab.Father().Father();
1792 aFeature = feature(aFeatureLab);
1796 return FeaturePtr();
1799 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
1801 if (theFeature->data()->isValid()) {
1802 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1803 if (aLabIter.More()) {
1804 TDF_Label anArgLab = aLabIter.Value();
1805 Handle(TDataStd_Comment) aGroup;
1806 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1807 return TCollection_AsciiString(aGroup->Get()).ToCString();
1811 static std::string anEmpty;
1812 return anEmpty; // not found
1815 void Model_Objects::updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
1817 if (theProcessed.find(theFeature) != theProcessed.end())
1819 theProcessed.insert(theFeature);
1820 // for composites update subs recursively (sketch elements results are needed for the sketch)
1821 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
1822 if (aComp.get() && aComp->getKind() != "Part") { // don't go inside of parts sub-features
1823 // update subs of composites first
1824 int aSubNum = aComp->numberOfSubs();
1825 for(int a = 0; a < aSubNum; a++) {
1826 FeaturePtr aSub = aComp->subFeature(a);
1827 updateResults(aComp->subFeature(a), theProcessed);
1831 // check the existing results and remove them if there is nothing on the label
1832 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1833 while(aResIter != theFeature->results().cend()) {
1834 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1836 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(aBody->data());
1837 if (!aData.get() || !aData->isValid() || (!aBody->isDisabled() && aData->isDeleted())) {
1838 // found a disappeared result => remove it
1839 theFeature->eraseResultFromList(aBody);
1840 // start iterate from beginning because iterator is corrupted by removing
1841 aResIter = theFeature->results().cbegin();
1847 // it may be on undo
1848 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
1850 // check that results are presented on all labels
1851 int aResSize = int(theFeature->results().size());
1852 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1853 for(; aLabIter.More(); aLabIter.Next()) {
1854 // here must be GUID of the feature
1855 int aResIndex = aLabIter.Value().Tag() - 1;
1857 if (aResSize <= aResIndex) {
1858 TDF_Label anArgLab = aLabIter.Value();
1859 Handle(TDataStd_Comment) aGroup;
1860 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1861 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1862 aNewBody = createBody(theFeature->data(), aResIndex);
1863 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1864 if (aResIndex <= theFeature->results().size()) { // to avoid crash if previous execute
1865 // for index = 0 erases result
1866 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
1867 theFeature->setResult(aNewP, aResIndex);
1868 if (!aNewP->partDoc().get())
1869 // create the part result: it is better to restore the previous result if possible
1870 theFeature->execute();
1872 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1873 ResultConstructionPtr aConstr = createConstruction(theFeature->data(), aResIndex);
1874 if (!aConstr->updateShape())
1875 theFeature->execute(); // not stored shape in the data structure, execute to have it
1877 theFeature->setResult(aConstr, aResIndex); // result is ready without execution
1878 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1879 aNewBody = createGroup(theFeature->data(), aResIndex);
1880 } else if (aGroup->Get() == ModelAPI_ResultField::group().c_str()) {
1881 aNewBody = createField(theFeature->data(), aResIndex);
1882 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1883 theFeature->attributeChanged("expression"); // just produce a value
1885 Events_InfoMessage("Model_Objects", "Unknown type of result is found in the document:")
1886 .arg(TCollection_AsciiString(aGroup->Get()).ToCString()).send();
1889 if (aNewBody && !aNewBody->data()->isDeleted()) {
1890 theFeature->setResult(aNewBody, aResIndex);
1894 if (aResSize > 0) { // check there exist a body that must be updated
1895 std::list<ResultPtr>::const_iterator aRes = theFeature->results().cbegin();
1896 for (; aResSize && aRes != theFeature->results().cend(); aRes++, aResSize++) {
1897 if ((*aRes)->data()->isValid()) {
1898 if ((*aRes)->groupName() == ModelAPI_ResultBody::group()) {
1899 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRes);
1900 aBody->updateSubs(aBody->shape(), false);
1901 } else if ((*aRes)->groupName() == ModelAPI_ResultConstruction::group()) {
1902 // update the cashed myShape presented in construction
1903 ResultConstructionPtr aConstr =
1904 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
1905 aConstr->updateShape();
1912 ResultPtr Model_Objects::findByName(const std::string theName)
1915 FeaturePtr aResFeature; // keep feature to return the latest one
1916 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
1917 for(; anObjIter.More(); anObjIter.Next()) {
1918 FeaturePtr& aFeature = anObjIter.ChangeValue();
1919 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
1921 std::list<ResultPtr> allResults;
1922 ModelAPI_Tools::allResults(aFeature, allResults);
1923 std::list<ResultPtr>::iterator aRIter = allResults.begin();
1924 for (; aRIter != allResults.cend(); aRIter++) {
1925 ResultPtr aRes = *aRIter;
1926 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
1927 aRes->data()->name() == theName)
1929 if (!aResult.get() || isLater(aFeature, aResFeature)) { // select the latest
1931 aResFeature = aFeature;
1939 TDF_Label Model_Objects::nextLabel(TDF_Label theCurrent, int& theIndex, const bool theReverse)
1941 Handle(TDataStd_ReferenceArray) aRefs;
1942 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1943 int aStart = theIndex == kUNDEFINED_FEATURE_INDEX ? aRefs->Lower() : theIndex;
1944 for(int a = aStart; a <= aRefs->Upper(); a++) { // iterate all existing features
1945 TDF_Label aCurLab = aRefs->Value(a);
1946 if (aCurLab.IsEqual(theCurrent)) {
1947 a += theReverse ? -1 : 1;
1948 if (a >= aRefs->Lower() && a <= aRefs->Upper()) {
1950 return aRefs->Value(a);
1952 break; // finish iteration: it's last feature
1959 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, int& theIndex, const bool theReverse)
1961 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
1962 if (aData.get() && aData->isValid()) {
1963 TDF_Label aFeatureLabel = aData->label().Father();
1965 TDF_Label aNextLabel = nextLabel(aFeatureLabel, theIndex, theReverse);
1966 if (aNextLabel.IsNull())
1967 break; // the last or something is wrong
1968 FeaturePtr aFound = feature(aNextLabel);
1970 return aFound; // the feature is found
1971 // if the next label is a folder, skip it
1972 aFeatureLabel = folder(aNextLabel).get() ? aNextLabel : TDF_Label();
1973 } while (!aFeatureLabel.IsNull());
1975 return FeaturePtr(); // not found, last, or something is wrong
1978 FeaturePtr Model_Objects::firstFeature()
1980 Handle(TDataStd_ReferenceArray) aRefs;
1981 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1982 return feature(aRefs->Value(aRefs->Lower()));
1984 return FeaturePtr(); // no features at all
1987 FeaturePtr Model_Objects::lastFeature()
1989 Handle(TDataStd_ReferenceArray) aRefs;
1990 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1991 // comment this because of #2674 - features are removed from array on deactivation of Part
1992 /*FeaturePtr aLast = feature(aRefs->Value(aRefs->Upper()));
1993 if (!aLast.get() && aRefs->Length() != 0) { // erase the invalid feature from the array
1994 RemoveFromRefArray(featuresLabel(), aRefs->Value(aRefs->Upper()));
1995 return lastFeature(); // try once again, after the last was removed
1997 return feature(aRefs->Value(aRefs->Upper()));
1999 return FeaturePtr(); // no features at all
2002 bool Model_Objects::isLater(FeaturePtr theLater, FeaturePtr theCurrent) const
2004 std::shared_ptr<Model_Data> aLaterD = std::static_pointer_cast<Model_Data>(theLater->data());
2005 std::shared_ptr<Model_Data> aCurrentD = std::static_pointer_cast<Model_Data>(theCurrent->data());
2006 if (aLaterD.get() && aLaterD->isValid() && aCurrentD.get() && aCurrentD->isValid()) {
2007 TDF_Label aLaterL = aLaterD->label().Father();
2008 TDF_Label aCurrentL = aCurrentD->label().Father();
2009 int aLaterI = -1, aCurentI = -1; // not found yet state
2010 Handle(TDataStd_ReferenceArray) aRefs;
2011 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2012 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
2013 TDF_Label aCurLab = aRefs->Value(a);
2014 if (aCurLab.IsEqual(aLaterL)) {
2016 } else if (aCurLab.IsEqual(aCurrentL)) {
2019 if (aLaterI != -1 && aCurentI != -1) // both are found
2020 return aLaterI > aCurentI;
2024 return false; // not found, or something is wrong
2027 std::list<std::shared_ptr<ModelAPI_Object> > Model_Objects::allObjects()
2029 std::list<std::shared_ptr<ModelAPI_Object> > aResult;
2030 Handle(TDataStd_ReferenceArray) aRefs;
2031 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2032 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
2033 ObjectPtr anObject = object(aRefs->Value(a));
2034 if (!anObject.get()) // is it a folder?
2035 anObject = folder(aRefs->Value(a));
2037 aResult.push_back(anObject);
2043 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
2045 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
2046 Handle(TDataStd_ReferenceArray) aRefs;
2047 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2048 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
2049 FeaturePtr aFeature = feature(aRefs->Value(a));
2051 aResult.push_back(aFeature);
2057 int Model_Objects::numInternalFeatures()
2059 Handle(TDataStd_ReferenceArray) aRefs;
2060 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2061 return aRefs->Upper() - aRefs->Lower() + 1;
2063 return 0; // invalid
2066 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
2068 Handle(TDataStd_ReferenceArray) aRefs;
2069 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2070 return feature(aRefs->Value(aRefs->Lower() + theIndex));
2072 return FeaturePtr(); // invalid
2075 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
2077 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
2080 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
2082 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);