1 // SMESH SMESH : GUI for SMESH component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : libSMESH_Swig.cxx
23 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
27 #include "libSMESH_Swig.h"
30 #include <SMESHGUI_Utils.h>
31 #include <SMESHGUI_Displayer.h>
33 // SALOME KERNEL includes
34 #include <Utils_ORB_INIT.hxx>
35 #include <Utils_SINGLETON.hxx>
36 #include <SALOMEDSClient_ClientFactory.hxx>
38 #include <utilities.h>
40 // SALOME GUI includes
41 #include <SUIT_Session.h>
42 #include <VTKViewer_ViewModel.h>
43 #include <SALOME_Event.h>
44 #include <SalomeApp_Application.h>
50 #include <QApplication>
53 #include <SALOMEconfig.h>
54 #include CORBA_SERVER_HEADER(SMESH_Gen)
55 #include CORBA_SERVER_HEADER(SMESH_Hypothesis)
57 static CORBA::ORB_var anORB;
61 //---------------------------------------------------------------
64 StringToObject(const std::string& theIOR)
66 return anORB->string_to_object(theIOR.c_str());
70 //---------------------------------------------------------------
73 GetDomainRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
74 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
75 CORBA::Long theDomainRootTag,
76 const QString& theName,
77 const QString& thePixmap)
79 SALOMEDS::SObject_var aDomainRoot;
80 if (!theSComponentMesh->FindSubObject(theDomainRootTag,aDomainRoot)) {
81 aDomainRoot = theStudyBuilder->NewObjectToTag(theSComponentMesh,theDomainRootTag);
82 SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeName");
83 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
84 aName->SetValue(theName.toLatin1().data());
85 anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributePixMap");
86 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
87 aPixmap->SetPixMap(thePixmap.toLatin1().data());
88 anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeSelectable");
89 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
90 aSelAttr->SetSelectable(false);
97 //---------------------------------------------------------------
100 GetHypothesisRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
101 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
103 return GetDomainRoot(theSComponentMesh,
105 SMESH::Tag_HypothesisRoot,
106 QObject::tr("SMESH_MEN_HYPOTHESIS"),
107 "ICON_SMESH_TREE_HYPO");
111 //---------------------------------------------------------------
113 SALOMEDS::SObject_var
114 GetAlgorithmsRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
115 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
117 return GetDomainRoot(theSComponentMesh,
119 SMESH::Tag_AlgorithmsRoot,
120 QObject::tr("SMESH_MEN_ALGORITHMS"),
121 "ICON_SMESH_TREE_ALGO");
125 //---------------------------------------------------------------
127 SALOMEDS::SObject_var
128 AddToDomain(const std::string& theIOR,
129 const SALOMEDS::SComponent_var& theSComponentMesh,
130 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
131 CORBA::Long theDomainRootTag,
132 const QString& theDomainName,
133 const QString& theDomainPixmap)
135 SALOMEDS::SObject_var aDomain = GetDomainRoot(theSComponentMesh,
137 SMESH::Tag_AlgorithmsRoot,
140 // Add New Hypothesis
141 SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(aDomain);
142 SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
143 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
144 CORBA::Object_var anObject = StringToObject(theIOR);
145 SMESH::SMESH_Hypothesis_var aDomainItem = SMESH::SMESH_Hypothesis::_narrow(anObject.in());
146 CORBA::String_var aType = aDomainItem->GetName();
147 QString aPixmapName = theDomainPixmap + "_" + aType.in();
148 aPixmap->SetPixMap(aPixmapName.toLatin1().data());
149 anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
150 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
151 anIOR->SetValue(theIOR.c_str());
157 //---------------------------------------------------------------
158 SALOMEDS::SObject_var
159 AddHypothesis(const std::string& theIOR,
160 const SALOMEDS::SComponent_var& theSComponentMesh,
161 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
163 return AddToDomain(theIOR,
166 SMESH::Tag_HypothesisRoot,
167 QObject::tr("SMESH_MEN_HYPOTHESIS"),
168 "ICON_SMESH_TREE_HYPO");
172 //---------------------------------------------------------------
173 SALOMEDS::SObject_var
174 AddAlgorithms(const std::string& theIOR,
175 const SALOMEDS::SComponent_var& theSComponentMesh,
176 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
178 return AddToDomain(theIOR,
181 SMESH::Tag_AlgorithmsRoot,
182 QObject::tr("SMESH_MEN_ALGORITHMS"),
183 "ICON_SMESH_TREE_ALGO");
187 //---------------------------------------------------------------
189 SetDomain(const char* theMeshOrSubMeshEntry,
190 const char* theDomainEntry,
191 const SALOMEDS::Study_var& theStudy,
192 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
193 long theRefOnAppliedDomainTag,
194 const QString& theAppliedDomainMEN,
195 const QString& theAppliedDomainICON)
197 SALOMEDS::SObject_var aMeshOrSubMeshSO = theStudy->FindObjectID(theMeshOrSubMeshEntry);
198 SALOMEDS::SObject_var aHypothesisSO = theStudy->FindObjectID(theDomainEntry);
200 if(!aMeshOrSubMeshSO->_is_nil() && !aHypothesisSO->_is_nil()){
201 //Find or Create Applied Hypothesis root
202 SALOMEDS::SObject_var anAppliedDomainSO;
203 if(!aMeshOrSubMeshSO->FindSubObject(theRefOnAppliedDomainTag,anAppliedDomainSO)){
204 anAppliedDomainSO = theStudyBuilder->NewObjectToTag(aMeshOrSubMeshSO,theRefOnAppliedDomainTag);
205 SALOMEDS::GenericAttribute_var anAttr =
206 theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeName");
207 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
208 aName->SetValue(theAppliedDomainMEN.toLatin1().data());
209 anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeSelectable");
210 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
211 aSelAttr->SetSelectable(false);
212 anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributePixMap");
213 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
214 aPixmap->SetPixMap(theAppliedDomainICON.toLatin1().data());
216 SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(anAppliedDomainSO);
217 theStudyBuilder->Addreference(aSObject,aHypothesisSO);
222 //---------------------------------------------------------------
224 SetHypothesis(const char* theMeshOrSubMeshEntry,
225 const char* theDomainEntry,
226 const SALOMEDS::Study_var& theStudy,
227 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
229 SetDomain(theMeshOrSubMeshEntry,
233 SMESH::Tag_RefOnAppliedHypothesis,
234 QObject::tr("SMESH_MEN_APPLIED_HYPOTHESIS"),
235 "ICON_SMESH_TREE_HYPO");
239 //---------------------------------------------------------------
241 SetAlgorithms(const char* theMeshOrSubMeshEntry,
242 const char* theDomainEntry,
243 const SALOMEDS::Study_var& theStudy,
244 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
246 SetDomain(theMeshOrSubMeshEntry,
250 SMESH::Tag_RefOnAppliedAlgorithms,
251 QObject::tr("SMESH_MEN_APPLIED_ALGORIHTMS"),
252 "ICON_SMESH_TREE_ALGO");
257 //===============================================================
258 SMESH_Swig::SMESH_Swig()
260 class TEvent: public SALOME_Event
262 CORBA::ORB_var& myORB;
265 TEvent(CORBA::ORB_var& theORB):
274 ORB_INIT &anORBInit = *SINGLETON_<ORB_INIT>::Instance();
275 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
276 myORB = anORBInit( 0, 0 );
278 INFOS("internal error : orb not found");
283 MESSAGE("Constructeur");
285 if(CORBA::is_nil(anORB))
286 ProcessVoidEvent(new TEvent(anORB));
288 ASSERT(!CORBA::is_nil(anORB));
292 //===============================================================
294 SMESH_Swig::Init(int theStudyID)
296 class TEvent: public SALOME_Event
299 SALOMEDS::Study_var& myStudy;
300 SALOMEDS::StudyBuilder_var& myStudyBuilder;
301 SALOMEDS::SComponent_var& mySComponentMesh;
303 TEvent(int theStudyID,
304 SALOMEDS::Study_var& theStudy,
305 SALOMEDS::StudyBuilder_var& theStudyBuilder,
306 SALOMEDS::SComponent_var& theSComponentMesh):
307 myStudyID(theStudyID),
309 myStudyBuilder(theStudyBuilder),
310 mySComponentMesh(theSComponentMesh)
317 SUIT_Session* aSession = SUIT_Session::session();
318 SUIT_Application* anApplication = aSession->activeApplication();
319 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(anApplication);
321 SALOME_NamingService* aNamingService = anApp->namingService();
322 CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
323 SALOMEDS::StudyManager_var aStudyMgr = SALOMEDS::StudyManager::_narrow(anObject);
324 myStudy = aStudyMgr->GetStudyByID(myStudyID);
326 SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
327 aSMESHGen->SetCurrentStudy( myStudy.in() );
329 myStudyBuilder = myStudy->NewBuilder();
331 SALOMEDS::GenericAttribute_var anAttr;
332 SALOMEDS::AttributeName_var aName;
333 SALOMEDS::AttributePixMap_var aPixmap;
335 SALOMEDS::SComponent_var aSComponent = myStudy->FindComponent("SMESH");
336 if(aSComponent->_is_nil()){
337 bool aLocked = myStudy->GetProperties()->IsLocked();
339 myStudy->GetProperties()->SetLocked(false);
341 aSComponent = myStudyBuilder->NewComponent("SMESH");
342 anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributeName");
343 aName = SALOMEDS::AttributeName::_narrow(anAttr);
345 SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI(); //SRN: BugID IPAL9186, load a SMESH gui if it hasn't been loaded
347 CAM_Module* aModule = anApp->module("Mesh");
349 aModule = anApp->loadModule("Mesh");
350 aSMESHGUI = dynamic_cast<SMESHGUI*>(aModule);
351 } //SRN: BugID IPAL9186: end of a fix
352 aName->SetValue(aSMESHGUI->moduleName().toLatin1().data());
353 anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributePixMap");
354 aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
355 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
356 myStudyBuilder->DefineComponentInstance(aSComponent,aSMESHGen);
358 myStudy->GetProperties()->SetLocked(true);
361 mySComponentMesh = SALOMEDS::SComponent::_narrow(aSComponent);
363 qApp->processEvents(); // Workaround for bug 12662
369 ProcessVoidEvent(new TEvent(theStudyID,
376 //===============================================================
377 SMESH_Swig::~SMESH_Swig()
379 MESSAGE("Destructeur");
383 //===============================================================
384 const char* SMESH_Swig::AddNewMesh(const char* theIOR)
386 MESSAGE("AddNewMesh");
388 // VSR: added temporarily - to be removed - objects are published automatically by engine
389 SALOMEDS::SObject_var aSObject = myStudy->FindObjectIOR(theIOR);
390 if (aSObject->_is_nil()){
391 //Find or Create Hypothesis root
392 GetHypothesisRoot(mySComponentMesh,myStudyBuilder);
393 GetAlgorithmsRoot(mySComponentMesh,myStudyBuilder);
396 aSObject = myStudyBuilder->NewObject(mySComponentMesh);
397 SALOMEDS::GenericAttribute_var anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
398 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
399 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
400 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeIOR");
401 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
402 anIOR->SetValue(theIOR);
405 CORBA::String_var anEntry = aSObject->GetID();
407 return anEntry._retn();
411 //===============================================================
412 const char* SMESH_Swig::AddNewHypothesis(const char* theIOR)
414 MESSAGE("AddNewHypothesis");
416 SALOMEDS::SObject_var aSObject = ::AddHypothesis(theIOR,
419 CORBA::String_var anEntry = aSObject->GetID();
420 return anEntry._retn();
424 //===============================================================
425 const char* SMESH_Swig::AddNewAlgorithms(const char* theIOR)
427 MESSAGE("AddNewAlgorithms");
429 SALOMEDS::SObject_var aSObject = ::AddAlgorithms(theIOR,
432 CORBA::String_var anEntry = aSObject->GetID();
433 return anEntry._retn();
437 //===============================================================
438 void SMESH_Swig::SetShape(const char* theShapeEntry,
439 const char* theMeshEntry)
441 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID( theMeshEntry );
442 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID( theShapeEntry );
444 if(!aMeshSO->_is_nil() && !aGeomShapeSO->_is_nil()){
445 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObjectToTag(aMeshSO, SMESH::Tag_RefOnShape);
446 myStudyBuilder->Addreference(aSObject,aGeomShapeSO);
451 //===============================================================
452 void SMESH_Swig::SetHypothesis(const char* theMeshOrSubMeshEntry,
453 const char* theDomainEntry)
455 ::SetHypothesis(theMeshOrSubMeshEntry,
462 //===============================================================
463 void SMESH_Swig::SetAlgorithms(const char* theMeshOrSubMeshEntry,
464 const char* theDomainEntry)
466 ::SetAlgorithms(theMeshOrSubMeshEntry,
473 //===============================================================
475 SMESH_Swig::UnSetHypothesis(const char* theDomainEntry)
477 SALOMEDS::SObject_var aDomainSO = myStudy->FindObjectID(theDomainEntry);
478 if(!aDomainSO->_is_nil())
479 myStudyBuilder->RemoveObject(aDomainSO);
482 const char* SMESH_Swig::AddSubMesh(const char* theMeshEntry,
483 const char* theSubMeshIOR,
486 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(theMeshEntry);
487 if(!aMeshSO->_is_nil()){
489 QString aSubMeshName;
490 switch(theShapeType){
492 aShapeTag = SMESH::Tag_SubMeshOnSolid;
493 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnSolid");
496 aShapeTag = SMESH::Tag_SubMeshOnFace;
497 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnFace");
500 aShapeTag = SMESH::Tag_SubMeshOnEdge;
501 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnEdge");
504 aShapeTag = SMESH::Tag_SubMeshOnVertex;
505 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnVertex");
508 aShapeTag = SMESH::Tag_SubMeshOnCompound;
509 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnCompound");
512 SALOMEDS::SObject_var aSubMeshesRoot;
513 SALOMEDS::GenericAttribute_var anAttr;
514 if(!aMeshSO->FindSubObject(aShapeTag,aSubMeshesRoot)){
515 aSubMeshesRoot = myStudyBuilder->NewObjectToTag(aMeshSO,aShapeTag);
516 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeName");
517 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
518 aName->SetValue(aSubMeshName.toLatin1().data());
519 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeSelectable");
520 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
521 aSelAttr->SetSelectable(false);
524 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObject(aSubMeshesRoot);
525 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
526 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
527 anIOR->SetValue(theSubMeshIOR);
529 CORBA::String_var aString = aSObject->GetID();
530 return aString._retn();
536 const char* SMESH_Swig::AddSubMeshOnShape(const char* theMeshEntry,
537 const char* theGeomShapeEntry,
538 const char* theSubMeshIOR,
541 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID(theGeomShapeEntry);
542 if(!aGeomShapeSO->_is_nil()){
543 const char * aSubMeshEntry = AddSubMesh(theMeshEntry,theSubMeshIOR,ShapeType);
544 SALOMEDS::SObject_var aSubMeshSO = myStudy->FindObjectID(aSubMeshEntry);
545 if(!aSubMeshSO->_is_nil()){
546 SetShape(theGeomShapeEntry,aSubMeshEntry);
547 CORBA::String_var aString = aSubMeshSO->GetID();
548 return aString._retn();
555 void SMESH_Swig::CreateAndDisplayActor( const char* Mesh_Entry )
557 // SMESH_Actor* Mesh = smeshGUI->ReadScript(aM);
558 class TEvent: public SALOME_Event
563 TEvent(const char* Mesh_Entry) {
566 virtual void Execute() {
567 //SMESH::UpdateView(SMESH::eDisplay, _entry);
568 SUIT_Session* aSession = SUIT_Session::session();
569 SUIT_Application* anApplication = aSession->activeApplication();
570 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(anApplication);
571 /*SUIT_ViewManager* vman = */anApp->getViewManager(VTKViewer_Viewer::Type(),true);
572 SMESHGUI_Displayer* aDisp = new SMESHGUI_Displayer(anApp);
573 aDisp->Display(_entry,1);
577 ProcessVoidEvent(new TEvent(Mesh_Entry));
580 void SMESH_Swig::SetName(const char* theEntry,
583 SALOMEDS::SObject_var aSObject = myStudy->FindObjectID(theEntry);
584 SALOMEDS::GenericAttribute_var anAttr;
585 SALOMEDS::AttributeName_var aName;
586 if(!aSObject->_is_nil()){
587 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeName");
588 aName = SALOMEDS::AttributeName::_narrow(anAttr);
589 aName->SetValue(theName);
593 //================================================================================
595 * \brief Set mesh icon according to compute status
596 * \param Mesh_Entry - entry of a mesh
597 * \param isComputed - is mesh computed or not
599 //================================================================================
601 void SMESH_Swig::SetMeshIcon(const char* theMeshEntry,
602 const bool theIsComputed,
605 class TEvent: public SALOME_Event
607 SALOMEDS::Study_var myStudy;
608 std::string myMeshEntry;
609 bool myIsComputed, myIsEmpty;
611 TEvent(const SALOMEDS::Study_var& theStudy,
612 const std::string& theMeshEntry,
613 const bool theIsComputed,
616 myMeshEntry(theMeshEntry),
617 myIsComputed(theIsComputed),
625 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(myMeshEntry.c_str());
626 if(!aMeshSO->_is_nil())
627 if(_PTR(SObject) aMesh = ClientFactory::SObject(aMeshSO))
628 SMESH::ModifiedMesh(aMesh,myIsComputed,myIsEmpty);
632 ProcessVoidEvent(new TEvent(myStudy,