1 // File: Model_Document.cxx
2 // Created: 28 Feb 2014
3 // Author: Mikhail PONIKAROV
5 #include <Model_Document.h>
6 #include <Model_Data.h>
7 #include <Model_Application.h>
8 #include <Model_PluginManager.h>
9 #include <Model_Events.h>
10 #include <Model_ResultPart.h>
11 #include <Model_ResultConstruction.h>
12 #include <Model_ResultBody.h>
13 #include <Events_Loop.h>
14 #include <Events_Error.h>
16 #include <TDataStd_Integer.hxx>
17 #include <TDataStd_Comment.hxx>
18 #include <TDF_ChildIDIterator.hxx>
19 #include <TDataStd_ReferenceArray.hxx>
20 #include <TDataStd_HLabelArray1.hxx>
21 #include <TDataStd_Name.hxx>
22 #include <TDF_Reference.hxx>
30 # define _separator_ '\\'
32 # define _separator_ '/'
35 static const int UNDO_LIMIT = 10; // number of possible undo operations
37 static const int TAG_GENERAL = 1; // general properties tag
38 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
39 static const int TAG_HISTORY = 3; // tag of the history sub-tree (python dump)
42 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
43 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
45 /// Returns the file name of this document by the nameof directory and identifuer of a document
46 static TCollection_ExtendedString DocFileName(const char* theFileName, const std::string& theID)
48 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
50 aPath += theID.c_str();
51 aPath += ".cbf"; // standard binary file extension
55 bool Model_Document::load(const char* theFileName)
57 Handle(Model_Application) anApp = Model_Application::getApplication();
58 if (this == Model_PluginManager::get()->rootDocument().get()) {
59 anApp->setLoadPath(theFileName);
61 TCollection_ExtendedString aPath (DocFileName(theFileName, myID));
62 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
65 aStatus = anApp->Open(aPath, myDoc);
67 catch (Standard_Failure)
69 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
70 Events_Error::send(std::string("Exception in opening of document: ") + aFail->GetMessageString());
73 bool isError = aStatus != PCDM_RS_OK;
78 case PCDM_RS_UnknownDocument:
79 Events_Error::send(std::string("Can not open document: PCDM_RS_UnknownDocument")); break;
80 case PCDM_RS_AlreadyRetrieved:
81 Events_Error::send(std::string("Can not open document: PCDM_RS_AlreadyRetrieved")); break;
82 case PCDM_RS_AlreadyRetrievedAndModified:
84 std::string("Can not open document: PCDM_RS_AlreadyRetrievedAndModified"));
86 case PCDM_RS_NoDriver:
87 Events_Error::send(std::string("Can not open document: PCDM_RS_NoDriver")); break;
88 case PCDM_RS_UnknownFileDriver:
89 Events_Error::send(std::string("Can not open document: PCDM_RS_UnknownFileDriver")); break;
90 case PCDM_RS_OpenError:
91 Events_Error::send(std::string("Can not open document: PCDM_RS_OpenError")); break;
92 case PCDM_RS_NoVersion:
93 Events_Error::send(std::string("Can not open document: PCDM_RS_NoVersion")); break;
95 Events_Error::send(std::string("Can not open document: PCDM_RS_NoModel")); break;
96 case PCDM_RS_NoDocument:
97 Events_Error::send(std::string("Can not open document: PCDM_RS_NoDocument")); break;
98 case PCDM_RS_FormatFailure:
99 Events_Error::send(std::string("Can not open document: PCDM_RS_FormatFailure")); break;
100 case PCDM_RS_TypeNotFoundInSchema:
101 Events_Error::send(std::string("Can not open document: PCDM_RS_TypeNotFoundInSchema"));
103 case PCDM_RS_UnrecognizedFileFormat:
104 Events_Error::send(std::string("Can not open document: PCDM_RS_UnrecognizedFileFormat"));
106 case PCDM_RS_MakeFailure:
107 Events_Error::send(std::string("Can not open document: PCDM_RS_MakeFailure")); break;
108 case PCDM_RS_PermissionDenied:
109 Events_Error::send(std::string("Can not open document: PCDM_RS_PermissionDenied")); break;
110 case PCDM_RS_DriverFailure:
111 Events_Error::send(std::string("Can not open document: PCDM_RS_DriverFailure")); break;
113 Events_Error::send(std::string("Can not open document: unknown error")); break;
117 myDoc->SetUndoLimit(UNDO_LIMIT);
118 synchronizeFeatures();
123 bool Model_Document::save(const char* theFileName)
125 // create a directory in the root document if it is not yet exist
126 if (this == Model_PluginManager::get()->rootDocument().get()) {
128 CreateDirectory(theFileName, NULL);
130 mkdir(theFileName, 0x1ff);
133 // filename in the dir is id of document inside of the given directory
134 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
135 PCDM_StoreStatus aStatus;
137 aStatus = Model_Application::getApplication()->SaveAs(myDoc, aPath);
139 catch (Standard_Failure) {
140 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
141 Events_Error::send(std::string("Exception in saving of document: ") + aFail->GetMessageString());
144 bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
149 case PCDM_SS_DriverFailure:
150 Events_Error::send(std::string("Can not save document: PCDM_SS_DriverFailure"));
152 case PCDM_SS_WriteFailure:
153 Events_Error::send(std::string("Can not save document: PCDM_SS_WriteFailure"));
155 case PCDM_SS_Failure:
157 Events_Error::send(std::string("Can not save document: PCDM_SS_Failure"));
161 myTransactionsAfterSave = 0;
162 if (isDone) { // save also sub-documents if any
163 std::set<std::string>::iterator aSubIter = mySubs.begin();
164 for(; aSubIter != mySubs.end() && isDone; aSubIter++)
165 isDone = subDocument(*aSubIter)->save(theFileName);
170 void Model_Document::close()
172 boost::shared_ptr<ModelAPI_PluginManager> aPM = Model_PluginManager::get();
173 if (this != aPM->rootDocument().get() &&
174 this == aPM->currentDocument().get()) {
175 aPM->setCurrentDocument(aPM->rootDocument());
178 std::set<std::string>::iterator aSubIter = mySubs.begin();
179 for(; aSubIter != mySubs.end(); aSubIter++)
180 subDocument(*aSubIter)->close();
183 /* do not close because it can be undoed
184 if (myDoc->CanClose() == CDM_CCS_OK)
186 Model_Application::getApplication()->deleteDocument(myID);
190 void Model_Document::startOperation()
192 if (myDoc->HasOpenCommand()) { // start of nested command
193 if (myNestedNum == -1) {
195 myDoc->InitDeltaCompaction();
197 myIsEmptyTr[myTransactionsAfterSave] = false;
198 myTransactionsAfterSave++;
200 } else { // start of simple command
203 // new command for all subs
204 std::set<std::string>::iterator aSubIter = mySubs.begin();
205 for(; aSubIter != mySubs.end(); aSubIter++)
206 subDocument(*aSubIter)->startOperation();
209 void Model_Document::compactNested() {
210 while(myNestedNum != -1) {
211 myTransactionsAfterSave--;
212 myIsEmptyTr.erase(myTransactionsAfterSave);
215 myIsEmptyTr[myTransactionsAfterSave] = false;
216 myTransactionsAfterSave++;
217 myDoc->PerformDeltaCompaction();
220 void Model_Document::finishOperation()
222 // just to be sure that everybody knows that changes were performed
223 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
224 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
225 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
226 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
228 if (myNestedNum != -1) // this nested transaction is owervritten
230 if (!myDoc->HasOpenCommand()) {
231 if (myNestedNum != -1) {
236 // returns false if delta is empty and no transaction was made
237 myIsEmptyTr[myTransactionsAfterSave] = !myDoc->CommitCommand() && (myNestedNum == -1);
238 myTransactionsAfterSave++;
241 // finish for all subs
242 std::set<std::string>::iterator aSubIter = mySubs.begin();
243 for(; aSubIter != mySubs.end(); aSubIter++)
244 subDocument(*aSubIter)->finishOperation();
247 void Model_Document::abortOperation()
249 if (myNestedNum > 0 && !myDoc->HasOpenCommand()) { // abort all what was done in nested
250 // first compact all nested
252 // for nested it is undo and clear redos
255 myTransactionsAfterSave--;
256 myIsEmptyTr.erase(myTransactionsAfterSave);
258 if (myNestedNum == 0) // abort only high-level
260 myDoc->AbortCommand();
262 synchronizeFeatures(true);
263 // abort for all subs
264 std::set<std::string>::iterator aSubIter = mySubs.begin();
265 for(; aSubIter != mySubs.end(); aSubIter++)
266 subDocument(*aSubIter)->abortOperation();
269 bool Model_Document::isOperation()
271 // operation is opened for all documents: no need to check subs
272 return myDoc->HasOpenCommand() == Standard_True;
275 bool Model_Document::isModified()
277 // is modified if at least one operation was commited and not undoed
278 return myTransactionsAfterSave > 0;
281 bool Model_Document::canUndo()
283 if (myDoc->GetAvailableUndos() > 0 && myNestedNum != 0 && myTransactionsAfterSave != 0 /* for omitting the first useless transaction */)
285 // check other subs contains operation that can be undoed
286 std::set<std::string>::iterator aSubIter = mySubs.begin();
287 for(; aSubIter != mySubs.end(); aSubIter++)
288 if (subDocument(*aSubIter)->canUndo())
293 void Model_Document::undo()
295 myTransactionsAfterSave--;
296 if (myNestedNum > 0) myNestedNum--;
297 if (!myIsEmptyTr[myTransactionsAfterSave])
299 synchronizeFeatures(true);
301 std::set<std::string>::iterator aSubIter = mySubs.begin();
302 for(; aSubIter != mySubs.end(); aSubIter++)
303 subDocument(*aSubIter)->undo();
306 bool Model_Document::canRedo()
308 if (myDoc->GetAvailableRedos() > 0)
310 // check other subs contains operation that can be redoed
311 std::set<std::string>::iterator aSubIter = mySubs.begin();
312 for(; aSubIter != mySubs.end(); aSubIter++)
313 if (subDocument(*aSubIter)->canRedo())
318 void Model_Document::redo()
320 if (myNestedNum != -1) myNestedNum++;
321 if (!myIsEmptyTr[myTransactionsAfterSave])
323 myTransactionsAfterSave++;
324 synchronizeFeatures(true);
326 std::set<std::string>::iterator aSubIter = mySubs.begin();
327 for(; aSubIter != mySubs.end(); aSubIter++)
328 subDocument(*aSubIter)->redo();
331 /// Appenad to the array of references a new referenced label
332 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced) {
333 Handle(TDataStd_ReferenceArray) aRefs;
334 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
335 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
336 aRefs->SetValue(0, theReferenced);
337 } else { // extend array by one more element
338 Handle(TDataStd_HLabelArray1) aNewArray =
339 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper() + 1);
340 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
341 aNewArray->SetValue(a, aRefs->Value(a));
343 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
344 aRefs->SetInternalArray(aNewArray);
348 FeaturePtr Model_Document::addFeature(std::string theID)
350 TDF_Label anEmptyLab;
351 FeaturePtr anEmptyFeature;
352 FeaturePtr aFeature = ModelAPI_PluginManager::get()->createFeature(theID);
354 TDF_Label aFeatureLab;
355 if (!aFeature->isAction()) {// do not add action to the data model
356 TDF_Label aFeaturesLab = groupLabel(ModelAPI_Feature::group());
357 aFeatureLab = aFeaturesLab.NewChild();
358 initData(aFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
359 // keep the feature ID to restore document later correctly
360 TDataStd_Comment::Set(aFeatureLab, aFeature->getKind().c_str());
361 setUniqueName(aFeature);
362 myObjs[ModelAPI_Feature::group()].push_back(aFeature);
363 // store feature in the history of features array
364 if (aFeature->isInHistory()) {
365 AddToRefArray(aFeaturesLab, aFeatureLab);
368 if (!aFeature->isAction()) {// do not add action to the data model
369 // event: feature is added
370 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
371 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
377 /// Appenad to the array of references a new referenced label.
378 /// If theIndex is not -1, removes element at thisindex, not theReferenced.
379 /// \returns the index of removed element
380 static int RemoveFromRefArray(
381 TDF_Label theArrayLab, TDF_Label theReferenced, const int theIndex = -1) {
382 int aResult = -1; // no returned
383 Handle(TDataStd_ReferenceArray) aRefs;
384 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
385 if (aRefs->Length() == 1) { // just erase an array
386 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
387 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
390 } else { // reduce the array
391 Handle(TDataStd_HLabelArray1) aNewArray =
392 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper() - 1);
393 int aCount = aRefs->Lower();
394 for(int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
395 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
399 aNewArray->SetValue(aCount, aRefs->Value(a));
402 aRefs->SetInternalArray(aNewArray);
408 void Model_Document::removeFeature(FeaturePtr theFeature)
410 boost::shared_ptr<Model_Data> aData = boost::static_pointer_cast<Model_Data>(theFeature->data());
411 TDF_Label aFeatureLabel = aData->label().Father();
412 // remove feature from the myObjects list
413 std::vector<ObjectPtr>& aVec = myObjs[ModelAPI_Feature::group()];
414 std::vector<ObjectPtr>::iterator anIter = aVec.begin();
415 while(anIter != aVec.end()) {
416 if (*anIter == theFeature) {
417 anIter = aVec.erase(anIter);
422 // erase all attributes under the label of feature
423 aFeatureLabel.ForgetAllAttributes();
424 // remove it from the references array
425 RemoveFromRefArray(groupLabel(ModelAPI_Feature::group()), aData->label());
427 // event: feature is deleted
428 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
429 // results of this feature must be redisplayed
430 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
431 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
432 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
433 for(; aRIter != aResults.cend(); aRIter++) {
434 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
435 aRes->setData(boost::shared_ptr<ModelAPI_Data>()); // deleted flag
436 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
440 /// returns the object group name by the object label
441 static std::string groupName(TDF_Label theObjectLabel) {
442 TDF_Label aGroupLab = theObjectLabel.Father();
443 Handle(TDataStd_Comment) aComment;
444 if (aGroupLab.FindAttribute(TDataStd_Comment::GetID(), aComment))
445 return std::string(TCollection_AsciiString(aComment->Get()).ToCString());
446 return ""; // not found
449 FeaturePtr Model_Document::feature(TDF_Label& theLabel)
451 // iterate all features, may be optimized later by keeping labels-map
452 std::vector<ObjectPtr>& aVec = myObjs[ModelAPI_Feature::group()];
453 std::vector<ObjectPtr>::iterator aFIter = aVec.begin();
454 for(; aFIter != aVec.end(); aFIter++) {
455 boost::shared_ptr<Model_Data> aData =
456 boost::dynamic_pointer_cast<Model_Data>((*aFIter)->data());
457 if (aData->label().IsEqual(theLabel))
458 return boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
460 return FeaturePtr(); // not found
463 ObjectPtr Model_Document::object(TDF_Label& theLabel)
465 // iterate all features, may be optimized later by keeping labels-map
466 std::vector<ObjectPtr>& aVec = myObjs[ModelAPI_Feature::group()];
467 std::vector<ObjectPtr>::iterator aFIter = aVec.begin();
468 for(; aFIter != aVec.end(); aFIter++) {
469 boost::shared_ptr<Model_Data> aData =
470 boost::dynamic_pointer_cast<Model_Data>((*aFIter)->data());
471 if (aData->label().IsEqual(theLabel))
473 std::list<boost::shared_ptr<ModelAPI_Result> >& aResults =
474 boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter)->results();
475 std::list<boost::shared_ptr<ModelAPI_Result> >::iterator aRIter = aResults.begin();
476 for(; aRIter != aResults.end(); aRIter++) {
477 boost::shared_ptr<Model_Data> aResData =
478 boost::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
479 if (aResData->label().IsEqual(theLabel))
483 return FeaturePtr(); // not found
486 boost::shared_ptr<ModelAPI_Document> Model_Document::subDocument(std::string theDocID)
488 // just store sub-document identifier here to manage it later
489 if (mySubs.find(theDocID) == mySubs.end())
490 mySubs.insert(theDocID);
491 return Model_Application::getApplication()->getDocument(theDocID);
494 ObjectPtr Model_Document::object(const std::string& theGroupID, const int theIndex)
496 if (theGroupID == ModelAPI_Feature::group()) {
497 // features may be not in history but in the myObjs, so, iterate all
499 std::map<std::string, std::vector<ObjectPtr> >::iterator aFind =
500 myObjs.find(ModelAPI_Feature::group());
501 if (aFind != myObjs.end()) {
502 std::vector<ObjectPtr>::iterator aFIter = aFind->second.begin();
503 for(; aFIter != aFind->second.end(); aFIter++) {
504 if ((*aFIter)->isInHistory()) {
505 if (theIndex == anIndex)
512 // iterate all features in order to find the needed result
513 std::map<std::string, std::vector<ObjectPtr> >::iterator aFind =
514 myObjs.find(ModelAPI_Feature::group());
515 if (aFind != myObjs.end()) {
516 std::vector<ObjectPtr>::iterator aFIter = aFind->second.begin();
517 for(int anIndex = 0; aFIter != aFind->second.end(); aFIter++) {
518 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults =
519 boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter)->results();
520 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
521 for(; aRIter != aResults.cend(); aRIter++) {
522 if ((*aRIter)->isInHistory() && (*aRIter)->groupName() == theGroupID) {
523 if (anIndex == theIndex)
535 int Model_Document::size(const std::string& theGroupID)
538 if (theGroupID == ModelAPI_Feature::group()) {
539 // features may be not in history but in the myObjs, so, iterate all
540 std::map<std::string, std::vector<ObjectPtr> >::iterator aFind =
541 myObjs.find(ModelAPI_Feature::group());
542 if (aFind != myObjs.end()) {
543 std::vector<ObjectPtr>::iterator aFIter = aFind->second.begin();
544 for(; aFIter != aFind->second.end(); aFIter++) {
545 if ((*aFIter)->isInHistory()) {
551 // iterate all features in order to find the needed result
552 std::map<std::string, std::vector<ObjectPtr> >::iterator aFind =
553 myObjs.find(ModelAPI_Feature::group());
554 if (aFind != myObjs.end()) {
555 std::vector<ObjectPtr>::iterator aFIter = aFind->second.begin();
556 for(; aFIter != aFind->second.end(); aFIter++) {
557 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults =
558 boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter)->results();
559 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
560 for(; aRIter != aResults.cend(); aRIter++) {
561 if ((*aRIter)->isInHistory() && (*aRIter)->groupName() == theGroupID) {
568 // group is not found
572 Model_Document::Model_Document(const std::string theID)
573 : myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
575 myDoc->SetUndoLimit(UNDO_LIMIT);
576 myTransactionsAfterSave = 0;
578 //myDoc->SetNestedTransactionMode();
579 // to have something in the document and avoid empty doc open/save problem
580 // in transaction for nesting correct working
582 TDataStd_Integer::Set(myDoc->Main().Father(), 0);
583 myDoc->CommitCommand();
586 TDF_Label Model_Document::groupLabel(const std::string theGroup)
588 // searching for existing
589 TCollection_ExtendedString aGroup(theGroup.c_str());
590 TDF_ChildIDIterator aGroupIter(myDoc->Main().FindChild(TAG_OBJECTS), TDataStd_Comment::GetID());
591 for(; aGroupIter.More(); aGroupIter.Next()) {
592 Handle(TDataStd_Comment) aName = Handle(TDataStd_Comment)::DownCast(aGroupIter.Value());
593 if (aName->Get() == aGroup)
594 return aGroupIter.Value()->Label();
597 TDF_Label aNew = myDoc->Main().FindChild(TAG_OBJECTS).NewChild();
598 TDataStd_Comment::Set(aNew, aGroup);
602 void Model_Document::setUniqueName(FeaturePtr theFeature)
604 std::string aName; // result
605 // first count all objects of such kind to start with index = count + 1
606 int a, aNumObjects = 0;
607 int aSize = myObjs.find(ModelAPI_Feature::group()) == myObjs.end() ?
608 0 : myObjs[ModelAPI_Feature::group()].size();
609 for(a = 0; a < aSize; a++) {
610 if (boost::dynamic_pointer_cast<ModelAPI_Feature>(myObjs[ModelAPI_Feature::group()][a])->
611 getKind() == theFeature->getKind())
614 // generate candidate name
615 std::stringstream aNameStream;
616 aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
617 aName = aNameStream.str();
618 // check this is unique, if not, increase index by 1
619 for(a = 0; a < aSize;) {
620 FeaturePtr aFeature =
621 boost::dynamic_pointer_cast<ModelAPI_Feature>(myObjs[ModelAPI_Feature::group()][a]);
622 bool isSameName = aFeature->isInHistory() && aFeature->data()->name() == aName;
623 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
624 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
625 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
626 for(; aRIter != aResults.cend(); aRIter++) {
627 isSameName = (*aRIter)->isInHistory() && (*aRIter)->data()->name() == aName;
632 std::stringstream aNameStream;
633 aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
634 aName = aNameStream.str();
635 // reinitialize iterator to make sure a new name is unique
639 theFeature->data()->setName(aName);
642 void Model_Document::initData(ObjectPtr theObj, TDF_Label& theLab, const int theTag) {
643 boost::shared_ptr<ModelAPI_Document> aThis =
644 Model_Application::getApplication()->getDocument(myID);
645 boost::shared_ptr<Model_Data> aData(new Model_Data);
646 aData->setLabel(theLab.FindChild(theTag + 1));
647 aData->setObject(theObj);
648 theObj->setDoc(aThis);
649 theObj->setData(aData);
650 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
651 if (aFeature) aFeature->initAttributes();
654 void Model_Document::synchronizeFeatures(const bool theMarkUpdated)
656 boost::shared_ptr<ModelAPI_Document> aThis =
657 Model_Application::getApplication()->getDocument(myID);
658 // update all objects: iterate from the end: as they appeared in the list
659 std::map<std::string, std::vector<ObjectPtr> >::reverse_iterator aGroupIter = myObjs.rbegin();
660 for(; aGroupIter != myObjs.rend(); aGroupIter++) {
661 std::vector<ObjectPtr>::iterator anObjIter = aGroupIter->second.begin();
662 // and in parallel iterate labels of features
663 const std::string& aGroupName = aGroupIter->first;
664 TDF_ChildIDIterator aLabIter(groupLabel(aGroupName), TDataStd_Comment::GetID());
665 while(anObjIter != aGroupIter->second.end() || aLabIter.More()) {
666 static const int INFINITE_TAG = INT_MAX; // no label means that it exists somwhere in infinite
667 int aFeatureTag = INFINITE_TAG;
668 if (anObjIter != aGroupIter->second.end()) { // existing tag for feature
669 boost::shared_ptr<Model_Data> aData =
670 boost::dynamic_pointer_cast<Model_Data>((*anObjIter)->data());
671 aFeatureTag = aData->label().Tag();
673 int aDSTag = INFINITE_TAG;
674 if (aLabIter.More()) { // next label in DS is existing
675 aDSTag = aLabIter.Value()->Label().Tag();
677 if (aDSTag > aFeatureTag) { // feature is removed
678 ObjectPtr anObj = *anObjIter;
679 anObjIter = aGroupIter->second.erase(anObjIter);
680 // event: model is updated
681 if (anObj->isInHistory()) {
682 ModelAPI_EventCreator::get()->sendDeleted(aThis, aGroupName);
684 ModelAPI_EventCreator::get()->sendDeleted(aThis, aGroupName);
685 // results of this feature must be redisplayed (hided)
686 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
687 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj)->results();
688 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
689 for(; aRIter != aResults.cend(); aRIter++) {
690 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
691 aRes->setData(boost::shared_ptr<ModelAPI_Data>()); // deleted flag
692 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
694 } else if (aDSTag < aFeatureTag) { // a new feature is inserted
696 TDF_Label aLab = aLabIter.Value()->Label();
697 ObjectPtr aNewObj = ModelAPI_PluginManager::get()->createFeature(
698 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
700 // this must be before "setData" to redo the sketch line correctly
701 if (anObjIter == aGroupIter->second.end()) {
702 aGroupIter->second.push_back(aNewObj);
703 anObjIter = aGroupIter->second.end();
706 aGroupIter->second.insert(anObjIter, aNewObj);
708 initData(aNewObj, aLab, TAG_FEATURE_ARGUMENTS);
710 // event: model is updated
711 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
712 ModelAPI_EventCreator::get()->sendUpdated(aNewObj, anEvent);
713 // feature for this label is added, so go to the next label
715 } else { // nothing is changed, both iterators are incremented
716 if (theMarkUpdated) {
717 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
718 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, anEvent);
725 // after all updates, sends a message that groups of features were created or updated
726 boost::static_pointer_cast<Model_PluginManager>(Model_PluginManager::get())->
727 setCheckTransactions(false);
728 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
730 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
731 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
732 boost::static_pointer_cast<Model_PluginManager>(Model_PluginManager::get())->
733 setCheckTransactions(true);
736 void Model_Document::storeResult(boost::shared_ptr<ModelAPI_Data> theFeatureData,
737 boost::shared_ptr<ModelAPI_Result> theResult, const int theResultIndex)
739 boost::shared_ptr<ModelAPI_Document> aThis =
740 Model_Application::getApplication()->getDocument(myID);
741 theResult->setDoc(aThis);
742 initData(theResult, boost::dynamic_pointer_cast<Model_Data>(theFeatureData)->
743 label().Father().FindChild(TAG_FEATURE_RESULTS), theResultIndex);
744 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
745 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
746 ModelAPI_EventCreator::get()->sendUpdated(theResult, anEvent);
747 theResult->data()->setName(theFeatureData->name());
751 boost::shared_ptr<ModelAPI_ResultConstruction> Model_Document::createConstruction(
752 const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
754 ObjectPtr anOldObject = object(boost::dynamic_pointer_cast<Model_Data>(theFeatureData)->
755 label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theIndex + 1));
756 boost::shared_ptr<ModelAPI_ResultConstruction> aResult;
758 aResult = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
761 aResult = boost::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
762 storeResult(theFeatureData, aResult);
767 boost::shared_ptr<ModelAPI_ResultBody> Model_Document::createBody(
768 const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
770 ObjectPtr anOldObject = object(boost::dynamic_pointer_cast<Model_Data>(theFeatureData)->
771 label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theIndex + 1));
772 boost::shared_ptr<ModelAPI_ResultBody> aResult;
774 aResult = boost::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
777 aResult = boost::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
778 storeResult(theFeatureData, aResult);
783 boost::shared_ptr<ModelAPI_ResultPart> Model_Document::createPart(
784 const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
786 ObjectPtr anOldObject = object(boost::dynamic_pointer_cast<Model_Data>(theFeatureData)->
787 label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theIndex + 1));
788 boost::shared_ptr<ModelAPI_ResultPart> aResult;
790 aResult = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
793 aResult = boost::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
794 storeResult(theFeatureData, aResult);
799 boost::shared_ptr<ModelAPI_Feature> Model_Document::feature(
800 const boost::shared_ptr<ModelAPI_Result>& theResult)
802 // iterate all features in order to find the needed result
803 std::map<std::string, std::vector<ObjectPtr> >::iterator aFind =
804 myObjs.find(ModelAPI_Feature::group());
805 if (aFind != myObjs.end()) {
806 std::vector<ObjectPtr>::iterator aFIter = aFind->second.begin();
807 for(; aFIter != aFind->second.end(); aFIter++) {
808 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
809 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
810 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
811 for(; aRIter != aResults.cend(); aRIter++) {
812 if (*aRIter == theResult) {