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