Back to index

salome-geom  6.5.0
EntityGUI.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   : EntityGUI.cxx
00025 // Author : Damien COQUERET, Open CASCADE S.A.S.
00026 //
00027 #include "EntityGUI.h"
00028 
00029 #include <GeometryGUI.h>
00030 #include "GeometryGUI_Operations.h"
00031 
00032 #include <SUIT_Session.h>
00033 #include <SUIT_Desktop.h>
00034 #include <SUIT_ViewWindow.h>
00035 #include <OCCViewer_ViewModel.h>
00036 #include <OCCViewer_ViewManager.h>
00037 #include <OCCViewer_ViewWindow.h>
00038 #include <OCCViewer_ViewPort3d.h>
00039 #include <SalomeApp_Study.h>
00040 #include <SalomeApp_Application.h>
00041 
00042 #include <TopoDS_Shape.hxx>
00043 #include <TopoDS.hxx>
00044 #include <BRep_Tool.hxx>
00045 #include <ProjLib.hxx>
00046 #include <ElSLib.hxx>
00047 
00048 #include <QMouseEvent>
00049 #include <QApplication>
00050 
00051 #include "EntityGUI_SketcherDlg.h"        // Sketcher
00052 #include "EntityGUI_3DSketcherDlg.h"      // Sketcher
00053 #include "EntityGUI_SubShapeDlg.h"        // Method SUBSHAPE
00054 #include "EntityGUI_FeatureDetectorDlg.h" // Feature Detection
00055 #include "EntityGUI_PictureImportDlg.h"   // Import Picture in viewer
00056 
00057 //=======================================================================
00058 // function : EntityGUI()
00059 // purpose  : Constructor
00060 //=======================================================================
00061 EntityGUI::EntityGUI( GeometryGUI* parent ) :  GEOMGUI( parent )
00062 {
00063   mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
00064   mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
00065 }
00066 
00067 //=======================================================================
00068 // function : ~EntityGUI()
00069 // purpose  : Destructor
00070 //=======================================================================
00071 EntityGUI::~EntityGUI()
00072 {
00073 }
00074 
00075 
00076 //=======================================================================
00077 // function : OnGUIEvent()
00078 // purpose  : 
00079 //=======================================================================
00080 bool EntityGUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
00081 {
00082   SalomeApp_Application* app = getGeometryGUI()->getApp();
00083   if ( !app ) return false;
00084 
00085   getGeometryGUI()->EmitSignalDeactivateDialog();
00086   QDialog* aDlg = NULL;
00087 
00088   switch ( theCommandID ) {
00089   case GEOMOp::Op2dSketcher: // 2D SKETCHER
00090     getGeometryGUI()->ActiveWorkingPlane();
00091     aDlg = new EntityGUI_SketcherDlg( getGeometryGUI(), parent );
00092     break;
00093   case GEOMOp::Op3dSketcher: // 3D SKETCHER
00094     aDlg = new EntityGUI_3DSketcherDlg( getGeometryGUI(), parent );
00095     break;
00096   case GEOMOp::OpExplode:    // EXPLODE
00097     aDlg = new EntityGUI_SubShapeDlg( getGeometryGUI(), parent );
00098     break;
00099 #ifdef WITH_OPENCV
00100   case GEOMOp::OpFeatureDetect:    // FEATURE DETECTION
00101     aDlg = new EntityGUI_FeatureDetectorDlg( getGeometryGUI(), parent );
00102     break;
00103 #endif
00104   case GEOMOp::OpPictureImport:    // IMPORT PICTURE IN VIEWER
00105     aDlg = new EntityGUI_PictureImportDlg( getGeometryGUI(), parent );
00106     break;
00107   default:
00108     app->putInfo( tr( "GEOM_PRP_COMMAND" ).arg( theCommandID ) );
00109     break;
00110   }
00111   if ( aDlg )
00112     aDlg->show();
00113   
00114   return true;
00115 }
00116 
00117 //=================================================================================
00118 // function : 0nMousePress()
00119 // purpose  : [static] manage mouse events
00120 //=================================================================================
00121 bool EntityGUI::OnMousePress( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
00122 {
00123   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
00124 
00125   // Create Point dialog, OCC viewer 
00126   if ( aDlg && 
00127        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
00128        pe->modifiers() != Qt::ControlModifier ) {
00129     
00130     gp_Pnt aPnt;
00131   
00132     if ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_SketcherDlg" ) == 0 ) 
00133     { 
00134       EntityGUI_SketcherDlg* aSketcherDlg = (EntityGUI_SketcherDlg*) aDlg;
00135       ((OCCViewer_ViewWindow*)theViewWindow)->setSketcherStyle(true);
00136       if ( aSketcherDlg->acceptMouseEvent() ) {
00137         OCCViewer_Viewer* anOCCViewer =
00138           ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
00139         Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
00140     
00141         ic->InitSelected();
00142         if ( pe->modifiers() == Qt::ShiftModifier )
00143           ic->ShiftSelect();  // Append selection
00144         else
00145           ic->Select();       // New selection
00146 
00147         ic->InitSelected();
00148         if ( ic->MoreSelected() ) {
00149           TopoDS_Shape aShape = ic->SelectedShape();
00150           if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
00151             aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
00152         }
00153         else {
00154           OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
00155           aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
00156         }
00157         
00158         Qt::KeyboardModifiers modifiers = pe->modifiers();
00159         aSketcherDlg->OnPointSelected( modifiers, aPnt );  // "feed" the point to point construction dialog
00160       } // acceptMouseEvent()
00161     }
00162 #ifdef WITH_OPENCV
00163     if (  QString( aDlg->metaObject()->className() ).compare( "EntityGUI_FeatureDetectorDlg" ) == 0 ) 
00164     {
00165       EntityGUI_FeatureDetectorDlg* aCornerDlg = (EntityGUI_FeatureDetectorDlg*) aDlg;
00166       if ( aCornerDlg->acceptMouseEvent() ) {
00167         OCCViewer_Viewer* anOCCViewer =
00168           ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
00169         Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();   
00170 
00171         ic->InitSelected();
00172         ic->Select();       // New selection
00173 
00174         ic->InitSelected();
00175         TopoDS_Shape aShape;
00176         if ( ic->MoreSelected() ) 
00177           aShape = ic->SelectedShape();
00178         if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
00179             aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
00180         else 
00181         {
00182           OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
00183           aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
00184         }
00185         
00186 //         aCornerDlg->OnPointSelected( aPnt );  // "feed" the point to corner detection dialog
00187         
00188 //         QPoint start = QPoint(pe->x(),pe->y());
00189         aCornerDlg->setStartPnt( aPnt );
00190       } // acceptMouseEvent()
00191       
00192     }
00193 #endif
00194   }
00195 
00196   return false;
00197 }
00198 
00199 //=================================================================================
00200 // function : 0nMouseRelease()
00201 // purpose  : [static] manage mouse events
00202 //=================================================================================
00203 bool EntityGUI::OnMouseRelease( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
00204 {
00205   ((OCCViewer_ViewWindow*)theViewWindow)->setSketcherStyle(false);
00206   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
00207 #ifdef WITH_OPENCV
00208   if ( aDlg && ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_FeatureDetectorDlg" ) == 0 ) &&
00209        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
00210        pe->modifiers() != Qt::ControlModifier ) 
00211   {   
00212     EntityGUI_FeatureDetectorDlg* aCornerDlg = (EntityGUI_FeatureDetectorDlg*) aDlg;
00213    
00214     gp_Pnt aPnt; 
00215       
00216     if ( aCornerDlg->acceptMouseEvent() )
00217     {
00218 //       QPoint end = QPoint(pe->x(),pe->y());
00219       OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
00220       aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
00221       aCornerDlg->setEndPnt( aPnt );
00222     }    
00223   }
00224 #endif
00225   return false;
00226 }
00227 
00228 //=================================================================================
00229 // function : 0nMouseMove()
00230 // purpose  : [static] manage mouse events
00231 //=================================================================================
00232 bool EntityGUI::OnMouseMove( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
00233 {
00234   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
00235   
00236   if ( aDlg && QString( aDlg->metaObject()->className() ).compare( "EntityGUI_SketcherDlg" ) == 0 &&
00237        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) 
00238   { 
00239     EntityGUI_SketcherDlg* aSketcherDlg = (EntityGUI_SketcherDlg*) aDlg;
00240     if ( aSketcherDlg->acceptMouseEvent() ) 
00241     {    
00242       OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
00243       gp_Pnt aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
00244   
00245       Qt::KeyboardModifiers modifiers = pe->modifiers();
00246       if (QApplication::mouseButtons() == Qt::LeftButton )
00247         aSketcherDlg->OnPointSelected( modifiers, aPnt, false );  // "feed" the point to point construction dialog
00248     }
00249   }
00250     
00251   return false;
00252 }
00253 
00254 //=======================================================================
00255 // function : ConvertClickToPoint()
00256 // purpose  : Returns the point clicked in 3D view
00257 //=======================================================================
00258 gp_Pnt EntityGUI::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
00259 {
00260   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
00261   aView->Eye( XEye, YEye, ZEye );
00262 
00263   aView->At( XAt, YAt, ZAt );
00264   gp_Pnt EyePoint( XEye, YEye, ZEye );
00265   gp_Pnt AtPoint( XAt, YAt, ZAt );
00266 
00267   gp_Vec EyeVector( EyePoint, AtPoint );
00268   gp_Dir EyeDir( EyeVector );
00269 
00270   gp_Pln PlaneOfTheView = gp_Pln( AtPoint, EyeDir );
00271   Standard_Real X, Y, Z;
00272   aView->Convert( x, y, X, Y, Z );
00273   gp_Pnt ConvertedPoint( X, Y, Z );
00274 
00275   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project( PlaneOfTheView, ConvertedPoint );
00276   gp_Pnt ResultPoint = ElSLib::Value( ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView );
00277   return ResultPoint;
00278 }
00279 
00280 //=====================================================================================
00281 // function : DisplaySimulationShape() 
00282 // purpose  : Displays 'this->mySimulationShape' a pure graphical shape from a TopoDS_Shape
00283 //=====================================================================================
00284 void EntityGUI::DisplaySimulationShape( const TopoDS_Shape& S1, const TopoDS_Shape& S2 ) 
00285 {
00286   MESSAGE("EntityGUI::DisplaySimulationShape")
00287   SalomeApp_Application* app = getGeometryGUI()->getApp();
00288   if ( !app ) return;
00289 
00290   SUIT_ViewManager* aVM = app->desktop()->activeWindow()->getViewManager();
00291   if ( aVM->getType() != OCCViewer_Viewer::Type() )
00292     return;
00293 
00294   OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)aVM )->getOCCViewer();
00295   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
00296   try {
00297     if ( !S1.IsNull() ) {
00298       /* erase any previous */
00299       ic->Erase( mySimulationShape1, Standard_True, Standard_False );
00300       ic->ClearPrs( mySimulationShape1 );
00301 
00302       mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
00303       mySimulationShape1->Set( S1 );
00304       mySimulationShape1->SetColor( Quantity_NOC_RED );
00305 
00306       ic->Deactivate( mySimulationShape1 );
00307       ic->Display( mySimulationShape1, Standard_False );
00308       mySimulationShape1->UnsetColor();
00309     }
00310     if ( !S2.IsNull() ) {
00311       ic->Erase( mySimulationShape2, Standard_True, Standard_False );
00312       ic->ClearPrs( mySimulationShape2 );
00313 
00314       mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
00315       mySimulationShape2->Set( S2 );
00316       mySimulationShape2->SetColor( Quantity_NOC_VIOLET );
00317 
00318       ic->Deactivate( mySimulationShape2 );
00319       ic->Display( mySimulationShape2, Standard_False );
00320       mySimulationShape2->UnsetColor();
00321     }
00322     ic->UpdateCurrentViewer();
00323   }
00324   catch( Standard_Failure ) {
00325     MESSAGE( "Exception catched in EntityGUI::DisplaySimulationShape" );
00326   } 
00327 }
00328 
00329 //==================================================================================
00330 // function : EraseSimulationShape()
00331 // purpose  : Clears the display of 'mySimulationShape' a pure graphical shape
00332 //==================================================================================
00333 void EntityGUI::EraseSimulationShape()
00334 {
00335   MESSAGE("EntityGUI::EraseSimulationShape")
00336   SalomeApp_Application* app = getGeometryGUI()->getApp();
00337   if ( !app ) return;
00338 
00339   // get all view windows at the desktop
00340   QList<SUIT_ViewWindow*> aWndLst = app->desktop()->windows();
00341   //get all view windows, which belong to the active study
00342   QList<SUIT_ViewWindow*> aWndLstAS;
00343   SUIT_ViewWindow* vw;
00344 
00345   QListIterator<SUIT_ViewWindow*> itWL( aWndLst );
00346   while ( itWL.hasNext() && ( vw = itWL.next() ) )
00347     if ( vw->getViewManager()->study() == app->activeStudy() )
00348       aWndLstAS.append( vw );
00349 
00350   QListIterator<SUIT_ViewWindow*> itWLAS( aWndLstAS );
00351   while ( itWLAS.hasNext() && ( vw = itWLAS.next() ) ) {
00352     if ( vw->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
00353       OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)( vw->getViewManager() ) )->getOCCViewer();
00354       Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
00355       ic->Erase( mySimulationShape1, Standard_True, Standard_False );
00356       ic->ClearPrs( mySimulationShape1 );
00357       ic->Erase( mySimulationShape2, Standard_True, Standard_False );
00358       ic->ClearPrs( mySimulationShape2 );
00359       ic->UpdateCurrentViewer();
00360     } 
00361   }
00362 }
00363 
00364 //=====================================================================================
00365 // function : SObjectExist()
00366 // purpose  :
00367 //=====================================================================================
00368 bool EntityGUI::SObjectExist( const _PTR(SObject)& theFatherObject, const char* IOR )
00369 {
00370   SalomeApp_Application* app = getGeometryGUI()->getApp();
00371   if ( !app ) return false;
00372   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
00373   if ( !appStudy ) return false;
00374 
00375   _PTR(Study) aStudy = appStudy->studyDS();
00376   _PTR(ChildIterator) it ( aStudy->NewChildIterator( theFatherObject ) );
00377   _PTR(SObject) RefSO;
00378   _PTR(GenericAttribute) anAttr;
00379   for ( ; it->More();it->Next() ) {
00380     _PTR(SObject) SO ( it->Value() );
00381     if ( SO->FindAttribute( anAttr, "AttributeIOR" ) ) {
00382       _PTR(AttributeIOR) anIOR ( anAttr  );
00383       if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
00384         return true;
00385     }
00386     if ( SO->ReferencedObject( RefSO ) ) {
00387       if ( RefSO->FindAttribute( anAttr, "AttributeIOR" ) ) {
00388         _PTR(AttributeIOR) anIOR ( anAttr );
00389         if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
00390           return true;
00391       }
00392     }
00393   }
00394   return false;
00395 }
00396 
00397 
00398 //=====================================================================================
00399 // EXPORTED METHODS
00400 //=====================================================================================
00401 extern "C"
00402 {
00403 #ifdef WIN32
00404   __declspec( dllexport )
00405 #endif
00406   GEOMGUI* GetLibGUI( GeometryGUI* parent )
00407   {
00408     return new EntityGUI( parent );
00409   }
00410 }