Salome HOME
Fix for the issue #3200 : Can't select SHAPERSTUDY groups in Netgen local size
[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     std::string aComponentType = aSO->GetFatherComponent()->ComponentDataType();
203     if (aComponentType == "GEOM" || aComponentType == "SHAPERSTUDY")
204       aShape = SMESH::SObjectToInterface<GEOM::GEOM_Object>(aSO);
205     if ( !aShape->_is_nil() ){
206       // MESSAGE("Got the Geom Object ");
207       // MESSAGE("Geom Object Type "<< aShape->GetType());
208       SalomeApp_Application* anApp = GetSalomeApplication();
209       if (anApp) {
210 //         MESSAGE("Got Application");
211         Engines::EngineComponent_var component = anApp->lcc()->FindOrLoad_Component( "FactoryServer", aComponentType.c_str());
212         GEOM::GEOM_Gen_var _geomEngine = GEOM::GEOM_Gen::_narrow(component);
213 //         MESSAGE("Got GEOM engine");
214         // if the Geom Object is a group
215         if (aShape->GetType() == GEOM_GROUP){
216 //           MESSAGE("It's a group");
217           GEOM::GEOM_IGroupOperations_wrap aGroupOp =
218             _geomEngine->GetIGroupOperations();
219           ShapeType= (TopAbs_ShapeEnum)aGroupOp->GetType(aShape);
220         }
221         // if not
222         else {
223           GEOM_Client* aClient = new GEOM_Client();
224           if ( aClient && !_geomEngine->_is_nil() ) {
225 //             MESSAGE("GEOM client is OK and GEOM engine is not null");
226             S = aClient->GetShape( _geomEngine, aShape );
227             ShapeType=S.ShapeType();
228             if ( ShapeType == TopAbs_COMPOUND )
229             {
230               TopoDS_Iterator it( S );
231               if ( it.More() )
232                 ShapeType = it.Value().ShapeType();
233             }
234           }
235         }
236       }
237     }
238   }
239 //   MESSAGE("ShapeType returned is " << ShapeType);
240   return ShapeType;
241 }
242
243 /*!
244  * Gives the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
245  * @return the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
246  */
247 TopAbs_ShapeEnum GeomSelectionTools:: getFirstSelectedShapeType()
248 {
249  Handle(SALOME_InteractiveObject) anIO;
250  anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
251  return entryToShapeType(anIO->getEntry());
252 }
253
254 /*!
255  *  Print information to std output of the face
256  *  and return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
257  *  @param TopoDS_Shape S Face we want information about.
258  *  @return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
259  *  return Other_Surface if the selected face is not a face.
260  *  Information printed is :
261  *  U and V degrees
262  *  U and V number of poles
263  *  U and V number of knots
264  *  U or V is Rational ?
265  *
266  */
267 GeomAbs_SurfaceType GeomSelectionTools::getFaceInformation(TopoDS_Shape S)
268 {
269   GeomAbs_SurfaceType surf_type=GeomAbs_OtherSurface ;
270   if (!S.IsNull() &&  S.ShapeType()==TopAbs_FACE){
271     TopoDS_Face f=TopoDS::Face(S);
272     BRepAdaptor_Surface surf_adap(f);
273
274     /* Global Information */
275     std::cout << "GLOBAL INFORMATION" << std::endl;
276     std::cout << "******************" << std::endl;
277     std::stringstream buffer;
278     buffer << "Degre U : " <<  surf_adap.UDegree();
279    //conversion nĂ©cessaire pour affichage
280     std::cout << buffer.str() << std::endl;
281     std::cout <<  " Degre V : " <<  surf_adap.VDegree() << std::endl;
282     std::cout <<  " Nb Poles U : " <<  surf_adap.NbUPoles() << std::endl;
283     std::cout <<  " Nb Poles V : " <<  surf_adap.NbVPoles() << std::endl;
284     std::cout <<  " Nb Noeuds U : " <<  surf_adap.NbUKnots() << std::endl;
285     std::cout <<  " Nb Noeuds V : " <<  surf_adap.NbVKnots() << std::endl;
286     std::cout <<  " U Rationnel ? " <<  surf_adap.IsURational() << std::endl;
287     std::cout <<  " V Rationnel ? " <<  surf_adap.IsVRational() << std::endl;
288
289     surf_type=surf_adap.GetType();
290   }
291   return surf_type;
292 }
293
294
295 //////////////////////////////////////////
296 // Utility functions
297 //////////////////////////////////////////
298 #include <QLocale>
299 #include <QRegExp>
300
301 QString PluginUtils::PrintDoubleValue( double theValue, int thePrecision )
302 {
303   const double prec = 1e-12;
304
305   if ( qAbs(theValue) < prec )
306     return "0";
307
308   QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( thePrecision ) );
309
310   if ( prec > 0 ) {
311     int p = 0;
312     while ( p < thePrecision ) {
313       QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( p++ ) );
314       double v = aRes.toDouble();
315       double err = qAbs( theValue - v );
316       if ( err > 0 && err <= prec )
317         break;
318     }
319   }
320
321   // remove trailing zeroes
322
323   QRegExp expre( QString( "(%1|%2)[+-]?[0-9]+$" ).arg( QLocale().exponential().toLower(),
324                                QLocale().exponential().toUpper() ) );
325
326   int idx = aRes.indexOf( expre );
327   QString aResExp = "";
328   if ( idx >= 0 ) {
329     aResExp = aRes.mid( idx );
330     aRes = aRes.left( idx );
331   }
332
333   if ( aRes.contains( QLocale().decimalPoint() ) )
334     aRes.remove( QRegExp( QString( "(\\%1|0)0*$" ).arg( QLocale().decimalPoint() ) ) );
335
336   return aRes == "-0" ? QString( "0" ) : aRes + aResExp;
337 }