1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Model_Objects.h>
21 #include <Model_Data.h>
22 #include <Model_Document.h>
23 #include <Model_Events.h>
24 #include <Model_Session.h>
25 #include <Model_ResultPart.h>
26 #include <Model_ResultConstruction.h>
27 #include <Model_ResultBody.h>
28 #include <Model_ResultGroup.h>
29 #include <Model_ResultField.h>
30 #include <Model_ResultParameter.h>
31 #include <Model_AttributeRefList.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_CompositeFeature.h>
34 #include <ModelAPI_Tools.h>
35 #include <ModelAPI_Filter.h>
38 #include <Events_Loop.h>
39 #include <Events_InfoMessage.h>
41 #include <Locale_Convert.h>
43 #include <TDataStd_Integer.hxx>
44 #include <TDataStd_Comment.hxx>
45 #include <TDF_ChildIDIterator.hxx>
46 #include <TDataStd_ReferenceArray.hxx>
47 #include <TDataStd_HLabelArray1.hxx>
48 #include <TDF_Reference.hxx>
49 #include <TDF_ChildIDIterator.hxx>
50 #include <TDF_LabelMapHasher.hxx>
51 #include <TDF_LabelMap.hxx>
52 #include <TDF_ListIteratorOfLabelList.hxx>
54 // for TDF_Label map usage
55 static Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper);
56 static Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2);
58 int kUNDEFINED_FEATURE_INDEX = -1;
60 static const std::string& groupNameFoldering(const std::string& theGroupID,
61 const bool theAllowFolder)
64 static const std::string anOutOfFolderName = std::string("__") + ModelAPI_Feature::group();
65 static const std::string aDummyName;
66 return theGroupID == ModelAPI_Feature::group() ? anOutOfFolderName : aDummyName;
71 // Check theFeature is a first or last feature in folder and return this folder
72 static FolderPtr inFolder(const FeaturePtr& theFeature, const std::string& theFolderAttr)
74 const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
75 for (std::set<AttributePtr>::iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt) {
76 if ((*anIt)->id() != theFolderAttr)
79 ObjectPtr anOwner = (*anIt)->owner();
80 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(anOwner);
88 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
91 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
92 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
95 /// 0:1:2 - where features are located
96 /// 0:1:2:N:1 - data of the feature N
97 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
99 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
103 void Model_Objects::setOwner(DocumentPtr theDoc)
106 // update all fields and recreate features and result objects if needed
107 TDF_LabelList aNoUpdated;
108 synchronizeFeatures(aNoUpdated, true, false, true, true);
112 Model_Objects::~Model_Objects()
114 // delete all features of this document
115 Events_Loop* aLoop = Events_Loop::loop();
116 // erase one by one to avoid access from the feature destructor itself from he map
117 // blocks the flush signals to avoid the temporary objects visualization in the viewer
118 // they should not be shown in order to do not lose highlight by erasing them
119 bool isActive = aLoop->activateFlushes(false);
121 while(!myFeatures.IsEmpty()) {
122 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
123 FeaturePtr aFeature = aFeaturesIter.Value();
124 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
125 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
126 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
127 aFeature->removeResults(0, false);
129 myFeatures.UnBind(aFeaturesIter.Key());
131 while (!myFolders.IsEmpty()) {
132 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFoldersIter(myFolders);
133 ObjectPtr aFolder = aFoldersIter.Value();
134 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
135 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
136 ModelAPI_EventCreator::get()->sendUpdated(aFolder, EVENT_DISP);
138 myFolders.UnBind(aFoldersIter.Key());
141 aLoop->activateFlushes(isActive);
142 // erase update, because features are destroyed and update should not performed for them anywhere
143 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
144 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
145 // deleted and redisplayed is correctly performed: they know that features are destroyed
146 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
147 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
151 /// Appends to the array of references a new referenced label
152 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
154 Handle(TDataStd_ReferenceArray) aRefs;
155 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
156 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
157 aRefs->SetValue(0, theReferenced);
158 } else { // extend array by one more element
159 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
161 int aPassedPrev = 0; // previous feature is found and passed
162 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
163 aNewArray->SetValue(aRefs->Lower(), theReferenced);
166 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
167 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
168 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
170 aNewArray->SetValue(a + 1, theReferenced);
173 if (!aPassedPrev) // not found: unknown situation
174 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
175 aRefs->SetInternalArray(aNewArray);
179 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
181 if (!theFeature->isAction()) { // do not add action to the data model
182 TDF_Label aFeaturesLab = featuresLabel();
183 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
184 // store feature in the features array: before "initData" because in macro features
185 // in initData it creates new features, appeared later than this
186 TDF_Label aPrevFeateureLab;
187 FolderPtr aParentFolder;
188 if (theAfterThis.get()) { // searching for the previous feature label
189 std::shared_ptr<Model_Data> aPrevData =
190 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
191 if (aPrevData.get()) {
192 aPrevFeateureLab = aPrevData->label().Father();
194 // Check if the previous feature is the last feature in a folder,
195 // then the folder should be updated to contain additional feature.
196 // Macro features are not stored in folder.
197 if (!theFeature->isMacro()) {
198 // If the last feature is a sub-feature of composite, use parent feature
199 // to check belonging to a folder.
200 FeaturePtr afterThis = ModelAPI_Tools::compositeOwner(theAfterThis);
202 afterThis = theAfterThis;
203 aParentFolder = inFolder(afterThis, ModelAPI_Folder::LAST_FEATURE_ID());
206 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
208 // keep the feature ID to restore document later correctly
209 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
210 myFeatures.Bind(aFeatureLab, theFeature);
211 // must be before the event sending: for OB the feature is already added
212 updateHistory(ModelAPI_Feature::group());
213 // do not change the order:
216 // during python script with fillet constraint feature data should be
217 // initialized before using it in GUI
219 // must be after binding to the map because of "Box" macro feature that
220 // creates other features in "initData"
221 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
222 // put feature to the end of folder if it is added while
223 // the history line is set to the last feature from the folder
225 aParentFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(theFeature);
226 updateHistory(ModelAPI_Folder::group());
228 // event: feature is added, mist be before "initData" to update OB correctly on Duplicate:
229 // first new part, then the content
230 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
231 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
232 } else { // make feature has not-null data anyway
233 theFeature->setData(Model_Data::invalidData());
234 theFeature->setDoc(myDoc);
238 /// Appends to the array of references a new referenced label.
239 /// If theIndex is not -1, removes element at this index, not theReferenced.
240 /// \returns the index of removed element
241 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
242 const int theIndex = -1)
244 int aResult = -1; // no returned
245 Handle(TDataStd_ReferenceArray) aRefs;
246 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
247 if (aRefs->Length() == 1) { // just erase an array
248 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
249 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
252 } else { // reduce the array
253 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
255 int aCount = aRefs->Lower();
256 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
257 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
261 aNewArray->SetValue(aCount, aRefs->Value(a));
264 aRefs->SetInternalArray(aNewArray);
270 void Model_Objects::refsToFeature(FeaturePtr theFeature,
271 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
273 // check the feature: it must have no depended objects on it
274 // the dependencies can be in the feature results
275 std::list<ResultPtr> aResults;
276 ModelAPI_Tools::allResults(theFeature, aResults);
277 std::list<ResultPtr>::const_iterator aResIter = aResults.cbegin();
278 for (; aResIter != aResults.cend(); aResIter++) {
279 ResultPtr aResult = (*aResIter);
280 std::shared_ptr<Model_Data> aData =
281 std::dynamic_pointer_cast<Model_Data>(aResult->data());
282 if (aData.get() != NULL) {
283 const std::set<AttributePtr>& aRefs = aData->refsToMe();
284 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
285 for (; aRefIt != aRefLast; aRefIt++) {
286 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
287 if (aFeature.get() != NULL)
288 theRefs.insert(aFeature);
292 // the dependencies can be in the feature itself
293 std::shared_ptr<Model_Data> aData =
294 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
295 if (aData.get() && !aData->refsToMe().empty()) {
296 const std::set<AttributePtr>& aRefs = aData->refsToMe();
297 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
298 for (; aRefIt != aRefLast; aRefIt++) {
299 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
300 if (aFeature.get() != NULL)
301 theRefs.insert(aFeature);
305 if (!theRefs.empty() && isSendError) {
306 Events_InfoMessage("Model_Objects",
307 "Feature '%1' is used and can not be deleted").arg(theFeature->data()->name()).send();
311 void Model_Objects::removeFeature(FeaturePtr theFeature)
313 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
314 if (aData.get() && aData->isValid()) {
315 // checking that the sub-element of composite feature is removed: if yes, inform the owner
316 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
317 refsToFeature(theFeature, aRefs, false);
318 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
319 for(; aRefIter != aRefs.end(); aRefIter++) {
320 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
321 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
322 if (aComposite.get() && aComposite->data()->isValid() && aComposite->isSub(theFeature)) {
323 aComposite->removeFeature(theFeature);
326 // remove feature from folder
327 removeFromFolder(std::list<FeaturePtr>(1, theFeature));
328 // this must be before erase since theFeature erasing removes all information about
329 // the feature results and groups of results
330 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
331 clearHistory(theFeature);
335 TDF_Label aFeatureLabel = aData->label().Father();
336 if (myFeatures.IsBound(aFeatureLabel))
337 myFeatures.UnBind(aFeatureLabel);
339 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
340 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
341 // erase all attributes under the label of feature
342 aFeatureLabel.ForgetAllAttributes();
343 // remove it from the references array
344 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
345 // event: feature is deleted
346 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
347 updateHistory(ModelAPI_Feature::group());
351 void Model_Objects::eraseAllFeatures()
353 static Events_ID kDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
354 static const ModelAPI_EventCreator* kCreator = ModelAPI_EventCreator::get();
355 // make all features invalid (like deleted)
356 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
357 for(; aFIter.More(); aFIter.Next()) {
358 FeaturePtr aFeature = aFIter.Value();
359 std::list<ResultPtr> aResList;
360 ModelAPI_Tools::allResults(aFeature, aResList);
361 std::list<ResultPtr>::iterator aRIter = aResList.begin();
362 for(; aRIter != aResList.end(); aRIter++) {
363 ResultPtr aRes = *aRIter;
364 if (aRes && aRes->data()->isValid()) {
365 kCreator->sendDeleted(myDoc, aRes->groupName());
366 kCreator->sendUpdated(aRes, kDispEvent);
367 aRes->setData(aRes->data()->invalidPtr());
371 kCreator->sendUpdated(aFeature, kDispEvent);
372 aFeature->setData(aFeature->data()->invalidPtr());
374 kCreator->sendDeleted(myDoc, ModelAPI_Feature::group());
375 myFeatures.Clear(); // just remove features without modification of DS
379 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
381 TDF_Label aFeaturesLab = featuresLabel();
382 Handle(TDataStd_ReferenceArray) aRefs;
383 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
385 TDF_Label anAfterLab, aMovedLab =
386 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
387 if (theAfterThis.get())
388 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
390 // check whether some folder refers to the moved feature by start or end: if yes, remove from it
391 removeFromFolder(std::list<FeaturePtr>(1, theMoved));
393 Handle(TDataStd_HLabelArray1) aNewArray =
394 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
395 int aPassedMovedFrom = 0; // the previous feature location is found and passed
396 int aPassedMovedTo = 0; // the feature is added and this location is passed
397 if (!theAfterThis.get()) { // null means that inserted feature must be the first
398 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
401 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
402 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
404 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
405 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
406 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
407 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
409 } else { // just copy one
410 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
411 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
414 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
415 if (!aPassedMovedFrom) {
416 static std::string aMovedFromError("The moved feature is not found");
417 Events_InfoMessage("Model_Objects", aMovedFromError).send();
419 static std::string aMovedToError("The 'after' feature for movement is not found");
420 Events_InfoMessage("Model_Objects", aMovedToError).send();
424 // store the new array
425 aRefs->SetInternalArray(aNewArray);
426 // update the feature and the history
427 clearHistory(theMoved);
428 // make sure all (selection) attributes of moved feature will be updated
429 static Events_ID kUpdateSelection = Events_Loop::loop()->eventByName(EVENT_UPDATE_SELECTION);
430 ModelAPI_EventCreator::get()->sendUpdated(theMoved, kUpdateSelection, false);
431 ModelAPI_EventCreator::get()->sendReordered(theMoved);
434 void Model_Objects::clearHistory(ObjectPtr theObj)
437 const std::string aGroup = theObj->groupName();
438 updateHistory(aGroup);
440 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
441 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
442 std::string aResultGroup = featureResultGroup(aFeature);
443 if (!aResultGroup.empty()) {
444 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
445 myHistory.find(aResultGroup);
446 if (aHIter != myHistory.end())
447 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
453 void Model_Objects::createHistory(const std::string& theGroupID)
455 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
456 if (aHIter == myHistory.end()) {
457 std::vector<ObjectPtr> aResult;
458 std::vector<ObjectPtr> aResultOutOfFolder;
459 FeaturePtr aLastFeatureInFolder;
460 // iterate the array of references and get feature by feature from the array
461 bool isFeature = theGroupID == ModelAPI_Feature::group();
462 bool isFolder = theGroupID == ModelAPI_Folder::group();
463 Handle(TDataStd_ReferenceArray) aRefs;
464 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
465 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
466 FeaturePtr aFeature = feature(aRefs->Value(a));
467 if (aFeature.get()) {
468 // if feature is in sub-component, remove it from history:
469 // it is in sub-tree of sub-component
470 bool isSub = ModelAPI_Tools::compositeOwner(aFeature).get() != NULL;
471 if (isFeature) { // here may be also disabled features
472 if (!isSub && aFeature->isInHistory()) {
473 aResult.push_back(aFeature);
474 // the feature is out of the folders
475 if (aLastFeatureInFolder.get() == NULL)
476 aResultOutOfFolder.push_back(aFeature);
478 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
479 // construction results of sub-features should not be in the tree
480 if (!isSub || theGroupID != ModelAPI_ResultConstruction::group()) {
481 // do not use reference to the list here since results can be changed by "isConcealed"
482 const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
483 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator
484 aRIter = aResults.begin();
485 for (; aRIter != aResults.cend(); aRIter++) {
486 ResultPtr aRes = *aRIter;
487 if (aRes->groupName() != theGroupID) break; // feature have only same group results
488 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
489 aResult.push_back(*aRIter);
495 // the feature closes the folder, so the next features will be treated as out-of-folder
496 if (aLastFeatureInFolder.get() && aLastFeatureInFolder == aFeature)
497 aLastFeatureInFolder = FeaturePtr();
500 // it may be a folder
501 const ObjectPtr& aFolder = folder(aRefs->Value(a));
503 // store folder information for the Features group only
504 if (isFeature || isFolder) {
505 aResult.push_back(aFolder);
507 aResultOutOfFolder.push_back(aFolder);
510 // get the last feature in the folder
511 AttributeReferencePtr aLastFeatAttr =
512 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
514 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
519 // to be sure that isConcealed did not update the history (issue 1089) during the iteration
520 if (myHistory.find(theGroupID) == myHistory.end()) {
521 myHistory[theGroupID] = aResult;
523 // store the features placed out of any folder
524 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroupID, true);
525 if (!anOutOfFolderGroupID.empty())
526 myHistory[anOutOfFolderGroupID] = aResultOutOfFolder;
531 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
533 clearHistory(theObject);
536 void Model_Objects::updateHistory(const std::string theGroup)
538 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
539 if (aHIter != myHistory.end()) {
540 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
542 // erase history for the group of objects placed out of any folder
543 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroup, true);
544 if (!anOutOfFolderGroupID.empty())
545 myHistory.erase(anOutOfFolderGroupID);
549 const ObjectPtr& Model_Objects::folder(TDF_Label theLabel) const
551 if (myFolders.IsBound(theLabel))
552 return myFolders.Find(theLabel);
553 static ObjectPtr anEmptyResult;
554 return anEmptyResult;
557 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
559 if (myFeatures.IsBound(theLabel))
560 return myFeatures.Find(theLabel);
561 return FeaturePtr(); // not found
564 ObjectPtr Model_Objects::object(TDF_Label theLabel)
566 // try feature by label
567 FeaturePtr aFeature = feature(theLabel);
569 return feature(theLabel);
570 TDF_Label aFeatureLabel = theLabel; // let's suppose it is result of this feature
571 TDF_LabelList aSubLabs; // sub - labels from higher level to lower level of result
572 while(!aFeature.get() && aFeatureLabel.Depth() > 1) {
573 aSubLabs.Prepend(aFeatureLabel);
574 aFeatureLabel = aFeatureLabel.Father().Father();
575 aFeature = feature(aFeatureLabel);
577 if (aFeature.get()) {
578 ResultPtr aCurrentResult;
579 // searching for results then sub-results label by label
580 for(TDF_ListIteratorOfLabelList aSubLab(aSubLabs); aSubLab.More(); aSubLab.Next()) {
581 if (aCurrentResult.get()) { // iterate sub-results of result
582 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCurrentResult);
584 return ObjectPtr(); // only Body can have sub-results
585 int a, aNumSubs = anOwner->numberOfSubs();
586 for(a = 0; a < aNumSubs; a++) {
587 ResultPtr aSub = anOwner->subResult(a);
589 std::shared_ptr<Model_Data> aSubData = std::dynamic_pointer_cast<Model_Data>(
591 const TDF_Label& aSubLabVal = aSubLab.ChangeValue();
592 if (aSubData->label().Father().IsEqual(aSubLabVal)) {
593 aCurrentResult = aSub;
598 if (a == aNumSubs) // not found an appropriate sub-result of result
600 } else { // iterate results of feature
601 const std::list<ResultPtr>& aResults = aFeature->results();
602 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
603 for(; aRIter != aResults.cend(); aRIter++) {
604 std::shared_ptr<Model_Data> aResData =
605 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
606 if (aResData->label().Father().IsEqual(aSubLab.ChangeValue())) {
607 aCurrentResult = *aRIter;
611 if (aRIter == aResults.cend()) // not found an appropriate result of feature
615 return aCurrentResult;
617 return ObjectPtr(); // not found
620 ObjectPtr Model_Objects::object(const std::string& theGroupID,
622 const bool theAllowFolder)
626 createHistory(theGroupID);
627 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
628 return aGroupID.empty() ? myHistory[theGroupID][theIndex] : myHistory[aGroupID][theIndex];
631 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
632 const std::string& theGroupID, const std::wstring& theName)
634 createHistory(theGroupID);
635 if (theGroupID == ModelAPI_Feature::group()) { // searching among features (in history or not)
636 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
637 // from the end to find the latest result with such name
638 std::list<std::shared_ptr<ModelAPI_Feature> >::reverse_iterator anObjIter = allObjs.rbegin();
639 for(; anObjIter != allObjs.rend(); anObjIter++) {
640 if ((*anObjIter)->data()->name() == theName)
643 } else { // searching among results (concealed or not)
644 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
645 // from the end to find the latest result with such name
646 std::list<std::shared_ptr<ModelAPI_Feature> >::reverse_iterator anObjIter = allObjs.rbegin();
647 for(; anObjIter != allObjs.rend(); anObjIter++) {
648 std::list<ResultPtr> allRes;
649 ModelAPI_Tools::allResults(*anObjIter, allRes);
650 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
651 if (aRes->get() && (*aRes)->groupName() == theGroupID) {
652 if ((*aRes)->data()->name() == theName)
662 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject,
663 const bool theAllowFolder)
665 std::string aGroup = theObject->groupName();
666 // treat folder as feature
667 if (aGroup == ModelAPI_Folder::group())
668 aGroup = ModelAPI_Feature::group();
669 createHistory(aGroup);
671 // get the group of features out of folder (if enabled)
672 if (theAllowFolder && !groupNameFoldering(aGroup, theAllowFolder).empty())
673 aGroup = groupNameFoldering(aGroup, theAllowFolder);
675 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
676 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
677 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
678 if ((*anObjIter) == theObject)
685 int Model_Objects::size(const std::string& theGroupID, const bool theAllowFolder)
687 createHistory(theGroupID);
688 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
689 return aGroupID.empty() ? int(myHistory[theGroupID].size()) : int(myHistory[aGroupID].size());
692 std::shared_ptr<ModelAPI_Object> Model_Objects::parent(
693 const std::shared_ptr<ModelAPI_Object> theChild)
695 if (theChild.get()) {
696 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theChild->data());
697 TDF_Label aLab = aData->label();
698 if (!aLab.IsNull() && aLab.Depth() > 2) {
699 ObjectPtr anObj = object(aLab.Father().Father().Father());
707 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
709 // iterate the array of references and get feature by feature from the array
710 Handle(TDataStd_ReferenceArray) aRefs;
711 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
712 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
713 FeaturePtr aFeature = feature(aRefs->Value(a));
714 if (aFeature.get()) {
715 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
716 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
717 for (; aRIter != aResults.cend(); aRIter++) {
718 ResultPtr aRes = *aRIter;
719 if (aRes->groupName() != theGroupID) break; // feature have only same group results
720 // iterate also concealed: ALL RESULTS (for translation parts undo/redo management)
721 //if (aRes->isInHistory() && !aRes->isConcealed()) {
722 theResults.push_back(*aRIter);
731 TDF_Label Model_Objects::featuresLabel() const
733 return myMain.FindChild(TAG_OBJECTS);
736 static std::wstring composeName(const std::string& theFeatureKind, const int theIndex)
738 std::stringstream aNameStream;
739 aNameStream << theFeatureKind << "_" << theIndex;
740 return Locale::Convert::toWString(aNameStream.str());
743 void Model_Objects::setUniqueName(FeaturePtr theFeature)
745 if (!theFeature->data()->name().empty())
746 return; // not needed, name is already defined
747 std::wstring aName; // result
748 // first count all features of such kind to start with index = count + 1
749 int aNumObjects = -1; // this feature is already in this map
750 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
751 std::set<std::wstring> allNames;
752 for (; aFIter.More(); aFIter.Next()) {
753 if (aFIter.Value()->getKind() == theFeature->getKind())
755 allNames.insert(aFIter.Value()->data()->name());
757 // generate candidate name
758 aName = composeName(theFeature->getKind(), aNumObjects + 1);
759 // check this is unique, if not, increase index by 1
760 for(aNumObjects++; allNames.find(aName) != allNames.end(); aNumObjects++) {
761 aName = composeName(theFeature->getKind(), aNumObjects + 1);
763 theFeature->data()->setName(aName);
766 void Model_Objects::setUniqueName(FolderPtr theFolder)
768 if (!theFolder->name().empty())
769 return; // name is already defined
771 int aNbFolders = myFolders.Size();
772 std::wstring aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
774 // check the uniqueness of the name
775 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator anIt(myFolders);
776 while (anIt.More()) {
777 if (anIt.Value()->data()->name() == aName) {
778 aName = composeName(ModelAPI_Folder::ID(), ++aNbFolders);
779 // reinitialize iterator to make sure a new name is unique
780 anIt.Initialize(myFolders);
785 theFolder->data()->setName(aName);
788 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
790 std::shared_ptr<Model_Data> aData(new Model_Data);
791 aData->setLabel(theLab.FindChild(theTag));
792 aData->setObject(theObj);
793 theObj->setDoc(myDoc);
794 theObj->setData(aData);
795 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
796 if (aFeature.get()) {
797 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
798 } else { // is it a folder?
799 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theObj);
801 setUniqueName(aFolder);
803 theObj->initAttributes();
806 void Model_Objects::synchronizeFeatures(
807 const TDF_LabelList& theUpdated, const bool theUpdateReferences,
808 const bool theExecuteFeatures, const bool theOpen, const bool theFlush)
810 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
811 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
813 // after all updates, sends a message that groups of features were created or updated
814 Events_Loop* aLoop = Events_Loop::loop();
815 //static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
816 static Events_ID aCreateEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
817 static Events_ID anUpdateEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
818 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
819 static Events_ID aDeleteEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
820 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
821 bool isActive = aLoop->activateFlushes(false);
823 // collect all updated labels map
824 TDF_LabelMap anUpdatedMap;
825 TDF_ListIteratorOfLabelList anUpdatedIter(theUpdated);
826 for(; anUpdatedIter.More(); anUpdatedIter.Next()) {
827 TDF_Label& aFeatureLab = anUpdatedIter.Value();
828 while(aFeatureLab.Depth() > 3)
829 aFeatureLab = aFeatureLab.Father();
830 if (myFeatures.IsBound(aFeatureLab) || myFolders.IsBound(aFeatureLab))
831 anUpdatedMap.Add(aFeatureLab);
834 // update all objects by checking are they on labels or not
835 std::set<ObjectPtr> aNewFeatures, aKeptFeatures;
836 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
837 for (; aLabIter.More(); aLabIter.Next()) {
838 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
839 if (!myFeatures.IsBound(aFeatureLabel) && !myFolders.IsBound(aFeatureLabel)) {
840 // a new feature or folder is inserted
842 std::string aFeatureID = TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
843 aLabIter.Value())->Get()).ToCString();
844 bool isFolder = aFeatureID == ModelAPI_Folder::ID();
846 std::shared_ptr<Model_Session> aSession =
847 std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get());
850 ObjectPtr aFeature = isFolder ? ObjectPtr(new ModelAPI_Folder)
851 : ObjectPtr(aSession->createFeature(aFeatureID, anOwner));
852 if (!aFeature.get()) {
853 // something is wrong, most probably, the opened document has invalid structure
854 Events_InfoMessage("Model_Objects", "Invalid type of object in the document").send();
855 aLabIter.Value()->Label().ForgetAllAttributes();
859 // this must be before "setData" to redo the sketch line correctly
861 myFolders.Bind(aFeatureLabel, aFeature);
863 myFeatures.Bind(aFeatureLabel, std::dynamic_pointer_cast<ModelAPI_Feature>(aFeature));
864 aNewFeatures.insert(aFeature);
865 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
866 updateHistory(aFeature);
868 // event: model is updated
869 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
870 } else { // nothing is changed, both iterators are incremented
873 if (myFeatures.Find(aFeatureLabel, aFeature)) {
874 aKeptFeatures.insert(aFeature);
877 if (myFolders.Find(aFeatureLabel, anObject))
878 aKeptFeatures.insert(anObject);
880 if (anUpdatedMap.Contains(aFeatureLabel)) {
881 if (!theOpen) { // on abort/undo/redo reinitialize attributes if something is changed
882 FiltersFeaturePtr aFilter = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(anObject);
883 if (aFilter.get()) { // for filters attributes may be added/removed on undo/redo
884 std::dynamic_pointer_cast<Model_Data>(aFilter->data())->clearAttributes();
885 aFilter->initAttributes();
887 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
888 anObject->data()->attributes("");
889 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
890 for(; anAttr != anAttrs.end(); anAttr++)
892 // if feature contains results, re-init them too
893 if (aFeature.get()) {
894 std::list<ResultPtr> aResults;
895 ModelAPI_Tools::allResults(aFeature, aResults);
896 std::list<ResultPtr>::iterator aResIter = aResults.begin();
897 for(; aResIter != aResults.end(); aResIter++) {
898 anAttrs = (*aResIter)->data()->attributes("");
899 for(anAttr = anAttrs.begin(); anAttr != anAttrs.end(); anAttr++)
905 ModelAPI_EventCreator::get()->sendUpdated(anObject, anUpdateEvent);
906 if (aFeature && aFeature->getKind() == "Parameter") {
907 // if parameters are changed, update the results (issue 937)
908 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
909 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
910 for (; aRIter != aResults.cend(); aRIter++) {
911 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
912 if (aRes->data()->isValid() && !aRes->isDisabled()) {
913 ModelAPI_EventCreator::get()->sendUpdated(aRes, anUpdateEvent);
921 // check all features are checked: if not => it was removed
922 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
923 while (aFIter.More()) {
924 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
925 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
926 FeaturePtr aFeature = aFIter.Value();
927 // event: model is updated
928 //if (aFeature->isInHistory()) {
929 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::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(aFeature, aRedispEvent);
934 updateHistory(aFeature);
937 // unbind after the "erase" call: on abort sketch
938 // is removes sub-objects that corrupts aFIter
939 myFeatures.UnBind(aFIter.Key());
940 // reinitialize iterator because unbind may corrupt the previous order in the map
941 aFIter.Initialize(myFeatures);
945 // verify folders are checked: if not => is was removed
946 for (NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFldIt(myFolders);
947 aFldIt.More(); aFldIt.Next()) {
948 ObjectPtr aCurObj = aFldIt.Value();
949 if (aKeptFeatures.find(aCurObj) == aKeptFeatures.end() &&
950 aNewFeatures.find(aCurObj) == aNewFeatures.end()) {
951 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
952 // results of this feature must be redisplayed (hided)
953 // redisplay also removed feature (used for sketch and AISObject)
954 ModelAPI_EventCreator::get()->sendUpdated(aCurObj, aRedispEvent);
955 updateHistory(aCurObj);
958 // unbind after the "erase" call: on abort sketch
959 // is removes sub-objects that corrupts aFIter
960 myFolders.UnBind(aFldIt.Key());
961 // reinitialize iterator because unbind may corrupt the previous order in the map
962 aFldIt.Initialize(myFolders);
966 if (theUpdateReferences) {
967 synchronizeBackRefs();
969 // update results of the features (after features created because
970 // they may be connected, like sketch and sub elements)
971 // After synchronization of back references because sketch
972 // must be set in sub-elements before "execute" by updateResults
973 std::set<FeaturePtr> aProcessed; // composites must be updated after their subs (issue 360)
974 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
975 for (; aLabIter2.More(); aLabIter2.Next()) {
976 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
977 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
978 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
979 updateResults(aFeature, aProcessed);
982 // the synchronize should be done after updateResults
983 // in order to correct back references of updated results
984 if (theUpdateReferences) {
985 synchronizeBackRefs();
987 if (!theUpdated.IsEmpty()) {
988 // this means there is no control what was modified => remove history cash
992 if (!theExecuteFeatures)
993 anOwner->setExecuteFeatures(false);
994 aLoop->activateFlushes(isActive);
997 aLoop->flush(aDeleteEvent);
998 // delete should be emitted before create to reacts to aborted feature
999 aLoop->flush(aCreateEvent);
1000 aLoop->flush(anUpdateEvent);
1001 aLoop->flush(aCreateEvent); // after update of features, there could be results created
1002 aLoop->flush(aDeleteEvent); // or deleted
1003 aLoop->flush(aRedispEvent);
1004 aLoop->flush(aToHideEvent);
1006 if (!theExecuteFeatures)
1007 anOwner->setExecuteFeatures(true);
1010 /// synchronizes back references for the given object basing on the collected data
1011 void Model_Objects::synchronizeBackRefsForObject(const std::set<AttributePtr>& theNewRefs,
1012 ObjectPtr theObject)
1014 if (!theObject.get() || !theObject->data()->isValid())
1016 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
1017 // iterate new list to compare with current
1018 std::set<AttributePtr>::iterator aNewIter = theNewRefs.begin();
1019 for(; aNewIter != theNewRefs.end(); aNewIter++) {
1020 // for the Model_AttributeRefList erase cash (issue #2819)
1021 std::shared_ptr<Model_AttributeRefList> aRefList =
1022 std::dynamic_pointer_cast<Model_AttributeRefList>(*aNewIter);
1024 aRefList->eraseHash();
1026 if (aData->refsToMe().find(*aNewIter) == aData->refsToMe().end()) {
1027 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aNewIter)->owner());
1029 aData->addBackReference(aRefFeat, (*aNewIter)->id());
1030 else // add back reference to a folder
1031 aData->addBackReference((*aNewIter)->owner(), (*aNewIter)->id());
1034 if (theNewRefs.size() != aData->refsToMe().size()) { // some back ref must be removed
1035 std::set<AttributePtr>::iterator aCurrentIter = aData->refsToMe().begin();
1036 while(aCurrentIter != aData->refsToMe().end()) {
1037 if (theNewRefs.find(*aCurrentIter) == theNewRefs.end()) {
1038 // for external references from other documents this system
1039 // is not working: refs are collected from
1040 // different Model_Objects, so before remove check this
1041 // external object exists and still referenced
1042 bool aLeaveIt = false;
1043 if ((*aCurrentIter)->owner().get() && (*aCurrentIter)->owner()->document() != myDoc &&
1044 (*aCurrentIter)->owner()->data().get() && (*aCurrentIter)->owner()->data()->isValid()) {
1045 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
1046 (*aCurrentIter)->owner()->data()->referencesToObjects(aRefs);
1047 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> >>>::iterator
1048 aRefIter = aRefs.begin();
1049 for(; aRefIter != aRefs.end(); aRefIter++) {
1050 if ((*aCurrentIter)->id() == aRefIter->first) {
1051 std::list<std::shared_ptr<ModelAPI_Object> >::iterator anOIt;
1052 for(anOIt = aRefIter->second.begin(); anOIt != aRefIter->second.end(); anOIt++) {
1053 if (*anOIt == theObject) {
1061 aData->removeBackReference(*aCurrentIter);
1062 aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
1063 } else aCurrentIter++;
1064 } else aCurrentIter++;
1067 // for the last feature in the folder, check if it is a sub-feature,
1068 // then refer the folder to a top-level parent composite feature
1069 const std::set<AttributePtr>& aRefs = aData->refsToMe();
1070 std::set<AttributePtr>::iterator anIt = aRefs.begin();
1071 for (; anIt != aRefs.end(); ++anIt)
1072 if ((*anIt)->id() == ModelAPI_Folder::LAST_FEATURE_ID())
1074 if (anIt != aRefs.end()) {
1075 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1077 CompositeFeaturePtr aParent;
1078 CompositeFeaturePtr aGrandParent = ModelAPI_Tools::compositeOwner(aFeature);
1080 aParent = aGrandParent;
1082 aGrandParent = ModelAPI_Tools::compositeOwner(aParent);
1083 } while (aGrandParent.get());
1085 ObjectPtr aFolder = (*anIt)->owner();
1086 // remove reference from the current feature
1087 aData->removeBackReference(aFolder, ModelAPI_Folder::LAST_FEATURE_ID());
1088 // set reference to a top-level parent
1089 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aParent);
1090 std::shared_ptr<Model_Data> aParentData =
1091 std::dynamic_pointer_cast<Model_Data>(aParent->data());
1092 aParentData->addBackReference(aFolder, ModelAPI_Folder::LAST_FEATURE_ID());
1096 aData->updateConcealmentFlag();
1099 static void collectReferences(std::shared_ptr<ModelAPI_Data> theData,
1100 std::map<ObjectPtr, std::set<AttributePtr> >& theRefs)
1102 if (theData.get()) {
1103 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1104 theData->referencesToObjects(aRefs);
1105 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIt = aRefs.begin();
1106 for(; aRefsIt != aRefs.end(); aRefsIt++) {
1107 std::list<ObjectPtr>::iterator aRefTo = aRefsIt->second.begin();
1108 for(; aRefTo != aRefsIt->second.end(); aRefTo++) {
1110 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = theRefs.find(*aRefTo);
1111 if (aFound == theRefs.end()) {
1112 theRefs[*aRefTo] = std::set<AttributePtr>();
1113 aFound = theRefs.find(*aRefTo);
1115 aFound->second.insert(theData->attribute(aRefsIt->first));
1122 void Model_Objects::synchronizeBackRefs()
1124 // collect all back references in the separated container: to update everything at once,
1125 // without additional Concealment switching on and off: only the final modification
1127 // referenced (slave) objects to referencing attributes
1128 std::map<ObjectPtr, std::set<AttributePtr> > allRefs;
1129 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
1130 for(; aFeatures.More(); aFeatures.Next()) {
1131 FeaturePtr aFeature = aFeatures.Value();
1132 collectReferences(aFeature->data(), allRefs);
1134 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFolders(myFolders);
1135 for(; aFolders.More(); aFolders.Next()) {
1136 ObjectPtr aFolder = aFolders.Value();
1137 collectReferences(aFolder->data(), allRefs);
1139 // second iteration: just compare back-references with existing in features and results
1140 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1141 FeaturePtr aFeature = aFeatures.Value();
1142 static std::set<AttributePtr> anEmpty;
1143 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(aFeature);
1144 if (aFound == allRefs.end()) { // not found => erase all back references
1145 synchronizeBackRefsForObject(anEmpty, aFeature);
1147 synchronizeBackRefsForObject(aFound->second, aFeature);
1148 allRefs.erase(aFound); // to check that all refs are counted
1151 std::list<ResultPtr> aResults;
1152 ModelAPI_Tools::allResults(aFeature, aResults);
1153 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1154 for(; aRIter != aResults.cend(); aRIter++) {
1155 aFound = allRefs.find(*aRIter);
1156 if (aFound == allRefs.end()) { // not found => erase all back references
1157 synchronizeBackRefsForObject(anEmpty, *aRIter);
1159 synchronizeBackRefsForObject(aFound->second, *aRIter);
1160 allRefs.erase(aFound); // to check that all refs are counted
1164 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1165 FeaturePtr aFeature = aFeatures.Value();
1166 std::list<ResultPtr> aResults;
1167 ModelAPI_Tools::allResults(aFeature, aResults);
1168 // update the concealment status for display in isConcealed of ResultBody
1169 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1170 for(; aRIter != aResults.cend(); aRIter++) {
1171 (*aRIter)->isConcealed();
1174 // the rest all refs means that feature references to the external document feature:
1175 // process also them
1176 std::map<ObjectPtr, std::set<AttributePtr> >::iterator anExtIter = allRefs.begin();
1177 for(; anExtIter != allRefs.end(); anExtIter++) {
1178 synchronizeBackRefsForObject(anExtIter->second, anExtIter->first);
1182 TDF_Label Model_Objects::resultLabel(
1183 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1185 const std::shared_ptr<Model_Data>& aData =
1186 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1187 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1190 bool Model_Objects::hasCustomName(DataPtr theFeatureData,
1191 ResultPtr theResult,
1192 int /*theResultIndex*/,
1193 std::wstring& theParentName) const
1195 ResultBodyPtr aBodyRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theFeatureData->owner());
1197 // only for top-results (works for the cases when results are not yet added to the feature)
1198 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult);
1200 // names of sub-solids in CompSolid should be default (for example,
1201 // result of boolean operation 'Boolean_1' is a CompSolid which is renamed to 'MyBOOL',
1202 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1', 'Boolean_1_2' etc.)
1203 if (std::dynamic_pointer_cast<Model_Data>(aBodyRes->data())->label().Depth() == 6) {
1204 std::wostringstream aDefaultName;
1205 // compute default name of CompSolid (name of feature + index of CompSolid's result)
1206 int aBodyResultIndex = 0;
1207 const std::list<ResultPtr>& aResults = anOwner->results();
1208 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
1209 for (; anIt != aResults.end(); ++anIt, ++aBodyResultIndex)
1210 if (aBodyRes == *anIt)
1212 aDefaultName << anOwner->name();
1213 aDefaultName << "_" << (aBodyResultIndex + 1);
1214 theParentName = aDefaultName.str();
1215 } else { // just name of the parent result if it is deeper than just a sub-result
1216 theParentName = aBodyRes->data()->name();
1221 std::pair<std::wstring, bool> aName = ModelAPI_Tools::getDefaultName(theResult);
1223 theParentName = aName.first;
1224 return aName.second;
1227 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1228 std::shared_ptr<ModelAPI_Result> theResult,
1229 const int theResultIndex,
1230 const std::wstring& theNameShape)
1233 theResult->setDoc(myDoc);
1234 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1235 if (theResult->data()->name().empty()) {
1236 // if was not initialized, generate event and set a name
1237 std::wstring aNewName = theFeatureData->name();
1238 if (hasCustomName(theFeatureData, theResult, theResultIndex, aNewName)) {
1239 // if the name of result is user-defined, then, at first time, assign name of the result
1240 // by empty string to be sure that corresponding flag in the data model is set
1241 theResult->data()->setName(L"");
1243 std::wstringstream aName;
1244 if ( theNameShape != L"" ){
1245 aName << theNameShape;
1248 // if there are several results (issue #899: any number of result),
1249 // add unique prefix starting from second
1250 if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
1251 aName << "_" << theResultIndex + 1;
1253 aNewName = aName.str();
1255 theResult->data()->setName(aNewName);
1259 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
1260 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1262 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1263 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1264 ObjectPtr anOldObject = object(aLab);
1265 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1266 if (anOldObject.get()) {
1267 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1269 if (!aResult.get()) {
1270 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1271 storeResult(theFeatureData, aResult, theIndex);
1276 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
1277 const std::shared_ptr<ModelAPI_Data>& theFeatureData,
1279 const std::wstring& theNameShape)
1281 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1282 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1283 ObjectPtr anOldObject = object(aLab);
1284 std::shared_ptr<ModelAPI_ResultBody> aResult;
1285 if (anOldObject.get()) {
1286 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1288 if (!aResult.get()) {
1289 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1290 storeResult(theFeatureData, aResult, theIndex, theNameShape);
1295 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
1296 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1298 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1299 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1300 ObjectPtr anOldObject = object(aLab);
1301 std::shared_ptr<ModelAPI_ResultPart> aResult;
1302 if (anOldObject.get()) {
1303 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1305 if (!aResult.get()) {
1306 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1307 storeResult(theFeatureData, aResult, theIndex);
1312 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
1313 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
1314 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1316 std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
1317 aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
1321 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
1322 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1324 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1325 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1326 ObjectPtr anOldObject = object(aLab);
1327 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1328 if (anOldObject.get()) {
1329 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1331 if (!aResult.get()) {
1332 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1333 storeResult(theFeatureData, aResult, theIndex);
1338 std::shared_ptr<ModelAPI_ResultField> Model_Objects::createField(
1339 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1341 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1342 TDataStd_Comment::Set(aLab, ModelAPI_ResultField::group().c_str());
1343 ObjectPtr anOldObject = object(aLab);
1344 std::shared_ptr<ModelAPI_ResultField> aResult;
1345 if (anOldObject.get()) {
1346 aResult = std::dynamic_pointer_cast<ModelAPI_ResultField>(anOldObject);
1348 if (!aResult.get()) {
1349 aResult = std::shared_ptr<ModelAPI_ResultField>(new Model_ResultField(theFeatureData));
1350 storeResult(theFeatureData, aResult, theIndex);
1355 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
1356 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1358 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1359 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1360 ObjectPtr anOldObject = object(aLab);
1361 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1362 if (anOldObject.get()) {
1363 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1365 if (!aResult.get()) {
1366 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1367 storeResult(theFeatureData, aResult, theIndex);
1372 std::shared_ptr<ModelAPI_Folder> Model_Objects::createFolder(
1373 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis)
1375 FolderPtr aFolder(new ModelAPI_Folder);
1379 TDF_Label aFeaturesLab = featuresLabel();
1380 TDF_Label aFolderLab = aFeaturesLab.NewChild();
1381 // store feature in the features array: before "initData" because in macro features
1382 // in initData it creates new features, appeared later than this
1383 TDF_Label aPrevFeatureLab;
1384 if (theBeforeThis.get()) { // searching for the previous feature label
1385 std::shared_ptr<Model_Data> aPrevData =
1386 std::dynamic_pointer_cast<Model_Data>(theBeforeThis->data());
1387 if (aPrevData.get()) {
1388 int anIndex = kUNDEFINED_FEATURE_INDEX;
1389 aPrevFeatureLab = nextLabel(aPrevData->label().Father(), anIndex, true);
1391 } else { // find the label of the last feature
1392 Handle(TDataStd_ReferenceArray) aRefs;
1393 if (aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1394 aPrevFeatureLab = aRefs->Value(aRefs->Upper());
1396 AddToRefArray(aFeaturesLab, aFolderLab, aPrevFeatureLab);
1398 // keep the feature ID to restore document later correctly
1399 TDataStd_Comment::Set(aFolderLab, ModelAPI_Folder::ID().c_str());
1400 myFolders.Bind(aFolderLab, aFolder);
1401 // must be before the event sending: for OB the feature is already added
1402 updateHistory(ModelAPI_Folder::group());
1403 updateHistory(ModelAPI_Feature::group());
1405 // must be after binding to the map because of "Box" macro feature that
1406 // creates other features in "initData"
1407 initData(aFolder, aFolderLab, TAG_FEATURE_ARGUMENTS);
1408 // event: folder is added, must be before "initData" to update OB correctly on Duplicate:
1409 // first new part, then the content
1410 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1411 ModelAPI_EventCreator::get()->sendUpdated(aFolder, anEvent);
1416 void Model_Objects::removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder)
1418 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFolder->data());
1419 if (!aData.get() || !aData->isValid())
1422 // this must be before erase since theFolder erasing removes all information about it
1423 clearHistory(theFolder);
1427 TDF_Label aFolderLabel = aData->label().Father();
1428 if (myFolders.IsBound(aFolderLabel))
1429 myFolders.UnBind(aFolderLabel);
1431 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1432 ModelAPI_EventCreator::get()->sendUpdated(theFolder, EVENT_DISP);
1433 // erase all attributes under the label of feature
1434 aFolderLabel.ForgetAllAttributes();
1435 // remove it from the references array
1436 RemoveFromRefArray(featuresLabel(), aFolderLabel);
1437 // event: feature is deleted
1438 ModelAPI_EventCreator::get()->sendDeleted(theFolder->document(), ModelAPI_Folder::group());
1439 updateHistory(ModelAPI_Folder::group());
1440 updateHistory(ModelAPI_Feature::group());
1443 // Returns one of the limiting features of the list
1444 static FeaturePtr limitingFeature(std::list<FeaturePtr>& theFeatures, const bool isLast)
1446 FeaturePtr aFeature;
1448 aFeature = theFeatures.back();
1449 theFeatures.pop_back();
1451 aFeature = theFeatures.front();
1452 theFeatures.pop_front();
1457 // Verify the feature is sub-element in composite feature or it is not used in the history
1458 static bool isSkippedFeature(FeaturePtr theFeature)
1460 bool isSub = ModelAPI_Tools::compositeOwner(theFeature).get() != NULL;
1461 return isSub || (theFeature && !theFeature->isInHistory());
1464 std::shared_ptr<ModelAPI_Folder> Model_Objects::findFolder(
1465 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1466 const bool theBelow)
1468 if (theFeatures.empty())
1469 return FolderPtr(); // nothing to move
1471 TDF_Label aFeaturesLab = featuresLabel();
1472 Handle(TDataStd_ReferenceArray) aRefs;
1473 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1474 return FolderPtr(); // no reference array (something is wrong)
1476 std::list<std::shared_ptr<ModelAPI_Feature> > aFeatures = theFeatures;
1477 std::shared_ptr<ModelAPI_Feature> aLimitingFeature = limitingFeature(aFeatures, theBelow);
1479 std::shared_ptr<Model_Data> aData =
1480 std::static_pointer_cast<Model_Data>(aLimitingFeature->data());
1481 if (!aData || !aData->isValid())
1482 return FolderPtr(); // invalid feature
1484 // label of the first feature in the list for fast searching
1485 TDF_Label aFirstFeatureLabel = aData->label().Father();
1487 // find a folder above the features and
1488 // check the given features represent a sequential list of objects following the folder
1489 FolderPtr aFoundFolder;
1490 TDF_Label aLastFeatureInFolder;
1491 int aRefIndex = aRefs->Lower();
1492 for(; aRefIndex <= aRefs->Upper(); ++aRefIndex) { // iterate all existing features
1493 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1494 if (IsEqual(aCurLabel, aFirstFeatureLabel))
1495 break; // no need to continue searching
1497 // searching the folder below, just continue to search last feature from the list
1501 // issue #18733: check for the last feature in folder before checking the sub-feature,
1502 // because the folder may end by the feature which is
1503 // neither a sub-feature nor a feature in history.
1504 if (!aLastFeatureInFolder.IsNull()) {
1505 if (IsEqual(aCurLabel, aLastFeatureInFolder))
1506 aLastFeatureInFolder.Nullify(); // the last feature in the folder is achieved
1510 // if feature is in sub-component, skip it
1511 FeaturePtr aCurFeature = feature(aCurLabel);
1512 if (isSkippedFeature(aCurFeature))
1515 const ObjectPtr& aFolderObj = folder(aCurLabel);
1516 if (aFolderObj.get()) {
1517 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aFolderObj);
1518 AttributeReferencePtr aLastFeatAttr =
1519 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1520 if (aLastFeatAttr) {
1521 // setup iterating inside a folder to find last feature
1522 ObjectPtr aLastFeature = aLastFeatAttr->value();
1524 aData = std::static_pointer_cast<Model_Data>(aLastFeature->data());
1525 if (aData && aData->isValid())
1526 aLastFeatureInFolder = aData->label().Father();
1532 if (theBelow && aRefIndex < aRefs->Upper()) {
1534 // skip following features which are sub-components or not in history
1535 for (int anIndex = aRefIndex + 1; anIndex <= aRefs->Upper(); ++anIndex) {
1536 aLabel = aRefs->Value(anIndex);
1537 FeaturePtr aCurFeature = feature(aLabel);
1538 if (!isSkippedFeature(aCurFeature))
1541 // check the next object is a folder
1542 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(folder(aLabel));
1545 if (!aLastFeatureInFolder.IsNull() || // the last feature of the folder above is not found
1549 // check the given features are sequential list
1550 int aStep = theBelow ? -1 : 1;
1551 for (aRefIndex += aStep;
1552 !aFeatures.empty() && aRefIndex >= aRefs->Lower() && aRefIndex <= aRefs->Upper();
1553 aRefIndex += aStep) {
1554 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1555 // if feature is in sub-component, skip it
1556 FeaturePtr aCurFeature = feature(aCurLabel);
1557 if (isSkippedFeature(aCurFeature))
1560 aLimitingFeature = limitingFeature(aFeatures, theBelow);
1561 if (!aCurFeature->data()->isEqual(aLimitingFeature->data()))
1562 return FolderPtr(); // not a sequential list
1565 return aFoundFolder;
1568 bool Model_Objects::moveToFolder(
1569 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1570 const std::shared_ptr<ModelAPI_Folder>& theFolder)
1572 if (theFeatures.empty() || !theFolder)
1575 // labels for the folder and last feature in the list
1576 TDF_Label aFolderLabel, aLastFeatureLabel;
1577 std::shared_ptr<Model_Data> aData =
1578 std::static_pointer_cast<Model_Data>(theFolder->data());
1579 if (aData && aData->isValid())
1580 aFolderLabel = aData->label().Father();
1581 aData = std::static_pointer_cast<Model_Data>(theFeatures.back()->data());
1582 if (aData && aData->isValid())
1583 aLastFeatureLabel = aData->label().Father();
1585 if (aFolderLabel.IsNull() || aLastFeatureLabel.IsNull())
1588 AttributeReferencePtr aFirstFeatAttr =
1589 theFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1590 AttributeReferencePtr aLastFeatAttr =
1591 theFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1592 bool initFirstAttr = !aFirstFeatAttr->value().get();
1593 bool initLastAttr = !aLastFeatAttr->value().get();
1595 // check the folder is below the list of features
1596 bool isFolderBelow = false;
1597 TDF_Label aFeaturesLab = featuresLabel();
1598 Handle(TDataStd_ReferenceArray) aRefs;
1599 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1600 return false; // no reference array (something is wrong)
1601 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1602 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1603 if (aCurLabel == aFolderLabel)
1604 break; // folder is above the features
1605 else if (aCurLabel == aLastFeatureLabel) {
1606 isFolderBelow = true;
1611 if (isFolderBelow) {
1612 aData = std::static_pointer_cast<Model_Data>(theFeatures.front()->data());
1613 if (!aData || !aData->isValid())
1615 TDF_Label aPrevFeatureLabel = aData->label().Father();
1616 // label of the feature before the first feature in the list
1617 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex)
1618 if (aPrevFeatureLabel == aRefs->Value(aRefIndex)) {
1619 if (aRefIndex == aRefs->Lower())
1620 aPrevFeatureLabel.Nullify();
1622 aPrevFeatureLabel = aRefs->Value(aRefIndex - 1);
1626 // move the folder in the list of references before the first feature
1627 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1628 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1629 // update first feature of the folder
1630 initFirstAttr = true;
1632 // update last feature of the folder
1633 initLastAttr = true;
1637 aFirstFeatAttr->setValue(theFeatures.front());
1639 aLastFeatAttr->setValue(theFeatures.back());
1641 updateHistory(ModelAPI_Feature::group());
1645 static FolderPtr isExtractionCorrect(const FolderPtr& theFirstFeatureFolder,
1646 const FolderPtr& theLastFeatureFolder,
1647 bool& isExtractBefore)
1649 if (theFirstFeatureFolder.get()) {
1650 if (theLastFeatureFolder.get())
1651 return theFirstFeatureFolder == theLastFeatureFolder ? theFirstFeatureFolder : FolderPtr();
1653 isExtractBefore = true;
1654 return theFirstFeatureFolder;
1655 } else if (theLastFeatureFolder.get()) {
1656 isExtractBefore = false;
1657 return theLastFeatureFolder;
1663 bool Model_Objects::removeFromFolder(
1664 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1665 const bool theBefore)
1667 if (theFeatures.empty())
1670 FolderPtr aFirstFeatureFolder =
1671 inFolder(theFeatures.front(), ModelAPI_Folder::FIRST_FEATURE_ID());
1672 FolderPtr aLastFeatureFolder =
1673 inFolder(theFeatures.back(), ModelAPI_Folder::LAST_FEATURE_ID());
1675 bool isExtractBeforeFolder = theBefore;
1676 FolderPtr aFoundFolder =
1677 isExtractionCorrect(aFirstFeatureFolder, aLastFeatureFolder, isExtractBeforeFolder);
1679 return false; // list of features cannot be extracted
1681 // references of the current folder
1682 ObjectPtr aFolderStartFeature;
1683 ObjectPtr aFolderEndFeature;
1684 if (aFirstFeatureFolder != aLastFeatureFolder) {
1685 aFolderStartFeature = aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->value();
1686 aFolderEndFeature = aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->value();
1689 FeaturePtr aFeatureToFind = isExtractBeforeFolder ? theFeatures.back() : theFeatures.front();
1690 std::shared_ptr<Model_Data> aData =
1691 std::static_pointer_cast<Model_Data>(aFeatureToFind->data());
1692 if (!aData || !aData->isValid())
1694 TDF_Label aLabelToFind = aData->label().Father();
1696 // search the label in the list of references
1697 TDF_Label aFeaturesLab = featuresLabel();
1698 Handle(TDataStd_ReferenceArray) aRefs;
1699 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1700 return false; // no reference array (something is wrong)
1701 int aRefIndex = aRefs->Lower();
1702 for (; aRefIndex <= aRefs->Upper(); ++aRefIndex)
1703 if (aRefs->Value(aRefIndex) == aLabelToFind)
1706 // update folder position
1707 if (isExtractBeforeFolder) {
1708 aData = std::dynamic_pointer_cast<Model_Data>(aFoundFolder->data());
1709 TDF_Label aFolderLabel = aData->label().Father();
1710 TDF_Label aPrevFeatureLabel = aRefs->Value(aRefIndex);
1711 // update start reference of the folder
1712 if (aFolderStartFeature.get()) {
1713 FeaturePtr aNewStartFeature;
1714 do { // skip all features placed in the composite features
1715 aPrevFeatureLabel = aRefs->Value(aRefIndex++);
1717 aRefIndex <= aRefs->Upper() ? feature(aRefs->Value(aRefIndex)) : FeaturePtr();
1718 } while (aNewStartFeature && isSkippedFeature(aNewStartFeature));
1719 aFolderStartFeature = aNewStartFeature;
1721 // move the folder in the list of references after the last feature from the list
1722 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1723 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1725 // update end reference of the folder
1726 if (aFolderEndFeature.get()) {
1727 FeaturePtr aNewEndFeature;
1728 do { // skip all features placed in the composite features
1731 aRefIndex >= aRefs->Lower() ? feature(aRefs->Value(aRefIndex)) : FeaturePtr();
1732 } while (aNewEndFeature && isSkippedFeature(aNewEndFeature));
1733 aFolderEndFeature = aNewEndFeature;
1737 // update folder references
1738 aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->setValue(aFolderStartFeature);
1739 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aFolderEndFeature);
1741 updateHistory(ModelAPI_Feature::group());
1745 FolderPtr Model_Objects::findContainingFolder(const FeaturePtr& theFeature, int& theIndexInFolder)
1747 // search the label in the list of references
1748 TDF_Label aFeaturesLab = featuresLabel();
1749 Handle(TDataStd_ReferenceArray) aRefs;
1750 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1751 return FolderPtr(); // no reference array (something is wrong)
1753 std::shared_ptr<Model_Data> aData =
1754 std::static_pointer_cast<Model_Data>(theFeature->data());
1755 if (!aData || !aData->isValid())
1757 TDF_Label aLabelToFind = aData->label().Father();
1759 theIndexInFolder = -1;
1760 FolderPtr aFoundFolder;
1761 TDF_Label aLastFeatureLabel;
1763 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1764 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1769 if (aCurLabel == aLabelToFind) { // the feature is reached
1771 if (isSkippedFeature(theFeature)) {
1772 theIndexInFolder = -1;
1775 // decrease the index of the feature in the folder by the number of skipped features
1776 for (int anIndex = theIndexInFolder - 1; anIndex > 0; anIndex--) {
1777 aCurLabel = aRefs->Value(aRefIndex - anIndex);
1778 if (isSkippedFeature(feature(aCurLabel)))
1782 return aFoundFolder;
1785 if (!aFoundFolder) {
1786 // if the current label refers to a folder, feel all necessary data
1787 const ObjectPtr& aFolderObj = folder(aCurLabel);
1788 if (aFolderObj.get()) {
1789 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aFolderObj);
1790 theIndexInFolder = -1;
1792 AttributeReferencePtr aLastRef =
1793 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1794 if (aLastRef->value()) {
1795 aData = std::static_pointer_cast<Model_Data>(aLastRef->value()->data());
1796 if (aData && aData->isValid())
1797 aLastFeatureLabel = aData->label().Father();
1798 } else // folder is empty
1799 aFoundFolder = FolderPtr();
1801 } else if (aLastFeatureLabel == aCurLabel) {
1802 // folder is finished, clear all stored data
1803 theIndexInFolder = -1;
1804 aFoundFolder = FolderPtr();
1808 // folder is not found
1809 theIndexInFolder = -1;
1814 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
1815 const std::shared_ptr<ModelAPI_Result>& theResult)
1817 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1818 if (aData.get() && aData->isValid()) {
1819 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1820 FeaturePtr aFeature = feature(aFeatureLab);
1821 while(!aFeature.get() && aFeatureLab.Depth() > 1) { // this may be sub-result of result
1822 aFeatureLab = aFeatureLab.Father().Father();
1823 aFeature = feature(aFeatureLab);
1827 return FeaturePtr();
1830 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
1832 if (theFeature->data()->isValid()) {
1833 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1834 if (aLabIter.More()) {
1835 TDF_Label anArgLab = aLabIter.Value();
1836 Handle(TDataStd_Comment) aGroup;
1837 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1838 return TCollection_AsciiString(aGroup->Get()).ToCString();
1842 static std::string anEmpty;
1843 return anEmpty; // not found
1846 void Model_Objects::updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
1848 if (theProcessed.find(theFeature) != theProcessed.end())
1850 theProcessed.insert(theFeature);
1851 // for composites update subs recursively (sketch elements results are needed for the sketch)
1852 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
1853 if (aComp.get() && aComp->getKind() != "Part") { // don't go inside of parts sub-features
1854 // update subs of composites first
1855 int aSubNum = aComp->numberOfSubs();
1856 for(int a = 0; a < aSubNum; a++) {
1857 FeaturePtr aSub = aComp->subFeature(a);
1858 updateResults(aComp->subFeature(a), theProcessed);
1862 // it may be on undo
1863 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
1866 // check the existing results and remove them if there is nothing on the label
1867 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1868 while(aResIter != theFeature->results().cend()) {
1869 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1871 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(aBody->data());
1872 if (!aData.get() || !aData->isValid() || (!aBody->isDisabled() && aData->isDeleted())) {
1873 // found a disappeared result => remove it
1874 theFeature->eraseResultFromList(aBody);
1875 // start iterate from beginning because iterator is corrupted by removing
1876 aResIter = theFeature->results().cbegin();
1882 // check that results are presented on all labels
1883 int aResSize = int(theFeature->results().size());
1884 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1885 for(; aLabIter.More(); aLabIter.Next()) {
1886 // here must be GUID of the feature
1887 int aResIndex = aLabIter.Value().Tag() - 1;
1889 if (aResSize <= aResIndex) {
1890 TDF_Label anArgLab = aLabIter.Value();
1891 Handle(TDataStd_Comment) aGroup;
1892 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1893 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1894 aNewBody = createBody(theFeature->data(), aResIndex);
1895 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1896 if (aResIndex <= (int)theFeature->results().size()) {// to avoid crash if previous execute
1897 // for index = 0 erases result
1898 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
1899 if (!aNewP->data()->isDeleted()) {
1900 theFeature->setResult(aNewP, aResIndex);
1901 if (!aNewP->partDoc().get())
1902 // create the part result: it is better to restore the previous result if possible
1903 theFeature->execute();
1906 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1907 ResultConstructionPtr aConstr = createConstruction(theFeature->data(), aResIndex);
1908 if (!aConstr->data()->isDeleted()) {
1909 if (!aConstr->updateShape())
1910 theFeature->execute(); // not stored shape in the data structure, execute to have it
1912 theFeature->setResult(aConstr, aResIndex); // result is ready without execution
1914 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1915 aNewBody = createGroup(theFeature->data(), aResIndex);
1916 } else if (aGroup->Get() == ModelAPI_ResultField::group().c_str()) {
1917 ResultFieldPtr aField = createField(theFeature->data(), aResIndex);
1918 aField->updateSteps(); // to refresh the internal data
1920 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1921 theFeature->attributeChanged("expression"); // just produce a value
1923 Events_InfoMessage("Model_Objects", "Unknown type of result is found in the document:")
1924 .arg(TCollection_AsciiString(aGroup->Get()).ToCString()).send();
1927 if (aNewBody && !aNewBody->data()->isDeleted()) {
1928 theFeature->setResult(aNewBody, aResIndex);
1932 if (aResSize > 0) { // check there exist a body that must be updated
1933 std::list<ResultPtr>::const_iterator aRes = theFeature->results().cbegin();
1934 for (; aResSize && aRes != theFeature->results().cend(); aRes++, aResSize++) {
1935 if ((*aRes)->data()->isValid()) {
1936 if ((*aRes)->groupName() == ModelAPI_ResultBody::group()) {
1937 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRes);
1938 aBody->updateSubs(aBody->shape(), false);
1939 } else if ((*aRes)->groupName() == ModelAPI_ResultConstruction::group()) {
1940 // update the cashed myShape presented in construction
1941 ResultConstructionPtr aConstr =
1942 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
1943 aConstr->updateShape();
1950 ResultPtr Model_Objects::findByName(const std::wstring theName)
1953 FeaturePtr aResFeature; // keep feature to return the latest one
1954 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
1955 for(; anObjIter.More(); anObjIter.Next()) {
1956 FeaturePtr& aFeature = anObjIter.ChangeValue();
1957 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
1959 std::list<ResultPtr> allResults;
1960 ModelAPI_Tools::allResults(aFeature, allResults);
1961 std::list<ResultPtr>::iterator aRIter = allResults.begin();
1962 for (; aRIter != allResults.cend(); aRIter++) {
1963 ResultPtr aRes = *aRIter;
1964 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
1965 aRes->data()->name() == theName)
1967 if (!aResult.get() || isLater(aFeature, aResFeature)) { // select the latest
1969 aResFeature = aFeature;
1977 TDF_Label Model_Objects::nextLabel(TDF_Label theCurrent, int& theIndex, const bool theReverse)
1979 Handle(TDataStd_ReferenceArray) aRefs;
1980 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1981 int aStart = theIndex == kUNDEFINED_FEATURE_INDEX ? aRefs->Lower() : theIndex;
1982 for(int a = aStart; a <= aRefs->Upper(); a++) { // iterate all existing features
1983 TDF_Label aCurLab = aRefs->Value(a);
1984 if (aCurLab.IsEqual(theCurrent)) {
1985 a += theReverse ? -1 : 1;
1986 if (a >= aRefs->Lower() && a <= aRefs->Upper()) {
1988 return aRefs->Value(a);
1990 break; // finish iteration: it's last feature
1997 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, int& theIndex, const bool theReverse)
1999 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
2000 if (aData.get() && aData->isValid()) {
2001 TDF_Label aFeatureLabel = aData->label().Father();
2003 TDF_Label aNextLabel = nextLabel(aFeatureLabel, theIndex, theReverse);
2004 if (aNextLabel.IsNull())
2005 break; // the last or something is wrong
2006 FeaturePtr aFound = feature(aNextLabel);
2008 return aFound; // the feature is found
2009 // if the next label is a folder, skip it
2010 aFeatureLabel = folder(aNextLabel).get() ? aNextLabel : TDF_Label();
2011 } while (!aFeatureLabel.IsNull());
2013 return FeaturePtr(); // not found, last, or something is wrong
2016 FeaturePtr Model_Objects::firstFeature()
2018 Handle(TDataStd_ReferenceArray) aRefs;
2019 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2020 return feature(aRefs->Value(aRefs->Lower()));
2022 return FeaturePtr(); // no features at all
2025 FeaturePtr Model_Objects::lastFeature()
2027 Handle(TDataStd_ReferenceArray) aRefs;
2028 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2029 // comment this because of #2674 - features are removed from array on deactivation of Part
2030 /*FeaturePtr aLast = feature(aRefs->Value(aRefs->Upper()));
2031 if (!aLast.get() && aRefs->Length() != 0) { // erase the invalid feature from the array
2032 RemoveFromRefArray(featuresLabel(), aRefs->Value(aRefs->Upper()));
2033 return lastFeature(); // try once again, after the last was removed
2035 return feature(aRefs->Value(aRefs->Upper()));
2037 return FeaturePtr(); // no features at all
2040 bool Model_Objects::isLater(FeaturePtr theLater, FeaturePtr theCurrent) const
2042 if (theLater->getKind() == "InternalSelectionInPartFeature")
2044 std::shared_ptr<Model_Data> aLaterD = std::static_pointer_cast<Model_Data>(theLater->data());
2045 std::shared_ptr<Model_Data> aCurrentD = std::static_pointer_cast<Model_Data>(theCurrent->data());
2046 if (aLaterD.get() && aLaterD->isValid() && aCurrentD.get() && aCurrentD->isValid()) {
2047 TDF_Label aLaterL = aLaterD->label().Father();
2048 TDF_Label aCurrentL = aCurrentD->label().Father();
2049 int aLaterI = -1, aCurentI = -1; // not found yet state
2050 Handle(TDataStd_ReferenceArray) aRefs;
2051 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2052 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
2053 TDF_Label aCurLab = aRefs->Value(a);
2054 if (aCurLab.IsEqual(aLaterL)) {
2056 } else if (aCurLab.IsEqual(aCurrentL)) {
2059 if (aLaterI != -1 && aCurentI != -1) // both are found
2060 return aLaterI > aCurentI;
2064 return false; // not found, or something is wrong
2067 std::list<std::shared_ptr<ModelAPI_Object> > Model_Objects::allObjects()
2069 std::list<std::shared_ptr<ModelAPI_Object> > aResult;
2070 Handle(TDataStd_ReferenceArray) aRefs;
2071 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2072 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
2073 ObjectPtr anObject = object(aRefs->Value(a));
2074 if (!anObject.get()) // is it a folder?
2075 anObject = folder(aRefs->Value(a));
2077 aResult.push_back(anObject);
2083 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
2085 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
2086 Handle(TDataStd_ReferenceArray) aRefs;
2087 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2088 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
2089 FeaturePtr aFeature = feature(aRefs->Value(a));
2091 aResult.push_back(aFeature);
2097 int Model_Objects::numInternalFeatures()
2099 Handle(TDataStd_ReferenceArray) aRefs;
2100 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2101 return aRefs->Upper() - aRefs->Lower() + 1;
2103 return 0; // invalid
2106 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
2108 Handle(TDataStd_ReferenceArray) aRefs;
2109 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2110 return feature(aRefs->Value(aRefs->Lower() + theIndex));
2112 return FeaturePtr(); // invalid
2115 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
2117 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
2119 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
2121 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);