Salome HOME
rnc:
[modules/geom.git] / src / EntityGUI / EntityGUI.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : EntityGUI.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
26 //
27 #include "EntityGUI.h"
28
29 #include <GeometryGUI.h>
30 #include "GeometryGUI_Operations.h"
31
32 #include <SUIT_Session.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_ViewWindow.h>
35 #include <OCCViewer_ViewModel.h>
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewWindow.h>
38 #include <OCCViewer_ViewPort3d.h>
39 #include <SalomeApp_Study.h>
40 #include <SalomeApp_Application.h>
41
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS.hxx>
44 #include <BRep_Tool.hxx>
45 #include <ProjLib.hxx>
46 #include <ElSLib.hxx>
47
48 #include <QMouseEvent>
49
50 #include "EntityGUI_SketcherDlg.h" // Sketcher
51 #include "EntityGUI_3DSketcherDlg.h" // Sketcher
52 #include "EntityGUI_SubShapeDlg.h" // Method SUBSHAPE
53
54 //=======================================================================
55 // function : EntityGUI()
56 // purpose  : Constructor
57 //=======================================================================
58 EntityGUI::EntityGUI( GeometryGUI* parent ) :  GEOMGUI( parent )
59 {
60   mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
61   mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
62 }
63
64 //=======================================================================
65 // function : ~EntityGUI()
66 // purpose  : Destructor
67 //=======================================================================
68 EntityGUI::~EntityGUI()
69 {
70 }
71
72
73 //=======================================================================
74 // function : OnGUIEvent()
75 // purpose  : 
76 //=======================================================================
77 bool EntityGUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
78 {
79   SalomeApp_Application* app = getGeometryGUI()->getApp();
80   if ( !app ) return false;
81
82   getGeometryGUI()->EmitSignalDeactivateDialog();
83   QDialog* aDlg = NULL;
84
85   switch ( theCommandID ) {
86   case GEOMOp::Op2dSketcher: // 2D SKETCHER
87     getGeometryGUI()->ActiveWorkingPlane();
88     aDlg = new EntityGUI_SketcherDlg( getGeometryGUI(), parent );
89     break;
90   case GEOMOp::Op3dSketcher: // 3D SKETCHER
91     aDlg = new EntityGUI_3DSketcherDlg( getGeometryGUI(), parent );
92     break;
93   case GEOMOp::OpExplode:    // EXPLODE
94     aDlg = new EntityGUI_SubShapeDlg( getGeometryGUI(), parent );
95     break;
96   default:
97     app->putInfo( tr( "GEOM_PRP_COMMAND" ).arg( theCommandID ) );
98     break;
99   }
100   if ( aDlg )
101     aDlg->show();
102   
103   return true;
104 }
105
106 //=================================================================================
107 // function : 0nMousePress()
108 // purpose  : [static] manage mouse events
109 //=================================================================================
110 bool EntityGUI::OnMousePress( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
111 {
112   MESSAGE("EntityGUI::OnMousePress")
113   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
114
115   // Create Point dialog, OCC viewer 
116   if ( aDlg && ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_SketcherDlg" ) == 0 ) &&
117        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
118        pe->modifiers() != Qt::ControlModifier ) {
119     MESSAGE("Premier if ok!")
120     EntityGUI_SketcherDlg* aPntDlg = (EntityGUI_SketcherDlg*) aDlg;
121     if ( aPntDlg->acceptMouseEvent() ) {
122       OCCViewer_Viewer* anOCCViewer =
123         ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
124       Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
125
126       gp_Pnt aPnt;    
127
128       ic->InitSelected();
129       if ( pe->modifiers() == Qt::ShiftModifier )
130         ic->ShiftSelect();  // Append selection
131       else
132         ic->Select();       // New selection
133
134       ic->InitSelected();
135       if ( ic->MoreSelected() ) {
136         TopoDS_Shape aShape = ic->SelectedShape();
137         if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
138           aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
139       }
140       else {
141         OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
142         aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
143       }
144       
145       Qt::KeyboardModifiers modifiers = pe->modifiers();
146       aPntDlg->OnPointSelected( modifiers, aPnt );  // "feed" the point to point construction dialog
147     } // acceptMouseEvent()
148   }
149   return false;
150 }
151
152 //=======================================================================
153 // function : ConvertClickToPoint()
154 // purpose  : Returns the point clicked in 3D view
155 //=======================================================================
156 gp_Pnt EntityGUI::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
157 {
158   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
159   aView->Eye( XEye, YEye, ZEye );
160
161   aView->At( XAt, YAt, ZAt );
162   gp_Pnt EyePoint( XEye, YEye, ZEye );
163   gp_Pnt AtPoint( XAt, YAt, ZAt );
164
165   gp_Vec EyeVector( EyePoint, AtPoint );
166   gp_Dir EyeDir( EyeVector );
167
168   gp_Pln PlaneOfTheView = gp_Pln( AtPoint, EyeDir );
169   Standard_Real X, Y, Z;
170   aView->Convert( x, y, X, Y, Z );
171   gp_Pnt ConvertedPoint( X, Y, Z );
172
173   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project( PlaneOfTheView, ConvertedPoint );
174   gp_Pnt ResultPoint = ElSLib::Value( ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView );
175   return ResultPoint;
176 }
177
178 //=====================================================================================
179 // function : DisplaySimulationShape() 
180 // purpose  : Displays 'this->mySimulationShape' a pure graphical shape from a TopoDS_Shape
181 //=====================================================================================
182 void EntityGUI::DisplaySimulationShape( const TopoDS_Shape& S1, const TopoDS_Shape& S2 ) 
183 {
184   MESSAGE("EntityGUI::DisplaySimulationShape")
185   SalomeApp_Application* app = getGeometryGUI()->getApp();
186   if ( !app ) return;
187
188   SUIT_ViewManager* aVM = app->desktop()->activeWindow()->getViewManager();
189   if ( aVM->getType() != OCCViewer_Viewer::Type() )
190     return;
191
192   OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)aVM )->getOCCViewer();
193   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
194   try {
195     if ( !S1.IsNull() ) {
196       /* erase any previous */
197       ic->Erase( mySimulationShape1, Standard_True, Standard_False );
198       ic->ClearPrs( mySimulationShape1 );
199
200       mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
201       mySimulationShape1->Set( S1 );
202       mySimulationShape1->SetColor( Quantity_NOC_RED );
203
204       ic->Deactivate( mySimulationShape1 );
205       ic->Display( mySimulationShape1, Standard_False );
206       mySimulationShape1->UnsetColor();
207     }
208     if ( !S2.IsNull() ) {
209       ic->Erase( mySimulationShape2, Standard_True, Standard_False );
210       ic->ClearPrs( mySimulationShape2 );
211
212       mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
213       mySimulationShape2->Set( S2 );
214       mySimulationShape2->SetColor( Quantity_NOC_VIOLET );
215
216       ic->Deactivate( mySimulationShape2 );
217       ic->Display( mySimulationShape2, Standard_False );
218       mySimulationShape2->UnsetColor();
219     }
220     ic->UpdateCurrentViewer();
221   }
222   catch( Standard_Failure ) {
223     MESSAGE( "Exception catched in EntityGUI::DisplaySimulationShape" );
224   } 
225 }
226
227 //==================================================================================
228 // function : EraseSimulationShape()
229 // purpose  : Clears the display of 'mySimulationShape' a pure graphical shape
230 //==================================================================================
231 void EntityGUI::EraseSimulationShape()
232 {
233   MESSAGE("EntityGUI::EraseSimulationShape")
234   SalomeApp_Application* app = getGeometryGUI()->getApp();
235   if ( !app ) return;
236
237   // get all view windows at the desktop
238   QList<SUIT_ViewWindow*> aWndLst = app->desktop()->windows();
239   //get all view windows, which belong to the active study
240   QList<SUIT_ViewWindow*> aWndLstAS;
241   SUIT_ViewWindow* vw;
242
243   QListIterator<SUIT_ViewWindow*> itWL( aWndLst );
244   while ( itWL.hasNext() && ( vw = itWL.next() ) )
245     if ( vw->getViewManager()->study() == app->activeStudy() )
246       aWndLstAS.append( vw );
247
248   QListIterator<SUIT_ViewWindow*> itWLAS( aWndLstAS );
249   while ( itWLAS.hasNext() && ( vw = itWLAS.next() ) ) {
250     if ( vw->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
251       OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)( vw->getViewManager() ) )->getOCCViewer();
252       Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
253       ic->Erase( mySimulationShape1, Standard_True, Standard_False );
254       ic->ClearPrs( mySimulationShape1 );
255       ic->Erase( mySimulationShape2, Standard_True, Standard_False );
256       ic->ClearPrs( mySimulationShape2 );
257       ic->UpdateCurrentViewer();
258     } 
259   }
260 }
261
262 //=====================================================================================
263 // function : SObjectExist()
264 // purpose  :
265 //=====================================================================================
266 bool EntityGUI::SObjectExist( const _PTR(SObject)& theFatherObject, const char* IOR )
267 {
268   SalomeApp_Application* app = getGeometryGUI()->getApp();
269   if ( !app ) return false;
270   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
271   if ( !appStudy ) return false;
272
273   _PTR(Study) aStudy = appStudy->studyDS();
274   _PTR(ChildIterator) it ( aStudy->NewChildIterator( theFatherObject ) );
275   _PTR(SObject) RefSO;
276   _PTR(GenericAttribute) anAttr;
277   for ( ; it->More();it->Next() ) {
278     _PTR(SObject) SO ( it->Value() );
279     if ( SO->FindAttribute( anAttr, "AttributeIOR" ) ) {
280       _PTR(AttributeIOR) anIOR ( anAttr  );
281       if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
282         return true;
283     }
284     if ( SO->ReferencedObject( RefSO ) ) {
285       if ( RefSO->FindAttribute( anAttr, "AttributeIOR" ) ) {
286         _PTR(AttributeIOR) anIOR ( anAttr );
287         if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
288           return true;
289       }
290     }
291   }
292   return false;
293 }
294
295
296 //=====================================================================================
297 // EXPORTED METHODS
298 //=====================================================================================
299 extern "C"
300 {
301 #ifdef WIN32
302   __declspec( dllexport )
303 #endif
304   GEOMGUI* GetLibGUI( GeometryGUI* parent )
305   {
306     return new EntityGUI( parent );
307   }
308 }