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