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