Salome HOME
Correct make file for shared modules script
[modules/geom.git] / src / BasicGUI / BasicGUI_WorkingPlaneDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  GEOM GEOMGUI : GUI for Geometry component
23 //  File   : BasicGUI_WorkingPlaneDlg.cxx
24 //  Author : Lucien PIGNOLONI
25 //  Module : GEOM
26 //
27 #include "BasicGUI_WorkingPlaneDlg.h"
28
29 #include "GEOMBase.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "LightApp_SelectionMgr.h"
35
36 // OCCT Includes
37 #include <BRep_Tool.hxx>
38 #include <TopoDS.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.h>
51 #include <qlabel.h>
52
53 #include "GEOMImpl_Types.hxx"
54
55 using namespace std;
56
57 //=================================================================================
58 // class    : BasicGUI_WorkingPlaneDlg()
59 // purpose  : Constructs a BasicGUI_WorkingPlaneDlg which is a child of 'parent', with the
60 //            name 'name' and widget flags set to 'f'.
61 //            The dialog will by default be modeless, unless you set 'modal' to
62 //            TRUE to construct a modal dialog.
63 //=================================================================================
64 BasicGUI_WorkingPlaneDlg::BasicGUI_WorkingPlaneDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
65                                                    const char* name, bool modal, WFlags fl)
66   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
67                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
68 {
69   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
70   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_FACE")));
71   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_VECTOR")));
72   QPixmap image3(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_ORIGIN")));
73
74   setCaption(tr("GEOM_WPLANE_TITLE"));
75
76   /***************************************************************/
77   GroupConstructors->setTitle(tr("GEOM_WPLANE"));
78   RadioButton1->setPixmap(image1);
79   RadioButton2->setPixmap(image2);
80   RadioButton3->setPixmap(image3);
81
82   Group1 = new DlgRef_1Sel_QTD(this, "Group1");
83   Group1->GroupBox1->setTitle(tr("GEOM_WPLANE_FACE"));
84   Group1->TextLabel1->setText(tr("GEOM_SELECTION"));
85   Group1->PushButton1->setPixmap(image0);
86   Group1->LineEdit1->setReadOnly( true );
87
88   Group2 = new DlgRef_2Sel_QTD(this, "Group2");
89   Group2->GroupBox1->setTitle(tr("GEOM_WPLANE_VECTOR"));
90   Group2->TextLabel1->setText(tr("GEOM_WPLANE_VX"));
91   Group2->TextLabel2->setText(tr("GEOM_WPLANE_VZ"));
92   Group2->PushButton1->setPixmap(image0);
93   Group2->PushButton2->setPixmap(image0);
94   Group2->LineEdit1->setReadOnly( true );
95   Group2->LineEdit2->setReadOnly( true );
96
97   Group3 = new DlgRef_3Check_QTD(this, "Group3");
98   Group3->GroupBox1->setTitle(tr("GEOM_WPLANE_ORIGIN"));
99   Group3->RadioButton1->setText(tr("GEOM_WPLANE_OXY"));
100   Group3->RadioButton2->setText(tr("GEOM_WPLANE_OYZ"));
101   Group3->RadioButton3->setText(tr("GEOM_WPLANE_OZX"));
102
103   Layout1->addWidget(Group1, 1, 0);
104   Layout1->addWidget(Group2, 1, 0);
105   Layout1->addWidget(Group3, 1, 0);
106   /***************************************************************/
107   QGroupBox* aReverseGroupBox = new QGroupBox(this, "aReverseGroupBox");
108   aReverseGroupBox->setTitle(tr(""));
109   aReverseGroupBox->setColumnLayout(1, Qt::Horizontal);
110   aReverseGroupBox->setInsideMargin(10);
111   
112   myReverseCB = new QCheckBox(aReverseGroupBox, "myReverseCB");
113   myReverseCB->setText(tr("GEOM_REVERSE_PLANE"));
114     
115   Layout1->addWidget(aReverseGroupBox, 2, 0);
116   
117
118   setHelpFileName("create_wplane_page.html");
119
120   Init();
121 }
122
123 //=================================================================================
124 // function : ~BasicGUI_WorkingPlaneDlg()
125 // purpose  : Destroys the object and frees any allocated resources
126 //=================================================================================
127 BasicGUI_WorkingPlaneDlg::~BasicGUI_WorkingPlaneDlg()
128 {
129 }
130
131 //=================================================================================
132 // function : Init()
133 // purpose  :
134 //=================================================================================
135 void BasicGUI_WorkingPlaneDlg::Init()
136 {
137   /* init variables */
138   myEditCurrentArgument = Group1->LineEdit1;
139   myWPlane = myGeomGUI->GetWorkingPlane();
140
141   // myGeomGUI->SetState( 0 );
142
143   myFace = GEOM::GEOM_Object::_nil();
144   myVectX = GEOM::GEOM_Object::_nil();
145   myVectZ = GEOM::GEOM_Object::_nil();
146
147   aOriginType = 1;
148
149   /* Filter definition */
150   globalSelection( GEOM_PLANE );
151
152    /* signals and slots connections */
153   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
154   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
155   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
156   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
157
158   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
159   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
160
161   connect(Group1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
162   connect(Group1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
163
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   ConstructorsClicked(0);
178 }
179
180 //=================================================================================
181 // function : ConstructorsClicked()
182 // purpose  : Radio button management
183 //=================================================================================
184 void BasicGUI_WorkingPlaneDlg::ConstructorsClicked(int constructorId)
185 {
186   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
187
188   disconnect(aSelMgr, 0, this, 0);
189
190   switch (constructorId)
191     {
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         resize(0, 0);
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         resize(0, 0);
221         Group2->show();
222
223         myEditCurrentArgument = Group2->LineEdit1;
224         Group2->LineEdit1->setText("");
225         Group2->LineEdit2->setText("");
226         myVectX = GEOM::GEOM_Object::_nil();
227         myVectZ = GEOM::GEOM_Object::_nil();
228
229         connect(aSelMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
230         break;
231       }
232     case 2:
233       {
234         Group1->hide();
235         Group2->hide();
236         resize(0, 0);
237         Group3->show();
238
239         Group3->RadioButton1->setChecked(true);
240         aOriginType = 1;
241         break;
242       }
243     }
244   
245   updateWPlane();
246 }
247
248 //=================================================================================
249 // function : GroupClicked()
250 // purpose  : Radio button management
251 //=================================================================================
252 void BasicGUI_WorkingPlaneDlg::GroupClicked(int groupId)
253 {
254   aOriginType = groupId;
255   updateWPlane();
256 }
257
258 //=================================================================================
259 // function : ClickOnOk()
260 // purpose  :
261 //=================================================================================
262 void BasicGUI_WorkingPlaneDlg::ClickOnOk()
263 {
264   if ( ClickOnApply() )
265     ClickOnCancel();
266 }
267
268 //=================================================================================
269 // function : ClickOnApply()
270 // purpose  :
271 //=================================================================================
272 bool BasicGUI_WorkingPlaneDlg::ClickOnApply()
273 {
274   buttonApply->setFocus();
275   myGeomGUI->application()->putInfo(tr(""));
276  
277   if (updateWPlane(false)){
278     myGeomGUI->SetWorkingPlane(myWPlane);
279     myGeomGUI->ActiveWorkingPlane();
280   }
281   
282   return true;
283 }
284
285 //=================================================================================
286 // function : SelectionIntoArgument()
287 // purpose  : Called when selection as changed or other case
288 //=================================================================================
289 void BasicGUI_WorkingPlaneDlg::SelectionIntoArgument()
290 {
291   myEditCurrentArgument->setText("");
292   QString aName;
293
294   const int id = getConstructorId();
295   if (IObjectCount() != 1) {
296     if (id == 0)
297       myFace = GEOM::GEOM_Object::_nil();
298     else if (id == 1) {
299       if (myEditCurrentArgument == Group2->LineEdit1)
300         myVectX = GEOM::GEOM_Object::_nil();
301       else if (myEditCurrentArgument == Group2->LineEdit2)
302         myVectZ = GEOM::GEOM_Object::_nil();
303     }
304     return;
305   }
306
307   // nbSel == 1
308   Standard_Boolean aRes = Standard_False;
309   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), aRes);
310
311   if (!aRes || CORBA::is_nil(aSelectedObject))
312     return;
313
314   aName = GEOMBase::GetName(aSelectedObject);
315
316   if (myEditCurrentArgument == Group1->LineEdit1)
317     myFace = aSelectedObject;
318   else if (myEditCurrentArgument == Group2->LineEdit1 || myEditCurrentArgument == Group2->LineEdit2)
319   {
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           int anIndex = aMap( 1 );
331           aName = aName + ":edge_" + QString::number( anIndex );
332
333           GEOM::GEOM_IShapesOperations_var aShapesOp =
334             getGeomEngine()->GetIShapesOperations( getStudyId() );
335           if(myEditCurrentArgument == Group2->LineEdit1)
336             myVectX = aShapesOp->GetSubShape(aSelectedObject, anIndex);
337           else
338             myVectZ = aShapesOp->GetSubShape(aSelectedObject, anIndex);
339         }
340         else {
341           if (myEditCurrentArgument == Group2->LineEdit1)
342             myVectX = aSelectedObject;
343           else
344             myVectZ = aSelectedObject;
345         }
346         aSelMgr->clearSelected();
347       }
348     }
349   }
350
351   myEditCurrentArgument->setText( aName );
352
353   updateWPlane();
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 // 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 // function : onReverse()
398 // purpose  :
399 //=================================================================================
400 void BasicGUI_WorkingPlaneDlg::onReverse()
401 {
402   updateWPlane();
403 }
404
405 //=================================================================================
406 // function : ActivateThisDialog()
407 // purpose  :
408 //=================================================================================
409 void BasicGUI_WorkingPlaneDlg::ActivateThisDialog( )
410 {
411   GEOMBase_Skeleton::ActivateThisDialog();
412   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
413           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
414
415   ConstructorsClicked( getConstructorId() );
416 }
417
418 //=================================================================================
419 // function : DeactivateActiveDialog()
420 // purpose  : public slot to deactivate if active
421 //=================================================================================
422 void BasicGUI_WorkingPlaneDlg::DeactivateActiveDialog()
423 {
424   GEOMBase_Skeleton::DeactivateActiveDialog();
425 }
426
427 //=================================================================================
428 // function : ClickOnCancel()
429 // purpose  :
430 //=================================================================================
431 void BasicGUI_WorkingPlaneDlg::ClickOnCancel()
432 {
433   GEOMBase_Skeleton::ClickOnCancel();
434 }
435
436 //=================================================================================
437 // function : enterEvent()
438 // purpose  :
439 //=================================================================================
440 void BasicGUI_WorkingPlaneDlg::enterEvent(QEvent* e)
441 {
442   if ( !GroupConstructors->isEnabled() )
443     ActivateThisDialog();
444 }
445
446 //=================================================================================
447 // function : closeEvent
448 // purpose  :
449 //=================================================================================
450 void BasicGUI_WorkingPlaneDlg::closeEvent( QCloseEvent* e )
451 {
452   GEOMBase_Skeleton::closeEvent( e );
453 }
454
455 //=================================================================================
456 // function : updateWPlane
457 // purpose  :
458 //=================================================================================
459 bool BasicGUI_WorkingPlaneDlg::updateWPlane( const bool showPreview )
460 {
461   erasePreview();
462   
463   const int id = getConstructorId();
464
465   if (id == 0) { // by planar face selection
466     if (CORBA::is_nil(myFace)) {
467       if(!showPreview)
468         showError( "Face has to be selected" );
469       return false;
470     }
471
472     // PAL12781: set center of WPL to face's center of mass
473     // like it is done for LCS creation
474     CORBA::Double Ox,Oy,Oz, Zx,Zy,Zz, Xx,Xy,Xz;
475     Ox = Oy = Oz = Zx = Zy = Xy = Xz = 0.;
476     Zz = Xx = 1.;
477
478     GEOM::GEOM_IMeasureOperations_ptr aMeasureOp =
479       myGeomGUI->GetGeomGen()->GetIMeasureOperations(getStudyId());
480     aMeasureOp->GetPosition(myFace, Ox,Oy,Oz, Zx,Zy,Zz, Xx,Xy,Xz);
481
482     if (aMeasureOp->IsDone()) {
483       gp_Pnt aPnt (Ox,Oy,Oz);
484       gp_Dir aDirN (Zx,Zy,Zz);
485       gp_Dir aDirX (Xx,Xy,Xz);
486       myWPlane = gp_Ax3(aPnt, aDirN, aDirX);
487     } else {
488       if(!showPreview)
489         showError( "Wrong shape selected (has to be a planar face)" );
490       return false;
491     }
492   }
493   else if (id == 1) { // by two vectors (Ox & Oz)
494     if ( CORBA::is_nil( myVectX ) || CORBA::is_nil( myVectZ ) ) {
495       if(!showPreview)
496         showError( "Two vectors have to be selected" );
497       return false;
498     }
499
500     TopoDS_Edge aVectX, aVectZ;
501     TopoDS_Vertex VX1, VX2, VZ1, VZ2;
502     gp_Vec aVX, aVZ;
503
504     if (!GEOMBase::GetShape( myVectX, aVectX, TopAbs_EDGE ) ||
505         !GEOMBase::GetShape( myVectZ, aVectZ, TopAbs_EDGE )) {
506       if(!showPreview)
507         showError( "Wrong shape selected (two vectors(edges) have to be selected)" );
508       return false;
509     }
510
511     TopExp::Vertices(aVectX, VX1, VX2, Standard_True);
512     TopExp::Vertices(aVectZ, VZ1, VZ2, Standard_True);
513
514     if (VX1.IsNull() || VX2.IsNull()) {
515       if(!showPreview)
516         showError( "Bad OX vector" );
517       return false;
518     }
519     if (VZ1.IsNull() || VZ2.IsNull()) {
520       if(!showPreview)
521         showError( "Bad OZ vector" );
522       return false;
523     }
524
525     aVX = gp_Vec(BRep_Tool::Pnt(VX1), BRep_Tool::Pnt(VX2));
526     aVZ = gp_Vec(BRep_Tool::Pnt(VZ1), BRep_Tool::Pnt(VZ2));
527
528     if (aVX.Magnitude() < Precision::Confusion()) {
529       if(!showPreview)
530         showError( "Bad OX vector" );
531       return false;
532     }
533     if (aVZ.Magnitude() < Precision::Confusion()) {
534       if(!showPreview)
535         showError( "Bad OZ vector" );
536       return false;
537     }
538
539     gp_Dir aDirX = gp_Dir(aVX.X(), aVX.Y(), aVX.Z());
540     gp_Dir aDirZ = gp_Dir(aVZ.X(), aVZ.Y(), aVZ.Z());
541
542     if (aDirX.IsParallel(aDirZ, Precision::Angular())) {
543       if(!showPreview)
544         showError( "Parallel vectors selected" );
545       return false;
546     }
547
548     myWPlane = gp_Ax3(BRep_Tool::Pnt(VX1), aDirZ, aDirX);
549   }
550   else if (id == 2) { // by selection from standard (OXY or OYZ, or OZX)
551     gp_Ax2 anAx2;
552
553     if      (aOriginType == 1) anAx2 = gp::XOY();
554     else if (aOriginType == 2) anAx2 = gp::YOZ();
555     else if (aOriginType == 0) anAx2 = gp::ZOX();
556
557     myWPlane = gp_Ax3(anAx2);
558   }
559   else {
560     return false;
561   }
562
563   if (myReverseCB->isChecked())
564   {
565     myWPlane.YReverse();
566     myWPlane.ZReverse();
567   }
568
569   if (showPreview)
570   {
571     GEOM::GEOM_IBasicOperations_var aBasicOp = getGeomEngine()->GetIBasicOperations(getStudyId());
572     GEOM::GEOM_Object_var anObj = aBasicOp->MakeMarker
573       (myWPlane.Location().X()  , myWPlane.Location().Y()  , myWPlane.Location().Z(),
574        myWPlane.XDirection().X(), myWPlane.XDirection().Y(), myWPlane.XDirection().Z(),
575        myWPlane.YDirection().X(), myWPlane.YDirection().Y(), myWPlane.YDirection().Z());
576     displayPreview(anObj);
577   }
578
579   return true;
580 }
581