Salome HOME
Bug PAL8221 - POST-PRO: Pal/Salome crashes if edit presentation in opened study
[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 #include <vtkDataSetMapper.h>
33
34 using namespace VISU;
35 using namespace std;
36
37 #ifdef _DEBUG_
38 static int MYDEBUG = 0;
39 #else
40 static int MYDEBUG = 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::PField aField = theResult->GetInput()->GetField(theMeshName,(VISU::TEntity)theEntity,theFieldName);
49     return aField->myNbComp > 1;
50   }catch(std::exception& exc){
51     INFOS("Follow exception was occured :\n"<<exc.what());
52   }catch(...){
53     INFOS("Unknown exception was occured!");
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::
66 DeformedShape_i(Result_i* theResult, 
67                 bool theAddToStudy) : 
68   PrsObject_i(theResult->GetStudyDocument()), 
69   Prs3d_i(theResult,theAddToStudy),
70   ScalarMap_i(theResult,theAddToStudy),
71   myDeformedShapePL(NULL)
72 {
73   if(MYDEBUG) MESSAGE("DeformedShape_i::DeformedShape_i");
74 }
75
76
77 VISU::DeformedShape_i::
78 DeformedShape_i(Result_i* theResult, 
79                 SALOMEDS::SObject_ptr theSObject) : 
80   PrsObject_i(theResult->GetStudyDocument()), 
81   Prs3d_i(theResult,theSObject),
82   ScalarMap_i(theResult,theSObject),
83   myDeformedShapePL(NULL)
84 {
85   if(MYDEBUG) MESSAGE("DeformedShape_i::DeformedShape_i");
86 }
87
88
89 void VISU::DeformedShape_i::SameAs(const DeformedShape_i* theOrigin)
90 {
91   DeformedShape_i* aDefShape = const_cast<DeformedShape_i*>(theOrigin);
92   VISU::ScalarMap_i::SameAs(theOrigin);
93
94   SetColor(aDefShape->GetColor());
95   ShowColored(aDefShape->IsColored());
96 }
97
98
99 VISU::Storable* VISU::DeformedShape_i::Create(const char* theMeshName, VISU::Entity theEntity, 
100                                               const char* theFieldName, int theIteration)
101 {
102   myIsColored = false;
103   myColor.R = myColor.G = myColor.B = 0.5;
104   return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
105 }
106
107
108 VISU::Storable* VISU::DeformedShape_i::Restore(const Storable::TRestoringMap& theMap)
109 {
110   DoHook();
111   SetScale(VISU::Storable::FindValue(theMap,"myFactor").toDouble());
112   myIsColored = VISU::Storable::FindValue(theMap,"myIsColored").toInt();
113   myColor.R = VISU::Storable::FindValue(theMap,"myColor.R").toDouble();
114   myColor.G = VISU::Storable::FindValue(theMap,"myColor.G").toDouble();
115   myColor.B = VISU::Storable::FindValue(theMap,"myColor.B").toDouble();
116
117   return ScalarMap_i::Restore(theMap);
118 }
119
120
121 void VISU::DeformedShape_i::ToStream(std::ostringstream& theStr){
122   ScalarMap_i::ToStream(theStr);
123
124   Storable::DataToStream( theStr, "myFactor", GetScale() );
125
126   Storable::DataToStream( theStr, "myIsColored", myIsColored );
127   Storable::DataToStream( theStr, "myColor.R", myColor.R );
128   Storable::DataToStream( theStr, "myColor.G", myColor.G );
129   Storable::DataToStream( theStr, "myColor.B", myColor.B );
130 }
131
132
133 VISU::DeformedShape_i::~DeformedShape_i(){
134   if(MYDEBUG) MESSAGE("DeformedShape_i::~DeformedShape_i()");
135 }
136
137
138 void VISU::DeformedShape_i::SetScale(CORBA::Double theScale) { 
139   myDeformedShapePL->SetScale(theScale);
140 }
141 CORBA::Double VISU::DeformedShape_i::GetScale(){
142   return myDeformedShapePL->GetScale();
143 }
144
145
146 void VISU::DeformedShape_i::DoHook(){
147   if(!myPipeLine) myPipeLine = VISU_DeformedShapePL::New();
148   myDeformedShapePL = dynamic_cast<VISU_DeformedShapePL*>(myPipeLine);
149   myPipeLine->GetMapper()->SetScalarVisibility(IsColored());
150
151   ScalarMap_i::DoHook();
152 }
153
154
155 void VISU::DeformedShape_i::SetMapScale(double theMapScale){
156   myDeformedShapePL->SetMapScale(theMapScale);
157 }
158
159
160 VISU_Actor* VISU::DeformedShape_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO) 
161 {
162   VISU_Actor* anActor = VISU::ScalarMap_i::CreateActor(theIO);
163   anActor->SetRepresentation(1);
164   anActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
165   return anActor;
166 }
167
168
169 void VISU::DeformedShape_i::UpdateActor(VISU_Actor* theActor) {
170   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
171     if(IsColored()){
172       anActor->SetBarVisibility(true);
173       myPipeLine->GetMapper()->SetScalarVisibility(1);
174     }else{
175       anActor->SetBarVisibility(false);
176       myPipeLine->GetMapper()->SetScalarVisibility(0);
177       anActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
178     }
179     VISU::ScalarMap_i::UpdateActor(theActor);
180   }
181 }