Salome HOME
Merge from OCC_development_generic_2006
[modules/visu.git] / src / VISUGUI / VisuGUI_Tools.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2005  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : VisuGUI_Tools.cxx
25 //  Author : Sergey Anikin
26 //  Module : VISU
27
28
29 #include "VisuGUI_Tools.h"
30
31 #include "VISU_Gen_i.hh"
32 #include "VISU_Prs3d_i.hh"
33 #include "VISU_Result_i.hh"
34 #include "VISU_Table_i.hh"
35 #include "VISU_Mesh_i.hh"
36 #include "VISU_ViewManager_i.hh"
37
38 #include "VISU_Actor.h"
39 #include "VISU_ScalarMapAct.h"
40
41 #include "SalomeApp_Module.h"
42 #include "SalomeApp_Study.h"
43 #include "SalomeApp_Application.h"
44
45 #include "OB_Browser.h"
46 #include "LightApp_DataObject.h"
47 #include "LightApp_SelectionMgr.h"
48
49 #include "SALOME_ListIO.hxx"
50 #include "SALOME_ListIteratorOfListIO.hxx"
51
52 #include "SVTK_ViewWindow.h"
53 #include "SVTK_ViewModel.h"
54 #include "SVTK_Functor.h"
55
56 #include "VTKViewer_Algorithm.h"
57
58 #include "SPlot2d_ViewModel.h"
59 #include "Plot2d_ViewFrame.h"
60
61 #include "SUIT_Session.h"
62 #include "SUIT_MessageBox.h"
63
64 #include "VisuGUI.h"
65
66 #include <vtkRenderer.h>
67 #include <vtkActorCollection.h>
68
69
70 //=============================================================================
71 namespace VISU
72 {
73   SUIT_Desktop*
74   GetDesktop(const CAM_Module* theModule)
75   {
76     return theModule->application()->desktop();
77   }
78
79   LightApp_SelectionMgr*
80   GetSelectionMgr(const SalomeApp_Module* theModule)
81   {
82     return theModule->getApp()->selectionMgr();
83   }
84
85   SalomeApp_Study*
86   GetAppStudy(const CAM_Module* theModule)
87   {
88     return
89       dynamic_cast<SalomeApp_Study*>(theModule->application()->activeStudy());
90   }
91
92   _PTR(Study)
93   GetCStudy(const SalomeApp_Study* theStudy)
94   {
95     return theStudy->studyDS();
96   }
97
98   bool
99   IsStudyLocked( _PTR(Study) theStudy )
100   {
101     if( theStudy )
102       return theStudy->GetProperties()->IsLocked();
103     return true;
104   }
105
106   bool
107   CheckLock( _PTR(Study) theStudy,
108              QWidget* theWidget )
109   {
110     if(IsStudyLocked(theStudy)){
111       SUIT_MessageBox::warn1(theWidget,
112                              QObject::tr("WRN_VISU_WARNING"),
113                              QObject::tr("WRN_STUDY_LOCKED"),
114                              QObject::tr("BUT_OK"));
115         return true;
116     }
117     return false;
118   }
119
120   LightApp_DataObject*
121   FindDataObject(SUIT_DataObject* theDataObject,
122                  const QString& theEntry,
123                  int theLevel)
124   {
125     int aLevel = theDataObject->level() + 2;
126     QString aSourceEntry = theEntry.section(':',0,aLevel);
127     DataObjectList aList;
128     theDataObject->children(aList);
129     DataObjectListIterator aDataObjectIter(aList);
130     while(SUIT_DataObject* aDataObject = aDataObjectIter.current()){
131       if(LightApp_DataObject* aChildDataObject = dynamic_cast<LightApp_DataObject*>(aDataObject)){
132         QString anEntry = aChildDataObject->entry();
133         QString aCurrentEntry = anEntry.section(':',0,aLevel);
134         if(aSourceEntry == aCurrentEntry){
135           if(theLevel == aLevel){
136             return aChildDataObject;
137           }else{
138             return FindDataObject(aChildDataObject,theEntry,theLevel);
139           }
140         }
141       }
142       ++aDataObjectIter;
143     }
144     return NULL;
145   }
146
147   LightApp_DataObject*
148   FindDataObject(CAM_Module* theModule,
149                  _PTR(SObject) theSObject)
150   {
151     CAM_DataModel* aDataModel = theModule->dataModel();
152     CAM_DataObject* aRootDataObject = aDataModel->root();
153     if(_PTR(SComponent) aComponent = theSObject)
154       return dynamic_cast<LightApp_DataObject*>(aRootDataObject);
155
156     int aLevel = theSObject->Depth();
157     std::string anEntry = theSObject->GetID();
158     return FindDataObject(aRootDataObject,anEntry.c_str(),aLevel);
159   }
160
161   void
162   UpdateObjBrowser(SalomeApp_Module* theModule,
163                    bool theIsUpdateDataModel,
164                    _PTR(SObject) theSObject)
165   {
166     LightApp_DataObject* aDataObject = NULL;
167     if(theSObject)
168       aDataObject = FindDataObject(theModule,theSObject);
169
170     theModule->updateObjBrowser(theIsUpdateDataModel,aDataObject);
171     theModule->getApp()->updateActions();
172   }
173
174   bool
175   IsSObjectTable( _PTR(SObject) theSObject )
176   {
177     if ( theSObject ) {
178       _PTR(GenericAttribute) anAttr;
179       if (theSObject->FindAttribute( anAttr, "AttributeTableOfInteger" ))
180         return true;
181       if (theSObject->FindAttribute( anAttr, "AttributeTableOfReal" ))
182         return true;
183     }
184     return false;
185   }
186
187   VISU_Gen_i*
188   GetVisuGen(const CAM_Module* theModule)
189   {
190     static VISU_Gen_i* aGen = NULL;
191     if(!aGen){
192       SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
193       Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","VISU");
194       VISU_Gen_var aVISU = VISU_Gen::_narrow(aComponent);
195       if(!CORBA::is_nil(aVISU)){
196         aGen = VISU_Gen_i::GetVisuGenImpl();
197         aGen->SetCurrentStudy(GetDSStudy(GetCStudy(GetAppStudy(theModule))));
198       }
199     }
200     if(!aGen)
201       throw std::runtime_error(QObject::tr("ERR_CANT_FIND_VISU_COMPONENT").latin1());
202     return aGen;
203   }
204
205   SALOME_MED::MED_Gen_var
206   GetMEDEngine()
207   {
208     static SALOME_MED::MED_Gen_var aGen;
209     if(CORBA::is_nil(aGen)){
210       SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
211       Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","MED");
212       aGen = SALOME_MED::MED_Gen::_narrow(aComponent);
213     }
214     if(CORBA::is_nil(aGen))
215       throw std::runtime_error(QObject::tr("ERR_CANT_FIND_MED_COMPONENT").latin1());
216     return aGen;
217   }
218
219
220   VISU::Storable::TRestoringMap getMapOfValue (_PTR(SObject) theSObject)
221   {
222     VISU::Storable::TRestoringMap aMap;
223     if (theSObject) {
224       _PTR(GenericAttribute) anAttr;
225       if (theSObject->FindAttribute(anAttr, "AttributeComment")) {
226         _PTR(AttributeComment) aComment (anAttr);
227         std::string aValue = aComment->Value();
228         QString aString (aValue.c_str());
229         VISU::Storable::StrToMap(aString, aMap);
230       }
231     }
232     return aMap;
233   }
234
235   QString getValue (_PTR(SObject) theSObject, QString theKey)
236   {
237     QString aStr("");
238     VISU::Storable::TRestoringMap aMap = getMapOfValue(theSObject);
239     if (!aMap.empty())
240       aStr = VISU::Storable::FindValue(aMap, theKey.latin1());
241     return aStr;
242   }
243
244   //------------------------------------------------------------
245   // Selection
246   CORBA::Object_var
247   GetSelectedObj(const SalomeApp_Study* theStudy,
248                  const QString& theEntry,
249                  VISU::Storable::TRestoringMap* theMap)
250   {
251     if (!theStudy || theEntry.isEmpty())
252       return CORBA::Object::_nil();
253
254     _PTR(Study) aStudy = GetCStudy(theStudy);
255     _PTR(SObject) aSObject = aStudy->FindObjectID(theEntry.latin1());
256     if (aSObject) {
257       _PTR(GenericAttribute) anAttr;
258       if (theMap && aSObject->FindAttribute(anAttr,"AttributeComment")) {
259         _PTR(AttributeComment) aComment (anAttr);
260         std::string aValue = aComment->Value();
261         QString aString (aValue.c_str());
262         VISU::Storable::StrToMap(aString, *theMap);
263       }
264       return VISU::ClientSObjectToObject(aSObject);
265     }
266     return CORBA::Object::_nil();
267   }
268
269   CORBA::Object_var
270   GetSelectedObj(const SalomeApp_Module* theModule,
271                  Handle(SALOME_InteractiveObject)* theIO,
272                  VISU::Storable::TRestoringMap* theMap)
273   {
274     LightApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
275     SALOME_ListIO aListIO;
276     aSelectionMgr->selectedObjects(aListIO);
277     SALOME_ListIteratorOfListIO anIter(aListIO);
278     if(anIter.More()){
279       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
280       if(theIO)
281         *theIO = anIO;
282       if(anIO->hasEntry())
283         return GetSelectedObj( GetAppStudy(theModule), anIO->getEntry(), theMap );
284     }
285     return CORBA::Object::_nil();
286   }
287
288   VISU::Prs3d_i*
289   GetPrsToModify(const SalomeApp_Module* theModule,
290                  Handle(SALOME_InteractiveObject)* theIO,
291                  VISU::Storable::TRestoringMap* theMap)
292   {
293     if (CheckLock(GetCStudy(GetAppStudy(theModule)),GetDesktop(theModule)))
294       return NULL;
295
296     CORBA::Object_var anObject = GetSelectedObj(theModule, theIO);
297     if (CORBA::is_nil(anObject))
298       return NULL;
299
300     PortableServer::ServantBase_var aServant = VISU::GetServant(anObject);
301     if (!aServant.in())
302       return NULL;
303
304     return dynamic_cast<VISU::Prs3d_i*>(aServant.in());
305   }
306
307   void
308   Add(LightApp_SelectionMgr* theSelectionMgr,
309       const Handle(SALOME_InteractiveObject)& theIO)
310   {
311     SALOME_ListIO aListIO;
312     theSelectionMgr->selectedObjects(aListIO);
313     aListIO.Append(theIO);
314     theSelectionMgr->setSelectedObjects(aListIO);
315   }
316
317   void
318   Remove(LightApp_SelectionMgr* theSelectionMgr,
319          const Handle(SALOME_InteractiveObject)& theIO)
320   {
321     if (theIO.IsNull()) return;
322     SALOME_ListIO aListIO, aNewListIO;
323     theSelectionMgr->selectedObjects(aListIO);
324     SALOME_ListIteratorOfListIO anIter (aListIO);
325     for (; anIter.More(); anIter.Next()) {
326       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
327       if (!anIO->isSame(theIO)) {
328         aNewListIO.Append(theIO);
329       }
330     }
331     theSelectionMgr->setSelectedObjects(aNewListIO);
332   }
333
334   /*!
335    * \brief Check, if "Delete" popup-menu can be put on current selection
336    *
337    * \param theModule - is used to access SelectionManager, Study and VISU_Gen
338    * \retval bool - returns TRUE if all currently selected objects are removable
339    */
340   bool
341   IsRemovableSelected (const SalomeApp_Module* theModule)
342   {
343     LightApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
344     SALOME_ListIO aListIO;
345     aSelectionMgr->selectedObjects(aListIO);
346
347     if (aListIO.Extent() < 1)
348       return false;
349
350     _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
351     if (!aStudy) return false;
352
353     // In some cases single selection can have its own popup-menu item for deletion
354     /*if (aListIO.Extent() == 1) {
355       Handle(SALOME_InteractiveObject) anIO = aListIO.First();
356       _PTR(SObject) aSObject = aStudy->FindObjectID(anIO->getEntry());
357       if (aSObject) {
358         VISU::VISUType aType = (VISU::VISUType)getValue(aSObject, "myType").toInt();
359         if (aType == VISU::TVIEW3D) {
360           return false; // special case
361         }
362       }
363     }*/
364
365     SALOME_ListIteratorOfListIO anIter (aListIO);
366     for (; anIter.More(); anIter.Next()) {
367       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
368       if (anIO->hasEntry()) {
369         _PTR(SObject) aSObject = aStudy->FindObjectID(anIO->getEntry());
370         VISU::Storable::TRestoringMap pMap;
371         if (aSObject) {
372           CORBA::Object_var aCORBAObject = VISU::ClientSObjectToObject(aSObject);
373           if (!CORBA::is_nil(aCORBAObject)) {
374             VISU::RemovableObject_var aRemovableObj = VISU::RemovableObject::_narrow(aCORBAObject);
375             if (CORBA::is_nil(aRemovableObj)) {
376               // Not removable CORBA object
377               return false;
378             }
379           } else {
380             // Can be removed, if lays directly under VISU
381             // (first sub-level) or is a child of such an object
382             string aNAME, aVisuNAME = GetVisuGen(theModule)->ComponentDataType();
383             _PTR(GenericAttribute) anAttr;
384             _PTR(AttributeComment) aComment;
385
386             bool isUnderVISU = false;
387             _PTR(SObject) aFatherSObject = aSObject->GetFather();
388             if (aFatherSObject->FindAttribute(anAttr, "AttributeComment")) {
389               _PTR(AttributeComment) aComment (anAttr);
390               aNAME = aComment->Value();
391               if (aNAME == aVisuNAME) {
392                 isUnderVISU = true;
393               }
394             }
395             if (!isUnderVISU) {
396               // Not directly under VISU component, check father
397               aCORBAObject = VISU::ClientSObjectToObject(aFatherSObject);
398               if (!CORBA::is_nil(aCORBAObject)) {
399                 // Father has IOR
400                 return false;
401               }
402
403               isUnderVISU = false;
404               aFatherSObject = aFatherSObject->GetFather();
405               if (aFatherSObject->FindAttribute(anAttr, "AttributeComment")) {
406                 _PTR(AttributeComment) aComment (anAttr);
407                 aNAME = aComment->Value();
408                 if (aNAME == aVisuNAME) {
409                   isUnderVISU = true;
410                 }
411               }
412               if (!isUnderVISU) {
413                 // Father is not directly under VISU component
414                 return false;
415               }
416             }
417           }
418         }
419       }
420     }
421     return true;
422   }
423
424   // Display/Erase
425
426   void
427   ErasePrs (const SalomeApp_Module* theModule,
428             CORBA::Object_ptr theObject, bool theUpdate)
429   {
430     if (!CORBA::is_nil(theObject)) {
431       VISU::Base_var aBase = VISU::Base::_narrow(theObject);
432       if (CORBA::is_nil(aBase)) return;
433       VISU::VISUType aType = aBase->GetType();
434       switch (aType) {
435       case VISU::TCURVE:
436         {
437           if (VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aBase).in()))
438             PlotCurve(theModule, aCurve, VISU::eErase );
439           break;
440         }
441       case VISU::TCONTAINER:
442         {
443           if (VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aBase).in()))
444             PlotContainer(theModule, aContainer, VISU::eErase );
445           break;
446         }
447       case VISU::TTABLE:
448         {
449           if (VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aBase).in()))
450             PlotTable(theModule, aTable, VISU::eErase );
451           break;
452         }
453       default:
454         {
455           if (VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aBase).in())) {
456             ErasePrs3d(theModule, aPrsObject);
457             if (theUpdate) {
458               if (SVTK_ViewWindow* vw = GetViewWindow(theModule))
459                 vw->Repaint();
460             }
461           }
462         }
463       } // switch (aType)
464     }
465   }
466
467   void
468   DeleteSObject(VisuGUI* theModule,
469                 _PTR(Study) theStudy,
470                 _PTR(SObject) theSObject)
471   {
472     _PTR(ChildIterator) aChildIter = theStudy->NewChildIterator(theSObject);
473     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
474       _PTR(SObject) aChildSObject = aChildIter->Value();
475       
476       CORBA::Object_var aChildObj = VISU::ClientSObjectToObject(aChildSObject);
477       RemoveScalarBarPosition(theModule,aChildObj);
478       ErasePrs(theModule, aChildObj);
479     }
480
481     CORBA::Object_var anObj = VISU::ClientSObjectToObject(theSObject);
482     if (!CORBA::is_nil(anObj)) {
483       RemoveScalarBarPosition(theModule,anObj);
484       ErasePrs(theModule, anObj);
485
486       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(anObj);
487       if (!CORBA::is_nil(aRemovableObject)) {
488         aRemovableObject->RemoveFromStudy();
489       }
490     } else {
491       // Remove aSObject together with all its sub-objects
492
493       VISU::RemoveFromStudy(theSObject,
494                             false,  // remove not only IOR attribute, but Object With Children
495                             false); // not Destroy() sub-objects
496     }
497   }
498
499   void
500   DeletePrs3d(VisuGUI* theModule,
501               VISU::Prs3d_i* thePrs,
502               const Handle(SALOME_InteractiveObject)& theIO)
503   {
504     if (!thePrs)
505       return;
506     if (CheckLock(GetCStudy(GetAppStudy(theModule)),GetDesktop(theModule)))
507       return;
508
509     RemoveScalarBarPosition(theModule,thePrs);
510     thePrs->RemoveFromStudy();
511   }
512
513   void
514   ErasePrs3d(const SalomeApp_Module* theModule,
515              VISU::Prs3d_i* thePrs)
516   {
517     if ( SVTK_ViewWindow* vw = GetViewWindow( theModule ) ){
518       if (VISU_Actor* anVISUActor = FindActor( vw, thePrs )) {
519         anVISUActor->VisibilityOff();
520       }
521     }
522   }
523
524   // Presentation management
525
526   void
527   ChangeRepresentation (const SalomeApp_Module* theModule,
528                         VISU::PresentationType  theType)
529   {
530     SVTK_ViewWindow* vw  = GetViewWindow( theModule, false );
531     if( !vw )
532       return;
533
534     Handle(SALOME_InteractiveObject) anIO;
535     CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
536     if (CORBA::is_nil(anObject)) return;
537
538     VISU::Base_var aVisuObj = VISU::Base::_narrow(anObject);
539     if (CORBA::is_nil(aVisuObj)) return;
540
541     PortableServer::ServantBase_var aServant = VISU::GetServant(anObject);
542     if (!aServant.in()) return;
543
544     VISU::Prs3d_i* aPrs3d = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
545     if (aPrs3d) {
546       if (VISU_Actor* anActor = GetActor(aPrs3d, vw)) {
547         switch (theType) {
548         case VISU::SHRINK:
549           if (anActor->IsShrunk())
550             anActor->UnShrink();
551           else
552             anActor->SetShrink();
553           break;
554         default:
555           if (VISU::Mesh_i* aMesh = dynamic_cast<VISU::Mesh_i*>(aPrs3d)) {
556             aMesh->SetPresentationType(theType);
557             RecreateActor(theModule, aMesh);
558           } else {
559             anActor->SetRepresentation(theType);
560           }
561         }
562         vw->Repaint();
563       }
564     }
565   }
566   
567   void
568   SetShading ( const SalomeApp_Module* theModule,
569               bool theOn )
570   {
571     SUIT_ViewWindow* aView = GetActiveView(theModule, SVTK_Viewer::Type());
572     if (!aView) return;
573     SVTK_ViewWindow* vw  = dynamic_cast<SVTK_ViewWindow*>( aView );
574     if( !vw )
575       return;
576     
577     Handle(SALOME_InteractiveObject) anIO;
578     CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
579     if (CORBA::is_nil(anObject)) return;
580
581     VISU::Base_var aVisuObj = VISU::Base::_narrow(anObject);
582     if (CORBA::is_nil(aVisuObj)) return;
583
584     PortableServer::ServantBase_var aServant = VISU::GetServant(anObject);
585     if (!aServant.in()) return;
586
587     VISU::Prs3d_i* aPrs3d = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
588     if (aPrs3d) {
589       if (VISU_Actor* anActor = GetActor(aPrs3d, vw)) {
590         if ( VISU_ScalarMapAct* aScalarMapActor = dynamic_cast<VISU_ScalarMapAct*>(anActor) )
591           aScalarMapActor->SetShading( theOn );
592       }
593       vw->Repaint();
594     }
595   }
596
597   // SObject type
598
599   bool
600   CheckTimeStamp(const SalomeApp_Module* theModule,
601                  _PTR(SObject)&          theSObject,
602                  Handle(SALOME_InteractiveObject)* theIO)
603   {
604     Handle(SALOME_InteractiveObject) anIO;
605     CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
606     if (theIO)
607       *theIO = anIO;
608     if (!anIO.IsNull() && anIO->hasEntry()){
609       _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
610       theSObject = aStudy->FindObjectID(anIO->getEntry());
611       QString aValue = getValue(theSObject,"myType");
612       if (aValue.toInt() == int(VISU::TTIMESTAMP))
613         return true;
614     }
615     SUIT_MessageBox::warn1(GetDesktop(theModule),
616                            QObject::tr("WRN_VISU"),
617                            QObject::tr("WRN_NO_AVAILABLE_DATA"),
618                            QObject::tr("BUT_OK") );
619     return false;
620   }
621
622   VISU::Result_i*
623   CheckResult(const SalomeApp_Module* theModule,
624               _PTR(SObject)           theSource,
625               VISU::Result_var&       theResult)
626   {
627     if(theSource->Depth() < 3) // Bug of SALOMEDS : can\t get father from root object
628       return NULL;
629       
630     _PTR(SObject) aSObj = theSource->GetFather();
631     if (!aSObj)
632       return NULL;
633
634     aSObj = aSObj->GetFather();
635     if (!aSObj)
636       return NULL;
637
638     aSObj = aSObj->GetFather();
639     if (!aSObj)
640       return NULL;
641
642     CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObj);
643     if (CORBA::is_nil(anObject)) {
644       aSObj = aSObj->GetFather();
645       if (!aSObj)
646         return NULL;
647       anObject = VISU::ClientSObjectToObject(aSObj);
648     }
649
650     if (CORBA::is_nil(anObject))
651       return NULL;
652
653     theResult = VISU::Result::_narrow(anObject);
654     return dynamic_cast<VISU::Result_i*>(VISU::GetServant(anObject).in());
655   }
656
657   // Views
658
659   SUIT_ViewWindow* GetActiveView(const SalomeApp_Module* theModule, QString theType)
660   {
661     if(SalomeApp_Application* anApp = theModule->getApp()){
662       if(SUIT_ViewManager* aViewManager = anApp->activeViewManager()){
663         if (!theType.isNull()) {
664           if (aViewManager->getType() != theType)
665             return 0;
666         }
667         return aViewManager->getActiveView();
668       }
669     }
670     return 0;
671   }
672
673   // VTK View
674
675   TViewWindows
676   GetViews(const SalomeApp_Module* theModule)
677   {
678     TViewWindows aViewWindows;
679     if(SalomeApp_Application* anApp = theModule->getApp()){
680       ViewManagerList aViewManagerList;
681       anApp->viewManagers(SVTK_Viewer::Type(),aViewManagerList);
682       QPtrListIterator<SUIT_ViewManager> anIter(aViewManagerList);
683       while(SUIT_ViewManager* aViewManager = anIter.current()){
684         QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
685         for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
686           if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
687             if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
688               aViewWindows.push_back(aView);
689         }
690         ++anIter;
691       }
692     }
693     return aViewWindows;
694   }
695
696   SVTK_ViewWindow*
697   GetViewWindow( const SalomeApp_Module* theModule, const bool theCreate )
698   {
699     if (SalomeApp_Application* anApp = theModule->getApp())
700     {
701       SVTK_ViewWindow* wnd = dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow());
702       if( wnd )
703         return wnd;
704       else
705       {
706         SUIT_ViewManager* aViewManager = anApp->getViewManager( SVTK_Viewer::Type(), theCreate );
707         return aViewManager ? dynamic_cast<SVTK_ViewWindow*>( aViewManager->getActiveView() ) : 0;
708       }
709     }
710     return NULL;
711   }
712
713   /*SVTK_ViewWindow*
714   GetViewWindow(const SalomeApp_Module* theModule, const bool theCreate )
715   {
716     if (SalomeApp_Application* anApp = theModule->getApp())
717     {
718       SVTK_ViewWindow* wnd = dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow());
719       if( wnd )
720         return wnd;
721       else
722       {
723         if(SUIT_ViewManager* aViewManager = anApp->getViewManager( SVTK_Viewer::Type(), theCreate ))
724           return dynamic_cast<SVTK_ViewWindow*>( aViewManager->getActiveView() );
725       }
726     }
727     return NULL;
728   }*/
729
730
731   SVTK_ViewWindow*
732   GetViewWindow()
733   {
734     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
735       (SUIT_Session::session()->activeApplication());
736     if (anApp) {
737       if (SUIT_ViewManager* aViewManager = anApp->activeViewManager()) {
738         if (aViewManager->getType() == SVTK_Viewer::Type()) {
739           if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
740             return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
741           }
742         }
743       }
744     }
745     return NULL;
746   }
747
748   VISU_Actor*
749   PublishInView(const SalomeApp_Module* theModule,
750                 VISU::Prs3d_i* thePrs)
751   {
752     VISU_Actor* aActor = NULL;
753     if(!thePrs)
754       return aActor;
755     if(SVTK_ViewWindow* aView = GetViewWindow(theModule)){
756       QApplication::setOverrideCursor( Qt::waitCursor );
757       try{
758         if(aActor = thePrs->CreateActor())
759           aView->AddActor(aActor);
760       }catch(std::exception& exc){
761         SUIT_MessageBox::warn1
762           (GetDesktop(theModule), QObject::tr("WRN_VISU"),
763            QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()),
764            QObject::tr("BUT_OK"));
765       }
766       QApplication::restoreOverrideCursor();
767     }
768     return aActor;
769   }
770
771   VISU_Actor*
772   UpdateViewer(const SalomeApp_Module* theModule,
773                VISU::Prs3d_i* thePrs,
774                bool theDispOnly)
775   {
776     SVTK_ViewWindow* vw = GetViewWindow( theModule );
777     if (!vw) return NULL;
778
779     vtkRenderer *aRen = vw->getRenderer();
780     vtkActorCollection *anActColl = aRen->GetActors();
781     anActColl->InitTraversal();
782
783     vtkActor *anActor;
784     VISU_Actor* anVISUActor = NULL;
785     VISU_Actor* aResActor = NULL;
786     while(vtkActor *anActor = anActColl->GetNextActor()){
787       anVISUActor = VISU_Actor::SafeDownCast(anActor);
788       if(anVISUActor){
789         VISU::Prs3d_i* aPrs = anVISUActor->GetPrs3d();
790         if(aPrs == NULL) continue;
791         if (thePrs == aPrs) {
792           try {
793             aResActor = anVISUActor;
794             thePrs->UpdateActor(aResActor);
795             aResActor->VisibilityOn();
796           } catch (std::runtime_error& exc) {
797             aResActor->VisibilityOff();
798             INFOS(exc.what());
799             SUIT_MessageBox::warn1
800               (GetDesktop(theModule), QObject::tr("WRN_VISU"),
801                QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()),
802                QObject::tr("BUT_OK"));
803           }
804         } else if (theDispOnly) {
805           anVISUActor->VisibilityOff();
806         } else {
807         }
808       } else if (theDispOnly && anActor->GetVisibility()) {
809         anActor->VisibilityOff();
810       } else {
811       }
812     }
813     if (aResActor)
814       return aResActor;
815
816     //anVISUActor = PublishInView( theModule, thePrs );
817     return anVISUActor;
818   }
819
820   void
821   RepaintViewWindows (const SalomeApp_Module* theModule,
822                       const Handle(SALOME_InteractiveObject)& theIObject)
823   {
824     TViewWindows aViewWindows;
825     if (SalomeApp_Application* anApp = theModule->getApp()) {
826       ViewManagerList aViewManagerList;
827       anApp->viewManagers(SVTK_Viewer::Type(),aViewManagerList);
828       QPtrListIterator<SUIT_ViewManager> anIter (aViewManagerList);
829       while (SUIT_ViewManager* aViewManager = anIter.current()) {
830         QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
831         for (int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
832           if (SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
833             if (SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
834               if (vw->isVisible(theIObject)) {
835                 vw->getRenderer()->ResetCameraClippingRange();
836                 vw->Repaint();
837                 vw->highlight(theIObject, true, true);
838               }
839             }
840           }
841         }
842         ++anIter;
843       }
844     }
845   }
846
847   VISU_Actor*
848   FindActor(SVTK_ViewWindow* theViewWindow,
849             const char* theEntry)
850   {
851     using namespace SVTK;
852     if(vtkRenderer* aRenderer = theViewWindow->getRenderer()){
853       if(vtkActorCollection* aCollection = aRenderer->GetActors()){
854         if(VISU_Actor* anActor = Find<VISU_Actor>(aCollection,TIsSameEntry<VISU_Actor>(theEntry))){
855           return anActor;
856         }
857       }
858     }
859     return NULL;
860   }
861
862   VISU_Actor*
863   FindActor(SVTK_ViewWindow* theViewWindow,
864             VISU::Prs3d_i* thePrs)
865   {
866     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
867     CORBA::String_var anEntry = aSObject->GetID();
868     return FindActor(theViewWindow,anEntry.in());
869   }
870
871   void
872   RecreateActor (const SalomeApp_Module* theModule,
873                  VISU::Prs3d_i* thePrs)
874   {
875     QApplication::setOverrideCursor(Qt::waitCursor);
876
877     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
878     CORBA::String_var anEntry = aSObject->GetID();
879
880     try {
881       thePrs->UpdateActors();
882     } catch (std::runtime_error& exc) {
883       INFOS(exc.what());
884       QApplication::restoreOverrideCursor();
885       SUIT_MessageBox::warn1
886         (GetDesktop(theModule), QObject::tr("WRN_VISU"),
887          QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
888          QObject::tr("BUT_OK"));
889
890       thePrs->RemoveActors();
891       return;
892     }
893     QApplication::restoreOverrideCursor();
894   }
895
896   static bool ComputeVisiblePropBounds(SVTK_ViewWindow* theViewWindow,
897                                       float             allBounds[6], 
898                                       const char*       theActorClassName = "VISU_Actor")
899   {
900     vtkRenderer *aRen = theViewWindow->getRenderer();
901     vtkActorCollection *anActColl = aRen->GetActors(); 
902     vtkProp *prop;
903     float   *bounds;
904     int     somethingVisible = false;
905
906     allBounds[0] = allBounds[2] = allBounds[4] = VTK_LARGE_FLOAT;
907     allBounds[1] = allBounds[3] = allBounds[5] = -VTK_LARGE_FLOAT;
908     // loop through all props
909     for (anActColl->InitTraversal(); (prop = anActColl->GetNextProp()); ) {
910       // if it's invisible, or has no geometry, we can skip the rest 
911       if (prop->GetVisibility() && prop->IsA(theActorClassName)) {
912         bounds = prop->GetBounds();
913         // make sure we haven't got bogus bounds
914         if (bounds != NULL &&
915             bounds[0] > -VTK_LARGE_FLOAT && bounds[1] < VTK_LARGE_FLOAT &&
916             bounds[2] > -VTK_LARGE_FLOAT && bounds[3] < VTK_LARGE_FLOAT &&
917             bounds[4] > -VTK_LARGE_FLOAT && bounds[5] < VTK_LARGE_FLOAT)
918         {
919           somethingVisible = true;
920           if (bounds[0] < allBounds[0]) allBounds[0] = bounds[0]; 
921           if (bounds[1] > allBounds[1]) allBounds[1] = bounds[1]; 
922           if (bounds[2] < allBounds[2]) allBounds[2] = bounds[2]; 
923           if (bounds[3] > allBounds[3]) allBounds[3] = bounds[3]; 
924           if (bounds[4] < allBounds[4]) allBounds[4] = bounds[4]; 
925           if (bounds[5] > allBounds[5]) allBounds[5] = bounds[5]; 
926         }//not bogus
927       }
928     }
929     return somethingVisible;
930   }
931
932   void SetFitAll(SVTK_ViewWindow* theViewWindow)
933   {
934     static float PRECISION = 0.000001;
935     static float DEVIATION = 600;
936     float XYZ_Bnd[6];
937     if (!ComputeVisiblePropBounds(theViewWindow, XYZ_Bnd)) return;
938
939     float absX = XYZ_Bnd[1] - XYZ_Bnd[0];
940     float absY = XYZ_Bnd[3] - XYZ_Bnd[2];
941     float absZ = XYZ_Bnd[5] - XYZ_Bnd[4];
942
943     enum CameraOrient {e3D, eFront, eLeft, eTop};
944     CameraOrient aCameraOrient = e3D;
945     if (absX <= PRECISION) aCameraOrient = eFront;
946     else {
947       if (absY <= PRECISION) aCameraOrient = eLeft;
948       else {
949         if (absZ <= PRECISION) aCameraOrient = eTop;
950         else {
951           // all the three dimensions exceeds precision
952           float dev_abs_XY = absX / absY;
953           float dev_abs_YZ = absY / absZ;
954           float dev_abs_XZ = absX / absZ;
955           if (dev_abs_XY >= DEVIATION || 1./dev_abs_YZ >= DEVIATION)
956             aCameraOrient = eLeft;
957           else {
958             if (1./dev_abs_XY >= DEVIATION || 1./dev_abs_XZ >= DEVIATION)
959               aCameraOrient = eFront;
960             else {
961               if (dev_abs_XZ >= DEVIATION || dev_abs_YZ >= DEVIATION)
962                 aCameraOrient = eTop;
963             }
964           }
965         }
966       }
967     }
968
969     switch (aCameraOrient) {
970     case eFront: theViewWindow->onFrontView(); break;
971     case eLeft:  theViewWindow->onLeftView();  break;
972     case eTop:   theViewWindow->onTopView();   break;
973     case e3D:    theViewWindow->onResetView(); break;
974     }
975     theViewWindow->getRenderer()->ResetCameraClippingRange();
976     theViewWindow->onFitAll();
977   }
978
979   //************************************************************
980   // Plot2d View
981
982   SPlot2d_Viewer*
983   GetPlot2dViewer(const SalomeApp_Module* theModule, const bool theCreate)
984   {
985     if(SalomeApp_Application* anApp = theModule->getApp()){
986       if(SUIT_ViewManager* aViewManager = anApp->getViewManager( Plot2d_Viewer::Type(), theCreate )){
987         return dynamic_cast<SPlot2d_Viewer*>(aViewManager->getViewModel());
988       }
989     }
990     return NULL;
991   }
992
993   // Internal function used by several public functions below
994   void
995   UpdateCurve(VISU::Curve_i* theCurve,
996               Plot2d_ViewFrame* aPlot,
997               SPlot2d_Curve* plotCurve,
998               int theDisplaying)
999   {
1000     if ( theDisplaying == VISU::eErase ) {
1001       if ( plotCurve && aPlot )
1002         aPlot->eraseCurve( plotCurve, false );
1003     }
1004     else if ( theDisplaying == VISU::eDisplay || theDisplaying == VISU::eDisplayOnly ) {
1005       if ( plotCurve ) {
1006         plotCurve->setHorTitle( theCurve->GetHorTitle().c_str() );
1007         //plotCurve->setVerTitle( ( theCurve->GetVerTitle().c_str() ) );
1008         plotCurve->setVerTitle( theCurve->GetName() );
1009         plotCurve->setHorUnits( theCurve->GetHorUnits().c_str() );
1010         plotCurve->setVerUnits( theCurve->GetVerUnits().c_str() );
1011         double* xList = 0;
1012         double* yList = 0;
1013         int     nbPoints = theCurve->GetData( xList, yList );
1014         if ( nbPoints > 0 && xList && yList ) {
1015           plotCurve->setData( xList, yList, nbPoints );
1016         }
1017         if ( !theCurve->IsAuto() ) {
1018           plotCurve->setLine( (Plot2d_Curve::LineType)theCurve->GetLine(), theCurve->GetLineWidth() );
1019           plotCurve->setMarker( (Plot2d_Curve::MarkerType)theCurve->GetMarker() );
1020           SALOMEDS::Color color = theCurve->GetColor();
1021           plotCurve->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
1022         }
1023         plotCurve->setAutoAssign( theCurve->IsAuto() );
1024         if( aPlot )
1025           aPlot->displayCurve( plotCurve, false );
1026       }
1027       else {
1028         Plot2d_Curve* crv = theCurve->CreatePresentation();
1029         if ( crv ) {
1030           if( aPlot )
1031             aPlot->displayCurve( crv, false );
1032           theCurve->SetLine( (VISU::Curve::LineType)crv->getLine(), crv->getLineWidth() );
1033           theCurve->SetMarker( (VISU::Curve::MarkerType)crv->getMarker());
1034           SALOMEDS::Color newColor;
1035           newColor.R = crv->getColor().red()/255.;
1036           newColor.G = crv->getColor().green()/255.;
1037           newColor.B = crv->getColor().blue()/255.;
1038           theCurve->SetColor( newColor );
1039           crv->setAutoAssign( theCurve->IsAuto() );
1040         }
1041       }
1042     }
1043   }
1044
1045   void
1046   PlotTable(const SalomeApp_Module* theModule,
1047             VISU::Table_i* table,
1048             int theDisplaying)
1049   {
1050     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true ); // create if necessary
1051     if ( !aView )
1052       return;
1053     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
1054     if ( !aPlot )
1055       return;
1056
1057     if ( theDisplaying == VISU::eDisplayOnly )
1058       aPlot->EraseAll();
1059     QList<Plot2d_Curve> clist;
1060     aPlot->getCurves( clist );
1061     _PTR(Study) aStudy = GetCStudy( GetAppStudy( theModule ) );
1062     _PTR(SObject) TableSO = aStudy->FindObjectID( table->GetEntry().latin1() );
1063     if ( TableSO ) {
1064       _PTR(ChildIterator) Iter = aStudy->NewChildIterator( TableSO );
1065       for ( ; Iter->More(); Iter->Next() ) {
1066         CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
1067         if( !CORBA::is_nil( childObject ) ) {
1068           CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
1069           if( !CORBA::is_nil( aCurve ) ) {
1070             VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
1071             SPlot2d_Curve* plotCurve = 0;
1072             SPlot2d_Curve* tmpCurve;
1073             for ( int i = 0; i < clist.count(); i++ ) {
1074               tmpCurve = dynamic_cast<SPlot2d_Curve*>( clist.at( i ) );
1075               if (tmpCurve && tmpCurve->hasIO() &&
1076                   !strcmp(tmpCurve->getIO()->getEntry(), theCurve->GetEntry())) {
1077                 plotCurve = tmpCurve;
1078                 break;
1079               }
1080             }
1081
1082             UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
1083
1084             if ( theDisplaying == VISU::eErase && plotCurve ) {
1085               clist.remove( plotCurve );
1086             }
1087           }
1088         }
1089       }
1090       aPlot->Repaint();
1091     }
1092   }
1093
1094   void
1095   PlotCurve(const SalomeApp_Module* theModule,
1096             VISU::Curve_i* theCurve,
1097             int theDisplaying)
1098   {
1099     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true );
1100     if ( !aView )
1101       return;
1102     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
1103     if ( !aPlot )
1104       return;
1105
1106 //  if ( theDisplaying == VISU::eDisplayOnly )
1107 //    aPlot->EraseAll();
1108     QList<Plot2d_Curve> clist;
1109     aPlot->getCurves( clist );
1110     SPlot2d_Curve* plotCurve = 0;
1111     SPlot2d_Curve* tmpCurve;
1112     for (int i = 0; i < clist.count(); i++) {
1113       tmpCurve = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
1114       if (tmpCurve && tmpCurve->hasIO() &&
1115           !strcmp(tmpCurve->getIO()->getEntry(), theCurve->GetEntry())) {
1116         plotCurve = tmpCurve;
1117       } else if (theDisplaying == VISU::eDisplayOnly) {
1118         aPlot->eraseCurve(clist.at(i));
1119       }
1120     }
1121
1122     UpdateCurve(theCurve, aPlot, plotCurve, theDisplaying);
1123
1124     aPlot->Repaint();
1125   }
1126
1127   void
1128   PlotContainer(const SalomeApp_Module* theModule,
1129                 VISU::Container_i* container,
1130                 int theDisplaying)
1131   {
1132     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true );
1133     if ( !aView )
1134       return;
1135     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
1136     if ( !aPlot )
1137       return;
1138
1139     if ( theDisplaying == VISU::eDisplayOnly )
1140       aPlot->EraseAll();
1141     QList<Plot2d_Curve> clist;
1142     aPlot->getCurves( clist );
1143     if ( container->GetNbCurves() > 0 ) {
1144       int nbCurves = container->GetNbCurves();
1145       for ( int k = 1; k <= nbCurves; k++ ) {
1146         VISU::Curve_i* theCurve = container->GetCurve( k );
1147         if ( theCurve && theCurve->IsValid() ) {
1148           SPlot2d_Curve* plotCurve = dynamic_cast<SPlot2d_Curve*>
1149             (aView->getCurveByIO(new SALOME_InteractiveObject (theCurve->GetEntry(), "", "")));
1150
1151           UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
1152
1153           if ( plotCurve && theDisplaying == VISU::eErase ) {
1154             clist.remove( plotCurve );
1155           }
1156         }
1157       }
1158     }
1159     aPlot->Repaint();
1160   }
1161
1162   void
1163   CreatePlot(SalomeApp_Module* theModule,
1164              _PTR(SObject) theTableSO)
1165   {
1166     if ( IsSObjectTable(theTableSO) ) {
1167       CORBA::Object_var aTable = VISU::ClientSObjectToObject(theTableSO);
1168       CORBA::Object_var aContainer = GetVisuGen( theModule )->CreateContainer();
1169       if ( !CORBA::is_nil( aTable ) && !CORBA::is_nil( aContainer ) ) {
1170         VISU::Table_i*     pTable     = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aTable).in());
1171         VISU::Container_i* pContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aContainer).in());
1172
1173         if ( pContainer && pTable ) {
1174           for ( int i = 2; i <= pTable->GetNbRows(); i++ ) {
1175             CORBA::Object_var aNewCurve = GetVisuGen( theModule )->CreateCurve( pTable->_this(), 1, i );
1176             if( !CORBA::is_nil( aNewCurve ) ) {
1177               VISU::Curve_i* pCrv = dynamic_cast<VISU::Curve_i*>( VISU::GetServant(aNewCurve).in() );
1178               if ( pCrv ) {
1179                 pContainer->AddCurve( pCrv->_this() );
1180               }
1181             }
1182           }
1183           PlotContainer( theModule, pContainer, VISU::eDisplay );
1184
1185           QString anEntry = pContainer->GetEntry();
1186           _PTR(Study) aStudy = theTableSO->GetStudy();
1187           _PTR(SObject) aContainerSO = aStudy->FindObjectID(anEntry.latin1());
1188           _PTR(SObject) aParentSO = aContainerSO->GetFather();
1189           UpdateObjBrowser(theModule,true,aParentSO);
1190         }
1191       }
1192     }
1193   }
1194
1195   //************************************************************
1196   // Others
1197
1198   void CreateMesh (const SalomeApp_Module* theModule,
1199                     const Handle(SALOME_InteractiveObject)& theIO)
1200   {
1201     _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
1202     //if (CheckLock(aStudy))
1203     //  return;
1204
1205     _PTR(SObject) aResultSObj = aStudy->FindObjectID(theIO->getEntry());
1206
1207     // Get VISU::Result
1208     VISU::Result_var aResult;
1209     VISU::Result_i* pResult = CheckResult(theModule, aResultSObj, aResult);
1210     if (pResult == NULL)
1211       return;
1212
1213     Storable::TRestoringMap aMap = getMapOfValue(aResultSObj);
1214     bool isExist;
1215     string aComment = Storable::FindValue(aMap,"myComment",&isExist).latin1();
1216     if (!isExist)
1217       return;
1218
1219     CORBA::Object_var aMesh;
1220     string aMeshName = Storable::FindValue(aMap,"myMeshName").latin1();
1221 #ifdef CHECKTIME
1222     Utils_Timer timer;
1223     timer.Start();
1224 #endif
1225     if (aComment == "ENTITY") {
1226       VISU::Entity anEntity = (VISU::Entity)Storable::FindValue(aMap,"myId").toInt();
1227       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),anEntity))
1228         aMesh = GetVisuGen(theModule)->MeshOnEntity(aResult,aMeshName.c_str(),anEntity);
1229     } else if (aComment == "FAMILY") {
1230       VISU::Entity anEntity = (VISU::Entity)Storable::FindValue(aMap,"myEntityId").toInt();
1231       string aFamilyName = Storable::FindValue(aMap,"myName").latin1();
1232       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),anEntity,aFamilyName.c_str()))
1233         aMesh = GetVisuGen(theModule)->FamilyMeshOnEntity
1234           (aResult,aMeshName.c_str(),anEntity,aFamilyName.c_str());
1235     } else if (aComment == "GROUP") {
1236       string aGroupName = Storable::FindValue(aMap,"myName").latin1();
1237       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),aGroupName.c_str()))
1238         aMesh = GetVisuGen(theModule)->GroupMesh(aResult,aMeshName.c_str(),aGroupName.c_str());
1239     }
1240 #ifdef CHECKTIME
1241     timer.Stop();
1242     MESSAGE("VisuGUI::CreateMesh() - CREATE MESH");
1243     timer.Show();
1244 #endif
1245
1246     QApplication::restoreOverrideCursor();
1247     VISU::Mesh_i* pPresent = NULL;
1248     if (!CORBA::is_nil(aMesh))
1249       pPresent = dynamic_cast<VISU::Mesh_i*>(VISU::GetServant(aMesh).in());
1250     if (pPresent == NULL) {
1251       SUIT_MessageBox::warn1 (GetDesktop(theModule),
1252                               QObject::tr("WRN_VISU"),
1253                               QObject::tr("ERR_CANT_BUILD_PRESENTATION"),
1254                               QObject::tr("BUT_OK"));
1255       return;
1256     }
1257
1258     if (SVTK_ViewWindow* aView = GetViewWindow(theModule)) {
1259       try {
1260 #ifdef CHECKTIME
1261         Utils_Timer timer;
1262         timer.Start();
1263 #endif
1264         PublishInView(theModule, pPresent);
1265         //aView->onFitAll();
1266         SetFitAll(aView);
1267 #ifdef CHECKTIME
1268         timer.Stop();
1269         MESSAGE("VisuGUI::CreateMesh() - DISPLAY MESH");
1270         timer.Show();
1271 #endif
1272         theModule->application()->putInfo(QObject::tr("INF_DONE"));
1273         // Make "Save" button active
1274         theModule->getApp()->updateActions();
1275       } catch (std::runtime_error& exc) {
1276         INFOS(exc.what());
1277         SUIT_MessageBox::warn1
1278           (GetDesktop(theModule), QObject::tr("WRN_VISU"),
1279            QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()),
1280            QObject::tr("BUT_OK"));
1281       }
1282     }
1283   }
1284
1285   // ========================================================================================
1286   // GetPrs3dList: find list of presentations for the given object
1287   // ========================================================================================
1288   std::vector<VISU::Prs3d_i*> GetPrs3dList (const SalomeApp_Module* theModule,
1289                                             const Handle(SALOME_InteractiveObject)& theIO)
1290   {
1291     std::vector<VISU::Prs3d_i*> aList;
1292     if (!theIO.IsNull() && theIO->hasEntry()) {
1293       _PTR(Study) aCStudy = GetCStudy(GetAppStudy(theModule));
1294       _PTR(SObject) aSObject = aCStudy->FindObjectID(theIO->getEntry());
1295       aList = GetPrs3dList(theModule, aSObject);
1296     }
1297     return aList;
1298   }
1299
1300   std::vector<VISU::Prs3d_i*> GetPrs3dList (const SalomeApp_Module* theModule,
1301                                             _PTR(SObject) theObject)
1302   {
1303     std::vector<VISU::Prs3d_i*> aList; int k = 0;
1304     if (!theObject)
1305       return aList;
1306
1307     _PTR(Study) aCStudy = GetCStudy(GetAppStudy(theModule));
1308
1309     CORBA::Object_var aCORBAObject = VISU::ClientSObjectToObject(theObject);
1310     if (!CORBA::is_nil(aCORBAObject)) {
1311       VISU::Base_var aVisuObj = VISU::Base::_narrow(aCORBAObject);
1312       if (!CORBA::is_nil(aVisuObj)) {
1313         VISU::VISUType aType = aVisuObj->GetType();
1314         switch (aType) {
1315         case VISU::TSCALARMAP:
1316         case VISU::TISOSURFACE:
1317         case VISU::TDEFORMEDSHAPE:
1318         case VISU::TCUTPLANES:
1319         case VISU::TCUTLINES:
1320         case VISU::TVECTORS:
1321         case VISU::TSTREAMLINES:
1322         case VISU::TPLOT3D:
1323         case VISU::TSCALARMAPONDEFORMEDSHAPE:
1324           {
1325             PortableServer::ServantBase_var aServant = VISU::GetServant(aCORBAObject);
1326             if (aServant.in()) {
1327               VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
1328               aList.resize(k+1);
1329               aList[k] = aPrsObject;
1330               k++;
1331             }
1332           }
1333           break;
1334         case VISU::TFIELD:
1335           {
1336             _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
1337             _PTR(SObject) aTimeStamp;
1338             anIter->Next(); // First is reference on support
1339             for (; anIter->More(); anIter->Next()) {
1340               aTimeStamp = anIter->Value();
1341               if (!aTimeStamp) continue;
1342               std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
1343               if (!aSubList.empty()) {
1344                 int n = aSubList.size();
1345                 aList.resize(k+n);
1346                 for (int i = 0; i < n; i++) {
1347                   aList[k] = aSubList[i];
1348                   k++;
1349                 }
1350               }
1351             }
1352           }
1353           break;
1354         }
1355       }
1356     } else {
1357       _PTR(GenericAttribute) anAttr;
1358       if (theObject->FindAttribute(anAttr, "AttributeComment")) {
1359         _PTR(AttributeComment) aComment (anAttr);
1360         string aComm = aComment->Value();
1361         QString strIn (aComm.c_str());
1362         VISU::Storable::TRestoringMap pMap;
1363         VISU::Storable::StrToMap(strIn, pMap);
1364         bool isExist;
1365         VISU::VISUType aType =
1366           (VISU::VISUType)VISU::Storable::FindValue(pMap,"myType",&isExist).toInt();
1367         if (isExist) {
1368           switch (aType) {
1369           case VISU::TFIELD:
1370             {
1371               _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
1372               _PTR(SObject) aTimeStamp;
1373               anIter->Next(); // First is reference on support
1374               for (; anIter->More(); anIter->Next()) {
1375                 aTimeStamp = anIter->Value();
1376                 if (!aTimeStamp) continue;
1377                 std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
1378                 if (!aSubList.empty()) {
1379                   int n = aSubList.size();
1380                   aList.resize(k+n);
1381                   for (int i = 0; i < n; i++) {
1382                     aList[k] = aSubList[i];
1383                     k++;
1384                   }
1385                 }
1386               }
1387             }
1388             break;
1389           case VISU::TTIMESTAMP:
1390             {
1391               _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
1392               _PTR(SObject) aPrs;
1393               for (; anIter->More(); anIter->Next()) {
1394                 aPrs = anIter->Value();
1395                 if (!aPrs) continue;
1396                 std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aPrs);
1397                 if (!aSubList.empty()) {
1398                   int n = aSubList.size();
1399                   aList.resize(k+n);
1400                   for (int i = 0; i < n; i++) {
1401                     aList[k] = aSubList[i];
1402                     k++;
1403                   }
1404                 }
1405               }
1406             }
1407             break;
1408           }
1409         }
1410       }
1411     }
1412     return aList;
1413   }
1414   
1415   int GetFreePositionOfDefaultScalarBar(VisuGUI* theModule){
1416     int minIndx = 1;
1417     std::set<int> aIndexes;
1418     SVTK_ViewWindow* vw = GetViewWindow(theModule);
1419     TViewToPrs3d aMap = theModule->getScalarBarsMap();
1420     TViewToPrs3d::const_iterator aViewToPrsIter = aMap.find(vw);
1421     if (aViewToPrsIter != aMap.end()){
1422       TSetPrs3d::const_iterator aPrsIter = (aViewToPrsIter->second).begin();
1423       for(;aPrsIter!=(aViewToPrsIter->second).end();aPrsIter++){
1424         aIndexes.insert((*aPrsIter).second);
1425       }
1426     }
1427     std::set<int>::const_iterator aIter = aIndexes.begin();
1428     for (int i=1,length=aIndexes.size(); i <= length; i++){
1429       std::set<int>::const_iterator aIter = aIndexes.find(i);
1430       if(aIter==aIndexes.end()){minIndx = i;break;} 
1431       else minIndx = i + 1;
1432       
1433     }
1434     return minIndx;
1435   }
1436   
1437   void AddScalarBarPosition(VisuGUI* theModule,VISU::Prs3d_i* thePrs3d, int pos){
1438     SVTK_ViewWindow* vw = GetViewWindow(theModule);
1439     TViewToPrs3d& aMap = theModule->myScalarBarsMap;
1440     TPrs3dToInd aPair; aPair.first=thePrs3d; aPair.second=pos;
1441     aMap[vw].insert(aPair);
1442   }
1443   
1444   void RemoveScalarBarPosition(VisuGUI* theModule,SVTK_ViewWindow* vw,VISU::Prs3d_i* thePrs3d){
1445     TViewToPrs3d& aMap = theModule->myScalarBarsMap;
1446     TSetPrs3d::iterator aIter = aMap[vw].begin();
1447     for(;aIter!=aMap[vw].end();aIter++)
1448       if((*aIter).first == thePrs3d){
1449         aMap[vw].erase(*aIter);
1450         return;
1451       }
1452   }
1453
1454   void RemoveScalarBarPosition(VisuGUI* theModule,VISU::Prs3d_i* thePrs3d){
1455     SVTK_ViewWindow* vw = GetViewWindow(theModule);
1456     RemoveScalarBarPosition(theModule,vw,thePrs3d);
1457   }
1458   
1459   void RemoveScalarBarPosition(VisuGUI* theModule,CORBA::Object_ptr theObject){
1460     if (!CORBA::is_nil(theObject)) {
1461       Base_var aBase = Base::_narrow(theObject);
1462       if (CORBA::is_nil(aBase)) return;
1463       if (VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aBase).in())){
1464         RemoveScalarBarPosition(theModule,aPrsObject);
1465       }
1466     }
1467   }
1468 }