Salome HOME
Merge from V6_3_BR 06/06/2011
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Utils.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_Utils.cxx
25 // Author : Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_Utils.h"
29 #include "SMESHGUI.h"
30
31 #include <SMDS_MeshNode.hxx>
32 #include <SMDS_MeshFace.hxx>
33
34 // SALOME GUI includes
35 #include <SUIT_Desktop.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_ResourceMgr.h>
39
40 #include <LightApp_SelectionMgr.h>
41 #include <SalomeApp_Application.h>
42 #include <SalomeApp_Module.h>
43 #include <SalomeApp_Study.h>
44
45 #include <SALOME_ListIO.hxx>
46
47 // OCC includes
48 #include <gp_XYZ.hxx>
49 #include <TColgp_Array1OfXYZ.hxx>
50
51 namespace SMESH
52 {
53   SUIT_Desktop*
54   GetDesktop(const CAM_Module* theModule)
55   {
56     return theModule->application()->desktop();
57   }
58
59   LightApp_SelectionMgr*
60   GetSelectionMgr(const SalomeApp_Module* theModule)
61   {
62     return theModule->getApp()->selectionMgr();
63   }
64
65   SUIT_ResourceMgr*
66   GetResourceMgr( const SalomeApp_Module* )
67   {
68     return SUIT_Session::session()->resourceMgr();
69   }
70
71   _PTR(Study)
72   GetCStudy(const SalomeApp_Study* theStudy)
73   {
74     return theStudy->studyDS();
75   }
76
77   CORBA::Object_var 
78   DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
79   {
80     CORBA::Object_var anObj;
81     if(theOwner){
82       const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
83       if(!anIO.IsNull()){
84         if(anIO->hasEntry()){
85           _PTR(Study) aStudy = GetActiveStudyDocument();
86           _PTR(SObject) aSObj = aStudy->FindObjectID(anIO->getEntry());
87           anObj = SObjectToObject(aSObj,aStudy);
88         }
89       }
90     }
91     return anObj;
92   }
93
94   SUIT_Study* GetActiveStudy()
95   {
96     SUIT_Application* app = SUIT_Session::session()->activeApplication();
97     if (app)
98       return app->activeStudy();
99     else
100       return NULL;
101   }
102
103   SUIT_ViewWindow* GetActiveWindow()
104   {
105     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
106       (SUIT_Session::session()->activeApplication());
107     if (app && app->desktop() )
108       return app->desktop()->activeWindow();
109     else
110       return NULL;
111   }
112
113   _PTR(Study) GetActiveStudyDocument()
114   {
115     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
116     if (aStudy)
117       return aStudy->studyDS();
118     else
119       return _PTR(Study)();
120   }
121
122   _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
123   {
124     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
125       (SUIT_Session::session()->activeApplication());
126     if (app && !CORBA::is_nil(theObject)) {
127       if(_PTR(Study) aStudy = GetActiveStudyDocument()){
128         CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
129         if (strcmp(anIOR.in(), "") != 0)
130           return aStudy->FindObjectIOR(anIOR.in());
131       }
132     }
133     return _PTR(SObject)();
134   }
135
136   void SetName (_PTR(SObject) theSObject, const QString& theName)
137   {
138     _PTR(Study) aStudy = GetActiveStudyDocument();
139     if (aStudy->GetProperties()->IsLocked())
140       return;
141     SMESHGUI::GetSMESHGen()->SetName(theSObject->GetIOR().c_str(), theName.toLatin1().data());
142   }
143
144   void SetValue (_PTR(SObject) theSObject, const QString& theValue)
145   {
146     _PTR(Study) aStudy = GetActiveStudyDocument();
147     if (aStudy->GetProperties()->IsLocked())
148       return;
149     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
150     _PTR(GenericAttribute) anAttr =
151       aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
152     _PTR(AttributeComment) aComment = anAttr;
153     if (aComment)
154       aComment->SetValue(theValue.toLatin1().data());
155   }
156   
157   void setFileName (_PTR(SObject) theSObject, const QString& theValue)
158   {
159     _PTR(Study) aStudy = GetActiveStudyDocument();
160     if (aStudy->GetProperties()->IsLocked())
161       return;
162     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
163     _PTR(GenericAttribute) anAttr =
164       aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
165     _PTR(AttributeExternalFileDef) aFileName = anAttr;
166     if (aFileName)
167       aFileName->SetValue(theValue.toLatin1().data());
168   }
169   
170   void setFileType (_PTR(SObject) theSObject, const QString& theValue)
171   {
172     _PTR(Study) aStudy = GetActiveStudyDocument();
173     if (aStudy->GetProperties()->IsLocked())
174       return;
175     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
176     _PTR(GenericAttribute) anAttr =
177       aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
178     _PTR(AttributeFileType) aFileType = anAttr;
179     if (aFileType)
180       aFileType->SetValue(theValue.toLatin1().data());
181   }
182
183   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
184                                      _PTR(Study)   theStudy)
185   {
186     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
187       (SUIT_Session::session()->activeApplication());
188     if (theSObject) {
189       _PTR(GenericAttribute) anAttr;
190       if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
191         _PTR(AttributeIOR) anIOR = anAttr;
192         CORBA::String_var aVal = anIOR->Value().c_str();
193         // string_to_object() DOC: If the input string is not valid ...
194         // a CORBA::SystemException is thrown.
195         if ( aVal && strlen( aVal ) > 0 )
196           return app->orb()->string_to_object(aVal);
197       }
198     }
199     return CORBA::Object::_nil();
200   }
201
202   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
203   {
204     _PTR(Study) aStudy = GetActiveStudyDocument();
205     return SObjectToObject(theSObject,aStudy);
206   }
207
208   _PTR(SObject) ObjectToSObject( CORBA::Object_ptr theObject )
209   {
210     _PTR(SObject) res;
211     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
212       (SUIT_Session::session()->activeApplication());
213     if ( app ) {
214       QString IOR = app->orb()->object_to_string( theObject );
215       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
216       if ( study && !IOR.isEmpty() )
217         res = study->studyDS()->FindObjectIOR( IOR.toLatin1().constData() );
218     }
219     return res;
220   }
221
222   CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
223   {
224     if (!theIO.IsNull()) {
225       if (theIO->hasEntry()) {
226         _PTR(Study) aStudy = GetActiveStudyDocument();
227         _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
228         return SObjectToObject(anObj,aStudy);
229       }
230     }
231     return CORBA::Object::_nil();
232   }
233
234   CORBA::Object_var IORToObject (const QString& theIOR)
235   {
236     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
237       (SUIT_Session::session()->activeApplication());
238     return app->orb()->string_to_object(theIOR.toLatin1().data());
239   }
240
241   int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
242   {
243     if (!theMgr)
244       return 0;
245
246     SALOME_ListIO selected;
247     theMgr->selectedObjects(selected);
248     int aNbSel = selected.Extent();
249     if (aNbSel == 1) {
250       Handle(SALOME_InteractiveObject) anIObject = selected.First();
251       theName = anIObject->getName();
252     } else {
253       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
254     }
255     return aNbSel;
256   }
257
258   _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
259   {
260     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
261     if (!aShape->_is_nil()){ //It s a shape
262       return theSObject->GetFather();
263     }
264     _PTR(SObject) aSObject;
265     if (theSObject->ReferencedObject(aSObject)) {
266       aSObject = theSObject->GetFather();
267       return aSObject->GetFather();
268     }
269     return theSObject->GetFather();
270   }
271
272   void ModifiedMesh (_PTR(SObject) theSObject, bool theIsNotModif, bool isEmptyMesh)
273   {
274     _PTR(Study) aStudy = GetActiveStudyDocument();
275     if (aStudy->GetProperties()->IsLocked())
276       return;
277
278     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
279     _PTR(GenericAttribute) anAttr =
280       aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
281     _PTR(AttributePixMap) aPixmap = anAttr;
282     if (theIsNotModif) {
283       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
284     } else if ( isEmptyMesh ) {
285       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
286     } else {
287       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_PARTIAL");
288     }
289
290     _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
291     for (int i = 1; anIter->More(); anIter->Next(), i++) {
292       _PTR(SObject) aSObj = anIter->Value();
293       if (i >= 4) {
294         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
295         for ( ; anIter1->More(); anIter1->Next()) {
296           _PTR(SObject) aSObj1 = anIter1->Value();
297           anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
298           aPixmap = anAttr;
299           if (theIsNotModif) {
300             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
301           } else if ( isEmptyMesh ) {
302             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
303           } else {
304             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_PARTIAL");
305           }
306         }
307       }
308     }
309   }
310
311   void ShowHelpFile (const QString& theHelpFileName)
312   {
313     LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
314     if (app) {
315       SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
316       app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
317                                theHelpFileName);
318     }
319     else {
320       SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
321                                QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
322                                arg(app->resourceMgr()->stringValue("ExternalBrowser", 
323                                                                    "application")).
324                                arg(theHelpFileName));
325     }
326   }
327
328   //=======================================================================
329   /**
330      Return normale to a given face
331   */
332   //=======================================================================
333   gp_XYZ getNormale( const SMDS_MeshFace* theFace )
334   {
335     gp_XYZ n;
336     int aNbNode = theFace->NbNodes();
337     TColgp_Array1OfXYZ anArrOfXYZ(1,4);
338     SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
339     int i = 1;
340     for ( ; aNodeItr->more() && i <= 4; i++ ) {
341       SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
342       anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
343     }
344     
345     gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
346     gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
347     n  = q1 ^ q2;
348     if ( aNbNode > 3 ) {
349       gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
350       n += q2 ^ q3;
351     }
352     double len = n.Modulus();
353     if ( len > 0 )
354       n /= len;
355     return n;
356   }
357   
358 } // end of namespace SMESH