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