]> SALOME platform Git repositories - modules/geom.git/blob - src/GenerationGUI/GenerationGUI_RevolDlg.cxx
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/geom.git] / src / GenerationGUI / GenerationGUI_RevolDlg.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   : GenerationGUI_RevolDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GenerationGUI_RevolDlg.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "LightApp_SelectionMgr.h"
35
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <gp_Lin.hxx>
44 #include <BRepAdaptor_Curve.hxx>
45 #include <BRepPrimAPI_MakeRevol.hxx>
46 #include <TopExp_Explorer.hxx>
47 #include "GEOMImpl_Types.hxx"
48
49 #include <qlabel.h>
50 #include <qcheckbox.h>
51
52 #include "utilities.h"
53
54 //=================================================================================
55 // class    : GenerationGUI_RevolDlg()
56 // purpose  : Constructs a GenerationGUI_RevolDlg 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 GenerationGUI_RevolDlg::GenerationGUI_RevolDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
62                                                const char* name, bool modal, WFlags fl)
63   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
64                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
65 {
66   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_REVOL")));
67   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
68
69   setCaption(tr("GEOM_REVOLUTION_TITLE"));
70
71   /***************************************************************/
72   GroupConstructors->setTitle(tr("GEOM_REVOLUTION"));
73   RadioButton1->setPixmap(image0);
74   RadioButton2->close(TRUE);
75   RadioButton3->close(TRUE);
76   myBothway = false;
77
78   GroupPoints = new DlgRef_2Sel1Spin2Check(this, "GroupPoints");
79   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
80   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECT"));
81   GroupPoints->TextLabel2->setText(tr("GEOM_AXIS"));
82   GroupPoints->TextLabel3->setText(tr("GEOM_ANGLE"));
83   GroupPoints->PushButton1->setPixmap(image1);
84   GroupPoints->PushButton2->setPixmap(image1);
85   GroupPoints->LineEdit1->setReadOnly( true );
86   GroupPoints->LineEdit2->setReadOnly( true );
87   GroupPoints->CheckButton1->setText(tr("GEOM_BOTHWAY"));
88   GroupPoints->CheckButton2->setText(tr("GEOM_REVERSE"));
89
90   Layout1->addWidget(GroupPoints, 2, 0);
91   /***************************************************************/
92
93   setHelpFileName("create_revolution_page.html");
94
95   /* Initialisations */
96   Init();
97 }
98
99
100 //=================================================================================
101 // function : ~GenerationGUI_RevolDlg()
102 // purpose  : Destroys the object and frees any allocated resources
103 //=================================================================================
104 GenerationGUI_RevolDlg::~GenerationGUI_RevolDlg()
105 {
106   // no need to delete child widgets, Qt does it all for us
107 }
108
109
110 //=================================================================================
111 // function : Init()
112 // purpose  :
113 //=================================================================================
114 void GenerationGUI_RevolDlg::Init()
115 {
116   /* init variables */
117   myEditCurrentArgument = GroupPoints->LineEdit1;
118   GroupPoints->LineEdit1->setReadOnly( true );
119   GroupPoints->LineEdit2->setReadOnly( true );
120
121   myOkBase = myOkAxis = false;
122
123   double SpecificStep = 5;
124   /* min, max, step and decimals for spin boxes & initial values */
125   GroupPoints->SpinBox_DX->RangeStepAndValidator(COORD_MIN, COORD_MAX, SpecificStep, DBL_DIGITS_DISPLAY);
126   GroupPoints->SpinBox_DX->SetValue(45.0);
127
128   /* signals and slots connections */
129   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
130   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
131
132   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
133   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
134
135   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
136   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
137
138   connect(GroupPoints->SpinBox_DX,   SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
139   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)),        this, SLOT(onBothway()));
140   connect(GroupPoints->CheckButton2, SIGNAL(toggled(bool)),        this, SLOT(onReverse()));
141
142   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
143
144   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
145           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
146
147   initName(tr("GEOM_REVOLUTION"));
148
149   globalSelection( GEOM_ALLSHAPES );
150 }
151
152
153 //=================================================================================
154 // function : ClickOnOk()
155 // purpose  :
156 //=================================================================================
157 void GenerationGUI_RevolDlg::ClickOnOk()
158 {
159   if ( ClickOnApply() )
160     ClickOnCancel();
161 }
162
163
164 //=================================================================================
165 // function : ClickOnApply()
166 // purpose  :
167 //=================================================================================
168 bool GenerationGUI_RevolDlg::ClickOnApply()
169 {
170   if ( !onAccept() )
171     return false;
172
173   initName();
174   return true;
175 }
176
177 //=======================================================================
178 //function : isAcceptableBase
179 //purpose  : return true if theBase can be used as algo argument
180 //=======================================================================
181
182 static bool isAcceptableBase(const TopoDS_Shape& theBase)
183 {
184   switch ( theBase.ShapeType() ) {
185   case TopAbs_VERTEX:
186   case TopAbs_EDGE:
187   case TopAbs_WIRE:
188   case TopAbs_FACE:
189   case TopAbs_SHELL:
190     return true;
191   case TopAbs_SOLID:
192   case TopAbs_COMPSOLID:
193     return false;
194   case TopAbs_COMPOUND: {
195     TopExp_Explorer exp( theBase, TopAbs_SOLID);
196     return !exp.More();
197   }
198   default:
199     return false;
200   }
201   return false;
202 }
203
204 //=================================================================================
205 // function : SelectionIntoArgument()
206 // purpose  : Called when selection as changed or other case
207 //=================================================================================
208 void GenerationGUI_RevolDlg::SelectionIntoArgument()
209 {
210   erasePreview();
211   myEditCurrentArgument->setText("");
212   
213   if(IObjectCount() != 1) {
214     if(myEditCurrentArgument == GroupPoints->LineEdit1)
215       myOkBase = false;        
216     else if(myEditCurrentArgument == GroupPoints->LineEdit2)
217       myOkAxis = false;
218     return;
219   }
220   
221   // nbSel == 1
222   Standard_Boolean testResult = Standard_False;
223   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
224   QString aName = GEOMBase::GetName( aSelectedObject );
225   
226   if (!testResult)
227     return;
228
229   if(myEditCurrentArgument == GroupPoints->LineEdit1) {
230     TopoDS_Shape S;
231     myOkBase = false;
232     
233     if ( !GEOMBase::GetShape(aSelectedObject, S) || !isAcceptableBase( S ) )
234       return;
235     
236     myBase = aSelectedObject;
237     myOkBase = true;
238   }
239   else if(myEditCurrentArgument == GroupPoints->LineEdit2) {
240     if ( testResult && !aSelectedObject->_is_nil() )
241       {
242         TopoDS_Shape aShape;
243         
244         if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
245           {
246             LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
247               TColStd_IndexedMapOfInteger aMap;
248               aSelMgr->GetIndexes( firstIObject(), aMap );
249               if ( aMap.Extent() == 1 )
250                 {
251
252                   int anIndex = aMap( 1 );
253                   aName.append( ":edge_" + QString::number( anIndex ) );
254
255                   //Find SubShape Object in Father
256                   GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
257                   
258                   if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
259                     GEOM::GEOM_IShapesOperations_var aShapesOp =
260                       getGeomEngine()->GetIShapesOperations( getStudyId() );
261                     myAxis = aShapesOp->GetSubShape(aSelectedObject, anIndex);
262                     myOkAxis = true;
263                   }
264                   else {
265                     myAxis = aFindedObject;
266                     myOkAxis = true;
267                   }
268                 }
269               else {
270                 myOkAxis = true;
271                 if (aShape.ShapeType() != TopAbs_EDGE) {
272                   aSelectedObject = GEOM::GEOM_Object::_nil();
273                   aName = "";
274                   myOkAxis = false;
275                 }
276                 myAxis = aSelectedObject;
277               }
278           }
279       }
280   }
281   myEditCurrentArgument->setText( aName );
282
283   displayPreview();
284 }
285
286
287 //=================================================================================
288 // function : SetEditCurrentArgument()
289 // purpose  :
290 //=================================================================================
291 void GenerationGUI_RevolDlg::SetEditCurrentArgument()
292 {
293   QPushButton* send = (QPushButton*)sender();
294   globalSelection( GEOM_ALLSHAPES );
295
296   if(send == GroupPoints->PushButton1) {
297     GroupPoints->LineEdit1->setFocus();
298     myEditCurrentArgument = GroupPoints->LineEdit1;
299   }
300   else if(send == GroupPoints->PushButton2) {
301     GroupPoints->LineEdit2->setFocus();
302     myEditCurrentArgument = GroupPoints->LineEdit2;
303     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
304   }
305   SelectionIntoArgument();
306 }
307
308
309 //=================================================================================
310 // function : LineEditReturnPressed()
311 // purpose  :
312 //=================================================================================
313 void GenerationGUI_RevolDlg::LineEditReturnPressed()
314 {  
315   QLineEdit* send = (QLineEdit*)sender();
316   if(send == GroupPoints->LineEdit1 ||
317      send == GroupPoints->LineEdit2)
318     {
319       myEditCurrentArgument = send;
320       GEOMBase_Skeleton::LineEditReturnPressed();
321     }
322 }
323
324
325 //=================================================================================
326 // function : ActivateThisDialog()
327 // purpose  :
328 //=================================================================================
329 void GenerationGUI_RevolDlg::ActivateThisDialog()
330 {
331   GEOMBase_Skeleton::ActivateThisDialog();
332   globalSelection( GEOM_ALLSHAPES );
333   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
334   GroupPoints->LineEdit1->setFocus();
335   myEditCurrentArgument = GroupPoints->LineEdit1;
336   displayPreview();
337 }
338
339
340 //=================================================================================
341 // function : enterEvent()
342 // purpose  :
343 //=================================================================================
344 void GenerationGUI_RevolDlg::enterEvent(QEvent* e)
345 {
346   if ( !GroupConstructors->isEnabled() )
347     ActivateThisDialog();
348 }
349
350
351 //=================================================================================
352 // function : ValueChangedInSpinBox()
353 // purpose  :
354 //=================================================================================
355 void GenerationGUI_RevolDlg::ValueChangedInSpinBox()
356 {
357   displayPreview();
358 }
359
360
361 //=================================================================================
362 // function : getAngle()
363 // purpose  :
364 //=================================================================================
365 double GenerationGUI_RevolDlg::getAngle() const
366 {
367   return GroupPoints->SpinBox_DX->GetValue();
368 }
369
370 //=================================================================================
371 // function : createOperation
372 // purpose  :
373 //=================================================================================
374 GEOM::GEOM_IOperations_ptr GenerationGUI_RevolDlg::createOperation()
375 {
376   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
377 }
378
379 //=================================================================================
380 // function : isValid
381 // purpose  :
382 //=================================================================================
383 bool GenerationGUI_RevolDlg::isValid( QString& )
384 {
385   return myOkBase && myOkAxis;
386 }
387
388 //=================================================================================
389 // function : execute
390 // purpose  :
391 //=================================================================================
392 bool GenerationGUI_RevolDlg::execute( ObjectList& objects )
393 {
394   GEOM::GEOM_Object_var anObj;
395
396   if (!myBothway)
397     anObj = GEOM::GEOM_I3DPrimOperations::_narrow(
398       getOperation() )->MakeRevolutionAxisAngle( myBase, myAxis, getAngle() * PI180 );
399   else
400     anObj = GEOM::GEOM_I3DPrimOperations::_narrow(
401       getOperation() )->MakeRevolutionAxisAngle2Ways( myBase, myAxis, getAngle() * PI180 );
402
403   if ( !anObj->_is_nil() )
404     objects.push_back( anObj._retn() );
405
406   return true;
407 }
408
409
410 //=================================================================================
411 // function :  onReverse()
412 // purpose  :
413 //=================================================================================
414 void GenerationGUI_RevolDlg::onReverse()
415 {
416   double anOldValue = GroupPoints->SpinBox_DX->GetValue();
417   GroupPoints->SpinBox_DX->SetValue( -anOldValue );
418 }
419
420 //=================================================================================
421 // function :  onBothway()
422 // purpose  :
423 //=================================================================================
424 void GenerationGUI_RevolDlg::onBothway()
425 {
426   bool anOldValue = myBothway;
427   myBothway = !anOldValue;
428   GroupPoints->CheckButton2->setEnabled(!myBothway);  
429   displayPreview();
430 }
431
432 //=================================================================================
433 // function : addSubshapeToStudy
434 // purpose  : virtual method to add new SubObjects if local selection
435 //=================================================================================
436 void GenerationGUI_RevolDlg::addSubshapesToStudy()
437 {
438   QMap<QString, GEOM::GEOM_Object_var> objMap;
439
440   objMap[GroupPoints->LineEdit2->text()] = myAxis;
441
442   addSubshapesToFather( objMap );
443 }