Salome HOME
55d92893026e5cdc5bb0dd81741e2639f9eab2b4
[modules/geom.git] / src / EntityGUI / EntityGUI.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 #include "GEOMUtils.hxx"
32
33 #include <LightApp_SelectionMgr.h>
34 #include <OCCViewer_ViewManager.h>
35 #include <OCCViewer_ViewModel.h>
36 #include <OCCViewer_ViewPort3d.h>
37 #include <OCCViewer_ViewWindow.h>
38 #include <SALOME_ListIO.hxx>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_Session.h>
42 #include <SUIT_ViewWindow.h>
43 #include <SalomeApp_Application.h>
44 #include <SalomeApp_Study.h>
45
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS.hxx>
48 #include <BRep_Tool.hxx>
49 #include <ProjLib.hxx>
50 #include <ElSLib.hxx>
51
52 #include <QMouseEvent>
53 #include <QApplication>
54
55 #include "EntityGUI_SketcherDlg.h"        // Sketcher
56 #include "EntityGUI_3DSketcherDlg.h"      // Sketcher
57 #include "EntityGUI_IsolineDlg.h"         // Isoline
58 #include "EntityGUI_SurfFromFaceDlg.h"    // Surface From Face
59 #include "EntityGUI_SubShapeDlg.h"        // Method SUBSHAPE
60 #include "EntityGUI_FeatureDetectorDlg.h" // Feature Detection
61 #include "EntityGUI_PictureImportDlg.h"   // Import Picture in viewer
62 #include "EntityGUI_FieldDlg.h"           // Create/Edit Field
63 #include "EntityGUI_PolylineDlg.h"        // Create/Edit 2d polyline
64
65 #include "GEOMImpl_Types.hxx"
66
67
68 //=======================================================================
69 // function : EntityGUI()
70 // purpose  : Constructor
71 //=======================================================================
72 EntityGUI::EntityGUI( GeometryGUI* parent ) :  GEOMGUI( parent )
73 {
74   mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
75   mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
76 }
77
78 //=======================================================================
79 // function : ~EntityGUI()
80 // purpose  : Destructor
81 //=======================================================================
82 EntityGUI::~EntityGUI()
83 {
84 }
85
86
87 //=======================================================================
88 // function : OnGUIEvent()
89 // purpose  : 
90 //=======================================================================
91 bool EntityGUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
92 {
93   SalomeApp_Application* app = getGeometryGUI()->getApp();
94   if ( !app ) return false;
95
96   getGeometryGUI()->EmitSignalDeactivateDialog();
97   QDialog* aDlg = NULL;
98
99   switch ( theCommandID ) {
100   case GEOMOp::Op2dSketcher: // 2D SKETCHER
101     getGeometryGUI()->ActiveWorkingPlane();
102     aDlg = new EntityGUI_SketcherDlg( getGeometryGUI(), parent );
103     break;
104   case GEOMOp::Op3dSketcher: // 3D SKETCHER
105     aDlg = new EntityGUI_3DSketcherDlg( getGeometryGUI(), parent );
106     break;
107   case GEOMOp::OpIsoline:    // ISOLINE
108     aDlg = new EntityGUI_IsolineDlg( getGeometryGUI(), parent );
109     break;
110   case GEOMOp::OpSurfaceFromFace:    // SURFACE FROM FACE
111     aDlg = new EntityGUI_SurfFromFaceDlg( getGeometryGUI(), parent );
112     break;
113   case GEOMOp::OpExplode:    // EXPLODE
114     aDlg = new EntityGUI_SubShapeDlg( getGeometryGUI(), parent );
115     break;
116 #ifdef WITH_OPENCV
117   case GEOMOp::OpFeatureDetect:    // FEATURE DETECTION
118     aDlg = new EntityGUI_FeatureDetectorDlg( getGeometryGUI(), parent );
119     break;
120 #endif
121   case GEOMOp::OpPictureImport:    // IMPORT PICTURE IN VIEWER
122     aDlg = new EntityGUI_PictureImportDlg( getGeometryGUI(), parent );
123     break;
124   case GEOMOp::OpCreateField: // CREATE FIELD
125     aDlg = new EntityGUI_FieldDlg (getGeometryGUI(), GEOM::GEOM_Field::_nil(), 0,
126                                    parent);
127     break;
128   case GEOMOp::OpEditField: // EDIT FIELD
129   case GEOMOp::OpEditFieldPopup:
130   {
131     SALOME_ListIO aList;
132     aList.Clear();
133
134     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
135     if (aSelMgr)
136       aSelMgr->selectedObjects(aList);
137     SALOME_ListIteratorOfListIO anIter (aList);
138
139     GEOM::GEOM_Field_var     field;
140     GEOM::GEOM_FieldStep_var step;
141
142     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
143     if ( aList.Extent() > 0 && study ) {
144       for ( ; anIter.More(); anIter.Next() )
145       {
146         Handle(SALOME_InteractiveObject) anIObj = anIter.Value();
147         if ( !anIObj.IsNull() && anIObj->hasEntry() )
148           if ( _PTR(SObject) obj = study->studyDS()->FindObjectID( anIObj->getEntry() ))
149           {
150             CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( obj );
151             GEOM::GEOM_BaseObject_var bo = GEOM::GEOM_BaseObject::_narrow( corbaObj );
152             GEOM::GEOM_Field_var     f;
153             GEOM::GEOM_FieldStep_var s;
154             switch ( bo->GetType() ) {
155             case GEOM_FIELD:
156               f = GEOM::GEOM_Field::_narrow( corbaObj ); break;
157             case GEOM_FIELD_STEP:
158               step = GEOM::GEOM_FieldStep::_narrow( corbaObj );
159               if ( !step->_is_nil() )
160                 f = step->GetField();
161               break;
162             default:
163               continue;
164             }
165             if ( !f->_is_nil() )
166             {
167               if ( !field->_is_nil() && !f->_is_equivalent( field ))
168               {
169                 field = GEOM::GEOM_Field::_nil(); // several field selected
170                 break;
171               }
172               field = f;
173             }
174           }
175       }
176     }
177
178     if ( !field->_is_nil()) {
179       int stepID;
180       if ( !step->_is_nil() ) {
181         stepID = step->GetID();
182       }
183       else {
184         GEOM::ListOfLong_var stepIDs = field->GetSteps();
185         if ( stepIDs->length() > 0 )
186           stepID = stepIDs[0];
187         else
188           stepID = 0;
189       }
190       aDlg = new EntityGUI_FieldDlg (getGeometryGUI(), field, stepID, parent); 
191       break;
192     }
193     SUIT_MessageBox::warning(parent, tr("WRN_WARNING"), tr("NO_FIELD"));
194     break;
195   }
196   case GEOMOp::Op2dPolylineEditor: // POLYLINE EDITOR
197     getGeometryGUI()->ActiveWorkingPlane();
198     aDlg = new EntityGUI_PolylineDlg( getGeometryGUI(), parent );
199     break;
200   default:
201     app->putInfo( tr( "GEOM_PRP_COMMAND" ).arg( theCommandID ) );
202     break;
203   }
204   if ( aDlg )
205     aDlg->show();
206
207   return true;
208 }
209
210 //=================================================================================
211 // function : 0nMousePress()
212 // purpose  : [static] manage mouse events
213 //=================================================================================
214 bool EntityGUI::OnMousePress( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
215 {
216   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
217
218   // Create Point dialog, OCC viewer 
219   if ( aDlg && 
220        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
221        pe->modifiers() != Qt::ControlModifier ) {
222     
223     gp_Pnt aPnt;
224   
225     if ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_SketcherDlg" ) == 0 ) 
226     { 
227       EntityGUI_SketcherDlg* aSketcherDlg = (EntityGUI_SketcherDlg*) aDlg;
228       ((OCCViewer_ViewWindow*)theViewWindow)->setSketcherStyle(true);
229       if ( aSketcherDlg->acceptMouseEvent() ) {
230         OCCViewer_Viewer* anOCCViewer =
231           ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
232         Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();
233     
234         ic->InitSelected();
235         if ( pe->modifiers() == Qt::ShiftModifier )
236           ic->ShiftSelect();  // Append selection
237         else
238           ic->Select();       // New selection
239
240         ic->InitSelected();
241         if ( ic->MoreSelected() ) {
242           TopoDS_Shape aShape = ic->SelectedShape();
243           if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
244             aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
245         }
246         else {
247           OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
248           aPnt = GEOMUtils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
249         }
250         
251         Qt::KeyboardModifiers modifiers = pe->modifiers();
252         aSketcherDlg->OnPointSelected( modifiers, aPnt );  // "feed" the point to point construction dialog
253       } // acceptMouseEvent()
254     }
255 #ifdef WITH_OPENCV
256     if (  QString( aDlg->metaObject()->className() ).compare( "EntityGUI_FeatureDetectorDlg" ) == 0 ) 
257     {
258       EntityGUI_FeatureDetectorDlg* aCornerDlg = (EntityGUI_FeatureDetectorDlg*) aDlg;
259       if ( aCornerDlg->acceptMouseEvent() ) {
260         OCCViewer_Viewer* anOCCViewer =
261           ( (OCCViewer_ViewManager*)( theViewWindow->getViewManager() ) )->getOCCViewer();
262         Handle(AIS_InteractiveContext) ic = anOCCViewer->getAISContext();   
263
264         ic->InitSelected();
265         ic->Select();       // New selection
266
267         ic->InitSelected();
268         TopoDS_Shape aShape;
269         if ( ic->MoreSelected() ) 
270           aShape = ic->SelectedShape();
271         if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
272             aPnt = BRep_Tool::Pnt( TopoDS::Vertex( ic->SelectedShape() ) );
273         else 
274         {
275           OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
276           aPnt = GEOMUtils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
277         }
278         
279 //         aCornerDlg->OnPointSelected( aPnt );  // "feed" the point to corner detection dialog
280         
281 //         QPoint start = QPoint(pe->x(),pe->y());
282         aCornerDlg->setStartPnt( aPnt );
283       } // acceptMouseEvent()
284       
285     }
286 #endif
287   }
288
289   return false;
290 }
291
292 //=================================================================================
293 // function : 0nMouseRelease()
294 // purpose  : [static] manage mouse events
295 //=================================================================================
296 bool EntityGUI::OnMouseRelease( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
297 {
298   ((OCCViewer_ViewWindow*)theViewWindow)->setSketcherStyle(false);
299   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
300 #ifdef WITH_OPENCV
301   if ( aDlg && ( QString( aDlg->metaObject()->className() ).compare( "EntityGUI_FeatureDetectorDlg" ) == 0 ) &&
302        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() &&
303        pe->modifiers() != Qt::ControlModifier ) 
304   {   
305     EntityGUI_FeatureDetectorDlg* aCornerDlg = (EntityGUI_FeatureDetectorDlg*) aDlg;
306    
307     gp_Pnt aPnt; 
308       
309     if ( aCornerDlg->acceptMouseEvent() )
310     {
311 //       QPoint end = QPoint(pe->x(),pe->y());
312       OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
313       aPnt = GEOMUtils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
314       aCornerDlg->setEndPnt( aPnt );
315     }    
316   }
317 #endif
318   return false;
319 }
320
321 //=================================================================================
322 // function : 0nMouseMove()
323 // purpose  : [static] manage mouse events
324 //=================================================================================
325 bool EntityGUI::OnMouseMove( QMouseEvent* pe, SUIT_Desktop* parent, SUIT_ViewWindow* theViewWindow )
326 {
327   QDialog* aDlg = getGeometryGUI()->GetActiveDialogBox();
328   
329   if ( aDlg && QString( aDlg->metaObject()->className() ).compare( "EntityGUI_SketcherDlg" ) == 0 &&
330        theViewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) 
331   { 
332     EntityGUI_SketcherDlg* aSketcherDlg = (EntityGUI_SketcherDlg*) aDlg;
333     if ( aSketcherDlg->acceptMouseEvent() ) 
334     {    
335       OCCViewer_ViewPort3d* vp =  ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
336       gp_Pnt aPnt = GEOMUtils::ConvertClickToPoint( pe->x(), pe->y(), vp->getView() );
337   
338       Qt::KeyboardModifiers modifiers = pe->modifiers();
339       if (QApplication::mouseButtons() == Qt::LeftButton )
340         aSketcherDlg->OnPointSelected( modifiers, aPnt, false );  // "feed" the point to point construction dialog
341     }
342   }
343     
344   return false;
345 }
346
347 //=====================================================================================
348 // function : DisplaySimulationShape() 
349 // purpose  : Displays 'this->mySimulationShape' a pure graphical shape from a TopoDS_Shape
350 //=====================================================================================
351 void EntityGUI::DisplaySimulationShape( const TopoDS_Shape& S1, const TopoDS_Shape& S2 ) 
352 {
353   MESSAGE("EntityGUI::DisplaySimulationShape")
354   SalomeApp_Application* app = getGeometryGUI()->getApp();
355   if ( !app ) return;
356
357   SUIT_ViewManager* aVM = app->desktop()->activeWindow()->getViewManager();
358   if ( aVM->getType() != OCCViewer_Viewer::Type() )
359     return;
360
361   OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)aVM )->getOCCViewer();
362   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
363   try {
364     if ( !S1.IsNull() ) {
365       /* erase any previous */
366       ic->Erase( mySimulationShape1, Standard_True );
367       ic->ClearPrs( mySimulationShape1 );
368
369       mySimulationShape1 = new AIS_Shape( TopoDS_Shape() );
370       mySimulationShape1->Set( S1 );
371       mySimulationShape1->SetColor( Quantity_NOC_RED );
372
373       ic->Deactivate( mySimulationShape1 );
374       ic->Display( mySimulationShape1, Standard_False );
375       mySimulationShape1->UnsetColor();
376     }
377     if ( !S2.IsNull() ) {
378       ic->Erase( mySimulationShape2, Standard_True );
379       ic->ClearPrs( mySimulationShape2 );
380
381       mySimulationShape2 = new AIS_Shape( TopoDS_Shape() );
382       mySimulationShape2->Set( S2 );
383       mySimulationShape2->SetColor( Quantity_NOC_VIOLET );
384
385       ic->Deactivate( mySimulationShape2 );
386       ic->Display( mySimulationShape2, Standard_False );
387       mySimulationShape2->UnsetColor();
388     }
389     ic->UpdateCurrentViewer();
390   }
391   catch( Standard_Failure ) {
392     MESSAGE( "Exception catched in EntityGUI::DisplaySimulationShape" );
393   } 
394 }
395
396 //==================================================================================
397 // function : EraseSimulationShape()
398 // purpose  : Clears the display of 'mySimulationShape' a pure graphical shape
399 //==================================================================================
400 void EntityGUI::EraseSimulationShape()
401 {
402   MESSAGE("EntityGUI::EraseSimulationShape")
403   SalomeApp_Application* app = getGeometryGUI()->getApp();
404   if ( !app ) return;
405
406   // get all view windows at the desktop
407   QList<SUIT_ViewWindow*> aWndLst = app->desktop()->windows();
408   //get all view windows, which belong to the active study
409   QList<SUIT_ViewWindow*> aWndLstAS;
410   SUIT_ViewWindow* vw;
411
412   QListIterator<SUIT_ViewWindow*> itWL( aWndLst );
413   while ( itWL.hasNext() && ( vw = itWL.next() ) )
414     if ( vw->getViewManager()->study() == app->activeStudy() )
415       aWndLstAS.append( vw );
416
417   QListIterator<SUIT_ViewWindow*> itWLAS( aWndLstAS );
418   while ( itWLAS.hasNext() && ( vw = itWLAS.next() ) ) {
419     if ( vw->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
420       OCCViewer_Viewer* v3d = ( (OCCViewer_ViewManager*)( vw->getViewManager() ) )->getOCCViewer();
421       Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
422       ic->Erase( mySimulationShape1, Standard_True );
423       ic->Erase( mySimulationShape2, Standard_True );
424       ic->ClearPrs( mySimulationShape1 );
425       ic->ClearPrs( mySimulationShape2 );
426       ic->UpdateCurrentViewer();
427     } 
428   }
429 }
430
431 //=====================================================================================
432 // function : SObjectExist()
433 // purpose  :
434 //=====================================================================================
435 bool EntityGUI::SObjectExist( const _PTR(SObject)& theFatherObject, const char* IOR )
436 {
437   SalomeApp_Application* app = getGeometryGUI()->getApp();
438   if ( !app ) return false;
439   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
440   if ( !appStudy ) return false;
441
442   _PTR(Study) aStudy = appStudy->studyDS();
443   _PTR(ChildIterator) it ( aStudy->NewChildIterator( theFatherObject ) );
444   _PTR(SObject) RefSO;
445   _PTR(GenericAttribute) anAttr;
446   for ( ; it->More();it->Next() ) {
447     _PTR(SObject) SO ( it->Value() );
448     if ( SO->FindAttribute( anAttr, "AttributeIOR" ) ) {
449       _PTR(AttributeIOR) anIOR ( anAttr  );
450       if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
451         return true;
452     }
453     if ( SO->ReferencedObject( RefSO ) ) {
454       if ( RefSO->FindAttribute( anAttr, "AttributeIOR" ) ) {
455         _PTR(AttributeIOR) anIOR ( anAttr );
456         if ( strcmp( anIOR->Value().c_str(), IOR ) == 0 )
457           return true;
458       }
459     }
460   }
461   return false;
462 }
463
464
465 //=====================================================================================
466 // EXPORTED METHODS
467 //=====================================================================================
468 extern "C"
469 {
470 #ifdef WIN32
471   __declspec( dllexport )
472 #endif
473   GEOMGUI* GetLibGUI( GeometryGUI* parent )
474   {
475     return new EntityGUI( parent );
476   }
477 }