1 // Copyright (C) 2007-2011 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.
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"
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)
58 GetDesktop(const CAM_Module* theModule)
60 return theModule->application()->desktop();
63 LightApp_SelectionMgr*
64 GetSelectionMgr(const SalomeApp_Module* theModule)
66 return theModule->getApp()->selectionMgr();
70 GetResourceMgr( const SalomeApp_Module* )
72 return SUIT_Session::session()->resourceMgr();
76 GetCStudy(const SalomeApp_Study* theStudy)
78 return theStudy->studyDS();
82 DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
84 CORBA::Object_var anObj;
86 const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
89 _PTR(Study) aStudy = GetActiveStudyDocument();
90 _PTR(SObject) aSObj = aStudy->FindObjectID(anIO->getEntry());
91 anObj = SObjectToObject(aSObj,aStudy);
98 SUIT_Study* GetActiveStudy()
100 SUIT_Application* app = SUIT_Session::session()->activeApplication();
102 return app->activeStudy();
107 SUIT_ViewWindow* GetActiveWindow()
109 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
110 (SUIT_Session::session()->activeApplication());
111 if (app && app->desktop() )
112 return app->desktop()->activeWindow();
117 _PTR(Study) GetActiveStudyDocument()
119 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
121 return aStudy->studyDS();
123 return _PTR(Study)();
126 _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
128 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
129 (SUIT_Session::session()->activeApplication());
130 if (app && !CORBA::is_nil(theObject)) {
131 if(_PTR(Study) aStudy = GetActiveStudyDocument()){
132 CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
133 if (strcmp(anIOR.in(), "") != 0)
134 return aStudy->FindObjectIOR(anIOR.in());
137 return _PTR(SObject)();
140 void SetName (_PTR(SObject) theSObject, const QString& theName)
142 _PTR(Study) aStudy = GetActiveStudyDocument();
143 if (aStudy->GetProperties()->IsLocked())
145 SMESHGUI::GetSMESHGen()->SetName(theSObject->GetIOR().c_str(), theName.toLatin1().data());
148 void SetValue (_PTR(SObject) theSObject, const QString& theValue)
150 _PTR(Study) aStudy = GetActiveStudyDocument();
151 if (aStudy->GetProperties()->IsLocked())
153 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
154 _PTR(GenericAttribute) anAttr =
155 aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
156 _PTR(AttributeComment) aComment = anAttr;
158 aComment->SetValue(theValue.toLatin1().data());
161 void setFileName (_PTR(SObject) theSObject, const QString& theValue)
163 _PTR(Study) aStudy = GetActiveStudyDocument();
164 if (aStudy->GetProperties()->IsLocked())
166 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
167 _PTR(GenericAttribute) anAttr =
168 aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
169 _PTR(AttributeExternalFileDef) aFileName = anAttr;
171 aFileName->SetValue(theValue.toLatin1().data());
174 void setFileType (_PTR(SObject) theSObject, const QString& theValue)
176 _PTR(Study) aStudy = GetActiveStudyDocument();
177 if (aStudy->GetProperties()->IsLocked())
179 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
180 _PTR(GenericAttribute) anAttr =
181 aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
182 _PTR(AttributeFileType) aFileType = anAttr;
184 aFileType->SetValue(theValue.toLatin1().data());
187 CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
188 _PTR(Study) theStudy)
190 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
191 (SUIT_Session::session()->activeApplication());
193 _PTR(GenericAttribute) anAttr;
194 if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
195 _PTR(AttributeIOR) anIOR = anAttr;
196 CORBA::String_var aVal = anIOR->Value().c_str();
197 // string_to_object() DOC: If the input string is not valid ...
198 // a CORBA::SystemException is thrown.
199 if ( aVal && strlen( aVal ) > 0 )
200 return app->orb()->string_to_object(aVal);
203 return CORBA::Object::_nil();
206 CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
208 _PTR(Study) aStudy = GetActiveStudyDocument();
209 return SObjectToObject(theSObject,aStudy);
212 _PTR(SObject) ObjectToSObject( CORBA::Object_ptr theObject )
215 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
216 (SUIT_Session::session()->activeApplication());
218 QString IOR = app->orb()->object_to_string( theObject );
219 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
220 if ( study && !IOR.isEmpty() )
221 res = study->studyDS()->FindObjectIOR( IOR.toLatin1().constData() );
226 CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
228 if (!theIO.IsNull()) {
229 if (theIO->hasEntry()) {
230 _PTR(Study) aStudy = GetActiveStudyDocument();
231 _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
232 return SObjectToObject(anObj,aStudy);
235 return CORBA::Object::_nil();
238 CORBA::Object_var IORToObject (const QString& theIOR)
240 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
241 (SUIT_Session::session()->activeApplication());
242 return app->orb()->string_to_object(theIOR.toLatin1().data());
245 int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
250 SALOME_ListIO selected;
251 theMgr->selectedObjects(selected);
252 int aNbSel = selected.Extent();
254 Handle(SALOME_InteractiveObject) anIObject = selected.First();
255 theName = QString( anIObject->getName() ).trimmed();
257 theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
262 _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
264 GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
265 if (!aShape->_is_nil()){ //It s a shape
266 return theSObject->GetFather();
268 _PTR(SObject) aSObject;
269 if (theSObject->ReferencedObject(aSObject)) {
270 aSObject = theSObject->GetFather();
271 return aSObject->GetFather();
273 return theSObject->GetFather();
276 void ModifiedMesh (_PTR(SObject) theSObject, bool theIsNotModif, bool isEmptyMesh)
278 _PTR(Study) aStudy = GetActiveStudyDocument();
279 if (aStudy->GetProperties()->IsLocked())
282 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
283 _PTR(GenericAttribute) anAttr =
284 aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
285 _PTR(AttributePixMap) aPixmap = anAttr;
289 pmName = "ICON_SMESH_TREE_MESH";
290 else if ( isEmptyMesh )
291 pmName = "ICON_SMESH_TREE_MESH_WARN";
293 pmName = "ICON_SMESH_TREE_MESH_PARTIAL";
294 aPixmap->SetPixMap( pmName );
296 _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
297 for (int i = 1; anIter->More(); anIter->Next(), i++) {
298 _PTR(SObject) aSObj = anIter->Value();
300 _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
301 for ( ; anIter1->More(); anIter1->Next())
303 _PTR(SObject) aSObj1 = anIter1->Value();
305 anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
308 std::string entry = aSObj1->GetID();
309 int objType = SMESHGUI_Selection::type( entry.c_str(), aStudy );
311 SMESH::SMESH_IDSource_var idSrc = SObjectToInterface<SMESH::SMESH_IDSource>( aSObj1 );
312 if ( !idSrc->_is_nil() )
314 SMESH::SMESH_GroupOnFilter_var gof =
315 SObjectToInterface<SMESH::SMESH_GroupOnFilter>( aSObj1 );
316 const bool isGroupOnFilter = !gof->_is_nil();
318 SMESH::array_of_ElementType_var elemTypes = idSrc->GetTypes();
319 const bool isEmpty = ( elemTypes->length() == 0 );
322 aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
323 else if ( objType != GROUP )
324 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
325 else if ( isGroupOnFilter )
326 aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP_ON_FILTER" );
328 aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
332 if ( !theIsNotModif )
333 aPixmap->SetPixMap( pmName );
334 else if ( objType == GROUP )
335 aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
337 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
344 void ShowHelpFile (const QString& theHelpFileName)
346 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
348 SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
349 app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
353 SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
354 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
355 arg(app->resourceMgr()->stringValue("ExternalBrowser",
357 arg(theHelpFileName));
361 //=======================================================================
363 Return normale to a given face
365 //=======================================================================
366 gp_XYZ getNormale( const SMDS_MeshFace* theFace )
369 int aNbNode = theFace->NbNodes();
370 TColgp_Array1OfXYZ anArrOfXYZ(1,4);
371 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
373 for ( ; aNodeItr->more() && i <= 4; i++ ) {
374 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
375 anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
378 gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
379 gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
382 gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
385 double len = n.Modulus();
391 } // end of namespace SMESH