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 SUIT_Session* aSession = SUIT_Session::session();
335 SUIT_Application* anApplication = aSession->activeApplication();
336 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(anApplication);
338 SALOME_NamingService* aNamingService = anApp->namingService();
339 CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
340 SALOMEDS::StudyManager_var aStudyMgr = SALOMEDS::StudyManager::_narrow(anObject);
341 myStudy = aStudyMgr->GetStudyByID(myStudyID);
343 SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
344 aSMESHGen->SetCurrentStudy( myStudy.in() );
346 myStudyBuilder = myStudy->NewBuilder();
348 SALOMEDS::GenericAttribute_var anAttr;
349 SALOMEDS::AttributeName_var aName;
350 SALOMEDS::AttributePixMap_var aPixmap;
352 SALOMEDS::SComponent_var aSComponent = myStudy->FindComponent("SMESH");
353 if(aSComponent->_is_nil()){
354 bool aLocked = myStudy->GetProperties()->IsLocked();
356 myStudy->GetProperties()->SetLocked(false);
358 aSComponent = myStudyBuilder->NewComponent("SMESH");
359 anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributeName");
360 aName = SALOMEDS::AttributeName::_narrow(anAttr);
362 SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI(); //SRN: BugID IPAL9186, load a SMESH gui if it hasn't been loaded
364 CAM_Module* aModule = anApp->module("Mesh");
366 aModule = anApp->loadModule("Mesh");
367 aSMESHGUI = dynamic_cast<SMESHGUI*>(aModule);
368 } //SRN: BugID IPAL9186: end of a fix
369 aName->SetValue(aSMESHGUI->moduleName());
370 anAttr = myStudyBuilder->FindOrCreateAttribute(aSComponent,"AttributePixMap");
371 aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
372 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
373 myStudyBuilder->DefineComponentInstance(aSComponent,aSMESHGen);
375 myStudy->GetProperties()->SetLocked(true);
378 mySComponentMesh = SALOMEDS::SComponent::_narrow(aSComponent);
384 ProcessVoidEvent(new TEvent(theStudyID,
391 //===============================================================
392 SMESH_Swig::~SMESH_Swig()
394 MESSAGE("Destructeur");
398 //===============================================================
400 SMESH_Swig::AddNewMesh(const char* theIOR)
402 MESSAGE("AddNewMesh");
404 // VSR: added temporarily - to be removed - objects are published automatically by engine
405 SALOMEDS::SObject_var aSObject = myStudy->FindObjectIOR(theIOR);
406 if(aSObject->_is_nil()){
407 //Find or Create Hypothesis root
408 GetHypothesisRoot(mySComponentMesh,myStudyBuilder);
409 GetAlgorithmsRoot(mySComponentMesh,myStudyBuilder);
412 aSObject = myStudyBuilder->NewObject(mySComponentMesh);
413 SALOMEDS::GenericAttribute_var anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributePixMap");
414 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
415 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
416 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeIOR");
417 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
418 anIOR->SetValue(theIOR);
421 CORBA::String_var anEntry = aSObject->GetID();
423 return anEntry._retn();
427 //===============================================================
429 SMESH_Swig::AddNewHypothesis(const char* theIOR)
432 MESSAGE("AddNewHypothesis");
434 SALOMEDS::SObject_var aSObject = ::AddHypothesis(theIOR,
437 CORBA::String_var anEntry = aSObject->GetID();
438 return anEntry._retn();
442 //===============================================================
444 SMESH_Swig::AddNewAlgorithms(const char* theIOR)
446 MESSAGE("AddNewAlgorithms");
448 SALOMEDS::SObject_var aSObject = ::AddAlgorithms(theIOR,
451 CORBA::String_var anEntry = aSObject->GetID();
452 return anEntry._retn();
456 //===============================================================
458 SMESH_Swig::SetShape(const char* theShapeEntry,
459 const char* theMeshEntry)
461 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID( theMeshEntry );
462 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID( theShapeEntry );
464 if(!aMeshSO->_is_nil() && !aGeomShapeSO->_is_nil()){
465 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObjectToTag(aMeshSO,Tag_RefOnShape);
466 myStudyBuilder->Addreference(aSObject,aGeomShapeSO);
471 //===============================================================
473 SMESH_Swig::SetHypothesis(const char* theMeshOrSubMeshEntry,
474 const char* theDomainEntry)
476 ::SetHypothesis(theMeshOrSubMeshEntry,
483 //===============================================================
485 SMESH_Swig::SetAlgorithms(const char* theMeshOrSubMeshEntry,
486 const char* theDomainEntry)
488 ::SetAlgorithms(theMeshOrSubMeshEntry,
495 //===============================================================
497 SMESH_Swig::UnSetHypothesis(const char* theDomainEntry)
499 SALOMEDS::SObject_var aDomainSO = myStudy->FindObjectID(theDomainEntry);
500 if(!aDomainSO->_is_nil())
501 myStudyBuilder->RemoveObject(aDomainSO);
505 SMESH_Swig::AddSubMesh(const char* theMeshEntry,
506 const char* theSubMeshIOR,
509 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(theMeshEntry);
510 if(!aMeshSO->_is_nil()){
512 QString aSubMeshName;
513 switch(theShapeType){
515 aShapeTag = Tag_SubMeshOnSolid;
516 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnSolid");
519 aShapeTag = Tag_SubMeshOnFace;
520 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnFace");
523 aShapeTag = Tag_SubMeshOnEdge;
524 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnEdge");
527 aShapeTag = Tag_SubMeshOnVertex;
528 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnVertex");
531 aShapeTag = Tag_SubMeshOnCompound;
532 aSubMeshName = QObject::tr("SMESH_MEN_SubMeshesOnCompound");
535 SALOMEDS::SObject_var aSubMeshesRoot;
536 SALOMEDS::GenericAttribute_var anAttr;
537 if(!aMeshSO->FindSubObject(aShapeTag,aSubMeshesRoot)){
538 aSubMeshesRoot = myStudyBuilder->NewObjectToTag(aMeshSO,aShapeTag);
539 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeName");
540 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
541 aName->SetValue(aSubMeshName.latin1());
542 anAttr = myStudyBuilder->FindOrCreateAttribute(aSubMeshesRoot,"AttributeSelectable");
543 SALOMEDS::AttributeSelectable_var aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
544 aSelAttr->SetSelectable(false);
547 SALOMEDS::SObject_var aSObject = myStudyBuilder->NewObject(aSubMeshesRoot);
548 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeIOR");
549 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
550 anIOR->SetValue(theSubMeshIOR);
552 CORBA::String_var aString = aSObject->GetID();
553 return aString._retn();
560 SMESH_Swig::AddSubMeshOnShape(const char* theMeshEntry,
561 const char* theGeomShapeEntry,
562 const char* theSubMeshIOR,
565 SALOMEDS::SObject_var aGeomShapeSO = myStudy->FindObjectID(theGeomShapeEntry);
566 if(!aGeomShapeSO->_is_nil()){
567 const char * aSubMeshEntry = AddSubMesh(theMeshEntry,theSubMeshIOR,ShapeType);
568 SALOMEDS::SObject_var aSubMeshSO = myStudy->FindObjectID(aSubMeshEntry);
569 if(!aSubMeshSO->_is_nil()){
570 SetShape(theGeomShapeEntry,aSubMeshEntry);
571 CORBA::String_var aString = aSubMeshSO->GetID();
572 return aString._retn();
579 void SMESH_Swig::CreateAndDisplayActor( const char* Mesh_Entry )
581 // SMESH_Actor* Mesh = smeshGUI->ReadScript(aM);
585 SMESH_Swig::SetName(const char* theEntry,
588 SALOMEDS::SObject_var aSObject = myStudy->FindObjectID(theEntry);
589 SALOMEDS::GenericAttribute_var anAttr;
590 SALOMEDS::AttributeName_var aName;
591 if(!aSObject->_is_nil()){
592 anAttr = myStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeName");
593 aName = SALOMEDS::AttributeName::_narrow(anAttr);
594 aName->SetValue(theName);
598 //================================================================================
600 * \brief Set mesh icon according to compute status
601 * \param Mesh_Entry - entry of a mesh
602 * \param isComputed - is mesh computed or not
604 //================================================================================
606 void SMESH_Swig::SetMeshIcon(const char* theMeshEntry,
607 const bool theIsComputed)
609 class TEvent: public SALOME_Event
611 SALOMEDS::Study_var myStudy;
612 std::string myMeshEntry;
615 TEvent(const SALOMEDS::Study_var& theStudy,
616 const std::string& theMeshEntry,
617 const bool theIsComputed):
619 myMeshEntry(theMeshEntry),
620 myIsComputed(theIsComputed)
627 SALOMEDS::SObject_var aMeshSO = myStudy->FindObjectID(myMeshEntry.c_str());
628 if(!aMeshSO->_is_nil())
629 if(_PTR(SObject) aMesh = ClientFactory::SObject(aMeshSO))
630 SMESH::ModifiedMesh(aMesh,myIsComputed);
634 ProcessVoidEvent(new TEvent(myStudy,