Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Model / Model_Document.cxx
index d435a01277fec031419267bc3c0c494bb74928ad..b7f195242b01c28297f02a454a682fc245861acf 100644 (file)
@@ -153,33 +153,46 @@ void Model_Document::redo()
   myTransactionsAfterSave++;
 }
 
-void Model_Document::addFeature(
-  std::shared_ptr<ModelAPI_Feature> theFeature, const std::string theGroupID)
+shared_ptr<ModelAPI_Feature> Model_Document::addFeature(string theID)
 {
-  TDF_Label aGroupLab = groupLabel(theGroupID);
+  shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_PluginManager::get()->createFeature(theID);
+  if (aFeature) {
+    dynamic_pointer_cast<Model_Document>(aFeature->documentToAdd())->addFeature(aFeature);
+  } else {
+    // TODO: generate error that feature is not created
+  }
+  return aFeature;
+}
+
+void Model_Document::addFeature(const std::shared_ptr<ModelAPI_Feature> theFeature)
+{
+  TDF_Label aGroupLab = groupLabel(theFeature->getGroup());
   TDF_Label anObjLab = aGroupLab.NewChild();
   std::shared_ptr<Model_Object> aData(new Model_Object);
   aData->setLabel(anObjLab);
+  aData->setDocument(Model_Application::getApplication()->getDocument(myID));
   theFeature->setData(aData);
-  setUniqueName(theFeature, theGroupID);
+  setUniqueName(theFeature);
   theFeature->initAttributes();
+  // keep the feature ID to restore document later correctly
   TDataStd_Comment::Set(anObjLab, theFeature->getKind().c_str());
 
   // event: model is updated
-  static Event_ID anEvent = Event_Loop::eventByName(EVENT_MODEL_UPDATED);
-  Event_Message anUpdateMsg(anEvent, this);
-  Event_Loop::loop()->send(anUpdateMsg);
-
+  static Event_ID anEvent = Event_Loop::eventByName(EVENT_FEATURE_UPDATED);
+  ModelAPI_FeatureUpdatedMessage aMsg(theFeature);
+  Event_Loop::loop()->send(aMsg);
 }
 
-std::shared_ptr<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
+shared_ptr<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
 {
   Handle(TDataStd_Comment) aFeatureID;
   if (theLabel.FindAttribute(TDataStd_Comment::GetID(), aFeatureID)) {
     string anID(TCollection_AsciiString(aFeatureID->Get()).ToCString());
-    std::shared_ptr<ModelAPI_Feature> aResult = Model_PluginManager::get()->createFeature(anID);
+    std::shared_ptr<ModelAPI_Feature> aResult = 
+      Model_PluginManager::get()->createFeature(anID);
     std::shared_ptr<Model_Object> aData(new Model_Object);
     aData->setLabel(theLabel);
+    aData->setDocument(Model_Application::getApplication()->getDocument(myID));
     aResult->setData(aData);
     aResult->initAttributes();
     return aResult;
@@ -187,6 +200,17 @@ std::shared_ptr<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
   return std::shared_ptr<ModelAPI_Feature>(); // not found
 }
 
+int Model_Document::featureIndex(shared_ptr<ModelAPI_Feature> theFeature)
+{
+  if (theFeature->data()->document().get() != this)
+    return theFeature->data()->document()->featureIndex(theFeature);
+  shared_ptr<ModelAPI_Iterator> anIter(featuresIterator(theFeature->getGroup()));
+  for(int anIndex = 0; anIter->more(); anIter->next(), anIndex++)
+    if (anIter->is(theFeature)) 
+      return anIndex;
+  return -1; // not found
+}
+
 shared_ptr<ModelAPI_Document> Model_Document::subDocument(string theDocID)
 {
   return Model_Application::getApplication()->getDocument(theDocID);
@@ -202,8 +226,13 @@ shared_ptr<ModelAPI_Feature> Model_Document::feature(const string& theGroupID, c
 {
   // TODO: optimize this method
   shared_ptr<ModelAPI_Iterator>  anIter = featuresIterator(theGroupID);
-  for(int a = 0; a != theIndex; anIter->next()) a++;
-  return anIter->current();
+  for(int a = 0; a != theIndex && anIter->more(); anIter->next()) a++;
+  return anIter->more() ? anIter->current() : shared_ptr<ModelAPI_Feature>();
+}
+
+const vector<string>& Model_Document::getGroups() const
+{
+  return myGroupsNames;
 }
 
 Model_Document::Model_Document(const std::string theID)
@@ -217,16 +246,17 @@ TDF_Label Model_Document::groupLabel(const string theGroup)
 {
   if (myGroups.find(theGroup) == myGroups.end()) {
     myGroups[theGroup] = myDoc->Main().FindChild(TAG_OBJECTS).NewChild();
+    myGroupsNames.push_back(theGroup);
   }
   return myGroups[theGroup];
 }
 
 void Model_Document::setUniqueName(
-  shared_ptr<ModelAPI_Feature> theFeature, const string theGroupID)
+  shared_ptr<ModelAPI_Feature> theFeature)
 {
   // first count all objects of such kind to start with index = count + 1
   int aNumObjects = 0;
-  shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theGroupID);
+  shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theFeature->getGroup());
   for(; anIter->more(); anIter->next()) {
     if (anIter->currentKind() == theFeature->getKind())
       aNumObjects++;
@@ -236,15 +266,32 @@ void Model_Document::setUniqueName(
   aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
   string aName = aNameStream.str();
   // check this is unique, if not, increase index by 1
-  for(anIter = featuresIterator(theGroupID); anIter->more();) {
+  for(anIter = featuresIterator(theFeature->getGroup()); anIter->more();) {
     if (anIter->currentName() == aName) {
       aNumObjects++;
       stringstream aNameStream;
       aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
       // reinitialize iterator to make sure a new name is unique
-      anIter = featuresIterator(theGroupID);
+      anIter = featuresIterator(theFeature->getGroup());
     } else anIter->next();
   }
 
   theFeature->data()->setName(aName);
 }
+
+
+ModelAPI_FeatureUpdatedMessage::ModelAPI_FeatureUpdatedMessage(
+  shared_ptr<ModelAPI_Feature> theFeature)
+  : Event_Message(messageId(), 0), myFeature(theFeature)
+{}
+
+const Event_ID ModelAPI_FeatureUpdatedMessage::messageId()
+{
+  static Event_ID MY_ID = Event_Loop::eventByName("FeatureUpdated");
+  return MY_ID;
+}
+
+shared_ptr<ModelAPI_Feature> ModelAPI_FeatureUpdatedMessage::feature()
+{
+  return myFeature;
+}