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