1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Model_Objects.h>
21 #include <Model_Data.h>
22 #include <Model_Document.h>
23 #include <Model_Events.h>
24 #include <Model_Session.h>
25 #include <Model_ResultPart.h>
26 #include <Model_ResultConstruction.h>
27 #include <Model_ResultBody.h>
28 #include <Model_ResultGroup.h>
29 #include <Model_ResultField.h>
30 #include <Model_ResultParameter.h>
31 #include <Model_AttributeRefList.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_CompositeFeature.h>
34 #include <ModelAPI_Tools.h>
35 #include <ModelAPI_Filter.h>
38 #include <Events_Loop.h>
39 #include <Events_InfoMessage.h>
41 #include <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 int kUNDEFINED_FEATURE_INDEX = -1;
56 static const std::string& groupNameFoldering(const std::string& theGroupID,
57 const bool theAllowFolder)
60 static const std::string anOutOfFolderName = std::string("__") + ModelAPI_Feature::group();
61 static const std::string aDummyName;
62 return theGroupID == ModelAPI_Feature::group() ? anOutOfFolderName : aDummyName;
67 // Check theFeature is a first or last feature in folder and return this folder
68 static FolderPtr inFolder(const FeaturePtr& theFeature, const std::string& theFolderAttr)
70 const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
71 for (std::set<AttributePtr>::iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt) {
72 if ((*anIt)->id() != theFolderAttr)
75 ObjectPtr anOwner = (*anIt)->owner();
76 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(anOwner);
84 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
87 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
88 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
91 /// 0:1:2 - where features are located
92 /// 0:1:2:N:1 - data of the feature N
93 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
95 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
99 void Model_Objects::setOwner(DocumentPtr theDoc)
102 // update all fields and recreate features and result objects if needed
103 TDF_LabelList aNoUpdated;
104 synchronizeFeatures(aNoUpdated, true, false, true, true);
108 Model_Objects::~Model_Objects()
110 // delete all features of this document
111 Events_Loop* aLoop = Events_Loop::loop();
112 // erase one by one to avoid access from the feature destructor itself from he map
113 // blocks the flush signals to avoid the temporary objects visualization in the viewer
114 // they should not be shown in order to do not lose highlight by erasing them
115 bool isActive = aLoop->activateFlushes(false);
117 while(!myFeatures.IsEmpty()) {
118 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
119 FeaturePtr aFeature = aFeaturesIter.Value();
120 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
121 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
122 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
123 aFeature->removeResults(0, false);
125 myFeatures.UnBind(aFeaturesIter.Key());
127 while (!myFolders.IsEmpty()) {
128 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFoldersIter(myFolders);
129 ObjectPtr aFolder = aFoldersIter.Value();
130 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
131 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
132 ModelAPI_EventCreator::get()->sendUpdated(aFolder, EVENT_DISP);
134 myFolders.UnBind(aFoldersIter.Key());
137 aLoop->activateFlushes(isActive);
138 // erase update, because features are destroyed and update should not performed for them anywhere
139 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
140 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
141 // deleted and redisplayed is correctly performed: they know that features are destroyed
142 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
143 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
147 /// Appends to the array of references a new referenced label
148 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
150 Handle(TDataStd_ReferenceArray) aRefs;
151 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
152 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
153 aRefs->SetValue(0, theReferenced);
154 } else { // extend array by one more element
155 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
157 int aPassedPrev = 0; // previous feature is found and passed
158 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
159 aNewArray->SetValue(aRefs->Lower(), theReferenced);
162 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
163 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
164 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
166 aNewArray->SetValue(a + 1, theReferenced);
169 if (!aPassedPrev) // not found: unknown situation
170 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
171 aRefs->SetInternalArray(aNewArray);
175 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
177 if (!theFeature->isAction()) { // do not add action to the data model
178 TDF_Label aFeaturesLab = featuresLabel();
179 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
180 // store feature in the features array: before "initData" because in macro features
181 // in initData it creates new features, appeared later than this
182 TDF_Label aPrevFeateureLab;
183 FolderPtr aParentFolder;
184 if (theAfterThis.get()) { // searching for the previous feature label
185 std::shared_ptr<Model_Data> aPrevData =
186 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
187 if (aPrevData.get()) {
188 aPrevFeateureLab = aPrevData->label().Father();
190 // Check if the previous feature is the last feature in a folder,
191 // then the folder should be updated to contain additional feature.
192 // Macro features are not stored in folder.
193 if (!theFeature->isMacro()) {
194 // If the last feature is a sub-feature of composite, use parent feature
195 // to check belonging to a folder.
196 FeaturePtr afterThis = ModelAPI_Tools::compositeOwner(theAfterThis);
198 afterThis = theAfterThis;
199 aParentFolder = inFolder(afterThis, ModelAPI_Folder::LAST_FEATURE_ID());
202 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
204 // keep the feature ID to restore document later correctly
205 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
206 myFeatures.Bind(aFeatureLab, theFeature);
207 // must be before the event sending: for OB the feature is already added
208 updateHistory(ModelAPI_Feature::group());
209 // do not change the order:
212 // during python script with fillet constraint feature data should be
213 // initialized before using it in GUI
215 // must be after binding to the map because of "Box" macro feature that
216 // creates other features in "initData"
217 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
218 // put feature to the end of folder if it is added while
219 // the history line is set to the last feature from the folder
221 aParentFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(theFeature);
222 updateHistory(ModelAPI_Folder::group());
224 // event: feature is added, mist be before "initData" to update OB correctly on Duplicate:
225 // first new part, then the content
226 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
227 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
228 } else { // make feature has not-null data anyway
229 theFeature->setData(Model_Data::invalidData());
230 theFeature->setDoc(myDoc);
234 /// Appends to the array of references a new referenced label.
235 /// If theIndex is not -1, removes element at this index, not theReferenced.
236 /// \returns the index of removed element
237 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
238 const int theIndex = -1)
240 int aResult = -1; // no returned
241 Handle(TDataStd_ReferenceArray) aRefs;
242 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
243 if (aRefs->Length() == 1) { // just erase an array
244 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
245 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
248 } else { // reduce the array
249 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
251 int aCount = aRefs->Lower();
252 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
253 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
257 aNewArray->SetValue(aCount, aRefs->Value(a));
260 aRefs->SetInternalArray(aNewArray);
266 void Model_Objects::refsToFeature(FeaturePtr theFeature,
267 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
269 // check the feature: it must have no depended objects on it
270 // the dependencies can be in the feature results
271 std::list<ResultPtr> aResults;
272 ModelAPI_Tools::allResults(theFeature, aResults);
273 std::list<ResultPtr>::const_iterator aResIter = aResults.cbegin();
274 for (; aResIter != aResults.cend(); aResIter++) {
275 ResultPtr aResult = (*aResIter);
276 std::shared_ptr<Model_Data> aData =
277 std::dynamic_pointer_cast<Model_Data>(aResult->data());
278 if (aData.get() != NULL) {
279 const std::set<AttributePtr>& aRefs = aData->refsToMe();
280 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
281 for (; aRefIt != aRefLast; aRefIt++) {
282 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
283 if (aFeature.get() != NULL)
284 theRefs.insert(aFeature);
288 // the dependencies can be in the feature itself
289 std::shared_ptr<Model_Data> aData =
290 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
291 if (aData.get() && !aData->refsToMe().empty()) {
292 const std::set<AttributePtr>& aRefs = aData->refsToMe();
293 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
294 for (; aRefIt != aRefLast; aRefIt++) {
295 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
296 if (aFeature.get() != NULL)
297 theRefs.insert(aFeature);
301 if (!theRefs.empty() && isSendError) {
302 Events_InfoMessage("Model_Objects",
303 "Feature '%1' is used and can not be deleted").arg(theFeature->data()->name()).send();
307 void Model_Objects::removeFeature(FeaturePtr theFeature)
309 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
310 if (aData.get() && aData->isValid()) {
311 // checking that the sub-element of composite feature is removed: if yes, inform the owner
312 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
313 refsToFeature(theFeature, aRefs, false);
314 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
315 for(; aRefIter != aRefs.end(); aRefIter++) {
316 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
317 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
318 if (aComposite.get() && aComposite->data()->isValid() && aComposite->isSub(theFeature)) {
319 aComposite->removeFeature(theFeature);
322 // remove feature from folder
323 removeFromFolder(std::list<FeaturePtr>(1, theFeature));
324 // this must be before erase since theFeature erasing removes all information about
325 // the feature results and groups of results
326 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
327 clearHistory(theFeature);
331 TDF_Label aFeatureLabel = aData->label().Father();
332 if (myFeatures.IsBound(aFeatureLabel))
333 myFeatures.UnBind(aFeatureLabel);
335 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
336 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
337 // erase all attributes under the label of feature
338 aFeatureLabel.ForgetAllAttributes();
339 // remove it from the references array
340 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
341 // event: feature is deleted
342 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
343 updateHistory(ModelAPI_Feature::group());
347 void Model_Objects::eraseAllFeatures()
349 static Events_ID kDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
350 static const ModelAPI_EventCreator* kCreator = ModelAPI_EventCreator::get();
351 // make all features invalid (like deleted)
352 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
353 for(; aFIter.More(); aFIter.Next()) {
354 FeaturePtr aFeature = aFIter.Value();
355 std::list<ResultPtr> aResList;
356 ModelAPI_Tools::allResults(aFeature, aResList);
357 std::list<ResultPtr>::iterator aRIter = aResList.begin();
358 for(; aRIter != aResList.end(); aRIter++) {
359 ResultPtr aRes = *aRIter;
360 if (aRes && aRes->data()->isValid()) {
361 kCreator->sendDeleted(myDoc, aRes->groupName());
362 kCreator->sendUpdated(aRes, kDispEvent);
363 aRes->setData(aRes->data()->invalidPtr());
367 kCreator->sendUpdated(aFeature, kDispEvent);
368 aFeature->setData(aFeature->data()->invalidPtr());
370 kCreator->sendDeleted(myDoc, ModelAPI_Feature::group());
371 myFeatures.Clear(); // just remove features without modification of DS
375 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
377 TDF_Label aFeaturesLab = featuresLabel();
378 Handle(TDataStd_ReferenceArray) aRefs;
379 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
381 TDF_Label anAfterLab, aMovedLab =
382 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
383 if (theAfterThis.get())
384 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
386 // check whether some folder refers to the moved feature by start or end: if yes, remove from it
387 removeFromFolder(std::list<FeaturePtr>(1, theMoved));
389 Handle(TDataStd_HLabelArray1) aNewArray =
390 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
391 int aPassedMovedFrom = 0; // the previous feature location is found and passed
392 int aPassedMovedTo = 0; // the feature is added and this location is passed
393 if (!theAfterThis.get()) { // null means that inserted feature must be the first
394 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
397 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
398 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
400 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
401 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
402 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
403 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
405 } else { // just copy one
406 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
407 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
410 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
411 if (!aPassedMovedFrom) {
412 static std::string aMovedFromError("The moved feature is not found");
413 Events_InfoMessage("Model_Objects", aMovedFromError).send();
415 static std::string aMovedToError("The 'after' feature for movement is not found");
416 Events_InfoMessage("Model_Objects", aMovedToError).send();
420 // store the new array
421 aRefs->SetInternalArray(aNewArray);
422 // update the feature and the history
423 clearHistory(theMoved);
424 // make sure all (selection) attributes of moved feature will be updated
425 static Events_ID kUpdateSelection = Events_Loop::loop()->eventByName(EVENT_UPDATE_SELECTION);
426 ModelAPI_EventCreator::get()->sendUpdated(theMoved, kUpdateSelection, false);
427 ModelAPI_EventCreator::get()->sendReordered(theMoved);
430 void Model_Objects::clearHistory(ObjectPtr theObj)
433 const std::string aGroup = theObj->groupName();
434 updateHistory(aGroup);
436 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
437 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
438 std::string aResultGroup = featureResultGroup(aFeature);
439 if (!aResultGroup.empty()) {
440 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
441 myHistory.find(aResultGroup);
442 if (aHIter != myHistory.end())
443 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
449 void Model_Objects::createHistory(const std::string& theGroupID)
451 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
452 if (aHIter == myHistory.end()) {
453 std::vector<ObjectPtr> aResult;
454 std::vector<ObjectPtr> aResultOutOfFolder;
455 FeaturePtr aLastFeatureInFolder;
456 // iterate the array of references and get feature by feature from the array
457 bool isFeature = theGroupID == ModelAPI_Feature::group();
458 bool isFolder = theGroupID == ModelAPI_Folder::group();
459 Handle(TDataStd_ReferenceArray) aRefs;
460 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
461 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
462 FeaturePtr aFeature = feature(aRefs->Value(a));
463 if (aFeature.get()) {
464 // if feature is in sub-component, remove it from history:
465 // it is in sub-tree of sub-component
466 bool isSub = ModelAPI_Tools::compositeOwner(aFeature).get() != NULL;
467 if (isFeature) { // here may be also disabled features
468 if (!isSub && aFeature->isInHistory()) {
469 aResult.push_back(aFeature);
470 // the feature is out of the folders
471 if (aLastFeatureInFolder.get() == NULL)
472 aResultOutOfFolder.push_back(aFeature);
474 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
475 // construction results of sub-features should not be in the tree
476 if (!isSub || theGroupID != ModelAPI_ResultConstruction::group()) {
477 // do not use reference to the list here since results can be changed by "isConcealed"
478 const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
479 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator
480 aRIter = aResults.begin();
481 for (; aRIter != aResults.cend(); aRIter++) {
482 ResultPtr aRes = *aRIter;
483 if (aRes->groupName() != theGroupID) break; // feature have only same group results
484 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
485 aResult.push_back(*aRIter);
491 // the feature closes the folder, so the next features will be treated as out-of-folder
492 if (aLastFeatureInFolder.get() && aLastFeatureInFolder == aFeature)
493 aLastFeatureInFolder = FeaturePtr();
496 // it may be a folder
497 const ObjectPtr& aFolder = folder(aRefs->Value(a));
499 // store folder information for the Features group only
500 if (isFeature || isFolder) {
501 aResult.push_back(aFolder);
503 aResultOutOfFolder.push_back(aFolder);
506 // get the last feature in the folder
507 AttributeReferencePtr aLastFeatAttr =
508 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
510 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
515 // to be sure that isConcealed did not update the history (issue 1089) during the iteration
516 if (myHistory.find(theGroupID) == myHistory.end()) {
517 myHistory[theGroupID] = aResult;
519 // store the features placed out of any folder
520 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroupID, true);
521 if (!anOutOfFolderGroupID.empty())
522 myHistory[anOutOfFolderGroupID] = aResultOutOfFolder;
527 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
529 clearHistory(theObject);
532 void Model_Objects::updateHistory(const std::string theGroup)
534 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
535 if (aHIter != myHistory.end()) {
536 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
538 // erase history for the group of objects placed out of any folder
539 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroup, true);
540 if (!anOutOfFolderGroupID.empty())
541 myHistory.erase(anOutOfFolderGroupID);
545 const ObjectPtr& Model_Objects::folder(TDF_Label theLabel) const
547 if (myFolders.IsBound(theLabel))
548 return myFolders.Find(theLabel);
549 static ObjectPtr anEmptyResult;
550 return anEmptyResult;
553 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
555 if (myFeatures.IsBound(theLabel))
556 return myFeatures.Find(theLabel);
557 return FeaturePtr(); // not found
560 ObjectPtr Model_Objects::object(TDF_Label theLabel)
562 // try feature by label
563 FeaturePtr aFeature = feature(theLabel);
565 return feature(theLabel);
566 TDF_Label aFeatureLabel = theLabel; // let's suppose it is result of this feature
567 TDF_LabelList aSubLabs; // sub - labels from higher level to lower level of result
568 while(!aFeature.get() && aFeatureLabel.Depth() > 1) {
569 aSubLabs.Prepend(aFeatureLabel);
570 aFeatureLabel = aFeatureLabel.Father().Father();
571 aFeature = feature(aFeatureLabel);
573 if (aFeature.get()) {
574 ResultPtr aCurrentResult;
575 // searching for results then sub-results label by label
576 for(TDF_ListIteratorOfLabelList aSubLab(aSubLabs); aSubLab.More(); aSubLab.Next()) {
577 if (aCurrentResult.get()) { // iterate sub-results of result
578 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aCurrentResult);
580 return ObjectPtr(); // only Body can have sub-results
581 int a, aNumSubs = anOwner->numberOfSubs();
582 for(a = 0; a < aNumSubs; a++) {
583 ResultPtr aSub = anOwner->subResult(a);
585 std::shared_ptr<Model_Data> aSubData = std::dynamic_pointer_cast<Model_Data>(
587 const TDF_Label& aSubLabVal = aSubLab.ChangeValue();
588 if (aSubData->label().Father().IsEqual(aSubLabVal)) {
589 aCurrentResult = aSub;
594 if (a == aNumSubs) // not found an appropriate sub-result of result
596 } else { // iterate results of feature
597 const std::list<ResultPtr>& aResults = aFeature->results();
598 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
599 for(; aRIter != aResults.cend(); aRIter++) {
600 std::shared_ptr<Model_Data> aResData =
601 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
602 if (aResData->label().Father().IsEqual(aSubLab.ChangeValue())) {
603 aCurrentResult = *aRIter;
607 if (aRIter == aResults.cend()) // not found an appropriate result of feature
611 return aCurrentResult;
613 return ObjectPtr(); // not found
616 ObjectPtr Model_Objects::object(const std::string& theGroupID,
618 const bool theAllowFolder)
622 createHistory(theGroupID);
623 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
624 const std::vector<ObjectPtr>& aVec = myHistory[theGroupID];
625 //if (aVec.size() <= theIndex)
626 // return aVec[aVec.size() - 1]; // too high index requested (to avoid crash in #2360)
627 return aGroupID.empty() ? myHistory[theGroupID][theIndex] : myHistory[aGroupID][theIndex];
630 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
631 const std::string& theGroupID, const std::wstring& theName)
633 createHistory(theGroupID);
634 if (theGroupID == ModelAPI_Feature::group()) { // searching among features (in history or not)
635 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
636 // from the end to find the latest result with such name
637 std::list<std::shared_ptr<ModelAPI_Feature> >::reverse_iterator anObjIter = allObjs.rbegin();
638 for(; anObjIter != allObjs.rend(); anObjIter++) {
639 if ((*anObjIter)->data()->name() == theName)
642 } else { // searching among results (concealed or not)
643 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
644 // from the end to find the latest result with such name
645 std::list<std::shared_ptr<ModelAPI_Feature> >::reverse_iterator anObjIter = allObjs.rbegin();
646 for(; anObjIter != allObjs.rend(); anObjIter++) {
647 std::list<ResultPtr> allRes;
648 ModelAPI_Tools::allResults(*anObjIter, allRes);
649 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
650 if (aRes->get() && (*aRes)->groupName() == theGroupID) {
651 if ((*aRes)->data()->name() == theName)
661 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject,
662 const bool theAllowFolder)
664 std::string aGroup = theObject->groupName();
665 // treat folder as feature
666 if (aGroup == ModelAPI_Folder::group())
667 aGroup = ModelAPI_Feature::group();
668 createHistory(aGroup);
670 // get the group of features out of folder (if enabled)
671 if (theAllowFolder && !groupNameFoldering(aGroup, theAllowFolder).empty())
672 aGroup = groupNameFoldering(aGroup, theAllowFolder);
674 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
675 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
676 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
677 if ((*anObjIter) == theObject)
684 int Model_Objects::size(const std::string& theGroupID, const bool theAllowFolder)
686 createHistory(theGroupID);
687 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
688 return aGroupID.empty() ? int(myHistory[theGroupID].size()) : int(myHistory[aGroupID].size());
691 std::shared_ptr<ModelAPI_Object> Model_Objects::parent(
692 const std::shared_ptr<ModelAPI_Object> theChild)
694 if (theChild.get()) {
695 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theChild->data());
696 TDF_Label aLab = aData->label();
697 if (!aLab.IsNull() && aLab.Depth() > 2) {
698 ObjectPtr anObj = object(aLab.Father().Father().Father());
706 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
708 // iterate the array of references and get feature by feature from the array
709 Handle(TDataStd_ReferenceArray) aRefs;
710 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
711 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
712 FeaturePtr aFeature = feature(aRefs->Value(a));
713 if (aFeature.get()) {
714 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
715 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
716 for (; aRIter != aResults.cend(); aRIter++) {
717 ResultPtr aRes = *aRIter;
718 if (aRes->groupName() != theGroupID) break; // feature have only same group results
719 // iterate also concealed: ALL RESULTS (for translation parts undo/redo management)
720 //if (aRes->isInHistory() && !aRes->isConcealed()) {
721 theResults.push_back(*aRIter);
730 TDF_Label Model_Objects::featuresLabel() const
732 return myMain.FindChild(TAG_OBJECTS);
735 static std::wstring composeName(const std::string& theFeatureKind, const int theIndex)
737 std::stringstream aNameStream;
738 aNameStream << theFeatureKind << "_" << theIndex;
739 return Locale::Convert::toWString(aNameStream.str());
742 void Model_Objects::setUniqueName(FeaturePtr theFeature)
744 if (!theFeature->data()->name().empty())
745 return; // not needed, name is already defined
746 std::wstring aName; // result
747 // first count all features of such kind to start with index = count + 1
748 int aNumObjects = -1; // this feature is already in this map
749 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
750 std::set<std::wstring> allNames;
751 for (; aFIter.More(); aFIter.Next()) {
752 if (aFIter.Value()->getKind() == theFeature->getKind())
754 allNames.insert(aFIter.Value()->data()->name());
756 // generate candidate name
757 aName = composeName(theFeature->getKind(), aNumObjects + 1);
758 // check this is unique, if not, increase index by 1
759 for(aNumObjects++; allNames.find(aName) != allNames.end(); aNumObjects++) {
760 aName = composeName(theFeature->getKind(), aNumObjects + 1);
762 theFeature->data()->setName(aName);
765 void Model_Objects::setUniqueName(FolderPtr theFolder)
767 if (!theFolder->name().empty())
768 return; // name is already defined
770 int aNbFolders = myFolders.Size();
771 std::wstring aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
773 // check the uniqueness of the name
774 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator anIt(myFolders);
775 while (anIt.More()) {
776 if (anIt.Value()->data()->name() == aName) {
777 aName = composeName(ModelAPI_Folder::ID(), ++aNbFolders);
778 // reinitialize iterator to make sure a new name is unique
779 anIt.Initialize(myFolders);
784 theFolder->data()->setName(aName);
787 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
789 std::shared_ptr<Model_Data> aData(new Model_Data);
790 aData->setLabel(theLab.FindChild(theTag));
791 aData->setObject(theObj);
792 theObj->setDoc(myDoc);
793 theObj->setData(aData);
794 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
795 if (aFeature.get()) {
796 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
797 } else { // is it a folder?
798 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theObj);
800 setUniqueName(aFolder);
802 theObj->initAttributes();
805 void Model_Objects::synchronizeFeatures(
806 const TDF_LabelList& theUpdated, const bool theUpdateReferences,
807 const bool theExecuteFeatures, const bool theOpen, const bool theFlush)
809 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
810 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
812 // after all updates, sends a message that groups of features were created or updated
813 Events_Loop* aLoop = Events_Loop::loop();
814 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
815 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
816 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
817 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
818 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
819 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
820 bool isActive = aLoop->activateFlushes(false);
822 // collect all updated labels map
823 TDF_LabelMap anUpdatedMap;
824 TDF_ListIteratorOfLabelList anUpdatedIter(theUpdated);
825 for(; anUpdatedIter.More(); anUpdatedIter.Next()) {
826 TDF_Label& aFeatureLab = anUpdatedIter.Value();
827 while(aFeatureLab.Depth() > 3)
828 aFeatureLab = aFeatureLab.Father();
829 if (myFeatures.IsBound(aFeatureLab) || myFolders.IsBound(aFeatureLab))
830 anUpdatedMap.Add(aFeatureLab);
833 // update all objects by checking are they on labels or not
834 std::set<ObjectPtr> aNewFeatures, aKeptFeatures;
835 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
836 for (; aLabIter.More(); aLabIter.Next()) {
837 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
838 if (!myFeatures.IsBound(aFeatureLabel) && !myFolders.IsBound(aFeatureLabel)) {
839 // a new feature or folder is inserted
841 std::string aFeatureID = TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
842 aLabIter.Value())->Get()).ToCString();
843 bool isFolder = aFeatureID == ModelAPI_Folder::ID();
845 std::shared_ptr<Model_Session> aSession =
846 std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get());
849 ObjectPtr aFeature = isFolder ? ObjectPtr(new ModelAPI_Folder)
850 : ObjectPtr(aSession->createFeature(aFeatureID, anOwner));
851 if (!aFeature.get()) {
852 // something is wrong, most probably, the opened document has invalid structure
853 Events_InfoMessage("Model_Objects", "Invalid type of object in the document").send();
854 aLabIter.Value()->Label().ForgetAllAttributes();
858 // this must be before "setData" to redo the sketch line correctly
860 myFolders.Bind(aFeatureLabel, aFeature);
862 myFeatures.Bind(aFeatureLabel, std::dynamic_pointer_cast<ModelAPI_Feature>(aFeature));
863 aNewFeatures.insert(aFeature);
864 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
865 updateHistory(aFeature);
867 // event: model is updated
868 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
869 } else { // nothing is changed, both iterators are incremented
872 if (myFeatures.Find(aFeatureLabel, aFeature)) {
873 aKeptFeatures.insert(aFeature);
876 if (myFolders.Find(aFeatureLabel, anObject))
877 aKeptFeatures.insert(anObject);
879 if (anUpdatedMap.Contains(aFeatureLabel)) {
880 if (!theOpen) { // on abort/undo/redo reinitialize attributes if something is changed
881 FiltersFeaturePtr aFilter = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(anObject);
882 if (aFilter.get()) { // for filters attributes may be added/removed on undo/redo
883 std::dynamic_pointer_cast<Model_Data>(aFilter->data())->clearAttributes();
884 aFilter->initAttributes();
886 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
887 anObject->data()->attributes("");
888 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
889 for(; anAttr != anAttrs.end(); anAttr++)
891 // if feature contains results, re-init them too
892 if (aFeature.get()) {
893 std::list<ResultPtr> aResults;
894 ModelAPI_Tools::allResults(aFeature, aResults);
895 std::list<ResultPtr>::iterator aResIter = aResults.begin();
896 for(; aResIter != aResults.end(); aResIter++) {
897 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
898 (*aResIter)->data()->attributes("");
899 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
900 for(; anAttr != anAttrs.end(); anAttr++)
906 ModelAPI_EventCreator::get()->sendUpdated(anObject, anUpdateEvent);
907 if (aFeature && aFeature->getKind() == "Parameter") {
908 // if parameters are changed, update the results (issue 937)
909 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
910 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
911 for (; aRIter != aResults.cend(); aRIter++) {
912 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
913 if (aRes->data()->isValid() && !aRes->isDisabled()) {
914 ModelAPI_EventCreator::get()->sendUpdated(aRes, anUpdateEvent);
922 // check all features are checked: if not => it was removed
923 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
924 while (aFIter.More()) {
925 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
926 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
927 FeaturePtr aFeature = aFIter.Value();
928 // event: model is updated
929 //if (aFeature->isInHistory()) {
930 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
932 // results of this feature must be redisplayed (hided)
933 // redisplay also removed feature (used for sketch and AISObject)
934 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
935 updateHistory(aFeature);
938 // unbind after the "erase" call: on abort sketch
939 // is removes sub-objects that corrupts aFIter
940 myFeatures.UnBind(aFIter.Key());
941 // reinitialize iterator because unbind may corrupt the previous order in the map
942 aFIter.Initialize(myFeatures);
946 // verify folders are checked: if not => is was removed
947 for (NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFldIt(myFolders);
948 aFldIt.More(); aFldIt.Next()) {
949 ObjectPtr aCurObj = aFldIt.Value();
950 if (aKeptFeatures.find(aCurObj) == aKeptFeatures.end() &&
951 aNewFeatures.find(aCurObj) == aNewFeatures.end()) {
952 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
953 // results of this feature must be redisplayed (hided)
954 // redisplay also removed feature (used for sketch and AISObject)
955 ModelAPI_EventCreator::get()->sendUpdated(aCurObj, aRedispEvent);
956 updateHistory(aCurObj);
959 // unbind after the "erase" call: on abort sketch
960 // is removes sub-objects that corrupts aFIter
961 myFolders.UnBind(aFldIt.Key());
962 // reinitialize iterator because unbind may corrupt the previous order in the map
963 aFldIt.Initialize(myFolders);
967 if (theUpdateReferences) {
968 synchronizeBackRefs();
970 // update results of the features (after features created because
971 // they may be connected, like sketch and sub elements)
972 // After synchronization of back references because sketch
973 // must be set in sub-elements before "execute" by updateResults
974 std::set<FeaturePtr> aProcessed; // composites must be updated after their subs (issue 360)
975 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
976 for (; aLabIter2.More(); aLabIter2.Next()) {
977 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
978 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
979 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
980 updateResults(aFeature, aProcessed);
983 // the synchronize should be done after updateResults
984 // in order to correct back references of updated results
985 if (theUpdateReferences) {
986 synchronizeBackRefs();
988 if (!theUpdated.IsEmpty()) {
989 // this means there is no control what was modified => remove history cash
993 if (!theExecuteFeatures)
994 anOwner->setExecuteFeatures(false);
995 aLoop->activateFlushes(isActive);
998 aLoop->flush(aDeleteEvent);
999 // delete should be emitted before create to reacts to aborted feature
1000 aLoop->flush(aCreateEvent);
1001 aLoop->flush(anUpdateEvent);
1002 aLoop->flush(aCreateEvent); // after update of features, there could be results created
1003 aLoop->flush(aDeleteEvent); // or deleted
1004 aLoop->flush(aRedispEvent);
1005 aLoop->flush(aToHideEvent);
1007 if (!theExecuteFeatures)
1008 anOwner->setExecuteFeatures(true);
1011 /// synchronizes back references for the given object basing on the collected data
1012 void Model_Objects::synchronizeBackRefsForObject(const std::set<AttributePtr>& theNewRefs,
1013 ObjectPtr theObject)
1015 if (!theObject.get() || !theObject->data()->isValid())
1017 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
1018 // iterate new list to compare with current
1019 std::set<AttributePtr>::iterator aNewIter = theNewRefs.begin();
1020 for(; aNewIter != theNewRefs.end(); aNewIter++) {
1021 // for the Model_AttributeRefList erase cash (issue #2819)
1022 std::shared_ptr<Model_AttributeRefList> aRefList =
1023 std::dynamic_pointer_cast<Model_AttributeRefList>(*aNewIter);
1025 aRefList->eraseHash();
1027 if (aData->refsToMe().find(*aNewIter) == aData->refsToMe().end()) {
1028 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aNewIter)->owner());
1030 aData->addBackReference(aRefFeat, (*aNewIter)->id());
1031 else // add back reference to a folder
1032 aData->addBackReference((*aNewIter)->owner(), (*aNewIter)->id());
1035 if (theNewRefs.size() != aData->refsToMe().size()) { // some back ref must be removed
1036 std::set<AttributePtr>::iterator aCurrentIter = aData->refsToMe().begin();
1037 while(aCurrentIter != aData->refsToMe().end()) {
1038 if (theNewRefs.find(*aCurrentIter) == theNewRefs.end()) {
1039 // for external references from other documents this system
1040 // is not working: refs are collected from
1041 // different Model_Objects, so before remove check this
1042 // external object exists and still referenced
1043 bool aLeaveIt = false;
1044 if ((*aCurrentIter)->owner().get() && (*aCurrentIter)->owner()->document() != myDoc &&
1045 (*aCurrentIter)->owner()->data().get() && (*aCurrentIter)->owner()->data()->isValid()) {
1046 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
1047 (*aCurrentIter)->owner()->data()->referencesToObjects(aRefs);
1048 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> >>>::iterator
1049 aRefIter = aRefs.begin();
1050 for(; aRefIter != aRefs.end(); aRefIter++) {
1051 if ((*aCurrentIter)->id() == aRefIter->first) {
1052 std::list<std::shared_ptr<ModelAPI_Object> >::iterator anOIt;
1053 for(anOIt = aRefIter->second.begin(); anOIt != aRefIter->second.end(); anOIt++) {
1054 if (*anOIt == theObject) {
1062 aData->removeBackReference(*aCurrentIter);
1063 aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
1064 } else aCurrentIter++;
1065 } else aCurrentIter++;
1068 // for the last feature in the folder, check if it is a sub-feature,
1069 // then refer the folder to a top-level parent composite feature
1070 const std::set<AttributePtr>& aRefs = aData->refsToMe();
1071 std::set<AttributePtr>::iterator anIt = aRefs.begin();
1072 for (; anIt != aRefs.end(); ++anIt)
1073 if ((*anIt)->id() == ModelAPI_Folder::LAST_FEATURE_ID())
1075 if (anIt != aRefs.end()) {
1076 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1078 CompositeFeaturePtr aParent;
1079 CompositeFeaturePtr aGrandParent = ModelAPI_Tools::compositeOwner(aFeature);
1081 aParent = aGrandParent;
1083 aGrandParent = ModelAPI_Tools::compositeOwner(aParent);
1084 } while (aGrandParent.get());
1086 ObjectPtr aFolder = (*anIt)->owner();
1087 // remove reference from the current feature
1088 aData->removeBackReference(aFolder, ModelAPI_Folder::LAST_FEATURE_ID());
1089 // set reference to a top-level parent
1090 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aParent);
1091 std::shared_ptr<Model_Data> aParentData =
1092 std::dynamic_pointer_cast<Model_Data>(aParent->data());
1093 aParentData->addBackReference(aFolder, ModelAPI_Folder::LAST_FEATURE_ID());
1097 aData->updateConcealmentFlag();
1100 static void collectReferences(std::shared_ptr<ModelAPI_Data> theData,
1101 std::map<ObjectPtr, std::set<AttributePtr> >& theRefs)
1103 if (theData.get()) {
1104 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1105 theData->referencesToObjects(aRefs);
1106 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIt = aRefs.begin();
1107 for(; aRefsIt != aRefs.end(); aRefsIt++) {
1108 std::list<ObjectPtr>::iterator aRefTo = aRefsIt->second.begin();
1109 for(; aRefTo != aRefsIt->second.end(); aRefTo++) {
1111 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = theRefs.find(*aRefTo);
1112 if (aFound == theRefs.end()) {
1113 theRefs[*aRefTo] = std::set<AttributePtr>();
1114 aFound = theRefs.find(*aRefTo);
1116 aFound->second.insert(theData->attribute(aRefsIt->first));
1123 void Model_Objects::synchronizeBackRefs()
1125 // collect all back references in the separated container: to update everything at once,
1126 // without additional Concealment switching on and off: only the final modification
1128 // referenced (slave) objects to referencing attributes
1129 std::map<ObjectPtr, std::set<AttributePtr> > allRefs;
1130 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
1131 for(; aFeatures.More(); aFeatures.Next()) {
1132 FeaturePtr aFeature = aFeatures.Value();
1133 collectReferences(aFeature->data(), allRefs);
1135 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFolders(myFolders);
1136 for(; aFolders.More(); aFolders.Next()) {
1137 ObjectPtr aFolder = aFolders.Value();
1138 collectReferences(aFolder->data(), allRefs);
1140 // second iteration: just compare back-references with existing in features and results
1141 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1142 FeaturePtr aFeature = aFeatures.Value();
1143 static std::set<AttributePtr> anEmpty;
1144 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(aFeature);
1145 if (aFound == allRefs.end()) { // not found => erase all back references
1146 synchronizeBackRefsForObject(anEmpty, aFeature);
1148 synchronizeBackRefsForObject(aFound->second, aFeature);
1149 allRefs.erase(aFound); // to check that all refs are counted
1152 std::list<ResultPtr> aResults;
1153 ModelAPI_Tools::allResults(aFeature, aResults);
1154 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1155 for(; aRIter != aResults.cend(); aRIter++) {
1156 aFound = allRefs.find(*aRIter);
1157 if (aFound == allRefs.end()) { // not found => erase all back references
1158 synchronizeBackRefsForObject(anEmpty, *aRIter);
1160 synchronizeBackRefsForObject(aFound->second, *aRIter);
1161 allRefs.erase(aFound); // to check that all refs are counted
1165 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1166 FeaturePtr aFeature = aFeatures.Value();
1167 std::list<ResultPtr> aResults;
1168 ModelAPI_Tools::allResults(aFeature, aResults);
1169 // update the concealment status for display in isConcealed of ResultBody
1170 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1171 for(; aRIter != aResults.cend(); aRIter++) {
1172 (*aRIter)->isConcealed();
1175 // the rest all refs means that feature references to the external document feature:
1176 // process also them
1177 std::map<ObjectPtr, std::set<AttributePtr> >::iterator anExtIter = allRefs.begin();
1178 for(; anExtIter != allRefs.end(); anExtIter++) {
1179 synchronizeBackRefsForObject(anExtIter->second, anExtIter->first);
1183 TDF_Label Model_Objects::resultLabel(
1184 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1186 const std::shared_ptr<Model_Data>& aData =
1187 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1188 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1191 bool Model_Objects::hasCustomName(DataPtr theFeatureData,
1192 ResultPtr theResult,
1194 std::wstring& theParentName) const
1196 ResultBodyPtr aBodyRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theFeatureData->owner());
1198 // only for top-results (works for the cases when results are not yet added to the feature)
1199 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult);
1201 // names of sub-solids in CompSolid should be default (for example,
1202 // result of boolean operation 'Boolean_1' is a CompSolid which is renamed to 'MyBOOL',
1203 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1', 'Boolean_1_2' etc.)
1204 if (std::dynamic_pointer_cast<Model_Data>(aBodyRes->data())->label().Depth() == 6) {
1205 std::wostringstream aDefaultName;
1206 // compute default name of CompSolid (name of feature + index of CompSolid's result)
1207 int aBodyResultIndex = 0;
1208 const std::list<ResultPtr>& aResults = anOwner->results();
1209 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
1210 for (; anIt != aResults.end(); ++anIt, ++aBodyResultIndex)
1211 if (aBodyRes == *anIt)
1213 aDefaultName << anOwner->name();
1214 aDefaultName << "_" << (aBodyResultIndex + 1);
1215 theParentName = aDefaultName.str();
1216 } else { // just name of the parent result if it is deeper than just a sub-result
1217 theParentName = aBodyRes->data()->name();
1222 std::pair<std::wstring, bool> aName = ModelAPI_Tools::getDefaultName(theResult);
1224 theParentName = aName.first;
1225 return aName.second;
1228 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1229 std::shared_ptr<ModelAPI_Result> theResult,
1230 const int theResultIndex)
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;
1245 // if there are several results (issue #899: any number of result),
1246 // add unique prefix starting from second
1247 if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
1248 aName << "_" << theResultIndex + 1;
1249 aNewName = aName.str();
1251 theResult->data()->setName(aNewName);
1255 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
1256 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1258 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1259 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1260 ObjectPtr anOldObject = object(aLab);
1261 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1262 if (anOldObject.get()) {
1263 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1265 if (!aResult.get()) {
1266 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1267 storeResult(theFeatureData, aResult, theIndex);
1272 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
1273 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1275 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1276 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1277 ObjectPtr anOldObject = object(aLab);
1278 std::shared_ptr<ModelAPI_ResultBody> aResult;
1279 if (anOldObject.get()) {
1280 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1282 if (!aResult.get()) {
1283 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1284 storeResult(theFeatureData, aResult, theIndex);
1289 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
1290 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1292 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1293 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1294 ObjectPtr anOldObject = object(aLab);
1295 std::shared_ptr<ModelAPI_ResultPart> aResult;
1296 if (anOldObject.get()) {
1297 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1299 if (!aResult.get()) {
1300 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1301 storeResult(theFeatureData, aResult, theIndex);
1306 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
1307 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
1308 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1310 std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
1311 aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
1315 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
1316 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1318 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1319 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1320 ObjectPtr anOldObject = object(aLab);
1321 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1322 if (anOldObject.get()) {
1323 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1325 if (!aResult.get()) {
1326 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1327 storeResult(theFeatureData, aResult, theIndex);
1332 std::shared_ptr<ModelAPI_ResultField> Model_Objects::createField(
1333 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1335 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1336 TDataStd_Comment::Set(aLab, ModelAPI_ResultField::group().c_str());
1337 ObjectPtr anOldObject = object(aLab);
1338 std::shared_ptr<ModelAPI_ResultField> aResult;
1339 if (anOldObject.get()) {
1340 aResult = std::dynamic_pointer_cast<ModelAPI_ResultField>(anOldObject);
1342 if (!aResult.get()) {
1343 aResult = std::shared_ptr<ModelAPI_ResultField>(new Model_ResultField(theFeatureData));
1344 storeResult(theFeatureData, aResult, theIndex);
1349 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
1350 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1352 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1353 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1354 ObjectPtr anOldObject = object(aLab);
1355 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1356 if (anOldObject.get()) {
1357 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1359 if (!aResult.get()) {
1360 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1361 storeResult(theFeatureData, aResult, theIndex);
1366 std::shared_ptr<ModelAPI_Folder> Model_Objects::createFolder(
1367 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis)
1369 FolderPtr aFolder(new ModelAPI_Folder);
1373 TDF_Label aFeaturesLab = featuresLabel();
1374 TDF_Label aFolderLab = aFeaturesLab.NewChild();
1375 // store feature in the features array: before "initData" because in macro features
1376 // in initData it creates new features, appeared later than this
1377 TDF_Label aPrevFeatureLab;
1378 if (theBeforeThis.get()) { // searching for the previous feature label
1379 std::shared_ptr<Model_Data> aPrevData =
1380 std::dynamic_pointer_cast<Model_Data>(theBeforeThis->data());
1381 if (aPrevData.get()) {
1382 int anIndex = kUNDEFINED_FEATURE_INDEX;
1383 aPrevFeatureLab = nextLabel(aPrevData->label().Father(), anIndex, true);
1385 } else { // find the label of the last feature
1386 Handle(TDataStd_ReferenceArray) aRefs;
1387 if (aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1388 aPrevFeatureLab = aRefs->Value(aRefs->Upper());
1390 AddToRefArray(aFeaturesLab, aFolderLab, aPrevFeatureLab);
1392 // keep the feature ID to restore document later correctly
1393 TDataStd_Comment::Set(aFolderLab, ModelAPI_Folder::ID().c_str());
1394 myFolders.Bind(aFolderLab, aFolder);
1395 // must be before the event sending: for OB the feature is already added
1396 updateHistory(ModelAPI_Folder::group());
1397 updateHistory(ModelAPI_Feature::group());
1399 // must be after binding to the map because of "Box" macro feature that
1400 // creates other features in "initData"
1401 initData(aFolder, aFolderLab, TAG_FEATURE_ARGUMENTS);
1402 // event: folder is added, must be before "initData" to update OB correctly on Duplicate:
1403 // first new part, then the content
1404 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1405 ModelAPI_EventCreator::get()->sendUpdated(aFolder, anEvent);
1410 void Model_Objects::removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder)
1412 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFolder->data());
1413 if (!aData.get() || !aData->isValid())
1416 // this must be before erase since theFolder erasing removes all information about it
1417 clearHistory(theFolder);
1421 TDF_Label aFolderLabel = aData->label().Father();
1422 if (myFolders.IsBound(aFolderLabel))
1423 myFolders.UnBind(aFolderLabel);
1425 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1426 ModelAPI_EventCreator::get()->sendUpdated(theFolder, EVENT_DISP);
1427 // erase all attributes under the label of feature
1428 aFolderLabel.ForgetAllAttributes();
1429 // remove it from the references array
1430 RemoveFromRefArray(featuresLabel(), aFolderLabel);
1431 // event: feature is deleted
1432 ModelAPI_EventCreator::get()->sendDeleted(theFolder->document(), ModelAPI_Folder::group());
1433 updateHistory(ModelAPI_Folder::group());
1434 updateHistory(ModelAPI_Feature::group());
1437 // Returns one of the limiting features of the list
1438 static FeaturePtr limitingFeature(std::list<FeaturePtr>& theFeatures, const bool isLast)
1440 FeaturePtr aFeature;
1442 aFeature = theFeatures.back();
1443 theFeatures.pop_back();
1445 aFeature = theFeatures.front();
1446 theFeatures.pop_front();
1451 // Verify the feature is sub-element in composite feature or it is not used in the history
1452 static bool isSkippedFeature(FeaturePtr theFeature)
1454 bool isSub = ModelAPI_Tools::compositeOwner(theFeature).get() != NULL;
1455 return isSub || (theFeature && !theFeature->isInHistory());
1458 std::shared_ptr<ModelAPI_Folder> Model_Objects::findFolder(
1459 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1460 const bool theBelow)
1462 if (theFeatures.empty())
1463 return FolderPtr(); // nothing to move
1465 TDF_Label aFeaturesLab = featuresLabel();
1466 Handle(TDataStd_ReferenceArray) aRefs;
1467 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1468 return FolderPtr(); // no reference array (something is wrong)
1470 std::list<std::shared_ptr<ModelAPI_Feature> > aFeatures = theFeatures;
1471 std::shared_ptr<ModelAPI_Feature> aLimitingFeature = limitingFeature(aFeatures, theBelow);
1473 std::shared_ptr<Model_Data> aData =
1474 std::static_pointer_cast<Model_Data>(aLimitingFeature->data());
1475 if (!aData || !aData->isValid())
1476 return FolderPtr(); // invalid feature
1478 // label of the first feature in the list for fast searching
1479 TDF_Label aFirstFeatureLabel = aData->label().Father();
1481 // find a folder above the features and
1482 // check the given features represent a sequential list of objects following the folder
1483 FolderPtr aFoundFolder;
1484 TDF_Label aLastFeatureInFolder;
1485 int aRefIndex = aRefs->Lower();
1486 for(; aRefIndex <= aRefs->Upper(); ++aRefIndex) { // iterate all existing features
1487 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1488 if (IsEqual(aCurLabel, aFirstFeatureLabel))
1489 break; // no need to continue searching
1491 // searching the folder below, just continue to search last feature from the list
1495 // issue #18733: check for the last feature in folder before checking the sub-feature,
1496 // because the folder may end by the feature which is
1497 // neither a sub-feature nor a feature in history.
1498 if (!aLastFeatureInFolder.IsNull()) {
1499 if (IsEqual(aCurLabel, aLastFeatureInFolder))
1500 aLastFeatureInFolder.Nullify(); // the last feature in the folder is achieved
1504 // if feature is in sub-component, skip it
1505 FeaturePtr aCurFeature = feature(aCurLabel);
1506 if (isSkippedFeature(aCurFeature))
1509 const ObjectPtr& aFolderObj = folder(aCurLabel);
1510 if (aFolderObj.get()) {
1511 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aFolderObj);
1512 AttributeReferencePtr aLastFeatAttr =
1513 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1514 if (aLastFeatAttr) {
1515 // setup iterating inside a folder to find last feature
1516 ObjectPtr aLastFeature = aLastFeatAttr->value();
1518 aData = std::static_pointer_cast<Model_Data>(aLastFeature->data());
1519 if (aData && aData->isValid())
1520 aLastFeatureInFolder = aData->label().Father();
1526 if (theBelow && aRefIndex < aRefs->Upper()) {
1528 // skip following features which are sub-components or not in history
1529 for (int anIndex = aRefIndex + 1; anIndex <= aRefs->Upper(); ++anIndex) {
1530 aLabel = aRefs->Value(anIndex);
1531 FeaturePtr aCurFeature = feature(aLabel);
1532 if (!isSkippedFeature(aCurFeature))
1535 // check the next object is a folder
1536 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(folder(aLabel));
1539 if (!aLastFeatureInFolder.IsNull() || // the last feature of the folder above is not found
1543 // check the given features are sequential list
1544 int aStep = theBelow ? -1 : 1;
1545 for (aRefIndex += aStep;
1546 !aFeatures.empty() && aRefIndex >= aRefs->Lower() && aRefIndex <= aRefs->Upper();
1547 aRefIndex += aStep) {
1548 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1549 // if feature is in sub-component, skip it
1550 FeaturePtr aCurFeature = feature(aCurLabel);
1551 if (isSkippedFeature(aCurFeature))
1554 aLimitingFeature = limitingFeature(aFeatures, theBelow);
1555 if (!aCurFeature->data()->isEqual(aLimitingFeature->data()))
1556 return FolderPtr(); // not a sequential list
1559 return aFoundFolder;
1562 bool Model_Objects::moveToFolder(
1563 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1564 const std::shared_ptr<ModelAPI_Folder>& theFolder)
1566 if (theFeatures.empty() || !theFolder)
1569 // labels for the folder and last feature in the list
1570 TDF_Label aFolderLabel, aLastFeatureLabel;
1571 std::shared_ptr<Model_Data> aData =
1572 std::static_pointer_cast<Model_Data>(theFolder->data());
1573 if (aData && aData->isValid())
1574 aFolderLabel = aData->label().Father();
1575 aData = std::static_pointer_cast<Model_Data>(theFeatures.back()->data());
1576 if (aData && aData->isValid())
1577 aLastFeatureLabel = aData->label().Father();
1579 if (aFolderLabel.IsNull() || aLastFeatureLabel.IsNull())
1582 AttributeReferencePtr aFirstFeatAttr =
1583 theFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1584 AttributeReferencePtr aLastFeatAttr =
1585 theFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1586 bool initFirstAttr = !aFirstFeatAttr->value().get();
1587 bool initLastAttr = !aLastFeatAttr->value().get();
1589 // check the folder is below the list of features
1590 bool isFolderBelow = false;
1591 TDF_Label aFeaturesLab = featuresLabel();
1592 Handle(TDataStd_ReferenceArray) aRefs;
1593 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1594 return false; // no reference array (something is wrong)
1595 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1596 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1597 if (aCurLabel == aFolderLabel)
1598 break; // folder is above the features
1599 else if (aCurLabel == aLastFeatureLabel) {
1600 isFolderBelow = true;
1605 if (isFolderBelow) {
1606 aData = std::static_pointer_cast<Model_Data>(theFeatures.front()->data());
1607 if (!aData || !aData->isValid())
1609 TDF_Label aPrevFeatureLabel = aData->label().Father();
1610 // label of the feature before the first feature in the list
1611 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex)
1612 if (aPrevFeatureLabel == aRefs->Value(aRefIndex)) {
1613 if (aRefIndex == aRefs->Lower())
1614 aPrevFeatureLabel.Nullify();
1616 aPrevFeatureLabel = aRefs->Value(aRefIndex - 1);
1620 // move the folder in the list of references before the first feature
1621 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1622 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1623 // update first feature of the folder
1624 initFirstAttr = true;
1626 // update last feature of the folder
1627 initLastAttr = true;
1631 aFirstFeatAttr->setValue(theFeatures.front());
1633 aLastFeatAttr->setValue(theFeatures.back());
1635 updateHistory(ModelAPI_Feature::group());
1639 static FolderPtr isExtractionCorrect(const FolderPtr& theFirstFeatureFolder,
1640 const FolderPtr& theLastFeatureFolder,
1641 bool& isExtractBefore)
1643 if (theFirstFeatureFolder.get()) {
1644 if (theLastFeatureFolder.get())
1645 return theFirstFeatureFolder == theLastFeatureFolder ? theFirstFeatureFolder : FolderPtr();
1647 isExtractBefore = true;
1648 return theFirstFeatureFolder;
1649 } else if (theLastFeatureFolder.get()) {
1650 isExtractBefore = false;
1651 return theLastFeatureFolder;
1657 bool Model_Objects::removeFromFolder(
1658 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1659 const bool theBefore)
1661 if (theFeatures.empty())
1664 FolderPtr aFirstFeatureFolder =
1665 inFolder(theFeatures.front(), ModelAPI_Folder::FIRST_FEATURE_ID());
1666 FolderPtr aLastFeatureFolder =
1667 inFolder(theFeatures.back(), ModelAPI_Folder::LAST_FEATURE_ID());
1669 bool isExtractBeforeFolder = theBefore;
1670 FolderPtr aFoundFolder =
1671 isExtractionCorrect(aFirstFeatureFolder, aLastFeatureFolder, isExtractBeforeFolder);
1673 return false; // list of features cannot be extracted
1675 // references of the current folder
1676 ObjectPtr aFolderStartFeature;
1677 ObjectPtr aFolderEndFeature;
1678 if (aFirstFeatureFolder != aLastFeatureFolder) {
1679 aFolderStartFeature = aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->value();
1680 aFolderEndFeature = aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->value();
1683 FeaturePtr aFeatureToFind = isExtractBeforeFolder ? theFeatures.back() : theFeatures.front();
1684 std::shared_ptr<Model_Data> aData =
1685 std::static_pointer_cast<Model_Data>(aFeatureToFind->data());
1686 if (!aData || !aData->isValid())
1688 TDF_Label aLabelToFind = aData->label().Father();
1690 // search the label in the list of references
1691 TDF_Label aFeaturesLab = featuresLabel();
1692 Handle(TDataStd_ReferenceArray) aRefs;
1693 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1694 return false; // no reference array (something is wrong)
1695 int aRefIndex = aRefs->Lower();
1696 for (; aRefIndex <= aRefs->Upper(); ++aRefIndex)
1697 if (aRefs->Value(aRefIndex) == aLabelToFind)
1700 // update folder position
1701 if (isExtractBeforeFolder) {
1702 aData = std::dynamic_pointer_cast<Model_Data>(aFoundFolder->data());
1703 TDF_Label aFolderLabel = aData->label().Father();
1704 TDF_Label aPrevFeatureLabel = aRefs->Value(aRefIndex);
1705 // update start reference of the folder
1706 if (aFolderStartFeature.get()) {
1707 FeaturePtr aNewStartFeature;
1708 do { // skip all features placed in the composite features
1709 aPrevFeatureLabel = aRefs->Value(aRefIndex++);
1711 aRefIndex <= aRefs->Upper() ? feature(aRefs->Value(aRefIndex)) : FeaturePtr();
1712 } while (aNewStartFeature && isSkippedFeature(aNewStartFeature));
1713 aFolderStartFeature = aNewStartFeature;
1715 // move the folder in the list of references after the last feature from the list
1716 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1717 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1719 // update end reference of the folder
1720 if (aFolderEndFeature.get()) {
1721 FeaturePtr aNewEndFeature;
1722 do { // skip all features placed in the composite features
1725 aRefIndex >= aRefs->Lower() ? feature(aRefs->Value(aRefIndex)) : FeaturePtr();
1726 } while (aNewEndFeature && isSkippedFeature(aNewEndFeature));
1727 aFolderEndFeature = aNewEndFeature;
1731 // update folder references
1732 aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->setValue(aFolderStartFeature);
1733 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aFolderEndFeature);
1735 updateHistory(ModelAPI_Feature::group());
1739 FolderPtr Model_Objects::findContainingFolder(const FeaturePtr& theFeature, int& theIndexInFolder)
1741 // search the label in the list of references
1742 TDF_Label aFeaturesLab = featuresLabel();
1743 Handle(TDataStd_ReferenceArray) aRefs;
1744 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1745 return FolderPtr(); // no reference array (something is wrong)
1747 std::shared_ptr<Model_Data> aData =
1748 std::static_pointer_cast<Model_Data>(theFeature->data());
1749 if (!aData || !aData->isValid())
1751 TDF_Label aLabelToFind = aData->label().Father();
1753 theIndexInFolder = -1;
1754 FolderPtr aFoundFolder;
1755 TDF_Label aLastFeatureLabel;
1757 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1758 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1763 if (aCurLabel == aLabelToFind) { // the feature is reached
1765 if (isSkippedFeature(theFeature)) {
1766 theIndexInFolder = -1;
1769 // decrease the index of the feature in the folder by the number of skipped features
1770 for (int anIndex = theIndexInFolder - 1; anIndex > 0; anIndex--) {
1771 aCurLabel = aRefs->Value(aRefIndex - anIndex);
1772 if (isSkippedFeature(feature(aCurLabel)))
1776 return aFoundFolder;
1779 if (!aFoundFolder) {
1780 // if the current label refers to a folder, feel all necessary data
1781 const ObjectPtr& aFolderObj = folder(aCurLabel);
1782 if (aFolderObj.get()) {
1783 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aFolderObj);
1784 theIndexInFolder = -1;
1786 AttributeReferencePtr aLastRef =
1787 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1788 if (aLastRef->value()) {
1789 aData = std::static_pointer_cast<Model_Data>(aLastRef->value()->data());
1790 if (aData && aData->isValid())
1791 aLastFeatureLabel = aData->label().Father();
1792 } else // folder is empty
1793 aFoundFolder = FolderPtr();
1795 } else if (aLastFeatureLabel == aCurLabel) {
1796 // folder is finished, clear all stored data
1797 theIndexInFolder = -1;
1798 aFoundFolder = FolderPtr();
1802 // folder is not found
1803 theIndexInFolder = -1;
1808 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
1809 const std::shared_ptr<ModelAPI_Result>& theResult)
1811 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1812 if (aData.get() && aData->isValid()) {
1813 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1814 FeaturePtr aFeature = feature(aFeatureLab);
1815 while(!aFeature.get() && aFeatureLab.Depth() > 1) { // this may be sub-result of result
1816 aFeatureLab = aFeatureLab.Father().Father();
1817 aFeature = feature(aFeatureLab);
1821 return FeaturePtr();
1824 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
1826 if (theFeature->data()->isValid()) {
1827 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1828 if (aLabIter.More()) {
1829 TDF_Label anArgLab = aLabIter.Value();
1830 Handle(TDataStd_Comment) aGroup;
1831 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1832 return TCollection_AsciiString(aGroup->Get()).ToCString();
1836 static std::string anEmpty;
1837 return anEmpty; // not found
1840 void Model_Objects::updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
1842 if (theProcessed.find(theFeature) != theProcessed.end())
1844 theProcessed.insert(theFeature);
1845 // for composites update subs recursively (sketch elements results are needed for the sketch)
1846 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
1847 if (aComp.get() && aComp->getKind() != "Part") { // don't go inside of parts sub-features
1848 // update subs of composites first
1849 int aSubNum = aComp->numberOfSubs();
1850 for(int a = 0; a < aSubNum; a++) {
1851 FeaturePtr aSub = aComp->subFeature(a);
1852 updateResults(aComp->subFeature(a), theProcessed);
1856 // it may be on undo
1857 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
1860 // check the existing results and remove them if there is nothing on the label
1861 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1862 while(aResIter != theFeature->results().cend()) {
1863 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1865 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(aBody->data());
1866 if (!aData.get() || !aData->isValid() || (!aBody->isDisabled() && aData->isDeleted())) {
1867 // found a disappeared result => remove it
1868 theFeature->eraseResultFromList(aBody);
1869 // start iterate from beginning because iterator is corrupted by removing
1870 aResIter = theFeature->results().cbegin();
1876 // check that results are presented on all labels
1877 int aResSize = int(theFeature->results().size());
1878 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1879 for(; aLabIter.More(); aLabIter.Next()) {
1880 // here must be GUID of the feature
1881 int aResIndex = aLabIter.Value().Tag() - 1;
1883 if (aResSize <= aResIndex) {
1884 TDF_Label anArgLab = aLabIter.Value();
1885 Handle(TDataStd_Comment) aGroup;
1886 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1887 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1888 aNewBody = createBody(theFeature->data(), aResIndex);
1889 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1890 if (aResIndex <= theFeature->results().size()) { // to avoid crash if previous execute
1891 // for index = 0 erases result
1892 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
1893 if (!aNewP->data()->isDeleted()) {
1894 theFeature->setResult(aNewP, aResIndex);
1895 if (!aNewP->partDoc().get())
1896 // create the part result: it is better to restore the previous result if possible
1897 theFeature->execute();
1900 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1901 ResultConstructionPtr aConstr = createConstruction(theFeature->data(), aResIndex);
1902 if (!aConstr->updateShape())
1903 theFeature->execute(); // not stored shape in the data structure, execute to have it
1905 theFeature->setResult(aConstr, aResIndex); // result is ready without execution
1906 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1907 aNewBody = createGroup(theFeature->data(), aResIndex);
1908 } else if (aGroup->Get() == ModelAPI_ResultField::group().c_str()) {
1909 ResultFieldPtr aField = createField(theFeature->data(), aResIndex);
1910 aField->updateSteps(); // to refresh the internal data
1912 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1913 theFeature->attributeChanged("expression"); // just produce a value
1915 Events_InfoMessage("Model_Objects", "Unknown type of result is found in the document:")
1916 .arg(TCollection_AsciiString(aGroup->Get()).ToCString()).send();
1919 if (aNewBody && !aNewBody->data()->isDeleted()) {
1920 theFeature->setResult(aNewBody, aResIndex);
1924 if (aResSize > 0) { // check there exist a body that must be updated
1925 std::list<ResultPtr>::const_iterator aRes = theFeature->results().cbegin();
1926 for (; aResSize && aRes != theFeature->results().cend(); aRes++, aResSize++) {
1927 if ((*aRes)->data()->isValid()) {
1928 if ((*aRes)->groupName() == ModelAPI_ResultBody::group()) {
1929 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aRes);
1930 aBody->updateSubs(aBody->shape(), false);
1931 } else if ((*aRes)->groupName() == ModelAPI_ResultConstruction::group()) {
1932 // update the cashed myShape presented in construction
1933 ResultConstructionPtr aConstr =
1934 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
1935 aConstr->updateShape();
1942 ResultPtr Model_Objects::findByName(const std::wstring theName)
1945 FeaturePtr aResFeature; // keep feature to return the latest one
1946 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
1947 for(; anObjIter.More(); anObjIter.Next()) {
1948 FeaturePtr& aFeature = anObjIter.ChangeValue();
1949 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
1951 std::list<ResultPtr> allResults;
1952 ModelAPI_Tools::allResults(aFeature, allResults);
1953 std::list<ResultPtr>::iterator aRIter = allResults.begin();
1954 for (; aRIter != allResults.cend(); aRIter++) {
1955 ResultPtr aRes = *aRIter;
1956 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
1957 aRes->data()->name() == theName)
1959 if (!aResult.get() || isLater(aFeature, aResFeature)) { // select the latest
1961 aResFeature = aFeature;
1969 TDF_Label Model_Objects::nextLabel(TDF_Label theCurrent, int& theIndex, const bool theReverse)
1971 Handle(TDataStd_ReferenceArray) aRefs;
1972 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1973 int aStart = theIndex == kUNDEFINED_FEATURE_INDEX ? aRefs->Lower() : theIndex;
1974 for(int a = aStart; a <= aRefs->Upper(); a++) { // iterate all existing features
1975 TDF_Label aCurLab = aRefs->Value(a);
1976 if (aCurLab.IsEqual(theCurrent)) {
1977 a += theReverse ? -1 : 1;
1978 if (a >= aRefs->Lower() && a <= aRefs->Upper()) {
1980 return aRefs->Value(a);
1982 break; // finish iteration: it's last feature
1989 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, int& theIndex, const bool theReverse)
1991 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
1992 if (aData.get() && aData->isValid()) {
1993 TDF_Label aFeatureLabel = aData->label().Father();
1995 TDF_Label aNextLabel = nextLabel(aFeatureLabel, theIndex, theReverse);
1996 if (aNextLabel.IsNull())
1997 break; // the last or something is wrong
1998 FeaturePtr aFound = feature(aNextLabel);
2000 return aFound; // the feature is found
2001 // if the next label is a folder, skip it
2002 aFeatureLabel = folder(aNextLabel).get() ? aNextLabel : TDF_Label();
2003 } while (!aFeatureLabel.IsNull());
2005 return FeaturePtr(); // not found, last, or something is wrong
2008 FeaturePtr Model_Objects::firstFeature()
2010 Handle(TDataStd_ReferenceArray) aRefs;
2011 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2012 return feature(aRefs->Value(aRefs->Lower()));
2014 return FeaturePtr(); // no features at all
2017 FeaturePtr Model_Objects::lastFeature()
2019 Handle(TDataStd_ReferenceArray) aRefs;
2020 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2021 // comment this because of #2674 - features are removed from array on deactivation of Part
2022 /*FeaturePtr aLast = feature(aRefs->Value(aRefs->Upper()));
2023 if (!aLast.get() && aRefs->Length() != 0) { // erase the invalid feature from the array
2024 RemoveFromRefArray(featuresLabel(), aRefs->Value(aRefs->Upper()));
2025 return lastFeature(); // try once again, after the last was removed
2027 return feature(aRefs->Value(aRefs->Upper()));
2029 return FeaturePtr(); // no features at all
2032 bool Model_Objects::isLater(FeaturePtr theLater, FeaturePtr theCurrent) const
2034 std::shared_ptr<Model_Data> aLaterD = std::static_pointer_cast<Model_Data>(theLater->data());
2035 std::shared_ptr<Model_Data> aCurrentD = std::static_pointer_cast<Model_Data>(theCurrent->data());
2036 if (aLaterD.get() && aLaterD->isValid() && aCurrentD.get() && aCurrentD->isValid()) {
2037 TDF_Label aLaterL = aLaterD->label().Father();
2038 TDF_Label aCurrentL = aCurrentD->label().Father();
2039 int aLaterI = -1, aCurentI = -1; // not found yet state
2040 Handle(TDataStd_ReferenceArray) aRefs;
2041 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2042 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
2043 TDF_Label aCurLab = aRefs->Value(a);
2044 if (aCurLab.IsEqual(aLaterL)) {
2046 } else if (aCurLab.IsEqual(aCurrentL)) {
2049 if (aLaterI != -1 && aCurentI != -1) // both are found
2050 return aLaterI > aCurentI;
2054 return false; // not found, or something is wrong
2057 std::list<std::shared_ptr<ModelAPI_Object> > Model_Objects::allObjects()
2059 std::list<std::shared_ptr<ModelAPI_Object> > aResult;
2060 Handle(TDataStd_ReferenceArray) aRefs;
2061 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2062 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
2063 ObjectPtr anObject = object(aRefs->Value(a));
2064 if (!anObject.get()) // is it a folder?
2065 anObject = folder(aRefs->Value(a));
2067 aResult.push_back(anObject);
2073 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
2075 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
2076 Handle(TDataStd_ReferenceArray) aRefs;
2077 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2078 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
2079 FeaturePtr aFeature = feature(aRefs->Value(a));
2081 aResult.push_back(aFeature);
2087 int Model_Objects::numInternalFeatures()
2089 Handle(TDataStd_ReferenceArray) aRefs;
2090 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2091 return aRefs->Upper() - aRefs->Lower() + 1;
2093 return 0; // invalid
2096 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
2098 Handle(TDataStd_ReferenceArray) aRefs;
2099 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
2100 return feature(aRefs->Value(aRefs->Lower() + theIndex));
2102 return FeaturePtr(); // invalid
2105 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
2107 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
2110 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
2112 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);