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