Salome HOME
Merge with branch V2_2_0_VISU_improvement
[modules/visu.git] / src / VISU_I / VISU_TimeAnimation.cxx
index 1d0fa58d589d5a17c730e7accd2dcb0894932a86..2c7ba0ee5572a52ed122cd9bddb050f35fc75355 100644 (file)
@@ -70,6 +70,8 @@ VISU_TimeAnimation::VISU_TimeAnimation (_PTR(Study) theStudy,
   myTimeMax = 0;
   myLastError = "";
   myCycling = false;
+
+  myAnimEntry = "";
 }
 
 
@@ -83,14 +85,13 @@ VISU_TimeAnimation::~VISU_TimeAnimation()
 
 
 //************************************************************************
-//void VISU_TimeAnimation::addField (SALOMEDS::SObject_ptr theField)
 void VISU_TimeAnimation::addField (_PTR(SObject) theField)
 {
   FieldData aNewData;
-  //aNewData.myField = SALOMEDS::SObject::_duplicate(theField);
   aNewData.myField = theField;
   aNewData.myNbFrames = 0;
   aNewData.myPrsType = VISU::TSCALARMAP;
+  aNewData.myOffset[0] = aNewData.myOffset[1] = aNewData.myOffset[2] = 0;
   VISU::Storable::TRestoringMap aMap = getMapOfValue(aNewData.myField);
   aNewData.myNbTimes = VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
   myFieldsLst.append(aNewData);
@@ -110,7 +111,6 @@ void VISU_TimeAnimation::addField (_PTR(SObject) theField)
 void VISU_TimeAnimation::addField (SALOMEDS::SObject_ptr theField)
 {
   FieldData aNewData;
-  //aNewData.myField = SALOMEDS::SObject::_duplicate(theField);
   _PTR(SObject) aField = VISU::GetClientSObject(theField, myStudy);
   aNewData.myField = aField;
   aNewData.myNbFrames = 0;
@@ -270,11 +270,13 @@ void VISU_TimeAnimation::generatePresentations(CORBA::Long theFieldNum) {
   aData.myNbFrames = i;
   QString aFixRange = VISU::GetResourceMgr()->stringValue("Visu:SBImposeRange");
   if (aFixRange.compare("true") != 0) {
-    for (i = 0; i < aData.myNbFrames; i++)
+    for (i = 0; i < aData.myNbFrames; i++) {
       aData.myPrs[i]->SetRange(aMin, aMax);
-    if(aData.myPrsType == VISU::TISOSURFACE)
+      aData.myPrs[i]->SetOffset(aData.myOffset);
+    }
+    if (aData.myPrsType == VISU::TISOSURFACE)
       for (i = 0; i < aData.myNbFrames; i++)
-       if(VISU::IsoSurfaces_i* aPrs = dynamic_cast<VISU::IsoSurfaces_i*>(aData.myPrs[i]))
+       if (VISU::IsoSurfaces_i* aPrs = dynamic_cast<VISU::IsoSurfaces_i*>(aData.myPrs[i]))
           aPrs->SetSubRange(aMin, aMax);
   }
 }
@@ -298,6 +300,7 @@ CORBA::Boolean VISU_TimeAnimation::generateFrames() {
     for (long j = 0; j < aData.myNbFrames; j++) {
       VISU_Actor* aActor = NULL;
       try{
+       aData.myPrs[j]->SetOffset(aData.myOffset);
        aActor = aData.myPrs[j]->CreateActor();
        myView->AddActor(aActor);
        if(j == 0)
@@ -553,10 +556,8 @@ void VISU_TimeAnimation::run() {
 }
 
 //************************************************************************
-//VISU::Result_i* VISU_TimeAnimation::createPresent (SALOMEDS::SObject_var theField)
 VISU::Result_i* VISU_TimeAnimation::createPresent (_PTR(SObject) theField)
 {
-  //SALOMEDS::SObject_var aSObj = theField->GetFather();
   _PTR(SObject) aSObj = theField->GetFather();
   aSObj = aSObj->GetFather();
   aSObj = aSObj->GetFather();
@@ -566,20 +567,14 @@ VISU::Result_i* VISU_TimeAnimation::createPresent (_PTR(SObject) theField)
 }
 
 //************************************************************************
-//VISU::Storable::TRestoringMap VISU_TimeAnimation::getMapOfValue (SALOMEDS::SObject_var theSObject)
 VISU::Storable::TRestoringMap VISU_TimeAnimation::getMapOfValue (_PTR(SObject) theSObject)
 {
   VISU::Storable::TRestoringMap aMap;
-  //if (!theSObject->_is_nil()) {
   if (theSObject) {
-    //SALOMEDS::GenericAttribute_var anAttr;
     _PTR(GenericAttribute) anAttr;
     if (theSObject->FindAttribute(anAttr, "AttributeComment")) {
-      //SALOMEDS::AttributeComment_var aComment = SALOMEDS::AttributeComment::_narrow(anAttr);
       _PTR(AttributeComment) aComment (anAttr);
-      //CORBA::String_var aString = aComment->Value();
       std::string aString = aComment->Value();
-      //QString strIn (aString.in());
       QString strIn (aString.c_str());
       VISU::Storable::StrToMap(strIn, aMap);
     }
@@ -588,15 +583,11 @@ VISU::Storable::TRestoringMap VISU_TimeAnimation::getMapOfValue (_PTR(SObject) t
 }
 
 //************************************************************************
-//double VISU_TimeAnimation::getTimeValue (SALOMEDS::SObject_var theTimeStamp)
 double VISU_TimeAnimation::getTimeValue (_PTR(SObject) theTimeStamp)
 {
-  //SALOMEDS::GenericAttribute_var anAttr;
   _PTR(GenericAttribute) anAttr;
   if (theTimeStamp->FindAttribute(anAttr, "AttributeName")) {
-    //SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
     _PTR(AttributeName) aName (anAttr);
-    //QString aNameString (aName->Value());
     QString aNameString (aName->Value().c_str());
     return aNameString.toDouble();
   }
@@ -609,7 +600,171 @@ void VISU_TimeAnimation::setSpeed(CORBA::Long theSpeed)
   mySpeed = (theSpeed<1)? 1 : theSpeed;
 }
 
+//************************************************************************
+int VISU_TimeAnimation::myNBAnimations = 0;
+QString VISU_TimeAnimation::GenerateName()
+{
+  return VISU::GenerateName("Animation", myNBAnimations++);
+}
+
+//************************************************************************
+SALOMEDS::SObject_ptr VISU_TimeAnimation::publishInStudy()
+{
+  if (myStudy->GetProperties()->IsLocked())
+    return SALOMEDS::SObject::_nil();
+
+  _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
+  aStudyBuilder->NewCommand();  // There is a transaction
+  _PTR(SComponent) aSComponent = VISU::ClientFindOrCreateVisuComponent(myStudy);
+  std::string aSComponentEntry = aSComponent->GetID();
+
+  QString aComment;
+  aComment.sprintf("myComment=ANIMATION;myType=%d;myMinVal=%g;myMaxVal=%g",
+                   VISU::TANIMATION,myMinVal,myMaxVal);
+
+  string anEntry = VISU::CreateAttributes(myStudy,aSComponentEntry.c_str(),"","",
+                                          GenerateName(),"",aComment,true);
+  myAnimEntry = anEntry.c_str();
+  _PTR(SObject) aAnimSObject = myStudy->FindObjectID(anEntry.c_str());
+
+  for (int i = 0; i < getNbFields(); i++) {
+    FieldData& aData = myFieldsLst[i];
+    _PTR(SObject) newObj = aStudyBuilder->NewObject(aAnimSObject);
+    aStudyBuilder->Addreference(newObj, aData.myField);
+    if (!aData.myPrs.empty()) {
+      ostringstream strOut;
+      aData.myPrs[0]->ToStream(strOut);
+      string aPrsComment = strOut.str();
+      VISU::CreateAttributes(myStudy, newObj->GetID().c_str(),"","",
+                             aData.myPrs[0]->GetComment(),"",aPrsComment.c_str(),true);
+    }
+  }
+  aStudyBuilder->CommitCommand();
+
+  return VISU::GetSObject(aAnimSObject);
+}
+
+//************************************************************************
+void VISU_TimeAnimation::saveAnimation()
+{
+  if (myStudy->GetProperties()->IsLocked())  return;
+  if (myAnimEntry.isEmpty()) return;
+
+  _PTR(SObject) aAnimSObject = myStudy->FindObjectID(myAnimEntry);
+  if (!aAnimSObject) return;
+
+  _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
+  aStudyBuilder->NewCommand();  // There is a transaction
+  _PTR(SComponent) aSComponent = VISU::ClientFindOrCreateVisuComponent(myStudy);
+  std::string aSComponentEntry = aSComponent->GetID();
+
+  QString aComment;
+  aComment.sprintf("myComment=ANIMATION;myType=%d;myMinVal=%g;myMaxVal=%g",
+                   VISU::TANIMATION,myMinVal,myMaxVal);
+
+  _PTR(GenericAttribute) anAttr;
+  anAttr = aStudyBuilder->FindOrCreateAttribute(aAnimSObject, "AttributeComment");
+  _PTR(AttributeComment) aCmnt (anAttr);
+  aCmnt->SetValue(aComment);
+
+  _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aAnimSObject);
+  int i;
+  for (i = 0, anIter->Init(); anIter->More(); anIter->Next(), i++) {
+    _PTR(SObject) aRefObj = anIter->Value();
+    _PTR(ChildIterator) anPrsIter = myStudy->NewChildIterator(aRefObj);
+    anPrsIter->Init();
+    if (!anPrsIter->More()) continue;
+    FieldData& aData = myFieldsLst[i];
+    if (aData.myPrs.empty()) continue;
+
+    _PTR(SObject) aPrsObj = anPrsIter->Value();
+    ostringstream strOut;
+    aData.myPrs[0]->ToStream(strOut);
+    string aPrsComment = strOut.str();
+    anAttr = aStudyBuilder->FindOrCreateAttribute(aPrsObj, "AttributeComment");
+    aCmnt = _PTR(AttributeComment)(anAttr);
+    aCmnt->SetValue(aPrsComment.c_str());
+
+    anAttr = aStudyBuilder->FindOrCreateAttribute(aPrsObj, "AttributeName");
+    _PTR(AttributeName) aPrsName (anAttr);
+    aPrsName->SetValue(aData.myPrs[0]->GetComment());
+  }
+  aStudyBuilder->CommitCommand();
+}
+
+//************************************************************************
+void VISU_TimeAnimation::restoreFromStudy(SALOMEDS::SObject_ptr theField)
+{
+  //SALOMEDS::SObject_var aAnimSObject = SALOMEDS::SObject::_duplicate(theField);
+  _PTR(SObject) aAnimSObject = VISU::GetClientSObject(theField, myStudy);
+
+  VISU::Storable::TRestoringMap aMap;
+  _PTR(GenericAttribute) anAttr;
+  if (!aAnimSObject->FindAttribute(anAttr, "AttributeComment")) return;
+
+  _PTR(AttributeComment) aComment (anAttr);
+  string aComm = aComment->Value();
+  QString strIn (aComm.c_str());
+  VISU::Storable::StrToMap(strIn,aMap);
+  bool isExist;
+
+  myMinVal = VISU::Storable::FindValue(aMap,"myMinVal",&isExist).toDouble();
+  myMaxVal = VISU::Storable::FindValue(aMap,"myMaxVal",&isExist).toDouble();
+
+  _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aAnimSObject);
+  for (anIter->Init(); anIter->More(); anIter->Next()) {
+    _PTR(SObject) aRefObj = anIter->Value();
+    _PTR(SObject) aFieldObj;
+    if (!aRefObj->ReferencedObject(aFieldObj) ) continue;
+    addField(aFieldObj);
+    FieldData& aData = getFieldData(getNbFields()-1);
+
+    // Get Presentation object
+    _PTR(ChildIterator) anPrsIter = myStudy->NewChildIterator(aRefObj);
+    anPrsIter->Init();
+    if (!anPrsIter->More()) continue;
+    _PTR(SObject) aPrsObj = anPrsIter->Value();
+    if (!aPrsObj->FindAttribute(anAttr, "AttributeName")) continue;
+    _PTR(AttributeName) aName (anAttr);
+    string aStr = aName->Value();
+    QString strName (aStr.c_str());
+
+    if (strName == VISU::ScalarMap_i::myComment.c_str())
+      aData.myPrsType = VISU::TSCALARMAP;
+    else if (strName == VISU::IsoSurfaces_i::myComment.c_str())
+      aData.myPrsType = VISU::TISOSURFACE;
+    else if (strName == VISU::CutPlanes_i::myComment.c_str())
+      aData.myPrsType = VISU::TCUTPLANES;
+    else if (strName == VISU::DeformedShape_i::myComment.c_str())
+      aData.myPrsType = VISU::TDEFORMEDSHAPE;
+    else if (strName == VISU::Vectors_i::myComment.c_str())
+      aData.myPrsType = VISU::TVECTORS;
+    else if (strName == VISU::StreamLines_i::myComment.c_str())
+      aData.myPrsType = VISU::TSTREAMLINES;
+    else
+      continue;
+    generatePresentations(getNbFields()-1);
+
+    if (!aPrsObj->FindAttribute(anAttr, "AttributeComment")) return;
+    _PTR(AttributeComment) aPrsComment (anAttr);
+    string aPrsComm = aPrsComment->Value();
+    QString strPrsIn(aPrsComm.c_str());
+    VISU::Storable::TRestoringMap aPrsMap;
+    VISU::Storable::StrToMap(strPrsIn,aPrsMap);
+
+    aData.myPrs[0]->Restore(aPrsMap);
+    aData.myPrs[0]->GetOffset(aData.myOffset);
+    for (int i = 1; i < aData.myNbFrames; i++) {
+      aData.myPrs[i]->SameAs(aData.myPrs[0]);
+    }
+  }
+  string aStr = aAnimSObject->GetID();
+  myAnimEntry = aStr.c_str();
+}
+
 
+//========================================================================
+//========================================================================
 //========================================================================
 VISU_TimeAnimation_i::VISU_TimeAnimation_i (SALOMEDS::Study_ptr theStudy,
                                             VISU::View3D_ptr theView3D)
@@ -806,3 +961,23 @@ void VISU_TimeAnimation_i::setCycling (CORBA::Boolean theCycle)
 {
   myAnim->setCycling(theCycle);
 }
+
+SALOMEDS::SObject_ptr VISU_TimeAnimation_i::publishInStudy()
+{
+  return myAnim->publishInStudy();
+}
+
+void VISU_TimeAnimation_i::restoreFromStudy(SALOMEDS::SObject_ptr theObj)
+{
+  myAnim->restoreFromStudy(theObj);
+}
+
+CORBA::Boolean VISU_TimeAnimation_i::isSavedInStudy()
+{
+  return myAnim->isSavedInStudy();
+}
+
+void VISU_TimeAnimation_i::saveAnimation()
+{
+  myAnim->saveAnimation();
+}