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