Salome HOME
Update copyright information
[modules/visu.git] / src / VISU_I / VISU_DeformedShapeAndScalarMap_i.cc
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  VISU OBJECT : interactive object for VISU entities implementation
21 //  File   : VISU_DeformedShapeAndScalarMap_i.cc
22 //  Author : Eugeny Nikolaev
23 //  Module : VISU
24 //
25 #include "VISU_DeformedShapeAndScalarMap_i.hh"
26 #include "VISU_Prs3dUtils.hh"
27 #include "VISU_Result_i.hh"
28
29 #include "VISU_ScalarMapAct.h"
30 #include "VISU_DeformedShapeAndScalarMapPL.hxx"
31 #include "VISU_Convertor.hxx"
32
33 #include "SUIT_ResourceMgr.h"
34 #include "SALOME_Event.h"
35
36 #include <vtkUnstructuredGrid.h>
37 #include <vtkProperty.h>
38 #include <vtkMapper.h>
39
40 #ifdef _DEBUG_
41 static int MYDEBUG = 0;
42 #else
43 static int MYDEBUG = 0;
44 #endif
45
46 static int INCMEMORY = 4+12;
47
48 using namespace std;
49
50 //---------------------------------------------------------------
51 size_t
52 VISU::DeformedShapeAndScalarMap_i
53 ::IsPossible(Result_i* theResult, 
54              const std::string& theMeshName, 
55              VISU::Entity theEntity,
56              const std::string& theFieldName, 
57              CORBA::Long theTimeStampNumber,
58              bool theIsMemoryCheck)
59 {
60   size_t aResult = 0;
61   try{
62     aResult = TSuperClass::IsPossible(theResult,
63                                       theMeshName,
64                                       theEntity,
65                                       theFieldName,
66                                       theTimeStampNumber,
67                                       false);
68     if(theIsMemoryCheck && aResult){
69       VISU::Result_i::PInput anInput = theResult->GetInput(theMeshName,
70                                                            theEntity,
71                                                            theFieldName,
72                                                            theTimeStampNumber);
73       VISU::PField aField = anInput->GetField(theMeshName,
74                                               (VISU::TEntity)theEntity,
75                                               theFieldName);
76       if(aField->myNbComp <= 1)
77         return 0;
78
79       bool anIsEstimated = true;
80       size_t aSize = anInput->GetTimeStampOnMeshSize(theMeshName,
81                                                      (VISU::TEntity)theEntity,
82                                                      theFieldName,
83                                                      theTimeStampNumber,
84                                                      anIsEstimated);
85       if(anIsEstimated)
86         aSize *= INCMEMORY;
87       aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
88       if(MYDEBUG) 
89         MESSAGE("DeformedShapeAndScalarMap_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<aResult);
90     }
91   }catch(std::exception& exc){
92     INFOS("Follow exception was occured :\n"<<exc.what());
93   }catch(...){
94     INFOS("Unknown exception was occured!");
95   }
96   return aResult;
97 }
98
99 //---------------------------------------------------------------
100 int VISU::DeformedShapeAndScalarMap_i::myNbPresent = 0;
101
102 //---------------------------------------------------------------
103 QString
104 VISU::DeformedShapeAndScalarMap_i
105 ::GenerateName() 
106 {
107   return VISU::GenerateName("ScalarDef.Shape",myNbPresent++);
108 }
109
110 //---------------------------------------------------------------
111 const string VISU::DeformedShapeAndScalarMap_i::myComment = "SCALARMAPONDEFORMEDSHAPE"; // It is obsolete. Use "DEFORMEDSHAPEANDSCALARMAP" instead.
112
113 //---------------------------------------------------------------
114 const char* 
115 VISU::DeformedShapeAndScalarMap_i
116 ::GetComment() const
117
118   return myComment.c_str();
119 }
120
121 //----------------------------------------------------------------------------
122 const char*
123 VISU::DeformedShapeAndScalarMap_i
124 ::GetIconName()
125 {
126   if (!IsGroupsUsed())
127     return "ICON_TREE_SCALAR_MAP_ON_DEFORMED_SHAPE";
128   else
129     return "ICON_TREE_SCALAR_MAP_ON_DEFORMED_SHAPE_GROUPS";
130 }
131
132 //---------------------------------------------------------------
133 VISU::DeformedShapeAndScalarMap_i
134 ::DeformedShapeAndScalarMap_i(EPublishInStudyMode thePublishInStudyMode) :
135   ColoredPrs3d_i(thePublishInStudyMode),
136   ScalarMap_i(thePublishInStudyMode),
137   myDeformedShapeAndScalarMapPL(NULL),
138   myScalarTimeStampNumber(1)
139 {}
140
141
142 //---------------------------------------------------------------
143 VISU::Storable* 
144 VISU::DeformedShapeAndScalarMap_i
145 ::Create(const std::string& theMeshName, 
146          VISU::Entity theEntity,
147          const std::string& theFieldName, 
148          CORBA::Long theTimeStampNumber)
149 {
150   TSuperClass::Create(theMeshName,
151                              theEntity,
152                              theFieldName,
153                              theTimeStampNumber);
154   SetScalarField(theEntity,
155                  theFieldName.c_str(),
156                  theTimeStampNumber);
157   return this;
158 }
159
160
161 //---------------------------------------------------------------
162 VISU::Storable*
163 VISU::DeformedShapeAndScalarMap_i
164 ::Restore(SALOMEDS::SObject_ptr theSObject,
165           const Storable::TRestoringMap& theMap)
166 {
167   if(!TSuperClass::Restore(theSObject, theMap))
168     return NULL;
169
170   QString aMeshName = VISU::Storable::FindValue(theMap,"myScalarMeshName");
171   VISU::Entity anEntity = VISU::Entity(VISU::Storable::FindValue(theMap,"myScalarEntity").toInt());
172
173   QString aFieldName = VISU::Storable::FindValue(theMap,"myScalarFieldName");
174   int aTimeStampNumber = VISU::Storable::FindValue(theMap,"myScalarIteration").toInt();
175
176   SetScalarField(anEntity,
177                  aFieldName.toLatin1().constData(),
178                  aTimeStampNumber);
179
180   SetScale(VISU::Storable::FindValue(theMap,"myFactor").toDouble());
181   
182   return this;
183 }
184
185
186 //---------------------------------------------------------------
187 void
188 VISU::DeformedShapeAndScalarMap_i
189 ::ToStream(std::ostringstream& theStr)
190 {
191   TSuperClass::ToStream(theStr);
192   
193   Storable::DataToStream( theStr, "myScalarEntity",    int(myScalarEntity));
194   Storable::DataToStream( theStr, "myScalarFieldName", myScalarFieldName.c_str());
195   Storable::DataToStream( theStr, "myScalarIteration", int(myScalarTimeStampNumber));
196
197   Storable::DataToStream( theStr, "myFactor", GetScale() );
198 }
199
200
201 //---------------------------------------------------------------
202 VISU::DeformedShapeAndScalarMap_i
203 ::~DeformedShapeAndScalarMap_i()
204 {
205   if(MYDEBUG) MESSAGE("DeformedShapeAndScalarMap_i::~DeformedShapeAndScalarMap_i()");
206 }
207
208
209 //---------------------------------------------------------------
210 void
211 VISU::DeformedShapeAndScalarMap_i
212 ::SameAs(const Prs3d_i* theOrigin)
213 {
214   TSuperClass::SameAs(theOrigin);
215   
216   if(const DeformedShapeAndScalarMap_i* aPrs3d = dynamic_cast<const DeformedShapeAndScalarMap_i*>(theOrigin)){
217     DeformedShapeAndScalarMap_i* anOrigin = const_cast<DeformedShapeAndScalarMap_i*>(aPrs3d);
218     CORBA::String_var aFieldName = anOrigin->GetScalarFieldName();
219     
220     SetScalarField(anOrigin->GetScalarEntity(),
221                    aFieldName,
222                    anOrigin->GetScalarTimeStampNumber());
223     SetTitle(anOrigin->GetTitle()); //fix of 20094 issue
224     SetScalarMode(anOrigin->GetScalarMode()); //
225     if(!IsRangeFixed() && IsPipeLineExists()) //fix of 20107 issue
226       SetSourceRange();                       //
227     Update();
228   }
229 }
230
231
232 //---------------------------------------------------------------
233 void
234 VISU::DeformedShapeAndScalarMap_i
235 ::SetScale(CORBA::Double theScale) 
236
237   VISU::TSetModified aModified(this);
238
239   ProcessVoidEvent(new TVoidMemFun1ArgEvent<VISU_DeformedShapeAndScalarMapPL, vtkFloatingPointType>
240                    (GetSpecificPL(), &VISU_DeformedShapeAndScalarMapPL::SetScale, theScale));
241 }
242
243
244 //---------------------------------------------------------------
245 CORBA::Double
246 VISU::DeformedShapeAndScalarMap_i
247 ::GetScale()
248 {
249   return myDeformedShapeAndScalarMapPL->GetScale();
250 }
251
252
253 //---------------------------------------------------------------
254 void
255 VISU::DeformedShapeAndScalarMap_i
256 ::CreatePipeLine(VISU_PipeLine* thePipeLine)
257 {
258   if(!thePipeLine){
259     myDeformedShapeAndScalarMapPL = VISU_DeformedShapeAndScalarMapPL::New();
260   }else
261     myDeformedShapeAndScalarMapPL = dynamic_cast<VISU_DeformedShapeAndScalarMapPL*>(thePipeLine);
262
263   myDeformedShapeAndScalarMapPL->GetMapper()->SetScalarVisibility(1);
264
265   TSuperClass::CreatePipeLine(myDeformedShapeAndScalarMapPL);
266 }
267
268
269 //---------------------------------------------------------------
270 bool
271 VISU::DeformedShapeAndScalarMap_i
272 ::CheckIsPossible() 
273 {
274   return IsPossible(GetCResult(),GetCMeshName(),GetEntity(),GetCFieldName(),GetTimeStampNumber(),true);
275 }
276
277
278 //---------------------------------------------------------------
279 VISU_Actor* 
280 VISU::DeformedShapeAndScalarMap_i
281 ::CreateActor() 
282 {
283   VISU_Actor* anActor = TSuperClass::CreateActor(true);
284   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
285   int  aDispMode = aResourceMgr->integerValue("VISU", "scalar_def_represent", 2);
286   bool toShrink  = aResourceMgr->booleanValue("VISU", "scalar_def_shrink", false);
287   anActor->SetRepresentation(aDispMode);
288   if (toShrink) 
289     anActor->SetShrink();
290   anActor->SetVTKMapping(false);
291   return anActor;
292 }
293
294
295 //---------------------------------------------------------------
296 void
297 VISU::DeformedShapeAndScalarMap_i
298 ::UpdateActor(VISU_Actor* theActor) 
299 {
300   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
301     anActor->SetBarVisibility(true);
302     GetPipeLine()->GetMapper()->SetScalarVisibility(1);
303   }
304   TSuperClass::UpdateActor(theActor);
305 }
306
307
308 //---------------------------------------------------------------
309 void
310 VISU::DeformedShapeAndScalarMap_i
311 ::SetField(VISU::PField theField)
312 {
313   TSuperClass::SetField(theField);
314   
315   if(!myScalarField)
316     myScalarField = theField;
317 }
318
319
320 //---------------------------------------------------------------
321 void
322 VISU::DeformedShapeAndScalarMap_i
323 ::SetScalarField(VISU::Entity theEntity,
324                  const char* theFieldName,
325                  CORBA::Long theTimeStampNumber)
326 {
327   bool anIsModified = false;
328
329   if(!anIsModified)
330     anIsModified |= myScalarEntity != theEntity;
331
332   if(!anIsModified)
333     anIsModified |= myScalarFieldName != theFieldName;
334
335   if(!anIsModified)
336     anIsModified |= myScalarTimeStampNumber != theTimeStampNumber;
337   
338   if(!anIsModified)
339     return;
340
341   VISU::TSetModified aModified(this);
342
343   VISU::TEntity aEntity = VISU::TEntity(theEntity);
344   VISU::Result_i::PInput anInput = GetCResult()->GetInput(GetCMeshName(),
345                                                           theEntity,
346                                                           theFieldName,
347                                                           theTimeStampNumber);
348   myScalarField = anInput->GetField(GetCMeshName(), aEntity, theFieldName);
349
350   VISU::PUnstructuredGridIDMapper anIDMapper = 
351     anInput->GetTimeStampOnMesh(GetCMeshName(),
352                                 aEntity,
353                                 theFieldName,
354                                 theTimeStampNumber);
355
356   vtkUnstructuredGrid* anOutput = anIDMapper->GetUnstructuredGridOutput();
357   if(myDeformedShapeAndScalarMapPL && anOutput)
358     ProcessVoidEvent(new TVoidMemFun1ArgEvent<VISU_DeformedShapeAndScalarMapPL, vtkDataSet*>
359                      (GetSpecificPL(), &VISU_DeformedShapeAndScalarMapPL::SetScalars, anOutput));
360   
361   myScalarEntity = theEntity;
362   myScalarFieldName = theFieldName;
363   myScalarTimeStampNumber = theTimeStampNumber;
364
365   SetTitle(theFieldName);
366   SetScalarMode(GetScalarMode());
367   if(!IsRangeFixed() && IsPipeLineExists())
368     SetSourceRange();
369
370   myParamsTime.Modified();
371 }
372
373
374 //---------------------------------------------------------------
375 VISU::Entity 
376 VISU::DeformedShapeAndScalarMap_i
377 ::GetScalarEntity()
378 {
379   return myScalarEntity;
380 }
381
382
383 //---------------------------------------------------------------
384 char* 
385 VISU::DeformedShapeAndScalarMap_i
386 ::GetScalarFieldName()
387 {
388   return CORBA::string_dup(myScalarFieldName.c_str());
389 }
390
391
392 //---------------------------------------------------------------
393 CORBA::Long 
394 VISU::DeformedShapeAndScalarMap_i
395 ::GetScalarTimeStampNumber()
396 {
397   return myScalarTimeStampNumber;
398 }
399
400
401 //---------------------------------------------------------------
402 VISU::PField
403 VISU::DeformedShapeAndScalarMap_i
404 ::GetScalarField()
405 {
406   return myScalarField;
407 }