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