Salome HOME
PR: merged from V5_1_4rc1
[modules/smesh.git] / src / PluginUtils / GeomSelectionTools.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D
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.
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_ListIteratorOfListIO.hxx>
32 #include <GEOM_Client.hxx>
33 #include <SMESHGUI_Utils.h>
34 #include <boost/shared_ptr.hpp>
35 #include <GEOMImpl_Types.hxx>
36
37 #include <TopoDS.hxx>
38 #include <BRep_Tool.hxx>
39 #include <Handle_Geom_Surface.hxx>
40 #include <BRepAdaptor_Surface.hxx>
41
42 #include "utilities.h"
43
44 #include "SALOME_LifeCycleCORBA.hxx"
45 #include <sstream>
46
47 /*!
48  * Constructor
49  * @param aStudy pointer to the Study
50  *
51  */
52 GeomSelectionTools::GeomSelectionTools(_PTR(Study) aStudy)
53 {
54   myStudy = aStudy;
55 }
56
57 /*!
58  * Accessor to the Study used by this GeomSelectionTools object
59  * @return The study used by the GeomSelectionTools class
60  */
61 _PTR(Study) GeomSelectionTools::getMyStudy()
62 {
63     return myStudy;
64 }
65
66 /*!
67  * Allows to get the Salome Application
68  * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
69  */
70 SalomeApp_Application*  GeomSelectionTools::GetSalomeApplication()
71 {
72   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
73   if (anApp)
74     return anApp;
75   else
76     return 0;
77 }
78
79 /*!
80  * Allows to get the selection manager from LightApp
81  * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
82  */
83 LightApp_SelectionMgr*  GeomSelectionTools::selectionMgr()
84 {
85    SalomeApp_Application* anApp = GetSalomeApplication();
86    if (anApp)
87      return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
88    else
89      return 0;
90 }
91
92 /*!
93  * Return the list of the selected Salome Interactive Object (SALOME_ListIO*)
94  * @return the list of the selected Salome Interactive Object
95  */
96 SALOME_ListIO* GeomSelectionTools::getSelectedSalomeObjects()
97 {
98   SALOME_ListIO* selected;
99   LightApp_SelectionMgr* aSel = selectionMgr();
100   aSel->selectedObjects( *selected, NULL, false );
101   return selected;
102 }
103
104 /*!
105  * Return the first selected Salome Interactive Object (Handle(Salome_InteractiveObject))
106  * @return the first selected Salome Interactive Object
107  */
108 Handle(SALOME_InteractiveObject) GeomSelectionTools::getFirstSelectedSalomeObject()
109 {
110   SALOME_ListIO selected;
111   LightApp_SelectionMgr* aSel = selectionMgr();
112   aSel->selectedObjects( selected, NULL, false );
113   if (!selected.IsEmpty()){
114     SALOME_ListIteratorOfListIO anIt(selected);
115     Handle(SALOME_InteractiveObject) anIO;
116     anIO = anIt.Value();
117     return anIO;
118   }
119   return NULL;
120 }
121
122 /*!
123  * Return the entry of the first selected Object
124  * @return the entry of the first selected Object
125  */
126 std::string GeomSelectionTools::getFirstSelectedEntry()
127 {
128   Handle(SALOME_InteractiveObject) anIO;
129   std::string entry="";
130   anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
131   return GeomSelectionTools::getEntryOfObject(anIO);
132 }
133
134 /*!
135  * Return the entry of a Salome Interactive Object
136  * @param anIO the Handle of the Salome Interactive Object
137  * @return the entry of the Salome Interactive Object
138  */
139 std::string GeomSelectionTools::getEntryOfObject(Handle(SALOME_InteractiveObject) anIO){
140   std::string entry="";
141   _PTR(SObject) aSO = myStudy->FindObjectID(anIO->getEntry());
142   if (aSO){
143     _PTR(SObject) aRefSObj;
144     // If selected object is a reference
145     if ( aSO->ReferencedObject( aRefSObj ))
146       entry = aRefSObj->GetID();
147     // If selected object is a reference is not a reference
148     else
149       entry= anIO->getEntry();
150   }
151   return entry;
152 }
153
154 /*!
155  * Retrieve the name from the entry of the object
156  * @param entry the entry of the object
157  * @return the name of the object
158  */
159 std::string GeomSelectionTools::getNameFromEntry(std::string entry){
160   std::string name = "";
161   _PTR(SObject) aSO = myStudy->FindObjectID(entry);
162   if (aSO){
163     _PTR(SObject) aRefSObj;
164     // If selected object is a reference
165     if ( aSO->ReferencedObject( aRefSObj ))
166       name = aRefSObj->GetName();
167     // If selected object is a reference is not a reference
168     else
169       name = aSO->GetName();
170    }
171   return name;
172 }
173
174
175 /*!
176  * Retrieve the component type of the first selected object, it manages successfully references.
177  * @return the component type of the first selected object
178  */
179 std::string GeomSelectionTools::getFirstSelectedComponentDataType()
180 {
181   Handle(SALOME_InteractiveObject) anIO;
182   std::string DataType="";
183   anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
184   _PTR(SObject) aSO = myStudy->FindObjectID(anIO->getEntry());
185   if (aSO){
186     _PTR(SObject) aRefSObj;
187     // If selected object is a reference
188     if ( aSO->ReferencedObject( aRefSObj ))
189       DataType= aRefSObj->GetFatherComponent()->ComponentDataType();
190     // If selected object is a reference is not a reference
191     else
192       DataType=anIO->getComponentDataType();
193  }
194  return DataType;
195 }
196
197 /*!
198  * Retrieve the shape type from the entry
199  * @return the shape type from the entry, return TopAbs_SHAPE if the object does not define a shape or a group.
200  */
201 TopAbs_ShapeEnum GeomSelectionTools::entryToShapeType(std::string entry){
202 //   MESSAGE("GeomSelectionTools::entryToShapeType"<<entry );
203   TopoDS_Shape S = TopoDS_Shape();
204   TopAbs_ShapeEnum ShapeType = TopAbs_SHAPE;
205    _PTR(SObject) aSO = myStudy->FindObjectID(entry);
206   if (aSO){
207     _PTR(SObject) aRefSObj;
208     GEOM::GEOM_Object_var aShape;
209     // MESSAGE("Got a SO");
210     // If selected object is a reference
211     if ( aSO->ReferencedObject( aRefSObj ))
212       aSO = aRefSObj;
213     // MESSAGE("aSO->GetFatherComponent()->ComponentDataType(): " << aSO->GetFatherComponent()->ComponentDataType());
214     if (  strcmp(aSO->GetFatherComponent()->ComponentDataType().c_str(),"GEOM") == 0)
215       aShape = SMESH::SObjectToInterface<GEOM::GEOM_Object>(aSO);
216     if ( !aShape->_is_nil() ){
217       // MESSAGE("Got the Geom Object ");
218       // MESSAGE("Geom Object Type "<< aShape->GetType());
219       SalomeApp_Application* anApp = GetSalomeApplication();
220       if (anApp) {
221 //         MESSAGE("Got Application");
222         Engines::Component_var component = anApp->lcc()->FindOrLoad_Component( "FactoryServer","GEOM" );
223         GEOM::GEOM_Gen_var _geomEngine = GEOM::GEOM_Gen::_narrow(component);
224 //         MESSAGE("Got GEOM engine");
225         // if the Geom Object is a group
226         if (aShape->GetType() == GEOM_GROUP){
227 //           MESSAGE("It's a group");  
228           GEOM::GEOM_IGroupOperations_var aGroupOp = _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           }
239         }
240       }
241     }
242   }
243 //   MESSAGE("ShapeType returned is " << ShapeType);
244   return ShapeType;
245 }
246
247 /*!
248  * Gives the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
249  * @return the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
250  */
251 TopAbs_ShapeEnum GeomSelectionTools:: getFirstSelectedShapeType()
252 {
253  Handle(SALOME_InteractiveObject) anIO;
254  anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
255  return entryToShapeType(anIO->getEntry());
256 }
257
258 /*!
259  *  Print information to std output of the face
260  *  and return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
261  *  @param TopoDS_Shape S Face we want information about.
262  *  @return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
263  *  return Other_Surface if the selected face is not a face.
264  *  Information printed is :
265  *  U and V degrees
266  *  U and V number of poles
267  *  U and V number of knots
268  *  U or V is Rational ?
269  *
270  */
271 GeomAbs_SurfaceType GeomSelectionTools::getFaceInformation(TopoDS_Shape S)
272 {
273   GeomAbs_SurfaceType surf_type=GeomAbs_OtherSurface ;
274   if (!S.IsNull() &&  S.ShapeType()==TopAbs_FACE){
275     TopoDS_Face f=TopoDS::Face(S);
276     Handle(Geom_Surface) surf = BRep_Tool::Surface(f);
277     BRepAdaptor_Surface surf_adap=BRepAdaptor_Surface::BRepAdaptor_Surface(f);
278
279     /* Global Information */
280     std::cout << "GLOBAL INFORMATION" << std::endl;
281     std::cout << "******************" << std::endl;
282     std::stringstream buffer;
283     buffer << "Degre U : " <<  surf_adap.UDegree();
284    //conversion nĂ©cessaire pour affichage
285     std::cout << buffer.str() << std::endl;
286     std::cout <<  " Degre V : " <<  surf_adap.VDegree() << std::endl;
287     std::cout <<  " Nb Poles U : " <<  surf_adap.NbUPoles() << std::endl;
288     std::cout <<  " Nb Poles V : " <<  surf_adap.NbVPoles() << std::endl;
289     std::cout <<  " Nb Noeuds U : " <<  surf_adap.NbUKnots() << std::endl;
290     std::cout <<  " Nb Noeuds V : " <<  surf_adap.NbVKnots() << std::endl;
291     std::cout <<  " U Rationnel ? " <<  surf_adap.IsURational() << std::endl;
292     std::cout <<  " V Rationnel ? " <<  surf_adap.IsVRational() << std::endl;
293
294     surf_type=surf_adap.GetType();
295   }
296   return surf_type;
297 }
298
299
300
301