Salome HOME
Merge from V5_1_main 14/05/2010
[modules/visu.git] / src / VISU_I / VISU_Mesh_i.cc
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  VISU OBJECT : interactive object for VISU entities implementation
24 //  File   : VISU_PrsObject_i.cxx
25 //  Author : Alexey PETROV
26 //  Module : VISU
27 //
28 #include "VISU_Mesh_i.hh"
29 #include "VISU_Prs3dUtils.hh"
30
31 #include "VISU_Result_i.hh"
32
33 #include "VISU_MeshAct.h"
34 #include "VISU_MeshPL.hxx"
35 #include "VISU_PipeLineUtils.hxx"
36 #include "VISU_Convertor.hxx"
37
38 #include "SALOME_Event.h"
39
40 #include "SUIT_ResourceMgr.h"
41
42 #ifdef _DEBUG_
43 static int MYDEBUG = 0;
44 #else
45 static int MYDEBUG = 0;
46 #endif
47
48 static int INCMEMORY = 4;
49
50 using namespace std;
51
52 //----------------------------------------------------------------------------
53 namespace
54 {
55   bool
56   IsSameColor(const SALOMEDS::Color& theLeft,
57               const SALOMEDS::Color& theRight)
58   {
59     return 
60       VISU::CheckIsSameValue(theLeft.R, theRight.R) &&
61       VISU::CheckIsSameValue(theLeft.G, theRight.G) &&
62       VISU::CheckIsSameValue(theLeft.B, theRight.B);
63   }
64 }
65
66 //----------------------------------------------------------------------------
67 int VISU::Mesh_i::myNbPresent = 0;
68
69 //----------------------------------------------------------------------------
70 QString
71 VISU::Mesh_i
72 ::GenerateName() 
73
74   return VISU::GenerateName("Mesh", myNbPresent++);
75 }
76
77 //----------------------------------------------------------------------------
78 const string VISU::Mesh_i::myComment = "MESH";
79
80 //----------------------------------------------------------------------------
81 const char* 
82 VISU::Mesh_i
83 ::GetComment() const
84
85   return myComment.c_str();
86 }
87
88 //----------------------------------------------------------------------------
89 VISU::Mesh_i
90 ::Mesh_i():
91   myMeshPL(VISU_MeshPL::New())
92 {
93   SetPipeLine(myMeshPL);
94 }
95
96
97 //----------------------------------------------------------------------------
98 size_t
99 VISU::Mesh_i
100 ::IsPossible(Result_i* theResult, 
101              const std::string& theMeshName,
102              Entity theEntity, 
103              const std::string& theFamilyName)
104 {
105   try{
106     MESSAGE("Mesh_i::IsPossible - theMeshName = '"<<theMeshName<<"'; theEntity = "<<theEntity<<"; theFamilyName = '"<<theFamilyName<<"'");
107     size_t aSize = INCMEMORY*
108       theResult->GetInput()->GetMeshOnEntitySize(theMeshName,(VISU::TEntity)theEntity);
109     size_t aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
110     MESSAGE("Mesh_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<aResult);
111     return aResult;
112   }catch(std::exception& exc){
113     INFOS("Follow exception was occured :\n"<<exc.what());
114   }catch(...){
115     INFOS("Unknown exception was occured!");
116   }
117   return 0;
118 }
119
120
121 //----------------------------------------------------------------------------
122 VISU::Storable* 
123 VISU::Mesh_i
124 ::Create(VISU::Result_i* theResult, 
125          const std::string& theMeshName, 
126          VISU::Entity theEntity, 
127          const std::string& theFamilyName)
128 {
129   SetCResult(theResult);
130   SetMeshName(theMeshName.c_str());
131   myEntity = int(theEntity);//jfa IPAL9284
132   mySubMeshName = theFamilyName;
133   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
134   myIsShrank = aResourceMgr->booleanValue("VISU", "mesh_shrink", false);
135   return Build(false);
136 }
137
138
139 //----------------------------------------------------------------------------
140 size_t 
141 VISU::Mesh_i
142 ::IsPossible(Result_i* theResult, 
143              const std::string& theMeshName, 
144              const std::string& theGroupName)
145 {
146   try{
147     MESSAGE("Mesh_i::IsPossible - theMeshName = '"<<theMeshName<<"'; theGroupName = '"<<theGroupName<<"'");
148     size_t aSize = INCMEMORY*
149       theResult->GetInput()->GetMeshOnGroupSize(theMeshName,theGroupName);
150     size_t aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
151     INFOS("Mesh_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<aResult);
152     return aResult;
153   }catch(std::exception& exc){
154     INFOS("Follow exception was occured :\n"<<exc.what());
155   }catch(...){
156     INFOS("Unknown exception was occured!");
157   }
158   return 0;
159 }
160
161
162 //----------------------------------------------------------------------------
163 VISU::Storable* 
164 VISU::Mesh_i
165 ::Create(VISU::Result_i* theResult, 
166          const std::string& theMeshName, 
167          const std::string& theGroupName)
168 {
169   SetCResult(theResult);
170   SetMeshName(theMeshName.c_str());
171   myEntity = -1;//jfa IPAL9284
172   mySubMeshName = theGroupName;
173   return Build(false);
174 }
175
176
177 //----------------------------------------------------------------------------
178 VISU::Mesh_i
179 ::~Mesh_i()
180 {
181   if(MYDEBUG) MESSAGE("Mesh_i::~Mesh_i()");
182 }
183
184
185 //----------------------------------------------------------------------------
186 VISU::Storable* 
187 VISU::Mesh_i
188 ::Build(int theRestoring)
189 {
190   SALOMEDS::StudyBuilder_var aStudyBuilder = GetStudyDocument()->NewBuilder();
191   aStudyBuilder->NewCommand();  // There is a transaction
192   try {
193     if (GetCResult()->GetInput() == NULL)
194       throw std::runtime_error("Mesh_i::Build - GetCResult()->GetInput() == NULL !!!");
195     if (!theRestoring) {
196       myPresentType =  VISU::PresentationType(VISU::GetResourceMgr()->integerValue("VISU" , "mesh_represent", 2));
197       myIsShrank = (myPresentType == VISU::SHRINK);
198
199       if(myEntity == VISU::NODE_ENTITY)
200         myPresentType = VISU::POINT;
201       SetName((const char*)GenerateName().toLatin1(), false);
202       myCellColor.R = 0.0;  myCellColor.G = myCellColor.B = 1.0;
203       myNodeColor.R = myNodeColor.G = 1.0;  myNodeColor.B = 1.0;
204
205       QColor aColor = VISU::GetResourceMgr()->colorValue( "VISU", "edge_color", QColor( 255, 255, 255 ) );
206       SALOMEDS::Color aLinkColor;
207       aLinkColor.R = aColor.red()/255.;
208       aLinkColor.G = aColor.green()/255.;
209       aLinkColor.B = aColor.blue()/255.;
210       SetLinkColor(aLinkColor);
211
212       my2DQuadPrsType = VISU::Quadratic2DPresentationType(VISU::GetResourceMgr()->integerValue( "VISU", "quadratic_mode", 0));
213     }
214
215     if(myEntity >= 0)
216       if(mySubMeshName == "")
217         myType = VISU::TENTITY;
218       else
219         myType = VISU::TFAMILY;
220     else
221       myType = VISU::TGROUP;
222
223     if(MYDEBUG) MESSAGE("Mesh_i::Build - myType = "<<myType);
224     QString aComment,aTmp;
225
226     std::string aMeshName = GetCMeshName();
227     VISU::TEntity anEntity = VISU::TEntity(myEntity);
228
229     VISU::Storable::TRestoringMap aRestoringMap;
230     aRestoringMap["myMeshName"] = GetCMeshName().c_str();
231
232     VISU::PUnstructuredGridIDMapper anIDMapper;
233     VISU::Result_i::PInput anInput = GetCResult()->GetInput();
234     switch(myType){
235     case VISU::TENTITY :
236       anIDMapper = anInput->GetMeshOnEntity(aMeshName,
237                                             anEntity);//jfa IPAL9284
238       aRestoringMap["myComment"] = "ENTITY";
239       aRestoringMap["myId"] = QString::number(anEntity);
240       break;
241     case VISU::TFAMILY :
242       anIDMapper = anInput->GetFamilyOnEntity(aMeshName,
243                                               anEntity,
244                                               mySubMeshName);
245       aRestoringMap["myComment"] = "FAMILY";
246       aRestoringMap["myEntityId"] = QString::number(anEntity);
247       aRestoringMap["myName"] = mySubMeshName.c_str();
248       break;
249     case VISU::TGROUP :
250       anIDMapper = anInput->GetMeshOnGroup(GetCMeshName(),
251                                            mySubMeshName);
252       aRestoringMap["myComment"] = "GROUP";
253       aRestoringMap["myName"] = mySubMeshName.c_str();
254       break;
255     }
256     if(!anIDMapper) 
257       throw std::runtime_error("Mesh_i::Build - !anIDMapper !!!");
258
259     myMeshPL->SetUnstructuredGridIDMapper(anIDMapper);
260
261     string anEntry = GetCResult()->GetEntry(aRestoringMap);
262     if(anEntry == "") 
263       throw std::runtime_error("Mesh_i::Build - anEntry was not found !!!");
264     SALOMEDS::SObject_var aSObject = GetStudyDocument()->FindObjectID(anEntry.c_str());
265
266     if(!theRestoring) { //Setting IOR on the label
267       myMeshPL->Init();
268       string aResultEntry = GetCResult()->GetEntry();
269       SALOMEDS::StudyBuilder_var aStudyBuilder = GetStudyDocument()->NewBuilder();
270       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
271       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
272       CORBA::String_var aString = GetID();
273       anIOR->SetValue(aString);
274     }
275
276     // Set icon
277     SALOMEDS::GenericAttribute_var anAttr;
278     SALOMEDS::AttributePixMap_var  aPixmap;
279     
280     anAttr  = aStudyBuilder->FindOrCreateAttribute(aSObject, "AttributePixMap");
281     aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
282     aPixmap->SetPixMap("ICON_TREE_MESH");
283
284   }catch(std::exception& exc){
285     INFOS("Follow exception was occured :\n"<<exc.what());
286     return NULL;
287   }catch(...){
288     INFOS("Unknown exception was occured!");
289     return NULL;
290   }
291   aStudyBuilder->CommitCommand();
292   return this;
293 }
294
295
296 //---------------------------------------------------------------
297 void
298 VISU::Mesh_i
299 ::SameAs(const VISU::Prs3d_i* theOrigin)
300 {
301   return; // "SameAs" command for mesh is not valid in current architecture
302 }
303
304
305 //---------------------------------------------------------------
306 void
307 VISU::Mesh_i
308 ::RemoveFromStudy()
309 {
310   struct TRemoveFromStudy: public SALOME_Event
311   {
312     VISU::Mesh_i* myRemovable;
313     TRemoveFromStudy(VISU::Mesh_i* theRemovable):
314       myRemovable(theRemovable)
315     {}
316     
317     virtual
318     void
319     Execute()
320     {
321       VISU::RemoveFromStudy(myRemovable->GetSObject());
322       myRemovable->TSuperClass::RemoveFromStudy();
323     }
324   };
325
326   // Unset icon
327   SALOMEDS::StudyBuilder_var aStudyBuilder = GetStudyDocument()->NewBuilder();
328   aStudyBuilder->RemoveAttribute(GetSObject(), "AttributePixMap");
329
330   ProcessVoidEvent(new TRemoveFromStudy(this));
331 }
332
333
334 //----------------------------------------------------------------------------
335 void 
336 VISU::Mesh_i
337 ::SetCellColor(const SALOMEDS::Color& theColor) 
338
339   if(IsSameColor(myCellColor, theColor))
340     return;
341
342   VISU::TSetModified aModified(this);
343   
344   myCellColor = theColor;
345   myParamsTime.Modified();
346 }
347
348
349 //----------------------------------------------------------------------------
350 SALOMEDS::Color 
351 VISU::Mesh_i
352 ::GetCellColor() 
353
354   return myCellColor;
355 }
356
357
358 //----------------------------------------------------------------------------
359 void 
360 VISU::Mesh_i
361 ::SetNodeColor(const SALOMEDS::Color& theColor) 
362
363   if(IsSameColor(myNodeColor, theColor))
364     return;
365
366   VISU::TSetModified aModified(this);
367   
368   myNodeColor = theColor;
369   myParamsTime.Modified();
370 }
371
372
373 //----------------------------------------------------------------------------
374 SALOMEDS::Color 
375 VISU::Mesh_i
376 ::GetNodeColor() 
377
378   return myNodeColor;
379 }
380
381
382 //----------------------------------------------------------------------------
383 void 
384 VISU::Mesh_i
385 ::SetLinkColor(const SALOMEDS::Color& theColor) 
386
387   if(IsSameColor(myLinkColor, theColor))
388     return;
389
390   VISU::TSetModified aModified(this);
391   
392   myLinkColor = theColor;
393   myParamsTime.Modified();
394 }
395
396
397 //----------------------------------------------------------------------------
398 SALOMEDS::Color 
399 VISU::Mesh_i
400 ::GetLinkColor() 
401
402   return myLinkColor;
403 }
404
405
406 //----------------------------------------------------------------------------
407 void 
408 VISU::Mesh_i
409 ::SetPresentationType(VISU::PresentationType theType) 
410
411   if(myPresentType == theType)
412     return;
413
414   VISU::TSetModified aModified(this);
415   if (theType == VISU::SHRINK) {
416     myIsShrank = true;
417   }
418   myPresentType = theType;
419   myParamsTime.Modified();
420 }
421
422
423 void
424 VISU::Mesh_i
425 ::SetQuadratic2DPresentationType(VISU::Quadratic2DPresentationType theType)
426 {
427   if(my2DQuadPrsType == theType)
428     return;
429   
430   VISU::TSetModified aModified(this);
431   my2DQuadPrsType = theType;
432   myParamsTime.Modified();
433 }
434
435
436 //----------------------------------------------------------------------------
437 VISU::PresentationType 
438 VISU::Mesh_i
439 ::GetPresentationType() 
440
441   return myPresentType;
442 }
443
444
445 //----------------------------------------------------------------------------
446 VISU::Quadratic2DPresentationType
447 VISU::Mesh_i
448 ::GetQuadratic2DPresentationType()
449 {
450   return my2DQuadPrsType;
451 }
452
453
454
455
456 //----------------------------------------------------------------------------
457 VISU::Entity 
458 VISU::Mesh_i
459 ::GetEntity() const
460
461   return VISU::Entity(myEntity);
462 }
463
464
465 //----------------------------------------------------------------------------
466 const std::string&
467 VISU::Mesh_i
468 ::GetSubMeshName() const
469
470   return mySubMeshName;
471 }
472
473
474 //----------------------------------------------------------------------------
475 VISU::Storable* 
476 VISU::Mesh_i
477 ::Restore(SALOMEDS::SObject_ptr theSObject,
478           const Storable::TRestoringMap& theMap)
479 {
480   if(!TSuperClass::Restore(theSObject, theMap))
481     return NULL;
482
483   myEntity = VISU::Storable::FindValue(theMap,"myEntity").toInt();//jfa IPAL9284
484   mySubMeshName = (const char*)VISU::Storable::FindValue(theMap,"mySubMeshName").toLatin1();
485
486   myPresentType = VISU::PresentationType(VISU::Storable::FindValue(theMap,"myPresentType").toInt());
487
488   my2DQuadPrsType = VISU::Quadratic2DPresentationType(VISU::Storable::FindValue(theMap,"my2DQuadPrsType").toInt());
489
490   myIsShrank = (VISU::Storable::FindValue(theMap,"myIsShrank", "0").toInt() == 1)? true: false;
491
492   myCellColor.R = VISU::Storable::FindValue(theMap,"myCellColor.R").toDouble();
493   myCellColor.G = VISU::Storable::FindValue(theMap,"myCellColor.G").toDouble();
494   myCellColor.B = VISU::Storable::FindValue(theMap,"myCellColor.B").toDouble();
495
496   myNodeColor.R = VISU::Storable::FindValue(theMap,"myNodeColor.R").toDouble();
497   myNodeColor.G = VISU::Storable::FindValue(theMap,"myNodeColor.G").toDouble();
498   myNodeColor.B = VISU::Storable::FindValue(theMap,"myNodeColor.B").toDouble();
499
500   myLinkColor.R = VISU::Storable::FindValue(theMap,"myLinkColor.R").toDouble();
501   myLinkColor.G = VISU::Storable::FindValue(theMap,"myLinkColor.G").toDouble();
502   myLinkColor.B = VISU::Storable::FindValue(theMap,"myLinkColor.B").toDouble();
503
504   return Build(true);
505 }
506
507
508 //----------------------------------------------------------------------------
509 VISU::Storable*
510 VISU::Mesh_i
511 ::StorableEngine(SALOMEDS::SObject_ptr theSObject,
512                  const Storable::TRestoringMap& theMap,
513                  const std::string& thePrefix,
514                  CORBA::Boolean theIsMultiFile)
515 {
516   Mesh_i* aMesh = new Mesh_i();
517   return aMesh->Restore(theSObject, theMap);
518 }
519
520
521 //----------------------------------------------------------------------------
522 void 
523 VISU::Mesh_i
524 ::ToStream(std::ostringstream& theStr)
525 {
526   TSuperClass::ToStream(theStr);
527
528   Storable::DataToStream( theStr, "myEntity", myEntity );
529   Storable::DataToStream( theStr, "mySubMeshName", mySubMeshName.c_str() );
530
531   Storable::DataToStream( theStr, "myPresentType", int(myPresentType) );
532
533   Storable::DataToStream( theStr, "my2DQuadPrsType", int(my2DQuadPrsType) );
534
535   Storable::DataToStream( theStr, "myIsShrank",  (myIsShrank? "1":"0"));
536
537   Storable::DataToStream( theStr, "myCellColor.R", myCellColor.R );
538   Storable::DataToStream( theStr, "myCellColor.G", myCellColor.G );
539   Storable::DataToStream( theStr, "myCellColor.B", myCellColor.B );
540
541   Storable::DataToStream( theStr, "myNodeColor.R", myNodeColor.R );
542   Storable::DataToStream( theStr, "myNodeColor.G", myNodeColor.G );
543   Storable::DataToStream( theStr, "myNodeColor.B", myNodeColor.B );
544
545   Storable::DataToStream( theStr, "myLinkColor.R", myLinkColor.R );
546   Storable::DataToStream( theStr, "myLinkColor.G", myLinkColor.G );
547   Storable::DataToStream( theStr, "myLinkColor.B", myLinkColor.B );
548 }
549
550
551 //----------------------------------------------------------------------------
552 VISU_Actor* 
553 VISU::Mesh_i
554 ::CreateActor()
555 {
556   VISU_MeshAct* anActor = VISU_MeshAct::New();
557   try{
558     VISU::Prs3d_i::CreateActor(anActor);
559     SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
560     //bool toShrink  = aResourceMgr->booleanValue("VISU", "mesh_shrink", false);
561     //if (toShrink) anActor->SetShrink();
562     if (anActor->IsShrunkable()) {
563       if (myIsShrank)
564         anActor->SetShrink();
565       else
566         anActor->UnShrink();
567     }
568     anActor->SetFeatureEdgesAngle( aResourceMgr->doubleValue("VISU", "feature_edges_angle", 0.0) );
569     anActor->SetFeatureEdgesFlags( aResourceMgr->booleanValue("VISU", "show_feature_edges", false),
570                                    aResourceMgr->booleanValue("VISU", "show_boundary_edges", false),
571                                    aResourceMgr->booleanValue("VISU", "show_manifold_edges", false),
572                                    aResourceMgr->booleanValue("VISU", "show_non_manifold_edges", false) );
573     anActor->SetFeatureEdgesColoring( aResourceMgr->booleanValue("VISU", "feature_edges_coloring", false) );
574
575     if (myType == VISU::TGROUP )
576     {
577       anActor->SetIsDisplayNameActor( true );
578       anActor->SetNameActorText( GetSubMeshName().c_str() );
579     }
580
581     UpdateActor(anActor);
582   }catch (...) {
583     anActor->Delete();
584     throw ;
585   }
586   return anActor;
587 }
588
589
590 //----------------------------------------------------------------------------
591 void
592 VISU::Mesh_i
593 ::UpdateActor(VISU_ActorBase* theActor) 
594 {
595   if(VISU_MeshAct* anActor = dynamic_cast<VISU_MeshAct*>(theActor)){
596     if(MYDEBUG) MESSAGE("Mesh_i::UpdateActor");
597     VISU::Prs3d_i::UpdateActor(anActor);
598     anActor->SetRepresentation(myPresentType);
599     if (anActor->IsShrunkable()) {
600       if (myIsShrank)
601         anActor->SetShrink();
602       else
603         anActor->UnShrink();
604     }
605     if(my2DQuadPrsType == VISU::LINES)
606       anActor->SetQuadratic2DRepresentation(VISU_Actor::eLines);
607     else{
608       anActor->SetQuadratic2DRepresentation(VISU_Actor::eArcs);
609     }
610     anActor->GetSurfaceProperty()->SetColor(myCellColor.R, myCellColor.G, myCellColor.B);
611     anActor->GetEdgeProperty()->SetColor(myLinkColor.R, myLinkColor.G, myLinkColor.B);
612     anActor->GetNodeProperty()->SetColor(myNodeColor.R, myNodeColor.G, myNodeColor.B);
613   }
614 }
615
616 //----------------------------------------------------------------------------
617
618
619 void VISU::Mesh_i::SetShrink(CORBA::Boolean toShrink)
620 {
621   if(toShrink == myIsShrank)
622     return;
623   VISU::TSetModified aModified(this);
624   myIsShrank = toShrink;
625   myParamsTime.Modified();
626 }
627
628 //----------------------------------------------------------------------------
629 CORBA::Boolean VISU::Mesh_i::IsShrank()
630 {
631   return myIsShrank;
632 }
633