1 // Copyright (C) 2007-2019 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 <GEOMImpl_Types.hxx>
32 #include <GEOM_Client.hxx>
33 #include <GEOM_wrap.hxx>
34 #include <SALOME_ListIO.hxx>
35 #include <SMESHGUI_Utils.h>
37 #include <BRepAdaptor_Surface.hxx>
39 #include <TopoDS_Iterator.hxx>
41 #include "utilities.h"
43 #include "SALOME_LifeCycleCORBA.hxx"
50 GeomSelectionTools::GeomSelectionTools()
55 * Allows to get the Salome Application
56 * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
58 SalomeApp_Application* GeomSelectionTools::GetSalomeApplication()
60 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
68 * Allows to get the selection manager from LightApp
69 * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
71 LightApp_SelectionMgr* GeomSelectionTools::selectionMgr()
73 SalomeApp_Application* anApp = GetSalomeApplication();
75 return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
81 * Return the list of the selected Salome Interactive Object (SALOME_ListIO*)
82 * @return the list of the selected Salome Interactive Object
84 SALOME_ListIO* GeomSelectionTools::getSelectedSalomeObjects()
86 SALOME_ListIO* selected = new SALOME_ListIO;
87 LightApp_SelectionMgr* aSel = selectionMgr();
88 aSel->selectedObjects( *selected, NULL, false );
93 * Return the first selected Salome Interactive Object (Handle(Salome_InteractiveObject))
94 * @return the first selected Salome Interactive Object
96 Handle(SALOME_InteractiveObject) GeomSelectionTools::getFirstSelectedSalomeObject()
98 SALOME_ListIO selected;
99 LightApp_SelectionMgr* aSel = selectionMgr();
100 aSel->selectedObjects( selected, NULL, false );
101 if (!selected.IsEmpty()){
102 SALOME_ListIteratorOfListIO anIt(selected);
103 Handle(SALOME_InteractiveObject) anIO;
111 * Return the entry of the first selected Object
112 * @return the entry of the first selected Object
114 std::string GeomSelectionTools::getFirstSelectedEntry()
116 Handle(SALOME_InteractiveObject) anIO;
117 std::string entry="";
118 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
119 return GeomSelectionTools::getEntryOfObject(anIO);
123 * Return the entry of a Salome Interactive Object
124 * @param anIO the Handle of the Salome Interactive Object
125 * @return the entry of the Salome Interactive Object
127 std::string GeomSelectionTools::getEntryOfObject(Handle(SALOME_InteractiveObject) anIO){
128 std::string entry="";
129 _PTR(SObject) aSO = SalomeApp_Application::getStudy()->FindObjectID(anIO->getEntry());
131 _PTR(SObject) aRefSObj;
132 // If selected object is a reference
133 if ( aSO->ReferencedObject( aRefSObj ))
134 entry = aRefSObj->GetID();
135 // If selected object is a reference is not a reference
137 entry= anIO->getEntry();
143 * Retrieve the name from the entry of the object
144 * @param entry the entry of the object
145 * @return the name of the object
147 std::string GeomSelectionTools::getNameFromEntry(std::string entry){
148 std::string name = "";
149 _PTR(SObject) aSO = SalomeApp_Application::getStudy()->FindObjectID(entry);
151 _PTR(SObject) aRefSObj;
152 // If selected object is a reference
153 if ( aSO->ReferencedObject( aRefSObj ))
154 name = aRefSObj->GetName();
155 // If selected object is a reference is not a reference
157 name = aSO->GetName();
164 * Retrieve the component type of the first selected object, it manages successfully references.
165 * @return the component type of the first selected object
167 std::string GeomSelectionTools::getFirstSelectedComponentDataType()
169 Handle(SALOME_InteractiveObject) anIO;
170 std::string DataType="";
171 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
172 _PTR(SObject) aSO = SalomeApp_Application::getStudy()->FindObjectID(anIO->getEntry());
174 _PTR(SObject) aRefSObj;
175 // If selected object is a reference
176 if ( aSO->ReferencedObject( aRefSObj ))
177 DataType= aRefSObj->GetFatherComponent()->ComponentDataType();
178 // If selected object is a reference is not a reference
180 DataType=anIO->getComponentDataType();
186 * Retrieve the shape type from the entry
187 * @return the shape type from the entry, return TopAbs_SHAPE if the object does not define a shape or a group.
189 TopAbs_ShapeEnum GeomSelectionTools::entryToShapeType(std::string entry){
190 // MESSAGE("GeomSelectionTools::entryToShapeType"<<entry );
191 TopoDS_Shape S = TopoDS_Shape();
192 TopAbs_ShapeEnum ShapeType = TopAbs_SHAPE;
193 _PTR(SObject) aSO = SalomeApp_Application::getStudy()->FindObjectID(entry);
195 _PTR(SObject) aRefSObj;
196 GEOM::GEOM_Object_var aShape;
197 // MESSAGE("Got a SO");
198 // If selected object is a reference
199 if ( aSO->ReferencedObject( aRefSObj ))
201 // MESSAGE("aSO->GetFatherComponent()->ComponentDataType(): " << aSO->GetFatherComponent()->ComponentDataType());
202 if ( strcmp(aSO->GetFatherComponent()->ComponentDataType().c_str(),"GEOM") == 0)
203 aShape = SMESH::SObjectToInterface<GEOM::GEOM_Object>(aSO);
204 if ( !aShape->_is_nil() ){
205 // MESSAGE("Got the Geom Object ");
206 // MESSAGE("Geom Object Type "<< aShape->GetType());
207 SalomeApp_Application* anApp = GetSalomeApplication();
209 // MESSAGE("Got Application");
210 Engines::EngineComponent_var component = anApp->lcc()->FindOrLoad_Component( "FactoryServer","GEOM" );
211 GEOM::GEOM_Gen_var _geomEngine = GEOM::GEOM_Gen::_narrow(component);
212 // MESSAGE("Got GEOM engine");
213 // if the Geom Object is a group
214 if (aShape->GetType() == GEOM_GROUP){
215 // MESSAGE("It's a group");
216 GEOM::GEOM_IGroupOperations_wrap aGroupOp =
217 _geomEngine->GetIGroupOperations();
218 ShapeType= (TopAbs_ShapeEnum)aGroupOp->GetType(aShape);
222 GEOM_Client* aClient = new GEOM_Client();
223 if ( aClient && !_geomEngine->_is_nil() ) {
224 // MESSAGE("GEOM client is OK and GEOM engine is not null");
225 S = aClient->GetShape( _geomEngine, aShape );
226 ShapeType=S.ShapeType();
227 if ( ShapeType == TopAbs_COMPOUND )
229 TopoDS_Iterator it( S );
231 ShapeType = it.Value().ShapeType();
238 // MESSAGE("ShapeType returned is " << ShapeType);
243 * Gives the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
244 * @return the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
246 TopAbs_ShapeEnum GeomSelectionTools:: getFirstSelectedShapeType()
248 Handle(SALOME_InteractiveObject) anIO;
249 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
250 return entryToShapeType(anIO->getEntry());
254 * Print information to std output of the face
255 * and return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
256 * @param TopoDS_Shape S Face we want information about.
257 * @return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
258 * return Other_Surface if the selected face is not a face.
259 * Information printed is :
261 * U and V number of poles
262 * U and V number of knots
263 * U or V is Rational ?
266 GeomAbs_SurfaceType GeomSelectionTools::getFaceInformation(TopoDS_Shape S)
268 GeomAbs_SurfaceType surf_type=GeomAbs_OtherSurface ;
269 if (!S.IsNull() && S.ShapeType()==TopAbs_FACE){
270 TopoDS_Face f=TopoDS::Face(S);
271 BRepAdaptor_Surface surf_adap(f);
273 /* Global Information */
274 std::cout << "GLOBAL INFORMATION" << std::endl;
275 std::cout << "******************" << std::endl;
276 std::stringstream buffer;
277 buffer << "Degre U : " << surf_adap.UDegree();
278 //conversion nécessaire pour affichage
279 std::cout << buffer.str() << std::endl;
280 std::cout << " Degre V : " << surf_adap.VDegree() << std::endl;
281 std::cout << " Nb Poles U : " << surf_adap.NbUPoles() << std::endl;
282 std::cout << " Nb Poles V : " << surf_adap.NbVPoles() << std::endl;
283 std::cout << " Nb Noeuds U : " << surf_adap.NbUKnots() << std::endl;
284 std::cout << " Nb Noeuds V : " << surf_adap.NbVKnots() << std::endl;
285 std::cout << " U Rationnel ? " << surf_adap.IsURational() << std::endl;
286 std::cout << " V Rationnel ? " << surf_adap.IsVRational() << std::endl;
288 surf_type=surf_adap.GetType();
294 //////////////////////////////////////////
296 //////////////////////////////////////////
300 QString PluginUtils::PrintDoubleValue( double theValue, int thePrecision )
302 const double prec = 1e-12;
304 if ( qAbs(theValue) < prec )
307 QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( thePrecision ) );
311 while ( p < thePrecision ) {
312 QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( p++ ) );
313 double v = aRes.toDouble();
314 double err = qAbs( theValue - v );
315 if ( err > 0 && err <= prec )
320 // remove trailing zeroes
322 QRegExp expre( QString( "(%1|%2)[+-]?[0-9]+$" ).arg( QLocale().exponential().toLower(),
323 QLocale().exponential().toUpper() ) );
325 int idx = aRes.indexOf( expre );
326 QString aResExp = "";
328 aResExp = aRes.mid( idx );
329 aRes = aRes.left( idx );
332 if ( aRes.contains( QLocale().decimalPoint() ) )
333 aRes.remove( QRegExp( QString( "(\\%1|0)0*$" ).arg( QLocale().decimalPoint() ) ) );
335 return aRes == "-0" ? QString( "0" ) : aRes + aResExp;