1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : EntityGUI.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
27 #include "EntityGUI.h"
29 #include <GeometryGUI.h>
30 #include "GeometryGUI_Operations.h"
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>
42 #include <TopoDS_Shape.hxx>
44 #include <BRep_Tool.hxx>
45 #include <ProjLib.hxx>
48 #include <QMouseEvent>
49 #include <QApplication>
51 #include "EntityGUI_SketcherDlg.h" // Sketcher
52 #include "EntityGUI_3DSketcherDlg.h" // Sketcher
53 #include "EntityGUI_SubShapeDlg.h" // Method SUBSHAPE
54 #include "EntityGUI_FeatureDetectorDlg.h" // Feature Detection
55 #include "EntityGUI_PictureImportDlg.h" // Import Picture in viewer
57 //=======================================================================
58 // function : EntityGUI()
59 // purpose : Constructor
60 //=======================================================================
61 EntityGUI::EntityGUI( GeometryGUI* parent ) : GEOMGUI( parent )
63 mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
64 mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
67 //=======================================================================
68 // function : ~EntityGUI()
69 // purpose : Destructor
70 //=======================================================================
71 EntityGUI::~EntityGUI()
76 //=======================================================================
77 // function : OnGUIEvent()
79 //=======================================================================
80 bool EntityGUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
82 SalomeApp_Application* app = getGeometryGUI()->getApp();
83 if ( !app ) return false;
85 getGeometryGUI()->EmitSignalDeactivateDialog();
88 switch ( theCommandID ) {
89 case GEOMOp::Op2dSketcher: // 2D SKETCHER
90 getGeometryGUI()->ActiveWorkingPlane();
91 aDlg = new EntityGUI_SketcherDlg( getGeometryGUI(), parent );
93 case GEOMOp::Op3dSketcher: // 3D SKETCHER
94 aDlg = new EntityGUI_3DSketcherDlg( getGeometryGUI(), parent );
96 case GEOMOp::OpExplode: // EXPLODE
97 aDlg = new EntityGUI_SubShapeDlg( getGeometryGUI(), parent );
100 case GEOMOp::OpFeatureDetect: // FEATURE DETECTION
101 aDlg = new EntityGUI_FeatureDetectorDlg( getGeometryGUI(), parent );
103 case GEOMOp::OpPictureImport: // IMPORT PICTURE IN VIEWER
104 aDlg = new EntityGUI_PictureImportDlg( getGeometryGUI(), parent );
108 app->putInfo( tr( "GEOM_PRP_COMMAND" ).arg( theCommandID ) );
117 //=================================================================================
118 // function : 0nMousePress()
119 // purpose : [static] manage mouse events
120 //=================================================================================
121 bool EntityGUI::OnMousePress( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
123 MESSAGE("EntityGUI::OnMousePress")
124 QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
126 // Create Point dialog, OCC viewer
128 theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
129 pe->modifiers() != Qt::ControlModifier ) {
133 if ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_SketcherDlg" ) == 0 )
135 EntityGUI_SketcherDlg* aSketcherDlg = (EntityGUI_SketcherDlg*) aDlg;
136 if ( aSketcherDlg->acceptMouseEvent() ) {
137 OCCViewer_Viewer* anOCCViewer =
138 ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
139 Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
142 if ( pe->modifiers() == Qt::ShiftModifier )
143 ic->ShiftSelect(); // Append selection
145 ic->Select(); // New selection
148 if ( ic->MoreSelected() ) {
149 TopoDS_Shape aShape = ic->SelectedShape();
150 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
151 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
154 OCCViewer_ViewPort3d* vp = ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
155 aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
158 Qt::KeyboardModifiers modifiers = pe->modifiers();
159 aSketcherDlg->OnPointSelected( modifiers, aPnt ); // "feed" the point to point construction dialog
160 } // acceptMouseEvent()
162 if ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_FeatureDetectorDlg" ) == 0 )
164 EntityGUI_FeatureDetectorDlg* aCornerDlg = (EntityGUI_FeatureDetectorDlg*) aDlg;
165 if ( aCornerDlg->acceptMouseEvent() ) {
166 OCCViewer_Viewer* anOCCViewer =
167 ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
168 Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
171 ic->Select(); // New selection
174 if ( ic->MoreSelected() ) {
175 TopoDS_Shape aShape = ic->SelectedShape();
176 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
177 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
180 OCCViewer_ViewPort3d* vp = ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
181 aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
184 // aCornerDlg->OnPointSelected( aPnt ); // "feed" the point to corner detection dialog
186 QPoint start = QPoint(pe->x(),pe->y());
187 aCornerDlg->setStartPnt( start );
188 } // acceptMouseEvent()
195 //=================================================================================
196 // function : 0nMouseMove()
197 // purpose : [static] manage mouse events
198 //=================================================================================
199 bool EntityGUI::OnMouseMove( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
201 // MESSAGE("EntityGUI::OnMouseMove")
202 QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
203 if ( aDlg && ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_FeatureDetectorDlg" ) == 0 ) &&
204 theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
205 pe->modifiers() != Qt::ControlModifier ) {
207 EntityGUI_FeatureDetectorDlg* aCornerDlg = (EntityGUI_FeatureDetectorDlg*) aDlg;
211 if ( QApplication::mouseButtons() == Qt::LeftButton &&
212 aCornerDlg->acceptMouseEvent() )
214 QPoint end = QPoint(pe->x(),pe->y());
215 aCornerDlg->setEndPnt( end );
221 //=======================================================================
222 // function : ConvertClickToPoint()
223 // purpose : Returns the point clicked in 3D view
224 //=======================================================================
225 gp_Pnt EntityGUI::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
227 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
228 aView->Eye( XEye, YEye, ZEye );
230 aView->At( XAt, YAt, ZAt );
231 gp_Pnt EyePoint( XEye, YEye, ZEye );
232 gp_Pnt AtPoint( XAt, YAt, ZAt );
234 gp_Vec EyeVector( EyePoint, AtPoint );
235 gp_Dir EyeDir( EyeVector );
237 gp_Pln PlaneOfTheView = gp_Pln( AtPoint, EyeDir );
238 Standard_Real X, Y, Z;
239 aView->Convert( x, y, X, Y, Z );
240 gp_Pnt ConvertedPoint( X, Y, Z );
242 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project( PlaneOfTheView, ConvertedPoint );
243 gp_Pnt ResultPoint = ElSLib::Value( ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView );
247 //=====================================================================================
248 // function : DisplaySimulationShape()
249 // purpose : Displays 'this->mySimulationShape' a pure graphical shape from a TopoDS_Shape
250 //=====================================================================================
251 void EntityGUI::DisplaySimulationShape( const TopoDS_Shape& S1, const TopoDS_Shape& S2 )
253 MESSAGE("EntityGUI::DisplaySimulationShape")
254 SalomeApp_Application* app = getGeometryGUI()->getApp();
257 SUIT_ViewManager* aVM = app->desktop()->activeWindow()->getViewManager();
258 if ( aVM->getType() != OCCViewer_Viewer::Type() )
261 OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)aVM )->getOCCViewer();
262 Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
264 if ( !S1.IsNull() ) {
265 /* erase any previous */
266 ic->Erase( mySimulationShape1, Standard_True, Standard_False );
267 ic->ClearPrs( mySimulationShape1 );
269 mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
270 mySimulationShape1->Set( S1 );
271 mySimulationShape1->SetColor( Quantity_NOC_RED );
273 ic->Deactivate( mySimulationShape1 );
274 ic->Display( mySimulationShape1, Standard_False );
275 mySimulationShape1->UnsetColor();
277 if ( !S2.IsNull() ) {
278 ic->Erase( mySimulationShape2, Standard_True, Standard_False );
279 ic->ClearPrs( mySimulationShape2 );
281 mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
282 mySimulationShape2->Set( S2 );
283 mySimulationShape2->SetColor( Quantity_NOC_VIOLET );
285 ic->Deactivate( mySimulationShape2 );
286 ic->Display( mySimulationShape2, Standard_False );
287 mySimulationShape2->UnsetColor();
289 ic->UpdateCurrentViewer();
291 catch( Standard_Failure ) {
292 MESSAGE( "Exception catched in EntityGUI::DisplaySimulationShape" );
296 //==================================================================================
297 // function : EraseSimulationShape()
298 // purpose : Clears the display of 'mySimulationShape' a pure graphical shape
299 //==================================================================================
300 void EntityGUI::EraseSimulationShape()
302 MESSAGE("EntityGUI::EraseSimulationShape")
303 SalomeApp_Application* app = getGeometryGUI()->getApp();
306 // get all view windows at the desktop
307 QList<SUIT_ViewWindow*> aWndLst = app->desktop()->windows();
308 //get all view windows, which belong to the active study
309 QList<SUIT_ViewWindow*> aWndLstAS;
312 QListIterator<SUIT_ViewWindow*> itWL( aWndLst );
313 while ( itWL.hasNext() && ( vw = itWL.next() ) )
314 if ( vw->getViewManager()->study() == app->activeStudy() )
315 aWndLstAS.append( vw );
317 QListIterator<SUIT_ViewWindow*> itWLAS( aWndLstAS );
318 while ( itWLAS.hasNext() && ( vw = itWLAS.next() ) ) {
319 if ( vw->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
320 OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)( vw->getViewManager() ) )->getOCCViewer();
321 Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
322 ic->Erase( mySimulationShape1, Standard_True, Standard_False );
323 ic->ClearPrs( mySimulationShape1 );
324 ic->Erase( mySimulationShape2, Standard_True, Standard_False );
325 ic->ClearPrs( mySimulationShape2 );
326 ic->UpdateCurrentViewer();
331 //=====================================================================================
332 // function : SObjectExist()
334 //=====================================================================================
335 bool EntityGUI::SObjectExist( const _PTR(SObject)& theFatherObject, const char* IOR )
337 SalomeApp_Application* app = getGeometryGUI()->getApp();
338 if ( !app ) return false;
339 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
340 if ( !appStudy ) return false;
342 _PTR(Study) aStudy = appStudy->studyDS();
343 _PTR(ChildIterator) it ( aStudy->NewChildIterator( theFatherObject ) );
345 _PTR(GenericAttribute) anAttr;
346 for ( ; it->More();it->Next() ) {
347 _PTR(SObject) SO ( it->Value() );
348 if ( SO->FindAttribute( anAttr, "AttributeIOR" ) ) {
349 _PTR(AttributeIOR) anIOR ( anAttr );
350 if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
353 if ( SO->ReferencedObject( RefSO ) ) {
354 if ( RefSO->FindAttribute( anAttr, "AttributeIOR" ) ) {
355 _PTR(AttributeIOR) anIOR ( anAttr );
356 if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
365 //=====================================================================================
367 //=====================================================================================
371 __declspec( dllexport )
373 GEOMGUI* GetLibGUI( GeometryGUI* parent )
375 return new EntityGUI( parent );