1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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_ListIO.hxx>
32 #include <GEOM_Client.hxx>
33 #include <SMESHGUI_Utils.h>
34 #include <boost/shared_ptr.hpp>
35 #include <GEOMImpl_Types.hxx>
36 #include <GEOM_wrap.hxx>
39 #include <BRep_Tool.hxx>
40 #include <Handle_Geom_Surface.hxx>
41 #include <BRepAdaptor_Surface.hxx>
43 #include "utilities.h"
45 #include "SALOME_LifeCycleCORBA.hxx"
50 * @param aStudy pointer to the Study
53 GeomSelectionTools::GeomSelectionTools(_PTR(Study) aStudy)
59 * Accessor to the Study used by this GeomSelectionTools object
60 * @return The study used by the GeomSelectionTools class
62 _PTR(Study) GeomSelectionTools::getMyStudy()
68 * Allows to get the Salome Application
69 * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
71 SalomeApp_Application* GeomSelectionTools::GetSalomeApplication()
73 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
81 * Allows to get the selection manager from LightApp
82 * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
84 LightApp_SelectionMgr* GeomSelectionTools::selectionMgr()
86 SalomeApp_Application* anApp = GetSalomeApplication();
88 return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
94 * Return the list of the selected Salome Interactive Object (SALOME_ListIO*)
95 * @return the list of the selected Salome Interactive Object
97 SALOME_ListIO* GeomSelectionTools::getSelectedSalomeObjects()
99 SALOME_ListIO* selected = new SALOME_ListIO;
100 LightApp_SelectionMgr* aSel = selectionMgr();
101 aSel->selectedObjects( *selected, NULL, false );
106 * Return the first selected Salome Interactive Object (Handle(Salome_InteractiveObject))
107 * @return the first selected Salome Interactive Object
109 Handle(SALOME_InteractiveObject) GeomSelectionTools::getFirstSelectedSalomeObject()
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;
124 * Return the entry of the first selected Object
125 * @return the entry of the first selected Object
127 std::string GeomSelectionTools::getFirstSelectedEntry()
129 Handle(SALOME_InteractiveObject) anIO;
130 std::string entry="";
131 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
132 return GeomSelectionTools::getEntryOfObject(anIO);
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
140 std::string GeomSelectionTools::getEntryOfObject(Handle(SALOME_InteractiveObject) anIO){
141 std::string entry="";
142 _PTR(SObject) aSO = myStudy->FindObjectID(anIO->getEntry());
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
150 entry= anIO->getEntry();
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
160 std::string GeomSelectionTools::getNameFromEntry(std::string entry){
161 std::string name = "";
162 _PTR(SObject) aSO = myStudy->FindObjectID(entry);
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
170 name = aSO->GetName();
177 * Retrieve the component type of the first selected object, it manages successfully references.
178 * @return the component type of the first selected object
180 std::string GeomSelectionTools::getFirstSelectedComponentDataType()
182 Handle(SALOME_InteractiveObject) anIO;
183 std::string DataType="";
184 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
185 _PTR(SObject) aSO = myStudy->FindObjectID(anIO->getEntry());
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
193 DataType=anIO->getComponentDataType();
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.
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);
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 ))
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();
222 // MESSAGE("Got Application");
223 Engines::EngineComponent_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_wrap aGroupOp =
230 _geomEngine->GetIGroupOperations(myStudy->StudyId());
231 ShapeType= (TopAbs_ShapeEnum)aGroupOp->GetType(aShape);
235 GEOM_Client* aClient = new GEOM_Client();
236 if ( aClient && !_geomEngine->_is_nil() ) {
237 // MESSAGE("GEOM client is OK and GEOM engine is not null");
238 S = aClient->GetShape( _geomEngine, aShape );
239 ShapeType=S.ShapeType();
245 // MESSAGE("ShapeType returned is " << ShapeType);
250 * Gives the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
251 * @return the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
253 TopAbs_ShapeEnum GeomSelectionTools:: getFirstSelectedShapeType()
255 Handle(SALOME_InteractiveObject) anIO;
256 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
257 return entryToShapeType(anIO->getEntry());
261 * Print information to std output of the face
262 * and return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
263 * @param TopoDS_Shape S Face we want information about.
264 * @return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
265 * return Other_Surface if the selected face is not a face.
266 * Information printed is :
268 * U and V number of poles
269 * U and V number of knots
270 * U or V is Rational ?
273 GeomAbs_SurfaceType GeomSelectionTools::getFaceInformation(TopoDS_Shape S)
275 GeomAbs_SurfaceType surf_type=GeomAbs_OtherSurface ;
276 if (!S.IsNull() && S.ShapeType()==TopAbs_FACE){
277 TopoDS_Face f=TopoDS::Face(S);
278 Handle(Geom_Surface) surf = BRep_Tool::Surface(f);
279 BRepAdaptor_Surface surf_adap(f);
281 /* Global Information */
282 std::cout << "GLOBAL INFORMATION" << std::endl;
283 std::cout << "******************" << std::endl;
284 std::stringstream buffer;
285 buffer << "Degre U : " << surf_adap.UDegree();
286 //conversion nécessaire pour affichage
287 std::cout << buffer.str() << std::endl;
288 std::cout << " Degre V : " << surf_adap.VDegree() << std::endl;
289 std::cout << " Nb Poles U : " << surf_adap.NbUPoles() << std::endl;
290 std::cout << " Nb Poles V : " << surf_adap.NbVPoles() << std::endl;
291 std::cout << " Nb Noeuds U : " << surf_adap.NbUKnots() << std::endl;
292 std::cout << " Nb Noeuds V : " << surf_adap.NbVKnots() << std::endl;
293 std::cout << " U Rationnel ? " << surf_adap.IsURational() << std::endl;
294 std::cout << " V Rationnel ? " << surf_adap.IsVRational() << std::endl;
296 surf_type=surf_adap.GetType();
302 //////////////////////////////////////////
304 //////////////////////////////////////////
308 QString PluginUtils::PrintDoubleValue( double theValue, int thePrecision )
310 const double prec = 1e-12;
312 if ( qAbs(theValue) < prec )
315 QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( thePrecision ) );
319 while ( p < thePrecision ) {
320 QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( p++ ) );
321 double v = aRes.toDouble();
322 double err = qAbs( theValue - v );
323 if ( err > 0 && err <= prec )
328 // remove trailing zeroes
330 QRegExp expre( QString( "(%1|%2)[+-]?[0-9]+$" ).arg( QLocale().exponential().toLower(),
331 QLocale().exponential().toUpper() ) );
333 int idx = aRes.indexOf( expre );
334 QString aResExp = "";
336 aResExp = aRes.mid( idx );
337 aRes = aRes.left( idx );
340 if ( aRes.contains( QLocale().decimalPoint() ) )
341 aRes.remove( QRegExp( QString( "(\\%1|0)0*$" ).arg( QLocale().decimalPoint() ) ) );
343 return aRes == "-0" ? QString( "0" ) : aRes + aResExp;