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