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 <ModelAPI_Validator.h>
33 #include <ModelAPI_CompositeFeature.h>
34 #include <ModelAPI_Tools.h>
36 #include <Events_Loop.h>
37 #include <Events_InfoMessage.h>
39 #include <TDataStd_Integer.hxx>
40 #include <TDataStd_Comment.hxx>
41 #include <TDF_ChildIDIterator.hxx>
42 #include <TDataStd_ReferenceArray.hxx>
43 #include <TDataStd_HLabelArray1.hxx>
44 #include <TDataStd_Name.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 static const std::string& groupNameFoldering(const std::string& theGroupID,
52 const bool theAllowFolder)
55 static const std::string anOutOfFolderName = std::string("__") + ModelAPI_Feature::group();
56 static const std::string aDummyName;
57 return theGroupID == ModelAPI_Feature::group() ? anOutOfFolderName : aDummyName;
62 // Check theFeature is a first or last feature in folder and return this folder
63 static FolderPtr inFolder(const FeaturePtr& theFeature, const std::string& theFolderAttr)
65 const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
66 for (std::set<AttributePtr>::iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt) {
67 if ((*anIt)->id() != theFolderAttr)
70 ObjectPtr anOwner = (*anIt)->owner();
71 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(anOwner);
79 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
82 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
83 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
86 /// 0:1:2 - where features are located
87 /// 0:1:2:N:1 - data of the feature N
88 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
90 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
94 void Model_Objects::setOwner(DocumentPtr theDoc)
97 // update all fields and recreate features and result objects if needed
98 TDF_LabelList aNoUpdated;
99 synchronizeFeatures(aNoUpdated, true, false, true, true);
103 Model_Objects::~Model_Objects()
105 // delete all features of this document
106 Events_Loop* aLoop = Events_Loop::loop();
107 // erase one by one to avoid access from the feature destructor itself from he map
108 // blocks the flush signals to avoid the temporary objects visualization in the viewer
109 // they should not be shown in order to do not lose highlight by erasing them
110 bool isActive = aLoop->activateFlushes(false);
112 while(!myFeatures.IsEmpty()) {
113 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
114 FeaturePtr aFeature = aFeaturesIter.Value();
115 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
116 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
117 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
118 aFeature->removeResults(0, false);
120 myFeatures.UnBind(aFeaturesIter.Key());
122 while (!myFolders.IsEmpty()) {
123 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFoldersIter(myFolders);
124 ObjectPtr aFolder = aFoldersIter.Value();
125 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
126 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
127 ModelAPI_EventCreator::get()->sendUpdated(aFolder, EVENT_DISP);
129 myFolders.UnBind(aFoldersIter.Key());
132 aLoop->activateFlushes(isActive);
133 // erase update, because features are destroyed and update should not performed for them anywhere
134 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
135 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
136 // deleted and redisplayed is correctly performed: they know that features are destroyed
137 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
138 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
142 /// Appends to the array of references a new referenced label
143 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
145 Handle(TDataStd_ReferenceArray) aRefs;
146 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
147 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
148 aRefs->SetValue(0, theReferenced);
149 } else { // extend array by one more element
150 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
152 int aPassedPrev = 0; // prev feature is found and passed
153 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
154 aNewArray->SetValue(aRefs->Lower(), theReferenced);
157 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
158 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
159 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
161 aNewArray->SetValue(a + 1, theReferenced);
164 if (!aPassedPrev) // not found: unknown situation
165 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
166 aRefs->SetInternalArray(aNewArray);
170 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
172 if (!theFeature->isAction()) { // do not add action to the data model
173 TDF_Label aFeaturesLab = featuresLabel();
174 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
175 // store feature in the features array: before "initData" because in macro features
176 // in initData it creates new features, appeared later than this
177 TDF_Label aPrevFeateureLab;
178 FolderPtr aParentFolder;
179 if (theAfterThis.get()) { // searching for the previous feature label
180 std::shared_ptr<Model_Data> aPrevData =
181 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
182 if (aPrevData.get()) {
183 aPrevFeateureLab = aPrevData->label().Father();
185 // Check if the previous feature is the last feature in a folder,
186 // then the folder should be updated to contain additional feature.
187 // Macro features are not stored in folder.
188 if (!theFeature->isMacro()) {
189 // If the last feature is a sub-feature of composite, use parent feature
190 // to check belonging to a folder.
191 FeaturePtr afterThis = ModelAPI_Tools::compositeOwner(theAfterThis);
193 afterThis = theAfterThis;
194 aParentFolder = inFolder(afterThis, ModelAPI_Folder::LAST_FEATURE_ID());
197 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
199 // keep the feature ID to restore document later correctly
200 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
201 myFeatures.Bind(aFeatureLab, theFeature);
202 // must be before the event sending: for OB the feature is already added
203 updateHistory(ModelAPI_Feature::group());
204 // do not change the order:
207 // during python script with fillet constraint feature data should be
208 // initialized before using it in GUI
210 // must be after binding to the map because of "Box" macro feature that
211 // creates other features in "initData"
212 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
213 // put feature to the end of folder if it is added while
214 // the history line is set to the last feature from the folder
216 aParentFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(theFeature);
217 updateHistory(ModelAPI_Folder::group());
219 // event: feature is added, mist be before "initData" to update OB correctly on Duplicate:
220 // first new part, then the content
221 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
222 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
223 } else { // make feature has not-null data anyway
224 theFeature->setData(Model_Data::invalidData());
225 theFeature->setDoc(myDoc);
229 /// Appends to the array of references a new referenced label.
230 /// If theIndex is not -1, removes element at this index, not theReferenced.
231 /// \returns the index of removed element
232 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
233 const int theIndex = -1)
235 int aResult = -1; // no returned
236 Handle(TDataStd_ReferenceArray) aRefs;
237 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
238 if (aRefs->Length() == 1) { // just erase an array
239 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
240 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
243 } else { // reduce the array
244 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
246 int aCount = aRefs->Lower();
247 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
248 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
252 aNewArray->SetValue(aCount, aRefs->Value(a));
255 aRefs->SetInternalArray(aNewArray);
261 void Model_Objects::refsToFeature(FeaturePtr theFeature,
262 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
264 // check the feature: it must have no depended objects on it
265 // the dependencies can be in the feature results
266 std::list<ResultPtr> aResults;
267 ModelAPI_Tools::allResults(theFeature, aResults);
268 std::list<ResultPtr>::const_iterator aResIter = aResults.cbegin();
269 for (; aResIter != aResults.cend(); aResIter++) {
270 ResultPtr aResult = (*aResIter);
271 std::shared_ptr<Model_Data> aData =
272 std::dynamic_pointer_cast<Model_Data>(aResult->data());
273 if (aData.get() != NULL) {
274 const std::set<AttributePtr>& aRefs = aData->refsToMe();
275 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
276 for (; aRefIt != aRefLast; aRefIt++) {
277 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
278 if (aFeature.get() != NULL)
279 theRefs.insert(aFeature);
283 // the dependencies can be in the feature itself
284 std::shared_ptr<Model_Data> aData =
285 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
286 if (aData.get() && !aData->refsToMe().empty()) {
287 const std::set<AttributePtr>& aRefs = aData->refsToMe();
288 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
289 for (; aRefIt != aRefLast; aRefIt++) {
290 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
291 if (aFeature.get() != NULL)
292 theRefs.insert(aFeature);
296 if (!theRefs.empty() && isSendError) {
297 Events_InfoMessage("Model_Objects",
298 "Feature '%1' is used and can not be deleted").arg(theFeature->data()->name()).send();
302 void Model_Objects::removeFeature(FeaturePtr theFeature)
304 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
305 if (aData.get() && aData->isValid()) {
306 // checking that the sub-element of composite feature is removed: if yes, inform the owner
307 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
308 refsToFeature(theFeature, aRefs, false);
309 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
310 for(; aRefIter != aRefs.end(); aRefIter++) {
311 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
312 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
313 if (aComposite.get() && aComposite->isSub(theFeature)) {
314 aComposite->removeFeature(theFeature);
317 // remove feature from folder
318 removeFromFolder(std::list<FeaturePtr>(1, theFeature));
319 // this must be before erase since theFeature erasing removes all information about
320 // the feature results and groups of results
321 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
322 clearHistory(theFeature);
326 TDF_Label aFeatureLabel = aData->label().Father();
327 if (myFeatures.IsBound(aFeatureLabel))
328 myFeatures.UnBind(aFeatureLabel);
330 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
331 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
332 // erase all attributes under the label of feature
333 aFeatureLabel.ForgetAllAttributes();
334 // remove it from the references array
335 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
336 // event: feature is deleted
337 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
338 updateHistory(ModelAPI_Feature::group());
342 void Model_Objects::eraseAllFeatures()
344 static Events_ID kDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
345 static const ModelAPI_EventCreator* kCreator = ModelAPI_EventCreator::get();
346 // make all features invalid (like deleted)
347 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
348 for(; aFIter.More(); aFIter.Next()) {
349 FeaturePtr aFeature = aFIter.Value();
350 std::list<ResultPtr> aResList;
351 ModelAPI_Tools::allResults(aFeature, aResList);
352 std::list<ResultPtr>::iterator aRIter = aResList.begin();
353 for(; aRIter != aResList.end(); aRIter++) {
354 ResultPtr aRes = *aRIter;
355 if (aRes && aRes->data()->isValid()) {
356 kCreator->sendDeleted(myDoc, aRes->groupName());
357 kCreator->sendUpdated(aRes, kDispEvent);
358 aRes->setData(aRes->data()->invalidPtr());
362 kCreator->sendUpdated(aFeature, kDispEvent);
363 aFeature->setData(aFeature->data()->invalidPtr());
365 kCreator->sendDeleted(myDoc, ModelAPI_Feature::group());
366 myFeatures.Clear(); // just remove features without modification of DS
370 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
372 TDF_Label aFeaturesLab = featuresLabel();
373 Handle(TDataStd_ReferenceArray) aRefs;
374 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
376 TDF_Label anAfterLab, aMovedLab =
377 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
378 if (theAfterThis.get())
379 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
381 // check whether some folder refers to the moved feature by start or end: if yes, remove from it
382 removeFromFolder(std::list<FeaturePtr>(1, theMoved));
384 Handle(TDataStd_HLabelArray1) aNewArray =
385 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
386 int aPassedMovedFrom = 0; // the prev feature location is found and passed
387 int aPassedMovedTo = 0; // the feature is added and this location is passed
388 if (!theAfterThis.get()) { // null means that inserted feature must be the first
389 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
392 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
393 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
395 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
396 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
397 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
398 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
400 } else { // just copy one
401 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
402 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
405 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
406 if (!aPassedMovedFrom) {
407 static std::string aMovedFromError("The moved feature is not found");
408 Events_InfoMessage("Model_Objects", aMovedFromError).send();
410 static std::string aMovedToError("The 'after' feature for movement is not found");
411 Events_InfoMessage("Model_Objects", aMovedToError).send();
415 // store the new array
416 aRefs->SetInternalArray(aNewArray);
417 // update the feature and the history
418 clearHistory(theMoved);
419 // make sure all (selection) attributes of moved feature will be updated
420 static Events_ID kUpdateSelection = Events_Loop::loop()->eventByName(EVENT_UPDATE_SELECTION);
421 ModelAPI_EventCreator::get()->sendUpdated(theMoved, kUpdateSelection, false);
422 ModelAPI_EventCreator::get()->sendReordered(theMoved);
425 void Model_Objects::clearHistory(ObjectPtr theObj)
428 const std::string aGroup = theObj->groupName();
429 updateHistory(aGroup);
431 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
432 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
433 std::string aResultGroup = featureResultGroup(aFeature);
434 if (!aResultGroup.empty()) {
435 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
436 myHistory.find(aResultGroup);
437 if (aHIter != myHistory.end())
438 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
444 void Model_Objects::createHistory(const std::string& theGroupID)
446 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
447 if (aHIter == myHistory.end()) {
448 std::vector<ObjectPtr> aResult;
449 std::vector<ObjectPtr> aResultOutOfFolder;
450 FeaturePtr aLastFeatureInFolder;
451 // iterate the array of references and get feature by feature from the array
452 bool isFeature = theGroupID == ModelAPI_Feature::group();
453 bool isFolder = theGroupID == ModelAPI_Folder::group();
454 Handle(TDataStd_ReferenceArray) aRefs;
455 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
456 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
457 FeaturePtr aFeature = feature(aRefs->Value(a));
458 if (aFeature.get()) {
459 // if feature is in sub-component, remove it from history:
460 // it is in sub-tree of sub-component
461 bool isSub = ModelAPI_Tools::compositeOwner(aFeature).get() != NULL;
462 if (isFeature) { // here may be also disabled features
463 if (!isSub && aFeature->isInHistory()) {
464 aResult.push_back(aFeature);
465 // the feature is out of the folders
466 if (aLastFeatureInFolder.get() == NULL)
467 aResultOutOfFolder.push_back(aFeature);
469 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
470 // construction results of sub-features should not be in the tree
471 if (!isSub || theGroupID != ModelAPI_ResultConstruction::group()) {
472 // do not use reference to the list here since results can be changed by "isConcealed"
473 const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
474 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator
475 aRIter = aResults.begin();
476 for (; aRIter != aResults.cend(); aRIter++) {
477 ResultPtr aRes = *aRIter;
478 if (aRes->groupName() != theGroupID) break; // feature have only same group results
479 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
480 aResult.push_back(*aRIter);
486 // the feature closes the folder, so the next features will be treated as out-of-folder
487 if (aLastFeatureInFolder.get() && aLastFeatureInFolder == aFeature)
488 aLastFeatureInFolder = FeaturePtr();
491 // it may be a folder
492 const ObjectPtr& aFolder = folder(aRefs->Value(a));
494 // store folder information for the Features group only
495 if (isFeature || isFolder) {
496 aResult.push_back(aFolder);
498 aResultOutOfFolder.push_back(aFolder);
501 // get the last feature in the folder
502 AttributeReferencePtr aLastFeatAttr =
503 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
505 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
510 // to be sure that isConcealed did not update the history (issue 1089) during the iteration
511 if (myHistory.find(theGroupID) == myHistory.end()) {
512 myHistory[theGroupID] = aResult;
514 // store the features placed out of any folder
515 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroupID, true);
516 if (!anOutOfFolderGroupID.empty())
517 myHistory[anOutOfFolderGroupID] = aResultOutOfFolder;
522 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
524 clearHistory(theObject);
527 void Model_Objects::updateHistory(const std::string theGroup)
529 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
530 if (aHIter != myHistory.end()) {
531 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
533 // erase history for the group of objects placed out of any folder
534 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroup, true);
535 if (!anOutOfFolderGroupID.empty())
536 myHistory.erase(anOutOfFolderGroupID);
540 const ObjectPtr& Model_Objects::folder(TDF_Label theLabel) const
542 if (myFolders.IsBound(theLabel))
543 return myFolders.Find(theLabel);
544 static ObjectPtr anEmptyResult;
545 return anEmptyResult;
548 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
550 if (myFeatures.IsBound(theLabel))
551 return myFeatures.Find(theLabel);
552 return FeaturePtr(); // not found
555 ObjectPtr Model_Objects::object(TDF_Label theLabel)
557 // try feature by label
558 FeaturePtr aFeature = feature(theLabel);
560 return feature(theLabel);
561 TDF_Label aFeatureLabel = theLabel; // let's suppose it is result of this feature
562 TDF_LabelList aSubLabs; // sub - labels from higher level to lower level of result
563 while(!aFeature.get() && aFeatureLabel.Depth() > 1) {
564 aSubLabs.Prepend(aFeatureLabel);
565 aFeatureLabel = aFeatureLabel.Father().Father();
566 aFeature = feature(aFeatureLabel);
568 if (aFeature.get()) {
569 ResultPtr aCurrentResult;
570 // searching for results then sub-results label by label
571 for(TDF_ListIteratorOfLabelList aSubLab(aSubLabs); aSubLab.More(); aSubLab.Next()) {
572 if (aCurrentResult.get()) { // iterate sub-results of result
573 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCurrentResult);
575 return ObjectPtr(); // only Body can have sub-results
576 int a, aNumSubs = anOwner->numberOfSubs();
577 for(a = 0; a < aNumSubs; a++) {
578 ResultPtr aSub = anOwner->subResult(a);
580 std::shared_ptr<Model_Data> aSubData = std::dynamic_pointer_cast<Model_Data>(
582 const TDF_Label& aSubLabVal = aSubLab.ChangeValue();
583 if (aSubData->label().Father().IsEqual(aSubLabVal)) {
584 aCurrentResult = aSub;
589 if (a == aNumSubs) // not found an appropriate sub-result of result
591 } else { // iterate results of feature
592 const std::list<ResultPtr>& aResults = aFeature->results();
593 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
594 for(; aRIter != aResults.cend(); aRIter++) {
595 std::shared_ptr<Model_Data> aResData =
596 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
597 if (aResData->label().Father().IsEqual(aSubLab.ChangeValue())) {
598 aCurrentResult = *aRIter;
602 if (aRIter == aResults.cend()) // not found an appropriate result of feature
606 return aCurrentResult;
608 return ObjectPtr(); // not found
611 ObjectPtr Model_Objects::object(const std::string& theGroupID,
613 const bool theAllowFolder)
617 createHistory(theGroupID);
618 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
619 const std::vector<ObjectPtr>& aVec = myHistory[theGroupID];
620 //if (aVec.size() <= theIndex)
621 // return aVec[aVec.size() - 1]; // too high index requested (to avoid crash in #2360)
622 return aGroupID.empty() ? myHistory[theGroupID][theIndex] : myHistory[aGroupID][theIndex];
625 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
626 const std::string& theGroupID, const std::string& theName)
628 createHistory(theGroupID);
629 if (theGroupID == ModelAPI_Feature::group()) { // searching among features (in history or not)
630 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
631 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator anObjIter = allObjs.begin();
632 for(; anObjIter != allObjs.end(); anObjIter++) {
633 if ((*anObjIter)->data()->name() == theName)
636 } else { // searching among results (concealed or not)
637 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
638 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator anObjIter = allObjs.begin();
639 for(; anObjIter != allObjs.end(); anObjIter++) {
640 std::list<ResultPtr> allRes;
641 ModelAPI_Tools::allResults(*anObjIter, allRes);
642 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
643 if (aRes->get() && (*aRes)->groupName() == theGroupID) {
644 if ((*aRes)->data()->name() == theName)
654 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject,
655 const bool theAllowFolder)
657 std::string aGroup = theObject->groupName();
658 // treat folder as feature
659 if (aGroup == ModelAPI_Folder::group())
660 aGroup = ModelAPI_Feature::group();
661 createHistory(aGroup);
663 // get the group of features out of folder (if enabled)
664 if (theAllowFolder && !groupNameFoldering(aGroup, theAllowFolder).empty())
665 aGroup = groupNameFoldering(aGroup, theAllowFolder);
667 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
668 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
669 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
670 if ((*anObjIter) == theObject)
677 int Model_Objects::size(const std::string& theGroupID, const bool theAllowFolder)
679 createHistory(theGroupID);
680 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
681 return aGroupID.empty() ? int(myHistory[theGroupID].size()) : int(myHistory[aGroupID].size());
684 std::shared_ptr<ModelAPI_Object> Model_Objects::parent(
685 const std::shared_ptr<ModelAPI_Object> theChild)
687 if (theChild.get()) {
688 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theChild->data());
689 TDF_Label aLab = aData->label();
690 if (!aLab.IsNull() && aLab.Depth() > 2) {
691 ObjectPtr anObj = object(aLab.Father().Father().Father());
699 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
701 // iterate the array of references and get feature by feature from the array
702 Handle(TDataStd_ReferenceArray) aRefs;
703 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
704 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
705 FeaturePtr aFeature = feature(aRefs->Value(a));
706 if (aFeature.get()) {
707 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
708 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
709 for (; aRIter != aResults.cend(); aRIter++) {
710 ResultPtr aRes = *aRIter;
711 if (aRes->groupName() != theGroupID) break; // feature have only same group results
712 // iterate also concealed: ALL RESULTS (for translation parts undo/redo management)
713 //if (aRes->isInHistory() && !aRes->isConcealed()) {
714 theResults.push_back(*aRIter);
723 TDF_Label Model_Objects::featuresLabel() const
725 return myMain.FindChild(TAG_OBJECTS);
728 static std::string composeName(const std::string& theFeatureKind, const int theIndex)
730 std::stringstream aNameStream;
731 aNameStream << theFeatureKind << "_" << theIndex;
732 return aNameStream.str();
735 void Model_Objects::setUniqueName(FeaturePtr theFeature)
737 if (!theFeature->data()->name().empty())
738 return; // not needed, name is already defined
739 std::string aName; // result
740 // first count all features of such kind to start with index = count + 1
741 int aNumObjects = -1; // this feature is already in this map
742 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
743 for (; aFIter.More(); aFIter.Next()) {
744 if (aFIter.Value()->getKind() == theFeature->getKind())
747 // generate candidate name
748 aName = composeName(theFeature->getKind(), aNumObjects + 1);
749 // check this is unique, if not, increase index by 1
750 for (aFIter.Initialize(myFeatures); aFIter.More();) {
751 FeaturePtr aFeature = aFIter.Value();
752 bool isSameName = aFeature->data()->name() == aName;
753 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
754 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
755 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
756 for (; aRIter != aResults.cend(); aRIter++) {
757 isSameName = (*aRIter)->data()->name() == aName;
763 aName = composeName(theFeature->getKind(), aNumObjects + 1);
764 // reinitialize iterator to make sure a new name is unique
765 aFIter.Initialize(myFeatures);
769 theFeature->data()->setName(aName);
772 void Model_Objects::setUniqueName(FolderPtr theFolder)
774 if (!theFolder->name().empty())
775 return; // name is already defined
777 int aNbFolders = myFolders.Size();
778 std::string aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
780 // check the uniqueness of the name
781 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator anIt(myFolders);
782 while (anIt.More()) {
783 if (anIt.Value()->data()->name() == aName) {
784 aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
785 // reinitialize iterator to make sure a new name is unique
786 anIt.Initialize(myFolders);
791 theFolder->data()->setName(aName);
794 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
796 std::shared_ptr<Model_Data> aData(new Model_Data);
797 aData->setLabel(theLab.FindChild(theTag));
798 aData->setObject(theObj);
799 theObj->setDoc(myDoc);
800 theObj->setData(aData);
801 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
802 if (aFeature.get()) {
803 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
804 } else { // is it a folder?
805 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theObj);
807 setUniqueName(aFolder);
809 theObj->initAttributes();
812 std::shared_ptr<ModelAPI_Feature> Model_Objects::featureById(const int theId)
815 TDF_Label aLab = featuresLabel().FindChild(theId, Standard_False);
816 return feature(aLab);
818 return std::shared_ptr<ModelAPI_Feature>(); // not found
821 void Model_Objects::synchronizeFeatures(
822 const TDF_LabelList& theUpdated, const bool theUpdateReferences,
823 const bool theExecuteFeatures, const bool theOpen, const bool theFlush)
825 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
826 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
828 // after all updates, sends a message that groups of features were created or updated
829 Events_Loop* aLoop = Events_Loop::loop();
830 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
831 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
832 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
833 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
834 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
835 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
836 bool isActive = aLoop->activateFlushes(false);
838 // collect all updated labels map
839 TDF_LabelMap anUpdatedMap;
840 TDF_ListIteratorOfLabelList anUpdatedIter(theUpdated);
841 for(; anUpdatedIter.More(); anUpdatedIter.Next()) {
842 TDF_Label& aFeatureLab = anUpdatedIter.Value();
843 while(aFeatureLab.Depth() > 3)
844 aFeatureLab = aFeatureLab.Father();
845 if (myFeatures.IsBound(aFeatureLab) || myFolders.IsBound(aFeatureLab))
846 anUpdatedMap.Add(aFeatureLab);
849 // update all objects by checking are they on labels or not
850 std::set<ObjectPtr> aNewFeatures, aKeptFeatures;
851 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
852 for (; aLabIter.More(); aLabIter.Next()) {
853 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
854 if (!myFeatures.IsBound(aFeatureLabel) && !myFolders.IsBound(aFeatureLabel)) {
855 // a new feature or folder is inserted
857 std::string aFeatureID = TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
858 aLabIter.Value())->Get()).ToCString();
859 bool isFolder = aFeatureID == ModelAPI_Folder::ID();
861 std::shared_ptr<Model_Session> aSession =
862 std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get());
865 ObjectPtr aFeature = isFolder ? ObjectPtr(new ModelAPI_Folder)
866 : ObjectPtr(aSession->createFeature(aFeatureID, anOwner));
867 if (!aFeature.get()) {
868 // somethig is wrong, most probably, the opened document has invalid structure
869 Events_InfoMessage("Model_Objects", "Invalid type of object in the document").send();
870 aLabIter.Value()->Label().ForgetAllAttributes();
874 // this must be before "setData" to redo the sketch line correctly
876 myFolders.Bind(aFeatureLabel, aFeature);
878 myFeatures.Bind(aFeatureLabel, std::dynamic_pointer_cast<ModelAPI_Feature>(aFeature));
879 aNewFeatures.insert(aFeature);
880 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
881 updateHistory(aFeature);
883 // event: model is updated
884 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
885 } else { // nothing is changed, both iterators are incremented
888 if (myFeatures.Find(aFeatureLabel, aFeature)) {
889 aKeptFeatures.insert(aFeature);
892 if (myFolders.Find(aFeatureLabel, anObject))
893 aKeptFeatures.insert(anObject);
895 if (anUpdatedMap.Contains(aFeatureLabel)) {
896 if (!theOpen) { // on abort/undo/redo reinitialize attributes if something is changed
897 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
898 anObject->data()->attributes("");
899 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
900 for(; anAttr != anAttrs.end(); anAttr++)
903 ModelAPI_EventCreator::get()->sendUpdated(anObject, anUpdateEvent);
904 if (aFeature && aFeature->getKind() == "Parameter") {
905 // if parameters are changed, update the results (issue 937)
906 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
907 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
908 for (; aRIter != aResults.cend(); aRIter++) {
909 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
910 if (aRes->data()->isValid() && !aRes->isDisabled()) {
911 ModelAPI_EventCreator::get()->sendUpdated(aRes, anUpdateEvent);
919 // check all features are checked: if not => it was removed
920 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
921 while (aFIter.More()) {
922 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
923 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
924 FeaturePtr aFeature = aFIter.Value();
925 // event: model is updated
926 //if (aFeature->isInHistory()) {
927 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
929 // results of this feature must be redisplayed (hided)
930 // redisplay also removed feature (used for sketch and AISObject)
931 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
932 updateHistory(aFeature);
935 // unbind after the "erase" call: on abort sketch
936 // is removes sub-objects that corrupts aFIter
937 myFeatures.UnBind(aFIter.Key());
938 // reinitialize iterator because unbind may corrupt the previous order in the map
939 aFIter.Initialize(myFeatures);
943 // verify folders are checked: if not => is was removed
944 for (NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFldIt(myFolders);
945 aFldIt.More(); aFldIt.Next()) {
946 ObjectPtr aCurObj = aFldIt.Value();
947 if (aKeptFeatures.find(aCurObj) == aKeptFeatures.end() &&
948 aNewFeatures.find(aCurObj) == aNewFeatures.end()) {
949 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
950 // results of this feature must be redisplayed (hided)
951 // redisplay also removed feature (used for sketch and AISObject)
952 ModelAPI_EventCreator::get()->sendUpdated(aCurObj, aRedispEvent);
953 updateHistory(aCurObj);
956 // unbind after the "erase" call: on abort sketch
957 // is removes sub-objects that corrupts aFIter
958 myFolders.UnBind(aFldIt.Key());
959 // reinitialize iterator because unbind may corrupt the previous order in the map
960 aFldIt.Initialize(myFolders);
964 if (theUpdateReferences) {
965 synchronizeBackRefs();
967 // update results of the features (after features created because
968 // they may be connected, like sketch and sub elements)
969 // After synchronisation of back references because sketch
970 // must be set in sub-elements before "execute" by updateResults
971 std::set<FeaturePtr> aProcessed; // composites must be updated after their subs (issue 360)
972 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
973 for (; aLabIter2.More(); aLabIter2.Next()) {
974 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
975 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
976 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
977 updateResults(aFeature, aProcessed);
980 // the synchronize should be done after updateResults
981 // in order to correct back references of updated results
982 if (theUpdateReferences) {
983 synchronizeBackRefs();
985 if (!theUpdated.IsEmpty()) {
986 // this means there is no control what was modified => remove history cash
990 if (!theExecuteFeatures)
991 anOwner->setExecuteFeatures(false);
992 aLoop->activateFlushes(isActive);
995 aLoop->flush(aDeleteEvent);
996 // delete should be emitted before create to reacts to aborted feature
997 aLoop->flush(aCreateEvent);
998 aLoop->flush(anUpdateEvent);
999 aLoop->flush(aCreateEvent); // after update of features, there could be results created
1000 aLoop->flush(aDeleteEvent); // or deleted
1001 aLoop->flush(aRedispEvent);
1002 aLoop->flush(aToHideEvent);
1004 if (!theExecuteFeatures)
1005 anOwner->setExecuteFeatures(true);
1008 /// synchronises back references for the given object basing on the collected data
1009 void Model_Objects::synchronizeBackRefsForObject(const std::set<AttributePtr>& theNewRefs,
1010 ObjectPtr theObject)
1012 if (!theObject.get() || !theObject->data()->isValid())
1014 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
1015 // iterate new list to compare with curent
1016 std::set<AttributePtr>::iterator aNewIter = theNewRefs.begin();
1017 for(; aNewIter != theNewRefs.end(); aNewIter++) {
1018 if (aData->refsToMe().find(*aNewIter) == aData->refsToMe().end()) {
1019 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aNewIter)->owner());
1021 aData->addBackReference(aRefFeat, (*aNewIter)->id());
1022 else // add back reference to a folder
1023 aData->addBackReference((*aNewIter)->owner(), (*aNewIter)->id());
1026 if (theNewRefs.size() != aData->refsToMe().size()) { // some back ref must be removed
1027 std::set<AttributePtr>::iterator aCurrentIter = aData->refsToMe().begin();
1028 while(aCurrentIter != aData->refsToMe().end()) {
1029 if (theNewRefs.find(*aCurrentIter) == theNewRefs.end()) {
1030 // for external references from other documents this system
1031 // is not working: refs are collected from
1032 // different Model_Objects, so before remove check this
1033 // external object exists and still referenced
1034 bool aLeaveIt = false;
1035 if ((*aCurrentIter)->owner().get() && (*aCurrentIter)->owner()->document() != myDoc &&
1036 (*aCurrentIter)->owner()->data().get() && (*aCurrentIter)->owner()->data()->isValid()) {
1037 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
1038 (*aCurrentIter)->owner()->data()->referencesToObjects(aRefs);
1039 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> >>>::iterator
1040 aRefIter = aRefs.begin();
1041 for(; aRefIter != aRefs.end(); aRefIter++) {
1042 if ((*aCurrentIter)->id() == aRefIter->first) {
1043 std::list<std::shared_ptr<ModelAPI_Object> >::iterator anOIt;
1044 for(anOIt = aRefIter->second.begin(); anOIt != aRefIter->second.end(); anOIt++) {
1045 if (*anOIt == theObject) {
1053 aData->removeBackReference(*aCurrentIter);
1054 aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
1055 } else aCurrentIter++;
1056 } else aCurrentIter++;
1059 // for the last feature in the folder, check if it is a sub-feature,
1060 // then refer the folder to a top-level parent composite feature
1061 const std::set<AttributePtr>& aRefs = aData->refsToMe();
1062 std::set<AttributePtr>::iterator anIt = aRefs.begin();
1063 for (; anIt != aRefs.end(); ++anIt)
1064 if ((*anIt)->id() == ModelAPI_Folder::LAST_FEATURE_ID())
1066 if (anIt != aRefs.end()) {
1067 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1069 CompositeFeaturePtr aParent;
1070 CompositeFeaturePtr aGrandParent = ModelAPI_Tools::compositeOwner(aFeature);
1072 aParent = aGrandParent;
1074 aGrandParent = ModelAPI_Tools::compositeOwner(aParent);
1075 } while (aGrandParent.get());
1077 ObjectPtr aFolder = (*anIt)->owner();
1078 // remove reference from the current feature
1079 aData->removeBackReference(aFolder, ModelAPI_Folder::LAST_FEATURE_ID());
1080 // set reference to a top-level parent
1081 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aParent);
1082 std::shared_ptr<Model_Data> aParentData =
1083 std::dynamic_pointer_cast<Model_Data>(aParent->data());
1084 aParentData->addBackReference(aFolder, ModelAPI_Folder::LAST_FEATURE_ID());
1088 aData->updateConcealmentFlag();
1091 static void collectReferences(std::shared_ptr<ModelAPI_Data> theData,
1092 std::map<ObjectPtr, std::set<AttributePtr> >& theRefs)
1094 if (theData.get()) {
1095 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1096 theData->referencesToObjects(aRefs);
1097 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIt = aRefs.begin();
1098 for(; aRefsIt != aRefs.end(); aRefsIt++) {
1099 std::list<ObjectPtr>::iterator aRefTo = aRefsIt->second.begin();
1100 for(; aRefTo != aRefsIt->second.end(); aRefTo++) {
1102 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = theRefs.find(*aRefTo);
1103 if (aFound == theRefs.end()) {
1104 theRefs[*aRefTo] = std::set<AttributePtr>();
1105 aFound = theRefs.find(*aRefTo);
1107 aFound->second.insert(theData->attribute(aRefsIt->first));
1114 void Model_Objects::synchronizeBackRefs()
1116 // collect all back references in the separated container: to update everything at once,
1117 // without additional Concealment switchin on and off: only the final modification
1119 // referenced (slave) objects to referencing attirbutes
1120 std::map<ObjectPtr, std::set<AttributePtr> > allRefs;
1121 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
1122 for(; aFeatures.More(); aFeatures.Next()) {
1123 FeaturePtr aFeature = aFeatures.Value();
1124 collectReferences(aFeature->data(), allRefs);
1126 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFolders(myFolders);
1127 for(; aFolders.More(); aFolders.Next()) {
1128 ObjectPtr aFolder = aFolders.Value();
1129 collectReferences(aFolder->data(), allRefs);
1131 // second iteration: just compare back-references with existing in features and results
1132 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1133 FeaturePtr aFeature = aFeatures.Value();
1134 static std::set<AttributePtr> anEmpty;
1135 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(aFeature);
1136 if (aFound == allRefs.end()) { // not found => erase all back references
1137 synchronizeBackRefsForObject(anEmpty, aFeature);
1139 synchronizeBackRefsForObject(aFound->second, aFeature);
1140 allRefs.erase(aFound); // to check that all refs are counted
1143 std::list<ResultPtr> aResults;
1144 ModelAPI_Tools::allResults(aFeature, aResults);
1145 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1146 for(; aRIter != aResults.cend(); aRIter++) {
1147 aFound = allRefs.find(*aRIter);
1148 if (aFound == allRefs.end()) { // not found => erase all back references
1149 synchronizeBackRefsForObject(anEmpty, *aRIter);
1151 synchronizeBackRefsForObject(aFound->second, *aRIter);
1152 allRefs.erase(aFound); // to check that all refs are counted
1156 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1157 FeaturePtr aFeature = aFeatures.Value();
1158 std::list<ResultPtr> aResults;
1159 ModelAPI_Tools::allResults(aFeature, aResults);
1160 // update the concealment status for disply in isConcealed of ResultBody
1161 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1162 for(; aRIter != aResults.cend(); aRIter++) {
1163 (*aRIter)->isConcealed();
1166 // the rest all refs means that feature references to the external document feature:
1167 // process also them
1168 std::map<ObjectPtr, std::set<AttributePtr> >::iterator anExtIter = allRefs.begin();
1169 for(; anExtIter != allRefs.end(); anExtIter++) {
1170 synchronizeBackRefsForObject(anExtIter->second, anExtIter->first);
1174 TDF_Label Model_Objects::resultLabel(
1175 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1177 const std::shared_ptr<Model_Data>& aData =
1178 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1179 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1182 bool Model_Objects::hasCustomName(DataPtr theFeatureData,
1183 ResultPtr theResult,
1185 std::string& theParentName) const
1187 ResultBodyPtr aBodyRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theFeatureData->owner());
1189 // only for top-results (works for the cases when results are not yet added to the feature)
1190 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult);
1192 // names of sub-solids in CompSolid should be default (for example,
1193 // result of boolean operation 'Boolean_1' is a CompSolid which is renamed to 'MyBOOL',
1194 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1', 'Boolean_1_2' etc.)
1195 if (std::dynamic_pointer_cast<Model_Data>(aBodyRes->data())->label().Depth() == 6) {
1196 std::ostringstream aDefaultName;
1197 // compute default name of CompSolid (name of feature + index of CompSolid's result)
1198 int aBodyResultIndex = 0;
1199 const std::list<ResultPtr>& aResults = anOwner->results();
1200 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
1201 for (; anIt != aResults.end(); ++anIt, ++aBodyResultIndex)
1202 if (aBodyRes == *anIt)
1204 aDefaultName << anOwner->name();
1205 aDefaultName << "_" << (aBodyResultIndex + 1);
1206 theParentName = aDefaultName.str();
1207 } else { // just name of the parent result if it is deeper than just a sub-result
1208 theParentName = aBodyRes->data()->name();
1213 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(theResult);
1215 theParentName = aName.first;
1216 return aName.second;
1219 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1220 std::shared_ptr<ModelAPI_Result> theResult,
1221 const int theResultIndex)
1224 theResult->setDoc(myDoc);
1225 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1226 if (theResult->data()->name().empty()) {
1227 // if was not initialized, generate event and set a name
1228 std::string aNewName = theFeatureData->name();
1229 if (hasCustomName(theFeatureData, theResult, theResultIndex, aNewName)) {
1230 // if the name of result is user-defined, then, at first time, assign name of the result
1231 // by empty string to be sure that corresponding flag in the data model is set
1232 theResult->data()->setName("");
1234 std::stringstream aName;
1236 // if there are several results (issue #899: any number of result),
1237 // add unique prefix starting from second
1238 if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
1239 aName << "_" << theResultIndex + 1;
1240 aNewName = aName.str();
1242 theResult->data()->setName(aNewName);
1246 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
1247 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1249 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1250 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1251 ObjectPtr anOldObject = object(aLab);
1252 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1253 if (anOldObject.get()) {
1254 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1256 if (!aResult.get()) {
1257 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1258 storeResult(theFeatureData, aResult, theIndex);
1263 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
1264 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1266 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1267 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1268 ObjectPtr anOldObject = object(aLab);
1269 std::shared_ptr<ModelAPI_ResultBody> aResult;
1270 if (anOldObject.get()) {
1271 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1273 if (!aResult.get()) {
1274 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1275 storeResult(theFeatureData, aResult, theIndex);
1280 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
1281 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1283 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1284 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1285 ObjectPtr anOldObject = object(aLab);
1286 std::shared_ptr<ModelAPI_ResultPart> aResult;
1287 if (anOldObject.get()) {
1288 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1290 if (!aResult.get()) {
1291 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1292 storeResult(theFeatureData, aResult, theIndex);
1297 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
1298 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
1299 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1301 std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
1302 aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
1306 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
1307 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1309 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1310 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1311 ObjectPtr anOldObject = object(aLab);
1312 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1313 if (anOldObject.get()) {
1314 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1316 if (!aResult.get()) {
1317 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1318 storeResult(theFeatureData, aResult, theIndex);
1323 std::shared_ptr<ModelAPI_ResultField> Model_Objects::createField(
1324 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1326 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1327 TDataStd_Comment::Set(aLab, ModelAPI_ResultField::group().c_str());
1328 ObjectPtr anOldObject = object(aLab);
1329 std::shared_ptr<ModelAPI_ResultField> aResult;
1330 if (anOldObject.get()) {
1331 aResult = std::dynamic_pointer_cast<ModelAPI_ResultField>(anOldObject);
1333 if (!aResult.get()) {
1334 aResult = std::shared_ptr<ModelAPI_ResultField>(new Model_ResultField(theFeatureData));
1335 storeResult(theFeatureData, aResult, theIndex);
1340 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
1341 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1343 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1344 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1345 ObjectPtr anOldObject = object(aLab);
1346 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1347 if (anOldObject.get()) {
1348 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1350 if (!aResult.get()) {
1351 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1352 storeResult(theFeatureData, aResult, theIndex);
1357 std::shared_ptr<ModelAPI_Folder> Model_Objects::createFolder(
1358 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis)
1360 FolderPtr aFolder(new ModelAPI_Folder);
1364 TDF_Label aFeaturesLab = featuresLabel();
1365 TDF_Label aFolderLab = aFeaturesLab.NewChild();
1366 // store feature in the features array: before "initData" because in macro features
1367 // in initData it creates new features, appeared later than this
1368 TDF_Label aPrevFeatureLab;
1369 if (theBeforeThis.get()) { // searching for the previous feature label
1370 std::shared_ptr<Model_Data> aPrevData =
1371 std::dynamic_pointer_cast<Model_Data>(theBeforeThis->data());
1372 if (aPrevData.get()) {
1373 aPrevFeatureLab = nextLabel(aPrevData->label().Father(), true);
1375 } else { // find the label of the last feature
1376 Handle(TDataStd_ReferenceArray) aRefs;
1377 if (aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1378 aPrevFeatureLab = aRefs->Value(aRefs->Upper());
1380 AddToRefArray(aFeaturesLab, aFolderLab, aPrevFeatureLab);
1382 // keep the feature ID to restore document later correctly
1383 TDataStd_Comment::Set(aFolderLab, ModelAPI_Folder::ID().c_str());
1384 myFolders.Bind(aFolderLab, aFolder);
1385 // must be before the event sending: for OB the feature is already added
1386 updateHistory(ModelAPI_Folder::group());
1387 updateHistory(ModelAPI_Feature::group());
1389 // must be after binding to the map because of "Box" macro feature that
1390 // creates other features in "initData"
1391 initData(aFolder, aFolderLab, TAG_FEATURE_ARGUMENTS);
1392 // event: folder is added, must be before "initData" to update OB correctly on Duplicate:
1393 // first new part, then the content
1394 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1395 ModelAPI_EventCreator::get()->sendUpdated(aFolder, anEvent);
1400 void Model_Objects::removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder)
1402 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFolder->data());
1403 if (!aData.get() || !aData->isValid())
1406 // this must be before erase since theFolder erasing removes all information about it
1407 clearHistory(theFolder);
1411 TDF_Label aFolderLabel = aData->label().Father();
1412 if (myFolders.IsBound(aFolderLabel))
1413 myFolders.UnBind(aFolderLabel);
1415 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1416 ModelAPI_EventCreator::get()->sendUpdated(theFolder, EVENT_DISP);
1417 // erase all attributes under the label of feature
1418 aFolderLabel.ForgetAllAttributes();
1419 // remove it from the references array
1420 RemoveFromRefArray(featuresLabel(), aFolderLabel);
1421 // event: feature is deleted
1422 ModelAPI_EventCreator::get()->sendDeleted(theFolder->document(), ModelAPI_Folder::group());
1423 updateHistory(ModelAPI_Folder::group());
1424 updateHistory(ModelAPI_Feature::group());
1427 // Returns one of the limiting features of the list
1428 static FeaturePtr limitingFeature(std::list<FeaturePtr>& theFeatures, const bool isLast)
1430 FeaturePtr aFeature;
1432 aFeature = theFeatures.back();
1433 theFeatures.pop_back();
1435 aFeature = theFeatures.front();
1436 theFeatures.pop_front();
1441 // Verify the feature is sub-element in composite feature or it is not used in the history
1442 static bool isSkippedFeature(FeaturePtr theFeature)
1444 bool isSub = ModelAPI_Tools::compositeOwner(theFeature).get() != NULL;
1445 return isSub || (theFeature && !theFeature->isInHistory());
1448 std::shared_ptr<ModelAPI_Folder> Model_Objects::findFolder(
1449 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1450 const bool theBelow)
1452 if (theFeatures.empty())
1453 return FolderPtr(); // nothing to move
1455 TDF_Label aFeaturesLab = featuresLabel();
1456 Handle(TDataStd_ReferenceArray) aRefs;
1457 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1458 return FolderPtr(); // no reference array (something is wrong)
1460 std::list<std::shared_ptr<ModelAPI_Feature> > aFeatures = theFeatures;
1461 std::shared_ptr<ModelAPI_Feature> aLimitingFeature = limitingFeature(aFeatures, theBelow);
1463 std::shared_ptr<Model_Data> aData =
1464 std::static_pointer_cast<Model_Data>(aLimitingFeature->data());
1465 if (!aData || !aData->isValid())
1466 return FolderPtr(); // invalid feature
1468 // label of the first feature in the list for fast searching
1469 TDF_Label aFirstFeatureLabel = aData->label().Father();
1471 // find a folder above the features and
1472 // check the given features represent a sequential list of objects following the folder
1473 FolderPtr aFoundFolder;
1474 TDF_Label aLastFeatureInFolder;
1475 int aRefIndex = aRefs->Lower();
1476 for(; aRefIndex <= aRefs->Upper(); ++aRefIndex) { // iterate all existing features
1477 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1478 if (IsEqual(aCurLabel, aFirstFeatureLabel))
1479 break; // no need to continue searching
1481 // searching the folder below, just continue to search last feature from the list
1485 // if feature is in sub-component, skip it
1486 FeaturePtr aCurFeature = feature(aCurLabel);
1487 if (isSkippedFeature(aCurFeature))
1490 if (!aLastFeatureInFolder.IsNull()) {
1491 if (IsEqual(aCurLabel, aLastFeatureInFolder))
1492 aLastFeatureInFolder.Nullify(); // the last feature in the folder is achived
1496 const ObjectPtr& aFolderObj = folder(aCurLabel);
1497 if (aFolderObj.get()) {
1498 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aFolderObj);
1499 AttributeReferencePtr aLastFeatAttr =
1500 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1501 if (aLastFeatAttr) {
1502 // setup iterating inside a folder to find last feature
1503 ObjectPtr aLastFeature = aLastFeatAttr->value();
1505 aData = std::static_pointer_cast<Model_Data>(aLastFeature->data());
1506 if (aData && aData->isValid())
1507 aLastFeatureInFolder = aData->label().Father();
1513 if (theBelow && aRefIndex < aRefs->Upper()) {
1515 // skip following features which are sub-components or not in history
1516 for (int anIndex = aRefIndex + 1; anIndex <= aRefs->Upper(); ++anIndex) {
1517 aLabel = aRefs->Value(anIndex);
1518 FeaturePtr aCurFeature = feature(aLabel);
1519 if (!isSkippedFeature(aCurFeature))
1522 // check the next object is a folder
1523 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(folder(aLabel));
1526 if (!aLastFeatureInFolder.IsNull() || // the last feature of the folder above is not found
1530 // check the given features are sequential list
1531 int aStep = theBelow ? -1 : 1;
1532 for (aRefIndex += aStep;
1533 !aFeatures.empty() && aRefIndex >= aRefs->Lower() && aRefIndex <= aRefs->Upper();
1534 aRefIndex += aStep) {
1535 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1536 // if feature is in sub-component, skip it
1537 FeaturePtr aCurFeature = feature(aCurLabel);
1538 if (isSkippedFeature(aCurFeature))
1541 aLimitingFeature = limitingFeature(aFeatures, theBelow);
1542 if (!aCurFeature->data()->isEqual(aLimitingFeature->data()))
1543 return FolderPtr(); // not a sequential list
1546 return aFoundFolder;
1549 bool Model_Objects::moveToFolder(
1550 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1551 const std::shared_ptr<ModelAPI_Folder>& theFolder)
1553 if (theFeatures.empty() || !theFolder)
1556 // labels for the folder and last feature in the list
1557 TDF_Label aFolderLabel, aLastFeatureLabel;
1558 std::shared_ptr<Model_Data> aData =
1559 std::static_pointer_cast<Model_Data>(theFolder->data());
1560 if (aData && aData->isValid())
1561 aFolderLabel = aData->label().Father();
1562 aData = std::static_pointer_cast<Model_Data>(theFeatures.back()->data());
1563 if (aData && aData->isValid())
1564 aLastFeatureLabel = aData->label().Father();
1566 if (aFolderLabel.IsNull() || aLastFeatureLabel.IsNull())
1569 AttributeReferencePtr aFirstFeatAttr =
1570 theFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1571 AttributeReferencePtr aLastFeatAttr =
1572 theFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1573 bool initFirstAttr = !aFirstFeatAttr->value().get();
1574 bool initLastAttr = !aLastFeatAttr->value().get();
1576 // check the folder is below the list of features
1577 bool isFolderBelow = false;
1578 TDF_Label aFeaturesLab = featuresLabel();
1579 Handle(TDataStd_ReferenceArray) aRefs;
1580 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1581 return false; // no reference array (something is wrong)
1582 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1583 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1584 if (aCurLabel == aFolderLabel)
1585 break; // folder is above the features
1586 else if (aCurLabel == aLastFeatureLabel) {
1587 isFolderBelow = true;
1592 if (isFolderBelow) {
1593 aData = std::static_pointer_cast<Model_Data>(theFeatures.front()->data());
1594 if (!aData || !aData->isValid())
1596 TDF_Label aPrevFeatureLabel = aData->label().Father();
1597 // label of the feature before the first feature in the list
1598 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex)
1599 if (aPrevFeatureLabel == aRefs->Value(aRefIndex)) {
1600 if (aRefIndex == aRefs->Lower())
1601 aPrevFeatureLabel.Nullify();
1603 aPrevFeatureLabel = aRefs->Value(aRefIndex - 1);
1607 // move the folder in the list of references before the first feature
1608 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1609 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1610 // update first feature of the folder
1611 initFirstAttr = true;
1613 // update last feature of the folder
1614 initLastAttr = true;
1618 aFirstFeatAttr->setValue(theFeatures.front());
1620 aLastFeatAttr->setValue(theFeatures.back());
1622 updateHistory(ModelAPI_Feature::group());
1626 static FolderPtr isExtractionCorrect(const FolderPtr& theFirstFeatureFolder,
1627 const FolderPtr& theLastFeatureFolder,
1628 bool& isExtractBefore)
1630 if (theFirstFeatureFolder.get()) {
1631 if (theLastFeatureFolder.get())
1632 return theFirstFeatureFolder == theLastFeatureFolder ? theFirstFeatureFolder : FolderPtr();
1634 isExtractBefore = true;
1635 return theFirstFeatureFolder;
1636 } else if (theLastFeatureFolder.get()) {
1637 isExtractBefore = false;
1638 return theLastFeatureFolder;
1644 bool Model_Objects::removeFromFolder(
1645 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1646 const bool theBefore)
1648 if (theFeatures.empty())
1651 FolderPtr aFirstFeatureFolder =
1652 inFolder(theFeatures.front(), ModelAPI_Folder::FIRST_FEATURE_ID());
1653 FolderPtr aLastFeatureFolder =
1654 inFolder(theFeatures.back(), ModelAPI_Folder::LAST_FEATURE_ID());
1656 bool isExtractBeforeFolder = theBefore;
1657 FolderPtr aFoundFolder =
1658 isExtractionCorrect(aFirstFeatureFolder, aLastFeatureFolder, isExtractBeforeFolder);
1660 return false; // list of features cannot be extracted
1662 // references of the current folder
1663 ObjectPtr aFolderStartFeature;
1664 ObjectPtr aFolderEndFeature;
1665 if (aFirstFeatureFolder != aLastFeatureFolder) {
1666 aFolderStartFeature = aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->value();
1667 aFolderEndFeature = aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->value();
1670 FeaturePtr aFeatureToFind = isExtractBeforeFolder ? theFeatures.back() : theFeatures.front();
1671 std::shared_ptr<Model_Data> aData =
1672 std::static_pointer_cast<Model_Data>(aFeatureToFind->data());
1673 if (!aData || !aData->isValid())
1675 TDF_Label aLabelToFind = aData->label().Father();
1677 // search the label in the list of references
1678 TDF_Label aFeaturesLab = featuresLabel();
1679 Handle(TDataStd_ReferenceArray) aRefs;
1680 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1681 return false; // no reference array (something is wrong)
1682 int aRefIndex = aRefs->Lower();
1683 for (; aRefIndex <= aRefs->Upper(); ++aRefIndex)
1684 if (aRefs->Value(aRefIndex) == aLabelToFind)
1687 // update folder position
1688 if (isExtractBeforeFolder) {
1689 aData = std::dynamic_pointer_cast<Model_Data>(aFoundFolder->data());
1690 TDF_Label aFolderLabel = aData->label().Father();
1691 TDF_Label aPrevFeatureLabel = aRefs->Value(aRefIndex);
1692 // update start reference of the folder
1693 if (aFolderStartFeature.get()) {
1694 FeaturePtr aNewStartFeature;
1695 do { // skip all features placed in the composite features
1696 aPrevFeatureLabel = aRefs->Value(aRefIndex++);
1698 aRefIndex <= aRefs->Upper() ? feature(aRefs->Value(aRefIndex)) : FeaturePtr();
1699 } while (aNewStartFeature && isSkippedFeature(aNewStartFeature));
1700 aFolderStartFeature = aNewStartFeature;
1702 // move the folder in the list of references after the last feature from the list
1703 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1704 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1706 // update end reference of the folder
1707 if (aFolderEndFeature.get()) {
1708 FeaturePtr aNewEndFeature;
1709 do { // skip all features placed in the composite features
1712 aRefIndex >= aRefs->Lower() ? feature(aRefs->Value(aRefIndex)) : FeaturePtr();
1713 } while (aNewEndFeature && isSkippedFeature(aNewEndFeature));
1714 aFolderEndFeature = aNewEndFeature;
1718 // update folder references
1719 aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->setValue(aFolderStartFeature);
1720 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aFolderEndFeature);
1722 updateHistory(ModelAPI_Feature::group());
1726 FolderPtr Model_Objects::findContainingFolder(const FeaturePtr& theFeature, int& theIndexInFolder)
1728 // search the label in the list of references
1729 TDF_Label aFeaturesLab = featuresLabel();
1730 Handle(TDataStd_ReferenceArray) aRefs;
1731 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1732 return FolderPtr(); // no reference array (something is wrong)
1734 std::shared_ptr<Model_Data> aData =
1735 std::static_pointer_cast<Model_Data>(theFeature->data());
1736 if (!aData || !aData->isValid())
1738 TDF_Label aLabelToFind = aData->label().Father();
1740 theIndexInFolder = -1;
1741 FolderPtr aFoundFolder;
1742 TDF_Label aLastFeatureLabel;
1744 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1745 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1750 if (aCurLabel == aLabelToFind) { // the feature is reached
1752 if (isSkippedFeature(theFeature)) {
1753 theIndexInFolder = -1;
1756 // decrease the index of the feature in the folder by the number of skipped features
1757 for (int anIndex = theIndexInFolder - 1; anIndex > 0; anIndex--) {
1758 aCurLabel = aRefs->Value(aRefIndex - anIndex);
1759 if (isSkippedFeature(feature(aCurLabel)))
1763 return aFoundFolder;
1766 if (!aFoundFolder) {
1767 // if the current label refers to a folder, feel all necessary data
1768 const ObjectPtr& aFolderObj = folder(aCurLabel);
1769 if (aFolderObj.get()) {
1770 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aFolderObj);
1771 theIndexInFolder = -1;
1773 AttributeReferencePtr aLastRef =
1774 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1775 if (aLastRef->value()) {
1776 aData = std::static_pointer_cast<Model_Data>(aLastRef->value()->data());
1777 if (aData && aData->isValid())
1778 aLastFeatureLabel = aData->label().Father();
1779 } else // folder is empty
1780 aFoundFolder = FolderPtr();
1782 } else if (aLastFeatureLabel == aCurLabel) {
1783 // folder is finished, clear all stored data
1784 theIndexInFolder = -1;
1785 aFoundFolder = FolderPtr();
1789 // folder is not found
1790 theIndexInFolder = -1;
1795 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
1796 const std::shared_ptr<ModelAPI_Result>& theResult)
1798 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1800 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1801 FeaturePtr aFeature = feature(aFeatureLab);
1802 while(!aFeature.get() && aFeatureLab.Depth() > 1) { // this may be sub-result of result
1803 aFeatureLab = aFeatureLab.Father().Father();
1804 aFeature = feature(aFeatureLab);
1808 return FeaturePtr();
1811 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
1813 if (theFeature->data()->isValid()) {
1814 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1815 if (aLabIter.More()) {
1816 TDF_Label anArgLab = aLabIter.Value();
1817 Handle(TDataStd_Comment) aGroup;
1818 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1819 return TCollection_AsciiString(aGroup->Get()).ToCString();
1823 static std::string anEmpty;
1824 return anEmpty; // not found
1827 void Model_Objects::updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
1829 if (theProcessed.find(theFeature) != theProcessed.end())
1831 theProcessed.insert(theFeature);
1832 // for composites update subs recursively (sketch elements results are needed for the sketch)
1833 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
1834 if (aComp.get() && aComp->getKind() != "Part") { // don't go inside of parts sub-features
1835 // update subs of composites first
1836 int aSubNum = aComp->numberOfSubs();
1837 for(int a = 0; a < aSubNum; a++) {
1838 FeaturePtr aSub = aComp->subFeature(a);
1839 updateResults(aComp->subFeature(a), theProcessed);
1843 // for not persistent is will be done by parametric updater automatically
1844 //if (!theFeature->isPersistentResult()) return;
1845 // check the existing results and remove them if there is nothing on the label
1846 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1847 while(aResIter != theFeature->results().cend()) {
1848 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1850 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(aBody->data());
1851 if (!aData.get() || !aData->isValid() || (!aBody->isDisabled() && aData->isDeleted())) {
1852 // found a disappeared result => remove it
1853 theFeature->eraseResultFromList(aBody);
1854 // start iterate from beginning because iterator is corrupted by removing
1855 aResIter = theFeature->results().cbegin();
1861 // it may be on undo
1862 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
1864 // check that results are presented on all labels
1865 int aResSize = int(theFeature->results().size());
1866 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1867 for(; aLabIter.More(); aLabIter.Next()) {
1868 // here must be GUID of the feature
1869 int aResIndex = aLabIter.Value().Tag() - 1;
1871 if (aResSize <= aResIndex) {
1872 TDF_Label anArgLab = aLabIter.Value();
1873 Handle(TDataStd_Comment) aGroup;
1874 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1875 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1876 aNewBody = createBody(theFeature->data(), aResIndex);
1877 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1878 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
1879 theFeature->setResult(aNewP, aResIndex);
1880 if (!aNewP->partDoc().get())
1881 // create the part result: it is better to restore the previous result if it is possible
1882 theFeature->execute();
1883 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1884 theFeature->execute(); // construction shapes are needed for sketch solver
1885 if (!theFeature->results().empty()) // to fix #2640 : update sketch, but not naming
1886 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theFeature->firstResult())
1888 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1889 aNewBody = createGroup(theFeature->data(), aResIndex);
1890 } else if (aGroup->Get() == ModelAPI_ResultField::group().c_str()) {
1891 aNewBody = createField(theFeature->data(), aResIndex);
1892 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1893 theFeature->attributeChanged("expression"); // just produce a value
1895 Events_InfoMessage("Model_Objects", "Unknown type of result is found in the document:")
1896 .arg(TCollection_AsciiString(aGroup->Get()).ToCString()).send();
1899 if (aNewBody && !aNewBody->data()->isDeleted()) {
1900 theFeature->setResult(aNewBody, aResIndex);
1904 if (aResSize > 0) { // check there exist a body that must be updated
1905 std::list<ResultPtr>::const_iterator aRes = theFeature->results().cbegin();
1906 for (; aResSize && aRes != theFeature->results().cend(); aRes++, aResSize++) {
1907 if ((*aRes)->data()->isValid() && (*aRes)->groupName() == ModelAPI_ResultBody::group()) {
1908 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRes);
1909 aBody->updateSubs(aBody->shape(), false);
1915 ResultPtr Model_Objects::findByName(const std::string theName)
1918 FeaturePtr aResFeature; // keep feature to return the latest one
1919 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
1920 for(; anObjIter.More(); anObjIter.Next()) {
1921 FeaturePtr& aFeature = anObjIter.ChangeValue();
1922 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
1924 std::list<ResultPtr> allResults;
1925 ModelAPI_Tools::allResults(aFeature, allResults);
1926 std::list<ResultPtr>::iterator aRIter = allResults.begin();
1927 for (; aRIter != allResults.cend(); aRIter++) {
1928 ResultPtr aRes = *aRIter;
1929 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
1930 aRes->data()->name() == theName)
1932 if (!aResult.get() || isLater(aFeature, aResFeature)) { // select the latest
1934 aResFeature = aFeature;
1942 TDF_Label Model_Objects::nextLabel(TDF_Label theCurrent, const bool theReverse)
1944 Handle(TDataStd_ReferenceArray) aRefs;
1945 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1946 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
1947 TDF_Label aCurLab = aRefs->Value(a);
1948 if (aCurLab.IsEqual(theCurrent)) {
1949 a += theReverse ? -1 : 1;
1950 if (a >= aRefs->Lower() && a <= aRefs->Upper())
1951 return aRefs->Value(a);
1952 break; // finish iiteration: it's last feature
1959 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, 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, theReverse);
1966 if (aNextLabel.IsNull())
1967 break; // 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);