1 // Copyright (C) 2007-2013 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 <LightApp_SelectionMgr.h>
33 #include <OCCViewer_ViewManager.h>
34 #include <OCCViewer_ViewModel.h>
35 #include <OCCViewer_ViewPort3d.h>
36 #include <OCCViewer_ViewWindow.h>
37 #include <SALOME_ListIteratorOfListIO.hxx>
38 #include <SUIT_Desktop.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Session.h>
41 #include <SUIT_ViewWindow.h>
42 #include <SalomeApp_Application.h>
43 #include <SalomeApp_Study.h>
45 #include <TopoDS_Shape.hxx>
47 #include <BRep_Tool.hxx>
48 #include <ProjLib.hxx>
51 #include <QMouseEvent>
52 #include <QApplication>
54 #include "EntityGUI_SketcherDlg.h" // Sketcher
55 #include "EntityGUI_3DSketcherDlg.h" // Sketcher
56 #include "EntityGUI_SubShapeDlg.h" // Method SUBSHAPE
57 #include "EntityGUI_FeatureDetectorDlg.h" // Feature Detection
58 #include "EntityGUI_PictureImportDlg.h" // Import Picture in viewer
59 #include "EntityGUI_FieldDlg.h" // Create/Edit Field
61 #include "GEOMImpl_Types.hxx"
64 //=======================================================================
65 // function : EntityGUI()
66 // purpose : Constructor
67 //=======================================================================
68 EntityGUI::EntityGUI( GeometryGUI* parent ) : GEOMGUI( parent )
70 mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
71 mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
74 //=======================================================================
75 // function : ~EntityGUI()
76 // purpose : Destructor
77 //=======================================================================
78 EntityGUI::~EntityGUI()
83 //=======================================================================
84 // function : OnGUIEvent()
86 //=======================================================================
87 bool EntityGUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
89 SalomeApp_Application* app = getGeometryGUI()->getApp();
90 if ( !app ) return false;
92 getGeometryGUI()->EmitSignalDeactivateDialog();
95 switch ( theCommandID ) {
96 case GEOMOp::Op2dSketcher: // 2D SKETCHER
97 getGeometryGUI()->ActiveWorkingPlane();
98 aDlg = new EntityGUI_SketcherDlg( getGeometryGUI(), parent );
100 case GEOMOp::Op3dSketcher: // 3D SKETCHER
101 aDlg = new EntityGUI_3DSketcherDlg( getGeometryGUI(), parent );
103 case GEOMOp::OpExplode: // EXPLODE
104 aDlg = new EntityGUI_SubShapeDlg( getGeometryGUI(), parent );
107 case GEOMOp::OpFeatureDetect: // FEATURE DETECTION
108 aDlg = new EntityGUI_FeatureDetectorDlg( getGeometryGUI(), parent );
111 case GEOMOp::OpPictureImport: // IMPORT PICTURE IN VIEWER
112 aDlg = new EntityGUI_PictureImportDlg( getGeometryGUI(), parent );
114 case GEOMOp::OpCreateField: // CREATE FIELD
115 aDlg = new EntityGUI_FieldDlg (getGeometryGUI(), GEOM::GEOM_Field::_nil(), 0,
118 case GEOMOp::OpEditField: // EDIT FIELD
119 case GEOMOp::OpEditFieldPopup:
124 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
126 aSelMgr->selectedObjects(aList);
127 SALOME_ListIteratorOfListIO anIter (aList);
129 GEOM::GEOM_Field_var field;
130 GEOM::GEOM_FieldStep_var step;
132 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
133 if ( aList.Extent() > 0 && study ) {
134 for ( ; anIter.More(); anIter.Next() )
136 Handle(SALOME_InteractiveObject) anIObj = anIter.Value();
137 if ( !anIObj.IsNull() && anIObj->hasEntry() )
138 if ( _PTR(SObject) obj = study->studyDS()->FindObjectID( anIObj->getEntry() ))
140 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( obj );
141 GEOM::GEOM_BaseObject_var bo = GEOM::GEOM_BaseObject::_narrow( corbaObj );
142 GEOM::GEOM_Field_var f;
143 GEOM::GEOM_FieldStep_var s;
144 switch ( bo->GetType() ) {
146 f = GEOM::GEOM_Field::_narrow( corbaObj ); break;
147 case GEOM_FIELD_STEP:
148 step = GEOM::GEOM_FieldStep::_narrow( corbaObj );
149 if ( !step->_is_nil() )
150 f = step->GetField();
157 if ( !field->_is_nil() && !f->_is_equivalent( field ))
159 field = GEOM::GEOM_Field::_nil(); // several field selected
168 if ( !field->_is_nil()) {
170 if ( !step->_is_nil() ) {
171 stepID = step->GetID();
174 GEOM::ListOfLong_var stepIDs = field->GetSteps();
175 if ( stepIDs->length() > 0 )
180 aDlg = new EntityGUI_FieldDlg (getGeometryGUI(), field, stepID, parent);
183 SUIT_MessageBox::warning(parent, tr("WRN_WARNING"), tr("NO_FIELD"));
187 app->putInfo( tr( "GEOM_PRP_COMMAND" ).arg( theCommandID ) );
196 //=================================================================================
197 // function : 0nMousePress()
198 // purpose : [static] manage mouse events
199 //=================================================================================
200 bool EntityGUI::OnMousePress( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
202 QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
204 // Create Point dialog, OCC viewer
206 theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
207 pe->modifiers() != Qt::ControlModifier ) {
211 if ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_SketcherDlg" ) == 0 )
213 EntityGUI_SketcherDlg* aSketcherDlg = (EntityGUI_SketcherDlg*) aDlg;
214 ((OCCViewer_ViewWindow*)theViewWindow)->setSketcherStyle(true);
215 if ( aSketcherDlg->acceptMouseEvent() ) {
216 OCCViewer_Viewer* anOCCViewer =
217 ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
218 Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
221 if ( pe->modifiers() == Qt::ShiftModifier )
222 ic->ShiftSelect(); // Append selection
224 ic->Select(); // New selection
227 if ( ic->MoreSelected() ) {
228 TopoDS_Shape aShape = ic->SelectedShape();
229 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
230 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
233 OCCViewer_ViewPort3d* vp = ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
234 aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
237 Qt::KeyboardModifiers modifiers = pe->modifiers();
238 aSketcherDlg->OnPointSelected( modifiers, aPnt ); // "feed" the point to point construction dialog
239 } // acceptMouseEvent()
242 if ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_FeatureDetectorDlg" ) == 0 )
244 EntityGUI_FeatureDetectorDlg* aCornerDlg = (EntityGUI_FeatureDetectorDlg*) aDlg;
245 if ( aCornerDlg->acceptMouseEvent() ) {
246 OCCViewer_Viewer* anOCCViewer =
247 ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
248 Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
251 ic->Select(); // New selection
255 if ( ic->MoreSelected() )
256 aShape = ic->SelectedShape();
257 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
258 aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
261 OCCViewer_ViewPort3d* vp = ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
262 aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
265 // aCornerDlg->OnPointSelected( aPnt ); // "feed" the point to corner detection dialog
267 // QPoint start = QPoint(pe->x(),pe->y());
268 aCornerDlg->setStartPnt( aPnt );
269 } // acceptMouseEvent()
278 //=================================================================================
279 // function : 0nMouseRelease()
280 // purpose : [static] manage mouse events
281 //=================================================================================
282 bool EntityGUI::OnMouseRelease( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
284 ((OCCViewer_ViewWindow*)theViewWindow)->setSketcherStyle(false);
285 QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
287 if ( aDlg && ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_FeatureDetectorDlg" ) == 0 ) &&
288 theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
289 pe->modifiers() != Qt::ControlModifier )
291 EntityGUI_FeatureDetectorDlg* aCornerDlg = (EntityGUI_FeatureDetectorDlg*) aDlg;
295 if ( aCornerDlg->acceptMouseEvent() )
297 // QPoint end = QPoint(pe->x(),pe->y());
298 OCCViewer_ViewPort3d* vp = ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
299 aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
300 aCornerDlg->setEndPnt( aPnt );
307 //=================================================================================
308 // function : 0nMouseMove()
309 // purpose : [static] manage mouse events
310 //=================================================================================
311 bool EntityGUI::OnMouseMove( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
313 QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
315 if ( aDlg && QString( aDlg->metaObject()->className() ).compare( "EntityGUI_SketcherDlg" ) == 0 &&
316 theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() )
318 EntityGUI_SketcherDlg* aSketcherDlg = (EntityGUI_SketcherDlg*) aDlg;
319 if ( aSketcherDlg->acceptMouseEvent() )
321 OCCViewer_ViewPort3d* vp = ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
322 gp_Pnt aPnt = ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
324 Qt::KeyboardModifiers modifiers = pe->modifiers();
325 if (QApplication::mouseButtons() == Qt::LeftButton )
326 aSketcherDlg->OnPointSelected( modifiers, aPnt, false ); // "feed" the point to point construction dialog
333 //=======================================================================
334 // function : ConvertClickToPoint()
335 // purpose : Returns the point clicked in 3D view
336 //=======================================================================
337 gp_Pnt EntityGUI::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
339 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
340 aView->Eye( XEye, YEye, ZEye );
342 aView->At( XAt, YAt, ZAt );
343 gp_Pnt EyePoint( XEye, YEye, ZEye );
344 gp_Pnt AtPoint( XAt, YAt, ZAt );
346 gp_Vec EyeVector( EyePoint, AtPoint );
347 gp_Dir EyeDir( EyeVector );
349 gp_Pln PlaneOfTheView = gp_Pln( AtPoint, EyeDir );
350 Standard_Real X, Y, Z;
351 aView->Convert( x, y, X, Y, Z );
352 gp_Pnt ConvertedPoint( X, Y, Z );
354 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project( PlaneOfTheView, ConvertedPoint );
355 gp_Pnt ResultPoint = ElSLib::Value( ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView );
359 //=====================================================================================
360 // function : DisplaySimulationShape()
361 // purpose : Displays 'this->mySimulationShape' a pure graphical shape from a TopoDS_Shape
362 //=====================================================================================
363 void EntityGUI::DisplaySimulationShape( const TopoDS_Shape& S1, const TopoDS_Shape& S2 )
365 MESSAGE("EntityGUI::DisplaySimulationShape")
366 SalomeApp_Application* app = getGeometryGUI()->getApp();
369 SUIT_ViewManager* aVM = app->desktop()->activeWindow()->getViewManager();
370 if ( aVM->getType() != OCCViewer_Viewer::Type() )
373 OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)aVM )->getOCCViewer();
374 Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
376 if ( !S1.IsNull() ) {
377 /* erase any previous */
378 #if OCC_VERSION_LARGE <= 0x06060000
379 ic->Erase( mySimulationShape1, Standard_True, Standard_False );
381 ic->Erase( mySimulationShape1, Standard_True );
383 ic->ClearPrs( mySimulationShape1 );
385 mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
386 mySimulationShape1->Set( S1 );
387 mySimulationShape1->SetColor( Quantity_NOC_RED );
389 ic->Deactivate( mySimulationShape1 );
390 ic->Display( mySimulationShape1, Standard_False );
391 mySimulationShape1->UnsetColor();
393 if ( !S2.IsNull() ) {
394 #if OCC_VERSION_LARGE <= 0x06060000
395 ic->Erase( mySimulationShape2, Standard_True, Standard_False );
397 ic->Erase( mySimulationShape2, Standard_True );
399 ic->ClearPrs( mySimulationShape2 );
401 mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
402 mySimulationShape2->Set( S2 );
403 mySimulationShape2->SetColor( Quantity_NOC_VIOLET );
405 ic->Deactivate( mySimulationShape2 );
406 ic->Display( mySimulationShape2, Standard_False );
407 mySimulationShape2->UnsetColor();
409 ic->UpdateCurrentViewer();
411 catch( Standard_Failure ) {
412 MESSAGE( "Exception catched in EntityGUI::DisplaySimulationShape" );
416 //==================================================================================
417 // function : EraseSimulationShape()
418 // purpose : Clears the display of 'mySimulationShape' a pure graphical shape
419 //==================================================================================
420 void EntityGUI::EraseSimulationShape()
422 MESSAGE("EntityGUI::EraseSimulationShape")
423 SalomeApp_Application* app = getGeometryGUI()->getApp();
426 // get all view windows at the desktop
427 QList<SUIT_ViewWindow*> aWndLst = app->desktop()->windows();
428 //get all view windows, which belong to the active study
429 QList<SUIT_ViewWindow*> aWndLstAS;
432 QListIterator<SUIT_ViewWindow*> itWL( aWndLst );
433 while ( itWL.hasNext() && ( vw = itWL.next() ) )
434 if ( vw->getViewManager()->study() == app->activeStudy() )
435 aWndLstAS.append( vw );
437 QListIterator<SUIT_ViewWindow*> itWLAS( aWndLstAS );
438 while ( itWLAS.hasNext() && ( vw = itWLAS.next() ) ) {
439 if ( vw->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
440 OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)( vw->getViewManager() ) )->getOCCViewer();
441 Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
442 #if OCC_VERSION_LARGE <= 0x06060000
443 ic->Erase( mySimulationShape1, Standard_True, Standard_False );
444 ic->Erase( mySimulationShape2, Standard_True, Standard_False );
446 ic->Erase( mySimulationShape1, Standard_True );
447 ic->Erase( mySimulationShape2, Standard_True );
449 ic->ClearPrs( mySimulationShape1 );
450 ic->ClearPrs( mySimulationShape2 );
451 ic->UpdateCurrentViewer();
456 //=====================================================================================
457 // function : SObjectExist()
459 //=====================================================================================
460 bool EntityGUI::SObjectExist( const _PTR(SObject)& theFatherObject, const char* IOR )
462 SalomeApp_Application* app = getGeometryGUI()->getApp();
463 if ( !app ) return false;
464 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
465 if ( !appStudy ) return false;
467 _PTR(Study) aStudy = appStudy->studyDS();
468 _PTR(ChildIterator) it ( aStudy->NewChildIterator( theFatherObject ) );
470 _PTR(GenericAttribute) anAttr;
471 for ( ; it->More();it->Next() ) {
472 _PTR(SObject) SO ( it->Value() );
473 if ( SO->FindAttribute( anAttr, "AttributeIOR" ) ) {
474 _PTR(AttributeIOR) anIOR ( anAttr );
475 if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
478 if ( SO->ReferencedObject( RefSO ) ) {
479 if ( RefSO->FindAttribute( anAttr, "AttributeIOR" ) ) {
480 _PTR(AttributeIOR) anIOR ( anAttr );
481 if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
490 //=====================================================================================
492 //=====================================================================================
496 __declspec( dllexport )
498 GEOMGUI* GetLibGUI( GeometryGUI* parent )
500 return new EntityGUI( parent );