Salome HOME
Merge branch 'V8_4_BR'
[modules/smesh.git] / src / PluginUtils / GeomSelectionTools.cxx
1 // Copyright (C) 2007-2016  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  * @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 = new SALOME_ListIO;
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_wrap aGroupOp =
228             _geomEngine->GetIGroupOperations(myStudy->StudyId());
229           ShapeType= (TopAbs_ShapeEnum)aGroupOp->GetType(aShape);
230         }
231         // if not
232         else {
233           GEOM_Client* aClient = new GEOM_Client();
234           if ( aClient && !_geomEngine->_is_nil() ) {
235 //             MESSAGE("GEOM client is OK and GEOM engine is not null");
236             S = aClient->GetShape( _geomEngine, aShape );
237             ShapeType=S.ShapeType();
238             if ( ShapeType == TopAbs_COMPOUND )
239             {
240               TopoDS_Iterator it( S );
241               if ( it.More() )
242                 ShapeType = it.Value().ShapeType();
243             }
244           }
245         }
246       }
247     }
248   }
249 //   MESSAGE("ShapeType returned is " << ShapeType);
250   return ShapeType;
251 }
252
253 /*!
254  * Gives the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
255  * @return the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
256  */
257 TopAbs_ShapeEnum GeomSelectionTools:: getFirstSelectedShapeType()
258 {
259  Handle(SALOME_InteractiveObject) anIO;
260  anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
261  return entryToShapeType(anIO->getEntry());
262 }
263
264 /*!
265  *  Print information to std output of the face
266  *  and return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
267  *  @param TopoDS_Shape S Face we want information about.
268  *  @return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
269  *  return Other_Surface if the selected face is not a face.
270  *  Information printed is :
271  *  U and V degrees
272  *  U and V number of poles
273  *  U and V number of knots
274  *  U or V is Rational ?
275  *
276  */
277 GeomAbs_SurfaceType GeomSelectionTools::getFaceInformation(TopoDS_Shape S)
278 {
279   GeomAbs_SurfaceType surf_type=GeomAbs_OtherSurface ;
280   if (!S.IsNull() &&  S.ShapeType()==TopAbs_FACE){
281     TopoDS_Face f=TopoDS::Face(S);
282     BRepAdaptor_Surface surf_adap(f);
283
284     /* Global Information */
285     std::cout << "GLOBAL INFORMATION" << std::endl;
286     std::cout << "******************" << std::endl;
287     std::stringstream buffer;
288     buffer << "Degre U : " <<  surf_adap.UDegree();
289    //conversion nĂ©cessaire pour affichage
290     std::cout << buffer.str() << std::endl;
291     std::cout <<  " Degre V : " <<  surf_adap.VDegree() << std::endl;
292     std::cout <<  " Nb Poles U : " <<  surf_adap.NbUPoles() << std::endl;
293     std::cout <<  " Nb Poles V : " <<  surf_adap.NbVPoles() << std::endl;
294     std::cout <<  " Nb Noeuds U : " <<  surf_adap.NbUKnots() << std::endl;
295     std::cout <<  " Nb Noeuds V : " <<  surf_adap.NbVKnots() << std::endl;
296     std::cout <<  " U Rationnel ? " <<  surf_adap.IsURational() << std::endl;
297     std::cout <<  " V Rationnel ? " <<  surf_adap.IsVRational() << std::endl;
298
299     surf_type=surf_adap.GetType();
300   }
301   return surf_type;
302 }
303
304
305 //////////////////////////////////////////
306 // Utility functions
307 //////////////////////////////////////////
308 #include <QLocale>
309 #include <QRegExp>
310
311 QString PluginUtils::PrintDoubleValue( double theValue, int thePrecision )
312 {
313   const double prec = 1e-12;
314
315   if ( qAbs(theValue) < prec )
316     return "0";
317
318   QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( thePrecision ) );
319
320   if ( prec > 0 ) {
321     int p = 0;
322     while ( p < thePrecision ) {
323       QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( p++ ) );
324       double v = aRes.toDouble();
325       double err = qAbs( theValue - v );
326       if ( err > 0 && err <= prec )
327         break;
328     }
329   }
330
331   // remove trailing zeroes
332
333   QRegExp expre( QString( "(%1|%2)[+-]?[0-9]+$" ).arg( QLocale().exponential().toLower(),
334                                QLocale().exponential().toUpper() ) );
335
336   int idx = aRes.indexOf( expre );
337   QString aResExp = "";
338   if ( idx >= 0 ) {
339     aResExp = aRes.mid( idx );
340     aRes = aRes.left( idx );
341   }
342
343   if ( aRes.contains( QLocale().decimalPoint() ) )
344     aRes.remove( QRegExp( QString( "(\\%1|0)0*$" ).arg( QLocale().decimalPoint() ) ) );
345
346   return aRes == "-0" ? QString( "0" ) : aRes + aResExp;
347 }