Salome HOME
DCQ : Merge with Ecole Ete a6.
[modules/visu.git] / src / VISU_I / VISU_DeformedShape_i.cc
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //  File   : VISU_PrsObject_i.cxx
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_DeformedShapePL.hxx"
28 #include "VISU_Result_i.hh"
29 #include "VISU_DeformedShape_i.hh"
30 #include "VISU_ScalarMapAct.h"
31
32 using namespace VISU;
33 using namespace std;
34
35 #ifdef _DEBUG_
36 static int MYDEBUG = 0;
37 static int MYDEBUGWITHFILES = 0;
38 #else
39 static int MYDEBUG = 0;
40 static int MYDEBUGWITHFILES = 0;
41 #endif
42
43 int VISU::DeformedShape_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
44                                       const char* theFieldName, int theIteration, int isMemoryCheck)
45 {
46   try{
47     if(!VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration,isMemoryCheck)) return 0;
48     const VISU::TField& aField = theResult->GetInput()->GetField(theMeshName,(VISU::TEntity)theEntity,theFieldName);
49     return aField.myNbComp > 1;
50   }catch(std::runtime_error& exc){
51     INFOS("Follow exception was accured :\n"<<exc.what());
52   }catch(...){
53     INFOS("Unknown exception was accured!");
54   }
55   return 0;
56 }
57
58 int VISU::DeformedShape_i::myNbPresent = 0;
59 QString VISU::DeformedShape_i::GenerateName() { return VISU::GenerateName("Def.Shape",myNbPresent++);}
60
61 const string VISU::DeformedShape_i::myComment = "DEFORMEDSHAPE";
62 const char* VISU::DeformedShape_i::GetComment() const { return myComment.c_str();}
63
64
65 VISU::DeformedShape_i::DeformedShape_i(Result_i* theResult, bool theAddToStudy, SALOMEDS::SObject_ptr theSObject) : 
66        PrsObject_i(theResult->GetStudyDocument()), 
67        Prs3d_i(theResult,theSObject),
68        ScalarMap_i(theResult,theAddToStudy,theSObject)
69 {
70   if(MYDEBUG) MESSAGE("DeformedShape_i::DeformedShape_i");
71   myDeformedShapePL = NULL;
72 }
73
74
75
76 void VISU::DeformedShape_i::SameAs(const DeformedShape_i* theOrigin)
77 {
78   DeformedShape_i* aDefShape = const_cast<DeformedShape_i*>(theOrigin);
79   VISU::ScalarMap_i::SameAs(theOrigin);
80
81   SetColor(aDefShape->GetColor());
82   ShowColored(aDefShape->IsColored());
83 }
84
85
86 VISU::Storable* VISU::DeformedShape_i::Create(const char* theMeshName, VISU::Entity theEntity, 
87                                               const char* theFieldName, int theIteration)
88 {
89   myIsColored = false;
90   myColor.R = myColor.G = myColor.B = 0.5;
91   return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
92 }
93
94
95 VISU::Storable* VISU::DeformedShape_i::Restore(const Storable::TRestoringMap& theMap)
96 {
97   DoHook();
98   SetScale(VISU::Storable::FindValue(theMap,"myFactor").toDouble());
99   myIsColored = VISU::Storable::FindValue(theMap,"myIsColored").toInt();
100   myColor.R = VISU::Storable::FindValue(theMap,"myColor.R").toDouble();
101   myColor.G = VISU::Storable::FindValue(theMap,"myColor.G").toDouble();
102   myColor.B = VISU::Storable::FindValue(theMap,"myColor.B").toDouble();
103
104   return ScalarMap_i::Restore(theMap);
105 }
106
107
108 void VISU::DeformedShape_i::ToStream(std::ostringstream& theStr){
109   ScalarMap_i::ToStream(theStr);
110
111   Storable::DataToStream( theStr, "myFactor", GetScale() );
112
113   Storable::DataToStream( theStr, "myIsColored", myIsColored );
114   Storable::DataToStream( theStr, "myColor.R", myColor.R );
115   Storable::DataToStream( theStr, "myColor.G", myColor.G );
116   Storable::DataToStream( theStr, "myColor.B", myColor.B );
117 }
118
119
120 VISU::DeformedShape_i::~DeformedShape_i(){
121   if(MYDEBUG) MESSAGE("DeformedShape_i::~DeformedShape_i()");
122 }
123
124
125 void VISU::DeformedShape_i::SetScale(CORBA::Double theScale) { 
126   myDeformedShapePL->SetScale(theScale);
127 }
128 CORBA::Double VISU::DeformedShape_i::GetScale(){
129   return myDeformedShapePL->GetScale();
130 }
131
132
133 void VISU::DeformedShape_i::DoHook(){
134   if(!myPipeLine) myPipeLine = VISU_DeformedShapePL::New();
135   myDeformedShapePL = dynamic_cast<VISU_DeformedShapePL*>(myPipeLine);
136   myPipeLine->GetMapper()->SetScalarVisibility(IsColored());
137
138   ScalarMap_i::DoHook();
139 }
140
141
142 void VISU::DeformedShape_i::SetMapScale(double theMapScale){
143   myDeformedShapePL->SetMapScale(theMapScale);
144 }
145
146
147 VISU_Actor* VISU::DeformedShape_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO) 
148 {
149   VISU_Actor* anActor = VISU::ScalarMap_i::CreateActor(theIO);
150   anActor->SetRepresentation(1);
151   anActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
152   return anActor;
153 }
154
155
156 void VISU::DeformedShape_i::UpdateActor(VISU_Actor* theActor) {
157   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
158     if(IsColored()){
159       anActor->SetBarVisibility(true);
160       myPipeLine->GetMapper()->SetScalarVisibility(1);
161     }else{
162       anActor->SetBarVisibility(false);
163       myPipeLine->GetMapper()->SetScalarVisibility(0);
164       anActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
165     }
166     VISU::ScalarMap_i::UpdateActor(theActor);
167   }
168 }