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