]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_WorkingPlaneDlg.cxx
Salome HOME
NPAL18111: Bad shape build by partition.
[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 //
23 //
24 //  File   : BasicGUI_WorkingPlaneDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27
28 #include "BasicGUI_WorkingPlaneDlg.h"
29
30 #include "GEOMBase.h"
31
32 #include "SUIT_Desktop.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.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopExp.hxx>
43 #include <gp_Pnt.hxx>
44 #include <gp_Dir.hxx>
45 #include <TColStd_MapOfInteger.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TColStd_IndexedMapOfInteger.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49
50 // QT Includes
51 #include <qcheckbox.h>
52 #include <qlabel.h>
53
54 #include "GEOMImpl_Types.hxx"
55
56 using namespace std;
57
58 //=================================================================================
59 // class    : BasicGUI_WorkingPlaneDlg()
60 // purpose  : Constructs a BasicGUI_WorkingPlaneDlg which is a child of 'parent', with the
61 //            name 'name' and widget flags set to 'f'.
62 //            The dialog will by default be modeless, unless you set 'modal' to
63 //            TRUE to construct a modal dialog.
64 //=================================================================================
65 BasicGUI_WorkingPlaneDlg::BasicGUI_WorkingPlaneDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
66                                                    const char* name, bool modal, WFlags fl)
67   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
68                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
69 {
70   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
71   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_FACE")));
72   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_VECTOR")));
73   QPixmap image3(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_ORIGIN")));
74
75   setCaption(tr("GEOM_WPLANE_TITLE"));
76
77   /***************************************************************/
78   GroupConstructors->setTitle(tr("GEOM_WPLANE"));
79   RadioButton1->setPixmap(image1);
80   RadioButton2->setPixmap(image2);
81   RadioButton3->setPixmap(image3);
82
83   Group1 = new DlgRef_1Sel_QTD(this, "Group1");
84   Group1->GroupBox1->setTitle(tr("GEOM_WPLANE_FACE"));
85   Group1->TextLabel1->setText(tr("GEOM_SELECTION"));
86   Group1->PushButton1->setPixmap(image0);
87   Group1->LineEdit1->setReadOnly( true );
88
89   Group2 = new DlgRef_2Sel_QTD(this, "Group2");
90   Group2->GroupBox1->setTitle(tr("GEOM_WPLANE_VECTOR"));
91   Group2->TextLabel1->setText(tr("GEOM_WPLANE_VX"));
92   Group2->TextLabel2->setText(tr("GEOM_WPLANE_VZ"));
93   Group2->PushButton1->setPixmap(image0);
94   Group2->PushButton2->setPixmap(image0);
95   Group2->LineEdit1->setReadOnly( true );
96   Group2->LineEdit2->setReadOnly( true );
97
98   Group3 = new DlgRef_3Check_QTD(this, "Group3");
99   Group3->GroupBox1->setTitle(tr("GEOM_WPLANE_ORIGIN"));
100   Group3->RadioButton1->setText(tr("GEOM_WPLANE_OXY"));
101   Group3->RadioButton2->setText(tr("GEOM_WPLANE_OYZ"));
102   Group3->RadioButton3->setText(tr("GEOM_WPLANE_OZX"));
103
104   Layout1->addWidget(Group1, 1, 0);
105   Layout1->addWidget(Group2, 1, 0);
106   Layout1->addWidget(Group3, 1, 0);
107   /***************************************************************/
108   QGroupBox* aReverseGroupBox = new QGroupBox(this, "aReverseGroupBox");
109   aReverseGroupBox->setTitle(tr(""));
110   aReverseGroupBox->setColumnLayout(1, Qt::Horizontal);
111   aReverseGroupBox->setInsideMargin(10);
112   
113   myReverseCB = new QCheckBox(aReverseGroupBox, "myReverseCB");
114   myReverseCB->setText(tr("GEOM_REVERSE_PLANE"));
115     
116   Layout1->addWidget(aReverseGroupBox, 2, 0);
117   
118
119   setHelpFileName("working_plane.htm");
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(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
155   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
156   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
157   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
158
159   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
160   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
161
162   connect(Group1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
163   connect(Group1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
164
165   connect(Group2->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
166   connect(Group2->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
167   connect(Group2->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
168   connect(Group2->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
169
170   connect(Group3->GroupBox1, SIGNAL(clicked(int)), this, SLOT(GroupClicked(int)));
171
172   connect(myReverseCB, SIGNAL(clicked()), this, SLOT(onReverse()));
173
174   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
175           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
176
177   initName( tr( "GEOM_WPLANE" ) );
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     {
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         resize(0, 0);
204         Group1->show();
205
206         myEditCurrentArgument = Group1->LineEdit1;
207         Group1->LineEdit1->setText("");
208         myFace = GEOM::GEOM_Object::_nil();
209
210         connect(aSelMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
211         break;
212       }
213     case 1:
214       {
215         //globalSelection( GEOM_LINE );
216         GEOM::GEOM_Object_var anObj;
217         localSelection( anObj, TopAbs_EDGE );
218
219         Group1->hide();
220         Group3->hide();
221         resize(0, 0);
222         Group2->show();
223
224         myEditCurrentArgument = Group2->LineEdit1;
225         Group2->LineEdit1->setText("");
226          Group2->LineEdit2->setText("");
227         myVectX = GEOM::GEOM_Object::_nil();
228         myVectZ = GEOM::GEOM_Object::_nil();
229
230         connect(aSelMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
231         break;
232       }
233     case 2:
234       {
235         Group1->hide();
236         Group2->hide();
237         resize(0, 0);
238         Group3->show();
239
240         Group3->RadioButton1->setChecked(true);
241         aOriginType = 1;
242         break;
243       }
244     }
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(tr(""));
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 || myEditCurrentArgument == Group2->LineEdit2)  {
320       if ( aRes && !aSelectedObject->_is_nil() )
321         {
322           TopoDS_Shape aShape;
323           if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
324             {
325               LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
326               TColStd_IndexedMapOfInteger aMap;
327               aSelMgr->GetIndexes( firstIObject(), aMap );
328                 if ( aMap.Extent() == 1 )
329                   {
330                     GEOM::GEOM_IShapesOperations_var aShapesOp =
331                       getGeomEngine()->GetIShapesOperations( getStudyId() );
332                     int anIndex = aMap( 1 );
333                     TopTools_IndexedMapOfShape aShapes;
334                     TopExp::MapShapes( aShape, aShapes );
335                     aShape = aShapes.FindKey( anIndex );
336                     aName = aName + " Edge_" + QString::number( anIndex );
337                     if(myEditCurrentArgument == Group2->LineEdit1)
338                       myVectX = aShapesOp->GetSubShape(aSelectedObject, anIndex);
339                     else
340                       myVectZ = aShapesOp->GetSubShape(aSelectedObject, anIndex);
341                   }
342                 else {
343                   if(myEditCurrentArgument == Group2->LineEdit1)
344                     myVectX = aSelectedObject;
345                   else
346                     myVectZ = aSelectedObject;
347                 }
348                 aSelMgr->clearSelected();
349             }
350         }
351     }
352
353   myEditCurrentArgument->setText( aName );
354   
355   updateWPlane();
356 }
357
358
359 //=================================================================================
360 // function : SetEditCurrentArgument()
361 // purpose  :
362 //=================================================================================
363 void BasicGUI_WorkingPlaneDlg::SetEditCurrentArgument()
364 {
365   QPushButton* send = (QPushButton*)sender();
366
367   if(send == Group1->PushButton1) {
368     myEditCurrentArgument = Group1->LineEdit1;
369     globalSelection( GEOM_PLANE );
370   }
371   else if(send == Group2->PushButton1) {
372     myEditCurrentArgument = Group2->LineEdit1;
373     GEOM::GEOM_Object_var anObj;
374     localSelection( anObj, TopAbs_EDGE );
375   }
376   else if(send == Group2->PushButton2) {
377     myEditCurrentArgument = Group2->LineEdit2;
378     GEOM::GEOM_Object_var anObj;
379     localSelection( anObj, TopAbs_EDGE );
380   }
381
382   myEditCurrentArgument->setFocus();
383   SelectionIntoArgument();
384 }
385
386
387 //=================================================================================
388 // function : LineEditReturnPressed()
389 // purpose  :
390 //=================================================================================
391 void BasicGUI_WorkingPlaneDlg::LineEditReturnPressed()
392 {
393   QLineEdit* send = (QLineEdit*)sender();
394   if(send == Group1->LineEdit1 || send == Group2->LineEdit1 || send == Group2->LineEdit2) {
395     myEditCurrentArgument = send;
396     GEOMBase_Skeleton::LineEditReturnPressed();
397   }
398 }
399
400
401 //=================================================================================
402 // function : onReverse()
403 // purpose  :
404 //=================================================================================
405 void BasicGUI_WorkingPlaneDlg::onReverse()
406 {
407   updateWPlane();
408 }
409
410
411 //=================================================================================
412 // function : ActivateThisDialog()
413 // purpose  :
414 //=================================================================================
415 void BasicGUI_WorkingPlaneDlg::ActivateThisDialog( )
416 {
417   GEOMBase_Skeleton::ActivateThisDialog();
418   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
419           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
420
421   ConstructorsClicked( getConstructorId() );
422 }
423
424
425 //=================================================================================
426 // function : DeactivateActiveDialog()
427 // purpose  : public slot to deactivate if active
428 //=================================================================================
429 void BasicGUI_WorkingPlaneDlg::DeactivateActiveDialog()
430 {
431   // myGeomGUI->SetState( -1 );
432   GEOMBase_Skeleton::DeactivateActiveDialog();
433 }
434
435 //=======================================================================
436 // function : ClickOnCancel()
437 // purpose  :
438 //=======================================================================
439 void BasicGUI_WorkingPlaneDlg::ClickOnCancel()
440 {
441   GEOMBase_Skeleton::ClickOnCancel();
442 }
443
444 //=================================================================================
445 // function : enterEvent()
446 // purpose  :
447 //=================================================================================
448 void BasicGUI_WorkingPlaneDlg::enterEvent(QEvent* e)
449 {
450   if ( !GroupConstructors->isEnabled() )
451     ActivateThisDialog();
452 }
453
454 //=================================================================================
455 // function : closeEvent
456 // purpose  :
457 //=================================================================================
458 void BasicGUI_WorkingPlaneDlg::closeEvent( QCloseEvent* e )
459 {
460   GEOMBase_Skeleton::closeEvent( e );
461 }
462
463
464 //=================================================================================
465 // function : updateWPlane
466 // purpose  :
467 //=================================================================================
468 bool BasicGUI_WorkingPlaneDlg::updateWPlane( const bool showPreview )
469 {
470   erasePreview();
471   
472   const int id = getConstructorId();
473
474   if (id == 0) { // by planar face selection
475     if (CORBA::is_nil(myFace)) {
476       if(!showPreview)
477         showError( "Face has to be selected" );
478       return false;
479     }
480
481     // PAL12781: set center of WPL to face's center of mass
482     // like it is done for LCS creation
483     CORBA::Double Ox,Oy,Oz, Zx,Zy,Zz, Xx,Xy,Xz;
484     Ox = Oy = Oz = Zx = Zy = Xy = Xz = 0.;
485     Zz = Xx = 1.;
486
487     GEOM::GEOM_IMeasureOperations_ptr aMeasureOp =
488       myGeomGUI->GetGeomGen()->GetIMeasureOperations(getStudyId());
489     aMeasureOp->GetPosition(myFace, Ox,Oy,Oz, Zx,Zy,Zz, Xx,Xy,Xz);
490
491     if (aMeasureOp->IsDone()) {
492       gp_Pnt aPnt (Ox,Oy,Oz);
493       gp_Dir aDirN (Zx,Zy,Zz);
494       gp_Dir aDirX (Xx,Xy,Xz);
495       myWPlane = gp_Ax3(aPnt, aDirN, aDirX);
496     } else {
497       if(!showPreview)
498         showError( "Wrong shape selected (has to be a planar face)" );
499       return false;
500     }
501
502   } else if (id == 1) { // by two vectors (Ox & Oz)
503     if ( CORBA::is_nil( myVectX ) || CORBA::is_nil( myVectZ ) ) {
504       if(!showPreview)
505         showError( "Two vectors have to be selected" );
506       return false;
507     }
508
509     TopoDS_Edge aVectX, aVectZ;
510     TopoDS_Vertex VX1, VX2, VZ1, VZ2;
511     gp_Vec aVX, aVZ;
512
513     if (!GEOMBase::GetShape( myVectX, aVectX, TopAbs_EDGE ) ||
514         !GEOMBase::GetShape( myVectZ, aVectZ, TopAbs_EDGE )) {
515       if(!showPreview)
516         showError( "Wrong shape selected (two vectors(edges) have to be selected)" );
517       return false;
518     }
519
520     TopExp::Vertices(aVectX, VX1, VX2, Standard_True);
521     TopExp::Vertices(aVectZ, VZ1, VZ2, Standard_True);
522
523     if (VX1.IsNull() || VX2.IsNull()) {
524       if(!showPreview)
525         showError( "Bad OX vector" );
526       return false;
527     }
528     if (VZ1.IsNull() || VZ2.IsNull()) {
529       if(!showPreview)
530         showError( "Bad OZ vector" );
531       return false;
532     }
533
534     aVX = gp_Vec(BRep_Tool::Pnt(VX1), BRep_Tool::Pnt(VX2));
535     aVZ = gp_Vec(BRep_Tool::Pnt(VZ1), BRep_Tool::Pnt(VZ2));
536
537     if (aVX.Magnitude() < Precision::Confusion()) {
538       if(!showPreview)
539         showError( "Bad OX vector" );
540       return false;
541     }
542     if (aVZ.Magnitude() < Precision::Confusion()) {
543       if(!showPreview)
544         showError( "Bad OZ vector" );
545       return false;
546     }
547
548     gp_Dir aDirX = gp_Dir(aVX.X(), aVX.Y(), aVX.Z());
549     gp_Dir aDirZ = gp_Dir(aVZ.X(), aVZ.Y(), aVZ.Z());
550
551     if (aDirX.IsParallel(aDirZ, Precision::Angular())) {
552       if(!showPreview)
553         showError( "Parallel vectors selected" );
554       return false;
555     }
556
557     myWPlane = gp_Ax3(BRep_Tool::Pnt(VX1), aDirZ, aDirX);
558
559   } else if (id == 2) { // by selection from standard (OXY or OYZ, or OZX)
560     gp_Ax2 anAx2;
561
562     if      (aOriginType == 1) anAx2 = gp::XOY();
563     else if (aOriginType == 2) anAx2 = gp::YOZ();
564     else if (aOriginType == 0) anAx2 = gp::ZOX();
565
566     myWPlane = gp_Ax3(anAx2);
567
568   } else {
569     return false;
570   }
571   
572   if (myReverseCB->isChecked())
573     {
574       myWPlane.YReverse();
575       myWPlane.ZReverse();
576     }
577   
578   if (showPreview)
579     {
580       GEOM::GEOM_IBasicOperations_var aBasicOp = getGeomEngine()->GetIBasicOperations( getStudyId() );
581       GEOM::GEOM_Object_var anObj = aBasicOp->MakeMarker(  myWPlane.Location().X(), myWPlane.Location().Y(), myWPlane.Location().Z(),
582                                                            myWPlane.XDirection().X(), myWPlane.XDirection().Y(), myWPlane.XDirection().Z(),
583                                                            myWPlane.YDirection().X(), myWPlane.YDirection().Y(), myWPlane.YDirection().Z() );
584       displayPreview(anObj);
585     }
586
587   return true;
588 }