]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Mesh_i.cc
Salome HOME
6b9f866078d5d5492b0345dbd6013c52f94ce601
[modules/visu.git] / src / VISU_I / VISU_Mesh_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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
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.hxx"
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 //----------------------------------------------------------------------------
50 namespace
51 {
52   bool
53   IsSameColor(const SALOMEDS::Color& theLeft,
54               const SALOMEDS::Color& theRight)
55   {
56     return 
57       VISU::CheckIsSameValue(theLeft.R, theRight.R) &&
58       VISU::CheckIsSameValue(theLeft.G, theRight.G) &&
59       VISU::CheckIsSameValue(theLeft.B, theRight.B);
60   }
61 }
62
63 //----------------------------------------------------------------------------
64 int VISU::Mesh_i::myNbPresent = 0;
65
66 //----------------------------------------------------------------------------
67 QString
68 VISU::Mesh_i
69 ::GenerateName() 
70
71   return VISU::GenerateName("Mesh", myNbPresent++);
72 }
73
74 //----------------------------------------------------------------------------
75 const string VISU::Mesh_i::myComment = "MESH";
76
77 //----------------------------------------------------------------------------
78 const char* 
79 VISU::Mesh_i
80 ::GetComment() const
81
82   return myComment.c_str();
83 }
84
85 //----------------------------------------------------------------------------
86 VISU::Mesh_i
87 ::Mesh_i():
88   myMeshPL(VISU_MeshPL::New())
89 {
90   SetPipeLine(myMeshPL);
91 }
92
93
94 //----------------------------------------------------------------------------
95 size_t
96 VISU::Mesh_i
97 ::IsPossible(Result_i* theResult, 
98              const std::string& theMeshName,
99              Entity theEntity, 
100              const std::string& theFamilyName)
101 {
102   try{
103     MESSAGE("Mesh_i::IsPossible - theMeshName = '"<<theMeshName<<"'; theEntity = "<<theEntity<<"; theFamilyName = '"<<theFamilyName<<"'");
104     size_t aSize = INCMEMORY*
105       theResult->GetInput()->GetMeshOnEntitySize(theMeshName,(VISU::TEntity)theEntity);
106     size_t aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
107     MESSAGE("Mesh_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<aResult);
108     return aResult;
109   }catch(std::exception& exc){
110     INFOS("Follow exception was occured :\n"<<exc.what());
111   }catch(...){
112     INFOS("Unknown exception was occured!");
113   }
114   return 0;
115 }
116
117
118 //----------------------------------------------------------------------------
119 VISU::Storable* 
120 VISU::Mesh_i
121 ::Create(VISU::Result_i* theResult, 
122          const std::string& theMeshName, 
123          VISU::Entity theEntity, 
124          const std::string& theFamilyName)
125 {
126   SetCResult(theResult);
127   SetMeshName(theMeshName.c_str());
128   myEntity = int(theEntity);//jfa IPAL9284
129   mySubMeshName = theFamilyName;
130   return Build(false);
131 }
132
133
134 //----------------------------------------------------------------------------
135 size_t 
136 VISU::Mesh_i
137 ::IsPossible(Result_i* theResult, 
138              const std::string& theMeshName, 
139              const std::string& theGroupName)
140 {
141   try{
142     MESSAGE("Mesh_i::IsPossible - theMeshName = '"<<theMeshName<<"'; theGroupName = '"<<theGroupName<<"'");
143     size_t aSize = INCMEMORY*
144       theResult->GetInput()->GetMeshOnGroupSize(theMeshName,theGroupName);
145     size_t aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
146     INFOS("Mesh_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<aResult);
147     return aResult;
148   }catch(std::exception& exc){
149     INFOS("Follow exception was occured :\n"<<exc.what());
150   }catch(...){
151     INFOS("Unknown exception was occured!");
152   }
153   return 0;
154 }
155
156
157 //----------------------------------------------------------------------------
158 VISU::Storable* 
159 VISU::Mesh_i
160 ::Create(VISU::Result_i* theResult, 
161          const std::string& theMeshName, 
162          const std::string& theGroupName)
163 {
164   SetCResult(theResult);
165   SetMeshName(theMeshName.c_str());
166   myEntity = -1;//jfa IPAL9284
167   mySubMeshName = theGroupName;
168   return Build(false);
169 }
170
171
172 //----------------------------------------------------------------------------
173 VISU::Mesh_i
174 ::~Mesh_i()
175 {
176   if(MYDEBUG) MESSAGE("Mesh_i::~Mesh_i()");
177 }
178
179
180 //----------------------------------------------------------------------------
181 VISU::Storable* 
182 VISU::Mesh_i
183 ::Build(int theRestoring)
184 {
185   SALOMEDS::StudyBuilder_var aStudyBuilder = GetStudyDocument()->NewBuilder();
186   aStudyBuilder->NewCommand();  // There is a transaction
187   try {
188     if (GetCResult()->GetInput() == NULL)
189       throw std::runtime_error("Mesh_i::Build - GetCResult()->GetInput() == NULL !!!");
190     if (!theRestoring) {
191       myPresentType =  VISU::PresentationType(VISU::GetResourceMgr()->integerValue("VISU" , "mesh_represent", 2));
192       if(myEntity == VISU::NODE_ENTITY)
193         myPresentType = VISU::POINT;
194       SetName(GenerateName().latin1(), false);
195       myCellColor.R = 0.0;  myCellColor.G = myCellColor.B = 1.0;
196       myNodeColor.R = myNodeColor.G = 1.0;  myNodeColor.B = 1.0;
197       myLinkColor.R = myLinkColor.G = myLinkColor.B = 83/255.;
198     }
199
200     if(myEntity >= 0)
201       if(mySubMeshName == "")
202         myType = VISU::TENTITY;
203       else
204         myType = VISU::TFAMILY;
205     else
206       myType = VISU::TGROUP;
207
208     if(MYDEBUG) MESSAGE("Mesh_i::Build - myType = "<<myType);
209     QString aComment,aTmp;
210
211     std::string aMeshName = GetCMeshName();
212     VISU::TEntity anEntity = VISU::TEntity(myEntity);
213
214     VISU::Storable::TRestoringMap aRestoringMap;
215     aRestoringMap["myMeshName"] = GetCMeshName();
216
217     VISU::PUnstructuredGridIDMapper anIDMapper;
218     VISU::Result_i::PInput anInput = GetCResult()->GetInput();
219     switch(myType){
220     case VISU::TENTITY :
221       anIDMapper = anInput->GetMeshOnEntity(aMeshName,
222                                             anEntity);//jfa IPAL9284
223       aRestoringMap["myComment"] = "ENTITY";
224       aRestoringMap["myId"] = QString::number(anEntity);
225       break;
226     case VISU::TFAMILY :
227       anIDMapper = anInput->GetFamilyOnEntity(aMeshName,
228                                               anEntity,
229                                               mySubMeshName);
230       aRestoringMap["myComment"] = "FAMILY";
231       aRestoringMap["myEntityId"] = QString::number(anEntity);
232       aRestoringMap["myName"] = mySubMeshName;
233       break;
234     case VISU::TGROUP :
235       anIDMapper = anInput->GetMeshOnGroup(GetCMeshName(),
236                                            mySubMeshName);
237       aRestoringMap["myComment"] = "GROUP";
238       aRestoringMap["myName"] = mySubMeshName;
239       break;
240     }
241     if(!anIDMapper) 
242       throw std::runtime_error("Mesh_i::Build - !anIDMapper !!!");
243
244     myMeshPL->SetUnstructuredGridIDMapper(anIDMapper);
245
246     string anEntry = GetCResult()->GetEntry(aRestoringMap);
247     if(anEntry == "") 
248       throw std::runtime_error("Mesh_i::Build - anEntry was not found !!!");
249     SALOMEDS::SObject_var aSObject = GetStudyDocument()->FindObjectID(anEntry.c_str());
250
251     if(!theRestoring) { //Setting IOR on the label
252       myMeshPL->Init();
253       string aResultEntry = GetCResult()->GetEntry();
254       SALOMEDS::StudyBuilder_var aStudyBuilder = GetStudyDocument()->NewBuilder();
255       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
256       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
257       CORBA::String_var aString = GetID();
258       anIOR->SetValue(aString);
259     }
260
261     // Set icon
262     SALOMEDS::GenericAttribute_var anAttr;
263     SALOMEDS::AttributePixMap_var  aPixmap;
264     
265     anAttr  = aStudyBuilder->FindOrCreateAttribute(aSObject, "AttributePixMap");
266     aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
267     aPixmap->SetPixMap("ICON_TREE_MESH");
268
269   }catch(std::exception& exc){
270     INFOS("Follow exception was occured :\n"<<exc.what());
271     return NULL;
272   }catch(...){
273     INFOS("Unknown exception was occured!");
274     return NULL;
275   }
276   aStudyBuilder->CommitCommand();
277   return this;
278 }
279
280
281 //---------------------------------------------------------------
282 void
283 VISU::Mesh_i
284 ::SameAs(const VISU::Prs3d_i* theOrigin)
285 {
286   return; // "SameAs" command for mesh is not valid in current architecture
287 }
288
289
290 //---------------------------------------------------------------
291 void
292 VISU::Mesh_i
293 ::RemoveFromStudy()
294 {
295   struct TRemoveFromStudy: public SALOME_Event
296   {
297     VISU::Mesh_i* myRemovable;
298     TRemoveFromStudy(VISU::Mesh_i* theRemovable):
299       myRemovable(theRemovable)
300     {}
301     
302     virtual
303     void
304     Execute()
305     {
306       VISU::RemoveFromStudy(myRemovable->GetSObject());
307       myRemovable->TSuperClass::RemoveFromStudy();
308     }
309   };
310
311   // Unset icon
312   SALOMEDS::StudyBuilder_var aStudyBuilder = GetStudyDocument()->NewBuilder();
313   aStudyBuilder->RemoveAttribute(GetSObject(), "AttributePixMap");
314
315   ProcessVoidEvent(new TRemoveFromStudy(this));
316 }
317
318
319 //----------------------------------------------------------------------------
320 void 
321 VISU::Mesh_i
322 ::SetCellColor(const SALOMEDS::Color& theColor) 
323
324   if(IsSameColor(myCellColor, theColor))
325     return;
326
327   VISU::TSetModified aModified(this);
328   
329   myCellColor = theColor;
330   myParamsTime.Modified();
331 }
332
333
334 //----------------------------------------------------------------------------
335 SALOMEDS::Color 
336 VISU::Mesh_i
337 ::GetCellColor() 
338
339   return myCellColor;
340 }
341
342
343 //----------------------------------------------------------------------------
344 void 
345 VISU::Mesh_i
346 ::SetNodeColor(const SALOMEDS::Color& theColor) 
347
348   if(IsSameColor(myNodeColor, theColor))
349     return;
350
351   VISU::TSetModified aModified(this);
352   
353   myNodeColor = theColor;
354   myParamsTime.Modified();
355 }
356
357
358 //----------------------------------------------------------------------------
359 SALOMEDS::Color 
360 VISU::Mesh_i
361 ::GetNodeColor() 
362
363   return myNodeColor;
364 }
365
366
367 //----------------------------------------------------------------------------
368 void 
369 VISU::Mesh_i
370 ::SetLinkColor(const SALOMEDS::Color& theColor) 
371
372   if(IsSameColor(myLinkColor, theColor))
373     return;
374
375   VISU::TSetModified aModified(this);
376   
377   myLinkColor = theColor;
378   myParamsTime.Modified();
379 }
380
381
382 //----------------------------------------------------------------------------
383 SALOMEDS::Color 
384 VISU::Mesh_i
385 ::GetLinkColor() 
386
387   return myLinkColor;
388 }
389
390
391 //----------------------------------------------------------------------------
392 void 
393 VISU::Mesh_i
394 ::SetPresentationType(VISU::PresentationType theType) 
395
396   if(myPresentType == theType)
397     return;
398
399   VISU::TSetModified aModified(this);
400   
401   myPresentType = theType;
402   myParamsTime.Modified();
403 }
404
405
406 //----------------------------------------------------------------------------
407 VISU::PresentationType 
408 VISU::Mesh_i
409 ::GetPresentationType() 
410
411   return myPresentType;
412 }
413
414
415 //----------------------------------------------------------------------------
416 VISU::Entity 
417 VISU::Mesh_i
418 ::GetEntity() const
419
420   return VISU::Entity(myEntity);
421 }
422
423
424 //----------------------------------------------------------------------------
425 const std::string&
426 VISU::Mesh_i
427 ::GetSubMeshName() const
428
429   return mySubMeshName;
430 }
431
432
433 //----------------------------------------------------------------------------
434 VISU::Storable* 
435 VISU::Mesh_i
436 ::Restore(SALOMEDS::SObject_ptr theSObject,
437           const Storable::TRestoringMap& theMap)
438 {
439   if(!TSuperClass::Restore(theSObject, theMap))
440     return NULL;
441
442   myEntity = VISU::Storable::FindValue(theMap,"myEntity").toInt();//jfa IPAL9284
443   mySubMeshName = VISU::Storable::FindValue(theMap,"mySubMeshName").latin1();
444
445   myPresentType = VISU::PresentationType(VISU::Storable::FindValue(theMap,"myPresentType").toInt());
446
447   myCellColor.R = VISU::Storable::FindValue(theMap,"myCellColor.R").toDouble();
448   myCellColor.G = VISU::Storable::FindValue(theMap,"myCellColor.G").toDouble();
449   myCellColor.B = VISU::Storable::FindValue(theMap,"myCellColor.B").toDouble();
450
451   myNodeColor.R = VISU::Storable::FindValue(theMap,"myNodeColor.R").toDouble();
452   myNodeColor.G = VISU::Storable::FindValue(theMap,"myNodeColor.G").toDouble();
453   myNodeColor.B = VISU::Storable::FindValue(theMap,"myNodeColor.B").toDouble();
454
455   myLinkColor.R = VISU::Storable::FindValue(theMap,"myLinkColor.R").toDouble();
456   myLinkColor.G = VISU::Storable::FindValue(theMap,"myLinkColor.G").toDouble();
457   myLinkColor.B = VISU::Storable::FindValue(theMap,"myLinkColor.B").toDouble();
458
459   return Build(true);
460 }
461
462
463 //----------------------------------------------------------------------------
464 VISU::Storable*
465 VISU::Mesh_i
466 ::StorableEngine(SALOMEDS::SObject_ptr theSObject,
467                  const Storable::TRestoringMap& theMap,
468                  const std::string& thePrefix,
469                  CORBA::Boolean theIsMultiFile)
470 {
471   Mesh_i* aMesh = new Mesh_i();
472   return aMesh->Restore(theSObject, theMap);
473 }
474
475
476 //----------------------------------------------------------------------------
477 void 
478 VISU::Mesh_i
479 ::ToStream(std::ostringstream& theStr)
480 {
481   TSuperClass::ToStream(theStr);
482
483   Storable::DataToStream( theStr, "myEntity", myEntity );
484   Storable::DataToStream( theStr, "mySubMeshName", mySubMeshName.c_str() );
485
486   Storable::DataToStream( theStr, "myPresentType", int(myPresentType) );
487
488   Storable::DataToStream( theStr, "myCellColor.R", myCellColor.R );
489   Storable::DataToStream( theStr, "myCellColor.G", myCellColor.G );
490   Storable::DataToStream( theStr, "myCellColor.B", myCellColor.B );
491
492   Storable::DataToStream( theStr, "myNodeColor.R", myNodeColor.R );
493   Storable::DataToStream( theStr, "myNodeColor.G", myNodeColor.G );
494   Storable::DataToStream( theStr, "myNodeColor.B", myNodeColor.B );
495
496   Storable::DataToStream( theStr, "myLinkColor.R", myLinkColor.R );
497   Storable::DataToStream( theStr, "myLinkColor.G", myLinkColor.G );
498   Storable::DataToStream( theStr, "myLinkColor.B", myLinkColor.B );
499 }
500
501
502 //----------------------------------------------------------------------------
503 VISU_Actor* 
504 VISU::Mesh_i
505 ::CreateActor()
506 {
507   VISU_MeshAct* anActor = VISU_MeshAct::New();
508   try{
509     VISU::Prs3d_i::CreateActor(anActor);
510     SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
511     bool toShrink  = aResourceMgr->booleanValue("VISU", "mesh_shrink", false);
512     if (toShrink) anActor->SetShrink();
513     UpdateActor(anActor);
514   }catch (...) {
515     anActor->Delete();
516     throw ;
517   }
518   return anActor;
519 }
520
521
522 //----------------------------------------------------------------------------
523 void
524 VISU::Mesh_i
525 ::UpdateActor(VISU_Actor* theActor) 
526 {
527   if(VISU_MeshAct* anActor = dynamic_cast<VISU_MeshAct*>(theActor)){
528     if(MYDEBUG) MESSAGE("Mesh_i::UpdateActor");
529     VISU::Prs3d_i::UpdateActor(anActor);
530     anActor->SetRepresentation(myPresentType);
531     anActor->GetSurfaceProperty()->SetColor(myCellColor.R, myCellColor.G, myCellColor.B);
532     anActor->GetEdgeProperty()->SetColor(myLinkColor.R, myLinkColor.G, myLinkColor.B);
533     anActor->GetNodeProperty()->SetColor(myNodeColor.R, myNodeColor.G, myNodeColor.B);
534   }
535 }
536
537 //----------------------------------------------------------------------------