]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_WorkingPlaneDlg.cxx
Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[modules/geom.git] / src / BasicGUI / BasicGUI_WorkingPlaneDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  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.
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 // File   : BasicGUI_WorkingPlaneDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "BasicGUI_WorkingPlaneDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 // OCCT Includes
38 #include <BRep_Tool.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopExp.hxx>
42 #include <gp_Pnt.hxx>
43 #include <gp_Dir.hxx>
44 #include <TColStd_MapOfInteger.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TColStd_IndexedMapOfInteger.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48
49 // QT Includes
50 #include <QCheckBox>
51
52 #include <GEOMImpl_Types.hxx>
53
54 //=================================================================================
55 // class    : BasicGUI_WorkingPlaneDlg()
56 // purpose  : Constructs a BasicGUI_WorkingPlaneDlg which is a child of 'parent', with the
57 //            name 'name' and widget flags set to 'f'.
58 //            The dialog will by default be modeless, unless you set 'modal' to
59 //            TRUE to construct a modal dialog.
60 //=================================================================================
61 BasicGUI_WorkingPlaneDlg::BasicGUI_WorkingPlaneDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
62                                                     bool modal, Qt::WindowFlags fl )
63   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
64 {
65   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
66   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_WPLANE_FACE" ) ) );
67   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_WPLANE_VECTOR" ) ) );
68   QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_WPLANE_ORIGIN" ) ) );
69
70   setWindowTitle( tr( "GEOM_WPLANE_TITLE" ) );
71
72   /***************************************************************/
73   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_WPLANE" ) );
74   mainFrame()->RadioButton1->setIcon( image1 );
75   mainFrame()->RadioButton2->setIcon( image2 );
76   mainFrame()->RadioButton3->setIcon( image3 );
77
78   Group1 = new DlgRef_1Sel( centralWidget() );
79
80   Group1->GroupBox1->setTitle( tr( "GEOM_WPLANE_FACE" ) );
81   Group1->TextLabel1->setText( tr( "GEOM_SELECTION" ) );
82   Group1->PushButton1->setIcon( image0 );
83   Group1->LineEdit1->setReadOnly( true );
84
85   Group2 = new DlgRef_2Sel( centralWidget() );
86
87   Group2->GroupBox1->setTitle( tr( "GEOM_WPLANE_VECTOR" ) );
88   Group2->TextLabel1->setText( tr( "GEOM_WPLANE_VX" ) );
89   Group2->TextLabel2->setText( tr( "GEOM_WPLANE_VZ" ) );
90   Group2->PushButton1->setIcon( image0 );
91   Group2->PushButton2->setIcon( image0 );
92   Group2->LineEdit1->setReadOnly( true );
93   Group2->LineEdit2->setReadOnly( true );
94
95   Group3 = new DlgRef_3Check( centralWidget() );
96
97   Group3->GroupBox1->setTitle( tr( "GEOM_WPLANE_ORIGIN" ) );
98   Group3->RadioButton1->setText( tr( "GEOM_WPLANE_OXY" ) );
99   Group3->RadioButton2->setText( tr( "GEOM_WPLANE_OYZ" ) );
100   Group3->RadioButton3->setText( tr( "GEOM_WPLANE_OZX" ) );
101
102   /***************************************************************/
103   QGroupBox* aReverseGroupBox = new QGroupBox( centralWidget() );
104   QHBoxLayout* aReverseGroupBoxLayout = new QHBoxLayout( aReverseGroupBox );
105   aReverseGroupBoxLayout->setMargin( 9 ); 
106   aReverseGroupBoxLayout->setSpacing( 6 );
107     
108   myReverseCB = new QCheckBox( aReverseGroupBox );
109   myReverseCB->setText( tr( "GEOM_REVERSE_PLANE" ) );
110   aReverseGroupBoxLayout->addWidget( myReverseCB );
111     
112   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
113   layout->setMargin( 0 ); layout->setSpacing( 6 );
114   layout->addWidget( Group1 );
115   layout->addWidget( Group2 );
116   layout->addWidget( Group3 );
117   layout->addWidget( aReverseGroupBox );
118   
119   setHelpFileName( "create_wplane_page.html" );
120
121   Init();
122 }
123
124 //=================================================================================
125 // function : ~BasicGUI_WorkingPlaneDlg()
126 // purpose  : Destroys the object and frees any allocated resources
127 //=================================================================================
128 BasicGUI_WorkingPlaneDlg::~BasicGUI_WorkingPlaneDlg()
129 {
130 }
131
132 //=================================================================================
133 // function : Init()
134 // purpose  :
135 //=================================================================================
136 void BasicGUI_WorkingPlaneDlg::Init()
137 {
138   /* init variables */
139   myEditCurrentArgument = Group1->LineEdit1;
140   myWPlane = myGeomGUI->GetWorkingPlane();
141
142   // myGeomGUI->SetState( 0 );
143
144   myFace  = GEOM::GEOM_Object::_nil();
145   myVectX = GEOM::GEOM_Object::_nil();
146   myVectZ = GEOM::GEOM_Object::_nil();
147
148   aOriginType = 1;
149
150   /* Filter definition */
151   globalSelection( GEOM_PLANE );
152
153    /* signals and slots connections */
154   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
155   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
156
157   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
158   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
159
160   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
161
162   connect( Group1->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
163   connect( Group1->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
164   connect( Group2->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
165   connect( Group2->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
166   connect( Group2->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
167   connect( Group2->PushButton2, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
168
169   connect( Group3->GroupBox1, SIGNAL( clicked( int ) ), this, SLOT( GroupClicked( int ) ) );
170
171   connect( myReverseCB, SIGNAL( clicked() ), this, SLOT( onReverse() ) );
172
173   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
174            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
175
176   initName( tr( "GEOM_WPLANE" ) );
177
178   ConstructorsClicked( 0 );
179 }
180
181 //=================================================================================
182 // function : ConstructorsClicked()
183 // purpose  : Radio button management
184 //=================================================================================
185 void BasicGUI_WorkingPlaneDlg::ConstructorsClicked( int constructorId )
186 {
187   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
188
189   disconnect( aSelMgr, 0, this, 0 );
190
191   switch ( constructorId ) {
192   case 0:
193     {
194       //globalSelection( GEOM_PLANE );
195       TColStd_MapOfInteger aMap;
196       aMap.Add( GEOM_PLANE );
197       aMap.Add( GEOM_MARKER );
198       globalSelection( aMap );
199       
200       Group2->hide();
201       Group3->hide();
202       Group1->show();
203       
204       myEditCurrentArgument = Group1->LineEdit1;
205       Group1->LineEdit1->setText( "" );
206       myFace = GEOM::GEOM_Object::_nil();
207       
208       connect( aSelMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
209       break;
210     }
211   case 1:
212     {
213       //globalSelection( GEOM_LINE );
214       GEOM::GEOM_Object_var anObj;
215       localSelection( anObj, TopAbs_EDGE );
216       
217       Group1->hide();
218       Group3->hide();
219       Group2->show();
220       
221       myEditCurrentArgument = Group2->LineEdit1;
222       Group2->LineEdit1->setText( "" );
223       Group2->LineEdit2->setText( "" );
224       myVectX = GEOM::GEOM_Object::_nil();
225       myVectZ = GEOM::GEOM_Object::_nil();
226       
227       connect( aSelMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
228       break;
229     }
230   case 2:
231     {
232       Group1->hide();
233       Group2->hide();
234       Group3->show();
235       
236       Group3->RadioButton1->setChecked( true );
237       aOriginType = 1;
238       break;
239     }
240   }
241   
242   qApp->processEvents();
243   updateGeometry();
244   resize( minimumSize() );
245
246   updateWPlane();
247 }
248
249 //=================================================================================
250 // function : GroupClicked()
251 // purpose  : Radio button management
252 //=================================================================================
253 void BasicGUI_WorkingPlaneDlg::GroupClicked( int groupId )
254 {
255   aOriginType = groupId;
256   updateWPlane();
257 }
258
259 //=================================================================================
260 // function : ClickOnOk()
261 // purpose  :
262 //=================================================================================
263 void BasicGUI_WorkingPlaneDlg::ClickOnOk()
264 {
265   if ( ClickOnApply() )
266     ClickOnCancel();
267 }
268
269 //=================================================================================
270 // function : ClickOnApply()
271 // purpose  :
272 //=================================================================================
273 bool BasicGUI_WorkingPlaneDlg::ClickOnApply()
274 {
275   buttonApply()->setFocus();
276   myGeomGUI->application()->putInfo( "" );
277  
278   if ( updateWPlane( false ) ) {
279     myGeomGUI->SetWorkingPlane( myWPlane );
280     myGeomGUI->ActiveWorkingPlane();
281   }
282   
283   return true;
284 }
285
286 //=================================================================================
287 // function : SelectionIntoArgument()
288 // purpose  : Called when selection as changed or other case
289 //=================================================================================
290 void BasicGUI_WorkingPlaneDlg::SelectionIntoArgument()
291 {
292   myEditCurrentArgument->setText( "" );
293   QString aName;
294
295   const int id = getConstructorId();
296   if ( IObjectCount() != 1 ) {
297     if ( id == 0 )
298       myFace = GEOM::GEOM_Object::_nil();
299     else if ( id == 1 ) {
300       if ( myEditCurrentArgument == Group2->LineEdit1 )
301         myVectX = GEOM::GEOM_Object::_nil();
302       else if ( myEditCurrentArgument == Group2->LineEdit2 )
303         myVectZ = GEOM::GEOM_Object::_nil();
304     }
305     return;
306   }
307
308   // nbSel == 1
309   Standard_Boolean aRes = Standard_False;
310   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
311
312   if ( !aRes || CORBA::is_nil( aSelectedObject ) )
313     return;
314
315   aName = GEOMBase::GetName(aSelectedObject);
316
317   if ( myEditCurrentArgument == Group1->LineEdit1 )
318     myFace = aSelectedObject;
319   else if ( myEditCurrentArgument == Group2->LineEdit1 || 
320             myEditCurrentArgument == Group2->LineEdit2 ) {
321     if ( aRes && !aSelectedObject->_is_nil() ) {
322       TopoDS_Shape aShape;
323       if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
324         LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
325         TColStd_IndexedMapOfInteger aMap;
326         aSelMgr->GetIndexes( firstIObject(), aMap );
327         if ( aMap.Extent() == 1 ) {
328           int anIndex = aMap( 1 );
329           aName = aName + ":edge_" + QString::number( anIndex );
330
331           GEOM::GEOM_IShapesOperations_var aShapesOp =
332             getGeomEngine()->GetIShapesOperations( getStudyId() );
333           if ( myEditCurrentArgument == Group2->LineEdit1 )
334             myVectX = aShapesOp->GetSubShape( aSelectedObject, anIndex );
335           else
336             myVectZ = aShapesOp->GetSubShape( aSelectedObject, anIndex );
337         }
338         else {
339           if ( myEditCurrentArgument == Group2->LineEdit1 )
340             myVectX = aSelectedObject;
341           else
342             myVectZ = aSelectedObject;
343         }
344         aSelMgr->clearSelected();
345       }
346     }
347   }
348
349   myEditCurrentArgument->setText( aName );
350   
351   updateWPlane();
352 }
353
354
355 //=================================================================================
356 // function : SetEditCurrentArgument()
357 // purpose  :
358 //=================================================================================
359 void BasicGUI_WorkingPlaneDlg::SetEditCurrentArgument()
360 {
361   QPushButton* send = (QPushButton*)sender();
362
363   if ( send == Group1->PushButton1 ) {
364     myEditCurrentArgument = Group1->LineEdit1;
365     globalSelection( GEOM_PLANE );
366   }
367   else if ( send == Group2->PushButton1 ) {
368     myEditCurrentArgument = Group2->LineEdit1;
369     GEOM::GEOM_Object_var anObj;
370     localSelection( anObj, TopAbs_EDGE );
371   }
372   else if ( send == Group2->PushButton2 ) {
373     myEditCurrentArgument = Group2->LineEdit2;
374     GEOM::GEOM_Object_var anObj;
375     localSelection( anObj, TopAbs_EDGE );
376   }
377
378   myEditCurrentArgument->setFocus();
379   SelectionIntoArgument();
380 }
381
382
383 //=================================================================================
384 // function : LineEditReturnPressed()
385 // purpose  :
386 //=================================================================================
387 void BasicGUI_WorkingPlaneDlg::LineEditReturnPressed()
388 {
389   QLineEdit* send = (QLineEdit*)sender();
390   if ( send == Group1->LineEdit1 || send == Group2->LineEdit1 || send == Group2->LineEdit2 ) {
391     myEditCurrentArgument = send;
392     GEOMBase_Skeleton::LineEditReturnPressed();
393   }
394 }
395
396
397 //=================================================================================
398 // function : onReverse()
399 // purpose  :
400 //=================================================================================
401 void BasicGUI_WorkingPlaneDlg::onReverse()
402 {
403   updateWPlane();
404 }
405
406
407 //=================================================================================
408 // function : ActivateThisDialog()
409 // purpose  :
410 //=================================================================================
411 void BasicGUI_WorkingPlaneDlg::ActivateThisDialog( )
412 {
413   GEOMBase_Skeleton::ActivateThisDialog();
414   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication( ) ) )->selectionMgr(),
415            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
416
417   ConstructorsClicked( getConstructorId() );
418 }
419
420
421 //=================================================================================
422 // function : DeactivateActiveDialog()
423 // purpose  : public slot to deactivate if active
424 //=================================================================================
425 void BasicGUI_WorkingPlaneDlg::DeactivateActiveDialog()
426 {
427   // myGeomGUI->SetState( -1 );
428   GEOMBase_Skeleton::DeactivateActiveDialog();
429 }
430
431 //=================================================================================
432 // function : enterEvent()
433 // purpose  :
434 //=================================================================================
435 void BasicGUI_WorkingPlaneDlg::enterEvent( QEvent* )
436 {
437   if ( !mainFrame()->GroupConstructors->isEnabled() )
438     ActivateThisDialog();
439 }
440
441 //=================================================================================
442 // function : updateWPlane
443 // purpose  :
444 //=================================================================================
445 bool BasicGUI_WorkingPlaneDlg::updateWPlane( const bool showPreview )
446 {
447   erasePreview();
448   
449   const int id = getConstructorId();
450
451   if ( id == 0 ) { // by planar face selection
452     if ( CORBA::is_nil( myFace ) ) {
453       if ( !showPreview )
454         showError( "Face has to be selected" );
455       return false;
456     }
457
458     // PAL12781: set center of WPL to face's center of mass
459     // like it is done for LCS creation
460     CORBA::Double Ox, Oy, Oz,   Zx, Zy, Zz,   Xx, Xy, Xz;
461     Ox = Oy = Oz = Zx = Zy = Xy = Xz = 0.;
462     Zz = Xx = 1.;
463
464     GEOM::GEOM_IMeasureOperations_ptr aMeasureOp =
465       myGeomGUI->GetGeomGen()->GetIMeasureOperations( getStudyId() );
466     aMeasureOp->GetPosition( myFace, Ox, Oy, Oz, Zx, Zy, Zz, Xx, Xy, Xz);
467
468     if ( aMeasureOp->IsDone() ) {
469       gp_Pnt aPnt ( Ox, Oy, Oz );
470       gp_Dir aDirN ( Zx, Zy, Zz );
471       gp_Dir aDirX ( Xx, Xy, Xz );
472       myWPlane = gp_Ax3( aPnt, aDirN, aDirX );
473     } 
474     else {
475       if ( !showPreview )
476         showError( "Wrong shape selected (has to be a planar face)" );
477       return false;
478     }
479   } 
480   else if ( id == 1 ) { // by two vectors (Ox & Oz)
481     if ( CORBA::is_nil( myVectX ) || CORBA::is_nil( myVectZ ) ) {
482       if ( !showPreview )
483         showError( "Two vectors have to be selected" );
484       return false;
485     }
486     
487     TopoDS_Edge aVectX, aVectZ;
488     TopoDS_Vertex VX1, VX2, VZ1, VZ2;
489     gp_Vec aVX, aVZ;
490
491     if ( !GEOMBase::GetShape( myVectX, aVectX, TopAbs_EDGE ) ||
492          !GEOMBase::GetShape( myVectZ, aVectZ, TopAbs_EDGE ) ) {
493       if ( !showPreview )
494         showError( "Wrong shape selected (two vectors(edges) have to be selected)" );
495       return false;
496     }
497
498     TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
499     TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
500
501     if ( VX1.IsNull() || VX2.IsNull() ) {
502       if ( !showPreview )
503         showError( "Bad OX vector" );
504       return false;
505     }
506     if ( VZ1.IsNull() || VZ2.IsNull() ) {
507       if ( !showPreview )
508         showError( "Bad OZ vector" );
509       return false;
510     }
511
512     aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
513     aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
514
515     if ( aVX.Magnitude() < Precision::Confusion() ) {
516       if ( !showPreview )
517         showError( "Bad OX vector" );
518       return false;
519     }
520     if ( aVZ.Magnitude() < Precision::Confusion() ) {
521       if ( !showPreview )
522         showError( "Bad OZ vector" );
523       return false;
524     }
525
526     gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
527     gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
528
529     if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) ) {
530       if ( !showPreview )
531         showError( "Parallel vectors selected" );
532       return false;
533     }
534
535     myWPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
536
537   } 
538   else if ( id == 2 ) { // by selection from standard (OXY or OYZ, or OZX)
539     gp_Ax2 anAx2;
540
541     if      ( aOriginType == 1 ) anAx2 = gp::XOY();
542     else if ( aOriginType == 2 ) anAx2 = gp::YOZ();
543     else if ( aOriginType == 0 ) anAx2 = gp::ZOX();
544
545     myWPlane = gp_Ax3( anAx2 );
546
547   } 
548   else {
549     return false;
550   }
551   
552   if ( myReverseCB->isChecked() ) {
553     myWPlane.YReverse();
554     myWPlane.ZReverse();
555   }
556   
557   if ( showPreview ) {
558     GEOM::GEOM_IBasicOperations_var aBasicOp = getGeomEngine()->GetIBasicOperations( getStudyId() );
559     GEOM::GEOM_Object_var anObj = aBasicOp->MakeMarker( myWPlane.Location().X(),   myWPlane.Location().Y(),   myWPlane.Location().Z(),
560                                                         myWPlane.XDirection().X(), myWPlane.XDirection().Y(), myWPlane.XDirection().Z(),
561                                                         myWPlane.YDirection().X(), myWPlane.YDirection().Y(), myWPlane.YDirection().Z() );
562     displayPreview( anObj );
563   }
564   
565   return true;
566 }