1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Objects.cxx
4 // Created: 15 May 2015
5 // Author: Mikhail PONIKAROV
7 #include <Model_Objects.h>
8 #include <Model_Data.h>
9 #include <Model_Document.h>
10 #include <Model_Events.h>
11 #include <Model_Session.h>
12 #include <Model_ResultPart.h>
13 #include <Model_ResultConstruction.h>
14 #include <Model_ResultBody.h>
15 #include <Model_ResultGroup.h>
16 #include <Model_ResultParameter.h>
17 #include <ModelAPI_Validator.h>
18 #include <ModelAPI_CompositeFeature.h>
19 #include <ModelAPI_Tools.h>
21 #include <Events_Loop.h>
22 #include <Events_Error.h>
24 #include <TDataStd_Integer.hxx>
25 #include <TDataStd_Comment.hxx>
26 #include <TDF_ChildIDIterator.hxx>
27 #include <TDataStd_ReferenceArray.hxx>
28 #include <TDataStd_HLabelArray1.hxx>
29 #include <TDataStd_Name.hxx>
30 #include <TDF_Reference.hxx>
31 #include <TDF_ChildIDIterator.hxx>
32 #include <TDF_LabelMapHasher.hxx>
34 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
37 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
38 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
41 /// 0:1:2 - where features are located
42 /// 0:1:2:N:1 - data of the feature N
43 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
45 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
49 void Model_Objects::setOwner(DocumentPtr theDoc)
52 // update all fields and recreate features and result objects if needed
53 synchronizeFeatures(false, true, true);
57 Model_Objects::~Model_Objects()
59 // delete all features of this document
60 Events_Loop* aLoop = Events_Loop::loop();
61 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
62 for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
63 FeaturePtr aFeature = aFeaturesIter.Value();
64 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
65 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
66 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
67 aFeature->eraseResults();
71 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
72 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
76 /// Appends to the array of references a new referenced label
77 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
79 Handle(TDataStd_ReferenceArray) aRefs;
80 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
81 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
82 aRefs->SetValue(0, theReferenced);
83 } else { // extend array by one more element
84 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
86 int aPassedPrev = 0; // prev feature is found and passed
87 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
88 aNewArray->SetValue(aRefs->Lower(), theReferenced);
91 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
92 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
93 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
95 aNewArray->SetValue(a + 1, theReferenced);
98 if (!aPassedPrev) // not found: unknown situation
99 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
100 aRefs->SetInternalArray(aNewArray);
104 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
106 if (!theFeature->isAction()) { // do not add action to the data model
107 TDF_Label aFeaturesLab = featuresLabel();
108 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
109 // store feature in the features array: before "initData" because in macro features
110 // in initData it creates new features, appeared later than this
111 TDF_Label aPrevFeateureLab;
112 if (theAfterThis.get()) { // searching for the previous feature label
113 std::shared_ptr<Model_Data> aPrevData =
114 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
115 if (aPrevData.get()) {
116 aPrevFeateureLab = aPrevData->label().Father();
119 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
121 // keep the feature ID to restore document later correctly
122 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
123 myFeatures.Bind(aFeatureLab, theFeature);
124 // must be after binding to the map because of "Box" macro feature that
125 // creates other features in "initData"
126 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
127 // event: feature is added
128 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
129 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
130 theFeature->setDisabled(false); // by default created feature is enabled
131 updateHistory(ModelAPI_Feature::group());
132 } else { // make feature has not-null data anyway
133 theFeature->setData(Model_Data::invalidData());
134 theFeature->setDoc(myDoc);
138 /// Appends to the array of references a new referenced label.
139 /// If theIndex is not -1, removes element at this index, not theReferenced.
140 /// \returns the index of removed element
141 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
142 const int theIndex = -1)
144 int aResult = -1; // no returned
145 Handle(TDataStd_ReferenceArray) aRefs;
146 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
147 if (aRefs->Length() == 1) { // just erase an array
148 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
149 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
152 } else { // reduce the array
153 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
155 int aCount = aRefs->Lower();
156 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
157 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
161 aNewArray->SetValue(aCount, aRefs->Value(a));
164 aRefs->SetInternalArray(aNewArray);
170 void Model_Objects::refsToFeature(FeaturePtr theFeature,
171 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
173 // check the feature: it must have no depended objects on it
174 // the dependencies can be in the feature results
175 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
176 for(; aResIter != theFeature->results().cend(); aResIter++) {
177 ResultPtr aResult = (*aResIter);
178 std::shared_ptr<Model_Data> aData =
179 std::dynamic_pointer_cast<Model_Data>(aResult->data());
180 if (aData.get() != NULL) {
181 const std::set<AttributePtr>& aRefs = aData->refsToMe();
182 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
183 for(; aRefIt != aRefLast; aRefIt++) {
184 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
185 if (aFeature.get() != NULL)
186 theRefs.insert(aFeature);
190 // the dependencies can be in the feature itself
191 std::shared_ptr<Model_Data> aData =
192 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
193 if (aData && !aData->refsToMe().empty()) {
194 const std::set<AttributePtr>& aRefs = aData->refsToMe();
195 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
196 for(; aRefIt != aRefLast; aRefIt++) {
197 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
198 if (aFeature.get() != NULL)
199 theRefs.insert(aFeature);
203 if (!theRefs.empty() && isSendError) {
205 "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
209 void Model_Objects::removeFeature(FeaturePtr theFeature)
211 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
212 if (aData && aData->isValid()) {
213 // checking that the sub-element of composite feature is removed: if yes, inform the owner
214 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
215 refsToFeature(theFeature, aRefs, false);
216 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
217 for(; aRefIter != aRefs.end(); aRefIter++) {
218 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
219 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
220 if (aComposite.get()) {
221 aComposite->removeFeature(theFeature);
224 // this must be before erase since theFeature erasing removes all information about
225 // the feature results and groups of results
226 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
227 clearHistory(theFeature);
231 TDF_Label aFeatureLabel = aData->label().Father();
232 if (myFeatures.IsBound(aFeatureLabel))
233 myFeatures.UnBind(aFeatureLabel);
235 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
236 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
237 // erase all attributes under the label of feature
238 aFeatureLabel.ForgetAllAttributes();
239 // remove it from the references array
240 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
241 // event: feature is deleted
242 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
243 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
244 Events_Loop::loop()->flush(EVENT_DISP);
245 updateHistory(ModelAPI_Feature::group());
249 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
251 TDF_Label aFeaturesLab = featuresLabel();
252 Handle(TDataStd_ReferenceArray) aRefs;
253 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
255 TDF_Label anAfterLab, aMovedLab =
256 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
257 if (theAfterThis.get())
258 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
260 Handle(TDataStd_HLabelArray1) aNewArray =
261 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
262 int aPassedMovedFrom = 0; // the prev feature location is found and passed
263 int aPassedMovedTo = 0; // the feature is added and this location is passed
264 if (!theAfterThis.get()) { // null means that inserted feature must be the first
265 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
268 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
269 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
271 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
272 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
273 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
274 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
276 } else { // just copy one
277 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
278 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
281 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
282 if (!aPassedMovedFrom) {
283 static std::string aMovedFromError("The moved feature is not found");
284 Events_Error::send(aMovedFromError);
286 static std::string aMovedToError("The 'after' feature for movement is not found");
287 Events_Error::send(aMovedToError);
291 // store the new array
292 aRefs->SetInternalArray(aNewArray);
293 // update the feature and the history
294 clearHistory(theMoved);
295 static Events_ID EVENT_UPD = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
296 ModelAPI_EventCreator::get()->sendUpdated(theMoved, EVENT_UPD);
299 void Model_Objects::clearHistory(ObjectPtr theObj)
302 const std::string aGroup = theObj->groupName();
303 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(aGroup);
304 if (aHIter != myHistory.end())
305 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
306 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
307 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
308 std::string aResultGroup = featureResultGroup(aFeature);
309 if (!aResultGroup.empty()) {
310 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
311 myHistory.find(aResultGroup);
312 if (aHIter != myHistory.end())
313 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
319 void Model_Objects::createHistory(const std::string& theGroupID)
321 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
322 if (aHIter == myHistory.end()) {
323 myHistory[theGroupID] = std::vector<ObjectPtr>();
324 std::vector<ObjectPtr>& aResult = myHistory[theGroupID];
325 // iterate the array of references and get feature by feature from the array
326 bool isFeature = theGroupID == ModelAPI_Feature::group();
327 Handle(TDataStd_ReferenceArray) aRefs;
328 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
329 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
330 FeaturePtr aFeature = feature(aRefs->Value(a));
331 if (aFeature.get()) {
332 // if feature is in sub-component, remove it from history: it is in sub-tree of sub-component
333 if (!ModelAPI_Tools::compositeOwner(aFeature).get()) {
334 if (isFeature) { // here may be also disabled features
335 if (aFeature->isInHistory()) {
336 aResult.push_back(aFeature);
338 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
339 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
340 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
341 for (; aRIter != aResults.cend(); aRIter++) {
342 ResultPtr aRes = *aRIter;
343 if (aRes->groupName() != theGroupID) break; // feature have only same group results
344 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
345 aResult.push_back(*aRIter);
356 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
358 clearHistory(theObject);
361 void Model_Objects::updateHistory(const std::string theGroup)
363 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
364 if (aHIter != myHistory.end())
365 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
368 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
370 if (myFeatures.IsBound(theLabel))
371 return myFeatures.Find(theLabel);
372 return FeaturePtr(); // not found
375 ObjectPtr Model_Objects::object(TDF_Label theLabel)
377 // try feature by label
378 FeaturePtr aFeature = feature(theLabel);
380 return feature(theLabel);
381 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
382 aFeature = feature(aFeatureLabel);
384 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
385 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
386 for (; aRIter != aResults.cend(); aRIter++) {
387 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
389 if (aResData->label().Father().IsEqual(theLabel))
393 return FeaturePtr(); // not found
396 ObjectPtr Model_Objects::object(const std::string& theGroupID, const int theIndex)
400 createHistory(theGroupID);
401 return myHistory[theGroupID][theIndex];
404 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
405 const std::string& theGroupID, const std::string& theName)
407 createHistory(theGroupID);
408 std::vector<ObjectPtr>& allObjs = myHistory[theGroupID];
409 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin();
410 for(; anObjIter != allObjs.end(); anObjIter++) {
411 if ((*anObjIter)->data()->name() == theName)
418 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject)
420 std::string aGroup = theObject->groupName();
421 createHistory(aGroup);
422 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
423 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
424 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
425 if ((*anObjIter) == theObject)
432 int Model_Objects::size(const std::string& theGroupID)
434 createHistory(theGroupID);
435 return myHistory[theGroupID].size();
438 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
440 // iterate the array of references and get feature by feature from the array
441 Handle(TDataStd_ReferenceArray) aRefs;
442 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
443 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
444 FeaturePtr aFeature = feature(aRefs->Value(a));
445 if (aFeature.get()) {
446 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
447 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
448 for (; aRIter != aResults.cend(); aRIter++) {
449 ResultPtr aRes = *aRIter;
450 if (aRes->groupName() != theGroupID) break; // feature have only same group results
451 if (aRes->isInHistory() && !aRes->isConcealed()) {
452 theResults.push_back(*aRIter);
461 TDF_Label Model_Objects::featuresLabel() const
463 return myMain.FindChild(TAG_OBJECTS);
466 void Model_Objects::setUniqueName(FeaturePtr theFeature)
468 if (!theFeature->data()->name().empty())
469 return; // not needed, name is already defined
470 std::string aName; // result
471 // first count all features of such kind to start with index = count + 1
473 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
474 for (; aFIter.More(); aFIter.Next()) {
475 if (aFIter.Value()->getKind() == theFeature->getKind())
478 // generate candidate name
479 std::stringstream aNameStream;
480 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
481 aName = aNameStream.str();
482 // check this is unique, if not, increase index by 1
483 for (aFIter.Initialize(myFeatures); aFIter.More();) {
484 FeaturePtr aFeature = aFIter.Value();
485 bool isSameName = aFeature->data()->name() == aName;
486 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
487 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
488 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
489 for (; aRIter != aResults.cend(); aRIter++) {
490 isSameName = (*aRIter)->data()->name() == aName;
495 std::stringstream aNameStream;
496 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
497 aName = aNameStream.str();
498 // reinitialize iterator to make sure a new name is unique
499 aFIter.Initialize(myFeatures);
503 theFeature->data()->setName(aName);
506 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
508 std::shared_ptr<Model_Data> aData(new Model_Data);
509 aData->setLabel(theLab.FindChild(theTag));
510 aData->setObject(theObj);
511 theObj->setDoc(myDoc);
512 theObj->setData(aData);
513 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
515 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
517 theObj->initAttributes();
520 void Model_Objects::synchronizeFeatures(
521 const bool theMarkUpdated, const bool theUpdateReferences, const bool theFlush)
523 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
524 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
526 // after all updates, sends a message that groups of features were created or updated
527 Events_Loop* aLoop = Events_Loop::loop();
528 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
529 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
530 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
531 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
532 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
533 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
534 bool isActive = aLoop->activateFlushes(false);
536 // update all objects by checking are they on labels or not
537 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
538 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
539 for (; aLabIter.More(); aLabIter.Next()) {
540 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
542 if (!myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
544 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
545 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
546 .ToCString(), anOwner);
547 if (!aFeature) { // somethig is wrong, most probably, the opened document has invalid structure
548 Events_Error::send("Invalid type of object in the document");
549 aLabIter.Value()->Label().ForgetAllAttributes();
552 // this must be before "setData" to redo the sketch line correctly
553 myFeatures.Bind(aFeatureLabel, aFeature);
554 aNewFeatures.insert(aFeature);
555 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
556 updateHistory(aFeature);
557 aFeature->setDisabled(false); // by default created feature is enabled (this allows to recreate the results before "setCurrent" is called)
559 // event: model is updated
560 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
561 } else { // nothing is changed, both iterators are incremented
562 aFeature = myFeatures.Find(aFeatureLabel);
563 aKeptFeatures.insert(aFeature);
564 if (theMarkUpdated) {
565 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
570 // check all features are checked: if not => it was removed
571 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
572 while (aFIter.More()) {
573 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
574 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
575 FeaturePtr aFeature = aFIter.Value();
576 // event: model is updated
577 //if (aFeature->isInHistory()) {
578 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
580 // results of this feature must be redisplayed (hided)
581 // redisplay also removed feature (used for sketch and AISObject)
582 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
583 updateHistory(aFeature);
585 // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
586 myFeatures.UnBind(aFIter.Key());
587 // reinitialize iterator because unbind may corrupt the previous order in the map
588 aFIter.Initialize(myFeatures);
593 if (theUpdateReferences) {
594 synchronizeBackRefs();
596 // update results of the features (after features created because they may be connected, like sketch and sub elements)
597 // After synchronisation of back references because sketch must be set in sub-elements before "execute" by updateResults
598 std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
599 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
600 for (; aLabIter2.More(); aLabIter2.Next()) {
601 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
602 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
603 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
604 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
605 aComposites.push_back(aFeature);
606 updateResults(aFeature);
609 std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
610 for(; aComposite != aComposites.end(); aComposite++) {
611 updateResults(*aComposite);
614 // the synchronize should be done after updateResults in order to correct back references of updated results
615 if (theUpdateReferences) {
616 synchronizeBackRefs();
618 if (theMarkUpdated) { // this means there is no control what was modified => remove history cash
622 anOwner->executeFeatures() = false;
623 aLoop->activateFlushes(isActive);
626 aLoop->flush(aCreateEvent);
627 aLoop->flush(aDeleteEvent);
628 aLoop->flush(anUpdateEvent);
629 aLoop->flush(aRedispEvent);
630 aLoop->flush(aToHideEvent);
632 anOwner->executeFeatures() = true;
635 void Model_Objects::synchronizeBackRefs()
637 // keeps the concealed flags of result to catch the change and create created/deleted events
638 std::list<std::pair<ResultPtr, bool> > aConcealed;
639 // first cycle: erase all data about back-references
640 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
641 for(; aFeatures.More(); aFeatures.Next()) {
642 FeaturePtr aFeature = aFeatures.Value();
643 std::shared_ptr<Model_Data> aFData =
644 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
646 aFData->eraseBackReferences();
648 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
649 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
650 for (; aRIter != aResults.cend(); aRIter++) {
651 std::shared_ptr<Model_Data> aResData =
652 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
654 aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
655 aResData->eraseBackReferences();
660 // second cycle: set new back-references: only features may have reference, iterate only them
661 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
662 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
663 FeaturePtr aFeature = aFeatures.Value();
664 std::shared_ptr<Model_Data> aFData =
665 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
667 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
668 aFData->referencesToObjects(aRefs);
669 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
670 aRefsIter = aRefs.begin();
671 for(; aRefsIter != aRefs.end(); aRefsIter++) {
672 std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
673 for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
675 std::shared_ptr<Model_Data> aRefData =
676 std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
677 aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
678 // update enable/disable status: the nested status must be equal to the composite
679 CompositeFeaturePtr aComp =
680 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
682 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>(*aRefTo);
683 if (aReferenced.get()) {
684 aReferenced->setDisabled(aComp->isDisabled());
692 std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
693 for(; aCIter != aConcealed.end(); aCIter++) {
694 if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
695 if (aCIter->second) { // was concealed become not => creation event
696 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
697 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
698 } else { // was not concealed become concealed => delete event
699 ModelAPI_EventCreator::get()->sendDeleted(myDoc, aCIter->first->groupName());
700 // redisplay for the viewer (it must be disappeared also)
701 static Events_ID EVENT_DISP =
702 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
703 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
709 TDF_Label Model_Objects::resultLabel(
710 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
712 const std::shared_ptr<Model_Data>& aData =
713 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
714 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
717 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
718 std::shared_ptr<ModelAPI_Result> theResult,
719 const int theResultIndex)
721 theResult->setDoc(myDoc);
722 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
723 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
724 std::stringstream aNewName;
725 aNewName<<theFeatureData->name();
726 if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
727 aNewName<<"_"<<theResultIndex + 1;
728 theResult->data()->setName(aNewName.str());
732 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
733 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
735 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
736 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
737 ObjectPtr anOldObject = object(aLab);
738 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
740 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
743 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
744 storeResult(theFeatureData, aResult, theIndex);
749 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
750 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
752 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
753 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
754 ObjectPtr anOldObject = object(aLab);
755 std::shared_ptr<ModelAPI_ResultBody> aResult;
757 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
760 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
761 storeResult(theFeatureData, aResult, theIndex);
766 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
767 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
769 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
770 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
771 ObjectPtr anOldObject = object(aLab);
772 std::shared_ptr<ModelAPI_ResultPart> aResult;
774 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
777 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
778 storeResult(theFeatureData, aResult, theIndex);
783 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
784 const std::shared_ptr<ModelAPI_Result>& theOldPart,
785 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin, const int theIndex)
787 std::shared_ptr<ModelAPI_ResultPart> aResult;
788 if (theOldPart.get()) {
789 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theOldPart);
792 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
793 aResult->setDoc(myDoc);
794 aResult->setData(theOrigin->data());
799 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
800 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
802 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
803 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
804 ObjectPtr anOldObject = object(aLab);
805 std::shared_ptr<ModelAPI_ResultGroup> aResult;
807 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
810 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
811 storeResult(theFeatureData, aResult, theIndex);
816 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
817 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
819 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
820 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
821 ObjectPtr anOldObject = object(aLab);
822 std::shared_ptr<ModelAPI_ResultParameter> aResult;
824 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
827 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
828 storeResult(theFeatureData, aResult, theIndex);
833 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
834 const std::shared_ptr<ModelAPI_Result>& theResult)
836 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
838 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
839 return feature(aFeatureLab);
844 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
846 if (theFeature->data()->isValid()) {
847 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
848 if (aLabIter.More()) {
849 TDF_Label anArgLab = aLabIter.Value();
850 Handle(TDataStd_Comment) aGroup;
851 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
852 return TCollection_AsciiString(aGroup->Get()).ToCString();
856 static std::string anEmpty;
857 return anEmpty; // not found
860 void Model_Objects::updateResults(FeaturePtr theFeature)
862 // for not persistent is will be done by parametric updater automatically
863 //if (!theFeature->isPersistentResult()) return;
864 // check the existing results and remove them if there is nothing on the label
865 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
866 while(aResIter != theFeature->results().cend()) {
867 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
869 if (!aBody->data()->isValid()) {
870 // found a disappeared result => remove it
871 theFeature->eraseResultFromList(aBody);
872 // start iterate from beginning because iterator is corrupted by removing
873 aResIter = theFeature->results().cbegin();
880 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
882 // check that results are presented on all labels
883 int aResSize = theFeature->results().size();
884 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
885 for(; aLabIter.More(); aLabIter.Next()) {
886 // here must be GUID of the feature
887 int aResIndex = aLabIter.Value().Tag() - 1;
889 if (aResSize <= aResIndex) {
890 TDF_Label anArgLab = aLabIter.Value();
891 Handle(TDataStd_Comment) aGroup;
892 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
893 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
894 aNewBody = createBody(theFeature->data(), aResIndex);
895 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
896 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
897 theFeature->setResult(aNewP, aResIndex);
898 if (!aNewP->partDoc().get())
899 theFeature->execute(); // create the part result: it is better to restore the previous result if it is possible
901 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
902 theFeature->execute(); // construction shapes are needed for sketch solver
904 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
905 aNewBody = createGroup(theFeature->data(), aResIndex);
906 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
907 theFeature->attributeChanged("expression"); // just produce a value
910 Events_Error::send(std::string("Unknown type of result is found in the document:") +
911 TCollection_AsciiString(aGroup->Get()).ToCString());
915 theFeature->setResult(aNewBody, aResIndex);
921 ResultPtr Model_Objects::findByName(const std::string theName)
923 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
924 for(; anObjIter.More(); anObjIter.Next()) {
925 FeaturePtr& aFeature = anObjIter.ChangeValue();
926 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
928 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
929 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
930 for (; aRIter != aResults.cend(); aRIter++) {
931 ResultPtr aRes = *aRIter;
932 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
933 aRes->data()->name() == theName) {
942 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
944 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
945 if (aData && aData->isValid()) {
946 TDF_Label aFeatureLabel = aData->label().Father();
947 Handle(TDataStd_ReferenceArray) aRefs;
948 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
949 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
950 if (aRefs->Value(a).IsEqual(aFeatureLabel)) {
951 a += theReverse ? -1 : 1;
952 if (a >= aRefs->Lower() && a <= aRefs->Upper())
953 return feature(aRefs->Value(a));
954 break; // finish iiteration: it's last feature
959 return FeaturePtr(); // not found, last, or something is wrong
962 FeaturePtr Model_Objects::firstFeature()
964 Handle(TDataStd_ReferenceArray) aRefs;
965 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
966 return feature(aRefs->Value(aRefs->Lower()));
968 return FeaturePtr(); // no features at all
971 FeaturePtr Model_Objects::lastFeature()
973 Handle(TDataStd_ReferenceArray) aRefs;
974 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
975 return feature(aRefs->Value(aRefs->Upper()));
977 return FeaturePtr(); // no features at all
980 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
982 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
983 Handle(TDataStd_ReferenceArray) aRefs;
984 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
985 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
986 FeaturePtr aFeature = feature(aRefs->Value(a));
988 aResult.push_back(aFeature);
994 int Model_Objects::numInternalFeatures()
996 Handle(TDataStd_ReferenceArray) aRefs;
997 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
998 return aRefs->Upper() - aRefs->Lower() + 1;
1000 return 0; // invalid
1003 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
1005 Handle(TDataStd_ReferenceArray) aRefs;
1006 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1007 return feature(aRefs->Value(aRefs->Lower() + theIndex));
1009 return FeaturePtr(); // invalid
1012 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1014 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1017 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1019 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);