1 // SMESH SMESHGUI : 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
24 // File : SMESH_Swig.cxx
25 // Author : Nicolas REJNERI
29 #include "SMESHGUI_Swig.hxx"
31 #include "Utils_ORB_INIT.hxx"
32 #include "Utils_SINGLETON.hxx"
35 #include "SMESHGUI_Utils.h"
36 #include "SMESHGUI_GEOMGenUtils.h"
37 #include "SMESHGUI_Displayer.h"
40 #include "SUIT_ResourceMgr.h"
41 #include "SUIT_Session.h"
42 #include "SUIT_ViewModel.h"
43 #include "VTKViewer_ViewModel.h"
45 #include "SALOME_Event.hxx"
46 #include "SALOME_NamingService.hxx"
47 #include "SalomeApp_Application.h"
48 #include "SALOMEDSClient_ClientFactory.hxx"
50 #include "utilities.h"
52 // Open CASCADE Includes
56 #include <qapplication.h>
59 #include <SALOMEconfig.h>
60 #include CORBA_SERVER_HEADER(SMESH_Gen)
61 #include CORBA_SERVER_HEADER(SMESH_Mesh)
62 #include CORBA_SERVER_HEADER(SMESH_Hypothesis)
66 static CORBA::ORB_var anORB;
69 static long Tag_HypothesisRoot = 1;
70 static long Tag_AlgorithmsRoot = 2;
72 static long Tag_RefOnShape = 1;
73 static long Tag_RefOnAppliedHypothesis = 2;
74 static long Tag_RefOnAppliedAlgorithms = 3;
76 static long Tag_SubMeshOnVertex = 4;
77 static long Tag_SubMeshOnEdge = 5;
78 static long Tag_SubMeshOnFace = 6;
79 static long Tag_SubMeshOnSolid = 7;
80 static long Tag_SubMeshOnCompound = 8;
84 //---------------------------------------------------------------
87 StringToObject(const std::string& theIOR)
89 return anORB->string_to_object(theIOR.c_str());
93 //---------------------------------------------------------------
96 GetDomainRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
97 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
98 CORBA::Long theDomainRootTag,
99 const QString& theName,
100 const QString& thePixmap)
102 SALOMEDS::SObject_var aDomainRoot;
103 if (!theSComponentMesh->FindSubObject(theDomainRootTag,aDomainRoot)) {
104 aDomainRoot = theStudyBuilder->NewObjectToTag(theSComponentMesh,theDomainRootTag);
105 SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeName");
106 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
107 aName->SetValue(theName.latin1());
108 anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributePixMap");
109 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
110 aPixmap->SetPixMap(thePixmap.latin1());
111 anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeSelectable");
112 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
113 aSelAttr->SetSelectable(false);
120 //---------------------------------------------------------------
122 SALOMEDS::SObject_var
123 GetHypothesisRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
124 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
126 return GetDomainRoot(theSComponentMesh,
129 QObject::tr("SMESH_MEN_HYPOTHESIS"),
130 "ICON_SMESH_TREE_HYPO");
134 //---------------------------------------------------------------
136 SALOMEDS::SObject_var
137 GetAlgorithmsRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
138 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
140 return GetDomainRoot(theSComponentMesh,
143 QObject::tr("SMESH_MEN_ALGORITHMS"),
144 "ICON_SMESH_TREE_ALGO");
148 //---------------------------------------------------------------
150 SALOMEDS::SObject_var
151 AddToDomain(const std::string& theIOR,
152 const SALOMEDS::SComponent_var& theSComponentMesh,
153 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
154 CORBA::Long theDomainRootTag,
155 const QString& theDomainName,
156 const QString& theDomainPixmap)
158 SALOMEDS::SObject_var aDomain = GetDomainRoot(theSComponentMesh,
163 // Add New Hypothesis
164 SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(aDomain);
165 SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
166 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
167 CORBA::Object_var anObject = StringToObject(theIOR);
168 SMESH::SMESH_Hypothesis_var aDomainItem = SMESH::SMESH_Hypothesis::_narrow(anObject.in());
169 CORBA::String_var aType = aDomainItem->GetName();
170 QString aPixmapName = theDomainPixmap + "_" + aType.in();
171 aPixmap->SetPixMap(aPixmapName.latin1());
172 anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
173 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
174 anIOR->SetValue(theIOR.c_str());
180 //---------------------------------------------------------------
181 SALOMEDS::SObject_var
182 AddHypothesis(const std::string& theIOR,
183 const SALOMEDS::SComponent_var& theSComponentMesh,
184 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
186 return AddToDomain(theIOR,
190 QObject::tr("SMESH_MEN_HYPOTHESIS"),
191 "ICON_SMESH_TREE_HYPO");
195 //---------------------------------------------------------------
196 SALOMEDS::SObject_var
197 AddAlgorithms(const std::string& theIOR,
198 const SALOMEDS::SComponent_var& theSComponentMesh,
199 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
201 return AddToDomain(theIOR,
205 QObject::tr("SMESH_MEN_ALGORITHMS"),
206 "ICON_SMESH_TREE_ALGO");
210 //---------------------------------------------------------------
212 SetDomain(const char* theMeshOrSubMeshEntry,
213 const char* theDomainEntry,
214 const SALOMEDS::Study_var& theStudy,
215 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
216 long theRefOnAppliedDomainTag,
217 const QString& theAppliedDomainMEN,
218 const QString& theAppliedDomainICON)
220 SALOMEDS::SObject_var aMeshOrSubMeshSO = theStudy->FindObjectID(theMeshOrSubMeshEntry);
221 SALOMEDS::SObject_var aHypothesisSO = theStudy->FindObjectID(theDomainEntry);
223 if(!aMeshOrSubMeshSO->_is_nil() && !aHypothesisSO->_is_nil()){
224 //Find or Create Applied Hypothesis root
225 SALOMEDS::SObject_var anAppliedDomainSO;
226 if(!aMeshOrSubMeshSO->FindSubObject(theRefOnAppliedDomainTag,anAppliedDomainSO)){
227 anAppliedDomainSO = theStudyBuilder->NewObjectToTag(aMeshOrSubMeshSO,theRefOnAppliedDomainTag);
228 SALOMEDS::GenericAttribute_var anAttr =
229 theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeName");
230 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
231 aName->SetValue(theAppliedDomainMEN.latin1());
232 anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeSelectable");
233 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
234 aSelAttr->SetSelectable(false);
235 anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributePixMap");
236 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
237 aPixmap->SetPixMap(theAppliedDomainICON.latin1());
239 SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(anAppliedDomainSO);
240 theStudyBuilder->Addreference(aSObject,aHypothesisSO);
245 //---------------------------------------------------------------
247 SetHypothesis(const char* theMeshOrSubMeshEntry,
248 const char* theDomainEntry,
249 const SALOMEDS::Study_var& theStudy,
250 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
252 SetDomain(theMeshOrSubMeshEntry,
256 Tag_RefOnAppliedHypothesis,
257 QObject::tr("SMESH_MEN_APPLIED_HYPOTHESIS"),
258 "ICON_SMESH_TREE_HYPO");
262 //---------------------------------------------------------------
264 SetAlgorithms(const char* theMeshOrSubMeshEntry,
265 const char* theDomainEntry,
266 const SALOMEDS::Study_var& theStudy,
267 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
269 SetDomain(theMeshOrSubMeshEntry,
273 Tag_RefOnAppliedAlgorithms,
274 QObject::tr("SMESH_MEN_APPLIED_ALGORIHTMS"),
275 "ICON_SMESH_TREE_ALGO");
280 //===============================================================
281 SMESH_Swig::SMESH_Swig()
283 class TEvent: public SALOME_Event
285 CORBA::ORB_var& myORB;
288 TEvent(CORBA::ORB_var& theORB):
297 ORB_INIT &anORBInit = *SINGLETON_<ORB_INIT>::Instance();
298 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
299 myORB = anORBInit( 0 , 0 );
301 INFOS("internal error : orb not found");
306 MESSAGE("Constructeur");
308 if(CORBA::is_nil(anORB))
309 ProcessVoidEvent(new TEvent(anORB));
311 ASSERT(!CORBA::is_nil(anORB));
315 //===============================================================
317 SMESH_Swig::Init(int theStudyID)
319 class TEvent: public SALOME_Event
322 SALOMEDS::Study_var& myStudy;
323 SALOMEDS::StudyBuilder_var& myStudyBuilder;
324 SALOMEDS::SComponent_var& mySComponentMesh;
326 TEvent(int theStudyID,
327 SALOMEDS::Study_var& theStudy,
328 SALOMEDS::StudyBuilder_var& theStudyBuilder,
329 SALOMEDS::SComponent_var& theSComponentMesh):
330 myStudyID(theStudyID),
332 myStudyBuilder(theStudyBuilder),
333 mySComponentMesh(theSComponentMesh)
340 SUIT_Session* aSession = SUIT_Session::session();
341 SUIT_Application* anApplication = aSession->activeApplication();
342 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(anApplication);
344 SALOME_NamingService* aNamingService = anApp->namingService();
345 CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
346 SALOMEDS::StudyManager_var aStudyMgr = SALOMEDS::StudyManager::_narrow(anObject);
347 myStudy = aStudyMgr->GetStudyByID(myStudyID);
349 SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
350 aSMESHGen->SetCurrentStudy( myStudy.in() );
352 myStudyBuilder = myStudy->NewBuilder();
354 SALOMEDS::GenericAttribute_var anAttr;
355 SALOMEDS::AttributeName_var aName;
356 SALOMEDS::AttributePixMap_var aPixmap;
358 SALOMEDS::SComponent_var aSComponent = myStudy->FindComponent("SMESH");
359 if(aSComponent->_is_nil()){
360 bool aLocked = myStudy->GetProperties()->IsLocked();
362 myStudy->GetProperties()->SetLocked(false);
364 aSComponent = myStudyBuilder->NewComponent("SMESH");
365 anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributeName");
366 aName = SALOMEDS::AttributeName::_narrow(anAttr);
368 SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI(); //SRN: BugID IPAL9186, load a SMESH gui if it hasn't been loaded
370 CAM_Module* aModule = anApp->module("Mesh");
372 aModule = anApp->loadModule("Mesh");
373 aSMESHGUI = dynamic_cast<SMESHGUI*>(aModule);
374 } //SRN: BugID IPAL9186: end of a fix
375 aName->SetValue(aSMESHGUI->moduleName());
376 anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributePixMap");
377 aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
378 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
379 myStudyBuilder->DefineComponentInstance(aSComponent,aSMESHGen);
381 myStudy->GetProperties()->SetLocked(true);
384 mySComponentMesh = SALOMEDS::SComponent::_narrow(aSComponent);
386 qApp->processEvents(); // Workaround for bug 12662
392 ProcessVoidEvent(new TEvent(theStudyID,
399 //===============================================================
400 SMESH_Swig::~SMESH_Swig()
402 MESSAGE("Destructeur");
406 //===============================================================
408 SMESH_Swig::AddNewMesh(const char* theIOR)
410 MESSAGE("AddNewMesh");
412 // VSR: added temporarily - to be removed - objects are published automatically by engine
413 SALOMEDS::SObject_var aSObject = myStudy->FindObjectIOR(theIOR);
414 if(aSObject->_is_nil()){
415 //Find or Create Hypothesis root
416 GetHypothesisRoot(mySComponentMesh,myStudyBuilder);
417 GetAlgorithmsRoot(mySComponentMesh,myStudyBuilder);
420 aSObject = myStudyBuilder->NewObject(mySComponentMesh);
421 SALOMEDS::GenericAttribute_var anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
422 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
423 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
424 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeIOR");
425 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
426 anIOR->SetValue(theIOR);
429 CORBA::String_var anEntry = aSObject->GetID();
431 return anEntry._retn();
435 //===============================================================
437 SMESH_Swig::AddNewHypothesis(const char* theIOR)
440 MESSAGE("AddNewHypothesis");
442 SALOMEDS::SObject_var aSObject = ::AddHypothesis(theIOR,
445 CORBA::String_var anEntry = aSObject->GetID();
446 return anEntry._retn();
450 //===============================================================
452 SMESH_Swig::AddNewAlgorithms(const char* theIOR)
454 MESSAGE("AddNewAlgorithms");
456 SALOMEDS::SObject_var aSObject = ::AddAlgorithms(theIOR,
459 CORBA::String_var anEntry = aSObject->GetID();
460 return anEntry._retn();
464 //===============================================================
466 SMESH_Swig::SetShape(const char* theShapeEntry,
467 const char* theMeshEntry)
469 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID( theMeshEntry );
470 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID( theShapeEntry );
472 if(!aMeshSO->_is_nil() && !aGeomShapeSO->_is_nil()){
473 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObjectToTag(aMeshSO,Tag_RefOnShape);
474 myStudyBuilder->Addreference(aSObject,aGeomShapeSO);
479 //===============================================================
481 SMESH_Swig::SetHypothesis(const char* theMeshOrSubMeshEntry,
482 const char* theDomainEntry)
484 ::SetHypothesis(theMeshOrSubMeshEntry,
491 //===============================================================
493 SMESH_Swig::SetAlgorithms(const char* theMeshOrSubMeshEntry,
494 const char* theDomainEntry)
496 ::SetAlgorithms(theMeshOrSubMeshEntry,
503 //===============================================================
505 SMESH_Swig::UnSetHypothesis(const char* theDomainEntry)
507 SALOMEDS::SObject_var aDomainSO = myStudy->FindObjectID(theDomainEntry);
508 if(!aDomainSO->_is_nil())
509 myStudyBuilder->RemoveObject(aDomainSO);
513 SMESH_Swig::AddSubMesh(const char* theMeshEntry,
514 const char* theSubMeshIOR,
517 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(theMeshEntry);
518 if(!aMeshSO->_is_nil()){
520 QString aSubMeshName;
521 switch(theShapeType){
523 aShapeTag = Tag_SubMeshOnSolid;
524 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnSolid");
527 aShapeTag = Tag_SubMeshOnFace;
528 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnFace");
531 aShapeTag = Tag_SubMeshOnEdge;
532 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnEdge");
535 aShapeTag = Tag_SubMeshOnVertex;
536 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnVertex");
539 aShapeTag = Tag_SubMeshOnCompound;
540 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnCompound");
543 SALOMEDS::SObject_var aSubMeshesRoot;
544 SALOMEDS::GenericAttribute_var anAttr;
545 if(!aMeshSO->FindSubObject(aShapeTag,aSubMeshesRoot)){
546 aSubMeshesRoot = myStudyBuilder->NewObjectToTag(aMeshSO,aShapeTag);
547 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeName");
548 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
549 aName->SetValue(aSubMeshName.latin1());
550 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeSelectable");
551 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
552 aSelAttr->SetSelectable(false);
555 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObject(aSubMeshesRoot);
556 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
557 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
558 anIOR->SetValue(theSubMeshIOR);
560 CORBA::String_var aString = aSObject->GetID();
561 return aString._retn();
568 SMESH_Swig::AddSubMeshOnShape(const char* theMeshEntry,
569 const char* theGeomShapeEntry,
570 const char* theSubMeshIOR,
573 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID(theGeomShapeEntry);
574 if(!aGeomShapeSO->_is_nil()){
575 const char * aSubMeshEntry = AddSubMesh(theMeshEntry,theSubMeshIOR,ShapeType);
576 SALOMEDS::SObject_var aSubMeshSO = myStudy->FindObjectID(aSubMeshEntry);
577 if(!aSubMeshSO->_is_nil()){
578 SetShape(theGeomShapeEntry,aSubMeshEntry);
579 CORBA::String_var aString = aSubMeshSO->GetID();
580 return aString._retn();
587 void SMESH_Swig::CreateAndDisplayActor( const char* Mesh_Entry )
589 // SMESH_Actor* Mesh = smeshGUI->ReadScript(aM);
590 class TEvent: public SALOME_Event
595 TEvent(const char* Mesh_Entry) {
598 virtual void Execute() {
599 //SMESH::UpdateView(SMESH::eDisplay, _entry);
600 SUIT_Session* aSession = SUIT_Session::session();
601 SUIT_Application* anApplication = aSession->activeApplication();
602 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(anApplication);
603 SUIT_ViewManager* vman = anApp->getViewManager(VTKViewer_Viewer::Type(),true);
604 SMESHGUI_Displayer* aDisp = new SMESHGUI_Displayer(anApp);
605 aDisp->Display(_entry,1);
609 ProcessVoidEvent(new TEvent(Mesh_Entry));
614 SMESH_Swig::SetName(const char* theEntry,
617 SALOMEDS::SObject_var aSObject = myStudy->FindObjectID(theEntry);
618 SALOMEDS::GenericAttribute_var anAttr;
619 SALOMEDS::AttributeName_var aName;
620 if(!aSObject->_is_nil()){
621 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeName");
622 aName = SALOMEDS::AttributeName::_narrow(anAttr);
623 aName->SetValue(theName);
627 //================================================================================
629 * \brief Set mesh icon according to compute status
630 * \param Mesh_Entry - entry of a mesh
631 * \param isComputed - is mesh computed or not
633 //================================================================================
635 void SMESH_Swig::SetMeshIcon(const char* theMeshEntry,
636 const bool theIsComputed)
638 class TEvent: public SALOME_Event
640 SALOMEDS::Study_var myStudy;
641 std::string myMeshEntry;
644 TEvent(const SALOMEDS::Study_var& theStudy,
645 const std::string& theMeshEntry,
646 const bool theIsComputed):
648 myMeshEntry(theMeshEntry),
649 myIsComputed(theIsComputed)
656 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(myMeshEntry.c_str());
657 if(!aMeshSO->_is_nil())
658 if(_PTR(SObject) aMesh = ClientFactory::SObject(aMeshSO))
659 SMESH::ModifiedMesh(aMesh,myIsComputed);
663 ProcessVoidEvent(new TEvent(myStudy,