Salome HOME
5e93f1b31c2ce10f8fa796c570930abc7282378e
[modules/smesh.git] / src / PluginUtils / GeomSelectionTools.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18
19 // ---
20 // File    : GeomSelectionTools.cxx
21 // Authors : Nicolas GEIMER (OCC)
22 // ---
23
24 #include "GeomSelectionTools.h"
25
26 #include <LightApp_SelectionMgr.h>
27 #include <SalomeApp_Application.h>
28 #include <SUIT_Session.h>
29
30 #include <SALOME_ListIteratorOfListIO.hxx>
31 #include <GEOM_Client.hxx>
32 #include <SMESHGUI_Utils.h>
33 #include <boost/shared_ptr.hpp>
34 #include <GEOMImpl_Types.hxx>
35
36 #include <TopoDS.hxx>
37 #include <BRep_Tool.hxx>
38 #include <Handle_Geom_Surface.hxx>
39 #include <BRepAdaptor_Surface.hxx>
40
41 #include "utilities.h"
42
43 #include "SALOME_LifeCycleCORBA.hxx"
44 #include <sstream>
45
46 /*!
47  * Constructor
48  * @param aStudy pointer to the Study
49  *
50  */
51 GeomSelectionTools::GeomSelectionTools(_PTR(Study) aStudy)
52 {
53   myStudy = aStudy;
54 }
55
56 /*!
57  * Accessor to the Study used by this GeomSelectionTools object
58  * @return The study used by the GeomSelectionTools class
59  */
60 _PTR(Study) GeomSelectionTools::getMyStudy()
61 {
62     return myStudy;
63 }
64
65 /*!
66  * Allows to get the Salome Application
67  * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
68  */
69 SalomeApp_Application*  GeomSelectionTools::GetSalomeApplication()
70 {
71   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
72   if (anApp)
73     return anApp;
74   else
75     return 0;
76 }
77
78 /*!
79  * Allows to get the selection manager from LightApp
80  * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
81  */
82 LightApp_SelectionMgr*  GeomSelectionTools::selectionMgr()
83 {
84    SalomeApp_Application* anApp = GetSalomeApplication();
85    if (anApp)
86      return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
87    else
88      return 0;
89 }
90
91 /*!
92  * Return the list of the selected Salome Interactive Object (SALOME_ListIO*)
93  * @return the list of the selected Salome Interactive Object
94  */
95 SALOME_ListIO* GeomSelectionTools::getSelectedSalomeObjects()
96 {
97   SALOME_ListIO* selected;
98   LightApp_SelectionMgr* aSel = selectionMgr();
99   aSel->selectedObjects( *selected, NULL, false );
100   return selected;
101 }
102
103 /*!
104  * Return the first selected Salome Interactive Object (Handle(Salome_InteractiveObject))
105  * @return the first selected Salome Interactive Object
106  */
107 Handle(SALOME_InteractiveObject) GeomSelectionTools::getFirstSelectedSalomeObject()
108 {
109   SALOME_ListIO selected;
110   LightApp_SelectionMgr* aSel = selectionMgr();
111   aSel->selectedObjects( selected, NULL, false );
112   if (!selected.IsEmpty()){
113     SALOME_ListIteratorOfListIO anIt(selected);
114     Handle(SALOME_InteractiveObject) anIO;
115     anIO = anIt.Value();
116     return anIO;
117   }
118   return NULL;
119 }
120
121 /*!
122  * Return the entry of the first selected Object
123  * @return the entry of the first selected Object
124  */
125 std::string GeomSelectionTools::getFirstSelectedEntry()
126 {
127   Handle(SALOME_InteractiveObject) anIO;
128   std::string entry="";
129   anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
130   return GeomSelectionTools::getEntryOfObject(anIO);
131 }
132
133 /*!
134  * Return the entry of a Salome Interactive Object
135  * @param anIO the Handle of the Salome Interactive Object
136  * @return the entry of the Salome Interactive Object
137  */
138 std::string GeomSelectionTools::getEntryOfObject(Handle(SALOME_InteractiveObject) anIO){
139   std::string entry="";
140   _PTR(SObject) aSO = myStudy->FindObjectID(anIO->getEntry());
141   if (aSO){
142     _PTR(SObject) aRefSObj;
143     // If selected object is a reference
144     if ( aSO->ReferencedObject( aRefSObj ))
145       entry = aRefSObj->GetID();
146     // If selected object is a reference is not a reference
147     else
148       entry= anIO->getEntry();
149   }
150   return entry;
151 }
152
153 /*!
154  * Retrieve the name from the entry of the object
155  * @param entry the entry of the object
156  * @return the name of the object
157  */
158 std::string GeomSelectionTools::getNameFromEntry(std::string entry){
159   std::string name = "";
160   _PTR(SObject) aSO = myStudy->FindObjectID(entry);
161   if (aSO){
162     _PTR(SObject) aRefSObj;
163     // If selected object is a reference
164     if ( aSO->ReferencedObject( aRefSObj ))
165       name = aRefSObj->GetName();
166     // If selected object is a reference is not a reference
167     else
168       name = aSO->GetName();
169    }
170   return name;
171 }
172
173
174 /*!
175  * Retrieve the component type of the first selected object, it manages successfully references.
176  * @return the component type of the first selected object
177  */
178 std::string GeomSelectionTools::getFirstSelectedComponentDataType()
179 {
180   Handle(SALOME_InteractiveObject) anIO;
181   std::string DataType="";
182   anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
183   _PTR(SObject) aSO = myStudy->FindObjectID(anIO->getEntry());
184   if (aSO){
185     _PTR(SObject) aRefSObj;
186     // If selected object is a reference
187     if ( aSO->ReferencedObject( aRefSObj ))
188       DataType= aRefSObj->GetFatherComponent()->ComponentDataType();
189     // If selected object is a reference is not a reference
190     else
191       DataType=anIO->getComponentDataType();
192  }
193  return DataType;
194 }
195
196 /*!
197  * Retrieve the shape type from the entry
198  * @return the shape type from the entry, return TopAbs_SHAPE if the object does not define a shape or a group.
199  */
200 TopAbs_ShapeEnum GeomSelectionTools::entryToShapeType(std::string entry){
201 //   MESSAGE("GeomSelectionTools::entryToShapeType"<<entry );
202   TopoDS_Shape S = TopoDS_Shape();
203   TopAbs_ShapeEnum ShapeType = TopAbs_SHAPE;
204    _PTR(SObject) aSO = myStudy->FindObjectID(entry);
205   if (aSO){
206     _PTR(SObject) aRefSObj;
207     GEOM::GEOM_Object_var aShape;
208     // MESSAGE("Got a SO");
209     // If selected object is a reference
210     if ( aSO->ReferencedObject( aRefSObj ))
211       aSO = aRefSObj;
212     // MESSAGE("aSO->GetFatherComponent()->ComponentDataType(): " << aSO->GetFatherComponent()->ComponentDataType());
213     if (  strcmp(aSO->GetFatherComponent()->ComponentDataType().c_str(),"GEOM") == 0)
214       aShape = SMESH::SObjectToInterface<GEOM::GEOM_Object>(aSO);
215     if ( !aShape->_is_nil() ){
216       // MESSAGE("Got the Geom Object ");
217       // MESSAGE("Geom Object Type "<< aShape->GetType());
218       SalomeApp_Application* anApp = GetSalomeApplication();
219       if (anApp) {
220 //         MESSAGE("Got Application");
221         Engines::EngineComponent_var component = anApp->lcc()->FindOrLoad_Component( "FactoryServer","GEOM" );
222         GEOM::GEOM_Gen_var _geomEngine = GEOM::GEOM_Gen::_narrow(component);
223 //         MESSAGE("Got GEOM engine");
224         // if the Geom Object is a group
225         if (aShape->GetType() == GEOM_GROUP){
226 //           MESSAGE("It's a group");
227           GEOM::GEOM_IGroupOperations_var aGroupOp = _geomEngine->GetIGroupOperations(myStudy->StudyId());
228           ShapeType= (TopAbs_ShapeEnum)aGroupOp->GetType(aShape);
229         }
230         // if not
231         else {
232           GEOM_Client* aClient = new GEOM_Client();
233           if ( aClient && !_geomEngine->_is_nil() ) {
234 //             MESSAGE("GEOM client is OK and GEOM engine is not null");
235             S = aClient->GetShape( _geomEngine, aShape );
236             ShapeType=S.ShapeType();
237           }
238         }
239       }
240     }
241   }
242 //   MESSAGE("ShapeType returned is " << ShapeType);
243   return ShapeType;
244 }
245
246 /*!
247  * Gives the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
248  * @return the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
249  */
250 TopAbs_ShapeEnum GeomSelectionTools:: getFirstSelectedShapeType()
251 {
252  Handle(SALOME_InteractiveObject) anIO;
253  anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
254  return entryToShapeType(anIO->getEntry());
255 }
256
257 /*!
258  *  Print information to std output of the face
259  *  and return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
260  *  @param TopoDS_Shape S Face we want information about.
261  *  @return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
262  *  return Other_Surface if the selected face is not a face.
263  *  Information printed is :
264  *  U and V degrees
265  *  U and V number of poles
266  *  U and V number of knots
267  *  U or V is Rational ?
268  *
269  */
270 GeomAbs_SurfaceType GeomSelectionTools::getFaceInformation(TopoDS_Shape S)
271 {
272   GeomAbs_SurfaceType surf_type=GeomAbs_OtherSurface ;
273   if (!S.IsNull() &&  S.ShapeType()==TopAbs_FACE){
274     TopoDS_Face f=TopoDS::Face(S);
275     Handle(Geom_Surface) surf = BRep_Tool::Surface(f);
276     BRepAdaptor_Surface surf_adap(f);
277
278     /* Global Information */
279     std::cout << "GLOBAL INFORMATION" << std::endl;
280     std::cout << "******************" << std::endl;
281     std::stringstream buffer;
282     buffer << "Degre U : " <<  surf_adap.UDegree();
283    //conversion nĂ©cessaire pour affichage
284     std::cout << buffer.str() << std::endl;
285     std::cout <<  " Degre V : " <<  surf_adap.VDegree() << std::endl;
286     std::cout <<  " Nb Poles U : " <<  surf_adap.NbUPoles() << std::endl;
287     std::cout <<  " Nb Poles V : " <<  surf_adap.NbVPoles() << std::endl;
288     std::cout <<  " Nb Noeuds U : " <<  surf_adap.NbUKnots() << std::endl;
289     std::cout <<  " Nb Noeuds V : " <<  surf_adap.NbVKnots() << std::endl;
290     std::cout <<  " U Rationnel ? " <<  surf_adap.IsURational() << std::endl;
291     std::cout <<  " V Rationnel ? " <<  surf_adap.IsVRational() << std::endl;
292
293     surf_type=surf_adap.GetType();
294   }
295   return surf_type;
296 }
297
298
299 //////////////////////////////////////////
300 // Utility functions
301 //////////////////////////////////////////
302 #include <QLocale>
303 #include <QRegExp>
304
305 QString PluginUtils::PrintDoubleValue( double theValue, int thePrecision )
306 {
307   const double prec = 1e-12;
308
309   if ( qAbs(theValue) < prec )
310     return "0";
311
312   QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( thePrecision ) );
313
314   if ( prec > 0 ) {
315     int p = 0;
316     while ( p < thePrecision ) {
317       QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( p++ ) );
318       double v = aRes.toDouble();
319       double err = qAbs( theValue - v );
320       if ( err > 0 && err <= prec )
321         break;
322     }
323   }
324
325   // remove trailing zeroes
326
327   QRegExp expre( QString( "(%1|%2)[+-]?[0-9]+$" ).arg( QLocale().exponential().toLower(),
328                                QLocale().exponential().toUpper() ) );
329
330   int idx = aRes.indexOf( expre );
331   QString aResExp = "";
332   if ( idx >= 0 ) {
333     aResExp = aRes.mid( idx );
334     aRes = aRes.left( idx );
335   }
336
337   if ( aRes.contains( QLocale().decimalPoint() ) )
338     aRes.remove( QRegExp( QString( "(\\%1|0)0*$" ).arg( QLocale().decimalPoint() ) ) );
339
340   return aRes == "-0" ? QString( "0" ) : aRes + aResExp;
341 }