1 // Copyright (C) 2007-2020 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>
32 #include <GEOMImpl_Types.hxx>
33 #include <GEOM_Client.hxx>
34 #include <GEOM_wrap.hxx>
35 #include <SALOME_ListIO.hxx>
36 #include <SMESHGUI_Utils.h>
38 #include <BRepAdaptor_Surface.hxx>
40 #include <TopoDS_Iterator.hxx>
42 #include "utilities.h"
44 #include "SALOME_LifeCycleCORBA.hxx"
51 GeomSelectionTools::GeomSelectionTools()
56 * Allows to get the Salome Application
57 * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
59 SalomeApp_Application* GeomSelectionTools::GetSalomeApplication()
61 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
69 * Allows to get the selection manager from LightApp
70 * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
72 LightApp_SelectionMgr* GeomSelectionTools::selectionMgr()
74 SalomeApp_Application* anApp = GetSalomeApplication();
76 return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
82 * Return the list of the selected Salome Interactive Object (SALOME_ListIO*)
83 * @return the list of the selected Salome Interactive Object
85 SALOME_ListIO* GeomSelectionTools::getSelectedSalomeObjects()
87 SALOME_ListIO* selected = new SALOME_ListIO;
88 LightApp_SelectionMgr* aSel = selectionMgr();
89 aSel->selectedObjects( *selected, NULL, false );
94 * Return the first selected Salome Interactive Object (Handle(Salome_InteractiveObject))
95 * @return the first selected Salome Interactive Object
97 Handle(SALOME_InteractiveObject) GeomSelectionTools::getFirstSelectedSalomeObject()
99 SALOME_ListIO selected;
100 LightApp_SelectionMgr* aSel = selectionMgr();
101 aSel->selectedObjects( selected, NULL, false );
102 if (!selected.IsEmpty()){
103 SALOME_ListIteratorOfListIO anIt(selected);
104 Handle(SALOME_InteractiveObject) anIO;
112 * Return the entry of the first selected Object
113 * @return the entry of the first selected Object
115 std::string GeomSelectionTools::getFirstSelectedEntry()
117 Handle(SALOME_InteractiveObject) anIO;
118 std::string entry="";
119 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
120 return GeomSelectionTools::getEntryOfObject(anIO);
124 * Return the entry of a Salome Interactive Object
125 * @param anIO the Handle of the Salome Interactive Object
126 * @return the entry of the Salome Interactive Object
128 std::string GeomSelectionTools::getEntryOfObject(Handle(SALOME_InteractiveObject) anIO){
129 std::string entry="";
130 _PTR(SObject) aSO = SalomeApp_Application::getStudy()->FindObjectID(anIO->getEntry());
132 _PTR(SObject) aRefSObj;
133 // If selected object is a reference
134 if ( aSO->ReferencedObject( aRefSObj ))
135 entry = aRefSObj->GetID();
136 // If selected object is a reference is not a reference
138 entry= anIO->getEntry();
144 * Retrieve the name from the entry of the object
145 * @param entry the entry of the object
146 * @return the name of the object
148 std::string GeomSelectionTools::getNameFromEntry(const std::string& entry)
150 std::string name = "";
151 _PTR(SObject) aSO = SalomeApp_Application::getStudy()->FindObjectID(entry);
153 _PTR(SObject) aRefSObj;
154 // If selected object is a reference
155 if ( aSO->ReferencedObject( aRefSObj ))
156 name = aRefSObj->GetName();
157 // If selected object is a reference is not a reference
159 name = aSO->GetName();
166 * Retrieve the component type of the first selected object, it manages successfully references.
167 * @return the component type of the first selected object
169 std::string GeomSelectionTools::getFirstSelectedComponentDataType()
171 Handle(SALOME_InteractiveObject) anIO;
172 std::string DataType="";
173 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
174 _PTR(SObject) aSO = SalomeApp_Application::getStudy()->FindObjectID(anIO->getEntry());
176 _PTR(SObject) aRefSObj;
177 // If selected object is a reference
178 if ( aSO->ReferencedObject( aRefSObj ))
179 DataType= aRefSObj->GetFatherComponent()->ComponentDataType();
180 // If selected object is a reference is not a reference
182 DataType=anIO->getComponentDataType();
188 * Retrieve the shape type from the entry
189 * @return the shape type from the entry, return TopAbs_SHAPE if the object does not define a shape or a group.
191 TopAbs_ShapeEnum GeomSelectionTools::entryToShapeType(const std::string& entry)
193 TopAbs_ShapeEnum ShapeType = TopAbs_SHAPE;
194 if ( _PTR(SObject) aSO = SalomeApp_Application::getStudy()->FindObjectID(entry))
196 _PTR(SObject) aRefSObj;
197 if ( aSO->ReferencedObject( aRefSObj )) // If selected object is a reference
199 GEOM::GEOM_Object_var aShape = SMESH::SObjectToInterface<GEOM::GEOM_Object>(aSO);
200 if ( !aShape->_is_nil() )
202 ShapeType= (TopAbs_ShapeEnum)aShape->GetShapeType();
203 // if the Geom Object is a group
204 if (aShape->GetType() == GEOM_GROUP)
206 GEOM::GEOM_Gen_var _geomEngine = aShape->GetGen();
207 GEOM::GEOM_IGroupOperations_wrap aGroupOp = _geomEngine->GetIGroupOperations();
208 ShapeType= (TopAbs_ShapeEnum)aGroupOp->GetType(aShape);
211 else if ( ShapeType == TopAbs_COMPOUND )
214 if (GEOMBase::GetShape(aShape, shape))
216 TopoDS_Iterator it( shape );
218 ShapeType = it.Value().ShapeType();
227 * Gives the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
228 * @return the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
230 TopAbs_ShapeEnum GeomSelectionTools:: getFirstSelectedShapeType()
232 Handle(SALOME_InteractiveObject) anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
233 return entryToShapeType(anIO->getEntry());
237 * Print information to std output of the face
238 * and return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
239 * @param TopoDS_Shape S Face we want information about.
240 * @return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
241 * return Other_Surface if the selected face is not a face.
242 * Information printed is :
244 * U and V number of poles
245 * U and V number of knots
246 * U or V is Rational ?
249 GeomAbs_SurfaceType GeomSelectionTools::getFaceInformation(TopoDS_Shape S)
251 GeomAbs_SurfaceType surf_type=GeomAbs_OtherSurface ;
252 if (!S.IsNull() && S.ShapeType()==TopAbs_FACE){
253 TopoDS_Face f=TopoDS::Face(S);
254 BRepAdaptor_Surface surf_adap(f);
256 /* Global Information */
257 std::cout << "GLOBAL INFORMATION" << std::endl;
258 std::cout << "******************" << std::endl;
259 std::stringstream buffer;
260 buffer << "Degre U : " << surf_adap.UDegree();
261 //conversion nécessaire pour affichage
262 std::cout << buffer.str() << std::endl;
263 std::cout << " Degre V : " << surf_adap.VDegree() << std::endl;
264 std::cout << " Nb Poles U : " << surf_adap.NbUPoles() << std::endl;
265 std::cout << " Nb Poles V : " << surf_adap.NbVPoles() << std::endl;
266 std::cout << " Nb Noeuds U : " << surf_adap.NbUKnots() << std::endl;
267 std::cout << " Nb Noeuds V : " << surf_adap.NbVKnots() << std::endl;
268 std::cout << " U Rationnel ? " << surf_adap.IsURational() << std::endl;
269 std::cout << " V Rationnel ? " << surf_adap.IsVRational() << std::endl;
271 surf_type=surf_adap.GetType();
277 //////////////////////////////////////////
279 //////////////////////////////////////////
283 QString PluginUtils::PrintDoubleValue( double theValue, int thePrecision )
285 const double prec = 1e-12;
287 if ( qAbs(theValue) < prec )
290 QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( thePrecision ) );
294 while ( p < thePrecision ) {
295 QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( p++ ) );
296 double v = aRes.toDouble();
297 double err = qAbs( theValue - v );
298 if ( err > 0 && err <= prec )
303 // remove trailing zeroes
305 QRegExp expre( QString( "(%1|%2)[+-]?[0-9]+$" ).arg( QLocale().exponential().toLower(),
306 QLocale().exponential().toUpper() ) );
308 int idx = aRes.indexOf( expre );
309 QString aResExp = "";
311 aResExp = aRes.mid( idx );
312 aRes = aRes.left( idx );
315 if ( aRes.contains( QLocale().decimalPoint() ) )
316 aRes.remove( QRegExp( QString( "(\\%1|0)0*$" ).arg( QLocale().decimalPoint() ) ) );
318 return aRes == "-0" ? QString( "0" ) : aRes + aResExp;