1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File : GeomSelectionTools.cxx
22 // Authors : Nicolas GEIMER (OCC)
25 #include "GeomSelectionTools.h"
27 #include <LightApp_SelectionMgr.h>
28 #include <SalomeApp_Application.h>
29 #include <SUIT_Session.h>
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>
38 #include <BRep_Tool.hxx>
39 #include <Handle_Geom_Surface.hxx>
40 #include <BRepAdaptor_Surface.hxx>
42 #include "utilities.h"
44 #include "SALOME_LifeCycleCORBA.hxx"
49 * @param aStudy pointer to the Study
52 GeomSelectionTools::GeomSelectionTools(_PTR(Study) aStudy)
58 * Accessor to the Study used by this GeomSelectionTools object
59 * @return The study used by the GeomSelectionTools class
61 _PTR(Study) GeomSelectionTools::getMyStudy()
67 * Allows to get the Salome Application
68 * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
70 SalomeApp_Application* GeomSelectionTools::GetSalomeApplication()
72 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
80 * Allows to get the selection manager from LightApp
81 * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
83 LightApp_SelectionMgr* GeomSelectionTools::selectionMgr()
85 SalomeApp_Application* anApp = GetSalomeApplication();
87 return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
93 * Return the list of the selected Salome Interactive Object (SALOME_ListIO*)
94 * @return the list of the selected Salome Interactive Object
96 SALOME_ListIO* GeomSelectionTools::getSelectedSalomeObjects()
98 SALOME_ListIO* selected;
99 LightApp_SelectionMgr* aSel = selectionMgr();
100 aSel->selectedObjects( *selected, NULL, false );
105 * Return the first selected Salome Interactive Object (Handle(Salome_InteractiveObject))
106 * @return the first selected Salome Interactive Object
108 Handle(SALOME_InteractiveObject) GeomSelectionTools::getFirstSelectedSalomeObject()
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;
123 * Return the entry of the first selected Object
124 * @return the entry of the first selected Object
126 std::string GeomSelectionTools::getFirstSelectedEntry()
128 Handle(SALOME_InteractiveObject) anIO;
129 std::string entry="";
130 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
131 return GeomSelectionTools::getEntryOfObject(anIO);
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
139 std::string GeomSelectionTools::getEntryOfObject(Handle(SALOME_InteractiveObject) anIO){
140 std::string entry="";
141 _PTR(SObject) aSO = myStudy->FindObjectID(anIO->getEntry());
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
149 entry= anIO->getEntry();
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
159 std::string GeomSelectionTools::getNameFromEntry(std::string entry){
160 std::string name = "";
161 _PTR(SObject) aSO = myStudy->FindObjectID(entry);
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
169 name = aSO->GetName();
176 * Retrieve the component type of the first selected object, it manages successfully references.
177 * @return the component type of the first selected object
179 std::string GeomSelectionTools::getFirstSelectedComponentDataType()
181 Handle(SALOME_InteractiveObject) anIO;
182 std::string DataType="";
183 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
184 _PTR(SObject) aSO = myStudy->FindObjectID(anIO->getEntry());
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
192 DataType=anIO->getComponentDataType();
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.
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);
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 ))
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();
221 // MESSAGE("Got Application");
222 Engines::EngineComponent_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);
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();
243 // MESSAGE("ShapeType returned is " << ShapeType);
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.
251 TopAbs_ShapeEnum GeomSelectionTools:: getFirstSelectedShapeType()
253 Handle(SALOME_InteractiveObject) anIO;
254 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
255 return entryToShapeType(anIO->getEntry());
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 :
266 * U and V number of poles
267 * U and V number of knots
268 * U or V is Rational ?
271 GeomAbs_SurfaceType GeomSelectionTools::getFaceInformation(TopoDS_Shape S)
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(f);
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;
294 surf_type=surf_adap.GetType();
300 //////////////////////////////////////////
302 //////////////////////////////////////////
306 QString PluginUtils::PrintDoubleValue( double theValue, int thePrecision )
308 const double prec = 1e-12;
310 if ( qAbs(theValue) < prec )
313 QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( thePrecision ) );
317 while ( p < thePrecision ) {
318 QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( p++ ) );
319 double v = aRes.toDouble();
320 double err = qAbs( theValue - v );
321 if ( err > 0 && err <= prec )
326 // remove trailing zeroes
328 QRegExp expre( QString( "(%1|%2)[+-]?[0-9]+$" ).arg( QLocale().exponential().toLower(),
329 QLocale().exponential().toUpper() ) );
331 int idx = aRes.indexOf( expre );
332 QString aResExp = "";
334 aResExp = aRes.mid( idx );
335 aRes = aRes.left( idx );
338 if ( aRes.contains( QLocale().decimalPoint() ) )
339 aRes.remove( QRegExp( QString( "(\\%1|0)0*$" ).arg( QLocale().decimalPoint() ) ) );
341 return aRes == "-0" ? QString( "0" ) : aRes + aResExp;