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>
20 #include <Events_Loop.h>
21 #include <Events_Error.h>
23 #include <TDataStd_Integer.hxx>
24 #include <TDataStd_Comment.hxx>
25 #include <TDF_ChildIDIterator.hxx>
26 #include <TDataStd_ReferenceArray.hxx>
27 #include <TDataStd_HLabelArray1.hxx>
28 #include <TDataStd_Name.hxx>
29 #include <TDF_Reference.hxx>
30 #include <TDF_ChildIDIterator.hxx>
31 #include <TDF_LabelMapHasher.hxx>
33 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
36 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
37 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
40 /// 0:1:2 - where features are located
41 /// 0:1:2:N:1 - data of the feature N
42 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
44 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
48 void Model_Objects::setOwner(DocumentPtr theDoc)
51 // update all fields and recreate features and result objects if needed
52 synchronizeFeatures(false, true, true);
56 Model_Objects::~Model_Objects()
58 // delete all features of this document
59 Events_Loop* aLoop = Events_Loop::loop();
60 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
61 for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
62 FeaturePtr aFeature = aFeaturesIter.Value();
63 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
64 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
65 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
66 aFeature->eraseResults();
70 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
71 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
75 /// Appends to the array of references a new referenced label
76 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
78 Handle(TDataStd_ReferenceArray) aRefs;
79 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
80 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
81 aRefs->SetValue(0, theReferenced);
82 } else { // extend array by one more element
83 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
85 int aPassedPrev = 0; // prev feature is found and passed
86 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
87 aNewArray->SetValue(aRefs->Lower(), theReferenced);
90 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
91 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
92 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
94 aNewArray->SetValue(a + 1, theReferenced);
97 if (!aPassedPrev) // not found: unknown situation
98 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
99 aRefs->SetInternalArray(aNewArray);
103 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
105 if (!theFeature->isAction()) { // do not add action to the data model
106 TDF_Label aFeaturesLab = featuresLabel();
107 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
108 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
109 // keep the feature ID to restore document later correctly
110 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
111 myFeatures.Bind(aFeatureLab, theFeature);
112 // store feature in the features array
113 TDF_Label aPrevFeateureLab;
114 if (theAfterThis.get()) { // searching for the previous feature label
115 std::shared_ptr<Model_Data> aPrevData =
116 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
117 if (aPrevData.get()) {
118 aPrevFeateureLab = aPrevData->label().Father();
121 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
122 // event: feature is added
123 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
124 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
125 theFeature->setDisabled(false); // by default created feature is enabled
126 updateHistory(ModelAPI_Feature::group());
130 /// Appends to the array of references a new referenced label.
131 /// If theIndex is not -1, removes element at this index, not theReferenced.
132 /// \returns the index of removed element
133 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
134 const int theIndex = -1)
136 int aResult = -1; // no returned
137 Handle(TDataStd_ReferenceArray) aRefs;
138 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
139 if (aRefs->Length() == 1) { // just erase an array
140 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
141 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
144 } else { // reduce the array
145 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
147 int aCount = aRefs->Lower();
148 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
149 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
153 aNewArray->SetValue(aCount, aRefs->Value(a));
156 aRefs->SetInternalArray(aNewArray);
162 void Model_Objects::refsToFeature(FeaturePtr theFeature,
163 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
165 // check the feature: it must have no depended objects on it
166 // the dependencies can be in the feature results
167 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
168 for(; aResIter != theFeature->results().cend(); aResIter++) {
169 ResultPtr aResult = (*aResIter);
170 std::shared_ptr<Model_Data> aData =
171 std::dynamic_pointer_cast<Model_Data>(aResult->data());
172 if (aData.get() != NULL) {
173 const std::set<AttributePtr>& aRefs = aData->refsToMe();
174 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
175 for(; aRefIt != aRefLast; aRefIt++) {
176 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
177 if (aFeature.get() != NULL)
178 theRefs.insert(aFeature);
182 // the dependencies can be in the feature itself
183 std::shared_ptr<Model_Data> aData =
184 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
185 if (aData && !aData->refsToMe().empty()) {
186 const std::set<AttributePtr>& aRefs = aData->refsToMe();
187 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
188 for(; aRefIt != aRefLast; aRefIt++) {
189 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
190 if (aFeature.get() != NULL)
191 theRefs.insert(aFeature);
195 if (!theRefs.empty() && isSendError) {
197 "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
201 void Model_Objects::removeFeature(FeaturePtr theFeature)
203 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
205 TDF_Label aFeatureLabel = aData->label().Father();
206 if (myFeatures.IsBound(aFeatureLabel))
207 myFeatures.UnBind(aFeatureLabel);
209 return; // not found feature => do not remove
211 clearHistory(theFeature);
212 // checking that the sub-element of composite feature is removed: if yes, inform the owner
213 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
214 refsToFeature(theFeature, aRefs, false);
215 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
216 for(; aRefIter != aRefs.end(); aRefIter++) {
217 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
218 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
219 if (aComposite.get()) {
220 aComposite->removeFeature(theFeature);
225 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
226 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
227 // erase all attributes under the label of feature
228 aFeatureLabel.ForgetAllAttributes();
229 // remove it from the references array
230 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
231 // event: feature is deleted
232 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
233 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
234 Events_Loop::loop()->flush(EVENT_DISP);
235 updateHistory(ModelAPI_Feature::group());
239 void Model_Objects::clearHistory(ObjectPtr theObj)
242 const std::string aGroup = theObj->groupName();
243 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(aGroup);
244 if (aHIter != myHistory.end())
245 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
246 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
247 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
248 if (aFeature->firstResult().get())
249 clearHistory(aFeature->firstResult());
254 void Model_Objects::createHistory(const std::string& theGroupID)
256 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
257 if (aHIter == myHistory.end()) {
258 myHistory[theGroupID] = std::vector<ObjectPtr>();
259 std::vector<ObjectPtr>& aResult = myHistory[theGroupID];
260 // iterate the array of references and get feature by feature from the array
261 bool isFeature = theGroupID == ModelAPI_Feature::group();
262 Handle(TDataStd_ReferenceArray) aRefs;
263 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
264 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
265 FeaturePtr aFeature = feature(aRefs->Value(a));
266 if (aFeature.get()) {
267 if (isFeature) { // here may be also disabled features
268 if (aFeature->isInHistory()) {
269 aResult.push_back(aFeature);
271 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
272 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
273 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
274 for (; aRIter != aResults.cend(); aRIter++) {
275 ResultPtr aRes = *aRIter;
276 if (aRes->groupName() != theGroupID) break; // feature have only same group results
277 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
278 aResult.push_back(*aRIter);
288 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
290 clearHistory(theObject);
293 void Model_Objects::updateHistory(const std::string theGroup)
295 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
296 if (aHIter != myHistory.end())
297 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
300 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
302 if (myFeatures.IsBound(theLabel))
303 return myFeatures.Find(theLabel);
304 return FeaturePtr(); // not found
307 ObjectPtr Model_Objects::object(TDF_Label theLabel)
309 // try feature by label
310 FeaturePtr aFeature = feature(theLabel);
312 return feature(theLabel);
313 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
314 aFeature = feature(aFeatureLabel);
316 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
317 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
318 for (; aRIter != aResults.cend(); aRIter++) {
319 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
321 if (aResData->label().Father().IsEqual(theLabel))
325 return FeaturePtr(); // not found
328 ObjectPtr Model_Objects::object(const std::string& theGroupID, const int theIndex)
330 createHistory(theGroupID);
331 return myHistory[theGroupID][theIndex];
334 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
335 const std::string& theGroupID, const std::string& theName)
337 createHistory(theGroupID);
338 std::vector<ObjectPtr>& allObjs = myHistory[theGroupID];
339 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin();
340 for(; anObjIter != allObjs.end(); anObjIter++) {
341 if ((*anObjIter)->data()->name() == theName)
348 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject)
350 std::string aGroup = theObject->groupName();
351 createHistory(aGroup);
352 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
353 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
354 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
355 if ((*anObjIter) == theObject)
362 int Model_Objects::size(const std::string& theGroupID)
364 createHistory(theGroupID);
365 return myHistory[theGroupID].size();
368 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
370 // iterate the array of references and get feature by feature from the array
371 Handle(TDataStd_ReferenceArray) aRefs;
372 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
373 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
374 FeaturePtr aFeature = feature(aRefs->Value(a));
375 if (aFeature.get()) {
376 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
377 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
378 for (; aRIter != aResults.cend(); aRIter++) {
379 ResultPtr aRes = *aRIter;
380 if (aRes->groupName() != theGroupID) break; // feature have only same group results
381 if (aRes->isInHistory() && !aRes->isConcealed()) {
382 theResults.push_back(*aRIter);
391 TDF_Label Model_Objects::featuresLabel() const
393 return myMain.FindChild(TAG_OBJECTS);
396 void Model_Objects::setUniqueName(FeaturePtr theFeature)
398 if (!theFeature->data()->name().empty())
399 return; // not needed, name is already defined
400 std::string aName; // result
401 // first count all features of such kind to start with index = count + 1
403 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
404 for (; aFIter.More(); aFIter.Next()) {
405 if (aFIter.Value()->getKind() == theFeature->getKind())
408 // generate candidate name
409 std::stringstream aNameStream;
410 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
411 aName = aNameStream.str();
412 // check this is unique, if not, increase index by 1
413 for (aFIter.Initialize(myFeatures); aFIter.More();) {
414 FeaturePtr aFeature = aFIter.Value();
415 bool isSameName = aFeature->data()->name() == aName;
416 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
417 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
418 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
419 for (; aRIter != aResults.cend(); aRIter++) {
420 isSameName = (*aRIter)->data()->name() == aName;
425 std::stringstream aNameStream;
426 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
427 aName = aNameStream.str();
428 // reinitialize iterator to make sure a new name is unique
429 aFIter.Initialize(myFeatures);
433 theFeature->data()->setName(aName);
436 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
438 std::shared_ptr<Model_Data> aData(new Model_Data);
439 aData->setLabel(theLab.FindChild(theTag));
440 aData->setObject(theObj);
441 theObj->setDoc(myDoc);
442 theObj->setData(aData);
443 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
445 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
447 theObj->initAttributes();
450 void Model_Objects::synchronizeFeatures(
451 const bool theMarkUpdated, const bool theUpdateReferences, const bool theFlush)
453 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
454 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
456 // after all updates, sends a message that groups of features were created or updated
457 Events_Loop* aLoop = Events_Loop::loop();
458 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
459 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
460 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
461 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
462 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
463 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
464 aLoop->activateFlushes(false);
466 // update all objects by checking are they on labels or not
467 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
468 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
469 for (; aLabIter.More(); aLabIter.Next()) {
470 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
472 if (!myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
474 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
475 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
476 .ToCString(), anOwner);
477 if (!aFeature) { // somethig is wrong, most probably, the opened document has invalid structure
478 Events_Error::send("Invalid type of object in the document");
479 aLabIter.Value()->Label().ForgetAllAttributes();
482 // this must be before "setData" to redo the sketch line correctly
483 myFeatures.Bind(aFeatureLabel, aFeature);
484 aNewFeatures.insert(aFeature);
485 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
486 updateHistory(aFeature);
487 aFeature->setDisabled(false); // by default created feature is enabled (this allows to recreate the results before "setCurrent" is called)
489 // event: model is updated
490 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
491 } else { // nothing is changed, both iterators are incremented
492 aFeature = myFeatures.Find(aFeatureLabel);
493 aKeptFeatures.insert(aFeature);
494 if (theMarkUpdated) {
495 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
499 // update results of the features (after features created because they may be connected, like sketch and sub elements)
500 std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
501 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
502 for (; aLabIter2.More(); aLabIter2.Next()) {
503 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
504 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
505 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
506 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
507 aComposites.push_back(aFeature);
508 updateResults(aFeature);
511 std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
512 for(; aComposite != aComposites.end(); aComposite++) {
513 updateResults(*aComposite);
516 // check all features are checked: if not => it was removed
517 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
518 while (aFIter.More()) {
519 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
520 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
521 FeaturePtr aFeature = aFIter.Value();
522 // event: model is updated
523 //if (aFeature->isInHistory()) {
524 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
526 // results of this feature must be redisplayed (hided)
527 // redisplay also removed feature (used for sketch and AISObject)
528 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
529 updateHistory(aFeature);
531 // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
532 myFeatures.UnBind(aFIter.Key());
533 // reinitialize iterator because unbind may corrupt the previous order in the map
534 aFIter.Initialize(myFeatures);
539 if (theUpdateReferences) {
540 synchronizeBackRefs();
542 if (theMarkUpdated) { // this means there is no control what was modified => remove history cash
546 anOwner->executeFeatures() = false;
547 aLoop->activateFlushes(true);
550 aLoop->flush(aCreateEvent);
551 aLoop->flush(aDeleteEvent);
552 aLoop->flush(anUpdateEvent);
553 aLoop->flush(aRedispEvent);
554 aLoop->flush(aToHideEvent);
556 anOwner->executeFeatures() = true;
559 void Model_Objects::synchronizeBackRefs()
561 // keeps the concealed flags of result to catch the change and create created/deleted events
562 std::list<std::pair<ResultPtr, bool> > aConcealed;
563 // first cycle: erase all data about back-references
564 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
565 for(; aFeatures.More(); aFeatures.Next()) {
566 FeaturePtr aFeature = aFeatures.Value();
567 std::shared_ptr<Model_Data> aFData =
568 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
570 aFData->eraseBackReferences();
572 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
573 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
574 for (; aRIter != aResults.cend(); aRIter++) {
575 std::shared_ptr<Model_Data> aResData =
576 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
578 aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
579 aResData->eraseBackReferences();
584 // second cycle: set new back-references: only features may have reference, iterate only them
585 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
586 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
587 FeaturePtr aFeature = aFeatures.Value();
588 std::shared_ptr<Model_Data> aFData =
589 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
591 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
592 aFData->referencesToObjects(aRefs);
593 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
594 aRefsIter = aRefs.begin();
595 for(; aRefsIter != aRefs.end(); aRefsIter++) {
596 std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
597 for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
599 std::shared_ptr<Model_Data> aRefData =
600 std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
601 aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
602 // update enable/disable status: the nested status must be equal to the composite
603 CompositeFeaturePtr aComp =
604 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
606 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>(*aRefTo);
607 if (aReferenced.get()) {
608 aReferenced->setDisabled(aComp->isDisabled());
616 std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
617 for(; aCIter != aConcealed.end(); aCIter++) {
618 if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
619 if (aCIter->second) { // was concealed become not => creation event
620 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
621 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
622 } else { // was not concealed become concealed => delete event
623 ModelAPI_EventCreator::get()->sendDeleted(myDoc, aCIter->first->groupName());
624 // redisplay for the viewer (it must be disappeared also)
625 static Events_ID EVENT_DISP =
626 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
627 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
633 TDF_Label Model_Objects::resultLabel(
634 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
636 const std::shared_ptr<Model_Data>& aData =
637 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
638 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
641 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
642 std::shared_ptr<ModelAPI_Result> theResult,
643 const int theResultIndex)
645 theResult->setDoc(myDoc);
646 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
647 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
648 std::stringstream aNewName;
649 aNewName<<theFeatureData->name();
650 if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
651 aNewName<<"_"<<theResultIndex + 1;
652 theResult->data()->setName(aNewName.str());
656 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
657 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
659 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
660 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
661 ObjectPtr anOldObject = object(aLab);
662 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
664 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
667 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
668 storeResult(theFeatureData, aResult, theIndex);
673 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
674 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
676 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
677 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
678 ObjectPtr anOldObject = object(aLab);
679 std::shared_ptr<ModelAPI_ResultBody> aResult;
681 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
684 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
685 storeResult(theFeatureData, aResult, theIndex);
690 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
691 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
693 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
694 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
695 ObjectPtr anOldObject = object(aLab);
696 std::shared_ptr<ModelAPI_ResultPart> aResult;
698 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
701 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
702 storeResult(theFeatureData, aResult, theIndex);
707 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
708 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
710 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
711 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
712 ObjectPtr anOldObject = object(aLab);
713 std::shared_ptr<ModelAPI_ResultGroup> aResult;
715 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
718 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
719 storeResult(theFeatureData, aResult, theIndex);
724 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
725 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
727 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
728 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
729 ObjectPtr anOldObject = object(aLab);
730 std::shared_ptr<ModelAPI_ResultParameter> aResult;
732 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
735 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
736 storeResult(theFeatureData, aResult, theIndex);
741 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
742 const std::shared_ptr<ModelAPI_Result>& theResult)
744 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
746 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
747 return feature(aFeatureLab);
752 void Model_Objects::updateResults(FeaturePtr theFeature)
754 // for not persistent is will be done by parametric updater automatically
755 //if (!theFeature->isPersistentResult()) return;
756 // check the existing results and remove them if there is nothing on the label
757 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
758 while(aResIter != theFeature->results().cend()) {
759 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
761 if (!aBody->data()->isValid()) {
762 // found a disappeared result => remove it
763 theFeature->removeResult(aBody);
764 // start iterate from beginning because iterator is corrupted by removing
765 aResIter = theFeature->results().cbegin();
772 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
774 // check that results are presented on all labels
775 int aResSize = theFeature->results().size();
776 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
777 for(; aLabIter.More(); aLabIter.Next()) {
778 // here must be GUID of the feature
779 int aResIndex = aLabIter.Value().Tag() - 1;
781 if (aResSize <= aResIndex) {
782 TDF_Label anArgLab = aLabIter.Value();
783 Handle(TDataStd_Comment) aGroup;
784 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
785 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
786 aNewBody = createBody(theFeature->data(), aResIndex);
787 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
788 //aNewBody = createPart(theFeature->data(), aResIndex);
789 theFeature->execute(); // create the part result
791 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
792 theFeature->execute(); // construction shapes are needed for sketch solver
794 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
795 aNewBody = createGroup(theFeature->data(), aResIndex);
796 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
797 theFeature->attributeChanged("expression"); // just produce a value
800 Events_Error::send(std::string("Unknown type of result is found in the document:") +
801 TCollection_AsciiString(aGroup->Get()).ToCString());
805 theFeature->setResult(aNewBody, aResIndex);
811 ResultPtr Model_Objects::findByName(const std::string theName)
813 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
814 for(; anObjIter.More(); anObjIter.Next()) {
815 FeaturePtr& aFeature = anObjIter.ChangeValue();
816 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
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 ResultPtr aRes = *aRIter;
822 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
823 aRes->data()->name() == theName) {
832 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
834 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
836 TDF_Label aFeatureLabel = aData->label().Father();
837 Handle(TDataStd_ReferenceArray) aRefs;
838 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
839 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
840 if (aRefs->Value(a).IsEqual(aFeatureLabel)) {
841 a += theReverse ? -1 : 1;
842 if (a >= aRefs->Lower() && a <= aRefs->Upper())
843 return feature(aRefs->Value(a));
844 break; // finish iiteration: it's last feature
849 return FeaturePtr(); // not found, last, or something is wrong
852 FeaturePtr Model_Objects::firstFeature()
854 Handle(TDataStd_ReferenceArray) aRefs;
855 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
856 return feature(aRefs->Value(aRefs->Lower()));
858 return FeaturePtr(); // no features at all
861 FeaturePtr Model_Objects::lastFeature()
863 Handle(TDataStd_ReferenceArray) aRefs;
864 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
865 return feature(aRefs->Value(aRefs->Upper()));
867 return FeaturePtr(); // no features at all
870 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
872 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
875 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
877 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);