Salome HOME
83f679ce03552b0a79252f047ada9e81046c6fa3
[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_DeformedShape_i.hh"
28 #include "VISU_Result_i.hh"
29
30 #include "VISU_ScalarMapAct.h"
31 #include "VISU_DeformedShapePL.hxx"
32 #include "VISU_Convertor.hxx"
33
34 #include <vtkDataSetMapper.h>
35 #include <vtkProperty.h>
36
37 using namespace VISU;
38 using namespace std;
39
40 #ifdef _DEBUG_
41 static int MYDEBUG = 0;
42 #else
43 static int MYDEBUG = 0;
44 #endif
45
46 int VISU::DeformedShape_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
47                                       const char* theFieldName, int theIteration, int isMemoryCheck)
48 {
49   try{
50     if(!VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration,isMemoryCheck)) return 0;
51     const VISU::PField aField = theResult->GetInput()->GetField(theMeshName,(VISU::TEntity)theEntity,theFieldName);
52     return aField->myNbComp > 1;
53   }catch(std::exception& exc){
54     INFOS("Follow exception was occured :\n"<<exc.what());
55   }catch(...){
56     INFOS("Unknown exception was occured!");
57   }
58   return 0;
59 }
60
61 int VISU::DeformedShape_i::myNbPresent = 0;
62 QString VISU::DeformedShape_i::GenerateName() { return VISU::GenerateName("Def.Shape",myNbPresent++);}
63
64 const string VISU::DeformedShape_i::myComment = "DEFORMEDSHAPE";
65 const char* VISU::DeformedShape_i::GetComment() const { return myComment.c_str();}
66
67
68 VISU::DeformedShape_i::
69 DeformedShape_i(Result_i* theResult, 
70                 bool theAddToStudy) : 
71   PrsObject_i(theResult->GetStudyDocument()), 
72   Prs3d_i(theResult,theAddToStudy),
73   ColoredPrs3d_i(theResult,theAddToStudy),
74   ScalarMap_i(theResult,theAddToStudy),
75   myDeformedShapePL(NULL)
76 {
77   if(MYDEBUG) MESSAGE("DeformedShape_i::DeformedShape_i");
78 }
79
80
81 VISU::DeformedShape_i::
82 DeformedShape_i(Result_i* theResult, 
83                 SALOMEDS::SObject_ptr theSObject) : 
84   PrsObject_i(theResult->GetStudyDocument()), 
85   Prs3d_i(theResult,theSObject),
86   ColoredPrs3d_i(theResult,theSObject),
87   ScalarMap_i(theResult,theSObject),
88   myDeformedShapePL(NULL)
89 {
90   if(MYDEBUG) MESSAGE("DeformedShape_i::DeformedShape_i");
91 }
92
93
94 void VISU::DeformedShape_i::SameAs(const Prs3d_i* theOrigin)
95 {
96   TSuperClass::SameAs(theOrigin);
97
98   if(const DeformedShape_i* aPrs3d = dynamic_cast<const DeformedShape_i*>(theOrigin)){
99     DeformedShape_i* anOrigin = const_cast<DeformedShape_i*>(aPrs3d);
100     SetColor(anOrigin->GetColor());
101     ShowColored(anOrigin->IsColored());
102   }
103 }
104
105
106 VISU::Storable* VISU::DeformedShape_i::Create(const char* theMeshName, VISU::Entity theEntity, 
107                                               const char* theFieldName, int theIteration)
108 {
109   myIsColored = false;
110   myColor.R = myColor.G = myColor.B = 0.5;
111   return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
112 }
113
114
115 VISU::Storable* VISU::DeformedShape_i::Restore(const Storable::TRestoringMap& theMap)
116 {
117   TSuperClass::Restore(theMap);
118
119   SetScale(VISU::Storable::FindValue(theMap,"myFactor").toDouble());
120   myIsColored = VISU::Storable::FindValue(theMap,"myIsColored").toInt();
121   myColor.R = VISU::Storable::FindValue(theMap,"myColor.R").toDouble();
122   myColor.G = VISU::Storable::FindValue(theMap,"myColor.G").toDouble();
123   myColor.B = VISU::Storable::FindValue(theMap,"myColor.B").toDouble();
124
125   return this;
126 }
127
128
129 void VISU::DeformedShape_i::ToStream(std::ostringstream& theStr){
130   ScalarMap_i::ToStream(theStr);
131
132   Storable::DataToStream( theStr, "myFactor", GetScale() );
133
134   Storable::DataToStream( theStr, "myIsColored", myIsColored );
135   Storable::DataToStream( theStr, "myColor.R", myColor.R );
136   Storable::DataToStream( theStr, "myColor.G", myColor.G );
137   Storable::DataToStream( theStr, "myColor.B", myColor.B );
138 }
139
140
141 VISU::DeformedShape_i::~DeformedShape_i(){
142   if(MYDEBUG) MESSAGE("DeformedShape_i::~DeformedShape_i()");
143 }
144
145
146 void VISU::DeformedShape_i::SetScale(CORBA::Double theScale) { 
147   myDeformedShapePL->SetScale(theScale);
148 }
149 CORBA::Double VISU::DeformedShape_i::GetScale(){
150   return myDeformedShapePL->GetScale();
151 }
152
153
154 void VISU::DeformedShape_i::DoHook(){
155   if(!myPipeLine) myPipeLine = VISU_DeformedShapePL::New();
156   myDeformedShapePL = dynamic_cast<VISU_DeformedShapePL*>(myPipeLine);
157   myPipeLine->GetMapper()->SetScalarVisibility(IsColored());
158
159   ScalarMap_i::DoHook();
160 }
161
162
163 void VISU::DeformedShape_i::SetMapScale(double theMapScale){
164   myDeformedShapePL->SetMapScale(theMapScale);
165 }
166
167
168 VISU_Actor* 
169 VISU::DeformedShape_i
170 ::CreateActor(const Handle(SALOME_InteractiveObject)& theIO) 
171 {
172   VISU_Actor* anActor = VISU::ScalarMap_i::CreateActor(theIO);
173   anActor->SetRepresentation(1);
174   anActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
175   anActor->SetVTKMapping(false);
176   return anActor;
177 }
178
179
180 void VISU::DeformedShape_i::UpdateActor(VISU_Actor* theActor) {
181   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
182     if(IsColored()){
183       anActor->SetBarVisibility(true);
184       myPipeLine->GetMapper()->SetScalarVisibility(1);
185     }else{
186       anActor->SetBarVisibility(false);
187       myPipeLine->GetMapper()->SetScalarVisibility(0);
188       anActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
189     }
190     VISU::ScalarMap_i::UpdateActor(theActor);
191   }
192 }