1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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, or (at your option) any later version.
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
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File : SMESHGUI_Utils.cxx
25 // Author : Open CASCADE S.A.S.
28 #include "SMESHGUI_Utils.h"
30 #include "SMESHGUI_Selection.h"
31 #include "SMESH_Type.h"
32 #include "SMESH_MeshAlgos.hxx"
33 #include <SMDS_MeshNode.hxx>
34 #include <SMDS_MeshFace.hxx>
36 // SALOME GUI includes
37 #include <SUIT_Desktop.h>
38 #include <SUIT_Session.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_ResourceMgr.h>
42 #include <LightApp_SelectionMgr.h>
43 #include <SalomeApp_Application.h>
44 #include <SalomeApp_Module.h>
45 #include <SalomeApp_Study.h>
47 #include <SALOME_ListIO.hxx>
51 #include <TColgp_Array1OfXYZ.hxx>
53 #include CORBA_SERVER_HEADER(SMESH_Group)
55 //VSR: uncomment below macro to support unicode text properly in SALOME
56 // current commented out due to regressions
57 //#define PAL22528_UNICODE
62 GetDesktop(const CAM_Module* theModule)
64 return theModule->application()->desktop();
67 LightApp_SelectionMgr*
68 GetSelectionMgr(const SalomeApp_Module* theModule)
70 return theModule->getApp()->selectionMgr();
74 GetResourceMgr( const SalomeApp_Module* )
76 return SUIT_Session::session()->resourceMgr();
80 GetCStudy(const SalomeApp_Study* theStudy)
82 return theStudy->studyDS();
86 DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
88 CORBA::Object_var anObj;
90 const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
93 _PTR(Study) aStudy = GetActiveStudyDocument();
94 _PTR(SObject) aSObj = aStudy->FindObjectID(anIO->getEntry());
95 anObj = SObjectToObject(aSObj,aStudy);
102 SUIT_Study* GetActiveStudy()
104 SUIT_Application* app = SUIT_Session::session()->activeApplication();
106 return app->activeStudy();
111 SUIT_ViewWindow* GetActiveWindow()
113 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
114 (SUIT_Session::session()->activeApplication());
115 if (app && app->desktop() )
116 return app->desktop()->activeWindow();
121 _PTR(Study) GetActiveStudyDocument()
123 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
125 return aStudy->studyDS();
127 return _PTR(Study)();
130 _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
132 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
133 (SUIT_Session::session()->activeApplication());
134 if (app && !CORBA::is_nil(theObject)) {
135 if(_PTR(Study) aStudy = GetActiveStudyDocument()){
136 CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
137 if (strcmp(anIOR.in(), "") != 0)
138 return aStudy->FindObjectIOR(anIOR.in());
141 return _PTR(SObject)();
144 void SetName (_PTR(SObject) theSObject, const QString& theName)
146 _PTR(Study) aStudy = GetActiveStudyDocument();
147 if (aStudy->GetProperties()->IsLocked())
149 SMESHGUI::GetSMESHGen()->SetName(theSObject->GetIOR().c_str(), theName.toLatin1().data());
152 void SetValue (_PTR(SObject) theSObject, const QString& theValue)
154 _PTR(Study) aStudy = GetActiveStudyDocument();
155 if (aStudy->GetProperties()->IsLocked())
157 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
158 _PTR(GenericAttribute) anAttr =
159 aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
160 _PTR(AttributeComment) aComment = anAttr;
162 aComment->SetValue(theValue.toLatin1().data());
165 void setFileName (_PTR(SObject) theSObject, const QString& theValue)
167 _PTR(Study) aStudy = GetActiveStudyDocument();
168 if (aStudy->GetProperties()->IsLocked())
170 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
171 _PTR(GenericAttribute) anAttr =
172 aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
173 _PTR(AttributeExternalFileDef) aFileName = anAttr;
175 aFileName->SetValue(theValue.toLatin1().data());
178 void setFileType (_PTR(SObject) theSObject, const QString& theValue)
180 _PTR(Study) aStudy = GetActiveStudyDocument();
181 if (aStudy->GetProperties()->IsLocked())
183 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
184 _PTR(GenericAttribute) anAttr =
185 aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
186 _PTR(AttributeFileType) aFileType = anAttr;
188 aFileType->SetValue(theValue.toLatin1().data());
191 CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
192 _PTR(Study) /*theStudy*/)
194 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
195 (SUIT_Session::session()->activeApplication());
197 _PTR(GenericAttribute) anAttr;
198 if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
199 _PTR(AttributeIOR) anIOR = anAttr;
200 std::string aVal = anIOR->Value();
201 // string_to_object() DOC: If the input string is not valid ...
202 // a CORBA::SystemException is thrown.
203 if ( aVal.size() > 0 )
204 return app->orb()->string_to_object( aVal.c_str() );
207 return CORBA::Object::_nil();
210 CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
212 _PTR(Study) aStudy;// = GetActiveStudyDocument(); -- aStudy is not used
213 return SObjectToObject(theSObject,aStudy);
216 _PTR(SObject) ObjectToSObject( CORBA::Object_ptr theObject )
219 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
220 (SUIT_Session::session()->activeApplication());
222 CORBA::String_var ior = app->orb()->object_to_string( theObject );
223 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
224 if ( study && strlen( ior ) > 0 )
225 res = study->studyDS()->FindObjectIOR( ior.in() );
230 CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
232 if (!theIO.IsNull()) {
233 if (theIO->hasEntry()) {
234 _PTR(Study) aStudy = GetActiveStudyDocument();
235 _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
236 return SObjectToObject(anObj,aStudy);
239 return CORBA::Object::_nil();
242 CORBA::Object_var IORToObject (const QString& theIOR)
244 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
245 (SUIT_Session::session()->activeApplication());
246 return app->orb()->string_to_object(theIOR.toLatin1().data());
249 int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
254 SALOME_ListIO selected;
255 theMgr->selectedObjects(selected);
256 int aNbSel = selected.Extent();
258 Handle(SALOME_InteractiveObject) anIObject = selected.First();
259 theName = QString( anIObject->getName() ).trimmed();
261 theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
266 _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
268 GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
269 if (!aShape->_is_nil()){ //It s a shape
270 return theSObject->GetFather();
272 _PTR(SObject) aSObject;
273 if (theSObject->ReferencedObject(aSObject)) {
274 aSObject = theSObject->GetFather();
275 return aSObject->GetFather();
277 return theSObject->GetFather();
280 void ModifiedMesh (_PTR(SObject) theSObject, bool theIsNotModif, bool isEmptyMesh)
282 _PTR(Study) aStudy = GetActiveStudyDocument();
283 if (aStudy->GetProperties()->IsLocked())
286 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
287 _PTR(GenericAttribute) anAttr =
288 aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
289 _PTR(AttributePixMap) aPixmap = anAttr;
293 pmName = "ICON_SMESH_TREE_MESH";
294 else if ( isEmptyMesh )
295 pmName = "ICON_SMESH_TREE_MESH_WARN";
297 pmName = "ICON_SMESH_TREE_MESH_PARTIAL";
298 aPixmap->SetPixMap( pmName );
300 _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
301 for (int i = 1; anIter->More(); anIter->Next(), i++) {
302 _PTR(SObject) aSObj = anIter->Value();
304 _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
305 for ( ; anIter1->More(); anIter1->Next())
307 _PTR(SObject) aSObj1 = anIter1->Value();
309 anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
312 std::string entry = aSObj1->GetID();
313 int objType = SMESHGUI_Selection::type( entry.c_str(), aStudy );
315 SMESH::SMESH_IDSource_var idSrc = SObjectToInterface<SMESH::SMESH_IDSource>( aSObj1 );
316 if ( !idSrc->_is_nil() )
318 SMESH::SMESH_GroupOnFilter_var gof =
319 SObjectToInterface<SMESH::SMESH_GroupOnFilter>( aSObj1 );
320 const bool isGroupOnFilter = !gof->_is_nil();
322 bool isEmpty = false;
323 if ( !isGroupOnFilter ) // GetTypes() can be very long on isGroupOnFilter!
325 SMESH::array_of_ElementType_var elemTypes = idSrc->GetTypes();
326 isEmpty = ( elemTypes->length() == 0 );
329 aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
330 else if ( objType != GROUP )
331 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
332 else if ( isGroupOnFilter )
333 aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP_ON_FILTER" );
335 aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
339 if ( !theIsNotModif )
340 aPixmap->SetPixMap( pmName );
341 else if ( objType == GROUP )
342 aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
344 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
351 void ShowHelpFile (const QString& theHelpFileName)
353 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
355 SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
356 app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
360 SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
361 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
362 arg(app->resourceMgr()->stringValue("ExternalBrowser",
364 arg(theHelpFileName));
368 //=======================================================================
370 Return normale to a given face
372 //=======================================================================
373 gp_XYZ getNormale( const SMDS_MeshFace* theFace )
376 SMESH_MeshAlgos::FaceNormal( theFace, n, /*normalized=*/true );
380 QString fromUtf8( const char* txt )
382 #ifdef PAL22528_UNICODE
383 return QString::fromUtf8( txt );
385 return QString( txt );
389 QString fromUtf8( const std::string& txt )
391 return fromUtf8( txt.c_str() );
394 toUtf8::toUtf8( const QString& txt )
396 #ifdef PAL22528_UNICODE
397 assign( txt.toUtf8().constData() );
399 assign( txt.toLatin1().constData() );
403 } // end of namespace SMESH