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