Salome HOME
423de6a37e4864e512ab4d297a942d8071d5c6af
[modules/shaper.git] / src / Model / Model_Document.cpp
1 // File:        Model_Document.cxx
2 // Created:     28 Feb 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Document.h>
6 #include <ModelAPI_Feature.h>
7 #include <Model_Data.h>
8 #include <Model_Object.h>
9 #include <Model_Application.h>
10 #include <Model_PluginManager.h>
11 #include <Model_Events.h>
12 #include <Events_Loop.h>
13 #include <Events_Error.h>
14
15 #include <TDataStd_Integer.hxx>
16 #include <TDataStd_Comment.hxx>
17 #include <TDF_ChildIDIterator.hxx>
18 #include <TDataStd_ReferenceArray.hxx>
19 #include <TDataStd_HLabelArray1.hxx>
20 #include <TDataStd_Name.hxx>
21
22 #include <climits>
23
24 #ifdef WIN32
25 # define _separator_ '\\'
26 #else
27 # define _separator_ '/'
28 #endif
29
30 static const int UNDO_LIMIT = 10; // number of possible undo operations
31
32 static const int TAG_GENERAL = 1; // general properties tag
33 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features)
34 static const int TAG_HISTORY = 3; // tag of the history sub-tree (python dump)
35
36 using namespace std;
37
38 /// Returns the file name of this document by the nameof directory and identifuer of a document
39 static TCollection_ExtendedString DocFileName(const char* theFileName, const string& theID)
40 {
41   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
42   aPath += _separator_;
43   aPath += theID.c_str();
44   aPath += ".cbf"; // standard binary file extension
45   return aPath;
46 }
47
48 bool Model_Document::load(const char* theFileName)
49 {
50   Handle(Model_Application) anApp = Model_Application::getApplication();
51   if (this == Model_PluginManager::get()->rootDocument().get()) {
52     anApp->setLoadPath(theFileName);
53   }
54   TCollection_ExtendedString aPath (DocFileName(theFileName, myID));
55   PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
56   try
57   {
58     aStatus = anApp->Open(aPath, myDoc);
59   }
60   catch (Standard_Failure)
61   {
62     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
63     Events_Error::send(string("Exception in opening of document: ") + aFail->GetMessageString());
64     return false;
65   }
66   bool isError = aStatus != PCDM_RS_OK;
67   if (isError)
68   {
69     switch (aStatus)
70     {
71     case PCDM_RS_UnknownDocument: 
72       Events_Error::send(string("Can not open document: PCDM_RS_UnknownDocument")); break;
73     case PCDM_RS_AlreadyRetrieved: 
74       Events_Error::send(string("Can not open document: PCDM_RS_AlreadyRetrieved")); break;
75     case PCDM_RS_AlreadyRetrievedAndModified:
76       Events_Error::send(string("Can not open document: PCDM_RS_AlreadyRetrievedAndModified")); break;
77     case PCDM_RS_NoDriver:
78       Events_Error::send(string("Can not open document: PCDM_RS_NoDriver")); break;
79     case PCDM_RS_UnknownFileDriver:
80       Events_Error::send(string("Can not open document: PCDM_RS_UnknownFileDriver")); break;
81     case PCDM_RS_OpenError:
82       Events_Error::send(string("Can not open document: PCDM_RS_OpenError")); break;
83     case PCDM_RS_NoVersion:
84       Events_Error::send(string("Can not open document: PCDM_RS_NoVersion")); break;
85     case PCDM_RS_NoModel:
86       Events_Error::send(string("Can not open document: PCDM_RS_NoModel")); break;
87     case PCDM_RS_NoDocument:
88       Events_Error::send(string("Can not open document: PCDM_RS_NoDocument")); break;
89     case PCDM_RS_FormatFailure:
90       Events_Error::send(string("Can not open document: PCDM_RS_FormatFailure")); break;
91     case PCDM_RS_TypeNotFoundInSchema:
92       Events_Error::send(string("Can not open document: PCDM_RS_TypeNotFoundInSchema")); break;
93     case PCDM_RS_UnrecognizedFileFormat:
94       Events_Error::send(string("Can not open document: PCDM_RS_UnrecognizedFileFormat")); break;
95     case PCDM_RS_MakeFailure:
96       Events_Error::send(string("Can not open document: PCDM_RS_MakeFailure")); break;
97     case PCDM_RS_PermissionDenied:
98       Events_Error::send(string("Can not open document: PCDM_RS_PermissionDenied")); break;
99     case PCDM_RS_DriverFailure:
100       Events_Error::send(string("Can not open document: PCDM_RS_DriverFailure")); break;
101     default:
102       Events_Error::send(string("Can not open document: unknown error")); break;
103     }
104   }
105   if (!isError) {
106     myDoc->SetUndoLimit(UNDO_LIMIT);
107     synchronizeFeatures();
108   }
109   return !isError;
110 }
111
112 bool Model_Document::save(const char* theFileName)
113 {
114   // create a directory in the root document if it is not yet exist
115   if (this == Model_PluginManager::get()->rootDocument().get()) {
116 #ifdef WIN32
117     CreateDirectory(theFileName, NULL);
118 #else
119     mkdir(theFileName, 0x1ff); 
120 #endif
121   }
122   // filename in the dir is id of document inside of the given directory
123   TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
124   PCDM_StoreStatus aStatus;
125   try {
126     aStatus = Model_Application::getApplication()->SaveAs(myDoc, aPath);
127   }
128   catch (Standard_Failure) {
129     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
130     Events_Error::send(string("Exception in saving of document: ") + aFail->GetMessageString());
131     return false;
132   }
133   bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
134   if (!isDone)
135   {
136     switch (aStatus)
137     {
138     case PCDM_SS_DriverFailure:
139       Events_Error::send(string("Can not save document: PCDM_SS_DriverFailure"));
140       break;
141     case PCDM_SS_WriteFailure:
142       Events_Error::send(string("Can not save document: PCDM_SS_WriteFailure"));
143       break;
144     case PCDM_SS_Failure:
145     default:
146       Events_Error::send(string("Can not save document: PCDM_SS_Failure"));
147       break;
148     }
149   }
150   myTransactionsAfterSave = 0;
151   if (isDone) { // save also sub-documents if any
152     set<string>::iterator aSubIter = mySubs.begin();
153     for(; aSubIter != mySubs.end() && isDone; aSubIter++)
154       isDone = subDocument(*aSubIter)->save(theFileName);
155   }
156   return isDone;
157 }
158
159 void Model_Document::close()
160 {
161   // close all subs
162   set<string>::iterator aSubIter = mySubs.begin();
163   for(; aSubIter != mySubs.end(); aSubIter++)
164     subDocument(*aSubIter)->close();
165   mySubs.clear();
166   // close this
167   if (myDoc->CanClose() == CDM_CCS_OK)
168     myDoc->Close();
169   Model_Application::getApplication()->deleteDocument(myID);
170 }
171
172 void Model_Document::startOperation()
173 {
174   // check is it nested or not
175   if (myDoc->HasOpenCommand()) {
176     myNestedStart = myTransactionsAfterSave;
177   }
178   // new command for this
179   myDoc->NewCommand();
180   // new command for all subs
181   set<string>::iterator aSubIter = mySubs.begin();
182   for(; aSubIter != mySubs.end(); aSubIter++)
183     subDocument(*aSubIter)->startOperation();
184 }
185
186 void Model_Document::finishOperation()
187 {
188   if (myNestedStart > myTransactionsAfterSave) // this nested transaction is owervritten
189     myNestedStart = 0;
190   // returns false if delta is empty and no transaction was made
191   myIsEmptyTr[myTransactionsAfterSave] = !myDoc->CommitCommand();
192   myTransactionsAfterSave++;
193   // finish for all subs
194   set<string>::iterator aSubIter = mySubs.begin();
195   for(; aSubIter != mySubs.end(); aSubIter++)
196     subDocument(*aSubIter)->finishOperation();
197 }
198
199 void Model_Document::abortOperation()
200 {
201   myDoc->AbortCommand();
202   synchronizeFeatures();
203   // abort for all subs
204   set<string>::iterator aSubIter = mySubs.begin();
205   for(; aSubIter != mySubs.end(); aSubIter++)
206     subDocument(*aSubIter)->abortOperation();
207 }
208
209 bool Model_Document::isOperation()
210 {
211   // operation is opened for all documents: no need to check subs
212   return myDoc->HasOpenCommand() == Standard_True ;
213 }
214
215 bool Model_Document::isModified()
216 {
217   // is modified if at least one operation was commited and not undoed
218   return myTransactionsAfterSave > 0;
219 }
220
221 bool Model_Document::canUndo()
222 {
223   if (myDoc->GetAvailableUndos() > 0 && myNestedStart != myTransactionsAfterSave)
224     return true;
225   // check other subs contains operation that can be undoed
226   set<string>::iterator aSubIter = mySubs.begin();
227   for(; aSubIter != mySubs.end(); aSubIter++)
228     if (subDocument(*aSubIter)->canUndo())
229       return true;
230   return false;
231 }
232
233 void Model_Document::undo()
234 {
235   myTransactionsAfterSave--;
236   if (!myIsEmptyTr[myTransactionsAfterSave])
237     myDoc->Undo();
238   synchronizeFeatures();
239   // undo for all subs
240   set<string>::iterator aSubIter = mySubs.begin();
241   for(; aSubIter != mySubs.end(); aSubIter++)
242     subDocument(*aSubIter)->undo();
243 }
244
245 bool Model_Document::canRedo()
246 {
247   if (myDoc->GetAvailableRedos() > 0)
248     return true;
249   // check other subs contains operation that can be redoed
250   set<string>::iterator aSubIter = mySubs.begin();
251   for(; aSubIter != mySubs.end(); aSubIter++)
252     if (subDocument(*aSubIter)->canRedo())
253       return true;
254   return false;
255 }
256
257 void Model_Document::redo()
258 {
259   if (!myIsEmptyTr[myTransactionsAfterSave])
260     myDoc->Redo();
261   myTransactionsAfterSave++;
262   synchronizeFeatures();
263   // redo for all subs
264   set<string>::iterator aSubIter = mySubs.begin();
265   for(; aSubIter != mySubs.end(); aSubIter++)
266     subDocument(*aSubIter)->redo();
267 }
268
269 boost::shared_ptr<ModelAPI_Feature> Model_Document::addFeature(string theID)
270 {
271   boost::shared_ptr<ModelAPI_Feature> aFeature = 
272     ModelAPI_PluginManager::get()->createFeature(theID);
273   if (aFeature) {
274     boost::dynamic_pointer_cast<Model_Document>(aFeature->documentToAdd())->addFeature(aFeature);
275   } else {
276     // TODO: generate error that feature is not created
277   }
278   return aFeature;
279 }
280
281 /// Appenad to the array of references a new referenced label
282 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced) {
283   Handle(TDataStd_ReferenceArray) aRefs;
284   if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
285     aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
286     aRefs->SetValue(0, theReferenced);
287   } else { // extend array by one more element
288     Handle(TDataStd_HLabelArray1) aNewArray = 
289       new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper() + 1);
290     for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
291       aNewArray->SetValue(a, aRefs->Value(a));
292     }
293     aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
294     aRefs->SetInternalArray(aNewArray);
295   }
296 }
297
298 void Model_Document::addFeature(const boost::shared_ptr<ModelAPI_Feature> theFeature)
299 {
300   if (theFeature->isAction()) return; // do not add action to the data model
301
302   boost::shared_ptr<ModelAPI_Document> aThis = 
303     Model_Application::getApplication()->getDocument(myID);
304   TDF_Label aFeaturesLab = groupLabel(FEATURES_GROUP);
305   TDF_Label aFeatureLab = aFeaturesLab.NewChild();
306
307   // organize feature and data objects
308   boost::shared_ptr<Model_Data> aData(new Model_Data);
309   aData->setFeature(theFeature);
310   aData->setLabel(aFeatureLab);
311   theFeature->setDoc(aThis);
312   theFeature->setData(aData);
313   setUniqueName(theFeature);
314   theFeature->initAttributes();
315
316   // keep the feature ID to restore document later correctly
317   TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
318   myFeatures.push_back(theFeature);
319   // store feature in the history of features array
320   if (theFeature->isInHistory()) {
321     AddToRefArray(aFeaturesLab, aFeatureLab);
322   }
323   // add featue to the group
324   const std::string& aGroup = theFeature->getGroup();
325   TDF_Label aGroupLab = groupLabel(aGroup);
326   AddToRefArray(aGroupLab, aFeatureLab);
327   // new name of this feature object by default equal to name of feature
328   TDF_Label anObjLab = aGroupLab.NewChild();
329   TCollection_ExtendedString aName(theFeature->data()->getName().c_str());
330   TDataStd_Name::Set(anObjLab, aName);
331   AddToRefArray(aGroupLab.FindChild(1), anObjLab); // reference to names is on the first sub
332
333   // event: feature is added
334   static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_CREATED);
335   Model_FeatureUpdatedMessage aMsg(theFeature, anEvent);
336   Events_Loop::loop()->send(aMsg);
337 }
338
339 /// Appenad to the array of references a new referenced label.
340 /// If theIndex is not -1, removes element at thisindex, not theReferenced.
341 /// \returns the index of removed element
342 static int RemoveFromRefArray(
343   TDF_Label theArrayLab, TDF_Label theReferenced, const int theIndex = -1) {
344   int aResult = -1; // no returned
345   Handle(TDataStd_ReferenceArray) aRefs;
346   if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
347     if (aRefs->Length() == 1) { // just erase an array
348       if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0)
349         theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
350       aResult = 0;
351     } else { // reduce the array
352       Handle(TDataStd_HLabelArray1) aNewArray = 
353         new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper() - 1);
354       int aCount = aRefs->Lower();
355       for(int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
356         if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
357           aCount--;
358           aResult = a;
359         } else {
360           aNewArray->SetValue(aCount, aRefs->Value(a));
361         }
362       }
363     aRefs->SetInternalArray(aNewArray);
364     }
365   }
366   return aResult;
367 }
368
369 void Model_Document::removeFeature(boost::shared_ptr<ModelAPI_Feature> theFeature)
370 {
371   boost::shared_ptr<Model_Data> aData = boost::static_pointer_cast<Model_Data>(theFeature->data());
372   TDF_Label aFeatureLabel = aData->label();
373   // remove the object
374   TDF_Label aGroupLabel = groupLabel(theFeature->getGroup());
375   int aRemovedIndex = RemoveFromRefArray(aGroupLabel, aFeatureLabel);
376   RemoveFromRefArray(aGroupLabel.FindChild(1), TDF_Label(), aRemovedIndex);
377   // remove feature from the myFeatures list
378   std::vector<boost::shared_ptr<ModelAPI_Feature> >::iterator aFIter = myFeatures.begin();
379   while(aFIter != myFeatures.end()) {
380     if (*aFIter == theFeature) {
381       aFIter = myFeatures.erase(aFIter);
382     } else {
383       aFIter++;
384     }
385   }
386   // erase all attributes under the label of feature
387   aFeatureLabel.ForgetAllAttributes();
388   // remove it from the references array
389   RemoveFromRefArray(groupLabel(FEATURES_GROUP), aData->label());
390
391   // event: feature is added
392   Model_FeatureDeletedMessage aMsg(theFeature->document(), theFeature->getGroup());
393   Events_Loop::loop()->send(aMsg);
394 }
395
396 boost::shared_ptr<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
397 {
398   // iterate all features, may be optimized later by keeping labels-map
399   vector<boost::shared_ptr<ModelAPI_Feature> >::iterator aFIter = myFeatures.begin();
400   for(; aFIter != myFeatures.end(); aFIter++) {
401     boost::shared_ptr<Model_Data> aData = 
402       boost::dynamic_pointer_cast<Model_Data>((*aFIter)->data());
403     if (aData->label().IsEqual(theLabel))
404       return *aFIter;
405   }
406   return boost::shared_ptr<ModelAPI_Feature>(); // not found
407 }
408
409 boost::shared_ptr<ModelAPI_Document> Model_Document::subDocument(string theDocID)
410 {
411   // just store sub-document identifier here to manage it later
412   if (mySubs.find(theDocID) == mySubs.end())
413     mySubs.insert(theDocID);
414   return Model_Application::getApplication()->getDocument(theDocID);
415 }
416
417 boost::shared_ptr<ModelAPI_Feature> Model_Document::feature(
418   const string& theGroupID, const int theIndex, const bool isOperation)
419 {
420   TDF_Label aGroupLab = groupLabel(theGroupID);
421   Handle(TDataStd_ReferenceArray) aRefs;
422   if (aGroupLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
423     if (aRefs->Lower() <= theIndex && aRefs->Upper() >= theIndex) {
424       TDF_Label aFeatureLab = aRefs->Value(theIndex);
425       boost::shared_ptr<ModelAPI_Feature> aFeature = feature(aFeatureLab);
426
427       if (theGroupID == FEATURES_GROUP || isOperation) { // just returns the feature from the history
428         return aFeature;
429       } else { // create a new object from the group to return it
430         Handle(TDataStd_Name) aName; // name of the object
431         if (aGroupLab.FindChild(1).FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
432           aRefs->Value(theIndex).FindAttribute(TDataStd_Name::GetID(), aName);
433         boost::shared_ptr<Model_Object> anObj(new Model_Object(aFeature, aName));
434         return anObj;
435       }
436     }
437   }
438
439   // not found
440   return boost::shared_ptr<ModelAPI_Feature>();
441 }
442
443 int Model_Document::size(const string& theGroupID) 
444 {
445   Handle(TDataStd_ReferenceArray) aRefs;
446   if (groupLabel(theGroupID).FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
447     return aRefs->Length();
448   // group is not found
449   return 0;
450 }
451
452 Model_Document::Model_Document(const std::string theID)
453     : myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
454 {
455   myDoc->SetUndoLimit(UNDO_LIMIT);
456   myTransactionsAfterSave = 0;
457   myNestedStart = 0;
458   myDoc->SetNestedTransactionMode();
459   // to have something in the document and avoid empty doc open/save problem
460   TDataStd_Integer::Set(myDoc->Main().Father(), 0);
461 }
462
463 TDF_Label Model_Document::groupLabel(const string theGroup)
464 {
465   // searching for existing
466   TCollection_ExtendedString aGroup(theGroup.c_str());
467   TDF_ChildIDIterator aGroupIter(myDoc->Main().FindChild(TAG_OBJECTS), TDataStd_Comment::GetID());
468   for(; aGroupIter.More(); aGroupIter.Next()) {
469     Handle(TDataStd_Comment) aName = Handle(TDataStd_Comment)::DownCast(aGroupIter.Value());
470     if (aName->Get() == aGroup)
471       return aGroupIter.Value()->Label();
472   }
473   // create a new
474   TDF_Label aNew = myDoc->Main().FindChild(TAG_OBJECTS).NewChild();
475   TDataStd_Comment::Set(aNew, aGroup);
476   return aNew;
477 }
478
479 void Model_Document::setUniqueName(boost::shared_ptr<ModelAPI_Feature> theFeature)
480 {
481   string aName; // result
482   // iterate all features but also iterate group of this feature if object is not in history
483   list<string> aGroups;
484   aGroups.push_back(FEATURES_GROUP);
485   if (!theFeature->isInHistory()) {
486     aGroups.push_back(theFeature->getGroup());
487   }
488   for(list<string>::iterator aGIter = aGroups.begin(); aGIter != aGroups.end(); aGIter++) {
489     // first count all objects of such kind to start with index = count + 1
490     int a, aNumObjects = 0;
491     int aSize = size(*aGIter);
492     for(a = 0; a < aSize; a++) {
493       if (feature(*aGIter, a)->getKind() == theFeature->getKind())
494         aNumObjects++;
495     }
496     // generate candidate name
497     stringstream aNameStream;
498     aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
499     aName = aNameStream.str();
500     // check this is unique, if not, increase index by 1
501     for(a = 0; a < aSize;) {
502       if (feature(*aGIter, a, true)->data()->getName() == aName) {
503         aNumObjects++;
504         stringstream aNameStream;
505         aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
506         aName = aNameStream.str();
507         // reinitialize iterator to make sure a new name is unique
508         a = 0;
509       } else a++;
510     }
511   }
512   theFeature->data()->setName(aName);
513 }
514
515 //! Returns the object by the feature
516 boost::shared_ptr<ModelAPI_Feature> Model_Document::objectByFeature(
517   const boost::shared_ptr<ModelAPI_Feature> theFeature)
518 {
519   for(int a = 0; a < size(theFeature->getGroup()); a++) {
520     boost::shared_ptr<Model_Object> anObj = 
521       boost::dynamic_pointer_cast<Model_Object>(feature(theFeature->getGroup(), a));
522     if (anObj) {
523       return anObj;
524     }
525   }
526   return boost::shared_ptr<ModelAPI_Feature>(); // not found
527 }
528
529 void Model_Document::synchronizeFeatures()
530 {
531   boost::shared_ptr<ModelAPI_Document> aThis = Model_Application::getApplication()->getDocument(myID);
532   // update features
533   vector<boost::shared_ptr<ModelAPI_Feature> >::iterator aFIter = myFeatures.begin();
534   // and in parallel iterate labels of features
535   TDF_ChildIDIterator aFLabIter(groupLabel(FEATURES_GROUP), TDataStd_Comment::GetID());
536   while(aFIter != myFeatures.end() || aFLabIter.More()) {
537     static const int INFINITE_TAG = INT_MAX; // no label means that it exists somwhere in infinite
538     int aFeatureTag = INFINITE_TAG; 
539     if (aFIter != myFeatures.end()) { // existing tag for feature
540       boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>((*aFIter)->data());
541       aFeatureTag = aData->label().Tag();
542     }
543     int aDSTag = INFINITE_TAG; 
544     if (aFLabIter.More()) { // next label in DS is existing
545       aDSTag = aFLabIter.Value()->Label().Tag();
546     }
547     if (aDSTag > aFeatureTag) { // feature is removed
548       Model_FeatureDeletedMessage aMsg1(aThis, FEATURES_GROUP);
549       Model_FeatureDeletedMessage aMsg2(aThis, (*aFIter)->getGroup());
550       aFIter = myFeatures.erase(aFIter);
551       // event: model is updated
552       Events_Loop::loop()->send(aMsg1);
553       Events_Loop::loop()->send(aMsg2);
554     } else if (aDSTag < aFeatureTag) { // a new feature is inserted
555       // create a feature
556       boost::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_PluginManager::get()->createFeature(
557         TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
558         aFLabIter.Value())->Get()).ToCString());
559
560       if (aFIter == myFeatures.end()) { // must be before "setData" to redo the sketch line correctly
561         myFeatures.push_back(aFeature);
562         aFIter = myFeatures.end();
563       } else {
564         aFIter++;
565         myFeatures.insert(aFIter, aFeature);
566       }
567       boost::shared_ptr<Model_Data> aData(new Model_Data);
568       TDF_Label aLab = aFLabIter.Value()->Label();
569       aData->setLabel(aLab);
570       aData->setFeature(aFeature);
571       aFeature->setDoc(aThis);
572       aFeature->setData(aData);
573       aFeature->initAttributes();
574
575       // event: model is updated
576       static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_CREATED);
577       Model_FeatureUpdatedMessage aMsg1(aFeature, anEvent);
578       Events_Loop::loop()->send(aMsg1);
579       boost::shared_ptr<ModelAPI_Feature> anObj = objectByFeature(aFeature);
580       if (anObj) {
581         Model_FeatureUpdatedMessage aMsg2(anObj, anEvent);
582         Events_Loop::loop()->send(aMsg2);
583       }
584
585       // feature for this label is added, so go to the next label
586       aFLabIter.Next();
587     } else { // nothing is changed, both iterators are incremented
588       aFIter++;
589       aFLabIter.Next();
590     }
591   }
592 }