1 // Copyright (C) 2007-2016 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"
48 * @param aStudy pointer to the Study
51 GeomSelectionTools::GeomSelectionTools(_PTR(Study) aStudy)
57 * Accessor to the Study used by this GeomSelectionTools object
58 * @return The study used by the GeomSelectionTools class
60 _PTR(Study) GeomSelectionTools::getMyStudy()
66 * Allows to get the Salome Application
67 * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
69 SalomeApp_Application* GeomSelectionTools::GetSalomeApplication()
71 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
79 * Allows to get the selection manager from LightApp
80 * @return A LightApp_SelectionMgr Pointer or 0 if it can't get it.
82 LightApp_SelectionMgr* GeomSelectionTools::selectionMgr()
84 SalomeApp_Application* anApp = GetSalomeApplication();
86 return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
92 * Return the list of the selected Salome Interactive Object (SALOME_ListIO*)
93 * @return the list of the selected Salome Interactive Object
95 SALOME_ListIO* GeomSelectionTools::getSelectedSalomeObjects()
97 SALOME_ListIO* selected = new SALOME_ListIO;
98 LightApp_SelectionMgr* aSel = selectionMgr();
99 aSel->selectedObjects( *selected, NULL, false );
104 * Return the first selected Salome Interactive Object (Handle(Salome_InteractiveObject))
105 * @return the first selected Salome Interactive Object
107 Handle(SALOME_InteractiveObject) GeomSelectionTools::getFirstSelectedSalomeObject()
109 SALOME_ListIO selected;
110 LightApp_SelectionMgr* aSel = selectionMgr();
111 aSel->selectedObjects( selected, NULL, false );
112 if (!selected.IsEmpty()){
113 SALOME_ListIteratorOfListIO anIt(selected);
114 Handle(SALOME_InteractiveObject) anIO;
122 * Return the entry of the first selected Object
123 * @return the entry of the first selected Object
125 std::string GeomSelectionTools::getFirstSelectedEntry()
127 Handle(SALOME_InteractiveObject) anIO;
128 std::string entry="";
129 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
130 return GeomSelectionTools::getEntryOfObject(anIO);
134 * Return the entry of a Salome Interactive Object
135 * @param anIO the Handle of the Salome Interactive Object
136 * @return the entry of the Salome Interactive Object
138 std::string GeomSelectionTools::getEntryOfObject(Handle(SALOME_InteractiveObject) anIO){
139 std::string entry="";
140 _PTR(SObject) aSO = myStudy->FindObjectID(anIO->getEntry());
142 _PTR(SObject) aRefSObj;
143 // If selected object is a reference
144 if ( aSO->ReferencedObject( aRefSObj ))
145 entry = aRefSObj->GetID();
146 // If selected object is a reference is not a reference
148 entry= anIO->getEntry();
154 * Retrieve the name from the entry of the object
155 * @param entry the entry of the object
156 * @return the name of the object
158 std::string GeomSelectionTools::getNameFromEntry(std::string entry){
159 std::string name = "";
160 _PTR(SObject) aSO = myStudy->FindObjectID(entry);
162 _PTR(SObject) aRefSObj;
163 // If selected object is a reference
164 if ( aSO->ReferencedObject( aRefSObj ))
165 name = aRefSObj->GetName();
166 // If selected object is a reference is not a reference
168 name = aSO->GetName();
175 * Retrieve the component type of the first selected object, it manages successfully references.
176 * @return the component type of the first selected object
178 std::string GeomSelectionTools::getFirstSelectedComponentDataType()
180 Handle(SALOME_InteractiveObject) anIO;
181 std::string DataType="";
182 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
183 _PTR(SObject) aSO = myStudy->FindObjectID(anIO->getEntry());
185 _PTR(SObject) aRefSObj;
186 // If selected object is a reference
187 if ( aSO->ReferencedObject( aRefSObj ))
188 DataType= aRefSObj->GetFatherComponent()->ComponentDataType();
189 // If selected object is a reference is not a reference
191 DataType=anIO->getComponentDataType();
197 * Retrieve the shape type from the entry
198 * @return the shape type from the entry, return TopAbs_SHAPE if the object does not define a shape or a group.
200 TopAbs_ShapeEnum GeomSelectionTools::entryToShapeType(std::string entry){
201 // MESSAGE("GeomSelectionTools::entryToShapeType"<<entry );
202 TopoDS_Shape S = TopoDS_Shape();
203 TopAbs_ShapeEnum ShapeType = TopAbs_SHAPE;
204 _PTR(SObject) aSO = myStudy->FindObjectID(entry);
206 _PTR(SObject) aRefSObj;
207 GEOM::GEOM_Object_var aShape;
208 // MESSAGE("Got a SO");
209 // If selected object is a reference
210 if ( aSO->ReferencedObject( aRefSObj ))
212 // MESSAGE("aSO->GetFatherComponent()->ComponentDataType(): " << aSO->GetFatherComponent()->ComponentDataType());
213 if ( strcmp(aSO->GetFatherComponent()->ComponentDataType().c_str(),"GEOM") == 0)
214 aShape = SMESH::SObjectToInterface<GEOM::GEOM_Object>(aSO);
215 if ( !aShape->_is_nil() ){
216 // MESSAGE("Got the Geom Object ");
217 // MESSAGE("Geom Object Type "<< aShape->GetType());
218 SalomeApp_Application* anApp = GetSalomeApplication();
220 // MESSAGE("Got Application");
221 Engines::EngineComponent_var component = anApp->lcc()->FindOrLoad_Component( "FactoryServer","GEOM" );
222 GEOM::GEOM_Gen_var _geomEngine = GEOM::GEOM_Gen::_narrow(component);
223 // MESSAGE("Got GEOM engine");
224 // if the Geom Object is a group
225 if (aShape->GetType() == GEOM_GROUP){
226 // MESSAGE("It's a group");
227 GEOM::GEOM_IGroupOperations_wrap aGroupOp =
228 _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();
238 if ( ShapeType == TopAbs_COMPOUND )
240 TopoDS_Iterator it( S );
242 ShapeType = it.Value().ShapeType();
249 // MESSAGE("ShapeType returned is " << ShapeType);
254 * Gives the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
255 * @return the ShapeType of the first Selected Object, return TopAbs_SHAPE if the first selected object does not define a shape.
257 TopAbs_ShapeEnum GeomSelectionTools:: getFirstSelectedShapeType()
259 Handle(SALOME_InteractiveObject) anIO;
260 anIO=GeomSelectionTools::getFirstSelectedSalomeObject();
261 return entryToShapeType(anIO->getEntry());
265 * Print information to std output of the face
266 * and return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
267 * @param TopoDS_Shape S Face we want information about.
268 * @return the OCC type of face: Plane, Cylinder,Cone, Sphere, Torus, BezierSurface,BSplineSurface, SurfaceOfRevolution,SurfaceOfExtrusion, OtherSurface
269 * return Other_Surface if the selected face is not a face.
270 * Information printed is :
272 * U and V number of poles
273 * U and V number of knots
274 * U or V is Rational ?
277 GeomAbs_SurfaceType GeomSelectionTools::getFaceInformation(TopoDS_Shape S)
279 GeomAbs_SurfaceType surf_type=GeomAbs_OtherSurface ;
280 if (!S.IsNull() && S.ShapeType()==TopAbs_FACE){
281 TopoDS_Face f=TopoDS::Face(S);
282 BRepAdaptor_Surface surf_adap(f);
284 /* Global Information */
285 std::cout << "GLOBAL INFORMATION" << std::endl;
286 std::cout << "******************" << std::endl;
287 std::stringstream buffer;
288 buffer << "Degre U : " << surf_adap.UDegree();
289 //conversion nécessaire pour affichage
290 std::cout << buffer.str() << std::endl;
291 std::cout << " Degre V : " << surf_adap.VDegree() << std::endl;
292 std::cout << " Nb Poles U : " << surf_adap.NbUPoles() << std::endl;
293 std::cout << " Nb Poles V : " << surf_adap.NbVPoles() << std::endl;
294 std::cout << " Nb Noeuds U : " << surf_adap.NbUKnots() << std::endl;
295 std::cout << " Nb Noeuds V : " << surf_adap.NbVKnots() << std::endl;
296 std::cout << " U Rationnel ? " << surf_adap.IsURational() << std::endl;
297 std::cout << " V Rationnel ? " << surf_adap.IsVRational() << std::endl;
299 surf_type=surf_adap.GetType();
305 //////////////////////////////////////////
307 //////////////////////////////////////////
311 QString PluginUtils::PrintDoubleValue( double theValue, int thePrecision )
313 const double prec = 1e-12;
315 if ( qAbs(theValue) < prec )
318 QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( thePrecision ) );
322 while ( p < thePrecision ) {
323 QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( p++ ) );
324 double v = aRes.toDouble();
325 double err = qAbs( theValue - v );
326 if ( err > 0 && err <= prec )
331 // remove trailing zeroes
333 QRegExp expre( QString( "(%1|%2)[+-]?[0-9]+$" ).arg( QLocale().exponential().toLower(),
334 QLocale().exponential().toUpper() ) );
336 int idx = aRes.indexOf( expre );
337 QString aResExp = "";
339 aResExp = aRes.mid( idx );
340 aRes = aRes.left( idx );
343 if ( aRes.contains( QLocale().decimalPoint() ) )
344 aRes.remove( QRegExp( QString( "(\\%1|0)0*$" ).arg( QLocale().decimalPoint() ) ) );
346 return aRes == "-0" ? QString( "0" ) : aRes + aResExp;