1 // Copyright (C) 2007-2016 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 || 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 ( ; anIter->More(); anIter->Next() ) {
302 _PTR(SObject) aSObj = anIter->Value();
303 if ( aSObj->Tag() >= SMESH::Tag_FirstSubMesh )
305 _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
306 for ( ; anIter1->More(); anIter1->Next())
308 _PTR(SObject) aSObj1 = anIter1->Value();
309 _PTR(SObject) aSObjectRef;
310 if (aSObj1->ReferencedObject(aSObjectRef))
311 continue; // reference to an object
313 anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
316 std::string entry = aSObj1->GetID();
317 int objType = SMESHGUI_Selection::type( entry.c_str(), aStudy );
318 if ( objType == SMESH::HYPOTHESIS || objType == SMESH::ALGORITHM )
321 SMESH::SMESH_IDSource_var idSrc = SObjectToInterface<SMESH::SMESH_IDSource>( aSObj1 );
322 if ( !idSrc->_is_nil() )
324 SMESH::SMESH_GroupOnFilter_var gof =
325 SObjectToInterface<SMESH::SMESH_GroupOnFilter>( aSObj1 );
326 const bool isGroupOnFilter = !gof->_is_nil();
328 bool isEmpty = false;
329 if ( !isGroupOnFilter ) // GetTypes() can be very long on GroupOnFilter!
331 SMESH::array_of_ElementType_var elemTypes = idSrc->GetTypes();
332 isEmpty = ( elemTypes->length() == 0 );
335 aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
336 else if ( objType != GROUP )
337 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
338 else if ( isGroupOnFilter )
339 aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP_ON_FILTER" );
341 aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
343 else // is it necessary?
345 if ( !theIsNotModif )
346 aPixmap->SetPixMap( pmName );
347 else if ( objType == GROUP )
348 aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
350 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
357 void ShowHelpFile (const QString& theHelpFileName)
359 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
361 SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
362 app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
366 SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
367 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
368 arg(app->resourceMgr()->stringValue("ExternalBrowser",
370 arg(theHelpFileName));
374 //=======================================================================
376 Return normale to a given face
378 //=======================================================================
379 gp_XYZ getNormale( const SMDS_MeshFace* theFace )
382 SMESH_MeshAlgos::FaceNormal( theFace, n, /*normalized=*/true );
386 QString fromUtf8( const char* txt )
388 #ifdef PAL22528_UNICODE
389 return QString::fromUtf8( txt );
391 return QString( txt );
395 QString fromUtf8( const std::string& txt )
397 return fromUtf8( txt.c_str() );
400 toUtf8::toUtf8( const QString& txt )
402 #ifdef PAL22528_UNICODE
403 assign( txt.toUtf8().constData() );
405 assign( txt.toLatin1().constData() );
409 } // end of namespace SMESH