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)
46 // update all fields and recreate features and result objects if needed
47 synchronizeFeatures(false, true, true);
50 Model_Objects::~Model_Objects()
52 // delete all features of this document
53 Events_Loop* aLoop = Events_Loop::loop();
54 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
55 for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
56 FeaturePtr aFeature = aFeaturesIter.Value();
57 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
58 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
59 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
60 aFeature->eraseResults();
64 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
65 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
69 /// Appends to the array of references a new referenced label
70 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
72 Handle(TDataStd_ReferenceArray) aRefs;
73 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
74 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
75 aRefs->SetValue(0, theReferenced);
76 } else { // extend array by one more element
77 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
79 int aPassedPrev = 0; // prev feature is found and passed
80 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
81 aNewArray->SetValue(aRefs->Lower(), theReferenced);
84 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
85 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
86 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
88 aNewArray->SetValue(a + 1, theReferenced);
91 if (!aPassedPrev) // not found: unknown situation
92 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
93 aRefs->SetInternalArray(aNewArray);
97 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
99 if (!theFeature->isAction()) { // do not add action to the data model
100 TDF_Label aFeaturesLab = featuresLabel();
101 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
102 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
103 // keep the feature ID to restore document later correctly
104 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
105 myFeatures.Bind(aFeatureLab, theFeature);
106 // store feature in the features array
107 TDF_Label aPrevFeateureLab;
108 if (theAfterThis.get()) { // searching for the previous feature label
109 std::shared_ptr<Model_Data> aPrevData =
110 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
111 if (aPrevData.get()) {
112 aPrevFeateureLab = aPrevData->label().Father();
115 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
116 // event: feature is added
117 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
118 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
119 theFeature->setDisabled(false); // by default created feature is enabled
120 updateHistory(ModelAPI_Feature::group());
124 /// Appends to the array of references a new referenced label.
125 /// If theIndex is not -1, removes element at this index, not theReferenced.
126 /// \returns the index of removed element
127 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
128 const int theIndex = -1)
130 int aResult = -1; // no returned
131 Handle(TDataStd_ReferenceArray) aRefs;
132 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
133 if (aRefs->Length() == 1) { // just erase an array
134 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
135 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
138 } else { // reduce the array
139 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
141 int aCount = aRefs->Lower();
142 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
143 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
147 aNewArray->SetValue(aCount, aRefs->Value(a));
150 aRefs->SetInternalArray(aNewArray);
156 void Model_Objects::refsToFeature(FeaturePtr theFeature,
157 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
159 // check the feature: it must have no depended objects on it
160 // the dependencies can be in the feature results
161 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
162 for(; aResIter != theFeature->results().cend(); aResIter++) {
163 ResultPtr aResult = (*aResIter);
164 std::shared_ptr<Model_Data> aData =
165 std::dynamic_pointer_cast<Model_Data>(aResult->data());
166 if (aData.get() != NULL) {
167 const std::set<AttributePtr>& aRefs = aData->refsToMe();
168 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
169 for(; aRefIt != aRefLast; aRefIt++) {
170 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
171 if (aFeature.get() != NULL)
172 theRefs.insert(aFeature);
176 // the dependencies can be in the feature itself
177 std::shared_ptr<Model_Data> aData =
178 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
179 if (aData && !aData->refsToMe().empty()) {
180 const std::set<AttributePtr>& aRefs = aData->refsToMe();
181 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
182 for(; aRefIt != aRefLast; aRefIt++) {
183 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
184 if (aFeature.get() != NULL)
185 theRefs.insert(aFeature);
189 if (!theRefs.empty() && isSendError) {
191 "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
195 void Model_Objects::removeFeature(FeaturePtr theFeature)
197 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
199 TDF_Label aFeatureLabel = aData->label().Father();
200 if (myFeatures.IsBound(aFeatureLabel))
201 myFeatures.UnBind(aFeatureLabel);
203 return; // not found feature => do not remove
205 clearHistory(theFeature);
206 // checking that the sub-element of composite feature is removed: if yes, inform the owner
207 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
208 refsToFeature(theFeature, aRefs, false);
209 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
210 for(; aRefIter != aRefs.end(); aRefIter++) {
211 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
212 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
213 if (aComposite.get()) {
214 aComposite->removeFeature(theFeature);
219 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
220 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
221 // erase all attributes under the label of feature
222 aFeatureLabel.ForgetAllAttributes();
223 // remove it from the references array
224 if (theFeature->isInHistory()) {
225 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
227 // event: feature is deleted
228 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
229 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
230 Events_Loop::loop()->flush(EVENT_DISP);
231 updateHistory(ModelAPI_Feature::group());
235 void Model_Objects::clearHistory(ObjectPtr theObj)
238 const std::string aGroup = theObj->groupName();
239 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(aGroup);
240 if (aHIter != myHistory.end())
241 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
242 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
243 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
244 if (aFeature->firstResult().get())
245 clearHistory(aFeature->firstResult());
250 void Model_Objects::createHistory(const std::string& theGroupID)
252 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
253 if (aHIter == myHistory.end()) {
254 myHistory[theGroupID] = std::vector<ObjectPtr>();
255 std::vector<ObjectPtr>& aResult = myHistory[theGroupID];
256 // iterate the array of references and get feature by feature from the array
257 bool isFeature = theGroupID == ModelAPI_Feature::group();
258 Handle(TDataStd_ReferenceArray) aRefs;
259 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
260 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
261 FeaturePtr aFeature = feature(aRefs->Value(a));
262 if (aFeature.get()) {
263 if (isFeature) { // here may be also disabled features
264 if (aFeature->isInHistory()) {
265 aResult.push_back(aFeature);
267 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
268 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
269 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
270 for (; aRIter != aResults.cend(); aRIter++) {
271 ResultPtr aRes = *aRIter;
272 if (aRes->groupName() != theGroupID) break; // feature have only same group results
273 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
274 aResult.push_back(*aRIter);
284 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
286 clearHistory(theObject);
289 void Model_Objects::updateHistory(const std::string theGroup)
291 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
292 if (aHIter != myHistory.end())
293 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
296 FeaturePtr Model_Objects::feature(TDF_Label& theLabel) const
298 if (myFeatures.IsBound(theLabel))
299 return myFeatures.Find(theLabel);
300 return FeaturePtr(); // not found
303 ObjectPtr Model_Objects::object(TDF_Label theLabel)
305 // try feature by label
306 FeaturePtr aFeature = feature(theLabel);
308 return feature(theLabel);
309 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
310 aFeature = feature(aFeatureLabel);
312 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
313 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
314 for (; aRIter != aResults.cend(); aRIter++) {
315 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
317 if (aResData->label().Father().IsEqual(theLabel))
321 return FeaturePtr(); // not found
324 ObjectPtr Model_Objects::object(const std::string& theGroupID, const int theIndex)
326 createHistory(theGroupID);
327 return myHistory[theGroupID][theIndex];
330 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
331 const std::string& theGroupID, const std::string& theName)
333 createHistory(theGroupID);
334 std::vector<ObjectPtr>& allObjs = myHistory[theGroupID];
335 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin();
336 for(; anObjIter != allObjs.end(); anObjIter++) {
337 if ((*anObjIter)->data()->name() == theName)
344 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject)
346 std::string aGroup = theObject->groupName();
347 createHistory(aGroup);
348 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
349 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
350 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
351 if ((*anObjIter) == theObject)
358 int Model_Objects::size(const std::string& theGroupID)
360 createHistory(theGroupID);
361 return myHistory[theGroupID].size();
364 TDF_Label Model_Objects::featuresLabel() const
366 return myMain.FindChild(TAG_OBJECTS);
369 void Model_Objects::setUniqueName(FeaturePtr theFeature)
371 if (!theFeature->data()->name().empty())
372 return; // not needed, name is already defined
373 std::string aName; // result
374 // first count all features of such kind to start with index = count + 1
376 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
377 for (; aFIter.More(); aFIter.Next()) {
378 if (aFIter.Value()->getKind() == theFeature->getKind())
381 // generate candidate name
382 std::stringstream aNameStream;
383 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
384 aName = aNameStream.str();
385 // check this is unique, if not, increase index by 1
386 for (aFIter.Initialize(myFeatures); aFIter.More();) {
387 FeaturePtr aFeature = aFIter.Value();
388 bool isSameName = aFeature->data()->name() == aName;
389 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
390 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
391 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
392 for (; aRIter != aResults.cend(); aRIter++) {
393 isSameName = (*aRIter)->data()->name() == aName;
398 std::stringstream aNameStream;
399 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
400 aName = aNameStream.str();
401 // reinitialize iterator to make sure a new name is unique
402 aFIter.Initialize(myFeatures);
406 theFeature->data()->setName(aName);
409 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
411 std::shared_ptr<Model_Data> aData(new Model_Data);
412 aData->setLabel(theLab.FindChild(theTag));
413 aData->setObject(theObj);
414 theObj->setDoc(myDoc);
415 theObj->setData(aData);
416 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
418 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
420 theObj->initAttributes();
423 void Model_Objects::synchronizeFeatures(
424 const bool theMarkUpdated, const bool theUpdateReferences, const bool theFlush)
426 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
427 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
429 // after all updates, sends a message that groups of features were created or updated
430 Events_Loop* aLoop = Events_Loop::loop();
431 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
432 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
433 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
434 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
435 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
436 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
437 aLoop->activateFlushes(false);
439 // update all objects by checking are they on labels or not
440 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
441 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
442 for (; aLabIter.More(); aLabIter.Next()) {
443 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
445 if (!myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
447 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
448 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
449 .ToCString(), anOwner);
450 if (!aFeature) { // somethig is wrong, most probably, the opened document has invalid structure
451 Events_Error::send("Invalid type of object in the document");
452 aLabIter.Value()->Label().ForgetAllAttributes();
455 // this must be before "setData" to redo the sketch line correctly
456 myFeatures.Bind(aFeatureLabel, aFeature);
457 aNewFeatures.insert(aFeature);
458 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
459 updateHistory(aFeature);
461 // event: model is updated
462 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
463 } else { // nothing is changed, both iterators are incremented
464 aFeature = myFeatures.Find(aFeatureLabel);
465 aKeptFeatures.insert(aFeature);
466 if (theMarkUpdated) {
467 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
471 // update results of the features (after features created because they may be connected, like sketch and sub elements)
472 std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
473 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
474 for (; aLabIter2.More(); aLabIter2.Next()) {
475 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
476 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
477 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
478 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
479 aComposites.push_back(aFeature);
480 updateResults(aFeature);
483 std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
484 for(; aComposite != aComposites.end(); aComposite++) {
485 updateResults(*aComposite);
488 // check all features are checked: if not => it was removed
489 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
490 while (aFIter.More()) {
491 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
492 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
493 FeaturePtr aFeature = aFIter.Value();
494 // event: model is updated
495 //if (aFeature->isInHistory()) {
496 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
498 // results of this feature must be redisplayed (hided)
499 // redisplay also removed feature (used for sketch and AISObject)
500 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
501 updateHistory(aFeature);
503 // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
504 myFeatures.UnBind(aFIter.Key());
505 // reinitialize iterator because unbind may corrupt the previous order in the map
506 aFIter.Initialize(myFeatures);
511 if (theUpdateReferences) {
512 synchronizeBackRefs();
514 if (theMarkUpdated) { // this means there is no control what was modified => remove history cash
518 anOwner->executeFeatures() = false;
519 aLoop->activateFlushes(true);
522 aLoop->flush(aCreateEvent);
523 aLoop->flush(aDeleteEvent);
524 aLoop->flush(anUpdateEvent);
525 aLoop->flush(aRedispEvent);
526 aLoop->flush(aToHideEvent);
528 anOwner->executeFeatures() = true;
531 void Model_Objects::synchronizeBackRefs()
533 // keeps the concealed flags of result to catch the change and create created/deleted events
534 std::list<std::pair<ResultPtr, bool> > aConcealed;
535 // first cycle: erase all data about back-references
536 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
537 for(; aFeatures.More(); aFeatures.Next()) {
538 FeaturePtr aFeature = aFeatures.Value();
539 std::shared_ptr<Model_Data> aFData =
540 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
542 aFData->eraseBackReferences();
544 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
545 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
546 for (; aRIter != aResults.cend(); aRIter++) {
547 std::shared_ptr<Model_Data> aResData =
548 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
550 aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
551 aResData->eraseBackReferences();
556 // second cycle: set new back-references: only features may have reference, iterate only them
557 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
558 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
559 FeaturePtr aFeature = aFeatures.Value();
560 std::shared_ptr<Model_Data> aFData =
561 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
563 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
564 aFData->referencesToObjects(aRefs);
565 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
566 aRefsIter = aRefs.begin();
567 for(; aRefsIter != aRefs.end(); aRefsIter++) {
568 std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
569 for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
571 std::shared_ptr<Model_Data> aRefData =
572 std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
573 aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
579 std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
580 for(; aCIter != aConcealed.end(); aCIter++) {
581 if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
582 if (aCIter->second) { // was concealed become not => creation event
583 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
584 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
585 } else { // was not concealed become concealed => delete event
586 ModelAPI_EventCreator::get()->sendDeleted(myDoc, aCIter->first->groupName());
587 // redisplay for the viewer (it must be disappeared also)
588 static Events_ID EVENT_DISP =
589 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
590 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
596 TDF_Label Model_Objects::resultLabel(
597 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
599 const std::shared_ptr<Model_Data>& aData =
600 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
601 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
604 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
605 std::shared_ptr<ModelAPI_Result> theResult,
606 const int theResultIndex)
608 theResult->setDoc(myDoc);
609 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
610 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
611 std::stringstream aNewName;
612 aNewName<<theFeatureData->name();
613 if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
614 aNewName<<"_"<<theResultIndex + 1;
615 theResult->data()->setName(aNewName.str());
619 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
620 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
622 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
623 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
624 ObjectPtr anOldObject = object(aLab);
625 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
627 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
630 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
631 storeResult(theFeatureData, aResult, theIndex);
636 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
637 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
639 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
640 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
641 ObjectPtr anOldObject = object(aLab);
642 std::shared_ptr<ModelAPI_ResultBody> aResult;
644 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
647 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
648 storeResult(theFeatureData, aResult, theIndex);
653 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
654 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
656 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
657 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
658 ObjectPtr anOldObject = object(aLab);
659 std::shared_ptr<ModelAPI_ResultPart> aResult;
661 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
664 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
665 storeResult(theFeatureData, aResult, theIndex);
670 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
671 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
673 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
674 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
675 ObjectPtr anOldObject = object(aLab);
676 std::shared_ptr<ModelAPI_ResultGroup> aResult;
678 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
681 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
682 storeResult(theFeatureData, aResult, theIndex);
687 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
688 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
690 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
691 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
692 ObjectPtr anOldObject = object(aLab);
693 std::shared_ptr<ModelAPI_ResultParameter> aResult;
695 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
698 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
699 storeResult(theFeatureData, aResult, theIndex);
704 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
705 const std::shared_ptr<ModelAPI_Result>& theResult)
707 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
709 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
710 return feature(aFeatureLab);
715 void Model_Objects::updateResults(FeaturePtr theFeature)
717 // for not persistent is will be done by parametric updater automatically
718 //if (!theFeature->isPersistentResult()) return;
719 // check the existing results and remove them if there is nothing on the label
720 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
721 while(aResIter != theFeature->results().cend()) {
722 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
724 if (!aBody->data()->isValid()) {
725 // found a disappeared result => remove it
726 theFeature->removeResult(aBody);
727 // start iterate from beginning because iterator is corrupted by removing
728 aResIter = theFeature->results().cbegin();
735 if (!theFeature->data() || !theFeature->data()->isValid())
737 // check that results are presented on all labels
738 int aResSize = theFeature->results().size();
739 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
740 for(; aLabIter.More(); aLabIter.Next()) {
741 // here must be GUID of the feature
742 int aResIndex = aLabIter.Value().Tag() - 1;
744 if (aResSize <= aResIndex) {
745 TDF_Label anArgLab = aLabIter.Value();
746 Handle(TDataStd_Comment) aGroup;
747 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
748 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
749 aNewBody = createBody(theFeature->data(), aResIndex);
750 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
751 aNewBody = createPart(theFeature->data(), aResIndex);
752 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
753 theFeature->execute(); // construction shapes are needed for sketch solver
755 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
756 aNewBody = createGroup(theFeature->data(), aResIndex);
757 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
758 theFeature->attributeChanged("expression"); // just produce a value
761 Events_Error::send(std::string("Unknown type of result is found in the document:") +
762 TCollection_AsciiString(aGroup->Get()).ToCString());
766 theFeature->setResult(aNewBody, aResIndex);
772 ResultPtr Model_Objects::findByName(const std::string theName)
774 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
775 for(; anObjIter.More(); anObjIter.Next()) {
776 FeaturePtr& aFeature = anObjIter.ChangeValue();
777 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
779 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
780 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
781 for (; aRIter != aResults.cend(); aRIter++) {
782 ResultPtr aRes = *aRIter;
783 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
784 aRes->data()->name() == theName) {
793 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
795 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
797 TDF_Label aFeatureLabel = aData->label().Father();
798 Handle(TDataStd_ReferenceArray) aRefs;
799 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
800 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
801 if (aRefs->Value(a).IsEqual(aFeatureLabel)) {
802 a += theReverse ? -1 : 1;
803 if (a >= aRefs->Lower() && a <= aRefs->Upper())
804 return feature(aRefs->Value(a));
805 break; // finish iiteration: it's last feature
810 return FeaturePtr(); // not found, last, or something is wrong
813 FeaturePtr Model_Objects::firstFeature()
815 Handle(TDataStd_ReferenceArray) aRefs;
816 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
817 return feature(aRefs->Value(aRefs->Lower()));
819 return FeaturePtr(); // no features at all
822 FeaturePtr Model_Objects::lastFeature()
824 Handle(TDataStd_ReferenceArray) aRefs;
825 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
826 return feature(aRefs->Value(aRefs->Upper()));
828 return FeaturePtr(); // no features at all
831 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
833 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
836 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
838 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);