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 <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>
32 #include <OSD_File.hxx>
33 #include <OSD_Path.hxx>
41 # define _separator_ '\\'
43 # define _separator_ '/'
46 static const int UNDO_LIMIT = 1000; // number of possible undo operations (big for sketcher)
48 static const int TAG_GENERAL = 1; // general properties tag
49 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
50 static const int TAG_HISTORY = 3; // tag of the history sub-tree (python dump)
53 static const int TAG_CURRENT_FEATURE = 1; ///< where the reference to the current feature label is located (or no attribute if null feature)
56 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
57 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
60 /// 0:1:2 - where features are located
61 /// 0:1:2:N:1 - data of the feature N
62 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
64 Model_Document::Model_Document(const std::string theID, const std::string theKind)
65 : myID(theID), myKind(theKind),
66 myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
68 myDoc->SetUndoLimit(UNDO_LIMIT);
69 myTransactionSave = 0;
70 myExecuteFeatures = true;
71 // to have something in the document and avoid empty doc open/save problem
72 // in transaction for nesting correct working
74 TDataStd_Integer::Set(myDoc->Main().Father(), 0);
75 myDoc->CommitCommand();
78 /// Returns the file name of this document by the nameof directory and identifuer of a document
79 static TCollection_ExtendedString DocFileName(const char* theFileName, const std::string& theID)
81 TCollection_ExtendedString aPath((const Standard_CString) theFileName);
82 // remove end-separators
83 while(aPath.Length() &&
84 (aPath.Value(aPath.Length()) == '\\' || aPath.Value(aPath.Length()) == '/'))
85 aPath.Remove(aPath.Length());
87 aPath += theID.c_str();
88 aPath += ".cbf"; // standard binary file extension
92 bool Model_Document::isRoot() const
94 return this == Model_Session::get()->moduleDocument().get();
97 bool Model_Document::load(const char* theFileName)
99 Handle(Model_Application) anApp = Model_Application::getApplication();
101 anApp->setLoadPath(theFileName);
103 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
104 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
106 aStatus = anApp->Open(aPath, myDoc);
107 } catch (Standard_Failure) {
108 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
110 std::string("Exception in opening of document: ") + aFail->GetMessageString());
113 bool isError = aStatus != PCDM_RS_OK;
116 case PCDM_RS_UnknownDocument:
117 Events_Error::send(std::string("Can not open document"));
119 case PCDM_RS_AlreadyRetrieved:
120 Events_Error::send(std::string("Can not open document: already opened"));
122 case PCDM_RS_AlreadyRetrievedAndModified:
124 std::string("Can not open document: already opened and modified"));
126 case PCDM_RS_NoDriver:
127 Events_Error::send(std::string("Can not open document: driver library is not found"));
129 case PCDM_RS_UnknownFileDriver:
130 Events_Error::send(std::string("Can not open document: unknown driver for opening"));
132 case PCDM_RS_OpenError:
133 Events_Error::send(std::string("Can not open document: file open error"));
135 case PCDM_RS_NoVersion:
136 Events_Error::send(std::string("Can not open document: invalid version"));
138 case PCDM_RS_NoModel:
139 Events_Error::send(std::string("Can not open document: no data model"));
141 case PCDM_RS_NoDocument:
142 Events_Error::send(std::string("Can not open document: no document inside"));
144 case PCDM_RS_FormatFailure:
145 Events_Error::send(std::string("Can not open document: format failure"));
147 case PCDM_RS_TypeNotFoundInSchema:
148 Events_Error::send(std::string("Can not open document: invalid object"));
150 case PCDM_RS_UnrecognizedFileFormat:
151 Events_Error::send(std::string("Can not open document: unrecognized file format"));
153 case PCDM_RS_MakeFailure:
154 Events_Error::send(std::string("Can not open document: make failure"));
156 case PCDM_RS_PermissionDenied:
157 Events_Error::send(std::string("Can not open document: permission denied"));
159 case PCDM_RS_DriverFailure:
160 Events_Error::send(std::string("Can not open document: driver failure"));
163 Events_Error::send(std::string("Can not open document: unknown error"));
168 myDoc->SetUndoLimit(UNDO_LIMIT);
169 // to avoid the problem that feature is created in the current, not this, document
170 std::shared_ptr<Model_Session> aSession =
171 std::dynamic_pointer_cast<Model_Session>(Model_Session::get());
172 aSession->setActiveDocument(anApp->getDocument(myID), false);
173 aSession->setCheckTransactions(false);
174 synchronizeFeatures(false, true, true);
175 aSession->setCheckTransactions(true);
176 aSession->setActiveDocument(Model_Session::get()->moduleDocument(), false);
177 aSession->setActiveDocument(anApp->getDocument(myID), true);
182 bool Model_Document::save(const char* theFileName, std::list<std::string>& theResults)
184 // create a directory in the root document if it is not yet exist
185 Handle(Model_Application) anApp = Model_Application::getApplication();
188 CreateDirectory(theFileName, NULL);
190 mkdir(theFileName, 0x1ff);
193 // filename in the dir is id of document inside of the given directory
194 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
195 PCDM_StoreStatus aStatus;
197 aStatus = anApp->SaveAs(myDoc, aPath);
198 } catch (Standard_Failure) {
199 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
201 std::string("Exception in saving of document: ") + aFail->GetMessageString());
204 bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
207 case PCDM_SS_DriverFailure:
208 Events_Error::send(std::string("Can not save document: save driver-library failure"));
210 case PCDM_SS_WriteFailure:
211 Events_Error::send(std::string("Can not save document: file writing failure"));
213 case PCDM_SS_Failure:
215 Events_Error::send(std::string("Can not save document"));
219 myTransactionSave = myTransactions.size();
220 if (isDone) { // save also sub-documents if any
221 theResults.push_back(TCollection_AsciiString(aPath).ToCString());
222 const std::set<std::string> aSubs = subDocuments(false);
223 std::set<std::string>::iterator aSubIter = aSubs.begin();
224 for (; aSubIter != aSubs.end() && isDone; aSubIter++) {
225 if (anApp->isLoadByDemand(*aSubIter)) {
226 // copy not-activated document that is not in the memory
227 std::string aDocName = *aSubIter;
228 if (!aDocName.empty()) {
230 TCollection_AsciiString aSubPath(DocFileName(anApp->loadPath().c_str(), aDocName));
231 OSD_Path aPath(aSubPath);
232 OSD_File aFile(aPath);
233 if (aFile.Exists()) {
234 TCollection_AsciiString aDestinationDir(DocFileName(theFileName, aDocName));
235 OSD_Path aDestination(aDestinationDir);
236 aFile.Copy(aDestination);
237 theResults.push_back(aDestinationDir.ToCString());
240 std::string("Can not open file ") + aSubPath.ToCString() + " for saving");
243 } else { // simply save opened document
244 isDone = subDoc(*aSubIter)->save(theFileName, theResults);
251 void Model_Document::close(const bool theForever)
253 std::shared_ptr<ModelAPI_Session> aPM = Model_Session::get();
254 if (!isRoot() && this == aPM->activeDocument().get()) {
255 aPM->setActiveDocument(aPM->moduleDocument());
256 } else if (isRoot()) {
257 // erase the active document if root is closed
258 aPM->setActiveDocument(DocumentPtr());
261 const std::set<std::string> aSubs = subDocuments(true);
262 std::set<std::string>::iterator aSubIter = aSubs.begin();
263 for (; aSubIter != aSubs.end(); aSubIter++)
264 subDoc(*aSubIter)->close(theForever);
266 // close for thid document needs no transaction in this document
267 std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(false);
269 // delete all features of this document
270 std::shared_ptr<ModelAPI_Document> aThis =
271 Model_Application::getApplication()->getDocument(myID);
272 Events_Loop* aLoop = Events_Loop::loop();
273 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myObjs);
274 for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
275 FeaturePtr aFeature = aFeaturesIter.Value();
276 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
277 ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
278 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
279 aFeature->eraseResults();
280 if (theForever) { // issue #294: do not delete content of the document until it can be redone
283 aFeature->data()->execState(ModelAPI_StateMustBeUpdated);
289 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
290 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
292 // close all only if it is really asked, otherwise it can be undoed/redoed
294 if (myDoc->CanClose() == CDM_CCS_OK)
298 std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(true);
301 void Model_Document::startOperation()
303 if (myDoc->HasOpenCommand()) { // start of nested command
304 if (myDoc->CommitCommand()) { // commit the current: it will contain all nested after compactification
305 myTransactions.rbegin()->myOCAFNum++; // if has open command, the list is not empty
307 myNestedNum.push_back(0); // start of nested operation with zero transactions inside yet
308 myDoc->OpenCommand();
309 } else { // start the simple command
312 // starts a new operation
313 myTransactions.push_back(Transaction());
314 if (!myNestedNum.empty())
315 (*myNestedNum.rbegin())++;
317 // new command for all subs
318 const std::set<std::string> aSubs = subDocuments(true);
319 std::set<std::string>::iterator aSubIter = aSubs.begin();
320 for (; aSubIter != aSubs.end(); aSubIter++)
321 subDoc(*aSubIter)->startOperation();
324 void Model_Document::compactNested()
326 if (!myNestedNum.empty()) {
327 int aNumToCompact = *(myNestedNum.rbegin());
328 int aSumOfTransaction = 0;
329 for(int a = 0; a < aNumToCompact; a++) {
330 aSumOfTransaction += myTransactions.rbegin()->myOCAFNum;
331 myTransactions.pop_back();
333 // the latest transaction is the start of lower-level operation which startes the nested
334 myTransactions.rbegin()->myOCAFNum += aSumOfTransaction;
335 myNestedNum.pop_back();
339 bool Model_Document::finishOperation()
341 bool isNestedClosed = !myDoc->HasOpenCommand() && !myNestedNum.empty();
342 static std::shared_ptr<Model_Session> aSession =
343 std::static_pointer_cast<Model_Session>(Model_Session::get());
344 synchronizeBackRefs();
345 Events_Loop* aLoop = Events_Loop::loop();
346 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
347 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
348 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
349 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
350 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
351 // this must be here just after everything is finished but before real transaction stop
352 // to avoid messages about modifications outside of the transaction
353 // and to rebuild everything after all updates and creates
354 if (isRoot()) { // once for root document
355 Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
356 static std::shared_ptr<Events_Message> aFinishMsg
357 (new Events_Message(Events_Loop::eventByName("FinishOperation")));
358 Events_Loop::loop()->send(aFinishMsg);
359 Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), false);
361 // to avoid "updated" message appearance by updater
362 //aLoop->clear(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
364 // finish for all subs first: to avoid nested finishing and "isOperation" calls problems inside
365 bool aResult = false;
366 const std::set<std::string> aSubs = subDocuments(true);
367 std::set<std::string>::iterator aSubIter = aSubs.begin();
368 for (; aSubIter != aSubs.end(); aSubIter++)
369 if (subDoc(*aSubIter)->finishOperation())
372 // transaction may be empty if this document was created during this transaction (create part)
373 if (!myTransactions.empty() && myDoc->CommitCommand()) { // if commit is successfull, just increment counters
374 myTransactions.rbegin()->myOCAFNum++;
378 if (isNestedClosed) {
381 if (!aResult && !myTransactions.empty() /* it can be for just created part document */)
382 aResult = myTransactions.rbegin()->myOCAFNum != 0;
384 if (!aResult && isRoot()) {
385 // nothing inside in all documents, so remove this transaction from the transactions list
386 undoInternal(true, false);
388 // on finish clear redos in any case (issue 446) and for all subs (issue 408)
391 for (aSubIter = aSubs.begin(); aSubIter != aSubs.end(); aSubIter++) {
392 subDoc(*aSubIter)->myDoc->ClearRedos();
393 subDoc(*aSubIter)->myRedos.clear();
399 void Model_Document::abortOperation()
401 if (!myNestedNum.empty() && !myDoc->HasOpenCommand()) { // abort all what was done in nested
403 undoInternal(false, false);
406 } else { // abort the current
407 int aNumTransactions = myTransactions.rbegin()->myOCAFNum;
408 myTransactions.pop_back();
409 if (!myNestedNum.empty())
410 (*myNestedNum.rbegin())--;
411 // roll back the needed number of transactions
412 myDoc->AbortCommand();
413 for(int a = 0; a < aNumTransactions; a++)
417 // abort for all subs, flushes will be later, in the end of root abort
418 const std::set<std::string> aSubs = subDocuments(true);
419 std::set<std::string>::iterator aSubIter = aSubs.begin();
420 for (; aSubIter != aSubs.end(); aSubIter++)
421 subDoc(*aSubIter)->abortOperation();
422 // references may be changed because they are set in attributes on the fly
423 synchronizeFeatures(true, true, isRoot());
426 bool Model_Document::isOperation() const
428 // operation is opened for all documents: no need to check subs
429 return myDoc->HasOpenCommand() == Standard_True ;
432 bool Model_Document::isModified()
434 // is modified if at least one operation was commited and not undoed
435 return myTransactions.size() != myTransactionSave || isOperation();
438 bool Model_Document::canUndo()
440 // issue 406 : if transaction is opened, but nothing to undo behind, can not undo
441 int aCurrentNum = isOperation() ? 1 : 0;
442 if (myDoc->GetAvailableUndos() > 0 &&
443 (myNestedNum.empty() || *myNestedNum.rbegin() - aCurrentNum > 0) && // there is something to undo in nested
444 myTransactions.size() - aCurrentNum > 0 /* for omitting the first useless transaction */)
446 // check other subs contains operation that can be undoed
447 const std::set<std::string> aSubs = subDocuments(true);
448 std::set<std::string>::iterator aSubIter = aSubs.begin();
449 for (; aSubIter != aSubs.end(); aSubIter++)
450 if (subDoc(*aSubIter)->canUndo())
455 void Model_Document::undoInternal(const bool theWithSubs, const bool theSynchronize)
457 int aNumTransactions = myTransactions.rbegin()->myOCAFNum;
458 myRedos.push_back(*myTransactions.rbegin());
459 myTransactions.pop_back();
460 if (!myNestedNum.empty())
461 (*myNestedNum.rbegin())--;
462 // roll back the needed number of transactions
463 for(int a = 0; a < aNumTransactions; a++)
468 const std::set<std::string> aSubs = subDocuments(true);
469 std::set<std::string>::iterator aSubIter = aSubs.begin();
470 for (; aSubIter != aSubs.end(); aSubIter++)
471 subDoc(*aSubIter)->undoInternal(theWithSubs, theSynchronize);
473 // after redo of all sub-documents to avoid updates on not-modified data (issue 370)
475 synchronizeFeatures(true, true, isRoot());
478 void Model_Document::undo()
480 undoInternal(true, true);
483 bool Model_Document::canRedo()
485 if (!myRedos.empty())
487 // check other subs contains operation that can be redoed
488 const std::set<std::string> aSubs = subDocuments(true);
489 std::set<std::string>::iterator aSubIter = aSubs.begin();
490 for (; aSubIter != aSubs.end(); aSubIter++)
491 if (subDoc(*aSubIter)->canRedo())
496 void Model_Document::redo()
498 if (!myNestedNum.empty())
499 (*myNestedNum.rbegin())++;
500 int aNumRedos = myRedos.rbegin()->myOCAFNum;
501 myTransactions.push_back(*myRedos.rbegin());
503 for(int a = 0; a < aNumRedos; a++)
507 const std::set<std::string> aSubs = subDocuments(true);
508 std::set<std::string>::iterator aSubIter = aSubs.begin();
509 for (; aSubIter != aSubs.end(); aSubIter++)
510 subDoc(*aSubIter)->redo();
512 // after redo of all sub-documents to avoid updates on not-modified data (issue 370)
513 synchronizeFeatures(true, true, isRoot());
516 std::list<std::string> Model_Document::undoList() const
518 std::list<std::string> aResult;
519 // the number of skipped current operations (on undo they will be aborted)
520 int aSkipCurrent = isOperation() ? 1 : 0;
521 std::list<Transaction>::const_reverse_iterator aTrIter = myTransactions.crbegin();
522 int aNumUndo = myTransactions.size();
523 if (!myNestedNum.empty())
524 aNumUndo = *myNestedNum.rbegin();
525 for( ; aNumUndo > 0; aTrIter++, aNumUndo--) {
526 if (aSkipCurrent == 0) aResult.push_back(aTrIter->myId);
532 std::list<std::string> Model_Document::redoList() const
534 std::list<std::string> aResult;
535 std::list<Transaction>::const_reverse_iterator aTrIter = myRedos.crbegin();
536 for( ; aTrIter != myRedos.crend(); aTrIter++) {
537 aResult.push_back(aTrIter->myId);
542 void Model_Document::operationId(const std::string& theId)
544 myTransactions.rbegin()->myId = theId;
547 /// Append to the array of references a new referenced label
548 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced)
550 Handle(TDataStd_ReferenceArray) aRefs;
551 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
552 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
553 aRefs->SetValue(0, theReferenced);
554 } else { // extend array by one more element
555 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
557 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
558 aNewArray->SetValue(a, aRefs->Value(a));
560 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
561 aRefs->SetInternalArray(aNewArray);
565 FeaturePtr Model_Document::addFeature(std::string theID)
567 TDF_Label anEmptyLab;
568 FeaturePtr anEmptyFeature;
569 std::shared_ptr<Model_Session> aSession =
570 std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get());
571 FeaturePtr aFeature = aSession->createFeature(theID, this);
574 Model_Document* aDocToAdd;
575 if (!aFeature->documentToAdd().empty()) { // use the customized document to add
576 if (aFeature->documentToAdd() != kind()) { // the root document by default
577 aDocToAdd = std::dynamic_pointer_cast<Model_Document>(aSession->moduleDocument()).get();
581 } else { // if customized is not presented, add to "this" document
585 TDF_Label aFeatureLab;
586 if (!aFeature->isAction()) { // do not add action to the data model
587 TDF_Label aFeaturesLab = aDocToAdd->featuresLabel();
588 aFeatureLab = aFeaturesLab.NewChild();
589 aDocToAdd->initData(aFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
590 // keep the feature ID to restore document later correctly
591 TDataStd_Comment::Set(aFeatureLab, aFeature->getKind().c_str());
592 aDocToAdd->myObjs.Bind(aFeatureLab, aFeature);
593 // store feature in the history of features array
594 if (aFeature->isInHistory()) {
595 AddToRefArray(aFeaturesLab, aFeatureLab);
598 if (!aFeature->isAction()) { // do not add action to the data model
599 // event: feature is added
600 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
601 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
602 aFeature->setDisabled(false); // by default created feature is enabled
603 setCurrentFeature(aFeature); // after all this feature stays in the document, so make it current
604 } else { // feature must be executed
605 // no creation event => updater not working, problem with remove part
612 /// Appenad to the array of references a new referenced label.
613 /// If theIndex is not -1, removes element at this index, not theReferenced.
614 /// \returns the index of removed element
615 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
616 const int theIndex = -1)
618 int aResult = -1; // no returned
619 Handle(TDataStd_ReferenceArray) aRefs;
620 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
621 if (aRefs->Length() == 1) { // just erase an array
622 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
623 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
626 } else { // reduce the array
627 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
629 int aCount = aRefs->Lower();
630 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
631 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
635 aNewArray->SetValue(aCount, aRefs->Value(a));
638 aRefs->SetInternalArray(aNewArray);
644 void Model_Document::refsToFeature(FeaturePtr theFeature,
645 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs,
646 const bool isSendError)
648 // check the feature: it must have no depended objects on it
649 // the dependencies can be in the feature results
650 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
651 for(; aResIter != theFeature->results().cend(); aResIter++) {
652 ResultPtr aResult = (*aResIter);
653 std::shared_ptr<Model_Data> aData =
654 std::dynamic_pointer_cast<Model_Data>(aResult->data());
655 if (aData.get() != NULL) {
656 const std::set<AttributePtr>& aRefs = aData->refsToMe();
657 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
658 for(; aRefIt != aRefLast; aRefIt++) {
659 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
660 if (aFeature.get() != NULL)
661 theRefs.insert(aFeature);
665 // the dependencies can be in the feature itself
666 std::shared_ptr<Model_Data> aData =
667 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
668 if (aData && !aData->refsToMe().empty()) {
669 const std::set<AttributePtr>& aRefs = aData->refsToMe();
670 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
671 for(; aRefIt != aRefLast; aRefIt++) {
672 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
673 if (aFeature.get() != NULL)
674 theRefs.insert(aFeature);
678 if (!theRefs.empty() && isSendError) {
680 "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
684 void Model_Document::removeFeature(FeaturePtr theFeature/*, const bool theCheck*/)
686 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
688 TDF_Label aFeatureLabel = aData->label().Father();
689 if (myObjs.IsBound(aFeatureLabel))
690 myObjs.UnBind(aFeatureLabel);
692 return; // not found feature => do not remove
694 // checking that the sub-element of composite feature is removed: if yes, inform the owner
695 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
696 refsToFeature(theFeature, aRefs, false);
697 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
698 for(; aRefIter != aRefs.end(); aRefIter++) {
699 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
700 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
701 if (aComposite.get()) {
702 aComposite->removeFeature(theFeature);
705 // if this feature is current, make the current the previous feature
706 if (theFeature == currentFeature()) {
707 int aCurrentIndex = index(theFeature);
708 if (aCurrentIndex != -1) {
709 setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(
710 object(ModelAPI_Feature::group(), aCurrentIndex - 1)));
716 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
717 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
718 // erase all attributes under the label of feature
719 aFeatureLabel.ForgetAllAttributes();
720 // remove it from the references array
721 if (theFeature->isInHistory()) {
722 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
724 // event: feature is deleted
725 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
726 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
727 Events_Loop::loop()->flush(EVENT_DISP);
731 void Model_Document::addToHistory(const std::shared_ptr<ModelAPI_Object> theObject)
733 TDF_Label aFeaturesLab = featuresLabel();
734 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theObject->data());
736 return; // not found feature => do not remove
738 TDF_Label aFeatureLabel = aData->label().Father();
739 // store feature in the history of features array
740 if (theObject->isInHistory()) {
741 AddToRefArray(aFeaturesLab, aFeatureLabel);
743 RemoveFromRefArray(aFeaturesLab, aFeatureLabel);
747 FeaturePtr Model_Document::feature(TDF_Label& theLabel) const
749 if (myObjs.IsBound(theLabel))
750 return myObjs.Find(theLabel);
751 return FeaturePtr(); // not found
754 ObjectPtr Model_Document::object(TDF_Label theLabel)
756 // try feature by label
757 FeaturePtr aFeature = feature(theLabel);
759 return feature(theLabel);
760 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
761 aFeature = feature(aFeatureLabel);
763 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
764 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
765 for (; aRIter != aResults.cend(); aRIter++) {
766 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
768 if (aResData->label().Father().IsEqual(theLabel))
772 return FeaturePtr(); // not found
775 std::shared_ptr<ModelAPI_Document> Model_Document::subDocument(std::string theDocID)
777 return Model_Application::getApplication()->getDocument(theDocID);
780 const std::set<std::string> Model_Document::subDocuments(const bool theActivatedOnly) const
782 std::set<std::string> aResult;
783 // comment must be in any feature: it is kind
785 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
786 for (; aLabIter.More(); aLabIter.Next()) {
787 TDF_Label aFLabel = aLabIter.Value()->Label();
788 FeaturePtr aFeature = feature(aFLabel);
789 if (aFeature.get()) { // if document is closed the feature may be not in myObjs map
790 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
791 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
792 for (; aRIter != aResults.cend(); aRIter++) {
793 if ((*aRIter)->groupName() != ModelAPI_ResultPart::group()) continue;
794 if ((*aRIter)->isInHistory()) {
795 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRIter);
796 if (aPart && (!theActivatedOnly || aPart->isActivated()))
797 aResult.insert(aPart->data()->name());
805 std::shared_ptr<Model_Document> Model_Document::subDoc(std::string theDocID)
807 // just store sub-document identifier here to manage it later
808 return std::dynamic_pointer_cast<Model_Document>(
809 Model_Application::getApplication()->getDocument(theDocID));
812 ObjectPtr Model_Document::object(const std::string& theGroupID, const int theIndex,
813 const bool theHidden)
815 if (theGroupID == ModelAPI_Feature::group()) {
818 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
819 for (; aLabIter.More(); aLabIter.Next()) {
820 if (theIndex == anIndex) {
821 TDF_Label aFLabel = aLabIter.Value()->Label();
822 return feature(aFLabel);
827 Handle(TDataStd_ReferenceArray) aRefs;
828 if (!featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
830 if (aRefs->Lower() > theIndex || aRefs->Upper() < theIndex)
832 TDF_Label aFeatureLabel = aRefs->Value(theIndex);
833 return feature(aFeatureLabel);
836 // comment must be in any feature: it is kind
838 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
839 for (; aLabIter.More(); aLabIter.Next()) {
840 TDF_Label aFLabel = aLabIter.Value()->Label();
841 FeaturePtr aFeature = feature(aFLabel);
842 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
843 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
844 for (; aRIter != aResults.cend(); aRIter++) {
845 if ((*aRIter)->groupName() != theGroupID) continue;
846 bool isIn = theHidden && (*aRIter)->isInHistory();
847 if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
848 isIn = !(*aRIter)->isConcealed();
851 if (anIndex == theIndex)
862 std::shared_ptr<ModelAPI_Object> Model_Document::objectByName(
863 const std::string& theGroupID, const std::string& theName)
865 if (theGroupID == ModelAPI_Feature::group()) {
867 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
868 for (; aLabIter.More(); aLabIter.Next()) {
869 TDF_Label aFLabel = aLabIter.Value()->Label();
870 FeaturePtr aFeature = feature(aFLabel);
871 if (aFeature && aFeature->name() == theName)
875 // comment must be in any feature: it is kind
877 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
878 for (; aLabIter.More(); aLabIter.Next()) {
879 TDF_Label aFLabel = aLabIter.Value()->Label();
880 FeaturePtr aFeature = feature(aFLabel);
881 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
882 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
883 for (; aRIter != aResults.cend(); aRIter++) {
884 if ((*aRIter)->groupName() == theGroupID && (*aRIter)->data()->name() == theName)
893 const int Model_Document::index(std::shared_ptr<ModelAPI_Object> theObject)
895 const std::string aGroupName = theObject->groupName();
896 if (aGroupName == ModelAPI_Feature::group()) {
898 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
899 for (; aLabIter.More(); aLabIter.Next()) {
900 TDF_Label aFLabel = aLabIter.Value()->Label();
901 FeaturePtr aFeature = feature(aFLabel);
902 if (aFeature == theObject)
904 if (aFeature->isInHistory())
908 // comment must be in any feature: it is kind
910 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
911 for (; aLabIter.More(); aLabIter.Next()) {
912 TDF_Label aFLabel = aLabIter.Value()->Label();
913 FeaturePtr aFeature = feature(aFLabel);
914 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
915 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
916 for (; aRIter != aResults.cend(); aRIter++) {
917 if (*aRIter == theObject)
919 if ((*aRIter)->groupName() == aGroupName) {
920 bool isIn = (*aRIter)->isInHistory() && !(*aRIter)->isConcealed();
932 int Model_Document::size(const std::string& theGroupID, const bool theHidden)
935 if (theGroupID == ModelAPI_Feature::group()) {
937 return myObjs.Size();
939 Handle(TDataStd_ReferenceArray) aRefs;
940 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
941 return aRefs->Length();
944 // comment must be in any feature: it is kind
945 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
946 for (; aLabIter.More(); aLabIter.Next()) {
947 TDF_Label aFLabel = aLabIter.Value()->Label();
948 FeaturePtr aFeature = feature(aFLabel);
949 if (!aFeature) // may be on close
951 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
952 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
953 for (; aRIter != aResults.cend(); aRIter++) {
954 if ((*aRIter)->groupName() != theGroupID) continue;
955 bool isIn = theHidden;
956 if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
957 isIn = !(*aRIter)->isConcealed();
964 // group is not found
968 std::shared_ptr<ModelAPI_Feature> Model_Document::currentFeature()
970 TDF_Label aRefLab = generalLabel().FindChild(TAG_CURRENT_FEATURE);
971 Handle(TDF_Reference) aRef;
972 if (aRefLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
973 TDF_Label aLab = aRef->Get();
974 return feature(aLab);
976 return std::shared_ptr<ModelAPI_Feature>(); // null feature means the higher than first
979 void Model_Document::setCurrentFeature(std::shared_ptr<ModelAPI_Feature> theCurrent)
981 TDF_Label aRefLab = generalLabel().FindChild(TAG_CURRENT_FEATURE);
982 if (theCurrent.get()) {
983 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
985 TDF_Label aFeatureLabel = aData->label().Father();
986 Handle(TDF_Reference) aRef;
987 if (aRefLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
988 aRef->Set(aFeatureLabel);
990 aRef = TDF_Reference::Set(aRefLab, aFeatureLabel);
993 } else { // remove reference for the null feature
994 aRefLab.ForgetAttribute(TDF_Reference::GetID());
996 // make all features after this feature disabled in reversed order (to remove results without deps)
997 bool aPassed = false; // flag that the current object is already passed in cycle
998 int aSize = size(ModelAPI_Feature::group(), true);
999 for(int a = aSize - 1; a >= 0; a--) {
1000 FeaturePtr aFeature =
1001 std::dynamic_pointer_cast<ModelAPI_Feature>(object(ModelAPI_Feature::group(), a, true));
1003 // check this before passed become enabled: the current feature is enabled!
1004 if (aFeature == theCurrent) aPassed = true;
1006 if (aFeature->setDisabled(!aPassed)) {
1007 // state of feature is changed => so feature become updated
1008 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1009 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
1015 TDF_Label Model_Document::featuresLabel() const
1017 return myDoc->Main().FindChild(TAG_OBJECTS);
1020 TDF_Label Model_Document::generalLabel() const
1022 return myDoc->Main().FindChild(TAG_GENERAL);
1025 void Model_Document::setUniqueName(FeaturePtr theFeature)
1027 if (!theFeature->data()->name().empty())
1028 return; // not needed, name is already defined
1029 std::string aName; // result
1030 // first count all objects of such kind to start with index = count + 1
1031 int aNumObjects = 0;
1032 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
1033 for (; aFIter.More(); aFIter.Next()) {
1034 if (aFIter.Value()->getKind() == theFeature->getKind())
1037 // generate candidate name
1038 std::stringstream aNameStream;
1039 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
1040 aName = aNameStream.str();
1041 // check this is unique, if not, increase index by 1
1042 for (aFIter.Initialize(myObjs); aFIter.More();) {
1043 FeaturePtr aFeature = aFIter.Value();
1044 bool isSameName = aFeature->data()->name() == aName;
1045 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
1046 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
1047 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
1048 for (; aRIter != aResults.cend(); aRIter++) {
1049 isSameName = (*aRIter)->data()->name() == aName;
1054 std::stringstream aNameStream;
1055 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
1056 aName = aNameStream.str();
1057 // reinitialize iterator to make sure a new name is unique
1058 aFIter.Initialize(myObjs);
1062 theFeature->data()->setName(aName);
1065 void Model_Document::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
1067 std::shared_ptr<ModelAPI_Document> aThis = Model_Application::getApplication()->getDocument(
1069 std::shared_ptr<Model_Data> aData(new Model_Data);
1070 aData->setLabel(theLab.FindChild(theTag));
1071 aData->setObject(theObj);
1072 theObj->setDoc(aThis);
1073 theObj->setData(aData);
1074 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
1076 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
1078 theObj->initAttributes();
1081 void Model_Document::synchronizeFeatures(
1082 const bool theMarkUpdated, const bool theUpdateReferences, const bool theFlush)
1084 std::shared_ptr<ModelAPI_Document> aThis =
1085 Model_Application::getApplication()->getDocument(myID);
1086 // after all updates, sends a message that groups of features were created or updated
1087 Events_Loop* aLoop = Events_Loop::loop();
1088 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1089 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1090 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1091 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1092 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
1093 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1094 aLoop->activateFlushes(false);
1096 // update all objects by checking are they on labels or not
1097 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
1098 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
1099 for (; aLabIter.More(); aLabIter.Next()) {
1100 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
1101 FeaturePtr aFeature;
1102 if (!myObjs.IsBound(aFeatureLabel)) { // a new feature is inserted
1104 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
1105 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
1106 .ToCString(), this);
1107 if (!aFeature) { // somethig is wrong, most probably, the opened document has invalid structure
1108 Events_Error::send("Invalid type of object in the document");
1109 aLabIter.Value()->Label().ForgetAllAttributes();
1112 // this must be before "setData" to redo the sketch line correctly
1113 myObjs.Bind(aFeatureLabel, aFeature);
1114 aNewFeatures.insert(aFeature);
1115 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
1117 // event: model is updated
1118 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
1119 } else { // nothing is changed, both iterators are incremented
1120 aFeature = myObjs.Find(aFeatureLabel);
1121 aKeptFeatures.insert(aFeature);
1122 if (theMarkUpdated) {
1123 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
1127 // update results of the features (after features created because they may be connected, like sketch and sub elements)
1128 std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
1129 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
1130 for (; aLabIter2.More(); aLabIter2.Next()) {
1131 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
1132 if (myObjs.IsBound(aFeatureLabel)) { // a new feature is inserted
1133 FeaturePtr aFeature = myObjs.Find(aFeatureLabel);
1134 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
1135 aComposites.push_back(aFeature);
1136 updateResults(aFeature);
1139 std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
1140 for(; aComposite != aComposites.end(); aComposite++) {
1141 updateResults(*aComposite);
1144 // check all features are checked: if not => it was removed
1145 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
1146 while (aFIter.More()) {
1147 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
1148 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
1149 FeaturePtr aFeature = aFIter.Value();
1150 // event: model is updated
1151 //if (aFeature->isInHistory()) {
1152 ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
1154 // results of this feature must be redisplayed (hided)
1155 // redisplay also removed feature (used for sketch and AISObject)
1156 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
1158 // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
1159 myObjs.UnBind(aFIter.Key());
1160 // reinitialize iterator because unbind may corrupt the previous order in the map
1161 aFIter.Initialize(myObjs);
1166 if (theUpdateReferences) {
1167 synchronizeBackRefs();
1170 myExecuteFeatures = false;
1171 aLoop->activateFlushes(true);
1174 aLoop->flush(aCreateEvent);
1175 aLoop->flush(aDeleteEvent);
1176 aLoop->flush(anUpdateEvent);
1177 aLoop->flush(aRedispEvent);
1178 aLoop->flush(aToHideEvent);
1180 myExecuteFeatures = true;
1183 void Model_Document::synchronizeBackRefs()
1185 std::shared_ptr<ModelAPI_Document> aThis =
1186 Model_Application::getApplication()->getDocument(myID);
1187 // keeps the concealed flags of result to catch the change and create created/deleted events
1188 std::list<std::pair<ResultPtr, bool> > aConcealed;
1189 // first cycle: erase all data about back-references
1190 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myObjs);
1191 for(; aFeatures.More(); aFeatures.Next()) {
1192 FeaturePtr aFeature = aFeatures.Value();
1193 std::shared_ptr<Model_Data> aFData =
1194 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
1196 aFData->eraseBackReferences();
1198 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
1199 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
1200 for (; aRIter != aResults.cend(); aRIter++) {
1201 std::shared_ptr<Model_Data> aResData =
1202 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
1204 aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
1205 aResData->eraseBackReferences();
1210 // second cycle: set new back-references: only features may have reference, iterate only them
1211 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
1212 for(aFeatures.Initialize(myObjs); aFeatures.More(); aFeatures.Next()) {
1213 FeaturePtr aFeature = aFeatures.Value();
1214 std::shared_ptr<Model_Data> aFData =
1215 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
1217 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1218 aFData->referencesToObjects(aRefs);
1219 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
1220 aRefsIter = aRefs.begin();
1221 for(; aRefsIter != aRefs.end(); aRefsIter++) {
1222 std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
1223 for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
1225 std::shared_ptr<Model_Data> aRefData =
1226 std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
1227 aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
1233 std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
1234 for(; aCIter != aConcealed.end(); aCIter++) {
1235 if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
1236 if (aCIter->second) { // was concealed become not => creation event
1237 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1238 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
1239 } else { // was not concealed become concealed => delete event
1240 ModelAPI_EventCreator::get()->sendDeleted(aThis, aCIter->first->groupName());
1241 // redisplay for the viewer (it must be disappeared also)
1242 static Events_ID EVENT_DISP =
1243 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1244 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
1250 TDF_Label Model_Document::resultLabel(
1251 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1253 const std::shared_ptr<Model_Data>& aData =
1254 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1255 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1258 void Model_Document::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1259 std::shared_ptr<ModelAPI_Result> theResult,
1260 const int theResultIndex)
1262 std::shared_ptr<ModelAPI_Document> aThis =
1263 Model_Application::getApplication()->getDocument(myID);
1264 theResult->setDoc(aThis);
1265 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1266 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
1267 std::stringstream aNewName;
1268 aNewName<<theFeatureData->name();
1269 if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
1270 aNewName<<"_"<<theResultIndex + 1;
1271 theResult->data()->setName(aNewName.str());
1275 std::shared_ptr<ModelAPI_ResultConstruction> Model_Document::createConstruction(
1276 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1278 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1279 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1280 ObjectPtr anOldObject = object(aLab);
1281 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1283 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1286 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1287 storeResult(theFeatureData, aResult, theIndex);
1292 std::shared_ptr<ModelAPI_ResultBody> Model_Document::createBody(
1293 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1295 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1296 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1297 ObjectPtr anOldObject = object(aLab);
1298 std::shared_ptr<ModelAPI_ResultBody> aResult;
1300 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1303 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1304 storeResult(theFeatureData, aResult, theIndex);
1309 std::shared_ptr<ModelAPI_ResultPart> Model_Document::createPart(
1310 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1312 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1313 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1314 ObjectPtr anOldObject = object(aLab);
1315 std::shared_ptr<ModelAPI_ResultPart> aResult;
1317 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1320 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1321 storeResult(theFeatureData, aResult, theIndex);
1326 std::shared_ptr<ModelAPI_ResultGroup> Model_Document::createGroup(
1327 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1329 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1330 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1331 ObjectPtr anOldObject = object(aLab);
1332 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1334 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1337 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1338 storeResult(theFeatureData, aResult, theIndex);
1343 std::shared_ptr<ModelAPI_ResultParameter> Model_Document::createParameter(
1344 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1346 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1347 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1348 ObjectPtr anOldObject = object(aLab);
1349 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1351 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1354 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1355 storeResult(theFeatureData, aResult, theIndex);
1360 std::shared_ptr<ModelAPI_Feature> Model_Document::feature(
1361 const std::shared_ptr<ModelAPI_Result>& theResult)
1363 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1365 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1366 return feature(aFeatureLab);
1368 return FeaturePtr();
1371 void Model_Document::updateResults(FeaturePtr theFeature)
1373 // for not persistent is will be done by parametric updater automatically
1374 //if (!theFeature->isPersistentResult()) return;
1375 // check the existing results and remove them if there is nothing on the label
1376 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1377 while(aResIter != theFeature->results().cend()) {
1378 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1380 if (!aBody->data()->isValid()) {
1381 // found a disappeared result => remove it
1382 theFeature->removeResult(aBody);
1383 // start iterate from beginning because iterator is corrupted by removing
1384 aResIter = theFeature->results().cbegin();
1390 // it may be on undo
1391 if (!theFeature->data() || !theFeature->data()->isValid())
1393 // check that results are presented on all labels
1394 int aResSize = theFeature->results().size();
1395 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1396 for(; aLabIter.More(); aLabIter.Next()) {
1397 // here must be GUID of the feature
1398 int aResIndex = aLabIter.Value().Tag() - 1;
1400 if (aResSize <= aResIndex) {
1401 TDF_Label anArgLab = aLabIter.Value();
1402 Handle(TDataStd_Comment) aGroup;
1403 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1404 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1405 aNewBody = createBody(theFeature->data(), aResIndex);
1406 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1407 aNewBody = createPart(theFeature->data(), aResIndex);
1408 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1409 theFeature->execute(); // construction shapes are needed for sketch solver
1411 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1412 aNewBody = createGroup(theFeature->data(), aResIndex);
1413 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1414 theFeature->attributeChanged("expression"); // just produce a value
1417 Events_Error::send(std::string("Unknown type of result is found in the document:") +
1418 TCollection_AsciiString(aGroup->Get()).ToCString());
1422 theFeature->setResult(aNewBody, aResIndex);
1428 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1430 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1433 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1435 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);
1438 void Model_Document::addNamingName(const TDF_Label theLabel, std::string theName)
1440 myNamingNames[theName] = theLabel;
1443 TDF_Label Model_Document::findNamingName(std::string theName)
1445 std::map<std::string, TDF_Label>::iterator aFind = myNamingNames.find(theName);
1446 if (aFind == myNamingNames.end())
1447 return TDF_Label(); // not found
1448 return aFind->second;
1451 ResultPtr Model_Document::findByName(const std::string theName)
1453 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myObjs);
1454 for(; anObjIter.More(); anObjIter.Next()) {
1455 FeaturePtr& aFeature = anObjIter.ChangeValue();
1456 if (!aFeature) // may be on close
1458 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
1459 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
1460 for (; aRIter != aResults.cend(); aRIter++) {
1461 if (aRIter->get() && (*aRIter)->data() && (*aRIter)->data()->isValid() &&
1462 (*aRIter)->data()->name() == theName) {