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