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