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 DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
82 CORBA::Object_var anObj;
84 const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
87 _PTR(SObject) aSObj = getStudy()->FindObjectID(anIO->getEntry());
88 anObj = SObjectToObject(aSObj);
95 SUIT_Study* GetActiveStudy()
97 SUIT_Application* app = SUIT_Session::session()->activeApplication();
99 return app->activeStudy();
104 SUIT_ViewWindow* GetActiveWindow()
106 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
107 (SUIT_Session::session()->activeApplication());
108 if (app && app->desktop() )
109 return app->desktop()->activeWindow();
114 _PTR(Study) getStudy()
116 static _PTR(Study) _study;
118 _study = SalomeApp_Application::getStudy();
122 _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
124 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
125 (SUIT_Session::session()->activeApplication());
126 if (app && !CORBA::is_nil(theObject)) {
127 CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
128 if (strcmp(anIOR.in(), "") != 0)
129 return getStudy()->FindObjectIOR(anIOR.in());
131 return _PTR(SObject)();
134 void SetName (_PTR(SObject) theSObject, const QString& theName)
136 if (getStudy()->GetProperties()->IsLocked())
138 SMESHGUI::GetSMESHGen()->SetName(theSObject->GetIOR().c_str(), theName.toUtf8().data());
141 void SetValue (_PTR(SObject) theSObject, const QString& theValue)
143 _PTR(Study) aStudy = getStudy();
144 if (aStudy->GetProperties()->IsLocked())
146 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
147 _PTR(GenericAttribute) anAttr =
148 aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
149 _PTR(AttributeComment) aComment = anAttr;
151 aComment->SetValue(theValue.toUtf8().data());
154 void setFileName (_PTR(SObject) theSObject, const QString& theValue)
156 _PTR(Study) aStudy = getStudy();
157 if (aStudy->GetProperties()->IsLocked())
159 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
160 _PTR(GenericAttribute) anAttr =
161 aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
162 _PTR(AttributeExternalFileDef) aFileName = anAttr;
164 aFileName->SetValue(theValue.toUtf8().data());
167 void setFileType (_PTR(SObject) theSObject, const QString& theValue)
169 _PTR(Study) aStudy = getStudy();
170 if (aStudy->GetProperties()->IsLocked())
172 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
173 _PTR(GenericAttribute) anAttr =
174 aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
175 _PTR(AttributeFileType) aFileType = anAttr;
177 aFileType->SetValue(theValue.toUtf8().data());
180 CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject )
182 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
183 (SUIT_Session::session()->activeApplication());
185 _PTR(GenericAttribute) anAttr;
186 if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
187 _PTR(AttributeIOR) anIOR = anAttr;
188 std::string aVal = anIOR->Value();
189 // string_to_object() DOC: If the input string is not valid ...
190 // a CORBA::SystemException is thrown.
191 if ( aVal.size() > 0 )
192 return app->orb()->string_to_object( aVal.c_str() );
195 return CORBA::Object::_nil();
198 _PTR(SObject) ObjectToSObject( CORBA::Object_ptr theObject )
201 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
202 (SUIT_Session::session()->activeApplication());
204 CORBA::String_var ior = app->orb()->object_to_string( theObject );
205 if ( strlen( ior ) > 0 )
206 res = getStudy()->FindObjectIOR( ior.in() );
211 CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
213 if (!theIO.IsNull()) {
214 if (theIO->hasEntry()) {
215 _PTR(SObject) anObj = getStudy()->FindObjectID(theIO->getEntry());
216 return SObjectToObject(anObj);
219 return CORBA::Object::_nil();
222 CORBA::Object_var IORToObject (const QString& theIOR)
224 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
225 (SUIT_Session::session()->activeApplication());
226 return app->orb()->string_to_object(theIOR.toLatin1().data());
229 CORBA::Object_var EntryToObject( const QString& theEntry )
231 _PTR(SObject) anObj = getStudy()->FindObjectID( theEntry.toLatin1().data() );
232 return SObjectToObject(anObj);
235 int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
240 SALOME_ListIO selected;
241 theMgr->selectedObjects(selected);
242 int aNbSel = selected.Extent();
244 Handle(SALOME_InteractiveObject) anIObject = selected.First();
245 theName = QString( anIObject->getName() ).trimmed();
247 theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
252 _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
254 GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
255 if (!aShape->_is_nil()){ //It s a shape
256 return theSObject->GetFather();
258 _PTR(SObject) aSObject;
259 if (theSObject->ReferencedObject(aSObject)) {
260 aSObject = theSObject->GetFather();
261 return aSObject->GetFather();
263 return theSObject->GetFather();
266 void ShowHelpFile (const QString& theHelpFileName)
268 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
270 SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
271 app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
275 SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
276 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
277 arg(app->resourceMgr()->stringValue("ExternalBrowser",
279 arg(theHelpFileName));
283 //=======================================================================
285 Return normale to a given face
287 //=======================================================================
288 gp_XYZ getNormale( const SMDS_MeshFace* theFace )
291 SMESH_MeshAlgos::FaceNormal( theFace, n, /*normalized=*/true );
295 QString fromUtf8( const char* txt )
297 #ifdef PAL22528_UNICODE
298 return QString::fromUtf8( txt );
300 return QString( txt );
304 QString fromUtf8( const std::string& txt )
306 return fromUtf8( txt.c_str() );
309 toUtf8::toUtf8( const QString& txt )
311 #ifdef PAL22528_UNICODE
312 assign( txt.toUtf8().constData() );
314 assign( txt.toLatin1().constData() );
318 } // end of namespace SMESH