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