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