Salome HOME
IPAL19834: Incorrect result after make Partition of 2 objects. A fix by PKV.
[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   setConstructorId( 2 ); // simplest constructor
179   ConstructorsClicked( 2 );
180 }
181
182 //=================================================================================
183 // function : ConstructorsClicked()
184 // purpose  : Radio button management
185 //=================================================================================
186 void BasicGUI_WorkingPlaneDlg::ConstructorsClicked( int constructorId )
187 {
188   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
189
190   disconnect( aSelMgr, 0, this, 0 );
191
192   switch ( constructorId ) {
193   case 0:
194     {
195       //globalSelection( GEOM_PLANE );
196       TColStd_MapOfInteger aMap;
197       aMap.Add( GEOM_PLANE );
198       aMap.Add( GEOM_MARKER );
199       globalSelection( aMap );
200
201       Group2->hide();
202       Group3->hide();
203       Group1->show();
204
205       myEditCurrentArgument = Group1->LineEdit1;
206       Group1->LineEdit1->setText( "" );
207       myFace = GEOM::GEOM_Object::_nil();
208
209       connect( aSelMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
210       break;
211     }
212   case 1:
213     {
214       //globalSelection( GEOM_LINE );
215       GEOM::GEOM_Object_var anObj;
216       localSelection( anObj, TopAbs_EDGE );
217
218       Group1->hide();
219       Group3->hide();
220       Group2->show();
221
222       myEditCurrentArgument = Group2->LineEdit1;
223       Group2->LineEdit1->setText( "" );
224       Group2->LineEdit2->setText( "" );
225       myVectX = GEOM::GEOM_Object::_nil();
226       myVectZ = GEOM::GEOM_Object::_nil();
227
228       connect( aSelMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
229       break;
230     }
231   case 2:
232     {
233       Group1->hide();
234       Group2->hide();
235       Group3->show();
236
237       Group3->RadioButton1->setChecked( true );
238       aOriginType = 1;
239       break;
240     }
241   }
242
243   qApp->processEvents();
244   updateGeometry();
245   resize( minimumSize() );
246
247   updateWPlane();
248 }
249
250 //=================================================================================
251 // function : GroupClicked()
252 // purpose  : Radio button management
253 //=================================================================================
254 void BasicGUI_WorkingPlaneDlg::GroupClicked( int groupId )
255 {
256   aOriginType = groupId;
257   updateWPlane();
258 }
259
260 //=================================================================================
261 // function : ClickOnOk()
262 // purpose  :
263 //=================================================================================
264 void BasicGUI_WorkingPlaneDlg::ClickOnOk()
265 {
266   if ( ClickOnApply() )
267     ClickOnCancel();
268 }
269
270 //=================================================================================
271 // function : ClickOnApply()
272 // purpose  :
273 //=================================================================================
274 bool BasicGUI_WorkingPlaneDlg::ClickOnApply()
275 {
276   buttonApply()->setFocus();
277   myGeomGUI->application()->putInfo( "" );
278
279   if ( updateWPlane( false ) ) {
280     myGeomGUI->SetWorkingPlane( myWPlane );
281     myGeomGUI->ActiveWorkingPlane();
282   }
283
284   return true;
285 }
286
287 //=================================================================================
288 // function : SelectionIntoArgument()
289 // purpose  : Called when selection as changed or other case
290 //=================================================================================
291 void BasicGUI_WorkingPlaneDlg::SelectionIntoArgument()
292 {
293   myEditCurrentArgument->setText( "" );
294   QString aName;
295
296   const int id = getConstructorId();
297   if ( IObjectCount() != 1 ) {
298     if ( id == 0 )
299       myFace = GEOM::GEOM_Object::_nil();
300     else if ( id == 1 ) {
301       if ( myEditCurrentArgument == Group2->LineEdit1 )
302         myVectX = GEOM::GEOM_Object::_nil();
303       else if ( myEditCurrentArgument == Group2->LineEdit2 )
304         myVectZ = GEOM::GEOM_Object::_nil();
305     }
306     return;
307   }
308
309   // nbSel == 1
310   Standard_Boolean aRes = Standard_False;
311   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
312
313   if ( !aRes || CORBA::is_nil( aSelectedObject ) )
314     return;
315
316   aName = GEOMBase::GetName(aSelectedObject);
317
318   if ( myEditCurrentArgument == Group1->LineEdit1 )
319     myFace = aSelectedObject;
320   else if ( myEditCurrentArgument == Group2->LineEdit1 ||
321             myEditCurrentArgument == Group2->LineEdit2 ) {
322     if ( aRes && !aSelectedObject->_is_nil() ) {
323       TopoDS_Shape aShape;
324       if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
325         LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
326         TColStd_IndexedMapOfInteger aMap;
327         aSelMgr->GetIndexes( firstIObject(), aMap );
328         if ( aMap.Extent() == 1 ) {
329           int anIndex = aMap( 1 );
330           aName = aName + ":edge_" + QString::number( anIndex );
331
332           GEOM::GEOM_IShapesOperations_var aShapesOp =
333             getGeomEngine()->GetIShapesOperations( getStudyId() );
334           if ( myEditCurrentArgument == Group2->LineEdit1 )
335             myVectX = aShapesOp->GetSubShape( aSelectedObject, anIndex );
336           else
337             myVectZ = aShapesOp->GetSubShape( aSelectedObject, anIndex );
338         }
339         else {
340           if ( myEditCurrentArgument == Group2->LineEdit1 )
341             myVectX = aSelectedObject;
342           else
343             myVectZ = aSelectedObject;
344         }
345         aSelMgr->clearSelected();
346       }
347     }
348   }
349
350   myEditCurrentArgument->setText( aName );
351
352   updateWPlane();
353 }
354
355
356 //=================================================================================
357 // function : SetEditCurrentArgument()
358 // purpose  :
359 //=================================================================================
360 void BasicGUI_WorkingPlaneDlg::SetEditCurrentArgument()
361 {
362   QPushButton* send = (QPushButton*)sender();
363
364   if ( send == Group1->PushButton1 ) {
365     myEditCurrentArgument = Group1->LineEdit1;
366     globalSelection( GEOM_PLANE );
367   }
368   else if ( send == Group2->PushButton1 ) {
369     myEditCurrentArgument = Group2->LineEdit1;
370     GEOM::GEOM_Object_var anObj;
371     localSelection( anObj, TopAbs_EDGE );
372   }
373   else if ( send == Group2->PushButton2 ) {
374     myEditCurrentArgument = Group2->LineEdit2;
375     GEOM::GEOM_Object_var anObj;
376     localSelection( anObj, TopAbs_EDGE );
377   }
378
379   myEditCurrentArgument->setFocus();
380   SelectionIntoArgument();
381 }
382
383
384 //=================================================================================
385 // function : LineEditReturnPressed()
386 // purpose  :
387 //=================================================================================
388 void BasicGUI_WorkingPlaneDlg::LineEditReturnPressed()
389 {
390   QLineEdit* send = (QLineEdit*)sender();
391   if ( send == Group1->LineEdit1 || send == Group2->LineEdit1 || send == Group2->LineEdit2 ) {
392     myEditCurrentArgument = send;
393     GEOMBase_Skeleton::LineEditReturnPressed();
394   }
395 }
396
397
398 //=================================================================================
399 // function : onReverse()
400 // purpose  :
401 //=================================================================================
402 void BasicGUI_WorkingPlaneDlg::onReverse()
403 {
404   updateWPlane();
405 }
406
407
408 //=================================================================================
409 // function : ActivateThisDialog()
410 // purpose  :
411 //=================================================================================
412 void BasicGUI_WorkingPlaneDlg::ActivateThisDialog( )
413 {
414   GEOMBase_Skeleton::ActivateThisDialog();
415   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication( ) ) )->selectionMgr(),
416            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
417
418   ConstructorsClicked( getConstructorId() );
419 }
420
421
422 //=================================================================================
423 // function : DeactivateActiveDialog()
424 // purpose  : public slot to deactivate if active
425 //=================================================================================
426 void BasicGUI_WorkingPlaneDlg::DeactivateActiveDialog()
427 {
428   // myGeomGUI->SetState( -1 );
429   GEOMBase_Skeleton::DeactivateActiveDialog();
430 }
431
432 //=================================================================================
433 // function : enterEvent()
434 // purpose  :
435 //=================================================================================
436 void BasicGUI_WorkingPlaneDlg::enterEvent( QEvent* )
437 {
438   if ( !mainFrame()->GroupConstructors->isEnabled() )
439     ActivateThisDialog();
440 }
441
442 //=================================================================================
443 // function : updateWPlane
444 // purpose  :
445 //=================================================================================
446 bool BasicGUI_WorkingPlaneDlg::updateWPlane( const bool showPreview )
447 {
448   erasePreview();
449
450   const int id = getConstructorId();
451
452   if ( id == 0 ) { // by planar face selection
453     if ( CORBA::is_nil( myFace ) ) {
454       if ( !showPreview )
455         showError( "Face has to be selected" );
456       return false;
457     }
458
459     // PAL12781: set center of WPL to face's center of mass
460     // like it is done for LCS creation
461     CORBA::Double Ox, Oy, Oz,   Zx, Zy, Zz,   Xx, Xy, Xz;
462     Ox = Oy = Oz = Zx = Zy = Xy = Xz = 0.;
463     Zz = Xx = 1.;
464
465     GEOM::GEOM_IMeasureOperations_ptr aMeasureOp =
466       myGeomGUI->GetGeomGen()->GetIMeasureOperations( getStudyId() );
467     aMeasureOp->GetPosition( myFace, Ox, Oy, Oz, Zx, Zy, Zz, Xx, Xy, Xz);
468
469     if ( aMeasureOp->IsDone() ) {
470       gp_Pnt aPnt ( Ox, Oy, Oz );
471       gp_Dir aDirN ( Zx, Zy, Zz );
472       gp_Dir aDirX ( Xx, Xy, Xz );
473       myWPlane = gp_Ax3( aPnt, aDirN, aDirX );
474     }
475     else {
476       if ( !showPreview )
477         showError( "Wrong shape selected (has to be a planar face)" );
478       return false;
479     }
480   }
481   else if ( id == 1 ) { // by two vectors (Ox & Oz)
482     if ( CORBA::is_nil( myVectX ) || CORBA::is_nil( myVectZ ) ) {
483       if ( !showPreview )
484         showError( "Two vectors have to be selected" );
485       return false;
486     }
487
488     TopoDS_Edge aVectX, aVectZ;
489     TopoDS_Vertex VX1, VX2, VZ1, VZ2;
490     gp_Vec aVX, aVZ;
491
492     if ( !GEOMBase::GetShape( myVectX, aVectX, TopAbs_EDGE ) ||
493          !GEOMBase::GetShape( myVectZ, aVectZ, TopAbs_EDGE ) ) {
494       if ( !showPreview )
495         showError( "Wrong shape selected (two vectors(edges) have to be selected)" );
496       return false;
497     }
498
499     TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
500     TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
501
502     if ( VX1.IsNull() || VX2.IsNull() ) {
503       if ( !showPreview )
504         showError( "Bad OX vector" );
505       return false;
506     }
507     if ( VZ1.IsNull() || VZ2.IsNull() ) {
508       if ( !showPreview )
509         showError( "Bad OZ vector" );
510       return false;
511     }
512
513     aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
514     aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
515
516     if ( aVX.Magnitude() < Precision::Confusion() ) {
517       if ( !showPreview )
518         showError( "Bad OX vector" );
519       return false;
520     }
521     if ( aVZ.Magnitude() < Precision::Confusion() ) {
522       if ( !showPreview )
523         showError( "Bad OZ vector" );
524       return false;
525     }
526
527     gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
528     gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
529
530     if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) ) {
531       if ( !showPreview )
532         showError( "Parallel vectors selected" );
533       return false;
534     }
535
536     myWPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
537
538   }
539   else if ( id == 2 ) { // by selection from standard (OXY or OYZ, or OZX)
540     gp_Ax2 anAx2;
541
542     if      ( aOriginType == 1 ) anAx2 = gp::XOY();
543     else if ( aOriginType == 2 ) anAx2 = gp::YOZ();
544     else if ( aOriginType == 0 ) anAx2 = gp::ZOX();
545
546     myWPlane = gp_Ax3( anAx2 );
547
548   }
549   else {
550     return false;
551   }
552
553   if ( myReverseCB->isChecked() ) {
554     myWPlane.YReverse();
555     myWPlane.ZReverse();
556   }
557
558   if ( showPreview ) {
559     GEOM::GEOM_IBasicOperations_var aBasicOp = getGeomEngine()->GetIBasicOperations( getStudyId() );
560     GEOM::GEOM_Object_var anObj = aBasicOp->MakeMarker
561       ( myWPlane.Location().X(),   myWPlane.Location().Y(),   myWPlane.Location().Z(),
562         myWPlane.XDirection().X(), myWPlane.XDirection().Y(), myWPlane.XDirection().Z(),
563         myWPlane.YDirection().X(), myWPlane.YDirection().Y(), myWPlane.YDirection().Z() );
564     displayPreview( anObj );
565   }
566
567   return true;
568 }