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