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