1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Document.cxx
4 // Created: 28 Feb 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_Document.h>
8 #include <Model_Data.h>
9 #include <Model_Application.h>
10 #include <Model_Session.h>
11 #include <Model_Events.h>
12 #include <Model_ResultPart.h>
13 #include <Model_ResultConstruction.h>
14 #include <Model_ResultBody.h>
15 #include <Model_ResultGroup.h>
16 #include <ModelAPI_Validator.h>
17 #include <Events_Loop.h>
18 #include <Events_Error.h>
20 #include <TDataStd_Integer.hxx>
21 #include <TDataStd_Comment.hxx>
22 #include <TDF_ChildIDIterator.hxx>
23 #include <TDataStd_ReferenceArray.hxx>
24 #include <TDataStd_HLabelArray1.hxx>
25 #include <TDataStd_Name.hxx>
26 #include <TDF_Reference.hxx>
27 #include <TDF_ChildIDIterator.hxx>
28 #include <TDF_LabelMapHasher.hxx>
36 # define _separator_ '\\'
38 # define _separator_ '/'
41 static const int UNDO_LIMIT = 10; // number of possible undo operations
43 static const int TAG_GENERAL = 1; // general properties tag
44 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
45 static const int TAG_HISTORY = 3; // tag of the history sub-tree (python dump)
48 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
49 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
51 Model_Document::Model_Document(const std::string theID, const std::string theKind)
52 : myID(theID), myKind(theKind),
53 myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
55 myDoc->SetUndoLimit(UNDO_LIMIT);
56 myTransactionsCounter = 0;
57 myTransactionSave = 0;
59 myExecuteFeatures = true;
60 // to have something in the document and avoid empty doc open/save problem
61 // in transaction for nesting correct working
63 TDataStd_Integer::Set(myDoc->Main().Father(), 0);
64 myDoc->CommitCommand();
67 /// Returns the file name of this document by the nameof directory and identifuer of a document
68 static TCollection_ExtendedString DocFileName(const char* theFileName, const std::string& theID)
70 TCollection_ExtendedString aPath((const Standard_CString) theFileName);
71 // remove end-separators
72 while(aPath.Length() && (aPath.Value(aPath.Length()) == '\\' || aPath.Value(aPath.Length()) == '/'))
73 aPath.Remove(aPath.Length());
75 aPath += theID.c_str();
76 aPath += ".cbf"; // standard binary file extension
80 bool Model_Document::load(const char* theFileName)
82 Handle(Model_Application) anApp = Model_Application::getApplication();
83 if (this == Model_Session::get()->moduleDocument().get()) {
84 anApp->setLoadPath(theFileName);
86 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
87 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
89 aStatus = anApp->Open(aPath, myDoc);
90 } catch (Standard_Failure) {
91 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
93 std::string("Exception in opening of document: ") + aFail->GetMessageString());
96 bool isError = aStatus != PCDM_RS_OK;
99 case PCDM_RS_UnknownDocument:
100 Events_Error::send(std::string("Can not open document: unknown format"));
102 case PCDM_RS_AlreadyRetrieved:
103 Events_Error::send(std::string("Can not open document: already opened"));
105 case PCDM_RS_AlreadyRetrievedAndModified:
107 std::string("Can not open document: already opened and modified"));
109 case PCDM_RS_NoDriver:
110 Events_Error::send(std::string("Can not open document: driver library is not found"));
112 case PCDM_RS_UnknownFileDriver:
113 Events_Error::send(std::string("Can not open document: unknown driver for opening"));
115 case PCDM_RS_OpenError:
116 Events_Error::send(std::string("Can not open document: file open error"));
118 case PCDM_RS_NoVersion:
119 Events_Error::send(std::string("Can not open document: invalid version"));
121 case PCDM_RS_NoModel:
122 Events_Error::send(std::string("Can not open document: no data model"));
124 case PCDM_RS_NoDocument:
125 Events_Error::send(std::string("Can not open document: no document inside"));
127 case PCDM_RS_FormatFailure:
128 Events_Error::send(std::string("Can not open document: format failure"));
130 case PCDM_RS_TypeNotFoundInSchema:
131 Events_Error::send(std::string("Can not open document: invalid object"));
133 case PCDM_RS_UnrecognizedFileFormat:
134 Events_Error::send(std::string("Can not open document: unrecognized file format"));
136 case PCDM_RS_MakeFailure:
137 Events_Error::send(std::string("Can not open document: make failure"));
139 case PCDM_RS_PermissionDenied:
140 Events_Error::send(std::string("Can not open document: permission denied"));
142 case PCDM_RS_DriverFailure:
143 Events_Error::send(std::string("Can not open document: driver failure"));
146 Events_Error::send(std::string("Can not open document: unknown error"));
151 myDoc->SetUndoLimit(UNDO_LIMIT);
152 // to avoid the problem that feature is created in the current, not this, document
153 Model_Session::get()->setActiveDocument(anApp->getDocument(myID), false);
154 synchronizeFeatures(false, true);
155 Model_Session::get()->setActiveDocument(Model_Session::get()->moduleDocument(), false);
156 Model_Session::get()->setActiveDocument(anApp->getDocument(myID), true);
161 bool Model_Document::save(const char* theFileName, std::list<std::string>& theResults)
163 // create a directory in the root document if it is not yet exist
164 if (this == Model_Session::get()->moduleDocument().get()) {
166 CreateDirectory(theFileName, NULL);
168 mkdir(theFileName, 0x1ff);
171 // filename in the dir is id of document inside of the given directory
172 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
173 PCDM_StoreStatus aStatus;
175 aStatus = Model_Application::getApplication()->SaveAs(myDoc, aPath);
176 } catch (Standard_Failure) {
177 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
179 std::string("Exception in saving of document: ") + aFail->GetMessageString());
182 bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
185 case PCDM_SS_DriverFailure:
186 Events_Error::send(std::string("Can not save document: save driver-library failure"));
188 case PCDM_SS_WriteFailure:
189 Events_Error::send(std::string("Can not save document: file writing failure"));
191 case PCDM_SS_Failure:
193 Events_Error::send(std::string("Can not save document"));
197 myTransactionSave = myTransactionsCounter;
198 if (isDone) { // save also sub-documents if any
199 theResults.push_back(TCollection_AsciiString(aPath).ToCString());
200 std::set<std::string>::iterator aSubIter = mySubs.begin();
201 for (; aSubIter != mySubs.end() && isDone; aSubIter++) {
202 isDone = subDoc(*aSubIter)->save(theFileName, theResults);
208 void Model_Document::close(const bool theForever)
210 std::shared_ptr<ModelAPI_Session> aPM = Model_Session::get();
211 if (this != aPM->moduleDocument().get() && this == aPM->activeDocument().get()) {
212 aPM->setActiveDocument(aPM->moduleDocument());
215 std::set<std::string>::iterator aSubIter = mySubs.begin();
216 for (; aSubIter != mySubs.end(); aSubIter++)
217 subDoc(*aSubIter)->close(theForever);
220 // close for thid document needs no transaction in this document
221 std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(false);
223 // delete all features of this document
224 std::shared_ptr<ModelAPI_Document> aThis =
225 Model_Application::getApplication()->getDocument(myID);
226 Events_Loop* aLoop = Events_Loop::loop();
227 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myObjs);
228 for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
229 FeaturePtr aFeature = aFeaturesIter.Value();
230 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
231 ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
232 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
233 aFeature->eraseResults();
237 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
238 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
240 // close all only if it is really asked, otherwise it can be undoed/redoed
242 if (myDoc->CanClose() == CDM_CCS_OK)
246 std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(true);
249 void Model_Document::startOperation()
251 if (myDoc->HasOpenCommand()) { // start of nested command
252 if (myNestedNum == -1) {
254 myDoc->InitDeltaCompaction();
256 myIsEmptyTr[myTransactionsCounter] = !myDoc->CommitCommand();
257 myTransactionsCounter++;
258 myDoc->OpenCommand();
259 } else { // start the simple command
262 // new command for all subs
263 std::set<std::string>::iterator aSubIter = mySubs.begin();
264 for (; aSubIter != mySubs.end(); aSubIter++)
265 subDoc(*aSubIter)->startOperation();
268 bool Model_Document::compactNested()
270 bool allWasEmpty = true;
271 while (myNestedNum != -1) {
272 myTransactionsCounter--;
273 if (!myIsEmptyTr[myTransactionsCounter]) {
276 myIsEmptyTr.erase(myTransactionsCounter);
279 myIsEmptyTr[myTransactionsCounter] = allWasEmpty;
280 myTransactionsCounter++;
282 // Issue 151: if everything is empty, it is a problem for OCCT to work with it,
283 // just commit the empty that returns nothing
284 myDoc->CommitCommand();
286 myDoc->PerformDeltaCompaction();
291 void Model_Document::finishOperation()
293 // just to be sure that everybody knows that changes were performed
294 if (!myDoc->HasOpenCommand() && myNestedNum != -1)
295 std::static_pointer_cast<Model_Session>(Model_Session::get())
296 ->setCheckTransactions(false); // for nested transaction commit
297 synchronizeBackRefs();
298 Events_Loop* aLoop = Events_Loop::loop();
299 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
300 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
301 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
302 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
303 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
304 // this must be here just after everything is finished but before real transaction stop
305 // to avoid messages about modifications outside of the transaction
306 // and to rebuild everything after all updates and creates
307 if (Model_Session::get()->moduleDocument().get() == this) { // once for root document
308 Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
309 static std::shared_ptr<Events_Message> aFinishMsg
310 (new Events_Message(Events_Loop::eventByName("FinishOperation")));
311 Events_Loop::loop()->send(aFinishMsg);
312 Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), false);
314 // to avoid "updated" message appearance by updater
315 //aLoop->clear(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
317 if (!myDoc->HasOpenCommand() && myNestedNum != -1)
318 std::static_pointer_cast<Model_Session>(Model_Session::get())
319 ->setCheckTransactions(true); // for nested transaction commit
321 // finish for all subs first: to avoid nested finishing and "isOperation" calls problems inside
322 std::set<std::string>::iterator aSubIter = mySubs.begin();
323 for (; aSubIter != mySubs.end(); aSubIter++)
324 subDoc(*aSubIter)->finishOperation();
326 if (myNestedNum != -1) // this nested transaction is owervritten
328 if (!myDoc->HasOpenCommand()) {
329 if (myNestedNum != -1) {
334 // returns false if delta is empty and no transaction was made
335 myIsEmptyTr[myTransactionsCounter] = !myDoc->CommitCommand(); // && (myNestedNum == -1);
336 myTransactionsCounter++;
340 void Model_Document::abortOperation()
342 if (myNestedNum > 0 && !myDoc->HasOpenCommand()) { // abort all what was done in nested
343 // first compact all nested
344 if (compactNested()) {
345 myDoc->Undo(); // undo only compacted, if not: do not undo the empty transaction
348 myTransactionsCounter--;
349 myIsEmptyTr.erase(myTransactionsCounter);
351 if (myNestedNum == 0) // abort only high-level
353 myDoc->AbortCommand();
355 synchronizeFeatures(true, false); // references were not changed since transaction start
356 // abort for all subs
357 std::set<std::string>::iterator aSubIter = mySubs.begin();
358 for (; aSubIter != mySubs.end(); aSubIter++)
359 subDoc(*aSubIter)->abortOperation();
362 bool Model_Document::isOperation()
364 // operation is opened for all documents: no need to check subs
365 return myDoc->HasOpenCommand() == Standard_True ;
368 bool Model_Document::isModified()
370 // is modified if at least one operation was commited and not undoed
371 return myTransactionsCounter != myTransactionSave || isOperation();
374 bool Model_Document::canUndo()
376 if (myDoc->GetAvailableUndos() > 0 && myNestedNum != 0
377 && myTransactionsCounter != 0 /* for omitting the first useless transaction */)
379 // check other subs contains operation that can be undoed
380 std::set<std::string>::iterator aSubIter = mySubs.begin();
381 for (; aSubIter != mySubs.end(); aSubIter++)
382 if (subDoc(*aSubIter)->canUndo())
387 void Model_Document::undo()
389 myTransactionsCounter--;
392 if (!myIsEmptyTr[myTransactionsCounter])
394 synchronizeFeatures(true, true);
396 std::set<std::string>::iterator aSubIter = mySubs.begin();
397 for (; aSubIter != mySubs.end(); aSubIter++)
398 subDoc(*aSubIter)->undo();
401 bool Model_Document::canRedo()
403 if (myDoc->GetAvailableRedos() > 0)
405 // check other subs contains operation that can be redoed
406 std::set<std::string>::iterator aSubIter = mySubs.begin();
407 for (; aSubIter != mySubs.end(); aSubIter++)
408 if (subDoc(*aSubIter)->canRedo())
413 void Model_Document::redo()
415 if (myNestedNum != -1)
417 if (!myIsEmptyTr[myTransactionsCounter])
419 myTransactionsCounter++;
420 synchronizeFeatures(true, true);
422 std::set<std::string>::iterator aSubIter = mySubs.begin();
423 for (; aSubIter != mySubs.end(); aSubIter++)
424 subDoc(*aSubIter)->redo();
427 /// Appenad to the array of references a new referenced label
428 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced)
430 Handle(TDataStd_ReferenceArray) aRefs;
431 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
432 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
433 aRefs->SetValue(0, theReferenced);
434 } else { // extend array by one more element
435 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
437 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
438 aNewArray->SetValue(a, aRefs->Value(a));
440 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
441 aRefs->SetInternalArray(aNewArray);
445 FeaturePtr Model_Document::addFeature(std::string theID)
447 TDF_Label anEmptyLab;
448 FeaturePtr anEmptyFeature;
449 FeaturePtr aFeature = ModelAPI_Session::get()->createFeature(theID);
452 std::shared_ptr<Model_Document> aDocToAdd = std::dynamic_pointer_cast<Model_Document>(
453 aFeature->documentToAdd());
455 TDF_Label aFeatureLab;
456 if (!aFeature->isAction()) { // do not add action to the data model
457 TDF_Label aFeaturesLab = aDocToAdd->featuresLabel();
458 aFeatureLab = aFeaturesLab.NewChild();
459 aDocToAdd->initData(aFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
460 // keep the feature ID to restore document later correctly
461 TDataStd_Comment::Set(aFeatureLab, aFeature->getKind().c_str());
462 aDocToAdd->myObjs.Bind(aFeatureLab, aFeature);
463 // store feature in the history of features array
464 if (aFeature->isInHistory()) {
465 AddToRefArray(aFeaturesLab, aFeatureLab);
468 if (!aFeature->isAction()) { // do not add action to the data model
469 // event: feature is added
470 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
471 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
472 } else { // feature must be executed
473 // no creation event => updater not working, problem with remove part
480 /// Appenad to the array of references a new referenced label.
481 /// If theIndex is not -1, removes element at thisindex, not theReferenced.
482 /// \returns the index of removed element
483 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced, const int theIndex =
486 int aResult = -1; // no returned
487 Handle(TDataStd_ReferenceArray) aRefs;
488 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
489 if (aRefs->Length() == 1) { // just erase an array
490 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
491 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
494 } else { // reduce the array
495 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
497 int aCount = aRefs->Lower();
498 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
499 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
503 aNewArray->SetValue(aCount, aRefs->Value(a));
506 aRefs->SetInternalArray(aNewArray);
512 void Model_Document::removeFeature(FeaturePtr theFeature, const bool theCheck)
515 // check the feature: it must have no depended objects on it
516 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
517 for(; aResIter != theFeature->results().cend(); aResIter++) {
518 std::shared_ptr<Model_Data> aData =
519 std::dynamic_pointer_cast<Model_Data>((*aResIter)->data());
520 if (aData && !aData->refsToMe().empty()) {
522 "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
528 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
530 TDF_Label aFeatureLabel = aData->label().Father();
531 if (myObjs.IsBound(aFeatureLabel))
532 myObjs.UnBind(aFeatureLabel);
534 return; // not found feature => do not remove
537 // erase all attributes under the label of feature
538 aFeatureLabel.ForgetAllAttributes();
539 // remove it from the references array
540 if (theFeature->isInHistory()) {
541 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
544 // event: feature is deleted
545 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
548 FeaturePtr Model_Document::feature(TDF_Label& theLabel)
550 if (myObjs.IsBound(theLabel))
551 return myObjs.Find(theLabel);
552 return FeaturePtr(); // not found
555 ObjectPtr Model_Document::object(TDF_Label theLabel)
557 // try feature by label
558 FeaturePtr aFeature = feature(theLabel);
560 return feature(theLabel);
561 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
562 aFeature = feature(aFeatureLabel);
564 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
565 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
566 for (; aRIter != aResults.cend(); aRIter++) {
567 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
569 if (aResData->label().Father().IsEqual(theLabel))
573 return FeaturePtr(); // not found
576 std::shared_ptr<ModelAPI_Document> Model_Document::subDocument(std::string theDocID)
578 // just store sub-document identifier here to manage it later
579 if (mySubs.find(theDocID) == mySubs.end())
580 mySubs.insert(theDocID);
581 return Model_Application::getApplication()->getDocument(theDocID);
584 std::shared_ptr<Model_Document> Model_Document::subDoc(std::string theDocID)
586 // just store sub-document identifier here to manage it later
587 if (mySubs.find(theDocID) == mySubs.end())
588 mySubs.insert(theDocID);
589 return std::dynamic_pointer_cast<Model_Document>(
590 Model_Application::getApplication()->getDocument(theDocID));
593 ObjectPtr Model_Document::object(const std::string& theGroupID, const int theIndex,
594 const bool theHidden)
596 if (theGroupID == ModelAPI_Feature::group()) {
599 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
600 for (; aLabIter.More(); aLabIter.Next()) {
601 if (theIndex == anIndex) {
602 TDF_Label aFLabel = aLabIter.Value()->Label();
603 return feature(aFLabel);
608 Handle(TDataStd_ReferenceArray) aRefs;
609 if (!featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
611 if (aRefs->Lower() > theIndex || aRefs->Upper() < theIndex)
613 TDF_Label aFeatureLabel = aRefs->Value(theIndex);
614 return feature(aFeatureLabel);
617 // comment must be in any feature: it is kind
619 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
620 for (; aLabIter.More(); aLabIter.Next()) {
621 TDF_Label aFLabel = aLabIter.Value()->Label();
622 FeaturePtr aFeature = feature(aFLabel);
623 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
624 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
625 for (; aRIter != aResults.cend(); aRIter++) {
626 if ((*aRIter)->groupName() != theGroupID) continue;
627 bool isIn = theHidden && (*aRIter)->isInHistory();
628 if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
629 isIn = !(*aRIter)->isConcealed();
632 if (anIndex == theIndex)
643 int Model_Document::size(const std::string& theGroupID, const bool theHidden)
646 if (theGroupID == ModelAPI_Feature::group()) {
648 return myObjs.Size();
650 Handle(TDataStd_ReferenceArray) aRefs;
651 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
652 return aRefs->Length();
655 // comment must be in any feature: it is kind
656 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
657 for (; aLabIter.More(); aLabIter.Next()) {
658 TDF_Label aFLabel = aLabIter.Value()->Label();
659 FeaturePtr aFeature = feature(aFLabel);
660 if (!aFeature) // may be on close
662 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
663 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
664 for (; aRIter != aResults.cend(); aRIter++) {
665 if ((*aRIter)->groupName() != theGroupID) continue;
666 bool isIn = theHidden;
667 if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
668 isIn = !(*aRIter)->isConcealed();
675 // group is not found
679 TDF_Label Model_Document::featuresLabel()
681 return myDoc->Main().FindChild(TAG_OBJECTS);
684 void Model_Document::setUniqueName(FeaturePtr theFeature)
686 if (!theFeature->data()->name().empty())
687 return; // not needed, name is already defined
688 std::string aName; // result
689 // first count all objects of such kind to start with index = count + 1
691 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
692 for (; aFIter.More(); aFIter.Next()) {
693 if (aFIter.Value()->getKind() == theFeature->getKind())
696 // generate candidate name
697 std::stringstream aNameStream;
698 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
699 aName = aNameStream.str();
700 // check this is unique, if not, increase index by 1
701 for (aFIter.Initialize(myObjs); aFIter.More();) {
702 FeaturePtr aFeature = aFIter.Value();
703 bool isSameName = aFeature->data()->name() == aName;
704 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
705 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
706 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
707 for (; aRIter != aResults.cend(); aRIter++) {
708 isSameName = (*aRIter)->data()->name() == aName;
713 std::stringstream aNameStream;
714 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
715 aName = aNameStream.str();
716 // reinitialize iterator to make sure a new name is unique
717 aFIter.Initialize(myObjs);
721 theFeature->data()->setName(aName);
724 void Model_Document::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
726 std::shared_ptr<ModelAPI_Document> aThis = Model_Application::getApplication()->getDocument(
728 std::shared_ptr<Model_Data> aData(new Model_Data);
729 aData->setLabel(theLab.FindChild(theTag));
730 aData->setObject(theObj);
731 theObj->setDoc(aThis);
732 theObj->setData(aData);
733 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
735 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
736 aFeature->initAttributes();
740 void Model_Document::synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences)
742 std::shared_ptr<ModelAPI_Document> aThis =
743 Model_Application::getApplication()->getDocument(myID);
744 // after all updates, sends a message that groups of features were created or updated
745 std::static_pointer_cast<Model_Session>(Model_Session::get())
746 ->setCheckTransactions(false);
747 Events_Loop* aLoop = Events_Loop::loop();
748 aLoop->activateFlushes(false);
750 // update all objects by checking are they of labels or not
751 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
752 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
753 for (; aLabIter.More(); aLabIter.Next()) {
754 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
756 if (!myObjs.IsBound(aFeatureLabel)) { // a new feature is inserted
758 aFeature = ModelAPI_Session::get()->createFeature(
759 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
761 if (!aFeature) { // somethig is wrong, most probably, the opened document has invalid structure
762 Events_Error::send("Invalid type of object in the document");
763 aLabIter.Value()->Label().ForgetAllAttributes();
766 // this must be before "setData" to redo the sketch line correctly
767 myObjs.Bind(aFeatureLabel, aFeature);
768 aNewFeatures.insert(aFeature);
769 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
771 // event: model is updated
772 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
773 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
774 } else { // nothing is changed, both iterators are incremented
775 aFeature = myObjs.Find(aFeatureLabel);
776 aKeptFeatures.insert(aFeature);
777 if (theMarkUpdated) {
778 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
779 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
783 // update results of thefeatures (after features created because they may be connected, like sketch and sub elements)
784 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
785 for (; aLabIter2.More(); aLabIter2.Next()) {
786 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
787 if (myObjs.IsBound(aFeatureLabel)) { // a new feature is inserted
788 FeaturePtr aFeature = myObjs.Find(aFeatureLabel);
789 updateResults(aFeature);
793 // check all features are checked: if not => it was removed
794 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
795 while (aFIter.More()) {
796 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
797 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
798 FeaturePtr aFeature = aFIter.Value();
799 // event: model is updated
800 //if (aFeature->isInHistory()) {
801 ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
803 // results of this feature must be redisplayed (hided)
804 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
805 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
806 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
807 // redisplay also removed feature (used for sketch and AISObject)
808 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
810 // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
811 TDF_Label aLab = aFIter.Key();
818 if (theUpdateReferences) {
819 synchronizeBackRefs();
822 myExecuteFeatures = false;
823 aLoop->activateFlushes(true);
825 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
826 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
827 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
828 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
829 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
830 std::static_pointer_cast<Model_Session>(Model_Session::get())
831 ->setCheckTransactions(true);
832 myExecuteFeatures = true;
835 void Model_Document::synchronizeBackRefs()
837 std::shared_ptr<ModelAPI_Document> aThis =
838 Model_Application::getApplication()->getDocument(myID);
839 // keeps the concealed flags of result to catch the change and create created/deleted events
840 std::list<std::pair<ResultPtr, bool> > aConcealed;
841 // first cycle: erase all data about back-references
842 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myObjs);
843 for(; aFeatures.More(); aFeatures.Next()) {
844 FeaturePtr aFeature = aFeatures.Value();
845 std::shared_ptr<Model_Data> aFData =
846 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
848 aFData->eraseBackReferences();
850 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
851 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
852 for (; aRIter != aResults.cend(); aRIter++) {
853 std::shared_ptr<Model_Data> aResData =
854 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
856 aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
857 aResData->eraseBackReferences();
862 // second cycle: set new back-references: only features may have reference, iterate only them
863 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
864 for(aFeatures.Initialize(myObjs); aFeatures.More(); aFeatures.Next()) {
865 FeaturePtr aFeature = aFeatures.Value();
866 std::shared_ptr<Model_Data> aFData =
867 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
869 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
870 aFData->referencesToObjects(aRefs);
871 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIter = aRefs.begin();
872 for(; aRefsIter != aRefs.end(); aRefsIter++) {
873 std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
874 for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
876 std::shared_ptr<Model_Data> aRefData =
877 std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
878 aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
884 std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
885 for(; aCIter != aConcealed.end(); aCIter++) {
886 if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
887 if (aCIter->second) { // was concealed become not => creation event
888 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
889 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
890 } else { // was not concealed become concealed => delete event
891 ModelAPI_EventCreator::get()->sendDeleted(aThis, aCIter->first->groupName());
892 // redisplay for the viewer (it must be disappeared also)
893 static Events_ID EVENT_DISP =
894 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
895 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
901 TDF_Label Model_Document::resultLabel(
902 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
904 const std::shared_ptr<Model_Data>& aData =
905 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
906 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
909 void Model_Document::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
910 std::shared_ptr<ModelAPI_Result> theResult,
911 const int theResultIndex)
913 std::shared_ptr<ModelAPI_Document> aThis =
914 Model_Application::getApplication()->getDocument(myID);
915 theResult->setDoc(aThis);
916 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
917 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
918 theResult->data()->setName(theFeatureData->name());
922 std::shared_ptr<ModelAPI_ResultConstruction> Model_Document::createConstruction(
923 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
925 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
926 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
927 ObjectPtr anOldObject = object(aLab);
928 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
930 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
933 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
934 storeResult(theFeatureData, aResult, theIndex);
939 std::shared_ptr<ModelAPI_ResultBody> Model_Document::createBody(
940 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
942 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
943 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
944 ObjectPtr anOldObject = object(aLab);
945 std::shared_ptr<ModelAPI_ResultBody> aResult;
947 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
950 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
951 storeResult(theFeatureData, aResult, theIndex);
956 std::shared_ptr<ModelAPI_ResultPart> Model_Document::createPart(
957 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
959 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
960 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
961 ObjectPtr anOldObject = object(aLab);
962 std::shared_ptr<ModelAPI_ResultPart> aResult;
964 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
967 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
968 storeResult(theFeatureData, aResult, theIndex);
973 std::shared_ptr<ModelAPI_ResultGroup> Model_Document::createGroup(
974 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
976 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
977 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
978 ObjectPtr anOldObject = object(aLab);
979 std::shared_ptr<ModelAPI_ResultGroup> aResult;
981 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
984 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
985 storeResult(theFeatureData, aResult, theIndex);
990 std::shared_ptr<ModelAPI_Feature> Model_Document::feature(
991 const std::shared_ptr<ModelAPI_Result>& theResult)
993 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
995 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
996 return feature(aFeatureLab);
1001 void Model_Document::updateResults(FeaturePtr theFeature)
1003 // for not persistent is will be done by parametric updater automatically
1004 //if (!theFeature->isPersistentResult()) return;
1005 // check the existing results and remove them if there is nothing on the label
1006 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1007 while(aResIter != theFeature->results().cend()) {
1008 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1010 if (!aBody->data()->isValid()) {
1011 // found a disappeared result => remove it
1012 theFeature->removeResult(aBody);
1013 // start iterate from beginning because iterator is corrupted by removing
1014 aResIter = theFeature->results().cbegin();
1020 // it may be on undo
1021 if (!theFeature->data() || !theFeature->data()->isValid())
1023 // check that results are presented on all labels
1024 int aResSize = theFeature->results().size();
1025 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1026 for(; aLabIter.More(); aLabIter.Next()) {
1027 // here must be GUID of the feature
1028 int aResIndex = aLabIter.Value().Tag() - 1;
1030 if (aResSize <= aResIndex) {
1031 TDF_Label anArgLab = aLabIter.Value();
1032 Handle(TDataStd_Comment) aGroup;
1033 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1034 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1035 aNewBody = createBody(theFeature->data(), aResIndex);
1036 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1037 aNewBody = createPart(theFeature->data(), aResIndex);
1038 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1039 theFeature->execute(); // construction shapes are needed for sketch solver
1041 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1042 aNewBody = createGroup(theFeature->data(), aResIndex);
1044 Events_Error::send(std::string("Unknown type of result is found in the document:") +
1045 TCollection_AsciiString(aGroup->Get()).ToCString());
1049 theFeature->setResult(aNewBody, aResIndex);
1055 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1057 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1060 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1062 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);