1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // SMESH SMESH : GUI for SMESH component
21 // File : libSMESH_Swig.cxx
22 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
25 #include "libSMESH_Swig.h"
28 #include <SMESHGUI_Utils.h>
29 #include <SMESHGUI_Displayer.h>
31 // SALOME KERNEL includes
32 #include <Utils_ORB_INIT.hxx>
33 #include <Utils_SINGLETON.hxx>
34 #include <SALOMEDSClient_ClientFactory.hxx>
36 #include <utilities.h>
38 // SALOME GUI includes
39 #include <SUIT_Session.h>
40 #include <VTKViewer_ViewModel.h>
41 #include <SALOME_Event.h>
42 #include <SalomeApp_Application.h>
48 #include <QApplication>
51 #include <SALOMEconfig.h>
52 #include CORBA_SERVER_HEADER(SMESH_Gen)
53 #include CORBA_SERVER_HEADER(SMESH_Hypothesis)
55 static CORBA::ORB_var anORB;
59 //---------------------------------------------------------------
62 StringToObject(const std::string& theIOR)
64 return anORB->string_to_object(theIOR.c_str());
68 //---------------------------------------------------------------
71 GetDomainRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
72 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
73 CORBA::Long theDomainRootTag,
74 const QString& theName,
75 const QString& thePixmap)
77 SALOMEDS::SObject_var aDomainRoot;
78 if (!theSComponentMesh->FindSubObject(theDomainRootTag,aDomainRoot)) {
79 aDomainRoot = theStudyBuilder->NewObjectToTag(theSComponentMesh,theDomainRootTag);
80 SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeName");
81 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
82 aName->SetValue(theName.toLatin1().data());
83 anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributePixMap");
84 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
85 aPixmap->SetPixMap(thePixmap.toLatin1().data());
86 anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeSelectable");
87 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
88 aSelAttr->SetSelectable(false);
95 //---------------------------------------------------------------
98 GetHypothesisRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
99 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
101 return GetDomainRoot(theSComponentMesh,
103 SMESH::Tag_HypothesisRoot,
104 QObject::tr("SMESH_MEN_HYPOTHESIS"),
105 "ICON_SMESH_TREE_HYPO");
109 //---------------------------------------------------------------
111 SALOMEDS::SObject_var
112 GetAlgorithmsRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
113 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
115 return GetDomainRoot(theSComponentMesh,
117 SMESH::Tag_AlgorithmsRoot,
118 QObject::tr("SMESH_MEN_ALGORITHMS"),
119 "ICON_SMESH_TREE_ALGO");
123 //---------------------------------------------------------------
125 SALOMEDS::SObject_var
126 AddToDomain(const std::string& theIOR,
127 const SALOMEDS::SComponent_var& theSComponentMesh,
128 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
129 CORBA::Long theDomainRootTag,
130 const QString& theDomainName,
131 const QString& theDomainPixmap)
133 SALOMEDS::SObject_var aDomain = GetDomainRoot(theSComponentMesh,
135 SMESH::Tag_AlgorithmsRoot,
138 // Add New Hypothesis
139 SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(aDomain);
140 SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
141 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
142 CORBA::Object_var anObject = StringToObject(theIOR);
143 SMESH::SMESH_Hypothesis_var aDomainItem = SMESH::SMESH_Hypothesis::_narrow(anObject.in());
144 CORBA::String_var aType = aDomainItem->GetName();
145 QString aPixmapName = theDomainPixmap + "_" + aType.in();
146 aPixmap->SetPixMap(aPixmapName.toLatin1().data());
147 anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
148 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
149 anIOR->SetValue(theIOR.c_str());
155 //---------------------------------------------------------------
156 SALOMEDS::SObject_var
157 AddHypothesis(const std::string& theIOR,
158 const SALOMEDS::SComponent_var& theSComponentMesh,
159 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
161 return AddToDomain(theIOR,
164 SMESH::Tag_HypothesisRoot,
165 QObject::tr("SMESH_MEN_HYPOTHESIS"),
166 "ICON_SMESH_TREE_HYPO");
170 //---------------------------------------------------------------
171 SALOMEDS::SObject_var
172 AddAlgorithms(const std::string& theIOR,
173 const SALOMEDS::SComponent_var& theSComponentMesh,
174 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
176 return AddToDomain(theIOR,
179 SMESH::Tag_AlgorithmsRoot,
180 QObject::tr("SMESH_MEN_ALGORITHMS"),
181 "ICON_SMESH_TREE_ALGO");
185 //---------------------------------------------------------------
187 SetDomain(const char* theMeshOrSubMeshEntry,
188 const char* theDomainEntry,
189 const SALOMEDS::Study_var& theStudy,
190 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
191 long theRefOnAppliedDomainTag,
192 const QString& theAppliedDomainMEN,
193 const QString& theAppliedDomainICON)
195 SALOMEDS::SObject_var aMeshOrSubMeshSO = theStudy->FindObjectID(theMeshOrSubMeshEntry);
196 SALOMEDS::SObject_var aHypothesisSO = theStudy->FindObjectID(theDomainEntry);
198 if(!aMeshOrSubMeshSO->_is_nil() && !aHypothesisSO->_is_nil()){
199 //Find or Create Applied Hypothesis root
200 SALOMEDS::SObject_var anAppliedDomainSO;
201 if(!aMeshOrSubMeshSO->FindSubObject(theRefOnAppliedDomainTag,anAppliedDomainSO)){
202 anAppliedDomainSO = theStudyBuilder->NewObjectToTag(aMeshOrSubMeshSO,theRefOnAppliedDomainTag);
203 SALOMEDS::GenericAttribute_var anAttr =
204 theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeName");
205 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
206 aName->SetValue(theAppliedDomainMEN.toLatin1().data());
207 anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeSelectable");
208 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
209 aSelAttr->SetSelectable(false);
210 anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributePixMap");
211 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
212 aPixmap->SetPixMap(theAppliedDomainICON.toLatin1().data());
214 SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(anAppliedDomainSO);
215 theStudyBuilder->Addreference(aSObject,aHypothesisSO);
220 //---------------------------------------------------------------
222 SetHypothesis(const char* theMeshOrSubMeshEntry,
223 const char* theDomainEntry,
224 const SALOMEDS::Study_var& theStudy,
225 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
227 SetDomain(theMeshOrSubMeshEntry,
231 SMESH::Tag_RefOnAppliedHypothesis,
232 QObject::tr("SMESH_MEN_APPLIED_HYPOTHESIS"),
233 "ICON_SMESH_TREE_HYPO");
237 //---------------------------------------------------------------
239 SetAlgorithms(const char* theMeshOrSubMeshEntry,
240 const char* theDomainEntry,
241 const SALOMEDS::Study_var& theStudy,
242 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
244 SetDomain(theMeshOrSubMeshEntry,
248 SMESH::Tag_RefOnAppliedAlgorithms,
249 QObject::tr("SMESH_MEN_APPLIED_ALGORIHTMS"),
250 "ICON_SMESH_TREE_ALGO");
255 //===============================================================
256 SMESH_Swig::SMESH_Swig()
258 class TEvent: public SALOME_Event
260 CORBA::ORB_var& myORB;
263 TEvent(CORBA::ORB_var& theORB):
272 ORB_INIT &anORBInit = *SINGLETON_<ORB_INIT>::Instance();
273 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
274 myORB = anORBInit( 0, 0 );
276 INFOS("internal error : orb not found");
281 MESSAGE("Constructeur");
283 if(CORBA::is_nil(anORB))
284 ProcessVoidEvent(new TEvent(anORB));
286 ASSERT(!CORBA::is_nil(anORB));
290 //===============================================================
292 SMESH_Swig::Init(int theStudyID)
294 class TEvent: public SALOME_Event
297 SALOMEDS::Study_var& myStudy;
298 SALOMEDS::StudyBuilder_var& myStudyBuilder;
299 SALOMEDS::SComponent_var& mySComponentMesh;
301 TEvent(int theStudyID,
302 SALOMEDS::Study_var& theStudy,
303 SALOMEDS::StudyBuilder_var& theStudyBuilder,
304 SALOMEDS::SComponent_var& theSComponentMesh):
305 myStudyID(theStudyID),
307 myStudyBuilder(theStudyBuilder),
308 mySComponentMesh(theSComponentMesh)
315 SUIT_Session* aSession = SUIT_Session::session();
316 SUIT_Application* anApplication = aSession->activeApplication();
317 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(anApplication);
319 SALOME_NamingService* aNamingService = anApp->namingService();
320 CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
321 SALOMEDS::StudyManager_var aStudyMgr = SALOMEDS::StudyManager::_narrow(anObject);
322 myStudy = aStudyMgr->GetStudyByID(myStudyID);
324 SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
325 aSMESHGen->SetCurrentStudy( myStudy.in() );
327 myStudyBuilder = myStudy->NewBuilder();
329 SALOMEDS::GenericAttribute_var anAttr;
330 SALOMEDS::AttributeName_var aName;
331 SALOMEDS::AttributePixMap_var aPixmap;
333 SALOMEDS::SComponent_var aSComponent = myStudy->FindComponent("SMESH");
334 if(aSComponent->_is_nil()){
335 bool aLocked = myStudy->GetProperties()->IsLocked();
337 myStudy->GetProperties()->SetLocked(false);
339 aSComponent = myStudyBuilder->NewComponent("SMESH");
340 anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributeName");
341 aName = SALOMEDS::AttributeName::_narrow(anAttr);
343 SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI(); //SRN: BugID IPAL9186, load a SMESH gui if it hasn't been loaded
345 CAM_Module* aModule = anApp->module("Mesh");
347 aModule = anApp->loadModule("Mesh");
348 aSMESHGUI = dynamic_cast<SMESHGUI*>(aModule);
349 } //SRN: BugID IPAL9186: end of a fix
350 aName->SetValue(aSMESHGUI->moduleName().toLatin1().data());
351 anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributePixMap");
352 aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
353 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
354 myStudyBuilder->DefineComponentInstance(aSComponent,aSMESHGen);
356 myStudy->GetProperties()->SetLocked(true);
359 mySComponentMesh = SALOMEDS::SComponent::_narrow(aSComponent);
361 qApp->processEvents(); // Workaround for bug 12662
367 ProcessVoidEvent(new TEvent(theStudyID,
374 //===============================================================
375 SMESH_Swig::~SMESH_Swig()
377 MESSAGE("Destructeur");
381 //===============================================================
382 const char* SMESH_Swig::AddNewMesh(const char* theIOR)
384 MESSAGE("AddNewMesh");
386 // VSR: added temporarily - to be removed - objects are published automatically by engine
387 SALOMEDS::SObject_var aSObject = myStudy->FindObjectIOR(theIOR);
388 if (aSObject->_is_nil()){
389 //Find or Create Hypothesis root
390 GetHypothesisRoot(mySComponentMesh,myStudyBuilder);
391 GetAlgorithmsRoot(mySComponentMesh,myStudyBuilder);
394 aSObject = myStudyBuilder->NewObject(mySComponentMesh);
395 SALOMEDS::GenericAttribute_var anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
396 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
397 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
398 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeIOR");
399 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
400 anIOR->SetValue(theIOR);
403 CORBA::String_var anEntry = aSObject->GetID();
405 return anEntry._retn();
409 //===============================================================
410 const char* SMESH_Swig::AddNewHypothesis(const char* theIOR)
412 MESSAGE("AddNewHypothesis");
414 SALOMEDS::SObject_var aSObject = ::AddHypothesis(theIOR,
417 CORBA::String_var anEntry = aSObject->GetID();
418 return anEntry._retn();
422 //===============================================================
423 const char* SMESH_Swig::AddNewAlgorithms(const char* theIOR)
425 MESSAGE("AddNewAlgorithms");
427 SALOMEDS::SObject_var aSObject = ::AddAlgorithms(theIOR,
430 CORBA::String_var anEntry = aSObject->GetID();
431 return anEntry._retn();
435 //===============================================================
436 void SMESH_Swig::SetShape(const char* theShapeEntry,
437 const char* theMeshEntry)
439 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID( theMeshEntry );
440 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID( theShapeEntry );
442 if(!aMeshSO->_is_nil() && !aGeomShapeSO->_is_nil()){
443 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObjectToTag(aMeshSO, SMESH::Tag_RefOnShape);
444 myStudyBuilder->Addreference(aSObject,aGeomShapeSO);
449 //===============================================================
450 void SMESH_Swig::SetHypothesis(const char* theMeshOrSubMeshEntry,
451 const char* theDomainEntry)
453 ::SetHypothesis(theMeshOrSubMeshEntry,
460 //===============================================================
461 void SMESH_Swig::SetAlgorithms(const char* theMeshOrSubMeshEntry,
462 const char* theDomainEntry)
464 ::SetAlgorithms(theMeshOrSubMeshEntry,
471 //===============================================================
473 SMESH_Swig::UnSetHypothesis(const char* theDomainEntry)
475 SALOMEDS::SObject_var aDomainSO = myStudy->FindObjectID(theDomainEntry);
476 if(!aDomainSO->_is_nil())
477 myStudyBuilder->RemoveObject(aDomainSO);
480 const char* SMESH_Swig::AddSubMesh(const char* theMeshEntry,
481 const char* theSubMeshIOR,
484 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(theMeshEntry);
485 if(!aMeshSO->_is_nil()){
487 QString aSubMeshName;
488 switch(theShapeType){
490 aShapeTag = SMESH::Tag_SubMeshOnSolid;
491 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnSolid");
494 aShapeTag = SMESH::Tag_SubMeshOnFace;
495 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnFace");
498 aShapeTag = SMESH::Tag_SubMeshOnEdge;
499 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnEdge");
502 aShapeTag = SMESH::Tag_SubMeshOnVertex;
503 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnVertex");
506 aShapeTag = SMESH::Tag_SubMeshOnCompound;
507 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnCompound");
510 SALOMEDS::SObject_var aSubMeshesRoot;
511 SALOMEDS::GenericAttribute_var anAttr;
512 if(!aMeshSO->FindSubObject(aShapeTag,aSubMeshesRoot)){
513 aSubMeshesRoot = myStudyBuilder->NewObjectToTag(aMeshSO,aShapeTag);
514 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeName");
515 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
516 aName->SetValue(aSubMeshName.toLatin1().data());
517 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeSelectable");
518 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
519 aSelAttr->SetSelectable(false);
522 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObject(aSubMeshesRoot);
523 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
524 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
525 anIOR->SetValue(theSubMeshIOR);
527 CORBA::String_var aString = aSObject->GetID();
528 return aString._retn();
534 const char* SMESH_Swig::AddSubMeshOnShape(const char* theMeshEntry,
535 const char* theGeomShapeEntry,
536 const char* theSubMeshIOR,
539 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID(theGeomShapeEntry);
540 if(!aGeomShapeSO->_is_nil()){
541 const char * aSubMeshEntry = AddSubMesh(theMeshEntry,theSubMeshIOR,ShapeType);
542 SALOMEDS::SObject_var aSubMeshSO = myStudy->FindObjectID(aSubMeshEntry);
543 if(!aSubMeshSO->_is_nil()){
544 SetShape(theGeomShapeEntry,aSubMeshEntry);
545 CORBA::String_var aString = aSubMeshSO->GetID();
546 return aString._retn();
553 void SMESH_Swig::CreateAndDisplayActor( const char* Mesh_Entry )
555 // SMESH_Actor* Mesh = smeshGUI->ReadScript(aM);
556 class TEvent: public SALOME_Event
561 TEvent(const char* Mesh_Entry) {
564 virtual void Execute() {
565 //SMESH::UpdateView(SMESH::eDisplay, _entry);
566 SUIT_Session* aSession = SUIT_Session::session();
567 SUIT_Application* anApplication = aSession->activeApplication();
568 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(anApplication);
569 /*SUIT_ViewManager* vman = */anApp->getViewManager(VTKViewer_Viewer::Type(),true);
570 SMESHGUI_Displayer* aDisp = new SMESHGUI_Displayer(anApp);
571 aDisp->Display(_entry,1);
575 ProcessVoidEvent(new TEvent(Mesh_Entry));
578 void SMESH_Swig::SetName(const char* theEntry,
581 SALOMEDS::SObject_var aSObject = myStudy->FindObjectID(theEntry);
582 SALOMEDS::GenericAttribute_var anAttr;
583 SALOMEDS::AttributeName_var aName;
584 if(!aSObject->_is_nil()){
585 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeName");
586 aName = SALOMEDS::AttributeName::_narrow(anAttr);
587 aName->SetValue(theName);
591 //================================================================================
593 * \brief Set mesh icon according to compute status
594 * \param Mesh_Entry - entry of a mesh
595 * \param isComputed - is mesh computed or not
597 //================================================================================
599 void SMESH_Swig::SetMeshIcon(const char* theMeshEntry,
600 const bool theIsComputed,
603 class TEvent: public SALOME_Event
605 SALOMEDS::Study_var myStudy;
606 std::string myMeshEntry;
607 bool myIsComputed, myIsEmpty;
609 TEvent(const SALOMEDS::Study_var& theStudy,
610 const std::string& theMeshEntry,
611 const bool theIsComputed,
614 myMeshEntry(theMeshEntry),
615 myIsComputed(theIsComputed),
623 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(myMeshEntry.c_str());
624 if(!aMeshSO->_is_nil())
625 if(_PTR(SObject) aMesh = ClientFactory::SObject(aMeshSO))
626 SMESH::ModifiedMesh(aMesh,myIsComputed,myIsEmpty);
630 ProcessVoidEvent(new TEvent(myStudy,