Salome HOME
Merge remote-tracking branch 'origin/master' into V8_5_BR
[modules/smesh.git] / src / SMESH_SWIG_WITHIHM / libSMESH_Swig.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // SMESH SMESH : GUI for SMESH component
21 // File   : libSMESH_Swig.cxx
22 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
23 // SMESH includes
24 //
25 #include "libSMESH_Swig.h"
26
27 #include <SVTK_Selector.h>
28
29 #include <SMESHGUI.h>
30 #include <SMESHGUI_Utils.h>
31 #include <SMESHGUI_Displayer.h>
32 #include <SMESHGUI_VTKUtils.h>
33 #include <SMESH_Actor.h>
34
35 // SALOME KERNEL includes
36 #include <Utils_ORB_INIT.hxx>
37 #include <Utils_SINGLETON.hxx>
38 #include <SALOMEDSClient_ClientFactory.hxx>
39
40 #include <utilities.h>
41
42 // SALOME GUI includes
43 #include <SUIT_Session.h>
44 #include <SUIT_ViewManager.h>
45 #include <SALOME_Prs.h>
46 #include <SUIT_ViewWindow.h>
47 #include <SVTK_ViewWindow.h>
48 #include <VTKViewer_ViewModel.h>
49 #include <SALOME_Event.h>
50 #include <SalomeApp_Application.h>
51 #include <LightApp_SelectionMgr.h>
52 #include <SVTK_RenderWindowInteractor.h>
53 #include <VTKViewer_Algorithm.h>
54
55 // OCCT includes
56 #include <TopAbs.hxx>
57 #include <TColStd_MapOfInteger.hxx>
58
59 // Qt includes
60 #include <QApplication>
61
62 // IDL includes
63 #include <SALOMEconfig.h>
64 #include CORBA_SERVER_HEADER(SMESH_Gen)
65 #include CORBA_SERVER_HEADER(SMESH_Hypothesis)
66
67 // VTK includes
68 #include <vtkActorCollection.h>
69 #include <vtkRenderer.h>
70
71 static CORBA::ORB_var anORB;
72
73 namespace
74 {
75   //---------------------------------------------------------------
76   inline
77   CORBA::Object_var
78   StringToObject(const std::string& theIOR)
79   {
80     return anORB->string_to_object(theIOR.c_str());
81   }
82
83
84   //---------------------------------------------------------------
85   inline
86   SALOMEDS::SObject_var
87   GetDomainRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
88                 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
89                 CORBA::Long theDomainRootTag,
90                 const QString& theName,
91                 const QString& thePixmap)
92   {
93     SALOMEDS::SObject_var aDomainRoot;
94     if (!theSComponentMesh->FindSubObject(theDomainRootTag,aDomainRoot)) {
95       aDomainRoot = theStudyBuilder->NewObjectToTag(theSComponentMesh,theDomainRootTag);
96       SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeName");
97       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
98       aName->SetValue(theName.toLatin1().data());
99       aName->UnRegister();
100       anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributePixMap");
101       SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
102       aPixmap->SetPixMap(thePixmap.toLatin1().data());
103       aPixmap->UnRegister();
104       anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeSelectable");
105       SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
106       aSelAttr->SetSelectable(false);
107       aSelAttr->UnRegister();
108     }
109
110     return aDomainRoot;
111   }
112
113
114   //---------------------------------------------------------------
115   inline
116   SALOMEDS::SObject_var
117   GetHypothesisRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
118                     const SALOMEDS::StudyBuilder_var& theStudyBuilder)
119   {
120     return GetDomainRoot(theSComponentMesh,
121                          theStudyBuilder,
122                          SMESH::Tag_HypothesisRoot,
123                          QObject::tr("SMESH_MEN_HYPOTHESIS"),
124                          "ICON_SMESH_TREE_HYPO");
125   }
126
127
128   //---------------------------------------------------------------
129   inline
130   SALOMEDS::SObject_var
131   GetAlgorithmsRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
132                     const SALOMEDS::StudyBuilder_var& theStudyBuilder)
133   {
134     return GetDomainRoot(theSComponentMesh,
135                          theStudyBuilder,
136                          SMESH::Tag_AlgorithmsRoot,
137                          QObject::tr("SMESH_MEN_ALGORITHMS"),
138                          "ICON_SMESH_TREE_ALGO");
139   }
140
141
142   //---------------------------------------------------------------
143   inline
144   SALOMEDS::SObject_var
145   AddToDomain(const std::string&                theIOR,
146               const SALOMEDS::SComponent_var&   theSComponentMesh,
147               const SALOMEDS::StudyBuilder_var& theStudyBuilder,
148               CORBA::Long                       theDomainRootTag,
149               const QString&                    theDomainName,
150               const QString&                    theDomainPixmap)
151   {
152     SALOMEDS::SObject_var aDomain = GetDomainRoot(theSComponentMesh,
153                                                   theStudyBuilder,
154                                                   SMESH::Tag_AlgorithmsRoot,
155                                                   theDomainName,
156                                                   theDomainPixmap);
157     // Add New Hypothesis
158     SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(aDomain);
159     aDomain->UnRegister();
160     SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
161     SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
162     CORBA::Object_var anObject = StringToObject(theIOR);
163     SMESH::SMESH_Hypothesis_var aDomainItem = SMESH::SMESH_Hypothesis::_narrow(anObject.in());
164     CORBA::String_var aType = aDomainItem->GetName();
165     QString aPixmapName = theDomainPixmap + "_" + aType.in();
166     aPixmap->SetPixMap(aPixmapName.toLatin1().data());
167     aPixmap->UnRegister();
168     anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
169     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
170     anIOR->SetValue(theIOR.c_str());
171     anIOR->UnRegister();
172
173     return aSObject;
174   }
175
176
177   //---------------------------------------------------------------
178   SALOMEDS::SObject_var
179   AddHypothesis(const std::string& theIOR,
180                 const SALOMEDS::SComponent_var& theSComponentMesh,
181                 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
182   {
183     return AddToDomain(theIOR,
184                        theSComponentMesh,
185                        theStudyBuilder,
186                        SMESH::Tag_HypothesisRoot,
187                        QObject::tr("SMESH_MEN_HYPOTHESIS"),
188                        "ICON_SMESH_TREE_HYPO");
189   }
190
191
192   //---------------------------------------------------------------
193   SALOMEDS::SObject_var
194   AddAlgorithms(const std::string& theIOR,
195                 const SALOMEDS::SComponent_var& theSComponentMesh,
196                 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
197   {
198     return AddToDomain(theIOR,
199                        theSComponentMesh,
200                        theStudyBuilder,
201                        SMESH::Tag_AlgorithmsRoot,
202                        QObject::tr("SMESH_MEN_ALGORITHMS"),
203                        "ICON_SMESH_TREE_ALGO");
204   }
205
206
207   //---------------------------------------------------------------
208   void
209   SetDomain(const char*                       theMeshOrSubMeshEntry,
210             const char*                       theDomainEntry,
211             const SALOMEDS::Study_var&        theStudy,
212             const SALOMEDS::StudyBuilder_var& theStudyBuilder,
213             long                              theRefOnAppliedDomainTag,
214             const QString&                    theAppliedDomainMEN,
215             const QString&                    theAppliedDomainICON)
216   {
217     SALOMEDS::SObject_var aMeshOrSubMeshSO = theStudy->FindObjectID(theMeshOrSubMeshEntry);
218     SALOMEDS::SObject_var    aHypothesisSO = theStudy->FindObjectID(theDomainEntry);
219
220     if(!aMeshOrSubMeshSO->_is_nil() && !aHypothesisSO->_is_nil()){
221       //Find or Create Applied Hypothesis root
222       SALOMEDS::SObject_var anAppliedDomainSO;
223       if( !aMeshOrSubMeshSO->FindSubObject( theRefOnAppliedDomainTag, anAppliedDomainSO ))
224       {
225         anAppliedDomainSO = theStudyBuilder->NewObjectToTag(aMeshOrSubMeshSO,theRefOnAppliedDomainTag);
226         SALOMEDS::GenericAttribute_var anAttr =
227           theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeName");
228         SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
229         aName->SetValue(theAppliedDomainMEN.toLatin1().data());
230         aName->UnRegister();
231         anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeSelectable");
232         SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
233         aSelAttr->SetSelectable(false);
234         aSelAttr->UnRegister();
235         anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributePixMap");
236         SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
237         aPixmap->SetPixMap(theAppliedDomainICON.toLatin1().data());
238         aPixmap->UnRegister();
239       }
240       SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(anAppliedDomainSO);
241       theStudyBuilder->Addreference(aSObject,aHypothesisSO);
242       aSObject->UnRegister();
243       anAppliedDomainSO->UnRegister();
244     }
245     if ( !aMeshOrSubMeshSO->_is_nil() ) aMeshOrSubMeshSO->UnRegister();
246     if ( !aHypothesisSO->_is_nil())     aHypothesisSO->UnRegister();
247   }
248
249
250   //---------------------------------------------------------------
251   void
252   SetHypothesis(const char*                       theMeshOrSubMeshEntry,
253                 const char*                       theDomainEntry,
254                 const SALOMEDS::Study_var&        theStudy,
255                 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
256   {
257     SetDomain(theMeshOrSubMeshEntry,
258               theDomainEntry,
259               theStudy,
260               theStudyBuilder,
261               SMESH::Tag_RefOnAppliedHypothesis,
262               QObject::tr("SMESH_MEN_APPLIED_HYPOTHESIS"),
263               "ICON_SMESH_TREE_HYPO");
264   }
265
266
267   //---------------------------------------------------------------
268   void
269   SetAlgorithms(const char*                       theMeshOrSubMeshEntry,
270                 const char*                       theDomainEntry,
271                 const SALOMEDS::Study_var&        theStudy,
272                 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
273   {
274     SetDomain(theMeshOrSubMeshEntry,
275               theDomainEntry,
276               theStudy,
277               theStudyBuilder,
278               SMESH::Tag_RefOnAppliedAlgorithms,
279               QObject::tr("SMESH_MEN_APPLIED_ALGORIHTMS"),
280               "ICON_SMESH_TREE_ALGO");
281   }
282 }
283
284
285 //===============================================================
286 SMESH_Swig::SMESH_Swig()
287 {
288   class TEvent: public SALOME_Event
289   {
290     CORBA::ORB_var& myORB;
291   public:
292
293     TEvent(CORBA::ORB_var& theORB):
294       myORB(theORB)
295     {}
296
297     virtual
298     void
299     Execute()
300     {
301       try {
302         ORB_INIT &anORBInit = *SINGLETON_<ORB_INIT>::Instance();
303         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
304         myORB = anORBInit( 0, 0 );
305       } catch (...) {
306         INFOS("internal error : orb not found");
307       }
308     }
309   };
310
311   //MESSAGE("Constructeur");
312
313   if(CORBA::is_nil(anORB))
314     ProcessVoidEvent(new TEvent(anORB));
315
316   ASSERT(!CORBA::is_nil(anORB));
317 }
318
319
320 //===============================================================
321 void
322 SMESH_Swig::Init(int theStudyID)
323 {
324   class TEvent: public SALOME_Event
325   {
326     int                         myStudyID;
327     SALOMEDS::Study_var&        myStudy;
328     SALOMEDS::StudyBuilder_var& myStudyBuilder;
329     SALOMEDS::SComponent_var&   mySComponentMesh;
330   public:
331     TEvent(int                         theStudyID,
332            SALOMEDS::Study_var&        theStudy,
333            SALOMEDS::StudyBuilder_var& theStudyBuilder,
334            SALOMEDS::SComponent_var&   theSComponentMesh):
335       myStudyID       (theStudyID),
336       myStudy         (theStudy),
337       myStudyBuilder  (theStudyBuilder),
338       mySComponentMesh(theSComponentMesh)
339     {}
340
341     ~TEvent()
342     {
343       if ( !mySComponentMesh->_is_nil() ) mySComponentMesh->UnRegister();
344     }
345
346     virtual
347     void
348     Execute()
349     {
350       SUIT_Session* aSession          = SUIT_Session::session();
351       SUIT_Application* anApplication = aSession->activeApplication();
352       SalomeApp_Application* anApp    = dynamic_cast<SalomeApp_Application*>(anApplication);
353
354       SALOME_NamingService* aNamingService = anApp->namingService();
355       CORBA::Object_var anObject           = aNamingService->Resolve("/myStudyManager");
356       SALOMEDS::StudyManager_var aStudyMgr = SALOMEDS::StudyManager::_narrow(anObject);
357       myStudy = aStudyMgr->GetStudyByID(myStudyID);
358
359       SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
360       aSMESHGen->SetCurrentStudy( myStudy.in() );
361
362       myStudyBuilder = myStudy->NewBuilder();
363
364       SALOMEDS::GenericAttribute_var anAttr;
365       SALOMEDS::AttributeName_var    aName;
366       SALOMEDS::AttributePixMap_var  aPixmap;
367
368       SALOMEDS::SComponent_var aSComponent = myStudy->FindComponent("SMESH");
369       if ( aSComponent->_is_nil() )
370       {
371         bool aLocked = myStudy->GetProperties()->IsLocked();
372         if (aLocked)
373           myStudy->GetProperties()->SetLocked(false);
374
375         SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI();
376         //SRN: BugID IPAL9186, load a SMESH gui if it hasn't been loaded
377         if (!aSMESHGUI) {
378           CAM_Module* aModule = anApp->module("Mesh");
379           if(!aModule)
380             aModule = anApp->loadModule("Mesh");
381           aSMESHGUI = dynamic_cast<SMESHGUI*>(aModule);
382         } //SRN: BugID IPAL9186: end of a fix
383
384         aSComponent = myStudyBuilder->NewComponent("SMESH");
385
386         anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributeName");
387         aName  = SALOMEDS::AttributeName::_narrow(anAttr);
388         aName->SetValue(aSMESHGUI->moduleName().toLatin1().data());
389         aName->UnRegister();
390
391         anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributePixMap");
392         aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
393         aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
394         aPixmap->UnRegister();
395
396         SALOMEDS::UseCaseBuilder_var useCaseBuilder = myStudy->GetUseCaseBuilder();
397         useCaseBuilder->SetRootCurrent();
398         useCaseBuilder->Append( aSComponent.in() );
399   
400         myStudyBuilder->DefineComponentInstance(aSComponent,aSMESHGen);
401         if (aLocked)
402           myStudy->GetProperties()->SetLocked(true);
403       }
404
405       mySComponentMesh = SALOMEDS::SComponent::_narrow(aSComponent);
406
407       qApp->processEvents(); // Workaround for bug 12662
408     }
409   };
410
411   //MESSAGE("Init");
412
413   ProcessVoidEvent(new TEvent(theStudyID,
414                               myStudy,
415                               myStudyBuilder,
416                               mySComponentMesh));
417 }
418
419
420 //===============================================================
421 SMESH_Swig::~SMESH_Swig()
422 {
423   //MESSAGE("Destructeur");
424 }
425
426
427 //===============================================================
428 const char* SMESH_Swig::AddNewMesh(const char* theIOR)
429 {
430
431   // VSR: added temporarily - to be removed - objects are published automatically by engine
432   SALOMEDS::SObject_var aSObject = myStudy->FindObjectIOR(theIOR);
433   if (aSObject->_is_nil())
434   {
435     //Find or Create Hypothesis root
436     SALOMEDS::SObject_var hroot = GetHypothesisRoot(mySComponentMesh,myStudyBuilder);
437     SALOMEDS::SObject_var aroot = GetAlgorithmsRoot(mySComponentMesh,myStudyBuilder);
438     hroot->UnRegister();
439     aroot->UnRegister();
440
441     // Add New Mesh
442     aSObject = myStudyBuilder->NewObject(mySComponentMesh);
443     SALOMEDS::GenericAttribute_var anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
444     SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
445     aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
446     aPixmap->UnRegister();
447
448     anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeIOR");
449     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
450     anIOR->SetValue(theIOR);
451     anIOR->UnRegister();
452   }
453
454   CORBA::String_var anEntry = aSObject->GetID();
455   aSObject->UnRegister();
456
457   return anEntry._retn();
458 }
459
460
461 //===============================================================
462 const char* SMESH_Swig::AddNewHypothesis(const char* theIOR)
463 {
464   SALOMEDS::SObject_var aSObject = ::AddHypothesis(theIOR,
465                                                    mySComponentMesh,
466                                                    myStudyBuilder);
467   CORBA::String_var anEntry = aSObject->GetID();
468   aSObject->UnRegister();
469
470   return anEntry._retn();
471 }
472
473
474 //===============================================================
475 const char* SMESH_Swig::AddNewAlgorithms(const char* theIOR)
476 {
477   SALOMEDS::SObject_var aSObject = ::AddAlgorithms(theIOR,
478                                                    mySComponentMesh,
479                                                    myStudyBuilder);
480   CORBA::String_var anEntry = aSObject->GetID();
481   aSObject->UnRegister();
482
483   return anEntry._retn();
484 }
485
486
487 //===============================================================
488 void SMESH_Swig::SetShape(const char* theShapeEntry,
489                           const char* theMeshEntry)
490 {
491   SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID( theShapeEntry );
492   SALOMEDS::SObject_var      aMeshSO = myStudy->FindObjectID( theMeshEntry );
493
494   if(!aMeshSO->_is_nil() && !aGeomShapeSO->_is_nil()){
495     SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObjectToTag(aMeshSO, SMESH::Tag_RefOnShape);
496     myStudyBuilder->Addreference(aSObject,aGeomShapeSO);
497     aSObject->UnRegister();
498   }
499   if ( !aMeshSO->_is_nil() )      aMeshSO->UnRegister();
500   if ( !aGeomShapeSO->_is_nil() ) aGeomShapeSO->UnRegister();
501 }
502
503
504 //===============================================================
505 void SMESH_Swig::SetHypothesis(const char* theMeshOrSubMeshEntry,
506                                const char* theDomainEntry)
507 {
508   ::SetHypothesis(theMeshOrSubMeshEntry,
509                   theDomainEntry,
510                   myStudy,
511                   myStudyBuilder);
512 }
513
514
515 //===============================================================
516 void SMESH_Swig::SetAlgorithms(const char* theMeshOrSubMeshEntry,
517                                const char* theDomainEntry)
518 {
519   ::SetAlgorithms(theMeshOrSubMeshEntry,
520                   theDomainEntry,
521                   myStudy,
522                   myStudyBuilder);
523 }
524
525
526 //===============================================================
527 void
528 SMESH_Swig::UnSetHypothesis(const char* theDomainEntry)
529 {
530   SALOMEDS::SObject_var aDomainSO = myStudy->FindObjectID(theDomainEntry);
531   if(!aDomainSO->_is_nil())
532     myStudyBuilder->RemoveObject(aDomainSO);
533 }
534
535 const char* SMESH_Swig::AddSubMesh(const char* theMeshEntry,
536                                    const char* theSubMeshIOR,
537                                    int theShapeType)
538 {
539   SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(theMeshEntry);
540   if(!aMeshSO->_is_nil()) {
541     long aShapeTag;
542     QString aSubMeshName;
543     switch(theShapeType) {
544     case TopAbs_SOLID:
545       aShapeTag    = SMESH::Tag_SubMeshOnSolid;
546       aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnSolid");
547       break;
548     case TopAbs_FACE:
549       aShapeTag    = SMESH::Tag_SubMeshOnFace;
550       aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnFace");
551       break;
552     case TopAbs_EDGE:
553       aShapeTag    = SMESH::Tag_SubMeshOnEdge;
554       aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnEdge");
555       break;
556     case TopAbs_VERTEX:
557       aShapeTag    = SMESH::Tag_SubMeshOnVertex;
558       aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnVertex");
559       break;
560     default:
561       aShapeTag    = SMESH::Tag_SubMeshOnCompound;
562       aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnCompound");
563     }
564
565     SALOMEDS::GenericAttribute_var anAttr;
566     SALOMEDS::SObject_var aSubMeshesRoot;
567     if ( !aMeshSO->FindSubObject( aShapeTag, aSubMeshesRoot ) )
568     {
569       aSubMeshesRoot = myStudyBuilder->NewObjectToTag(aMeshSO,aShapeTag);
570       anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeName");
571       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
572       aName->SetValue(aSubMeshName.toLatin1().data());
573       aName->UnRegister();
574       anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeSelectable");
575       SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
576       aSelAttr->SetSelectable(false);
577       aSelAttr->UnRegister();
578     }
579     aSubMeshesRoot->UnRegister();
580     aMeshSO->UnRegister();
581
582     SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObject(aSubMeshesRoot);
583     anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
584     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
585     anIOR->SetValue(theSubMeshIOR);
586     anIOR->UnRegister();
587
588     CORBA::String_var aString = aSObject->GetID();
589     aSObject->UnRegister();
590
591     return aString._retn();
592   }
593
594   return "";
595 }
596
597 const char* SMESH_Swig::AddSubMeshOnShape(const char* theMeshEntry,
598                                           const char* theGeomShapeEntry,
599                                           const char* theSubMeshIOR,
600                                           int         ShapeType)
601 {
602   SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID(theGeomShapeEntry);
603   if(!aGeomShapeSO->_is_nil())
604   {
605     const char *       aSubMeshEntry = AddSubMesh(theMeshEntry,theSubMeshIOR,ShapeType);
606     SALOMEDS::SObject_var aSubMeshSO = myStudy->FindObjectID(aSubMeshEntry);
607     if ( !aSubMeshSO->_is_nil()) {
608       SetShape( theGeomShapeEntry, aSubMeshEntry );
609       CORBA::String_var aString = aSubMeshSO->GetID();
610       aSubMeshSO->UnRegister();
611       return aString._retn();
612     }
613     aGeomShapeSO->UnRegister();
614   }
615
616   return "";
617 }
618
619 /*!
620   \brief Gets window with specified identifier
621   \internal
622   \param id window identifier
623   \return pointer on the window
624 */
625
626 SUIT_ViewWindow* getWnd( const int id )
627 {
628   SUIT_ViewWindow* resWnd = 0;
629   SUIT_Session* aSession          = SUIT_Session::session();
630   SUIT_Application* anApplication = aSession->activeApplication();
631   SalomeApp_Application* app    = dynamic_cast<SalomeApp_Application*>(anApplication);
632   if ( app ) {
633     ViewManagerList vmlist = app->viewManagers();
634     foreach( SUIT_ViewManager* vm, vmlist ) {
635       QVector<SUIT_ViewWindow*> vwlist = vm->getViews();
636       foreach ( SUIT_ViewWindow* vw, vwlist ) {
637         if ( id == vw->getId() ) {
638           resWnd = vw;
639           break;
640         }
641       }
642     }
643   }
644   return resWnd;
645 }
646
647 class TGetActorAspect: public SALOME_Event
648 {
649 public:
650   typedef actorAspect TResult;
651   TResult myResult;
652   const char* _entry;
653   int _wid;
654   TGetActorAspect( const char* Mesh_Entry, int viewId )
655   {
656     _entry = Mesh_Entry;
657     _wid = viewId;
658   }
659   virtual void Execute()
660     {
661       SMESH_Actor* anActor;
662       if (_wid)
663         {
664           SUIT_ViewWindow* w = getWnd(_wid);
665           anActor = SMESH::FindActorByEntry( w, _entry );
666         }
667       else
668         anActor = SMESH::FindActorByEntry( _entry );
669       if ( !anActor )
670         {
671           MESSAGE("GetActorAspect: no actor corresponding to: " << _entry);
672           return;
673         }
674       anActor->GetSufaceColor(myResult.surfaceColor.r,
675                               myResult.surfaceColor.g,
676                               myResult.surfaceColor.b,
677                               myResult.surfaceColor.delta);
678       anActor->GetVolumeColor(myResult.volumeColor.r,
679                               myResult.volumeColor.g,
680                               myResult.volumeColor.b,
681                               myResult.volumeColor.delta);
682       anActor->GetEdgeColor(myResult.edgeColor.r,
683                             myResult.edgeColor.g,
684                             myResult.edgeColor.b);
685       anActor->GetNodeColor(myResult.nodeColor.r,
686                             myResult.nodeColor.g,
687                             myResult.nodeColor.b);
688       myResult.opacity= anActor->GetOpacity();
689       MESSAGE("opacity: " << myResult.opacity);
690     }
691 };
692
693 actorAspect SMESH_Swig::GetActorAspect( const char* Mesh_Entry, int viewId )
694 {
695   return ProcessEvent(new TGetActorAspect( Mesh_Entry, viewId));
696 }
697
698 void SMESH_Swig::SetActorAspect( const actorAspect& actorPres, const char* Mesh_Entry, int viewId )
699 {
700   class TSetActorAspect: public SALOME_Event
701   {
702   public:
703     const char* _entry;
704     actorAspect _actorPres;
705     int _wid;
706     TSetActorAspect(const actorAspect& actorPres, const char* Mesh_Entry, int viewId )
707     {
708       _entry = Mesh_Entry;
709       _actorPres = actorPres;
710       _wid = viewId;
711     }
712     virtual void Execute()
713     {
714       SMESH_Actor* anActor;
715       if (_wid)
716         {
717           SUIT_ViewWindow* w = getWnd(_wid);
718           anActor = SMESH::FindActorByEntry( w, _entry );
719         }
720       else
721         anActor = SMESH::FindActorByEntry( _entry );
722       if ( !anActor )
723         {
724           MESSAGE("SetActorAspect: no actor corresponding to: " << _entry);
725           return;
726         }
727       anActor->SetSufaceColor(_actorPres.surfaceColor.r,
728                               _actorPres.surfaceColor.g,
729                               _actorPres.surfaceColor.b,
730                               _actorPres.surfaceColor.delta);
731       anActor->SetVolumeColor(_actorPres.volumeColor.r,
732                               _actorPres.volumeColor.g,
733                               _actorPres.volumeColor.b,
734                               _actorPres.volumeColor.delta);
735       anActor->SetEdgeColor(_actorPres.edgeColor.r,
736                             _actorPres.edgeColor.g,
737                             _actorPres.edgeColor.b);
738       anActor->SetNodeColor(_actorPres.nodeColor.r,
739                             _actorPres.nodeColor.g,
740                             _actorPres.nodeColor.b);
741       anActor->SetOpacity(_actorPres.opacity);
742       if (_wid)
743         {
744           SUIT_ViewWindow* w = getWnd(_wid);
745           w->repaint();
746         }
747       else
748         {
749           SUIT_Session* aSession          = SUIT_Session::session();
750           SUIT_Application* anApplication = aSession->activeApplication();
751           SalomeApp_Application* anApp    = dynamic_cast<SalomeApp_Application*>(anApplication);
752           SUIT_ViewManager* vman          = anApp->getViewManager(VTKViewer_Viewer::Type(),true);
753           vman->getActiveView()->repaint();
754         }
755     }
756   };
757
758   ProcessVoidEvent(new TSetActorAspect(actorPres, Mesh_Entry, viewId));
759 }
760
761 void SMESH_Swig::CreateAndDisplayActor( const char* Mesh_Entry )
762 {
763   //  SMESH_Actor* Mesh = smeshGUI->ReadScript(aM);
764   class TEvent: public SALOME_Event
765   {
766   private:
767     const char* _entry;
768   public:
769     TEvent(const char* Mesh_Entry) {
770       _entry = Mesh_Entry;
771     }
772     virtual void Execute() {
773       //SMESH::UpdateView(SMESH::eDisplay, _entry);
774       SUIT_Session* aSession          = SUIT_Session::session();
775       SUIT_Application* anApplication = aSession->activeApplication();
776       SalomeApp_Application* anApp    = dynamic_cast<SalomeApp_Application*>(anApplication);
777       /*SUIT_ViewManager* vman        = */anApp->getViewManager(VTKViewer_Viewer::Type(),true);
778       SMESHGUI_Displayer* aDisp       = new SMESHGUI_Displayer(anApp);
779       aDisp->Display(_entry,1);
780     }
781   };
782
783   ProcessVoidEvent(new TEvent(Mesh_Entry));
784 }
785
786 void SMESH_Swig::EraseActor( const char* Mesh_Entry, const bool allViewers )
787 {
788   class TEvent: public SALOME_Event
789   {
790   private:
791     const char* _entry;
792     bool        _allViewers;
793   public:
794     TEvent(const char* Mesh_Entry, const bool allViewers ) {
795       _entry = Mesh_Entry;
796       _allViewers = allViewers;
797     }
798     virtual void Execute() {
799       SUIT_Session* aSession          = SUIT_Session::session();
800       SUIT_Application* anApplication = aSession->activeApplication();
801       SalomeApp_Application* anApp    = dynamic_cast<SalomeApp_Application*>(anApplication);
802       SMESHGUI_Displayer* aDisp       = new SMESHGUI_Displayer(anApp);
803       ViewManagerList aManagers;
804       if ( !_allViewers ) {
805         aManagers << anApp->activeViewManager();
806       }
807       else {
808         aManagers = anApp->viewManagers();
809       }
810       foreach( SUIT_ViewManager* aMgr, aManagers ) {
811         if ( aMgr && aMgr->getType() == VTKViewer_Viewer::Type() ) {
812           SALOME_View* aSalomeView = dynamic_cast<SALOME_View*>(aMgr->getViewModel());
813           if (aSalomeView) {
814             aDisp->Erase(_entry,true, true, aSalomeView);
815           }
816         }
817       }
818     }
819   };
820
821   ProcessVoidEvent(new TEvent(Mesh_Entry, allViewers));
822 }
823
824 void SMESH_Swig::UpdateActor( const char* Mesh_Entry ) {
825   class TEvent: public SALOME_Event
826   {
827   private:
828     const char* _entry;
829   public:
830     TEvent( const char* Mesh_Entry ) {
831       _entry = Mesh_Entry;
832     }
833     virtual void Execute() {
834       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
835         ( _entry, "SMESH", "" );
836       SMESH::Update( anIO, true );
837     }
838   };
839
840   ProcessVoidEvent( new TEvent(Mesh_Entry) );
841 }
842
843 void SMESH_Swig::SetName(const char* theEntry,
844                          const char* theName)
845 {
846   SALOMEDS::SObject_var aSObject = myStudy->FindObjectID(theEntry);
847   SALOMEDS::GenericAttribute_var anAttr;
848   SALOMEDS::AttributeName_var aName;
849   if(!aSObject->_is_nil()){
850     anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeName");
851     aName = SALOMEDS::AttributeName::_narrow(anAttr);
852     aName->SetValue(theName);
853     aName->UnRegister();
854     aSObject->UnRegister();
855   }
856 }
857
858 //================================================================================
859 /*!
860  * \brief Set mesh icon according to compute status
861   * \param Mesh_Entry - entry of a mesh
862   * \param isComputed - is mesh computed or not
863  */
864 //================================================================================
865
866 void SMESH_Swig::SetMeshIcon(const char* theMeshEntry,
867                              const bool  theIsComputed,
868                              const bool  isEmpty)
869 {
870   class TEvent: public SALOME_Event
871   {
872     SALOMEDS::Study_var myStudy;
873     std::string         myMeshEntry;
874     bool                myIsComputed, myIsEmpty;
875   public:
876     TEvent(const SALOMEDS::Study_var& theStudy,
877            const std::string&         theMeshEntry,
878            const bool                 theIsComputed,
879            const bool                 isEmpty):
880       myStudy     (theStudy),
881       myMeshEntry (theMeshEntry),
882       myIsComputed(theIsComputed),
883       myIsEmpty   (isEmpty)
884     {}
885
886     virtual
887     void
888     Execute()
889     {
890       SALOMEDS::SObject_ptr aMeshSO = myStudy->FindObjectID(myMeshEntry.c_str());
891       if(_PTR(SObject) aMesh = ClientFactory::SObject(aMeshSO))
892         SMESH::ModifiedMesh(aMesh,myIsComputed,myIsEmpty);
893       // aMeshSO->UnRegister();  ~aMesh() already called UnRegister()!
894     }
895   };
896
897   ProcessVoidEvent(new TEvent(myStudy,
898                               theMeshEntry,
899                               theIsComputed,
900                               isEmpty));
901 }
902
903 /*!
904   \brief Helper class for selection event.
905 */
906 class TSelectListEvent: public SALOME_Event
907 {
908   const char*       myId;
909   std::vector<int>  myIdsList;
910   bool              myIsAppend;
911
912 public:
913   TSelectListEvent(const char* id, std::vector<int> ids, bool append) :
914     myId(id),
915     myIdsList(ids),
916     myIsAppend(append)
917   {}
918   virtual void Execute()
919   {
920     
921     LightApp_SelectionMgr* selMgr = 0;
922     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
923     if( anApp )
924       selMgr = dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
925
926     if( !selMgr )
927       return;
928     
929     selMgr->clearFilters();
930
931     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow();
932     if(!aViewWindow)
933       return;
934
935     SMESH_Actor* anActor = SMESH::FindActorByEntry( myId );
936     
937     if (!anActor || !anActor->hasIO())
938       return;
939     
940     Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
941     SALOME_ListIO aList;
942     aList.Append(anIO);
943     selMgr->setSelectedObjects(aList, false);
944
945     if ( aViewWindow->SelectionMode() ==  ActorSelection ) {
946       return;
947     }
948         
949     TColStd_MapOfInteger aMap;
950     std::vector<int>::const_iterator anIter;
951     for (anIter = myIdsList.begin(); anIter != myIdsList.end(); ++anIter) {
952       aMap.Add(*anIter);
953     }
954
955     // Set new selection
956     SVTK_Selector* aSelector  = aViewWindow->GetSelector();
957     aSelector->AddOrRemoveIndex(anIO, aMap, myIsAppend);
958     aViewWindow->highlight( anIO, true, true );
959     aViewWindow->GetInteractor()->onEmitSelectionChanged();
960   }
961 };
962
963 /*!
964   \brief Select the elements on the mesh, sub-mesh or group.
965   \param id object entry
966   \param ids list of the element ids
967   \param mode selection mode
968 */
969 void SMESH_Swig::select( const char* id, std::vector<int> ids, bool append ) {
970   ProcessVoidEvent( new TSelectListEvent( id, ids, append ) );
971 }
972   
973 /*!
974   \brief Select the elements on the mesh, sub-mesh or group.
975   \param id object entry
976   \param id id of the element
977   \param mode selection mode
978 */
979 void SMESH_Swig::select( const char* id, int id1, bool append ) {
980   std::vector<int> ids;
981   ids.push_back( id1 );
982   ProcessVoidEvent( new TSelectListEvent( id, ids, append ) );
983 }
984
985 /*!
986   \brief Helper class for selection edges of cell event
987 */
988 class TSelectListOfPairEvent: public SALOME_Event
989 {
990   const char*                        myId;
991   std::vector<std::pair<int, int> >  myIdsList;
992   bool                               myIsAppend;
993
994 public:
995   TSelectListOfPairEvent(const char* id, std::vector<std::pair<int, int> > ids, bool append) :
996     myId(id),
997     myIdsList(ids),
998     myIsAppend(append)
999   {}
1000   virtual void Execute()
1001   {
1002     
1003     LightApp_SelectionMgr* selMgr = 0;
1004     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1005     if( anApp )
1006       selMgr = dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
1007
1008     if( !selMgr )
1009       return;
1010     
1011     selMgr->clearFilters();
1012
1013     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow();
1014     if(!aViewWindow)
1015       return;
1016
1017     SMESH_Actor* anActor = SMESH::FindActorByEntry( myId );
1018     
1019     if (!anActor || !anActor->hasIO())
1020       return;
1021     
1022     Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
1023     SALOME_ListIO aList;
1024     aList.Append(anIO);
1025     selMgr->setSelectedObjects(aList, false);
1026
1027     if ( aViewWindow->SelectionMode() !=  EdgeOfCellSelection ) {
1028       return;
1029     }
1030         
1031     SVTK_IndexedMapOfIds aMap;
1032     std::vector<std::pair<int, int> >::const_iterator anIter;
1033     for (anIter = myIdsList.begin(); anIter != myIdsList.end(); ++anIter) {
1034       std::vector<int> aCompositeId;
1035       aCompositeId.push_back((*anIter).first);
1036       aCompositeId.push_back((*anIter).second);
1037       aMap.Add(aCompositeId);
1038     }
1039
1040     // Set new selection
1041     SVTK_Selector* aSelector  = aViewWindow->GetSelector();
1042     aSelector->AddOrRemoveCompositeIndex(anIO, aMap, myIsAppend);
1043     aViewWindow->highlight( anIO, true, true );
1044     aViewWindow->GetInteractor()->onEmitSelectionChanged();
1045   }
1046 };
1047
1048 /*!
1049   \brief Select the elements on the mesh, sub-mesh or group.
1050   \param id object entry
1051   \param ids list of the element ids
1052   \param mode selection mode
1053 */
1054 void SMESH_Swig::select( const char* id, std::vector<std::pair<int,int> > ids, bool append ) {
1055   ProcessVoidEvent( new TSelectListOfPairEvent( id, ids, append ) );
1056 }
1057
1058 class TGetSelectionModeEvent : public SALOME_Event
1059 {
1060 public:
1061   typedef SelectionMode TResult;
1062   TResult myResult;
1063   TGetSelectionModeEvent() : myResult( Undefined ) {}
1064   virtual void Execute()
1065   {
1066     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( );
1067     if(!aViewWindow)
1068       return;
1069     
1070     myResult = (SelectionMode) aViewWindow->SelectionMode();
1071   }
1072 };
1073
1074 /*!
1075   \brief Get selection mode of the active VTK View window.
1076 */
1077 SelectionMode SMESH_Swig::getSelectionMode() {
1078   return ProcessEvent( new TGetSelectionModeEvent() );
1079 }
1080
1081
1082 /*!
1083  * Event to set selection mode
1084 */
1085 class TSetSelectionModeEvent : public SALOME_Event
1086 {
1087   SelectionMode mySelectionMode;
1088
1089 public:
1090
1091   TSetSelectionModeEvent(const SelectionMode selectionMode) :
1092     mySelectionMode(selectionMode) 
1093   {}
1094
1095   virtual void Execute()
1096   {
1097     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow();
1098     if(!aViewWindow)
1099       return;
1100
1101     Selection_Mode prevMode = aViewWindow->SelectionMode();
1102     bool changePointRepresentation = ( prevMode == NodeSelection && mySelectionMode != Node ) ||
1103       (prevMode != NodeSelection && mySelectionMode == Node);
1104       
1105     if( changePointRepresentation ) {
1106       vtkRenderer *aRenderer = aViewWindow->getRenderer();
1107       VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1108       vtkActorCollection *aCollection = aCopy.GetActors();
1109       aCollection->InitTraversal();
1110       while(vtkActor *anAct = aCollection->GetNextActor()){
1111         if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
1112           if(anActor->GetVisibility()){
1113             anActor->SetPointRepresentation(mySelectionMode == Node);
1114           }
1115         }
1116       }
1117     }
1118     aViewWindow->SetSelectionMode(mySelectionMode);
1119   }
1120 };
1121
1122 void SMESH_Swig::setSelectionMode(SelectionMode selectionMode){
1123   ProcessVoidEvent( new TSetSelectionModeEvent( selectionMode ) ); 
1124 }
1125
1126 class TGetSelectedEvent : public SALOME_Event
1127 {
1128 public:
1129   typedef std::vector<int> TResult;
1130   TResult myResult;
1131   const char* myId;
1132   
1133   TGetSelectedEvent( const char* id) : 
1134     myResult( std::vector<int>() ),
1135     myId(id)
1136   {}
1137   
1138   virtual void Execute()
1139   {
1140     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow();
1141     if( !aViewWindow )
1142       return;
1143
1144     SVTK_Selector* aSelector  = aViewWindow->GetSelector();    
1145     if( !aSelector )
1146       return;
1147
1148     SMESH_Actor* anActor = SMESH::FindActorByEntry( myId );
1149     
1150     if ( !anActor || !anActor->hasIO() )
1151       return;
1152
1153     TColStd_IndexedMapOfInteger aMapIndex;
1154     aSelector->GetIndex(anActor->getIO(),aMapIndex);
1155
1156     for( int i = 1; i <= aMapIndex.Extent(); i++ )
1157       myResult.push_back( aMapIndex( i ) );
1158   }
1159 };
1160
1161 std::vector<int> SMESH_Swig::getSelected( const char* Mesh_Entry ) {
1162   return ProcessEvent( new TGetSelectedEvent(Mesh_Entry) );
1163 }
1164
1165 class TGetSelectedPairEvent : public SALOME_Event
1166 {
1167 public:
1168   typedef std::vector<std::pair<int, int> > TResult;
1169   TResult myResult;
1170   const char* myId;
1171   
1172   TGetSelectedPairEvent( const char* id) : 
1173     myResult( std::vector<std::pair<int,int> >() ),
1174     myId(id)
1175   {}
1176   
1177   virtual void Execute()
1178   {
1179     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow();
1180     if( !aViewWindow )
1181       return;
1182
1183     if(aViewWindow->SelectionMode() != EdgeOfCellSelection )
1184       return;
1185
1186     SVTK_Selector* aSelector  = aViewWindow->GetSelector();    
1187     if( !aSelector )
1188       return;
1189
1190     SMESH_Actor* anActor = SMESH::FindActorByEntry( myId );
1191     
1192     if ( !anActor || !anActor->hasIO() )
1193       return;
1194
1195     SVTK_IndexedMapOfIds aMapIndex;
1196     aSelector->GetCompositeIndex(anActor->getIO(),aMapIndex);
1197
1198     for( int i = 1; i <= aMapIndex.Extent(); i++ )
1199       myResult.push_back( std::make_pair<int,int>( (int)aMapIndex( i )[0], (int)aMapIndex( i )[1]) );
1200   }
1201 };
1202
1203 std::vector<std::pair<int,int> > SMESH_Swig::getSelectedEdgeOfCell( const char* Mesh_Entry ) {
1204   return ProcessEvent( new TGetSelectedPairEvent(Mesh_Entry) );
1205 }
1206