Back to index

salome-geom  6.5.0
GEOMBase.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // GEOM GEOMGUI : GUI for Geometry component
00024 // File   : GEOMBase.cxx
00025 // Author : Damien COQUERET, Open CASCADE S.A.S.
00026 //
00027 #include "GEOMBase.h"
00028 
00029 #include <GeometryGUI.h>
00030 #include <GEOM_Client.hxx>
00031 
00033 #include <GEOM_Actor.h>
00034 #include <SVTK_ViewModel.h>
00035 #include <SVTK_ViewWindow.h>
00036 #include <OCCViewer_ViewPort3d.h>
00037 #include <OCCViewer_ViewModel.h>
00038 #include <OCCViewer_ViewWindow.h>
00039 #include <SOCC_ViewModel.h>
00040 #include <SOCC_Prs.h>
00041 
00042 #include <SALOME_ListIO.hxx>
00043 #include <SALOME_ListIteratorOfListIO.hxx>
00044 
00045 #include <SUIT_Desktop.h>
00046 #include <SUIT_Session.h>
00047 #include <SUIT_ViewManager.h>
00048 #include <SUIT_ViewWindow.h>
00049 #include <SUIT_MessageBox.h>
00050 #include <SalomeApp_Application.h>
00051 #include <SalomeApp_Study.h>
00052 
00053 // // Open CASCADE Includes
00054 #include <BRep_Tool.hxx>
00055 #include <BRepAdaptor_Curve.hxx>
00056 #include <BRepAdaptor_Surface.hxx>
00057 #include <BRepPrimAPI_MakeCone.hxx>
00058 
00059 #include <AIS_ListIteratorOfListOfInteractive.hxx>
00060 #include <AIS_ListOfInteractive.hxx>
00061 
00062 #include <TopAbs.hxx>
00063 #include <TopExp.hxx>
00064 #include <TopTools_IndexedMapOfShape.hxx>
00065 #include <TopoDS.hxx>
00066 
00067 #include <Precision.hxx>
00068 
00069 #include <vtkRenderer.h>
00070 
00071 #include <set>
00072 
00073 //=====================================================================================
00074 // function : GetShapeFromIOR()
00075 // purpose  : Get shape data by the specified IOR
00076 //=====================================================================================
00077 TopoDS_Shape GEOMBase::GetShapeFromIOR( const QString& IOR )
00078 {
00079   GEOM::GEOM_Object_var geomObj = GEOMBase::GetObjectFromIOR( IOR );
00080   TopoDS_Shape shape;
00081   GetShape( geomObj, shape, TopAbs_SHAPE );
00082   return shape;
00083 }
00084 
00085 
00086 //=====================================================================================
00087 // function : GetIndex()
00088 // purpose  : Get the index of a sub-shape in a main shape : index start at 1
00089 //=====================================================================================
00090 int GEOMBase::GetIndex( const TopoDS_Shape& subshape, const TopoDS_Shape& shape )
00091 {
00092   int idx = -1;
00093   if ( !shape.IsNull() && !subshape.IsNull() ) {
00094     TopTools_IndexedMapOfShape anIndices;
00095     TopExp::MapShapes( shape, anIndices );
00096     if ( anIndices.Contains( subshape ) ) 
00097       idx = anIndices.FindIndex( subshape );
00098   }
00099   return idx;
00100 }
00101 
00102 
00103 //=======================================================================
00104 // function : GetTopoFromSelection()
00105 // purpose  : Define tds from a single selection and retuen true
00106 //=======================================================================
00107 TopoDS_Shape GEOMBase::GetTopoFromSelection( const SALOME_ListIO& IObjects )
00108 {
00109   TopoDS_Shape shape;
00110   if ( IObjects.Extent() == 1 ){
00111     Handle(SALOME_InteractiveObject) IO = IObjects.First();
00112     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00113     if ( IO->hasEntry() && study ) {
00114       _PTR(Study) studyDS = study->studyDS();
00115       _PTR(SObject) obj( studyDS->FindObjectID( IO->getEntry() ) );
00116       _PTR(GenericAttribute) anAttr;
00117       if ( obj && obj->FindAttribute( anAttr, "AttributeIOR" ) ) {
00118        _PTR(AttributeIOR) anIOR( anAttr );
00119        shape = GetShapeFromIOR( anIOR->Value().c_str() );
00120       }
00121     }
00122   }
00123   return shape;
00124 }
00125 
00126 //=======================================================================
00127 // function : GetNameOfSelectedIObjects()
00128 // purpose  : Define the name geom++ or other name of mono or multi sel.
00129 //=======================================================================
00130 int GEOMBase::GetNameOfSelectedIObjects( const SALOME_ListIO& IObjects,
00131                                          QString&             name,
00132                                          const bool           shapesOnly )
00133 {
00134   int nbSel = 0;
00135   name = ""; // clear output name
00136 
00137   if ( !shapesOnly ) {
00138     nbSel = IObjects.Extent();
00139     if ( nbSel == 1 ) {
00140       Handle(SALOME_InteractiveObject) anIObj = IObjects.First();
00141       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00142       if ( anIObj->hasEntry() && study ) {
00143        _PTR(Study) studyDS = study->studyDS();
00144        _PTR(SObject) obj( studyDS->FindObjectID( anIObj->getEntry() ) );
00145        _PTR(GenericAttribute) anAttr;
00146        if ( obj && obj->FindAttribute( anAttr, "AttributeName" ) ) {
00147          _PTR(AttributeName) aNameAttr ( anAttr );
00148          name = aNameAttr->Value().c_str();
00149        }
00150       }
00151     }
00152   }
00153   else {
00154     GEOM::ListOfGO anObjs;
00155     ConvertListOfIOInListOfGO( IObjects, anObjs, shapesOnly );
00156     nbSel = anObjs.length();
00157     if ( nbSel == 1 )
00158       name = GetName( anObjs[ 0 ] );
00159   }
00160 
00161   if ( nbSel > 1 )
00162     name = QObject::tr( "%1_objects" ).arg( nbSel );
00163 
00164   return nbSel;
00165 }
00166 
00167 
00168 //=================================================================================
00169 // function : GetShapeTypeString()
00170 // purpose  : for a single shape
00171 //=================================================================================
00172 QString GEOMBase::GetShapeTypeString(const TopoDS_Shape& shape)
00173 {
00174   QString aTypeString;
00175   if ( !shape.IsNull() ) {
00176     switch ( shape.ShapeType() ) {
00177     case TopAbs_COMPOUND:
00178       {
00179        aTypeString = QObject::tr( "GEOM_COMPOUND" );
00180        break;
00181       }
00182     case TopAbs_COMPSOLID:
00183       {
00184        aTypeString = QObject::tr( "GEOM_COMPOUNDSOLID" );
00185        break;
00186       }
00187     case TopAbs_SOLID:
00188       {
00189        aTypeString = QObject::tr( "GEOM_SOLID" );
00190        break;
00191       }
00192     case TopAbs_SHELL:
00193       {
00194        aTypeString = QObject::tr( "GEOM_SHELL" );
00195        break;
00196       }
00197     case TopAbs_FACE:
00198       {
00199         BRepAdaptor_Surface surf( TopoDS::Face( shape ) );
00200        switch ( surf.GetType() ) {
00201        case GeomAbs_Plane:
00202          {
00203            aTypeString = QObject::tr( "GEOM_PLANE" );
00204            break;
00205          }
00206        case GeomAbs_Cylinder:
00207          {
00208            aTypeString = QObject::tr( "GEOM_SURFCYLINDER" );
00209            break;
00210          }
00211        case GeomAbs_Sphere:
00212          {
00213            aTypeString = QObject::tr( "GEOM_SURFSPHERE" );
00214            break;
00215          }
00216        case GeomAbs_Torus:
00217          {
00218            aTypeString = QObject::tr( "GEOM_SURFTORUS" );
00219            break;
00220          }
00221        case GeomAbs_Cone:
00222          {
00223            aTypeString = QObject::tr( "GEOM_SURFCONE" );
00224            break;
00225          }
00226        default:
00227          {
00228            aTypeString = QObject::tr( "GEOM_FACE" );
00229            break;
00230          }
00231        }
00232        break;
00233       }
00234     case TopAbs_WIRE:
00235       {
00236         aTypeString = QObject::tr( "GEOM_WIRE" );
00237        break;
00238       }
00239     case TopAbs_EDGE:
00240       {
00241         BRepAdaptor_Curve curv( TopoDS::Edge( shape ) );
00242        switch ( curv.GetType() ) {
00243        case GeomAbs_Line:
00244          {
00245            aTypeString = ( qAbs( curv.FirstParameter() ) >= 1E6 || qAbs( curv.LastParameter() ) >= 1E6 ) ?
00246              QObject::tr( "GEOM_LINE" ) : QObject::tr( "GEOM_EDGE" );
00247            break;
00248          }
00249        case GeomAbs_Circle:
00250          {
00251            aTypeString = curv.IsClosed() ? QObject::tr( "GEOM_CIRCLE" ) : QObject::tr( "GEOM_ARC" );
00252            break;
00253          }
00254        default:
00255          {
00256            aTypeString = QObject::tr( "GEOM_EDGE" );
00257            break;
00258          }
00259        }
00260        break;
00261       }
00262     case TopAbs_VERTEX:
00263       {
00264         aTypeString = QObject::tr( "GEOM_VERTEX" );
00265         break;
00266       }
00267     case TopAbs_SHAPE:
00268       {
00269         aTypeString = QObject::tr( "GEOM_SHAPE" );
00270         break;
00271       }
00272     default:
00273       {
00274        break;
00275       }
00276     }
00277   }
00278   return aTypeString;
00279 }
00280 
00281 
00282 //=======================================================================
00283 // function : ConvertIORinGEOMAISShape()
00284 // purpose  :
00285 //=======================================================================
00286 Handle(GEOM_AISShape) GEOMBase::ConvertIORinGEOMAISShape(const QString& IOR, bool onlyInActiveView)
00287 {
00288   Handle(GEOM_AISShape) shape;
00289 
00290   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00291   if ( study ) {
00292     _PTR(Study) studyDS = study->studyDS();
00293     _PTR(SObject) obj( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
00294     if ( obj ) {
00295       QList<SUIT_ViewWindow*> views;
00296       if ( onlyInActiveView ) 
00297        views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
00298       else 
00299        views = SUIT_Session::session()->activeApplication()->desktop()->windows();
00300       foreach ( SUIT_ViewWindow* view, views ) {
00301        if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
00302          Handle(AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)view->getViewManager()->getViewModel())->getAISContext();
00303 
00304          AIS_ListOfInteractive displayed;
00305          ic->DisplayedObjects( displayed );
00306          ic->ObjectsInCollector( displayed );
00307 
00308          AIS_ListIteratorOfListOfInteractive it( displayed );
00309          while ( it.More() && shape.IsNull() ) {
00310            if ( it.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
00311              Handle(GEOM_AISShape) sh = Handle(GEOM_AISShape)::DownCast( it.Value() );
00312              if ( !sh.IsNull() && sh->hasIO() ) {
00313               Handle(SALOME_InteractiveObject) IO = Handle(SALOME_InteractiveObject)::DownCast( sh->getIO() );
00314               if ( !IO.IsNull() && IO->hasEntry() && obj->GetID() == IO->getEntry() )
00315                 shape = sh;
00316              }
00317            }
00318            it.Next();
00319          }
00320        }
00321        if ( !shape.IsNull() ) break;
00322       }
00323     }
00324   }
00325   return shape;
00326 }
00327 
00328 
00329 //=======================================================================
00330 // function : ConvertIORinGEOMActor()
00331 // purpose  :
00332 //=======================================================================
00333 GEOM_Actor* GEOMBase::ConvertIORinGEOMActor(const QString& IOR, bool onlyInActiveView)
00334 {
00335   GEOM_Actor* actor = 0;
00336 
00337   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00338   if ( study ) {
00339     _PTR(Study) studyDS = study->studyDS();
00340     _PTR(SObject) obj( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
00341     if ( obj ) {
00342       QList<SUIT_ViewWindow*> views;
00343       if ( onlyInActiveView ) 
00344        views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
00345       else 
00346        views = SUIT_Session::session()->activeApplication()->desktop()->windows();
00347       foreach ( SUIT_ViewWindow* view, views ) {
00348        if ( view && view->getViewManager()->getType() == SVTK_Viewer::Type() ) {
00349          SVTK_ViewWindow* aVTKViewWindow = dynamic_cast<SVTK_ViewWindow*>( view );
00350          if( !aVTKViewWindow )
00351            continue;
00352          vtkRenderer* Renderer = aVTKViewWindow->getRenderer();
00353          vtkActorCollection* theActors = Renderer->GetActors();
00354          theActors->InitTraversal();
00355          vtkActor* a = theActors->GetNextActor();
00356          while( a && !actor ) {
00357            if ( a->IsA( "GEOM_Actor" ) ) {
00358              GEOM_Actor* ga = GEOM_Actor::SafeDownCast( a );
00359              if ( ga && ga->hasIO() ) {
00360               Handle(SALOME_InteractiveObject) IO = Handle(SALOME_InteractiveObject)::DownCast( ga->getIO() );
00361               if ( !IO.IsNull() && IO->hasEntry() && obj->GetID() == IO->getEntry() )
00362                 actor = ga;
00363              }
00364            }
00365            a = theActors->GetNextActor();
00366          }
00367        }
00368        if ( actor ) break;
00369       }
00370     }
00371   }
00372   return actor;
00373 }
00374 
00375 //=======================================================================
00376 // function : GetAIS()
00377 // purpose  :
00378 //=======================================================================
00379 Handle(AIS_InteractiveObject) GEOMBase::GetAIS( const Handle(SALOME_InteractiveObject)& IO,
00380                                                 bool onlyInActiveView, bool onlyGeom )
00381 {
00382   Handle(AIS_InteractiveObject) aisObject;
00383 
00384   if ( !IO.IsNull() && IO->hasEntry() ) {
00385     QList<SUIT_ViewWindow*> views;
00386     if ( onlyInActiveView ) 
00387       views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
00388     else 
00389       views = SUIT_Session::session()->activeApplication()->desktop()->windows();
00390     
00391     foreach ( SUIT_ViewWindow* view, views ) {
00392       if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
00393         OCCViewer_Viewer* occViewer=(OCCViewer_Viewer*)view->getViewManager()->getViewModel();
00394         SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
00395         if (soccViewer) {
00396           SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( soccViewer->CreatePrs( IO->getEntry() ) );
00397           if ( occPrs && !occPrs->IsNull() ) {
00398             AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
00399             if( !shapes.Extent() ) continue;
00400             aisObject=shapes.First();
00401             delete occPrs;
00402           }
00403         }
00404       }
00405       if ( !aisObject.IsNull() ) break;
00406     } // foreach
00407   }
00408 
00409   return aisObject;
00410 }
00411 
00412 
00413 //=======================================================================
00414 // function : ConvertIOinGEOMAISShape()
00415 // purpose  :
00416 //=======================================================================
00417 Handle(GEOM_AISShape) GEOMBase::ConvertIOinGEOMAISShape( const Handle(SALOME_InteractiveObject)& IO, bool onlyInActiveView )
00418 {
00419   return Handle(GEOM_AISShape)::DownCast( GetAIS( IO, onlyInActiveView, true ) );
00420 }
00421 
00422 
00423 //=======================================================================
00424 // function : ConvertListOfIOInListOfIOR()
00425 // purpose  :
00426 //=======================================================================
00427 QStringList GEOMBase::ConvertListOfIOInListOfIOR( const SALOME_ListIO& IObjects )
00428 {
00429   QStringList iors;
00430   SALOME_ListIteratorOfListIO it( IObjects );
00431   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00432   if ( study ) {
00433     _PTR(Study) studyDS = study->studyDS();
00434     for ( ; it.More(); it.Next() ) {
00435       GEOM::GEOM_Object_var geomObj = ConvertIOinGEOMObject( it.Value() );
00436       if ( !CORBA::is_nil( geomObj ) )
00437         iors.append( GetIORFromObject( geomObj ) );
00438     }
00439   }
00440   return iors;
00441 }
00442 
00443 
00444 //=======================================================================
00445 // function : ConvertIOinGEOMObject()
00446 // purpose  :
00447 //=======================================================================
00448 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMObject( const Handle(SALOME_InteractiveObject)& IO )
00449 {
00450   GEOM::GEOM_Object_var object;
00451 
00452   if ( !IO.IsNull() && IO->hasEntry() ) {
00453     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00454     if ( study ) {
00455       _PTR(Study) studyDS = study->studyDS();
00456       _PTR(SObject) obj( studyDS->FindObjectID( IO->getEntry() ) );
00457       if ( obj ) {
00458         CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( obj );
00459         if ( !CORBA::is_nil( corbaObj ) )
00460           object = GEOM::GEOM_Object::_narrow( corbaObj );
00461       }
00462     }
00463   }
00464   return object._retn();
00465 }
00466 
00467 
00468 //=======================================================================
00469 // function : ConvertListOfIOInListOfGO()
00470 // purpose  :
00471 //=======================================================================
00472 void GEOMBase::ConvertListOfIOInListOfGO( const SALOME_ListIO& IObjects,
00473                                           GEOM::ListOfGO&      geomObjects,
00474                                           bool                 shapesOnly )
00475 {
00476   geomObjects.length( 0 );
00477 
00478   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00479   if ( study ) {
00480     _PTR(Study) studyDS = study->studyDS();
00481 
00482     geomObjects.length( IObjects.Extent() );
00483     SALOME_ListIteratorOfListIO it( IObjects );
00484 
00485     int i = 0;
00486     for ( ; it.More(); it.Next() ) {
00487       GEOM::GEOM_Object_var geomObj = ConvertIOinGEOMObject( it.Value() );
00488       if ( !CORBA::is_nil( geomObj ) && ( !shapesOnly || IsShape( geomObj ) ) )
00489         geomObjects[ i++ ] = geomObj;
00490     }
00491     geomObjects.length( i );
00492   }
00493 }
00494 
00495 
00496 //=================================================================================
00497 // function : CreateArrowForLinearEdge()
00498 // purpose  : Create a cone topology to be used to display an arrow in the middle
00499 //          : of an edge showing its orientation. (For simulation and Viewer OCC only)
00500 //=================================================================================
00501 TopoDS_Shape GEOMBase::CreateArrowForLinearEdge( const TopoDS_Shape& shape )
00502 {
00503   TopoDS_Shape ArrowCone;
00504 
00505   SUIT_ViewWindow* view = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
00506   if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() && shape.ShapeType() == TopAbs_EDGE ) {
00507     Handle(V3d_View) view3d = ((OCCViewer_ViewWindow*)view)->getViewPort()->getView();
00508     Standard_Real Width, Height;
00509     view3d->Size( Width, Height );
00510     const Standard_Real aHeight = (Width + Height) / 50.0;
00511 
00512     try {
00513       Standard_Real first, last;
00514       Handle(Geom_Curve) curv = BRep_Tool::Curve( TopoDS::Edge( shape ), first, last );
00515       if ( curv->IsCN(1) ) {
00516         const Standard_Real param = ( first+last ) / 2.0;
00517         gp_Pnt middleParamPoint;
00518         gp_Vec V1;
00519         curv->D1( param, middleParamPoint, V1 );
00520         if ( V1.Magnitude() > Precision::Confusion() ) {
00521           /* Topology orientation not geom orientation */
00522           if ( shape.Orientation() == TopAbs_REVERSED )
00523             V1 *= -1.0;
00524 
00525           gp_Ax2 anAxis( middleParamPoint, gp_Dir( V1 ) );
00526           const Standard_Real radius1 = aHeight / 5.0;
00527           if ( radius1 > 10.0 * Precision::Confusion() && aHeight > 10.0 * Precision::Confusion() )
00528             ArrowCone = BRepPrimAPI_MakeCone( anAxis, radius1, 0.0, aHeight ).Shape();
00529         }
00530       }
00531     }
00532     catch ( Standard_Failure ) {
00533       // OCC failures are hard to catch in GUI.
00534       // This is because of the position for #include <Standard_ErrorHandler.hxx> that is very critical to find
00535       // in SALOME environment : compilation error !
00536     }
00537   }
00538 
00539   return ArrowCone;
00540 }
00541 
00542 
00543 //=================================================================================
00544 // function : VertexToPoint()
00545 // purpose  : If S can be converted in a gp_Pnt returns true and the result is P
00546 //=================================================================================
00547 bool GEOMBase::VertexToPoint( const TopoDS_Shape& shape, gp_Pnt& point )
00548 {
00549   if ( shape.IsNull() || shape.ShapeType() != TopAbs_VERTEX )
00550     return false;
00551   point = BRep_Tool::Pnt( TopoDS::Vertex( shape ) );
00552   return true;
00553 }
00554 
00555 
00556 //=================================================================================
00557 // function : GetBipointDxDyDz()
00558 // purpose  :
00559 //=================================================================================
00560 void GEOMBase::GetBipointDxDyDz( const gp_Pnt& point1, const gp_Pnt& point2, double& dx, double& dy, double& dz )
00561 {
00562   dx = point2.X() - point1.X();
00563   dy = point2.Y() - point1.Y();
00564   dz = point2.Z() - point1.Z();
00565 }
00566 
00567 
00568 //=================================================================================
00569 // function : LinearEdgeExtremities()
00570 // purpose  : If S can be converted in a linear edge and if initial an final points
00571 //          : distance is sufficient, returns true else returns false.
00572 //          : Resulting points are respectively P1 and P2
00573 //=================================================================================
00574 bool GEOMBase::LinearEdgeExtremities( const TopoDS_Shape& shape,  gp_Pnt& point1, gp_Pnt& point2 )
00575 {
00576   if ( shape.IsNull() || shape.ShapeType() != TopAbs_EDGE )
00577     return false;
00578 
00579   BRepAdaptor_Curve curv( TopoDS::Edge( shape ) );
00580   if ( curv.GetType() != GeomAbs_Line )
00581     return false;
00582   
00583   gp_Pnt p1, p2;
00584 
00585   curv.D0( curv.FirstParameter(), p1 );
00586   curv.D0( curv.LastParameter(),  p2 );
00587 
00588   if ( p1.Distance( p2 ) <= Precision::Confusion() )
00589     return false;
00590 
00591   point1 = p1;
00592   point2 = p2;
00593   return true;
00594 }
00595 
00596 
00597 //=======================================================================
00598 // function : SelectionByNameInDialogs()
00599 // purpose  : Called when user has entered a name of object in a LineEdit.
00600 //          : The selection is changed. Dialog box will receive the
00601 //          : corresponding signal to manage this event.
00602 //=======================================================================
00603 bool GEOMBase::SelectionByNameInDialogs( QWidget* widget, const QString& objectUserName, const SALOME_ListIO& /*IObjects*/ )
00604 {
00605   /* Find SObject with name in component GEOM */
00606   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00607   if ( !study ) return false;
00608   _PTR(Study) studyDS = study->studyDS();
00609 
00610   std::vector<_PTR(SObject)> listSO = studyDS->FindObjectByName( objectUserName.toStdString(), "GEOM" );
00611 
00612   if ( listSO.size() < 1 ) {
00613     SUIT_MessageBox::critical( widget, 
00614                             QObject::tr( "GEOM_WRN_WARNING" ),
00615                             QObject::tr( "GEOM_NAME_INCORRECT" ),
00616                             QObject::tr( "GEOM_BUT_OK" ) );
00617     return false;
00618   }
00619 
00620   /* More than one object with same name */
00621   if ( listSO.size() > 1 ) {
00622     SUIT_MessageBox::critical( widget,
00623                             QObject::tr("GEOM_WRN_WARNING"),
00624                             QObject::tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE"),
00625                             QObject::tr("GEOM_BUT_OK") );
00626     return false;
00627   }
00628 
00629   /* Create a SALOME_InteractiveObject with a SALOME::SObject */
00630   Handle(SALOME_InteractiveObject) IO = new SALOME_InteractiveObject( listSO[0]->GetID().c_str(),
00631                                                               "GEOM",
00632                                                               objectUserName.toLatin1().constData() );
00633 
00634   /* Add as a selected object       */
00635   /* Clear any previous selection : */
00636   /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */
00637   // Sel->ClearIObjects(); //mzn
00638   // Sel->AddIObject(SI); //mzn
00639   return true;
00640 }
00641 
00642 
00643 //=======================================================================
00644 // function : DefineDlgPosition()
00645 // purpose  : Define x and y the default position for a dialog box
00646 //=======================================================================
00647 void GEOMBase::DefineDlgPosition( QWidget* dlg, int& x, int& y )
00648 {
00649   /* Here the position is on the bottom right corner - 10 */
00650   SUIT_Desktop* d = SUIT_Session::session()->activeApplication()->desktop();
00651   x = abs( d->x() + d->size().width()  - dlg->size().width()  - 10 );
00652   y = abs( d->y() + d->size().height() - dlg->size().height() - 10 );
00653 }
00654 
00655 
00656 //=======================================================================
00657 // function : GetDefaultName()
00658 // purpose  : Generates default names
00659 //=======================================================================
00660 QString GEOMBase::GetDefaultName( const QString& operation, bool extractPrefix )
00661 {
00662   QString aName = "";
00663 
00664   // collect all object names of GEOM component
00665   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00666   if ( study ) {
00667     _PTR(Study) studyDS = study->studyDS();
00668 
00669     std::set<std::string> names;
00670     _PTR(SComponent) component( studyDS->FindComponent( "GEOM" ) );
00671     if ( component ) {
00672       _PTR(ChildIterator) it( studyDS->NewChildIterator( component ) );
00673       for ( it->InitEx( true ); it->More(); it->Next() ) {
00674        names.insert( it->Value()->GetName() );
00675       }
00676     }
00677 
00678     // build a unique name
00679     int aNumber = 0;
00680     bool isUnique = false;
00681     QString prefix = operation;
00682 
00683     if ( extractPrefix ) {
00684       QStringList parts = prefix.split( "_", QString::KeepEmptyParts );
00685       if ( parts.count() > 1 ) {
00686        bool ok;
00687        aNumber = parts.last().toLong( &ok );
00688        if ( ok ) {
00689          parts.removeLast();
00690          prefix = parts.join( "_" );
00691          aNumber--;
00692        }
00693       }
00694     }
00695     
00696     while ( !isUnique ) {
00697       aName = prefix + "_" + QString::number( ++aNumber );
00698       isUnique = ( names.count( aName.toStdString()) == 0 );
00699     }
00700   }
00701   return aName;
00702 }
00703 
00704 
00705 //=======================================================================
00706 // function : ShowErrorMessage()
00707 // purpose  : Shows message box with error code and comment
00708 //=======================================================================
00709 void GEOMBase::ShowErrorMessage( const QString& errorCode, const QString& comment )
00710 {
00711   QStringList text;
00712   text << QObject::tr( "GEOM_PRP_ABORT" );
00713   if ( !errorCode.isEmpty() )
00714     text << QObject::tr( errorCode.toLatin1().constData() );
00715   if ( !comment.isEmpty() )
00716     text << QObject::tr( comment.toLatin1().constData() );
00717 
00718   SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
00719                           QObject::tr( "GEOM_ERROR" ),
00720                              text.join( "\n" ),
00721                           QObject::tr( "GEOM_BUT_OK" ) );
00722 }
00723 
00724 
00725 //=======================================================================
00726 // function : GetObjectFromIOR()
00727 // purpose  : returns a GEOM_Object by given IOR (string)
00728 //=======================================================================
00729 GEOM::GEOM_Object_ptr GEOMBase::GetObjectFromIOR( const QString& IOR )
00730 {
00731   GEOM::GEOM_Object_var geomObj;
00732   if ( !IOR.isEmpty() ) {
00733     CORBA::Object_var corbaObj = SalomeApp_Application::orb()->string_to_object( IOR.toLatin1().constData() );
00734     if ( !CORBA::is_nil( corbaObj ) )
00735       geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
00736   }
00737   return geomObj._retn();
00738 }
00739 
00740 //=======================================================================
00741 // function : GetIORFromObject()
00742 // purpose  : returns IOR of a given GEOM_Object
00743 //=======================================================================
00744 QString GEOMBase::GetIORFromObject( GEOM::GEOM_Object_ptr object )
00745 {
00746   QString IOR;
00747   if ( !CORBA::is_nil( object ) ) {
00748     CORBA::String_var anIOR = SalomeApp_Application::orb()->object_to_string( object );
00749     IOR = anIOR.in();
00750   }
00751   return IOR;
00752 }
00753 
00754 //=======================================================================
00755 // function : GetShape()
00756 // purpose  : returns a TopoDS_Shape stored in GEOM_Object
00757 //=======================================================================
00758 bool GEOMBase::GetShape( GEOM::GEOM_Object_ptr object, TopoDS_Shape& shape, const TopAbs_ShapeEnum type )
00759 {
00760   shape = TopoDS_Shape();
00761   if ( !CORBA::is_nil( object ) ) {
00762     TopAbs_ShapeEnum stype = (TopAbs_ShapeEnum)( object->GetShapeType() );
00763     if ( type == TopAbs_SHAPE || type == stype )
00764       shape = GEOM_Client::get_client().GetShape(  GeometryGUI::GetGeomGen(), object );
00765   }
00766   return !shape.IsNull();
00767 }
00768 
00769 //=======================================================================
00770 // function : GetName()
00771 // purpose  : Get name of object
00772 //=======================================================================
00773 QString GEOMBase::GetName( GEOM::GEOM_Object_ptr object )
00774 {
00775   QString name;
00776   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00777   
00778   if ( !CORBA::is_nil( object ) ) {
00779     // 1. search if object is already published in the study
00780     CORBA::String_var IOR = SalomeApp_Application::orb()->object_to_string( object );
00781     if ( study && strcmp( IOR.in(), "" ) != 0 ) {
00782       _PTR(SObject) aSObj( study->studyDS()->FindObjectIOR( std::string( IOR.in() ) ) );
00783       _PTR(GenericAttribute) anAttr;
00784       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") ) {
00785        _PTR(AttributeName) aNameAttr( anAttr );
00786        name = aNameAttr->Value().c_str();
00787       }
00788     }
00789     
00790     // 2. if object is not found in the study, try default name
00791     if ( name.isEmpty() ) {
00792       if ( object->IsMainShape() ) {
00793        name = GetDefaultName( "geomObj" );
00794       }
00795       else {
00796        GEOM::GEOM_Object_var mainShape = object->GetMainShape();
00797        if ( !CORBA::is_nil( mainShape  ) ) { 
00798          GEOM::ListOfLong_var indices = object->GetSubShapeIndices();
00799          if ( indices->length() > 0 ) {
00800            TopAbs_ShapeEnum type = (TopAbs_ShapeEnum)( object->GetShapeType() );
00801            name = QString( "%1:%2_%3" ).arg( GetName( mainShape.in() ) )
00802              .arg( TypeName( type ) ).arg( indices[0] );
00803          }
00804        }
00805       }
00806     }
00807   }
00808 
00809   return name;
00810 }
00811 
00812 //=======================================================================
00813 // function : IsShape()
00814 // purpose  : Return TRUE if object is valid and has shape
00815 //=======================================================================
00816 bool GEOMBase::IsShape( GEOM::GEOM_Object_ptr object )
00817 {
00818   return !object->_is_nil() && object->IsShape();
00819 }
00820 
00821 //=======================================================================
00822 // function : TypeName()
00823 // purpose  : Get string representation for the shape type
00824 //=======================================================================
00825 QString GEOMBase::TypeName( TopAbs_ShapeEnum type )
00826 {
00827   QString name = "shape";
00828   switch( type ) {
00829   case TopAbs_COMPSOLID:
00830     name = "compsolid"; break;
00831   case TopAbs_COMPOUND:
00832     name = "compound";  break;
00833   case TopAbs_SOLID:
00834     name = "solid";     break;
00835   case TopAbs_SHELL:
00836     name = "shell";     break;
00837   case TopAbs_FACE:
00838     name = "face";      break;
00839   case TopAbs_WIRE:
00840     name = "wire";      break;
00841   case TopAbs_EDGE:
00842     name = "edge";      break;
00843   case TopAbs_VERTEX:
00844     name = "vertex";    break;
00845   default:
00846     break;
00847   }
00848   return name;
00849 }
00850 
00851 //================================================================
00852 // Function : GetEntry
00853 // Purpose  : Get study entry for the given object (if it is published)
00854 //================================================================
00855 QString GEOMBase::GetEntry( GEOM::GEOM_Object_ptr object )
00856 {
00857   QString entry;
00858   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00859   if ( study && !CORBA::is_nil( object ) ) {
00860     QString objIOR = GetIORFromObject( object );
00861     if ( !objIOR.isEmpty() ) {
00862       _PTR(SObject) SO( study->studyDS()->FindObjectIOR( objIOR.toLatin1().constData() ) );
00863       if ( SO )
00864         entry = SO->GetID().c_str();
00865     }
00866   }
00867   return entry;
00868 }
00869 
00870 //================================================================
00871 // Function : PublishSubObject
00872 // Purpose  : Publish sub-shape under the main object
00873 //================================================================
00874 void GEOMBase::PublishSubObject( GEOM::GEOM_Object_ptr object )
00875 {
00876   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00877   if ( study && !CORBA::is_nil( object ) ) {
00878     _PTR(Study) studyDS = study->studyDS();
00879     QString entry = GetEntry( object );
00880     GEOM::GEOM_Object_var father = object->GetMainShape();
00881     QString fatherEntry = GetEntry( father );
00882     if ( entry.isEmpty() && !CORBA::is_nil( father ) && !fatherEntry.isEmpty() ) {
00883       QString name = GetName( object );
00884       GeometryGUI::GetGeomGen()->AddInStudy( GeometryGUI::ClientStudyToStudy( studyDS ),
00885                                         object, name.toLatin1().data(), father.in() );
00886     }
00887   }
00888 }