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 std::map<std::string, std::string> aNames;
57 std::map<std::string, std::string>::const_iterator aFound = aNames.find(theGroupID);
58 if (aFound == aNames.end()) {
59 aNames[theGroupID] = std::string("__") + theGroupID;
60 aFound = aNames.find(theGroupID);
62 return aFound->second;
68 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
71 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
72 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
75 /// 0:1:2 - where features are located
76 /// 0:1:2:N:1 - data of the feature N
77 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
79 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
83 void Model_Objects::setOwner(DocumentPtr theDoc)
86 // update all fields and recreate features and result objects if needed
87 TDF_LabelList aNoUpdated;
88 synchronizeFeatures(aNoUpdated, true, true, true, true);
92 Model_Objects::~Model_Objects()
94 // delete all features of this document
95 Events_Loop* aLoop = Events_Loop::loop();
96 // erase one by one to avoid access from the feature destructor itself from he map
97 // blocks the flush signals to avoid the temporary objects visualization in the viewer
98 // they should not be shown in order to do not lose highlight by erasing them
99 bool isActive = aLoop->activateFlushes(false);
101 while(!myFeatures.IsEmpty()) {
102 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
103 FeaturePtr aFeature = aFeaturesIter.Value();
104 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
105 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
106 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
107 aFeature->removeResults(0, false);
109 myFeatures.UnBind(aFeaturesIter.Key());
111 while (!myFolders.IsEmpty()) {
112 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFoldersIter(myFolders);
113 ObjectPtr aFolder = aFoldersIter.Value();
114 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
115 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
116 ModelAPI_EventCreator::get()->sendUpdated(aFolder, EVENT_DISP);
118 myFolders.UnBind(aFoldersIter.Key());
121 aLoop->activateFlushes(isActive);
122 // erase update, because features are destroyed and update should not performed for them anywhere
123 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
124 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
125 // deleted and redisplayed is correctly performed: they know that features are destroyed
126 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
127 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
131 /// Appends to the array of references a new referenced label
132 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
134 Handle(TDataStd_ReferenceArray) aRefs;
135 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
136 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
137 aRefs->SetValue(0, theReferenced);
138 } else { // extend array by one more element
139 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
141 int aPassedPrev = 0; // prev feature is found and passed
142 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
143 aNewArray->SetValue(aRefs->Lower(), theReferenced);
146 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
147 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
148 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
150 aNewArray->SetValue(a + 1, theReferenced);
153 if (!aPassedPrev) // not found: unknown situation
154 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
155 aRefs->SetInternalArray(aNewArray);
159 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
161 if (!theFeature->isAction()) { // do not add action to the data model
162 TDF_Label aFeaturesLab = featuresLabel();
163 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
164 // store feature in the features array: before "initData" because in macro features
165 // in initData it creates new features, appeared later than this
166 TDF_Label aPrevFeateureLab;
167 if (theAfterThis.get()) { // searching for the previous feature label
168 std::shared_ptr<Model_Data> aPrevData =
169 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
170 if (aPrevData.get()) {
171 aPrevFeateureLab = aPrevData->label().Father();
174 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
176 // keep the feature ID to restore document later correctly
177 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
178 myFeatures.Bind(aFeatureLab, theFeature);
179 // must be before the event sending: for OB the feature is already added
180 updateHistory(ModelAPI_Feature::group());
181 // do not change the order:
184 // during python script with fillet constraint feature data should be
185 // initialized before using it in GUI
187 // must be after binding to the map because of "Box" macro feature that
188 // creates other features in "initData"
189 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
190 // event: feature is added, mist be before "initData" to update OB correctly on Duplicate:
191 // first new part, then the content
192 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
193 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
194 } else { // make feature has not-null data anyway
195 theFeature->setData(Model_Data::invalidData());
196 theFeature->setDoc(myDoc);
200 /// Appends to the array of references a new referenced label.
201 /// If theIndex is not -1, removes element at this index, not theReferenced.
202 /// \returns the index of removed element
203 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
204 const int theIndex = -1)
206 int aResult = -1; // no returned
207 Handle(TDataStd_ReferenceArray) aRefs;
208 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
209 if (aRefs->Length() == 1) { // just erase an array
210 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
211 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
214 } else { // reduce the array
215 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
217 int aCount = aRefs->Lower();
218 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
219 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
223 aNewArray->SetValue(aCount, aRefs->Value(a));
226 aRefs->SetInternalArray(aNewArray);
232 void Model_Objects::refsToFeature(FeaturePtr theFeature,
233 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
235 // check the feature: it must have no depended objects on it
236 // the dependencies can be in the feature results
237 std::list<ResultPtr> aResults;
238 ModelAPI_Tools::allResults(theFeature, aResults);
239 std::list<ResultPtr>::const_iterator aResIter = aResults.cbegin();
240 for (; aResIter != aResults.cend(); aResIter++) {
241 ResultPtr aResult = (*aResIter);
242 std::shared_ptr<Model_Data> aData =
243 std::dynamic_pointer_cast<Model_Data>(aResult->data());
244 if (aData.get() != NULL) {
245 const std::set<AttributePtr>& aRefs = aData->refsToMe();
246 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
247 for (; aRefIt != aRefLast; aRefIt++) {
248 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
249 if (aFeature.get() != NULL)
250 theRefs.insert(aFeature);
254 // the dependencies can be in the feature itself
255 std::shared_ptr<Model_Data> aData =
256 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
257 if (aData.get() && !aData->refsToMe().empty()) {
258 const std::set<AttributePtr>& aRefs = aData->refsToMe();
259 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
260 for (; aRefIt != aRefLast; aRefIt++) {
261 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
262 if (aFeature.get() != NULL)
263 theRefs.insert(aFeature);
267 if (!theRefs.empty() && isSendError) {
268 Events_InfoMessage("Model_Objects",
269 "Feature '%1' is used and can not be deleted").arg(theFeature->data()->name()).send();
273 void Model_Objects::removeFeature(FeaturePtr theFeature)
275 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
276 if (aData.get() && aData->isValid()) {
277 // checking that the sub-element of composite feature is removed: if yes, inform the owner
278 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
279 refsToFeature(theFeature, aRefs, false);
280 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
281 for(; aRefIter != aRefs.end(); aRefIter++) {
282 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
283 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
284 if (aComposite.get() && aComposite->isSub(theFeature)) {
285 aComposite->removeFeature(theFeature);
288 // this must be before erase since theFeature erasing removes all information about
289 // the feature results and groups of results
290 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
291 clearHistory(theFeature);
295 TDF_Label aFeatureLabel = aData->label().Father();
296 if (myFeatures.IsBound(aFeatureLabel))
297 myFeatures.UnBind(aFeatureLabel);
299 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
300 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
301 // erase all attributes under the label of feature
302 aFeatureLabel.ForgetAllAttributes();
303 // remove it from the references array
304 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
305 // event: feature is deleted
306 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
307 updateHistory(ModelAPI_Feature::group());
311 void Model_Objects::eraseAllFeatures()
313 static Events_ID kDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
314 static const ModelAPI_EventCreator* kCreator = ModelAPI_EventCreator::get();
315 // make all features invalid (like deleted)
316 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
317 for(; aFIter.More(); aFIter.Next()) {
318 FeaturePtr aFeature = aFIter.Value();
319 std::list<ResultPtr> aResList;
320 ModelAPI_Tools::allResults(aFeature, aResList);
321 std::list<ResultPtr>::iterator aRIter = aResList.begin();
322 for(; aRIter != aResList.end(); aRIter++) {
323 ResultPtr aRes = *aRIter;
324 if (aRes && aRes->data()->isValid()) {
325 kCreator->sendDeleted(myDoc, aRes->groupName());
326 kCreator->sendUpdated(aRes, kDispEvent);
327 aRes->setData(aRes->data()->invalidPtr());
331 kCreator->sendUpdated(aFeature, kDispEvent);
332 aFeature->setData(aFeature->data()->invalidPtr());
334 kCreator->sendDeleted(myDoc, ModelAPI_Feature::group());
335 myFeatures.Clear(); // just remove features without modification of DS
336 updateHistory(ModelAPI_Feature::group());
339 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
341 TDF_Label aFeaturesLab = featuresLabel();
342 Handle(TDataStd_ReferenceArray) aRefs;
343 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
345 TDF_Label anAfterLab, aMovedLab =
346 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
347 if (theAfterThis.get())
348 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
350 Handle(TDataStd_HLabelArray1) aNewArray =
351 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
352 int aPassedMovedFrom = 0; // the prev feature location is found and passed
353 int aPassedMovedTo = 0; // the feature is added and this location is passed
354 if (!theAfterThis.get()) { // null means that inserted feature must be the first
355 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
358 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
359 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
361 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
362 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
363 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
364 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
366 } else { // just copy one
367 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
368 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
371 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
372 if (!aPassedMovedFrom) {
373 static std::string aMovedFromError("The moved feature is not found");
374 Events_InfoMessage("Model_Objects", aMovedFromError).send();
376 static std::string aMovedToError("The 'after' feature for movement is not found");
377 Events_InfoMessage("Model_Objects", aMovedToError).send();
381 // store the new array
382 aRefs->SetInternalArray(aNewArray);
383 // update the feature and the history
384 clearHistory(theMoved);
385 // make sure all (selection) attributes of moved feature will be updated
386 static Events_ID kUpdateSelection = Events_Loop::loop()->eventByName(EVENT_UPDATE_SELECTION);
387 ModelAPI_EventCreator::get()->sendUpdated(theMoved, kUpdateSelection, false);
388 ModelAPI_EventCreator::get()->sendReordered(theMoved);
391 void Model_Objects::clearHistory(ObjectPtr theObj)
394 const std::string aGroup = theObj->groupName();
395 updateHistory(aGroup);
397 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
398 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
399 std::string aResultGroup = featureResultGroup(aFeature);
400 if (!aResultGroup.empty()) {
401 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
402 myHistory.find(aResultGroup);
403 if (aHIter != myHistory.end())
404 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
410 void Model_Objects::createHistory(const std::string& theGroupID)
412 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
413 if (aHIter == myHistory.end()) {
414 std::vector<ObjectPtr> aResult;
415 std::vector<ObjectPtr> aResultOutOfFolder;
416 FeaturePtr aLastFeatureInFolder;
417 // iterate the array of references and get feature by feature from the array
418 bool isFeature = theGroupID == ModelAPI_Feature::group();
419 Handle(TDataStd_ReferenceArray) aRefs;
420 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
421 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
422 FeaturePtr aFeature = feature(aRefs->Value(a));
423 if (aFeature.get()) {
424 // if feature is in sub-component, remove it from history:
425 // it is in sub-tree of sub-component
426 bool isSub = ModelAPI_Tools::compositeOwner(aFeature).get() != NULL;
427 if (isFeature) { // here may be also disabled features
428 if (!isSub && aFeature->isInHistory()) {
429 aResult.push_back(aFeature);
430 // the feature is out of the folders
431 if (aLastFeatureInFolder.get() == NULL)
432 aResultOutOfFolder.push_back(aFeature);
434 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
435 // construction results of sub-features should not be in the tree
436 if (!isSub || theGroupID != ModelAPI_ResultConstruction::group()) {
437 // do not use reference to the list here since results can be changed by "isConcealed"
438 const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
439 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator
440 aRIter = aResults.begin();
441 for (; aRIter != aResults.cend(); aRIter++) {
442 ResultPtr aRes = *aRIter;
443 if (aRes->groupName() != theGroupID) break; // feature have only same group results
444 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
445 aResult.push_back(*aRIter);
451 // the feature closes the folder, so the next features will be treated as out-of-folder
452 if (aLastFeatureInFolder.get() && aLastFeatureInFolder == aFeature)
453 aLastFeatureInFolder = FeaturePtr();
456 // it may be a folder
457 ObjectPtr aFolder = folder(aRefs->Value(a));
459 aResultOutOfFolder.push_back(aFolder);
461 // get the last feature in the folder
462 AttributeReferencePtr aLastFeatAttr =
463 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
465 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
470 // to be sure that isConcealed did not update the history (issue 1089) during the iteration
471 if (myHistory.find(theGroupID) == myHistory.end()) {
472 myHistory[theGroupID] = aResult;
474 // store the features placed out of any folder
475 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroupID, true);
476 myHistory[anOutOfFolderGroupID] = aResultOutOfFolder;
481 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
483 clearHistory(theObject);
486 void Model_Objects::updateHistory(const std::string theGroup)
488 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
489 if (aHIter != myHistory.end()) {
490 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
492 // erase history for the group of objects placed out of any folder
493 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroup, true);
494 myHistory.erase(anOutOfFolderGroupID);
498 ObjectPtr Model_Objects::folder(TDF_Label theLabel) const
500 if (myFolders.IsBound(theLabel))
501 return myFolders.Find(theLabel);
505 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
507 if (myFeatures.IsBound(theLabel))
508 return myFeatures.Find(theLabel);
509 return FeaturePtr(); // not found
512 ObjectPtr Model_Objects::object(TDF_Label theLabel)
514 // try feature by label
515 FeaturePtr aFeature = feature(theLabel);
517 return feature(theLabel);
518 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
519 aFeature = feature(aFeatureLabel);
520 bool isSubResult = false;
521 if (!aFeature.get() && aFeatureLabel.Depth() > 1) { // let's suppose this is sub-result of result
522 aFeatureLabel = aFeatureLabel.Father().Father();
523 aFeature = feature(aFeatureLabel);
526 if (aFeature.get()) {
527 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
528 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
529 for (; aRIter != aResults.cend(); aRIter++) {
531 ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
532 if (aCompRes.get()) {
533 int aNumSubs = aCompRes->numberOfSubs();
534 for(int a = 0; a < aNumSubs; a++) {
535 ResultPtr aSub = aCompRes->subResult(a);
537 std::shared_ptr<Model_Data> aSubData = std::dynamic_pointer_cast<Model_Data>(
539 if (aSubData->label().Father().IsEqual(theLabel))
545 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
547 if (aResData->label().Father().IsEqual(theLabel))
552 return FeaturePtr(); // not found
555 ObjectPtr Model_Objects::object(const std::string& theGroupID,
557 const bool theAllowFolder)
561 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
562 createHistory(aGroupID);
563 return myHistory[aGroupID][theIndex];
566 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
567 const std::string& theGroupID, const std::string& theName)
569 createHistory(theGroupID);
570 if (theGroupID == ModelAPI_Feature::group()) { // searching among features (in history or not)
571 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
572 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator anObjIter = allObjs.begin();
573 for(; anObjIter != allObjs.end(); anObjIter++) {
574 if ((*anObjIter)->data()->name() == theName)
577 } else { // searching among results (concealed or not)
578 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
579 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator anObjIter = allObjs.begin();
580 for(; anObjIter != allObjs.end(); anObjIter++) {
581 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = (*anObjIter)->results();
582 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
583 for (; aRIter != aResults.cend(); aRIter++) {
584 if (aRIter->get() && (*aRIter)->groupName() == theGroupID) {
585 if ((*aRIter)->data()->name() == theName)
587 ResultCompSolidPtr aCompRes =
588 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
589 if (aCompRes.get()) {
590 int aNumSubs = aCompRes->numberOfSubs();
591 for(int a = 0; a < aNumSubs; a++) {
592 ResultPtr aSub = aCompRes->subResult(a);
593 if (aSub.get() && aSub->groupName() == theGroupID) {
594 if (aSub->data()->name() == theName)
607 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject)
609 std::string aGroup = theObject->groupName();
610 createHistory(aGroup);
611 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
612 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
613 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
614 if ((*anObjIter) == theObject)
621 int Model_Objects::size(const std::string& theGroupID, const bool theAllowFolder)
623 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
624 createHistory(aGroupID);
625 return int(myHistory[aGroupID].size());
628 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
630 // iterate the array of references and get feature by feature from the array
631 Handle(TDataStd_ReferenceArray) aRefs;
632 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
633 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
634 FeaturePtr aFeature = feature(aRefs->Value(a));
635 if (aFeature.get()) {
636 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
637 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
638 for (; aRIter != aResults.cend(); aRIter++) {
639 ResultPtr aRes = *aRIter;
640 if (aRes->groupName() != theGroupID) break; // feature have only same group results
641 // iterate also concealed: ALL RESULTS (for translation parts undo/redo management)
642 //if (aRes->isInHistory() && !aRes->isConcealed()) {
643 theResults.push_back(*aRIter);
652 TDF_Label Model_Objects::featuresLabel() const
654 return myMain.FindChild(TAG_OBJECTS);
657 static std::string composeName(const std::string& theFeatureKind, const int theIndex)
659 std::stringstream aNameStream;
660 aNameStream << theFeatureKind << "_" << theIndex;
661 return aNameStream.str();
664 void Model_Objects::setUniqueName(FeaturePtr theFeature)
666 if (!theFeature->data()->name().empty())
667 return; // not needed, name is already defined
668 std::string aName; // result
669 // first count all features of such kind to start with index = count + 1
670 int aNumObjects = -1; // this feature is already in this map
671 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
672 for (; aFIter.More(); aFIter.Next()) {
673 if (aFIter.Value()->getKind() == theFeature->getKind())
676 // generate candidate name
677 aName = composeName(theFeature->getKind(), aNumObjects + 1);
678 // check this is unique, if not, increase index by 1
679 for (aFIter.Initialize(myFeatures); aFIter.More();) {
680 FeaturePtr aFeature = aFIter.Value();
681 bool isSameName = aFeature->data()->name() == aName;
682 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
683 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
684 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
685 for (; aRIter != aResults.cend(); aRIter++) {
686 isSameName = (*aRIter)->data()->name() == aName;
692 aName = composeName(theFeature->getKind(), aNumObjects + 1);
693 // reinitialize iterator to make sure a new name is unique
694 aFIter.Initialize(myFeatures);
698 theFeature->data()->setName(aName);
701 void Model_Objects::setUniqueName(FolderPtr theFolder)
703 if (!theFolder->name().empty())
704 return; // name is already defined
706 int aNbFolders = myFolders.Size();
707 std::string aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
709 // check the uniqueness of the name
710 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator anIt(myFolders);
711 while (anIt.More()) {
712 if (anIt.Value()->data()->name() == aName) {
713 aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
714 // reinitialize iterator to make sure a new name is unique
715 anIt.Initialize(myFolders);
720 theFolder->data()->setName(aName);
723 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
725 std::shared_ptr<Model_Data> aData(new Model_Data);
726 aData->setLabel(theLab.FindChild(theTag));
727 aData->setObject(theObj);
728 theObj->setDoc(myDoc);
729 theObj->setData(aData);
730 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
731 if (aFeature.get()) {
732 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
733 } else { // is it a folder?
734 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theObj);
736 setUniqueName(aFolder);
738 theObj->initAttributes();
741 std::shared_ptr<ModelAPI_Feature> Model_Objects::featureById(const int theId)
744 TDF_Label aLab = featuresLabel().FindChild(theId, Standard_False);
745 return feature(aLab);
747 return std::shared_ptr<ModelAPI_Feature>(); // not found
750 void Model_Objects::synchronizeFeatures(
751 const TDF_LabelList& theUpdated, const bool theUpdateReferences,
752 const bool theExecuteFeatures, const bool theOpen, const bool theFlush)
754 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
755 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
757 // after all updates, sends a message that groups of features were created or updated
758 Events_Loop* aLoop = Events_Loop::loop();
759 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
760 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
761 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
762 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
763 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
764 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
765 bool isActive = aLoop->activateFlushes(false);
767 // collect all updated labels map
768 TDF_LabelMap anUpdatedMap;
769 TDF_ListIteratorOfLabelList anUpdatedIter(theUpdated);
770 for(; anUpdatedIter.More(); anUpdatedIter.Next()) {
771 TDF_Label& aFeatureLab = anUpdatedIter.Value();
772 while(aFeatureLab.Depth() > 3)
773 aFeatureLab = aFeatureLab.Father();
774 if (myFeatures.IsBound(aFeatureLab))
775 anUpdatedMap.Add(aFeatureLab);
778 // update all objects by checking are they on labels or not
779 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
780 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
781 for (; aLabIter.More(); aLabIter.Next()) {
782 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
784 if (!myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
786 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
787 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
788 .ToCString(), anOwner);
789 if (!aFeature.get()) {
790 // somethig is wrong, most probably, the opened document has invalid structure
791 Events_InfoMessage("Model_Objects", "Invalid type of object in the document").send();
792 aLabIter.Value()->Label().ForgetAllAttributes();
796 // this must be before "setData" to redo the sketch line correctly
797 myFeatures.Bind(aFeatureLabel, aFeature);
798 aNewFeatures.insert(aFeature);
799 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
800 updateHistory(aFeature);
802 // event: model is updated
803 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
804 } else { // nothing is changed, both iterators are incremented
805 aFeature = myFeatures.Find(aFeatureLabel);
806 aKeptFeatures.insert(aFeature);
807 if (anUpdatedMap.Contains(aFeatureLabel)) {
808 if (!theOpen) { // on abort/undo/redo reinitialize attributes if something is changed
809 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
810 aFeature->data()->attributes("");
811 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
812 for(; anAttr != anAttrs.end(); anAttr++)
815 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
816 if (aFeature->getKind() == "Parameter") {
817 // if parameters are changed, update the results (issue 937)
818 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
819 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
820 for (; aRIter != aResults.cend(); aRIter++) {
821 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
822 if (aRes->data()->isValid() && !aRes->isDisabled()) {
823 ModelAPI_EventCreator::get()->sendUpdated(aRes, anUpdateEvent);
831 // check all features are checked: if not => it was removed
832 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
833 while (aFIter.More()) {
834 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
835 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
836 FeaturePtr aFeature = aFIter.Value();
837 // event: model is updated
838 //if (aFeature->isInHistory()) {
839 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
841 // results of this feature must be redisplayed (hided)
842 // redisplay also removed feature (used for sketch and AISObject)
843 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
844 updateHistory(aFeature);
847 // unbind after the "erase" call: on abort sketch
848 // is removes sub-objects that corrupts aFIter
849 myFeatures.UnBind(aFIter.Key());
850 // reinitialize iterator because unbind may corrupt the previous order in the map
851 aFIter.Initialize(myFeatures);
856 if (theUpdateReferences) {
857 synchronizeBackRefs();
859 // update results of the features (after features created because
860 // they may be connected, like sketch and sub elements)
861 // After synchronisation of back references because sketch
862 // must be set in sub-elements before "execute" by updateResults
863 std::set<FeaturePtr> aProcessed; // composites must be updated after their subs (issue 360)
864 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
865 for (; aLabIter2.More(); aLabIter2.Next()) {
866 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
867 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
868 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
869 updateResults(aFeature, aProcessed);
872 // the synchronize should be done after updateResults
873 // in order to correct back references of updated results
874 if (theUpdateReferences) {
875 synchronizeBackRefs();
877 if (!theUpdated.IsEmpty()) {
878 // this means there is no control what was modified => remove history cash
882 if (theExecuteFeatures)
883 anOwner->executeFeatures() = false;
884 aLoop->activateFlushes(isActive);
887 aLoop->flush(aDeleteEvent);
888 // delete should be emitted before create to reacts to aborted feature
889 aLoop->flush(aCreateEvent);
890 aLoop->flush(anUpdateEvent);
891 aLoop->flush(aCreateEvent); // after update of features, there could be results created
892 aLoop->flush(aDeleteEvent); // or deleted
893 aLoop->flush(aRedispEvent);
894 aLoop->flush(aToHideEvent);
896 if (theExecuteFeatures)
897 anOwner->executeFeatures() = true;
900 /// synchronises back references for the given object basing on the collected data
901 void Model_Objects::synchronizeBackRefsForObject(const std::set<AttributePtr>& theNewRefs,
904 if (!theObject.get() || !theObject->data()->isValid())
906 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
907 // iterate new list to compare with curent
908 std::set<AttributePtr>::iterator aNewIter = theNewRefs.begin();
909 for(; aNewIter != theNewRefs.end(); aNewIter++) {
910 if (aData->refsToMe().find(*aNewIter) == aData->refsToMe().end()) {
911 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aNewIter)->owner());
912 aData->addBackReference(aRefFeat, (*aNewIter)->id());
915 if (theNewRefs.size() != aData->refsToMe().size()) { // some back ref must be removed
916 std::set<AttributePtr>::iterator aCurrentIter = aData->refsToMe().begin();
917 while(aCurrentIter != aData->refsToMe().end()) {
918 if (theNewRefs.find(*aCurrentIter) == theNewRefs.end()) {
919 // for external references from other documents this system
920 // is not working: refs are collected from
921 // different Model_Objects, so before remove check this
922 // external object exists and still referenced
923 bool aLeaveIt = false;
924 if ((*aCurrentIter)->owner().get() && (*aCurrentIter)->owner()->document() != myDoc &&
925 (*aCurrentIter)->owner()->data().get() && (*aCurrentIter)->owner()->data()->isValid()) {
926 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
927 (*aCurrentIter)->owner()->data()->referencesToObjects(aRefs);
928 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> >>>::iterator
929 aRefIter = aRefs.begin();
930 for(; aRefIter != aRefs.end(); aRefIter++) {
931 if ((*aCurrentIter)->id() == aRefIter->first) {
932 std::list<std::shared_ptr<ModelAPI_Object> >::iterator anOIt;
933 for(anOIt = aRefIter->second.begin(); anOIt != aRefIter->second.end(); anOIt++) {
934 if (*anOIt == theObject) {
942 aData->removeBackReference(*aCurrentIter);
943 aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
944 } else aCurrentIter++;
945 } else aCurrentIter++;
948 aData->updateConcealmentFlag();
951 void Model_Objects::synchronizeBackRefs()
953 // collect all back references in the separated container: to update everything at once,
954 // without additional Concealment switchin on and off: only the final modification
956 // referenced (slave) objects to referencing attirbutes
957 std::map<ObjectPtr, std::set<AttributePtr> > allRefs;
958 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
959 for(; aFeatures.More(); aFeatures.Next()) {
960 FeaturePtr aFeature = aFeatures.Value();
961 std::shared_ptr<Model_Data> aFData = std::dynamic_pointer_cast<Model_Data>(aFeature->data());
963 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
964 aFData->referencesToObjects(aRefs);
965 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIt = aRefs.begin();
966 for(; aRefsIt != aRefs.end(); aRefsIt++) {
967 std::list<ObjectPtr>::iterator aRefTo = aRefsIt->second.begin();
968 for(; aRefTo != aRefsIt->second.end(); aRefTo++) {
970 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(*aRefTo);
971 if (aFound == allRefs.end()) {
972 allRefs[*aRefTo] = std::set<AttributePtr>();
973 aFound = allRefs.find(*aRefTo);
975 aFound->second.insert(aFeature->data()->attribute(aRefsIt->first));
981 // second iteration: just compare back-references with existing in features and results
982 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
983 FeaturePtr aFeature = aFeatures.Value();
984 static std::set<AttributePtr> anEmpty;
985 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(aFeature);
986 if (aFound == allRefs.end()) { // not found => erase all back references
987 synchronizeBackRefsForObject(anEmpty, aFeature);
989 synchronizeBackRefsForObject(aFound->second, aFeature);
990 allRefs.erase(aFound); // to check that all refs are counted
993 std::list<ResultPtr> aResults;
994 ModelAPI_Tools::allResults(aFeature, aResults);
995 std::list<ResultPtr>::iterator aRIter = aResults.begin();
996 for(; aRIter != aResults.cend(); aRIter++) {
997 aFound = allRefs.find(*aRIter);
998 if (aFound == allRefs.end()) { // not found => erase all back references
999 synchronizeBackRefsForObject(anEmpty, *aRIter);
1001 synchronizeBackRefsForObject(aFound->second, *aRIter);
1002 allRefs.erase(aFound); // to check that all refs are counted
1006 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1007 FeaturePtr aFeature = aFeatures.Value();
1008 std::list<ResultPtr> aResults;
1009 ModelAPI_Tools::allResults(aFeature, aResults);
1010 // update the concealment status for disply in isConcealed of ResultBody
1011 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1012 for(; aRIter != aResults.cend(); aRIter++) {
1013 (*aRIter)->isConcealed();
1016 // the rest all refs means that feature references to the external document feature:
1017 // process also them
1018 std::map<ObjectPtr, std::set<AttributePtr> >::iterator anExtIter = allRefs.begin();
1019 for(; anExtIter != allRefs.end(); anExtIter++) {
1020 synchronizeBackRefsForObject(anExtIter->second, anExtIter->first);
1024 TDF_Label Model_Objects::resultLabel(
1025 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1027 const std::shared_ptr<Model_Data>& aData =
1028 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1029 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1032 bool Model_Objects::hasCustomName(DataPtr theFeatureData,
1033 ResultPtr theResult,
1035 std::string& theParentName) const
1037 ResultCompSolidPtr aCompSolidRes =
1038 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theFeatureData->owner());
1039 if (aCompSolidRes) {
1040 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult->data()->owner());
1042 // names of sub-solids in CompSolid should be default (for example,
1043 // result of boolean operation 'Boolean_1' is a CompSolid which is renamed to 'MyBOOL',
1044 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1', 'Boolean_1_2' etc.)
1045 std::ostringstream aDefaultName;
1046 aDefaultName << anOwner->name();
1047 // compute default name of CompSolid (name of feature + index of CompSolid's result)
1048 int aCompSolidResultIndex = 0;
1049 const std::list<ResultPtr>& aResults = anOwner->results();
1050 for (std::list<ResultPtr>::const_iterator anIt = aResults.begin();
1051 anIt != aResults.end(); ++anIt, ++aCompSolidResultIndex)
1052 if (aCompSolidRes == *anIt)
1054 aDefaultName << "_" << (aCompSolidResultIndex + 1);
1055 theParentName = aDefaultName.str();
1059 theParentName = ModelAPI_Tools::getDefaultName(theResult, theResultIndex);
1063 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1064 std::shared_ptr<ModelAPI_Result> theResult,
1065 const int theResultIndex)
1068 theResult->setDoc(myDoc);
1069 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1070 if (theResult->data()->name().empty()) {
1071 // if was not initialized, generate event and set a name
1072 std::string aNewName = theFeatureData->name();
1073 if (!hasCustomName(theFeatureData, theResult, theResultIndex, aNewName)) {
1074 std::stringstream aName;
1076 // if there are several results (issue #899: any number of result),
1077 // add unique prefix starting from second
1078 if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
1079 aName << "_" << theResultIndex + 1;
1080 aNewName = aName.str();
1082 theResult->data()->setName(aNewName);
1086 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
1087 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1089 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1090 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1091 ObjectPtr anOldObject = object(aLab);
1092 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1093 if (anOldObject.get()) {
1094 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1096 if (!aResult.get()) {
1097 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1098 storeResult(theFeatureData, aResult, theIndex);
1103 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
1104 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1106 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1107 // for feature create compsolid, but for result sub create body:
1108 // only one level of recursion is supported now
1109 ResultPtr aResultOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theFeatureData->owner());
1110 ObjectPtr anOldObject;
1111 if (aResultOwner.get()) {
1112 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1113 } else { // in compsolid (higher level result) old object probably may be found
1114 TDataStd_Comment::Set(aLab, ModelAPI_ResultCompSolid::group().c_str());
1115 anOldObject = object(aLab);
1117 std::shared_ptr<ModelAPI_ResultBody> aResult;
1118 if (anOldObject.get()) {
1119 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1121 if (!aResult.get()) {
1122 // create compsolid anyway; if it is compsolid, it will create sub-bodies internally
1123 if (aResultOwner.get()) {
1124 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1126 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultCompSolid);
1128 storeResult(theFeatureData, aResult, theIndex);
1133 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
1134 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1136 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1137 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1138 ObjectPtr anOldObject = object(aLab);
1139 std::shared_ptr<ModelAPI_ResultPart> aResult;
1140 if (anOldObject.get()) {
1141 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1143 if (!aResult.get()) {
1144 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1145 storeResult(theFeatureData, aResult, theIndex);
1150 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
1151 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
1152 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1154 std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
1155 aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
1159 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
1160 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1162 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1163 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1164 ObjectPtr anOldObject = object(aLab);
1165 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1166 if (anOldObject.get()) {
1167 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1169 if (!aResult.get()) {
1170 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1171 storeResult(theFeatureData, aResult, theIndex);
1176 std::shared_ptr<ModelAPI_ResultField> Model_Objects::createField(
1177 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1179 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1180 TDataStd_Comment::Set(aLab, ModelAPI_ResultField::group().c_str());
1181 ObjectPtr anOldObject = object(aLab);
1182 std::shared_ptr<ModelAPI_ResultField> aResult;
1183 if (anOldObject.get()) {
1184 aResult = std::dynamic_pointer_cast<ModelAPI_ResultField>(anOldObject);
1186 if (!aResult.get()) {
1187 aResult = std::shared_ptr<ModelAPI_ResultField>(new Model_ResultField(theFeatureData));
1188 storeResult(theFeatureData, aResult, theIndex);
1193 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
1194 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1196 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1197 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1198 ObjectPtr anOldObject = object(aLab);
1199 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1200 if (anOldObject.get()) {
1201 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1203 if (!aResult.get()) {
1204 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1205 storeResult(theFeatureData, aResult, theIndex);
1210 std::shared_ptr<ModelAPI_Folder> Model_Objects::createFolder(
1211 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis)
1213 FolderPtr aFolder(new ModelAPI_Folder);
1217 TDF_Label aFeaturesLab = featuresLabel();
1218 TDF_Label aFolderLab = aFeaturesLab.NewChild();
1219 // store feature in the features array: before "initData" because in macro features
1220 // in initData it creates new features, appeared later than this
1221 TDF_Label aPrevFeatureLab;
1222 if (theBeforeThis.get()) { // searching for the previous feature label
1223 std::shared_ptr<Model_Data> aPrevData =
1224 std::dynamic_pointer_cast<Model_Data>(theBeforeThis->data());
1225 if (aPrevData.get()) {
1226 aPrevFeatureLab = nextLabel(aPrevData->label().Father(), true);
1228 } else { // find the label of the last feature
1229 Handle(TDataStd_ReferenceArray) aRefs;
1230 if (aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1231 aPrevFeatureLab = aRefs->Value(aRefs->Upper());
1233 AddToRefArray(aFeaturesLab, aFolderLab, aPrevFeatureLab);
1235 // keep the feature ID to restore document later correctly
1236 TDataStd_Comment::Set(aFolderLab, ModelAPI_Folder::ID().c_str());
1237 myFolders.Bind(aFolderLab, aFolder);
1238 // must be before the event sending: for OB the feature is already added
1239 updateHistory(ModelAPI_Folder::group());
1241 // must be after binding to the map because of "Box" macro feature that
1242 // creates other features in "initData"
1243 initData(aFolder, aFolderLab, TAG_FEATURE_ARGUMENTS);
1244 // event: folder is added, must be before "initData" to update OB correctly on Duplicate:
1245 // first new part, then the content
1246 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1247 ModelAPI_EventCreator::get()->sendUpdated(aFolder, anEvent);
1252 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
1253 const std::shared_ptr<ModelAPI_Result>& theResult)
1255 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1257 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1258 FeaturePtr aFeature = feature(aFeatureLab);
1259 if (!aFeature.get() && aFeatureLab.Depth() > 1) { // this may be sub-result of result
1260 aFeatureLab = aFeatureLab.Father().Father();
1261 aFeature = feature(aFeatureLab);
1265 return FeaturePtr();
1268 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
1270 if (theFeature->data()->isValid()) {
1271 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1272 if (aLabIter.More()) {
1273 TDF_Label anArgLab = aLabIter.Value();
1274 Handle(TDataStd_Comment) aGroup;
1275 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1276 return TCollection_AsciiString(aGroup->Get()).ToCString();
1280 static std::string anEmpty;
1281 return anEmpty; // not found
1284 void Model_Objects::updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
1286 if (theProcessed.find(theFeature) != theProcessed.end())
1288 theProcessed.insert(theFeature);
1289 // for composites update subs recursively (sketch elements results are needed for the sketch)
1290 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
1291 if (aComp.get() && aComp->getKind() != "Part") { // don't go inside of parts sub-features
1292 // update subs of composites first
1293 int aSubNum = aComp->numberOfSubs();
1294 for(int a = 0; a < aSubNum; a++) {
1295 FeaturePtr aSub = aComp->subFeature(a);
1296 updateResults(aComp->subFeature(a), theProcessed);
1300 // for not persistent is will be done by parametric updater automatically
1301 //if (!theFeature->isPersistentResult()) return;
1302 // check the existing results and remove them if there is nothing on the label
1303 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1304 while(aResIter != theFeature->results().cend()) {
1305 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1307 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(aBody->data());
1308 if (!aData.get() || !aData->isValid() || (!aBody->isDisabled() && aData->isDeleted())) {
1309 // found a disappeared result => remove it
1310 theFeature->eraseResultFromList(aBody);
1311 // start iterate from beginning because iterator is corrupted by removing
1312 aResIter = theFeature->results().cbegin();
1318 // it may be on undo
1319 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
1321 // check that results are presented on all labels
1322 int aResSize = int(theFeature->results().size());
1323 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1324 for(; aLabIter.More(); aLabIter.Next()) {
1325 // here must be GUID of the feature
1326 int aResIndex = aLabIter.Value().Tag() - 1;
1328 if (aResSize <= aResIndex) {
1329 TDF_Label anArgLab = aLabIter.Value();
1330 Handle(TDataStd_Comment) aGroup;
1331 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1332 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str() ||
1333 aGroup->Get() == ModelAPI_ResultCompSolid::group().c_str()) {
1334 aNewBody = createBody(theFeature->data(), aResIndex);
1335 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1336 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
1337 theFeature->setResult(aNewP, aResIndex);
1338 if (!aNewP->partDoc().get())
1339 // create the part result: it is better to restore the previous result if it is possible
1340 theFeature->execute();
1341 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1342 theFeature->execute(); // construction shapes are needed for sketch solver
1343 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1344 aNewBody = createGroup(theFeature->data(), aResIndex);
1345 } else if (aGroup->Get() == ModelAPI_ResultField::group().c_str()) {
1346 aNewBody = createField(theFeature->data(), aResIndex);
1347 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1348 theFeature->attributeChanged("expression"); // just produce a value
1350 Events_InfoMessage("Model_Objects", "Unknown type of result is found in the document:")
1351 .arg(TCollection_AsciiString(aGroup->Get()).ToCString()).send();
1354 if (aNewBody && !aNewBody->data()->isDeleted()) {
1355 theFeature->setResult(aNewBody, aResIndex);
1361 ResultPtr Model_Objects::findByName(const std::string theName)
1364 FeaturePtr aResFeature; // keep feature to return the latest one
1365 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
1366 for(; anObjIter.More(); anObjIter.Next()) {
1367 FeaturePtr& aFeature = anObjIter.ChangeValue();
1368 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
1370 std::list<ResultPtr> allResults;
1371 ModelAPI_Tools::allResults(aFeature, allResults);
1372 std::list<ResultPtr>::iterator aRIter = allResults.begin();
1373 for (; aRIter != allResults.cend(); aRIter++) {
1374 ResultPtr aRes = *aRIter;
1375 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
1376 aRes->data()->name() == theName)
1378 if (!aResult.get() || isLater(aFeature, aResFeature)) { // select the latest
1380 aResFeature = aFeature;
1388 TDF_Label Model_Objects::nextLabel(TDF_Label theCurrent, const bool theReverse)
1390 Handle(TDataStd_ReferenceArray) aRefs;
1391 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1392 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
1393 TDF_Label aCurLab = aRefs->Value(a);
1394 if (aCurLab.IsEqual(theCurrent)) {
1395 a += theReverse ? -1 : 1;
1396 if (a >= aRefs->Lower() && a <= aRefs->Upper())
1397 return aRefs->Value(a);
1398 break; // finish iiteration: it's last feature
1405 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
1407 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
1408 if (aData.get() && aData->isValid()) {
1409 TDF_Label aFeatureLabel = aData->label().Father();
1410 TDF_Label aNextLabel = nextLabel(aFeatureLabel, theReverse);
1411 if (!aNextLabel.IsNull())
1412 return feature(aNextLabel);
1414 return FeaturePtr(); // not found, last, or something is wrong
1417 FeaturePtr Model_Objects::firstFeature()
1419 Handle(TDataStd_ReferenceArray) aRefs;
1420 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1421 return feature(aRefs->Value(aRefs->Lower()));
1423 return FeaturePtr(); // no features at all
1426 FeaturePtr Model_Objects::lastFeature()
1428 Handle(TDataStd_ReferenceArray) aRefs;
1429 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1430 return feature(aRefs->Value(aRefs->Upper()));
1432 return FeaturePtr(); // no features at all
1435 bool Model_Objects::isLater(FeaturePtr theLater, FeaturePtr theCurrent) const
1437 std::shared_ptr<Model_Data> aLaterD = std::static_pointer_cast<Model_Data>(theLater->data());
1438 std::shared_ptr<Model_Data> aCurrentD = std::static_pointer_cast<Model_Data>(theCurrent->data());
1439 if (aLaterD.get() && aLaterD->isValid() && aCurrentD.get() && aCurrentD->isValid()) {
1440 TDF_Label aLaterL = aLaterD->label().Father();
1441 TDF_Label aCurrentL = aCurrentD->label().Father();
1442 int aLaterI = -1, aCurentI = -1; // not found yet state
1443 Handle(TDataStd_ReferenceArray) aRefs;
1444 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1445 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
1446 TDF_Label aCurLab = aRefs->Value(a);
1447 if (aCurLab.IsEqual(aLaterL)) {
1449 } else if (aCurLab.IsEqual(aCurrentL)) {
1452 if (aLaterI != -1 && aCurentI != -1) // both are found
1453 return aLaterI > aCurentI;
1457 return false; // not found, or something is wrong
1460 std::list<std::shared_ptr<ModelAPI_Object> > Model_Objects::allObjects()
1462 std::list<std::shared_ptr<ModelAPI_Object> > aResult;
1463 Handle(TDataStd_ReferenceArray) aRefs;
1464 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1465 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
1466 ObjectPtr anObject = object(aRefs->Value(a));
1467 if (!anObject.get()) // is it a folder?
1468 anObject = folder(aRefs->Value(a));
1470 aResult.push_back(anObject);
1476 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
1478 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
1479 Handle(TDataStd_ReferenceArray) aRefs;
1480 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1481 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
1482 FeaturePtr aFeature = feature(aRefs->Value(a));
1484 aResult.push_back(aFeature);
1490 int Model_Objects::numInternalFeatures()
1492 Handle(TDataStd_ReferenceArray) aRefs;
1493 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1494 return aRefs->Upper() - aRefs->Lower() + 1;
1496 return 0; // invalid
1499 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
1501 Handle(TDataStd_ReferenceArray) aRefs;
1502 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1503 return feature(aRefs->Value(aRefs->Lower() + theIndex));
1505 return FeaturePtr(); // invalid
1508 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1510 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1513 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1515 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);