Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Model / Model_Document.cxx
index ddbdfc53a4ec6518bf18603a73ae88e905568c94..b7f195242b01c28297f02a454a682fc245861acf 100644 (file)
@@ -3,12 +3,15 @@
 // Author:      Mikhail PONIKAROV
 
 #include <Model_Document.h>
-#include <Model_Feature.h>
+#include <ModelAPI_Feature.h>
+#include <Model_Object.h>
+#include <Model_Application.h>
+#include <Model_PluginManager.h>
+#include <Model_Iterator.h>
+#include <Event_Loop.h>
 
 #include <TDataStd_Integer.hxx>
-
-IMPLEMENT_STANDARD_HANDLE(Model_Document, MMgt_TShared)
-IMPLEMENT_STANDARD_RTTIEXT(Model_Document, MMgt_TShared)
+#include <TDataStd_Comment.hxx>
 
 static const int UNDO_LIMIT = 10; // number of possible undo operations
 
@@ -18,7 +21,7 @@ static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for Model
 
 using namespace std;
 
-bool Model_Document::Load(const char* theFileName)
+bool Model_Document::load(const char* theFileName)
 {
   bool myIsError = Standard_False;
   /*
@@ -59,7 +62,7 @@ bool Model_Document::Load(const char* theFileName)
   return !myIsError;
 }
 
-bool Model_Document::Save(const char* theFileName)
+bool Model_Document::save(const char* theFileName)
 {
   bool myIsError = true;
   /*
@@ -97,78 +100,198 @@ bool Model_Document::Save(const char* theFileName)
   return !myIsError;
 }
 
-void Model_Document::Close()
+void Model_Document::close()
 {
-  TDocStd_Document::Close();
+  myDoc->Close();
 }
 
-void Model_Document::StartOperation()
+void Model_Document::startOperation()
 {
-  TDocStd_Document::NewCommand();
+  myDoc->NewCommand();
 }
 
-void Model_Document::FinishOperation()
+void Model_Document::finishOperation()
 {
-  TDocStd_Document::CommitCommand();
+  myDoc->CommitCommand();
   myTransactionsAfterSave++;
 }
 
-void Model_Document::AbortOperation()
+void Model_Document::abortOperation()
 {
-  TDocStd_Document::AbortCommand();
+  myDoc->AbortCommand();
 }
 
-bool Model_Document::IsOperation()
+bool Model_Document::isOperation()
 {
-  return TDocStd_Document::HasOpenCommand() == Standard_True ;
+  return myDoc->HasOpenCommand() == Standard_True ;
 }
 
-bool Model_Document::IsModified()
+bool Model_Document::isModified()
 {
   return myTransactionsAfterSave != 0;
 }
 
-bool Model_Document::CanUndo()
+bool Model_Document::canUndo()
 {
-  return TDocStd_Document::GetAvailableUndos() > 0;
+  return myDoc->GetAvailableUndos() > 0;
 }
 
-void Model_Document::Undo()
+void Model_Document::undo()
 {
-  TDocStd_Document::Undo();
+  myDoc->Undo();
   myTransactionsAfterSave--;
 }
 
-bool Model_Document::CanRedo()
+bool Model_Document::canRedo()
 {
-  return TDocStd_Document::GetAvailableRedos() > 0;
+  return myDoc->GetAvailableRedos() > 0;
 }
 
-void Model_Document::Redo()
+void Model_Document::redo()
 {
-  TDocStd_Document::Redo();
+  myDoc->Redo();
   myTransactionsAfterSave++;
 }
 
-void Model_Document::AddObject(
-  boost::shared_ptr<ModelAPI_Feature> theFeature, const int theGroupID)
+shared_ptr<ModelAPI_Feature> Model_Document::addFeature(string theID)
+{
+  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)
 {
-  boost::shared_ptr<Model_Feature> aModelFeature = 
-    boost::dynamic_pointer_cast<Model_Feature>(theFeature);
-  if (aModelFeature) {
-    TDF_Label aGroupLab = Main().FindChild(TAG_OBJECTS).FindChild(theGroupID + 1);
-    TDF_Label anObjLab = aGroupLab.NewChild();
-    aModelFeature->setLabel(anObjLab);
+  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);
+  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_FEATURE_UPDATED);
+  ModelAPI_FeatureUpdatedMessage aMsg(theFeature);
+  Event_Loop::loop()->send(aMsg);
+}
+
+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<Model_Object> aData(new Model_Object);
+    aData->setLabel(theLabel);
+    aData->setDocument(Model_Application::getApplication()->getDocument(myID));
+    aResult->setData(aData);
+    aResult->initAttributes();
+    return aResult;
   }
+  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);
 }
 
-Model_Document::Model_Document(const TCollection_ExtendedString& theStorageFormat)
-    : TDocStd_Document(theStorageFormat)
+shared_ptr<ModelAPI_Iterator> Model_Document::featuresIterator(const string theGroup)
 {
-  SetUndoLimit(UNDO_LIMIT);
+  shared_ptr<Model_Document> aThis(Model_Application::getApplication()->getDocument(myID));
+  return shared_ptr<ModelAPI_Iterator>(new Model_Iterator(aThis, groupLabel(theGroup)));
+}
+
+shared_ptr<ModelAPI_Feature> Model_Document::feature(const string& theGroupID, const int theIndex)
+{
+  // TODO: optimize this method
+  shared_ptr<ModelAPI_Iterator>  anIter = featuresIterator(theGroupID);
+  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)
+    : myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
+{
+  myDoc->SetUndoLimit(UNDO_LIMIT);
   myTransactionsAfterSave = 0;
 }
 
-Model_Document::~Model_Document()
+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)
+{
+  // first count all objects of such kind to start with index = count + 1
+  int aNumObjects = 0;
+  shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theFeature->getGroup());
+  for(; anIter->more(); anIter->next()) {
+    if (anIter->currentKind() == theFeature->getKind())
+      aNumObjects++;
+  }
+  // generate candidate name
+  stringstream aNameStream;
+  aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
+  string aName = aNameStream.str();
+  // check this is unique, if not, increase index by 1
+  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(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;
 }