1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Model_Objects.h>
22 #include <Model_Data.h>
23 #include <Model_Document.h>
24 #include <Model_Events.h>
25 #include <Model_Session.h>
26 #include <Model_ResultPart.h>
27 #include <Model_ResultConstruction.h>
28 #include <Model_ResultBody.h>
29 #include <Model_ResultCompSolid.h>
30 #include <Model_ResultGroup.h>
31 #include <Model_ResultField.h>
32 #include <Model_ResultParameter.h>
33 #include <ModelAPI_Validator.h>
34 #include <ModelAPI_CompositeFeature.h>
35 #include <ModelAPI_Tools.h>
37 #include <Events_Loop.h>
38 #include <Events_InfoMessage.h>
40 #include <TDataStd_Integer.hxx>
41 #include <TDataStd_Comment.hxx>
42 #include <TDF_ChildIDIterator.hxx>
43 #include <TDataStd_ReferenceArray.hxx>
44 #include <TDataStd_HLabelArray1.hxx>
45 #include <TDataStd_Name.hxx>
46 #include <TDF_Reference.hxx>
47 #include <TDF_ChildIDIterator.hxx>
48 #include <TDF_LabelMapHasher.hxx>
49 #include <TDF_LabelMap.hxx>
50 #include <TDF_ListIteratorOfLabelList.hxx>
52 static const std::string& groupNameFoldering(const std::string& theGroupID,
53 const bool theAllowFolder)
56 static const std::string anOutOfFolderName = std::string("__") + ModelAPI_Feature::group();
57 static const std::string aDummyName;
58 return theGroupID == ModelAPI_Feature::group() ? anOutOfFolderName : aDummyName;
64 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
67 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
68 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
71 /// 0:1:2 - where features are located
72 /// 0:1:2:N:1 - data of the feature N
73 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
75 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
79 void Model_Objects::setOwner(DocumentPtr theDoc)
82 // update all fields and recreate features and result objects if needed
83 TDF_LabelList aNoUpdated;
84 synchronizeFeatures(aNoUpdated, true, true, true, true);
88 Model_Objects::~Model_Objects()
90 // delete all features of this document
91 Events_Loop* aLoop = Events_Loop::loop();
92 // erase one by one to avoid access from the feature destructor itself from he map
93 // blocks the flush signals to avoid the temporary objects visualization in the viewer
94 // they should not be shown in order to do not lose highlight by erasing them
95 bool isActive = aLoop->activateFlushes(false);
97 while(!myFeatures.IsEmpty()) {
98 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
99 FeaturePtr aFeature = aFeaturesIter.Value();
100 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
101 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
102 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
103 aFeature->removeResults(0, false);
105 myFeatures.UnBind(aFeaturesIter.Key());
107 while (!myFolders.IsEmpty()) {
108 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFoldersIter(myFolders);
109 ObjectPtr aFolder = aFoldersIter.Value();
110 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
111 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
112 ModelAPI_EventCreator::get()->sendUpdated(aFolder, EVENT_DISP);
114 myFolders.UnBind(aFoldersIter.Key());
117 aLoop->activateFlushes(isActive);
118 // erase update, because features are destroyed and update should not performed for them anywhere
119 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
120 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
121 // deleted and redisplayed is correctly performed: they know that features are destroyed
122 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
123 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
127 /// Appends to the array of references a new referenced label
128 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
130 Handle(TDataStd_ReferenceArray) aRefs;
131 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
132 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
133 aRefs->SetValue(0, theReferenced);
134 } else { // extend array by one more element
135 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
137 int aPassedPrev = 0; // prev feature is found and passed
138 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
139 aNewArray->SetValue(aRefs->Lower(), theReferenced);
142 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
143 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
144 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
146 aNewArray->SetValue(a + 1, theReferenced);
149 if (!aPassedPrev) // not found: unknown situation
150 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
151 aRefs->SetInternalArray(aNewArray);
155 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
157 if (!theFeature->isAction()) { // do not add action to the data model
158 TDF_Label aFeaturesLab = featuresLabel();
159 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
160 // store feature in the features array: before "initData" because in macro features
161 // in initData it creates new features, appeared later than this
162 TDF_Label aPrevFeateureLab;
163 if (theAfterThis.get()) { // searching for the previous feature label
164 std::shared_ptr<Model_Data> aPrevData =
165 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
166 if (aPrevData.get()) {
167 aPrevFeateureLab = aPrevData->label().Father();
170 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
172 // keep the feature ID to restore document later correctly
173 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
174 myFeatures.Bind(aFeatureLab, theFeature);
175 // must be before the event sending: for OB the feature is already added
176 updateHistory(ModelAPI_Feature::group());
177 // do not change the order:
180 // during python script with fillet constraint feature data should be
181 // initialized before using it in GUI
183 // must be after binding to the map because of "Box" macro feature that
184 // creates other features in "initData"
185 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
186 // event: feature is added, mist be before "initData" to update OB correctly on Duplicate:
187 // first new part, then the content
188 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
189 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
190 } else { // make feature has not-null data anyway
191 theFeature->setData(Model_Data::invalidData());
192 theFeature->setDoc(myDoc);
196 /// Appends to the array of references a new referenced label.
197 /// If theIndex is not -1, removes element at this index, not theReferenced.
198 /// \returns the index of removed element
199 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
200 const int theIndex = -1)
202 int aResult = -1; // no returned
203 Handle(TDataStd_ReferenceArray) aRefs;
204 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
205 if (aRefs->Length() == 1) { // just erase an array
206 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
207 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
210 } else { // reduce the array
211 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
213 int aCount = aRefs->Lower();
214 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
215 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
219 aNewArray->SetValue(aCount, aRefs->Value(a));
222 aRefs->SetInternalArray(aNewArray);
228 void Model_Objects::refsToFeature(FeaturePtr theFeature,
229 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
231 // check the feature: it must have no depended objects on it
232 // the dependencies can be in the feature results
233 std::list<ResultPtr> aResults;
234 ModelAPI_Tools::allResults(theFeature, aResults);
235 std::list<ResultPtr>::const_iterator aResIter = aResults.cbegin();
236 for (; aResIter != aResults.cend(); aResIter++) {
237 ResultPtr aResult = (*aResIter);
238 std::shared_ptr<Model_Data> aData =
239 std::dynamic_pointer_cast<Model_Data>(aResult->data());
240 if (aData.get() != NULL) {
241 const std::set<AttributePtr>& aRefs = aData->refsToMe();
242 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
243 for (; aRefIt != aRefLast; aRefIt++) {
244 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
245 if (aFeature.get() != NULL)
246 theRefs.insert(aFeature);
250 // the dependencies can be in the feature itself
251 std::shared_ptr<Model_Data> aData =
252 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
253 if (aData.get() && !aData->refsToMe().empty()) {
254 const std::set<AttributePtr>& aRefs = aData->refsToMe();
255 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
256 for (; aRefIt != aRefLast; aRefIt++) {
257 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
258 if (aFeature.get() != NULL)
259 theRefs.insert(aFeature);
263 if (!theRefs.empty() && isSendError) {
264 Events_InfoMessage("Model_Objects",
265 "Feature '%1' is used and can not be deleted").arg(theFeature->data()->name()).send();
269 void Model_Objects::removeFeature(FeaturePtr theFeature)
271 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
272 if (aData.get() && aData->isValid()) {
273 // checking that the sub-element of composite feature is removed: if yes, inform the owner
274 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
275 refsToFeature(theFeature, aRefs, false);
276 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
277 for(; aRefIter != aRefs.end(); aRefIter++) {
278 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
279 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
280 if (aComposite.get() && aComposite->isSub(theFeature)) {
281 aComposite->removeFeature(theFeature);
284 // this must be before erase since theFeature erasing removes all information about
285 // the feature results and groups of results
286 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
287 clearHistory(theFeature);
291 TDF_Label aFeatureLabel = aData->label().Father();
292 if (myFeatures.IsBound(aFeatureLabel))
293 myFeatures.UnBind(aFeatureLabel);
295 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
296 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
297 // erase all attributes under the label of feature
298 aFeatureLabel.ForgetAllAttributes();
299 // remove it from the references array
300 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
301 // event: feature is deleted
302 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
303 updateHistory(ModelAPI_Feature::group());
307 void Model_Objects::eraseAllFeatures()
309 static Events_ID kDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
310 static const ModelAPI_EventCreator* kCreator = ModelAPI_EventCreator::get();
311 // make all features invalid (like deleted)
312 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
313 for(; aFIter.More(); aFIter.Next()) {
314 FeaturePtr aFeature = aFIter.Value();
315 std::list<ResultPtr> aResList;
316 ModelAPI_Tools::allResults(aFeature, aResList);
317 std::list<ResultPtr>::iterator aRIter = aResList.begin();
318 for(; aRIter != aResList.end(); aRIter++) {
319 ResultPtr aRes = *aRIter;
320 if (aRes && aRes->data()->isValid()) {
321 kCreator->sendDeleted(myDoc, aRes->groupName());
322 kCreator->sendUpdated(aRes, kDispEvent);
323 aRes->setData(aRes->data()->invalidPtr());
327 kCreator->sendUpdated(aFeature, kDispEvent);
328 aFeature->setData(aFeature->data()->invalidPtr());
330 kCreator->sendDeleted(myDoc, ModelAPI_Feature::group());
331 myFeatures.Clear(); // just remove features without modification of DS
332 updateHistory(ModelAPI_Feature::group());
335 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
337 TDF_Label aFeaturesLab = featuresLabel();
338 Handle(TDataStd_ReferenceArray) aRefs;
339 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
341 TDF_Label anAfterLab, aMovedLab =
342 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
343 if (theAfterThis.get())
344 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
346 Handle(TDataStd_HLabelArray1) aNewArray =
347 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
348 int aPassedMovedFrom = 0; // the prev feature location is found and passed
349 int aPassedMovedTo = 0; // the feature is added and this location is passed
350 if (!theAfterThis.get()) { // null means that inserted feature must be the first
351 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
354 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
355 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
357 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
358 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
359 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
360 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
362 } else { // just copy one
363 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
364 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
367 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
368 if (!aPassedMovedFrom) {
369 static std::string aMovedFromError("The moved feature is not found");
370 Events_InfoMessage("Model_Objects", aMovedFromError).send();
372 static std::string aMovedToError("The 'after' feature for movement is not found");
373 Events_InfoMessage("Model_Objects", aMovedToError).send();
377 // store the new array
378 aRefs->SetInternalArray(aNewArray);
379 // update the feature and the history
380 clearHistory(theMoved);
381 // make sure all (selection) attributes of moved feature will be updated
382 static Events_ID kUpdateSelection = Events_Loop::loop()->eventByName(EVENT_UPDATE_SELECTION);
383 ModelAPI_EventCreator::get()->sendUpdated(theMoved, kUpdateSelection, false);
384 ModelAPI_EventCreator::get()->sendReordered(theMoved);
387 void Model_Objects::clearHistory(ObjectPtr theObj)
390 const std::string aGroup = theObj->groupName();
391 updateHistory(aGroup);
393 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
394 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
395 std::string aResultGroup = featureResultGroup(aFeature);
396 if (!aResultGroup.empty()) {
397 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
398 myHistory.find(aResultGroup);
399 if (aHIter != myHistory.end())
400 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
406 void Model_Objects::createHistory(const std::string& theGroupID)
408 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
409 if (aHIter == myHistory.end()) {
410 std::vector<ObjectPtr> aResult;
411 std::vector<ObjectPtr> aResultOutOfFolder;
412 FeaturePtr aLastFeatureInFolder;
413 // iterate the array of references and get feature by feature from the array
414 bool isFeature = theGroupID == ModelAPI_Feature::group();
415 bool isFolder = theGroupID == ModelAPI_Folder::group();
416 Handle(TDataStd_ReferenceArray) aRefs;
417 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
418 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
419 FeaturePtr aFeature = feature(aRefs->Value(a));
420 if (aFeature.get()) {
421 // if feature is in sub-component, remove it from history:
422 // it is in sub-tree of sub-component
423 bool isSub = ModelAPI_Tools::compositeOwner(aFeature).get() != NULL;
424 if (isFeature) { // here may be also disabled features
425 if (!isSub && aFeature->isInHistory()) {
426 aResult.push_back(aFeature);
427 // the feature is out of the folders
428 if (aLastFeatureInFolder.get() == NULL)
429 aResultOutOfFolder.push_back(aFeature);
431 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
432 // construction results of sub-features should not be in the tree
433 if (!isSub || theGroupID != ModelAPI_ResultConstruction::group()) {
434 // do not use reference to the list here since results can be changed by "isConcealed"
435 const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
436 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator
437 aRIter = aResults.begin();
438 for (; aRIter != aResults.cend(); aRIter++) {
439 ResultPtr aRes = *aRIter;
440 if (aRes->groupName() != theGroupID) break; // feature have only same group results
441 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
442 aResult.push_back(*aRIter);
448 // the feature closes the folder, so the next features will be treated as out-of-folder
449 if (aLastFeatureInFolder.get() && aLastFeatureInFolder == aFeature)
450 aLastFeatureInFolder = FeaturePtr();
453 // it may be a folder
454 ObjectPtr aFolder = folder(aRefs->Value(a));
456 // store folder information for the Features group only
457 if (isFeature || isFolder) {
458 aResult.push_back(aFolder);
460 aResultOutOfFolder.push_back(aFolder);
463 // get the last feature in the folder
464 AttributeReferencePtr aLastFeatAttr =
465 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
467 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
472 // to be sure that isConcealed did not update the history (issue 1089) during the iteration
473 if (myHistory.find(theGroupID) == myHistory.end()) {
474 myHistory[theGroupID] = aResult;
476 // store the features placed out of any folder
477 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroupID, true);
478 if (!anOutOfFolderGroupID.empty())
479 myHistory[anOutOfFolderGroupID] = aResultOutOfFolder;
484 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
486 clearHistory(theObject);
489 void Model_Objects::updateHistory(const std::string theGroup)
491 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
492 if (aHIter != myHistory.end()) {
493 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
495 // erase history for the group of objects placed out of any folder
496 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroup, true);
497 if (!anOutOfFolderGroupID.empty())
498 myHistory.erase(anOutOfFolderGroupID);
502 ObjectPtr Model_Objects::folder(TDF_Label theLabel) const
504 if (myFolders.IsBound(theLabel))
505 return myFolders.Find(theLabel);
509 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
511 if (myFeatures.IsBound(theLabel))
512 return myFeatures.Find(theLabel);
513 return FeaturePtr(); // not found
516 ObjectPtr Model_Objects::object(TDF_Label theLabel)
518 // try feature by label
519 FeaturePtr aFeature = feature(theLabel);
521 return feature(theLabel);
522 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
523 aFeature = feature(aFeatureLabel);
524 bool isSubResult = false;
525 if (!aFeature.get() && aFeatureLabel.Depth() > 1) { // let's suppose this is sub-result of result
526 aFeatureLabel = aFeatureLabel.Father().Father();
527 aFeature = feature(aFeatureLabel);
530 if (aFeature.get()) {
531 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
532 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
533 for (; aRIter != aResults.cend(); aRIter++) {
535 ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
536 if (aCompRes.get()) {
537 int aNumSubs = aCompRes->numberOfSubs();
538 for(int a = 0; a < aNumSubs; a++) {
539 ResultPtr aSub = aCompRes->subResult(a);
541 std::shared_ptr<Model_Data> aSubData = std::dynamic_pointer_cast<Model_Data>(
543 if (aSubData->label().Father().IsEqual(theLabel))
549 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
551 if (aResData->label().Father().IsEqual(theLabel))
556 return FeaturePtr(); // not found
559 ObjectPtr Model_Objects::object(const std::string& theGroupID,
561 const bool theAllowFolder)
565 createHistory(theGroupID);
566 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
567 return aGroupID.empty() ? myHistory[theGroupID][theIndex] : myHistory[aGroupID][theIndex];
570 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
571 const std::string& theGroupID, const std::string& theName)
573 createHistory(theGroupID);
574 if (theGroupID == ModelAPI_Feature::group()) { // searching among features (in history or not)
575 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
576 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator anObjIter = allObjs.begin();
577 for(; anObjIter != allObjs.end(); anObjIter++) {
578 if ((*anObjIter)->data()->name() == theName)
581 } else { // searching among results (concealed or not)
582 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
583 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator anObjIter = allObjs.begin();
584 for(; anObjIter != allObjs.end(); anObjIter++) {
585 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = (*anObjIter)->results();
586 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
587 for (; aRIter != aResults.cend(); aRIter++) {
588 if (aRIter->get() && (*aRIter)->groupName() == theGroupID) {
589 if ((*aRIter)->data()->name() == theName)
591 ResultCompSolidPtr aCompRes =
592 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
593 if (aCompRes.get()) {
594 int aNumSubs = aCompRes->numberOfSubs();
595 for(int a = 0; a < aNumSubs; a++) {
596 ResultPtr aSub = aCompRes->subResult(a);
597 if (aSub.get() && aSub->groupName() == theGroupID) {
598 if (aSub->data()->name() == theName)
611 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject,
612 const bool theAllowFolder)
614 std::string aGroup = theObject->groupName();
615 // treat folder as feature
616 if (aGroup == ModelAPI_Folder::group())
617 aGroup = ModelAPI_Feature::group();
618 createHistory(aGroup);
620 // get the group of features out of folder (if enabled)
621 if (theAllowFolder && !groupNameFoldering(aGroup, theAllowFolder).empty())
622 aGroup = groupNameFoldering(aGroup, theAllowFolder);
624 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
625 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
626 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
627 if ((*anObjIter) == theObject)
634 int Model_Objects::size(const std::string& theGroupID, const bool theAllowFolder)
636 createHistory(theGroupID);
637 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
638 return aGroupID.empty() ? int(myHistory[theGroupID].size()) : int(myHistory[aGroupID].size());
641 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
643 // iterate the array of references and get feature by feature from the array
644 Handle(TDataStd_ReferenceArray) aRefs;
645 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
646 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
647 FeaturePtr aFeature = feature(aRefs->Value(a));
648 if (aFeature.get()) {
649 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
650 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
651 for (; aRIter != aResults.cend(); aRIter++) {
652 ResultPtr aRes = *aRIter;
653 if (aRes->groupName() != theGroupID) break; // feature have only same group results
654 // iterate also concealed: ALL RESULTS (for translation parts undo/redo management)
655 //if (aRes->isInHistory() && !aRes->isConcealed()) {
656 theResults.push_back(*aRIter);
665 TDF_Label Model_Objects::featuresLabel() const
667 return myMain.FindChild(TAG_OBJECTS);
670 static std::string composeName(const std::string& theFeatureKind, const int theIndex)
672 std::stringstream aNameStream;
673 aNameStream << theFeatureKind << "_" << theIndex;
674 return aNameStream.str();
677 void Model_Objects::setUniqueName(FeaturePtr theFeature)
679 if (!theFeature->data()->name().empty())
680 return; // not needed, name is already defined
681 std::string aName; // result
682 // first count all features of such kind to start with index = count + 1
683 int aNumObjects = -1; // this feature is already in this map
684 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
685 for (; aFIter.More(); aFIter.Next()) {
686 if (aFIter.Value()->getKind() == theFeature->getKind())
689 // generate candidate name
690 aName = composeName(theFeature->getKind(), aNumObjects + 1);
691 // check this is unique, if not, increase index by 1
692 for (aFIter.Initialize(myFeatures); aFIter.More();) {
693 FeaturePtr aFeature = aFIter.Value();
694 bool isSameName = aFeature->data()->name() == aName;
695 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
696 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
697 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
698 for (; aRIter != aResults.cend(); aRIter++) {
699 isSameName = (*aRIter)->data()->name() == aName;
705 aName = composeName(theFeature->getKind(), aNumObjects + 1);
706 // reinitialize iterator to make sure a new name is unique
707 aFIter.Initialize(myFeatures);
711 theFeature->data()->setName(aName);
714 void Model_Objects::setUniqueName(FolderPtr theFolder)
716 if (!theFolder->name().empty())
717 return; // name is already defined
719 int aNbFolders = myFolders.Size();
720 std::string aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
722 // check the uniqueness of the name
723 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator anIt(myFolders);
724 while (anIt.More()) {
725 if (anIt.Value()->data()->name() == aName) {
726 aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
727 // reinitialize iterator to make sure a new name is unique
728 anIt.Initialize(myFolders);
733 theFolder->data()->setName(aName);
736 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
738 std::shared_ptr<Model_Data> aData(new Model_Data);
739 aData->setLabel(theLab.FindChild(theTag));
740 aData->setObject(theObj);
741 theObj->setDoc(myDoc);
742 theObj->setData(aData);
743 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
744 if (aFeature.get()) {
745 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
746 } else { // is it a folder?
747 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theObj);
749 setUniqueName(aFolder);
751 theObj->initAttributes();
754 std::shared_ptr<ModelAPI_Feature> Model_Objects::featureById(const int theId)
757 TDF_Label aLab = featuresLabel().FindChild(theId, Standard_False);
758 return feature(aLab);
760 return std::shared_ptr<ModelAPI_Feature>(); // not found
763 void Model_Objects::synchronizeFeatures(
764 const TDF_LabelList& theUpdated, const bool theUpdateReferences,
765 const bool theExecuteFeatures, const bool theOpen, const bool theFlush)
767 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
768 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
770 // after all updates, sends a message that groups of features were created or updated
771 Events_Loop* aLoop = Events_Loop::loop();
772 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
773 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
774 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
775 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
776 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
777 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
778 bool isActive = aLoop->activateFlushes(false);
780 // collect all updated labels map
781 TDF_LabelMap anUpdatedMap;
782 TDF_ListIteratorOfLabelList anUpdatedIter(theUpdated);
783 for(; anUpdatedIter.More(); anUpdatedIter.Next()) {
784 TDF_Label& aFeatureLab = anUpdatedIter.Value();
785 while(aFeatureLab.Depth() > 3)
786 aFeatureLab = aFeatureLab.Father();
787 if (myFeatures.IsBound(aFeatureLab))
788 anUpdatedMap.Add(aFeatureLab);
791 // update all objects by checking are they on labels or not
792 std::set<ObjectPtr> aNewFeatures, aKeptFeatures;
793 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
794 for (; aLabIter.More(); aLabIter.Next()) {
795 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
796 if (!myFeatures.IsBound(aFeatureLabel) && !myFolders.IsBound(aFeatureLabel)) {
797 // a new feature or folder is inserted
799 std::string aFeatureID = TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
800 aLabIter.Value())->Get()).ToCString();
801 bool isFolder = aFeatureID == ModelAPI_Folder::ID();
803 std::shared_ptr<Model_Session> aSession =
804 std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get());
807 ObjectPtr aFeature = isFolder ? ObjectPtr(new ModelAPI_Folder)
808 : ObjectPtr(aSession->createFeature(aFeatureID, anOwner));
809 if (!aFeature.get()) {
810 // somethig is wrong, most probably, the opened document has invalid structure
811 Events_InfoMessage("Model_Objects", "Invalid type of object in the document").send();
812 aLabIter.Value()->Label().ForgetAllAttributes();
816 // this must be before "setData" to redo the sketch line correctly
818 myFolders.Bind(aFeatureLabel, aFeature);
820 myFeatures.Bind(aFeatureLabel, std::dynamic_pointer_cast<ModelAPI_Feature>(aFeature));
821 aNewFeatures.insert(aFeature);
822 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
823 updateHistory(aFeature);
825 // event: model is updated
826 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
827 } else { // nothing is changed, both iterators are incremented
830 if (myFeatures.Find(aFeatureLabel, aFeature)) {
831 aKeptFeatures.insert(aFeature);
834 if (myFolders.Find(aFeatureLabel, anObject))
835 aKeptFeatures.insert(anObject);
837 if (anUpdatedMap.Contains(aFeatureLabel)) {
838 if (!theOpen) { // on abort/undo/redo reinitialize attributes if something is changed
839 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
840 anObject->data()->attributes("");
841 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
842 for(; anAttr != anAttrs.end(); anAttr++)
845 ModelAPI_EventCreator::get()->sendUpdated(anObject, anUpdateEvent);
846 if (aFeature && aFeature->getKind() == "Parameter") {
847 // if parameters are changed, update the results (issue 937)
848 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
849 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
850 for (; aRIter != aResults.cend(); aRIter++) {
851 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
852 if (aRes->data()->isValid() && !aRes->isDisabled()) {
853 ModelAPI_EventCreator::get()->sendUpdated(aRes, anUpdateEvent);
861 // check all features are checked: if not => it was removed
862 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
863 while (aFIter.More()) {
864 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
865 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
866 FeaturePtr aFeature = aFIter.Value();
867 // event: model is updated
868 //if (aFeature->isInHistory()) {
869 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
871 // results of this feature must be redisplayed (hided)
872 // redisplay also removed feature (used for sketch and AISObject)
873 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
874 updateHistory(aFeature);
877 // unbind after the "erase" call: on abort sketch
878 // is removes sub-objects that corrupts aFIter
879 myFeatures.UnBind(aFIter.Key());
880 // reinitialize iterator because unbind may corrupt the previous order in the map
881 aFIter.Initialize(myFeatures);
885 // verify folders are checked: if not => is was removed
886 for (NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFldIt(myFolders);
887 aFldIt.More(); aFldIt.Next()) {
888 ObjectPtr aCurObj = aFldIt.Value();
889 if (aKeptFeatures.find(aCurObj) == aKeptFeatures.end() &&
890 aNewFeatures.find(aCurObj) == aNewFeatures.end()) {
891 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
892 // results of this feature must be redisplayed (hided)
893 // redisplay also removed feature (used for sketch and AISObject)
894 ModelAPI_EventCreator::get()->sendUpdated(aCurObj, aRedispEvent);
895 updateHistory(aCurObj);
898 // unbind after the "erase" call: on abort sketch
899 // is removes sub-objects that corrupts aFIter
900 myFolders.UnBind(aFldIt.Key());
904 if (theUpdateReferences) {
905 synchronizeBackRefs();
907 // update results of the features (after features created because
908 // they may be connected, like sketch and sub elements)
909 // After synchronisation of back references because sketch
910 // must be set in sub-elements before "execute" by updateResults
911 std::set<FeaturePtr> aProcessed; // composites must be updated after their subs (issue 360)
912 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
913 for (; aLabIter2.More(); aLabIter2.Next()) {
914 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
915 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
916 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
917 updateResults(aFeature, aProcessed);
920 // the synchronize should be done after updateResults
921 // in order to correct back references of updated results
922 if (theUpdateReferences) {
923 synchronizeBackRefs();
925 if (!theUpdated.IsEmpty()) {
926 // this means there is no control what was modified => remove history cash
930 if (theExecuteFeatures)
931 anOwner->executeFeatures() = false;
932 aLoop->activateFlushes(isActive);
935 aLoop->flush(aDeleteEvent);
936 // delete should be emitted before create to reacts to aborted feature
937 aLoop->flush(aCreateEvent);
938 aLoop->flush(anUpdateEvent);
939 aLoop->flush(aCreateEvent); // after update of features, there could be results created
940 aLoop->flush(aDeleteEvent); // or deleted
941 aLoop->flush(aRedispEvent);
942 aLoop->flush(aToHideEvent);
944 if (theExecuteFeatures)
945 anOwner->executeFeatures() = true;
948 /// synchronises back references for the given object basing on the collected data
949 void Model_Objects::synchronizeBackRefsForObject(const std::set<AttributePtr>& theNewRefs,
952 if (!theObject.get() || !theObject->data()->isValid())
954 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
955 // iterate new list to compare with curent
956 std::set<AttributePtr>::iterator aNewIter = theNewRefs.begin();
957 for(; aNewIter != theNewRefs.end(); aNewIter++) {
958 if (aData->refsToMe().find(*aNewIter) == aData->refsToMe().end()) {
959 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aNewIter)->owner());
961 aData->addBackReference(aRefFeat, (*aNewIter)->id());
962 else // add back reference to a folder
963 aData->addBackReference((*aNewIter)->owner(), (*aNewIter)->id());
966 if (theNewRefs.size() != aData->refsToMe().size()) { // some back ref must be removed
967 std::set<AttributePtr>::iterator aCurrentIter = aData->refsToMe().begin();
968 while(aCurrentIter != aData->refsToMe().end()) {
969 if (theNewRefs.find(*aCurrentIter) == theNewRefs.end()) {
970 // for external references from other documents this system
971 // is not working: refs are collected from
972 // different Model_Objects, so before remove check this
973 // external object exists and still referenced
974 bool aLeaveIt = false;
975 if ((*aCurrentIter)->owner().get() && (*aCurrentIter)->owner()->document() != myDoc &&
976 (*aCurrentIter)->owner()->data().get() && (*aCurrentIter)->owner()->data()->isValid()) {
977 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
978 (*aCurrentIter)->owner()->data()->referencesToObjects(aRefs);
979 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> >>>::iterator
980 aRefIter = aRefs.begin();
981 for(; aRefIter != aRefs.end(); aRefIter++) {
982 if ((*aCurrentIter)->id() == aRefIter->first) {
983 std::list<std::shared_ptr<ModelAPI_Object> >::iterator anOIt;
984 for(anOIt = aRefIter->second.begin(); anOIt != aRefIter->second.end(); anOIt++) {
985 if (*anOIt == theObject) {
993 aData->removeBackReference(*aCurrentIter);
994 aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
995 } else aCurrentIter++;
996 } else aCurrentIter++;
999 aData->updateConcealmentFlag();
1002 static void collectReferences(std::shared_ptr<ModelAPI_Data> theData,
1003 std::map<ObjectPtr, std::set<AttributePtr> >& theRefs)
1005 if (theData.get()) {
1006 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1007 theData->referencesToObjects(aRefs);
1008 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIt = aRefs.begin();
1009 for(; aRefsIt != aRefs.end(); aRefsIt++) {
1010 std::list<ObjectPtr>::iterator aRefTo = aRefsIt->second.begin();
1011 for(; aRefTo != aRefsIt->second.end(); aRefTo++) {
1013 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = theRefs.find(*aRefTo);
1014 if (aFound == theRefs.end()) {
1015 theRefs[*aRefTo] = std::set<AttributePtr>();
1016 aFound = theRefs.find(*aRefTo);
1018 aFound->second.insert(theData->attribute(aRefsIt->first));
1025 void Model_Objects::synchronizeBackRefs()
1027 // collect all back references in the separated container: to update everything at once,
1028 // without additional Concealment switchin on and off: only the final modification
1030 // referenced (slave) objects to referencing attirbutes
1031 std::map<ObjectPtr, std::set<AttributePtr> > allRefs;
1032 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
1033 for(; aFeatures.More(); aFeatures.Next()) {
1034 FeaturePtr aFeature = aFeatures.Value();
1035 collectReferences(aFeature->data(), allRefs);
1037 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFolders(myFolders);
1038 for(; aFolders.More(); aFolders.Next()) {
1039 ObjectPtr aFolder = aFolders.Value();
1040 collectReferences(aFolder->data(), allRefs);
1042 // second iteration: just compare back-references with existing in features and results
1043 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1044 FeaturePtr aFeature = aFeatures.Value();
1045 static std::set<AttributePtr> anEmpty;
1046 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(aFeature);
1047 if (aFound == allRefs.end()) { // not found => erase all back references
1048 synchronizeBackRefsForObject(anEmpty, aFeature);
1050 synchronizeBackRefsForObject(aFound->second, aFeature);
1051 allRefs.erase(aFound); // to check that all refs are counted
1054 std::list<ResultPtr> aResults;
1055 ModelAPI_Tools::allResults(aFeature, aResults);
1056 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1057 for(; aRIter != aResults.cend(); aRIter++) {
1058 aFound = allRefs.find(*aRIter);
1059 if (aFound == allRefs.end()) { // not found => erase all back references
1060 synchronizeBackRefsForObject(anEmpty, *aRIter);
1062 synchronizeBackRefsForObject(aFound->second, *aRIter);
1063 allRefs.erase(aFound); // to check that all refs are counted
1067 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1068 FeaturePtr aFeature = aFeatures.Value();
1069 std::list<ResultPtr> aResults;
1070 ModelAPI_Tools::allResults(aFeature, aResults);
1071 // update the concealment status for disply in isConcealed of ResultBody
1072 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1073 for(; aRIter != aResults.cend(); aRIter++) {
1074 (*aRIter)->isConcealed();
1077 // the rest all refs means that feature references to the external document feature:
1078 // process also them
1079 std::map<ObjectPtr, std::set<AttributePtr> >::iterator anExtIter = allRefs.begin();
1080 for(; anExtIter != allRefs.end(); anExtIter++) {
1081 synchronizeBackRefsForObject(anExtIter->second, anExtIter->first);
1085 TDF_Label Model_Objects::resultLabel(
1086 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1088 const std::shared_ptr<Model_Data>& aData =
1089 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1090 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1093 bool Model_Objects::hasCustomName(DataPtr theFeatureData,
1094 ResultPtr theResult,
1096 std::string& theParentName) const
1098 ResultCompSolidPtr aCompSolidRes =
1099 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theFeatureData->owner());
1100 if (aCompSolidRes) {
1101 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult->data()->owner());
1103 // names of sub-solids in CompSolid should be default (for example,
1104 // result of boolean operation 'Boolean_1' is a CompSolid which is renamed to 'MyBOOL',
1105 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1', 'Boolean_1_2' etc.)
1106 std::ostringstream aDefaultName;
1107 aDefaultName << anOwner->name();
1108 // compute default name of CompSolid (name of feature + index of CompSolid's result)
1109 int aCompSolidResultIndex = 0;
1110 const std::list<ResultPtr>& aResults = anOwner->results();
1111 for (std::list<ResultPtr>::const_iterator anIt = aResults.begin();
1112 anIt != aResults.end(); ++anIt, ++aCompSolidResultIndex)
1113 if (aCompSolidRes == *anIt)
1115 aDefaultName << "_" << (aCompSolidResultIndex + 1);
1116 theParentName = aDefaultName.str();
1120 theParentName = ModelAPI_Tools::getDefaultName(theResult, theResultIndex);
1124 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1125 std::shared_ptr<ModelAPI_Result> theResult,
1126 const int theResultIndex)
1129 theResult->setDoc(myDoc);
1130 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1131 if (theResult->data()->name().empty()) {
1132 // if was not initialized, generate event and set a name
1133 std::string aNewName = theFeatureData->name();
1134 if (!hasCustomName(theFeatureData, theResult, theResultIndex, aNewName)) {
1135 std::stringstream aName;
1137 // if there are several results (issue #899: any number of result),
1138 // add unique prefix starting from second
1139 if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
1140 aName << "_" << theResultIndex + 1;
1141 aNewName = aName.str();
1143 theResult->data()->setName(aNewName);
1147 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
1148 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1150 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1151 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1152 ObjectPtr anOldObject = object(aLab);
1153 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1154 if (anOldObject.get()) {
1155 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1157 if (!aResult.get()) {
1158 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1159 storeResult(theFeatureData, aResult, theIndex);
1164 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
1165 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1167 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1168 // for feature create compsolid, but for result sub create body:
1169 // only one level of recursion is supported now
1170 ResultPtr aResultOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theFeatureData->owner());
1171 ObjectPtr anOldObject;
1172 if (aResultOwner.get()) {
1173 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1174 } else { // in compsolid (higher level result) old object probably may be found
1175 TDataStd_Comment::Set(aLab, ModelAPI_ResultCompSolid::group().c_str());
1176 anOldObject = object(aLab);
1178 std::shared_ptr<ModelAPI_ResultBody> aResult;
1179 if (anOldObject.get()) {
1180 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1182 if (!aResult.get()) {
1183 // create compsolid anyway; if it is compsolid, it will create sub-bodies internally
1184 if (aResultOwner.get()) {
1185 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1187 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultCompSolid);
1189 storeResult(theFeatureData, aResult, theIndex);
1194 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
1195 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1197 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1198 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1199 ObjectPtr anOldObject = object(aLab);
1200 std::shared_ptr<ModelAPI_ResultPart> aResult;
1201 if (anOldObject.get()) {
1202 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1204 if (!aResult.get()) {
1205 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1206 storeResult(theFeatureData, aResult, theIndex);
1211 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
1212 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
1213 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1215 std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
1216 aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
1220 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
1221 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1223 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1224 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1225 ObjectPtr anOldObject = object(aLab);
1226 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1227 if (anOldObject.get()) {
1228 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1230 if (!aResult.get()) {
1231 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1232 storeResult(theFeatureData, aResult, theIndex);
1237 std::shared_ptr<ModelAPI_ResultField> Model_Objects::createField(
1238 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1240 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1241 TDataStd_Comment::Set(aLab, ModelAPI_ResultField::group().c_str());
1242 ObjectPtr anOldObject = object(aLab);
1243 std::shared_ptr<ModelAPI_ResultField> aResult;
1244 if (anOldObject.get()) {
1245 aResult = std::dynamic_pointer_cast<ModelAPI_ResultField>(anOldObject);
1247 if (!aResult.get()) {
1248 aResult = std::shared_ptr<ModelAPI_ResultField>(new Model_ResultField(theFeatureData));
1249 storeResult(theFeatureData, aResult, theIndex);
1254 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
1255 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1257 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1258 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1259 ObjectPtr anOldObject = object(aLab);
1260 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1261 if (anOldObject.get()) {
1262 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1264 if (!aResult.get()) {
1265 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1266 storeResult(theFeatureData, aResult, theIndex);
1271 std::shared_ptr<ModelAPI_Folder> Model_Objects::createFolder(
1272 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis)
1274 FolderPtr aFolder(new ModelAPI_Folder);
1278 TDF_Label aFeaturesLab = featuresLabel();
1279 TDF_Label aFolderLab = aFeaturesLab.NewChild();
1280 // store feature in the features array: before "initData" because in macro features
1281 // in initData it creates new features, appeared later than this
1282 TDF_Label aPrevFeatureLab;
1283 if (theBeforeThis.get()) { // searching for the previous feature label
1284 std::shared_ptr<Model_Data> aPrevData =
1285 std::dynamic_pointer_cast<Model_Data>(theBeforeThis->data());
1286 if (aPrevData.get()) {
1287 aPrevFeatureLab = nextLabel(aPrevData->label().Father(), true);
1289 } else { // find the label of the last feature
1290 Handle(TDataStd_ReferenceArray) aRefs;
1291 if (aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1292 aPrevFeatureLab = aRefs->Value(aRefs->Upper());
1294 AddToRefArray(aFeaturesLab, aFolderLab, aPrevFeatureLab);
1296 // keep the feature ID to restore document later correctly
1297 TDataStd_Comment::Set(aFolderLab, ModelAPI_Folder::ID().c_str());
1298 myFolders.Bind(aFolderLab, aFolder);
1299 // must be before the event sending: for OB the feature is already added
1300 updateHistory(ModelAPI_Folder::group());
1301 updateHistory(ModelAPI_Feature::group());
1303 // must be after binding to the map because of "Box" macro feature that
1304 // creates other features in "initData"
1305 initData(aFolder, aFolderLab, TAG_FEATURE_ARGUMENTS);
1306 // event: folder is added, must be before "initData" to update OB correctly on Duplicate:
1307 // first new part, then the content
1308 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1309 ModelAPI_EventCreator::get()->sendUpdated(aFolder, anEvent);
1314 void Model_Objects::removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder)
1316 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFolder->data());
1317 if (!aData.get() || !aData->isValid())
1320 // this must be before erase since theFolder erasing removes all information about it
1321 clearHistory(theFolder);
1325 TDF_Label aFolderLabel = aData->label().Father();
1326 if (myFolders.IsBound(aFolderLabel))
1327 myFolders.UnBind(aFolderLabel);
1329 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1330 ModelAPI_EventCreator::get()->sendUpdated(theFolder, EVENT_DISP);
1331 // erase all attributes under the label of feature
1332 aFolderLabel.ForgetAllAttributes();
1333 // remove it from the references array
1334 RemoveFromRefArray(featuresLabel(), aFolderLabel);
1335 // event: feature is deleted
1336 ModelAPI_EventCreator::get()->sendDeleted(theFolder->document(), ModelAPI_Folder::group());
1337 updateHistory(ModelAPI_Folder::group());
1338 updateHistory(ModelAPI_Feature::group());
1341 static FeaturePtr limitingFeature(std::list<FeaturePtr>& theFeatures, const bool isLast)
1343 FeaturePtr aFeature;
1345 aFeature = theFeatures.back();
1346 theFeatures.pop_back();
1348 aFeature = theFeatures.front();
1349 theFeatures.pop_front();
1354 // Obtain index of last sub-feature in the composite feature.
1355 // Parameter theIndex is an index of theComposite feature in theReferences list.
1356 static void lastIndexOfComposite(Model_Objects* theObjectsEnt,
1357 const FeaturePtr& theComposite,
1358 const Handle(TDataStd_ReferenceArray)& theReferences,
1361 CompositeFeaturePtr aComposite =
1362 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theComposite);
1366 for (; theIndex < theReferences->Upper(); ++theIndex) {
1367 TDF_Label aNextLab = theReferences->Value(theIndex + 1);
1368 ObjectPtr aNextObj = theObjectsEnt->object(aNextLab);
1369 if (!aComposite->isSub(aNextObj))
1374 std::shared_ptr<ModelAPI_Folder> Model_Objects::findFolder(
1375 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1376 const bool theBelow)
1378 if (theFeatures.empty())
1379 return FolderPtr(); // nothing to move
1381 TDF_Label aFeaturesLab = featuresLabel();
1382 Handle(TDataStd_ReferenceArray) aRefs;
1383 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1384 return FolderPtr(); // no reference array (something is wrong)
1386 // obtain first or last feature of the list according to the direction where to search a folder
1387 std::list<std::shared_ptr<ModelAPI_Feature> > aFeatures = theFeatures;
1388 std::shared_ptr<ModelAPI_Feature> aLimitingFeature = limitingFeature(aFeatures, theBelow);
1389 std::shared_ptr<Model_Data> aData =
1390 std::static_pointer_cast<Model_Data>(aLimitingFeature->data());
1391 if (!aData || !aData->isValid())
1392 return FolderPtr(); // invalid feature
1394 // label of the first feature in the list for fast searching
1395 TDF_Label aFirstFeatureLabel = aData->label().Father();
1397 // Search applicable folder (above or below the list of features):
1398 // [above] - find in the list of references the last folder
1399 // before the first feature from the list;
1400 // [below] - skip all references until last feature in the list is found,
1401 // then check the next reference is a folder.
1402 FolderPtr aFoundFolder;
1403 TDF_Label aLastFeatureInFolder;
1404 int aRefIndex = aRefs->Lower();
1405 for(; aRefIndex <= aRefs->Upper(); ++aRefIndex) { // iterate all existing features
1406 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1407 if (IsEqual(aCurLabel, aFirstFeatureLabel))
1408 break; // no need to continue searching
1410 // searching the folder below, just continue to search last feature from the list
1414 // skip features in the current folder
1415 if (!aLastFeatureInFolder.IsNull()) {
1416 if (IsEqual(aCurLabel, aLastFeatureInFolder))
1417 aLastFeatureInFolder.Nullify(); // the last feature in the folder is achived
1421 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(folder(aCurLabel));
1423 AttributeReferencePtr aLastFeatAttr =
1424 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1425 if (aLastFeatAttr) {
1426 // setup iterating inside a folder to find last feature
1427 ObjectPtr aLastFeature = aLastFeatAttr->value();
1429 aData = std::static_pointer_cast<Model_Data>(aLastFeature->data());
1430 if (aData && aData->isValid())
1431 aLastFeatureInFolder = aData->label().Father();
1437 // Skip all features in the composite until the last feature is not reached.
1438 // Keep the index, due to aLimitingFeature may be composite, thus aRefIndex
1439 // will be set to the last sub. But if we search a folder below the features,
1440 // we do not want to obtain index current composite feature once again.
1441 int anIndexCopy = aRefIndex;
1442 lastIndexOfComposite(this, aLimitingFeature, aRefs, aRefIndex);
1444 if (theBelow && aRefIndex < aRefs->Upper()) {
1445 // check the next object is a folder
1446 TDF_Label aLabel = aRefs->Value(aRefIndex + 1);
1447 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(folder(aLabel));
1448 aRefIndex = anIndexCopy; // restore index of parent composite
1451 if (!aLastFeatureInFolder.IsNull() || // the last feature of the folder above is not found
1455 // check the given features are sequential list
1456 int aStep = theBelow ? -1 : 1;
1457 for (aRefIndex += aStep;
1458 !aFeatures.empty() && aRefIndex >= aRefs->Lower() && aRefIndex <= aRefs->Upper();
1459 aRefIndex += aStep) {
1460 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1461 TDF_Label aFeatureLabel;
1463 aLimitingFeature = limitingFeature(aFeatures, theBelow);
1464 aData = std::static_pointer_cast<Model_Data>(aLimitingFeature->data());
1465 if (aData && aData->isValid())
1466 aFeatureLabel = aData->label().Father();
1469 // find index of parent composite feature
1470 FeaturePtr aComposite = ModelAPI_Tools::compositeOwner(feature(aCurLabel));
1472 aData = std::static_pointer_cast<Model_Data>(aComposite->data());
1473 if (aData && aData->isValid())
1474 aCurLabel = aData->label().Father();
1475 while (aRefs->Value(aRefIndex) != aCurLabel)
1479 // skip all features in the composite until the last feature is not reached
1480 lastIndexOfComposite(this, aLimitingFeature, aRefs, aRefIndex);
1483 if (!IsEqual(aCurLabel, aFeatureLabel))
1484 return FolderPtr(); // not a sequential list
1487 return aFoundFolder;
1490 bool Model_Objects::moveToFolder(
1491 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1492 const std::shared_ptr<ModelAPI_Folder>& theFolder)
1494 if (theFeatures.empty() || !theFolder)
1497 // labels for the folder and last feature in the list
1498 // (if the last feature is composite, obtain label of the last sub-feature)
1499 TDF_Label aFolderLabel, aLastFeatureLabel;
1500 std::shared_ptr<Model_Data> aData =
1501 std::static_pointer_cast<Model_Data>(theFolder->data());
1502 if (aData && aData->isValid())
1503 aFolderLabel = aData->label().Father();
1504 FeaturePtr aLastFeature = theFeatures.back();
1505 CompositeFeaturePtr aComposite =
1506 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastFeature);
1508 aLastFeature = aComposite->subFeature(aComposite->numberOfSubs() - 1);
1509 aData = std::static_pointer_cast<Model_Data>(theFeatures.back()->data());
1510 if (aData && aData->isValid())
1511 aLastFeatureLabel = aData->label().Father();
1513 if (aFolderLabel.IsNull() || aLastFeatureLabel.IsNull())
1516 AttributeReferencePtr aFirstFeatAttr =
1517 theFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1518 AttributeReferencePtr aLastFeatAttr =
1519 theFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1520 bool initFirstAttr = !aFirstFeatAttr->value().get();
1521 bool initLastAttr = !aLastFeatAttr->value().get();
1523 // check the folder is below the list of features
1524 bool isFolderBelow = false;
1525 TDF_Label aFeaturesLab = featuresLabel();
1526 Handle(TDataStd_ReferenceArray) aRefs;
1527 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1528 return false; // no reference array (something is wrong)
1529 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1530 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1531 if (aCurLabel == aFolderLabel)
1532 break; // folder is above the features
1533 else if (aCurLabel == aLastFeatureLabel) {
1534 isFolderBelow = true;
1539 if (isFolderBelow) {
1540 aData = std::static_pointer_cast<Model_Data>(theFeatures.front()->data());
1541 if (!aData || !aData->isValid())
1543 TDF_Label aPrevFeatureLabel = aData->label().Father();
1544 // label of the feature before the first feature in the list
1545 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex)
1546 if (aPrevFeatureLabel == aRefs->Value(aRefIndex)) {
1547 if (aRefIndex == aRefs->Lower())
1548 aPrevFeatureLabel.Nullify();
1550 aPrevFeatureLabel = aRefs->Value(aRefIndex - 1);
1554 // move the folder in the list of references before the first feature
1555 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1556 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1557 // update first feature of the folder
1558 initFirstAttr = true;
1560 // update last feature of the folder
1561 initLastAttr = true;
1565 aFirstFeatAttr->setValue(theFeatures.front());
1567 aLastFeatAttr->setValue(theFeatures.back());
1569 updateHistory(ModelAPI_Feature::group());
1573 static FolderPtr inFolder(const FeaturePtr& theFeature, const std::string& theFolderAttr)
1575 const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
1576 for (std::set<AttributePtr>::iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt) {
1577 if ((*anIt)->id() != theFolderAttr)
1580 ObjectPtr anOwner = (*anIt)->owner();
1581 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(anOwner);
1588 static FolderPtr isExtractionCorrect(const FolderPtr& theFirstFeatureFolder,
1589 const FolderPtr& theLastFeatureFolder,
1590 bool& isExtractBefore)
1592 if (theFirstFeatureFolder.get()) {
1593 if (theLastFeatureFolder.get())
1594 return theFirstFeatureFolder == theLastFeatureFolder ? theFirstFeatureFolder : FolderPtr();
1596 isExtractBefore = true;
1597 return theFirstFeatureFolder;
1598 } else if (theLastFeatureFolder.get()) {
1599 isExtractBefore = false;
1600 return theLastFeatureFolder;
1606 bool Model_Objects::removeFromFolder(
1607 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
1608 const bool theBefore)
1610 if (theFeatures.empty())
1613 FolderPtr aFirstFeatureFolder =
1614 inFolder(theFeatures.front(), ModelAPI_Folder::FIRST_FEATURE_ID());
1615 FolderPtr aLastFeatureFolder =
1616 inFolder(theFeatures.back(), ModelAPI_Folder::LAST_FEATURE_ID());
1618 bool isExtractBeforeFolder = theBefore;
1619 FolderPtr aFoundFolder =
1620 isExtractionCorrect(aFirstFeatureFolder, aLastFeatureFolder, isExtractBeforeFolder);
1622 return false; // list of features cannot be extracted
1624 // references of the current folder
1625 ObjectPtr aFolderStartFeature;
1626 ObjectPtr aFolderEndFeature;
1627 if (aFirstFeatureFolder != aLastFeatureFolder) {
1628 aFolderStartFeature = aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->value();
1629 aFolderEndFeature = aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->value();
1632 FeaturePtr aFeatureToFind;
1633 if (isExtractBeforeFolder) {
1634 aFeatureToFind = theFeatures.back();
1635 // if the last feature is composite, obtain its last sub-feature
1636 CompositeFeaturePtr aComposite =
1637 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeatureToFind);
1639 aFeatureToFind = aComposite->subFeature(aComposite->numberOfSubs() - 1);
1641 aFeatureToFind = theFeatures.front();
1642 std::shared_ptr<Model_Data> aData =
1643 std::static_pointer_cast<Model_Data>(aFeatureToFind->data());
1644 if (!aData || !aData->isValid())
1646 TDF_Label aLabelToFind = aData->label().Father();
1648 // search the label in the list of references
1649 TDF_Label aFeaturesLab = featuresLabel();
1650 Handle(TDataStd_ReferenceArray) aRefs;
1651 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1652 return false; // no reference array (something is wrong)
1653 int aRefIndex = aRefs->Lower();
1654 for (; aRefIndex <= aRefs->Upper(); ++aRefIndex)
1655 if (aRefs->Value(aRefIndex) == aLabelToFind)
1658 // update folder position
1659 if (isExtractBeforeFolder) {
1660 aData = std::dynamic_pointer_cast<Model_Data>(aFoundFolder->data());
1661 TDF_Label aFolderLabel = aData->label().Father();
1662 TDF_Label aPrevFeatureLabel = aRefs->Value(aRefIndex);
1663 // update start reference of the folder
1664 if (aFolderStartFeature.get())
1665 aFolderStartFeature = feature(aRefs->Value(aRefIndex + 1));
1666 // move the folder in the list of references after the last feature from the list
1667 RemoveFromRefArray(aFeaturesLab, aFolderLabel);
1668 AddToRefArray(aFeaturesLab, aFolderLabel, aPrevFeatureLabel);
1670 // update end reference of the folder
1671 if (aFolderEndFeature.get())
1672 aFolderEndFeature = feature(aRefs->Value(aRefIndex - 1));
1675 // update folder references
1676 aFoundFolder->reference(ModelAPI_Folder::FIRST_FEATURE_ID())->setValue(aFolderStartFeature);
1677 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID())->setValue(aFolderEndFeature);
1679 updateHistory(ModelAPI_Feature::group());
1683 FolderPtr Model_Objects::findContainingFolder(const FeaturePtr& theFeature, int& theIndexInFolder)
1685 // search the label in the list of references
1686 TDF_Label aFeaturesLab = featuresLabel();
1687 Handle(TDataStd_ReferenceArray) aRefs;
1688 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1689 return FolderPtr(); // no reference array (something is wrong)
1691 std::shared_ptr<Model_Data> aData =
1692 std::static_pointer_cast<Model_Data>(theFeature->data());
1693 if (!aData || !aData->isValid())
1695 TDF_Label aLabelToFind = aData->label().Father();
1697 theIndexInFolder = -1;
1698 FolderPtr aFoundFolder;
1699 TDF_Label aLastFeatureLabel;
1701 for (int aRefIndex = aRefs->Lower(); aRefIndex <= aRefs->Upper(); ++aRefIndex) {
1705 TDF_Label aCurLabel = aRefs->Value(aRefIndex);
1706 if (aCurLabel == aLabelToFind) // the feature is reached
1707 return aFoundFolder;
1709 if (!aFoundFolder) {
1710 // if the current label refers to a folder, feel all necessary data
1711 aFoundFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(folder(aCurLabel));
1713 theIndexInFolder = -1;
1715 AttributeReferencePtr aLastRef =
1716 aFoundFolder->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1717 if (aLastRef->value()) {
1718 aData = std::static_pointer_cast<Model_Data>(aLastRef->value()->data());
1719 if (aData && aData->isValid())
1720 aLastFeatureLabel = aData->label().Father();
1721 } else // folder is empty
1722 aFoundFolder = FolderPtr();
1724 } else if (aLastFeatureLabel == aCurLabel) {
1725 // folder is finished, clear all stored data
1726 theIndexInFolder = -1;
1727 aFoundFolder = FolderPtr();
1731 // folder is not found
1732 theIndexInFolder = -1;
1737 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
1738 const std::shared_ptr<ModelAPI_Result>& theResult)
1740 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1742 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1743 FeaturePtr aFeature = feature(aFeatureLab);
1744 if (!aFeature.get() && aFeatureLab.Depth() > 1) { // this may be sub-result of result
1745 aFeatureLab = aFeatureLab.Father().Father();
1746 aFeature = feature(aFeatureLab);
1750 return FeaturePtr();
1753 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
1755 if (theFeature->data()->isValid()) {
1756 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1757 if (aLabIter.More()) {
1758 TDF_Label anArgLab = aLabIter.Value();
1759 Handle(TDataStd_Comment) aGroup;
1760 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1761 return TCollection_AsciiString(aGroup->Get()).ToCString();
1765 static std::string anEmpty;
1766 return anEmpty; // not found
1769 void Model_Objects::updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
1771 if (theProcessed.find(theFeature) != theProcessed.end())
1773 theProcessed.insert(theFeature);
1774 // for composites update subs recursively (sketch elements results are needed for the sketch)
1775 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
1776 if (aComp.get() && aComp->getKind() != "Part") { // don't go inside of parts sub-features
1777 // update subs of composites first
1778 int aSubNum = aComp->numberOfSubs();
1779 for(int a = 0; a < aSubNum; a++) {
1780 FeaturePtr aSub = aComp->subFeature(a);
1781 updateResults(aComp->subFeature(a), theProcessed);
1785 // for not persistent is will be done by parametric updater automatically
1786 //if (!theFeature->isPersistentResult()) return;
1787 // check the existing results and remove them if there is nothing on the label
1788 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1789 while(aResIter != theFeature->results().cend()) {
1790 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1792 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(aBody->data());
1793 if (!aData.get() || !aData->isValid() || (!aBody->isDisabled() && aData->isDeleted())) {
1794 // found a disappeared result => remove it
1795 theFeature->eraseResultFromList(aBody);
1796 // start iterate from beginning because iterator is corrupted by removing
1797 aResIter = theFeature->results().cbegin();
1803 // it may be on undo
1804 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
1806 // check that results are presented on all labels
1807 int aResSize = int(theFeature->results().size());
1808 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1809 for(; aLabIter.More(); aLabIter.Next()) {
1810 // here must be GUID of the feature
1811 int aResIndex = aLabIter.Value().Tag() - 1;
1813 if (aResSize <= aResIndex) {
1814 TDF_Label anArgLab = aLabIter.Value();
1815 Handle(TDataStd_Comment) aGroup;
1816 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1817 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str() ||
1818 aGroup->Get() == ModelAPI_ResultCompSolid::group().c_str()) {
1819 aNewBody = createBody(theFeature->data(), aResIndex);
1820 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1821 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
1822 theFeature->setResult(aNewP, aResIndex);
1823 if (!aNewP->partDoc().get())
1824 // create the part result: it is better to restore the previous result if it is possible
1825 theFeature->execute();
1826 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1827 theFeature->execute(); // construction shapes are needed for sketch solver
1828 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1829 aNewBody = createGroup(theFeature->data(), aResIndex);
1830 } else if (aGroup->Get() == ModelAPI_ResultField::group().c_str()) {
1831 aNewBody = createField(theFeature->data(), aResIndex);
1832 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1833 theFeature->attributeChanged("expression"); // just produce a value
1835 Events_InfoMessage("Model_Objects", "Unknown type of result is found in the document:")
1836 .arg(TCollection_AsciiString(aGroup->Get()).ToCString()).send();
1839 if (aNewBody && !aNewBody->data()->isDeleted()) {
1840 theFeature->setResult(aNewBody, aResIndex);
1846 ResultPtr Model_Objects::findByName(const std::string theName)
1849 FeaturePtr aResFeature; // keep feature to return the latest one
1850 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
1851 for(; anObjIter.More(); anObjIter.Next()) {
1852 FeaturePtr& aFeature = anObjIter.ChangeValue();
1853 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
1855 std::list<ResultPtr> allResults;
1856 ModelAPI_Tools::allResults(aFeature, allResults);
1857 std::list<ResultPtr>::iterator aRIter = allResults.begin();
1858 for (; aRIter != allResults.cend(); aRIter++) {
1859 ResultPtr aRes = *aRIter;
1860 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
1861 aRes->data()->name() == theName)
1863 if (!aResult.get() || isLater(aFeature, aResFeature)) { // select the latest
1865 aResFeature = aFeature;
1873 TDF_Label Model_Objects::nextLabel(TDF_Label theCurrent, const bool theReverse)
1875 Handle(TDataStd_ReferenceArray) aRefs;
1876 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1877 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
1878 TDF_Label aCurLab = aRefs->Value(a);
1879 if (aCurLab.IsEqual(theCurrent)) {
1880 a += theReverse ? -1 : 1;
1881 if (a >= aRefs->Lower() && a <= aRefs->Upper())
1882 return aRefs->Value(a);
1883 break; // finish iiteration: it's last feature
1890 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
1892 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
1893 if (aData.get() && aData->isValid()) {
1894 TDF_Label aFeatureLabel = aData->label().Father();
1895 TDF_Label aNextLabel = nextLabel(aFeatureLabel, theReverse);
1896 if (!aNextLabel.IsNull())
1897 return feature(aNextLabel);
1899 return FeaturePtr(); // not found, last, or something is wrong
1902 FeaturePtr Model_Objects::firstFeature()
1904 Handle(TDataStd_ReferenceArray) aRefs;
1905 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1906 return feature(aRefs->Value(aRefs->Lower()));
1908 return FeaturePtr(); // no features at all
1911 FeaturePtr Model_Objects::lastFeature()
1913 Handle(TDataStd_ReferenceArray) aRefs;
1914 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1915 return feature(aRefs->Value(aRefs->Upper()));
1917 return FeaturePtr(); // no features at all
1920 bool Model_Objects::isLater(FeaturePtr theLater, FeaturePtr theCurrent) const
1922 std::shared_ptr<Model_Data> aLaterD = std::static_pointer_cast<Model_Data>(theLater->data());
1923 std::shared_ptr<Model_Data> aCurrentD = std::static_pointer_cast<Model_Data>(theCurrent->data());
1924 if (aLaterD.get() && aLaterD->isValid() && aCurrentD.get() && aCurrentD->isValid()) {
1925 TDF_Label aLaterL = aLaterD->label().Father();
1926 TDF_Label aCurrentL = aCurrentD->label().Father();
1927 int aLaterI = -1, aCurentI = -1; // not found yet state
1928 Handle(TDataStd_ReferenceArray) aRefs;
1929 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1930 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
1931 TDF_Label aCurLab = aRefs->Value(a);
1932 if (aCurLab.IsEqual(aLaterL)) {
1934 } else if (aCurLab.IsEqual(aCurrentL)) {
1937 if (aLaterI != -1 && aCurentI != -1) // both are found
1938 return aLaterI > aCurentI;
1942 return false; // not found, or something is wrong
1945 std::list<std::shared_ptr<ModelAPI_Object> > Model_Objects::allObjects()
1947 std::list<std::shared_ptr<ModelAPI_Object> > aResult;
1948 Handle(TDataStd_ReferenceArray) aRefs;
1949 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1950 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
1951 ObjectPtr anObject = object(aRefs->Value(a));
1952 if (!anObject.get()) // is it a folder?
1953 anObject = folder(aRefs->Value(a));
1955 aResult.push_back(anObject);
1961 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
1963 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
1964 Handle(TDataStd_ReferenceArray) aRefs;
1965 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1966 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
1967 FeaturePtr aFeature = feature(aRefs->Value(a));
1969 aResult.push_back(aFeature);
1975 int Model_Objects::numInternalFeatures()
1977 Handle(TDataStd_ReferenceArray) aRefs;
1978 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1979 return aRefs->Upper() - aRefs->Lower() + 1;
1981 return 0; // invalid
1984 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
1986 Handle(TDataStd_ReferenceArray) aRefs;
1987 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1988 return feature(aRefs->Value(aRefs->Lower() + theIndex));
1990 return FeaturePtr(); // invalid
1993 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1995 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1998 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
2000 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);