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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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"
39 #include "SUIT_ResourceMgr.h"
40 #include "SUIT_Session.h"
42 #include "SALOME_Event.hxx"
43 #include "SALOME_NamingService.hxx"
44 #include "SalomeApp_Application.h"
45 #include "SALOMEDSClient_ClientFactory.hxx"
47 #include "utilities.h"
49 // Open CASCADE Includes
53 #include <SALOMEconfig.h>
54 #include CORBA_SERVER_HEADER(SMESH_Gen)
55 #include CORBA_SERVER_HEADER(SMESH_Mesh)
56 #include CORBA_SERVER_HEADER(SMESH_Hypothesis)
60 static CORBA::ORB_var anORB;
63 static long Tag_HypothesisRoot = 1;
64 static long Tag_AlgorithmsRoot = 2;
66 static long Tag_RefOnShape = 1;
67 static long Tag_RefOnAppliedHypothesis = 2;
68 static long Tag_RefOnAppliedAlgorithms = 3;
70 static long Tag_SubMeshOnVertex = 4;
71 static long Tag_SubMeshOnEdge = 5;
72 static long Tag_SubMeshOnFace = 6;
73 static long Tag_SubMeshOnSolid = 7;
74 static long Tag_SubMeshOnCompound = 8;
78 //---------------------------------------------------------------
81 StringToObject(const std::string& theIOR)
83 return anORB->string_to_object(theIOR.c_str());
87 //---------------------------------------------------------------
90 GetDomainRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
91 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
92 CORBA::Long theDomainRootTag,
93 const QString& theName,
94 const QString& thePixmap)
96 SALOMEDS::SObject_var aDomainRoot;
97 if (!theSComponentMesh->FindSubObject(theDomainRootTag,aDomainRoot)) {
98 aDomainRoot = theStudyBuilder->NewObjectToTag(theSComponentMesh,theDomainRootTag);
99 SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeName");
100 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
101 aName->SetValue(theName.latin1());
102 anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributePixMap");
103 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
104 aPixmap->SetPixMap(thePixmap.latin1());
105 anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeSelectable");
106 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
107 aSelAttr->SetSelectable(false);
114 //---------------------------------------------------------------
116 SALOMEDS::SObject_var
117 GetHypothesisRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
118 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
120 return GetDomainRoot(theSComponentMesh,
123 QObject::tr("SMESH_MEN_HYPOTHESIS"),
124 "ICON_SMESH_TREE_HYPO");
128 //---------------------------------------------------------------
130 SALOMEDS::SObject_var
131 GetAlgorithmsRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
132 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
134 return GetDomainRoot(theSComponentMesh,
137 QObject::tr("SMESH_MEN_ALGORITHMS"),
138 "ICON_SMESH_TREE_ALGO");
142 //---------------------------------------------------------------
144 SALOMEDS::SObject_var
145 AddToDomain(const std::string& theIOR,
146 const SALOMEDS::SComponent_var& theSComponentMesh,
147 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
148 CORBA::Long theDomainRootTag,
149 const QString& theDomainName,
150 const QString& theDomainPixmap)
152 SALOMEDS::SObject_var aDomain = GetDomainRoot(theSComponentMesh,
157 // Add New Hypothesis
158 SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(aDomain);
159 SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
160 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
161 CORBA::Object_var anObject = StringToObject(theIOR);
162 SMESH::SMESH_Hypothesis_var aDomainItem = SMESH::SMESH_Hypothesis::_narrow(anObject.in());
163 CORBA::String_var aType = aDomainItem->GetName();
164 QString aPixmapName = theDomainPixmap + "_" + aType.in();
165 aPixmap->SetPixMap(aPixmapName.latin1());
166 anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
167 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
168 anIOR->SetValue(theIOR.c_str());
174 //---------------------------------------------------------------
175 SALOMEDS::SObject_var
176 AddHypothesis(const std::string& theIOR,
177 const SALOMEDS::SComponent_var& theSComponentMesh,
178 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
180 return AddToDomain(theIOR,
184 QObject::tr("SMESH_MEN_HYPOTHESIS"),
185 "ICON_SMESH_TREE_HYPO");
189 //---------------------------------------------------------------
190 SALOMEDS::SObject_var
191 AddAlgorithms(const std::string& theIOR,
192 const SALOMEDS::SComponent_var& theSComponentMesh,
193 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
195 return AddToDomain(theIOR,
199 QObject::tr("SMESH_MEN_ALGORITHMS"),
200 "ICON_SMESH_TREE_ALGO");
204 //---------------------------------------------------------------
206 SetDomain(const char* theMeshOrSubMeshEntry,
207 const char* theDomainEntry,
208 const SALOMEDS::Study_var& theStudy,
209 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
210 long theRefOnAppliedDomainTag,
211 const QString& theAppliedDomainMEN,
212 const QString& theAppliedDomainICON)
214 SALOMEDS::SObject_var aMeshOrSubMeshSO = theStudy->FindObjectID(theMeshOrSubMeshEntry);
215 SALOMEDS::SObject_var aHypothesisSO = theStudy->FindObjectID(theDomainEntry);
217 if(!aMeshOrSubMeshSO->_is_nil() && !aHypothesisSO->_is_nil()){
218 //Find or Create Applied Hypothesis root
219 SALOMEDS::SObject_var anAppliedDomainSO;
220 if(!aMeshOrSubMeshSO->FindSubObject(theRefOnAppliedDomainTag,anAppliedDomainSO)){
221 anAppliedDomainSO = theStudyBuilder->NewObjectToTag(aMeshOrSubMeshSO,theRefOnAppliedDomainTag);
222 SALOMEDS::GenericAttribute_var anAttr =
223 theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeName");
224 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
225 aName->SetValue(theAppliedDomainMEN.latin1());
226 anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeSelectable");
227 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
228 aSelAttr->SetSelectable(false);
229 anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributePixMap");
230 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
231 aPixmap->SetPixMap(theAppliedDomainICON.latin1());
233 SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(anAppliedDomainSO);
234 theStudyBuilder->Addreference(aSObject,aHypothesisSO);
239 //---------------------------------------------------------------
241 SetHypothesis(const char* theMeshOrSubMeshEntry,
242 const char* theDomainEntry,
243 const SALOMEDS::Study_var& theStudy,
244 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
246 SetDomain(theMeshOrSubMeshEntry,
250 Tag_RefOnAppliedHypothesis,
251 QObject::tr("SMESH_MEN_APPLIED_HYPOTHESIS"),
252 "ICON_SMESH_TREE_HYPO");
256 //---------------------------------------------------------------
258 SetAlgorithms(const char* theMeshOrSubMeshEntry,
259 const char* theDomainEntry,
260 const SALOMEDS::Study_var& theStudy,
261 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
263 SetDomain(theMeshOrSubMeshEntry,
267 Tag_RefOnAppliedAlgorithms,
268 QObject::tr("SMESH_MEN_APPLIED_ALGORIHTMS"),
269 "ICON_SMESH_TREE_ALGO");
274 //===============================================================
275 SMESH_Swig::SMESH_Swig()
277 class TEvent: public SALOME_Event
279 CORBA::ORB_var& myORB;
282 TEvent(CORBA::ORB_var& theORB):
291 ORB_INIT &anORBInit = *SINGLETON_<ORB_INIT>::Instance();
292 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
293 myORB = anORBInit( 0 , 0 );
295 INFOS("internal error : orb not found");
300 MESSAGE("Constructeur");
302 if(!CORBA::is_nil(anORB))
303 ProcessVoidEvent(new TEvent(anORB));
305 ASSERT(CORBA::is_nil(anORB));
309 //===============================================================
311 SMESH_Swig::Init(int theStudyID)
313 class TEvent: public SALOME_Event
316 SALOMEDS::Study_var& myStudy;
317 SALOMEDS::StudyBuilder_var& myStudyBuilder;
318 SALOMEDS::SComponent_var& mySComponentMesh;
320 TEvent(int theStudyID,
321 SALOMEDS::Study_var& theStudy,
322 SALOMEDS::StudyBuilder_var& theStudyBuilder,
323 SALOMEDS::SComponent_var& theSComponentMesh):
324 myStudyID(theStudyID),
326 myStudyBuilder(theStudyBuilder),
327 mySComponentMesh(theSComponentMesh)
334 if(SUIT_ResourceMgr* aResMgr = SMESHGUI::resourceMgr())
335 aResMgr->loadLanguage( QString::null, "en" );
337 SUIT_Session* aSession = SUIT_Session::session();
338 SUIT_Application* anApplication = aSession->activeApplication();
339 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(anApplication);
341 SALOME_NamingService* aNamingService = anApp->namingService();
342 CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
343 SALOMEDS::StudyManager_var aStudyMgr = SALOMEDS::StudyManager::_narrow(anObject);
344 myStudy = aStudyMgr->GetStudyByID(myStudyID);
346 SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
347 aSMESHGen->SetCurrentStudy( myStudy.in() );
349 myStudyBuilder = myStudy->NewBuilder();
351 SALOMEDS::GenericAttribute_var anAttr;
352 SALOMEDS::AttributeName_var aName;
353 SALOMEDS::AttributePixMap_var aPixmap;
355 SALOMEDS::SComponent_var aSComponent = myStudy->FindComponent("SMESH");
356 if(aSComponent->_is_nil()){
357 bool aLocked = myStudy->GetProperties()->IsLocked();
359 myStudy->GetProperties()->SetLocked(false);
361 aSComponent = myStudyBuilder->NewComponent("SMESH");
362 anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributeName");
363 aName = SALOMEDS::AttributeName::_narrow(anAttr);
365 SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI(); //SRN: BugID IPAL9186, load a SMESH gui if it hasn't been loaded
367 CAM_Module* aModule = anApp->module("Mesh");
369 aModule = anApp->loadModule("Mesh");
370 aSMESHGUI = dynamic_cast<SMESHGUI*>(aModule);
371 } //SRN: BugID IPAL9186: end of a fix
372 aName->SetValue(aSMESHGUI->moduleName());
373 anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributePixMap");
374 aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
375 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
376 myStudyBuilder->DefineComponentInstance(aSComponent,aSMESHGen);
378 myStudy->GetProperties()->SetLocked(true);
381 mySComponentMesh = SALOMEDS::SComponent::_narrow(aSComponent);
387 ProcessVoidEvent(new TEvent(theStudyID,
394 //===============================================================
395 SMESH_Swig::~SMESH_Swig()
397 MESSAGE("Destructeur");
401 //===============================================================
403 SMESH_Swig::AddNewMesh(const char* theIOR)
405 MESSAGE("AddNewMesh");
407 // VSR: added temporarily - to be removed - objects are published automatically by engine
408 SALOMEDS::SObject_var aSObject = myStudy->FindObjectIOR(theIOR);
409 if(aSObject->_is_nil()){
410 //Find or Create Hypothesis root
411 GetHypothesisRoot(mySComponentMesh,myStudyBuilder);
412 GetAlgorithmsRoot(mySComponentMesh,myStudyBuilder);
415 aSObject = myStudyBuilder->NewObject(mySComponentMesh);
416 SALOMEDS::GenericAttribute_var anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
417 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
418 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
419 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeIOR");
420 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
421 anIOR->SetValue(theIOR);
424 CORBA::String_var anEntry = aSObject->GetID();
426 return anEntry._retn();
430 //===============================================================
432 SMESH_Swig::AddNewHypothesis(const char* theIOR)
435 MESSAGE("AddNewHypothesis");
437 SALOMEDS::SObject_var aSObject = ::AddHypothesis(theIOR,
440 CORBA::String_var anEntry = aSObject->GetID();
441 return anEntry._retn();
445 //===============================================================
447 SMESH_Swig::AddNewAlgorithms(const char* theIOR)
449 MESSAGE("AddNewAlgorithms");
451 SALOMEDS::SObject_var aSObject = ::AddAlgorithms(theIOR,
454 CORBA::String_var anEntry = aSObject->GetID();
455 return anEntry._retn();
459 //===============================================================
461 SMESH_Swig::SetShape(const char* theShapeEntry,
462 const char* theMeshEntry)
464 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID( theMeshEntry );
465 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID( theShapeEntry );
467 if(!aMeshSO->_is_nil() && !aGeomShapeSO->_is_nil()){
468 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObjectToTag(aMeshSO,Tag_RefOnShape);
469 myStudyBuilder->Addreference(aSObject,aGeomShapeSO);
474 //===============================================================
476 SMESH_Swig::SetHypothesis(const char* theMeshOrSubMeshEntry,
477 const char* theDomainEntry)
479 ::SetHypothesis(theMeshOrSubMeshEntry,
486 //===============================================================
488 SMESH_Swig::SetAlgorithms(const char* theMeshOrSubMeshEntry,
489 const char* theDomainEntry)
491 ::SetAlgorithms(theMeshOrSubMeshEntry,
498 //===============================================================
500 SMESH_Swig::UnSetHypothesis(const char* theDomainEntry)
502 SALOMEDS::SObject_var aDomainSO = myStudy->FindObjectID(theDomainEntry);
503 if(!aDomainSO->_is_nil())
504 myStudyBuilder->RemoveObject(aDomainSO);
508 SMESH_Swig::AddSubMesh(const char* theMeshEntry,
509 const char* theSubMeshIOR,
512 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(theMeshEntry);
513 if(!aMeshSO->_is_nil()){
515 QString aSubMeshName;
516 switch(theShapeType){
518 aShapeTag = Tag_SubMeshOnSolid;
519 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnSolid");
522 aShapeTag = Tag_SubMeshOnFace;
523 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnFace");
526 aShapeTag = Tag_SubMeshOnEdge;
527 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnEdge");
530 aShapeTag = Tag_SubMeshOnVertex;
531 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnVertex");
534 aShapeTag = Tag_SubMeshOnCompound;
535 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnCompound");
538 SALOMEDS::SObject_var aSubMeshesRoot;
539 SALOMEDS::GenericAttribute_var anAttr;
540 if(!aMeshSO->FindSubObject(aShapeTag,aSubMeshesRoot)){
541 aSubMeshesRoot = myStudyBuilder->NewObjectToTag(aMeshSO,aShapeTag);
542 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeName");
543 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
544 aName->SetValue(aSubMeshName.latin1());
545 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeSelectable");
546 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
547 aSelAttr->SetSelectable(false);
550 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObject(aSubMeshesRoot);
551 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
552 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
553 anIOR->SetValue(theSubMeshIOR);
555 CORBA::String_var aString = aSObject->GetID();
556 return aString._retn();
563 SMESH_Swig::AddSubMeshOnShape(const char* theMeshEntry,
564 const char* theGeomShapeEntry,
565 const char* theSubMeshIOR,
568 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID(theGeomShapeEntry);
569 if(!aGeomShapeSO->_is_nil()){
570 const char * aSubMeshEntry = AddSubMesh(theMeshEntry,theSubMeshIOR,ShapeType);
571 SALOMEDS::SObject_var aSubMeshSO = myStudy->FindObjectID(aSubMeshEntry);
572 if(!aSubMeshSO->_is_nil()){
573 SetShape(theGeomShapeEntry,aSubMeshEntry);
574 CORBA::String_var aString = aSubMeshSO->GetID();
575 return aString._retn();
582 void SMESH_Swig::CreateAndDisplayActor( const char* Mesh_Entry )
584 // SMESH_Actor* Mesh = smeshGUI->ReadScript(aM);
588 SMESH_Swig::SetName(const char* theEntry,
591 SALOMEDS::SObject_var aSObject = myStudy->FindObjectID(theEntry);
592 SALOMEDS::GenericAttribute_var anAttr;
593 SALOMEDS::AttributeName_var aName;
594 if(!aSObject->_is_nil()){
595 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeName");
596 aName = SALOMEDS::AttributeName::_narrow(anAttr);
597 aName->SetValue(theName);
601 //================================================================================
603 * \brief Set mesh icon according to compute status
604 * \param Mesh_Entry - entry of a mesh
605 * \param isComputed - is mesh computed or not
607 //================================================================================
609 void SMESH_Swig::SetMeshIcon(const char* theMeshEntry,
610 const bool theIsComputed)
612 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(theMeshEntry);
613 if(!aMeshSO->_is_nil())
614 if(_PTR(SObject) aMesh = ClientFactory::SObject(aMeshSO))
615 SMESH::ModifiedMesh(aMesh,theIsComputed);