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"
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 <qapplication.h>
56 #include <SALOMEconfig.h>
57 #include CORBA_SERVER_HEADER(SMESH_Gen)
58 #include CORBA_SERVER_HEADER(SMESH_Mesh)
59 #include CORBA_SERVER_HEADER(SMESH_Hypothesis)
63 static CORBA::ORB_var anORB;
66 static long Tag_HypothesisRoot = 1;
67 static long Tag_AlgorithmsRoot = 2;
69 static long Tag_RefOnShape = 1;
70 static long Tag_RefOnAppliedHypothesis = 2;
71 static long Tag_RefOnAppliedAlgorithms = 3;
73 static long Tag_SubMeshOnVertex = 4;
74 static long Tag_SubMeshOnEdge = 5;
75 static long Tag_SubMeshOnFace = 6;
76 static long Tag_SubMeshOnSolid = 7;
77 static long Tag_SubMeshOnCompound = 8;
81 //---------------------------------------------------------------
84 StringToObject(const std::string& theIOR)
86 return anORB->string_to_object(theIOR.c_str());
90 //---------------------------------------------------------------
93 GetDomainRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
94 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
95 CORBA::Long theDomainRootTag,
96 const QString& theName,
97 const QString& thePixmap)
99 SALOMEDS::SObject_var aDomainRoot;
100 if (!theSComponentMesh->FindSubObject(theDomainRootTag,aDomainRoot)) {
101 aDomainRoot = theStudyBuilder->NewObjectToTag(theSComponentMesh,theDomainRootTag);
102 SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeName");
103 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
104 aName->SetValue(theName.latin1());
105 anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributePixMap");
106 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
107 aPixmap->SetPixMap(thePixmap.latin1());
108 anAttr = theStudyBuilder->FindOrCreateAttribute(aDomainRoot,"AttributeSelectable");
109 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
110 aSelAttr->SetSelectable(false);
117 //---------------------------------------------------------------
119 SALOMEDS::SObject_var
120 GetHypothesisRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
121 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
123 return GetDomainRoot(theSComponentMesh,
126 QObject::tr("SMESH_MEN_HYPOTHESIS"),
127 "ICON_SMESH_TREE_HYPO");
131 //---------------------------------------------------------------
133 SALOMEDS::SObject_var
134 GetAlgorithmsRoot(const SALOMEDS::SComponent_var& theSComponentMesh,
135 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
137 return GetDomainRoot(theSComponentMesh,
140 QObject::tr("SMESH_MEN_ALGORITHMS"),
141 "ICON_SMESH_TREE_ALGO");
145 //---------------------------------------------------------------
147 SALOMEDS::SObject_var
148 AddToDomain(const std::string& theIOR,
149 const SALOMEDS::SComponent_var& theSComponentMesh,
150 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
151 CORBA::Long theDomainRootTag,
152 const QString& theDomainName,
153 const QString& theDomainPixmap)
155 SALOMEDS::SObject_var aDomain = GetDomainRoot(theSComponentMesh,
160 // Add New Hypothesis
161 SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(aDomain);
162 SALOMEDS::GenericAttribute_var anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
163 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
164 CORBA::Object_var anObject = StringToObject(theIOR);
165 SMESH::SMESH_Hypothesis_var aDomainItem = SMESH::SMESH_Hypothesis::_narrow(anObject.in());
166 CORBA::String_var aType = aDomainItem->GetName();
167 QString aPixmapName = theDomainPixmap + "_" + aType.in();
168 aPixmap->SetPixMap(aPixmapName.latin1());
169 anAttr = theStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
170 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
171 anIOR->SetValue(theIOR.c_str());
177 //---------------------------------------------------------------
178 SALOMEDS::SObject_var
179 AddHypothesis(const std::string& theIOR,
180 const SALOMEDS::SComponent_var& theSComponentMesh,
181 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
183 return AddToDomain(theIOR,
187 QObject::tr("SMESH_MEN_HYPOTHESIS"),
188 "ICON_SMESH_TREE_HYPO");
192 //---------------------------------------------------------------
193 SALOMEDS::SObject_var
194 AddAlgorithms(const std::string& theIOR,
195 const SALOMEDS::SComponent_var& theSComponentMesh,
196 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
198 return AddToDomain(theIOR,
202 QObject::tr("SMESH_MEN_ALGORITHMS"),
203 "ICON_SMESH_TREE_ALGO");
207 //---------------------------------------------------------------
209 SetDomain(const char* theMeshOrSubMeshEntry,
210 const char* theDomainEntry,
211 const SALOMEDS::Study_var& theStudy,
212 const SALOMEDS::StudyBuilder_var& theStudyBuilder,
213 long theRefOnAppliedDomainTag,
214 const QString& theAppliedDomainMEN,
215 const QString& theAppliedDomainICON)
217 SALOMEDS::SObject_var aMeshOrSubMeshSO = theStudy->FindObjectID(theMeshOrSubMeshEntry);
218 SALOMEDS::SObject_var aHypothesisSO = theStudy->FindObjectID(theDomainEntry);
220 if(!aMeshOrSubMeshSO->_is_nil() && !aHypothesisSO->_is_nil()){
221 //Find or Create Applied Hypothesis root
222 SALOMEDS::SObject_var anAppliedDomainSO;
223 if(!aMeshOrSubMeshSO->FindSubObject(theRefOnAppliedDomainTag,anAppliedDomainSO)){
224 anAppliedDomainSO = theStudyBuilder->NewObjectToTag(aMeshOrSubMeshSO,theRefOnAppliedDomainTag);
225 SALOMEDS::GenericAttribute_var anAttr =
226 theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeName");
227 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
228 aName->SetValue(theAppliedDomainMEN.latin1());
229 anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributeSelectable");
230 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
231 aSelAttr->SetSelectable(false);
232 anAttr = theStudyBuilder->FindOrCreateAttribute(anAppliedDomainSO,"AttributePixMap");
233 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
234 aPixmap->SetPixMap(theAppliedDomainICON.latin1());
236 SALOMEDS::SObject_var aSObject = theStudyBuilder->NewObject(anAppliedDomainSO);
237 theStudyBuilder->Addreference(aSObject,aHypothesisSO);
242 //---------------------------------------------------------------
244 SetHypothesis(const char* theMeshOrSubMeshEntry,
245 const char* theDomainEntry,
246 const SALOMEDS::Study_var& theStudy,
247 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
249 SetDomain(theMeshOrSubMeshEntry,
253 Tag_RefOnAppliedHypothesis,
254 QObject::tr("SMESH_MEN_APPLIED_HYPOTHESIS"),
255 "ICON_SMESH_TREE_HYPO");
259 //---------------------------------------------------------------
261 SetAlgorithms(const char* theMeshOrSubMeshEntry,
262 const char* theDomainEntry,
263 const SALOMEDS::Study_var& theStudy,
264 const SALOMEDS::StudyBuilder_var& theStudyBuilder)
266 SetDomain(theMeshOrSubMeshEntry,
270 Tag_RefOnAppliedAlgorithms,
271 QObject::tr("SMESH_MEN_APPLIED_ALGORIHTMS"),
272 "ICON_SMESH_TREE_ALGO");
277 //===============================================================
278 SMESH_Swig::SMESH_Swig()
280 class TEvent: public SALOME_Event
282 CORBA::ORB_var& myORB;
285 TEvent(CORBA::ORB_var& theORB):
294 ORB_INIT &anORBInit = *SINGLETON_<ORB_INIT>::Instance();
295 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
296 myORB = anORBInit( 0 , 0 );
298 INFOS("internal error : orb not found");
303 MESSAGE("Constructeur");
305 if(CORBA::is_nil(anORB))
306 ProcessVoidEvent(new TEvent(anORB));
308 ASSERT(!CORBA::is_nil(anORB));
312 //===============================================================
314 SMESH_Swig::Init(int theStudyID)
316 class TEvent: public SALOME_Event
319 SALOMEDS::Study_var& myStudy;
320 SALOMEDS::StudyBuilder_var& myStudyBuilder;
321 SALOMEDS::SComponent_var& mySComponentMesh;
323 TEvent(int theStudyID,
324 SALOMEDS::Study_var& theStudy,
325 SALOMEDS::StudyBuilder_var& theStudyBuilder,
326 SALOMEDS::SComponent_var& theSComponentMesh):
327 myStudyID(theStudyID),
329 myStudyBuilder(theStudyBuilder),
330 mySComponentMesh(theSComponentMesh)
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);
383 qApp->processEvents(); // Workaround for bug 12662
389 ProcessVoidEvent(new TEvent(theStudyID,
396 //===============================================================
397 SMESH_Swig::~SMESH_Swig()
399 MESSAGE("Destructeur");
403 //===============================================================
405 SMESH_Swig::AddNewMesh(const char* theIOR)
407 MESSAGE("AddNewMesh");
409 // VSR: added temporarily - to be removed - objects are published automatically by engine
410 SALOMEDS::SObject_var aSObject = myStudy->FindObjectIOR(theIOR);
411 if(aSObject->_is_nil()){
412 //Find or Create Hypothesis root
413 GetHypothesisRoot(mySComponentMesh,myStudyBuilder);
414 GetAlgorithmsRoot(mySComponentMesh,myStudyBuilder);
417 aSObject = myStudyBuilder->NewObject(mySComponentMesh);
418 SALOMEDS::GenericAttribute_var anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
419 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
420 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
421 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeIOR");
422 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
423 anIOR->SetValue(theIOR);
426 CORBA::String_var anEntry = aSObject->GetID();
428 return anEntry._retn();
432 //===============================================================
434 SMESH_Swig::AddNewHypothesis(const char* theIOR)
437 MESSAGE("AddNewHypothesis");
439 SALOMEDS::SObject_var aSObject = ::AddHypothesis(theIOR,
442 CORBA::String_var anEntry = aSObject->GetID();
443 return anEntry._retn();
447 //===============================================================
449 SMESH_Swig::AddNewAlgorithms(const char* theIOR)
451 MESSAGE("AddNewAlgorithms");
453 SALOMEDS::SObject_var aSObject = ::AddAlgorithms(theIOR,
456 CORBA::String_var anEntry = aSObject->GetID();
457 return anEntry._retn();
461 //===============================================================
463 SMESH_Swig::SetShape(const char* theShapeEntry,
464 const char* theMeshEntry)
466 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID( theMeshEntry );
467 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID( theShapeEntry );
469 if(!aMeshSO->_is_nil() && !aGeomShapeSO->_is_nil()){
470 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObjectToTag(aMeshSO,Tag_RefOnShape);
471 myStudyBuilder->Addreference(aSObject,aGeomShapeSO);
476 //===============================================================
478 SMESH_Swig::SetHypothesis(const char* theMeshOrSubMeshEntry,
479 const char* theDomainEntry)
481 ::SetHypothesis(theMeshOrSubMeshEntry,
488 //===============================================================
490 SMESH_Swig::SetAlgorithms(const char* theMeshOrSubMeshEntry,
491 const char* theDomainEntry)
493 ::SetAlgorithms(theMeshOrSubMeshEntry,
500 //===============================================================
502 SMESH_Swig::UnSetHypothesis(const char* theDomainEntry)
504 SALOMEDS::SObject_var aDomainSO = myStudy->FindObjectID(theDomainEntry);
505 if(!aDomainSO->_is_nil())
506 myStudyBuilder->RemoveObject(aDomainSO);
510 SMESH_Swig::AddSubMesh(const char* theMeshEntry,
511 const char* theSubMeshIOR,
514 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(theMeshEntry);
515 if(!aMeshSO->_is_nil()){
517 QString aSubMeshName;
518 switch(theShapeType){
520 aShapeTag = Tag_SubMeshOnSolid;
521 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnSolid");
524 aShapeTag = Tag_SubMeshOnFace;
525 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnFace");
528 aShapeTag = Tag_SubMeshOnEdge;
529 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnEdge");
532 aShapeTag = Tag_SubMeshOnVertex;
533 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnVertex");
536 aShapeTag = Tag_SubMeshOnCompound;
537 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnCompound");
540 SALOMEDS::SObject_var aSubMeshesRoot;
541 SALOMEDS::GenericAttribute_var anAttr;
542 if(!aMeshSO->FindSubObject(aShapeTag,aSubMeshesRoot)){
543 aSubMeshesRoot = myStudyBuilder->NewObjectToTag(aMeshSO,aShapeTag);
544 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeName");
545 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
546 aName->SetValue(aSubMeshName.latin1());
547 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeSelectable");
548 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
549 aSelAttr->SetSelectable(false);
552 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObject(aSubMeshesRoot);
553 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
554 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
555 anIOR->SetValue(theSubMeshIOR);
557 CORBA::String_var aString = aSObject->GetID();
558 return aString._retn();
565 SMESH_Swig::AddSubMeshOnShape(const char* theMeshEntry,
566 const char* theGeomShapeEntry,
567 const char* theSubMeshIOR,
570 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID(theGeomShapeEntry);
571 if(!aGeomShapeSO->_is_nil()){
572 const char * aSubMeshEntry = AddSubMesh(theMeshEntry,theSubMeshIOR,ShapeType);
573 SALOMEDS::SObject_var aSubMeshSO = myStudy->FindObjectID(aSubMeshEntry);
574 if(!aSubMeshSO->_is_nil()){
575 SetShape(theGeomShapeEntry,aSubMeshEntry);
576 CORBA::String_var aString = aSubMeshSO->GetID();
577 return aString._retn();
584 void SMESH_Swig::CreateAndDisplayActor( const char* Mesh_Entry )
586 // SMESH_Actor* Mesh = smeshGUI->ReadScript(aM);
590 SMESH_Swig::SetName(const char* theEntry,
593 SALOMEDS::SObject_var aSObject = myStudy->FindObjectID(theEntry);
594 SALOMEDS::GenericAttribute_var anAttr;
595 SALOMEDS::AttributeName_var aName;
596 if(!aSObject->_is_nil()){
597 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeName");
598 aName = SALOMEDS::AttributeName::_narrow(anAttr);
599 aName->SetValue(theName);
603 //================================================================================
605 * \brief Set mesh icon according to compute status
606 * \param Mesh_Entry - entry of a mesh
607 * \param isComputed - is mesh computed or not
609 //================================================================================
611 void SMESH_Swig::SetMeshIcon(const char* theMeshEntry,
612 const bool theIsComputed)
614 class TEvent: public SALOME_Event
616 SALOMEDS::Study_var myStudy;
617 std::string myMeshEntry;
620 TEvent(const SALOMEDS::Study_var& theStudy,
621 const std::string& theMeshEntry,
622 const bool theIsComputed):
624 myMeshEntry(theMeshEntry),
625 myIsComputed(theIsComputed)
632 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(myMeshEntry.c_str());
633 if(!aMeshSO->_is_nil())
634 if(_PTR(SObject) aMesh = ClientFactory::SObject(aMeshSO))
635 SMESH::ModifiedMesh(aMesh,myIsComputed);
639 ProcessVoidEvent(new TEvent(myStudy,