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