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