Salome HOME
DCQ : Add Multi-Wire for MakeFace
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_ConeDlg.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : PrimitiveGUI_ConeDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "PrimitiveGUI_ConeDlg.h"
31
32 #include <gp_Lin.hxx>
33 #include <BRepPrimAPI_MakeCone.hxx>
34 #include <BRepPrimAPI_MakeCylinder.hxx>
35 #include <BRepAdaptor_Curve.hxx>
36 #include <Precision.hxx>
37 #include "QAD_Config.h"
38
39 //=================================================================================
40 // class    : PrimitiveGUI_ConeDlg()
41 // purpose  : Constructs a PrimitiveGUI_ConeDlg which is a child of 'parent', with the 
42 //            name 'name' and widget flags set to 'f'.
43 //            The dialog will by default be modeless, unless you set 'modal' to
44 //            TRUE to construct a modal dialog.
45 //=================================================================================
46 PrimitiveGUI_ConeDlg::PrimitiveGUI_ConeDlg(QWidget* parent, const char* name, PrimitiveGUI* thePrimitiveGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
47   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
48 {
49   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_CONE_PV")));
50   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_CONE_DXYZ")));
51   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
52
53   setCaption(tr("GEOM_CONE_TITLE"));
54
55   /***************************************************************/
56   GroupConstructors->setTitle(tr("GEOM_CONE"));
57   RadioButton1->setPixmap(image0);
58   RadioButton2->setPixmap(image1);
59   RadioButton3->close(TRUE);
60
61   GroupPoints = new DlgRef_2Sel3Spin(this, "GroupPoints");
62   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
63   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
64   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
65   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_I").arg("1"));
66   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_I").arg("2"));
67   GroupPoints->TextLabel5->setText(tr("GEOM_HEIGHT"));
68   GroupPoints->PushButton1->setPixmap(image2);
69   GroupPoints->PushButton2->setPixmap(image2);
70
71   GroupDimensions = new DlgRef_3Spin(this, "GroupDimensions");
72   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
73   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS_I").arg("1"));
74   GroupDimensions->TextLabel2->setText(tr("GEOM_RADIUS_I").arg("2"));
75   GroupDimensions->TextLabel3->setText(tr("GEOM_HEIGHT"));
76
77   Layout1->addWidget(GroupPoints, 1, 0);
78   Layout1->addWidget(GroupDimensions, 1, 0);
79   /***************************************************************/
80
81   /* Initialisations */
82   myPrimitiveGUI = thePrimitiveGUI;
83   Init();
84 }
85
86
87 //=================================================================================
88 // function : ~PrimitiveGUI_ConeDlg()
89 // purpose  : Destroys the object and frees any allocated resources
90 //=================================================================================
91 PrimitiveGUI_ConeDlg::~PrimitiveGUI_ConeDlg()
92 {
93     // no need to delete child widgets, Qt does it all for us
94 }
95
96
97 //=================================================================================
98 // function : Init()
99 // purpose  :
100 //=================================================================================
101 void PrimitiveGUI_ConeDlg::Init()
102 {
103   /* init variables */
104   myConstructorId = 0;
105   myEditCurrentArgument = GroupPoints->LineEdit1;
106
107   myRadius1 = 100.0;
108   myRadius2 = 0.0;
109   myHeight = 300.0;
110
111   myOkRadius1 = myOkRadius2 = myOkHeight = true;
112   myOkPoint1 = myOkDir = false;
113
114   myEdgeFilter = new GEOM_EdgeFilter(StdSelect_Line, myGeom);
115   myVertexFilter = new GEOM_ShapeTypeFilter(TopAbs_VERTEX, myGeom);
116   mySelection->AddFilter(myVertexFilter);
117
118   /* Get setting of step value from file configuration */
119   QString St = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
120   step = St.toDouble();
121
122   /* min, max, step and decimals for spin boxes & initial values */
123   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
124   GroupPoints->SpinBox_DY->RangeStepAndValidator(0.001, 999.999, step, 3);
125   GroupPoints->SpinBox_DZ->RangeStepAndValidator(-999.999, 999.999, step, 3);
126   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
127   GroupDimensions->SpinBox_DY->RangeStepAndValidator(0.001, 999.999, step, 3);
128   GroupDimensions->SpinBox_DZ->RangeStepAndValidator(-999.999, 999.999, step, 3);
129
130   GroupPoints->SpinBox_DX->SetValue(myRadius1);
131   GroupPoints->SpinBox_DY->SetValue(myRadius2);
132   GroupPoints->SpinBox_DZ->SetValue(myHeight);
133   GroupDimensions->SpinBox_DX->SetValue(myRadius1);
134   GroupDimensions->SpinBox_DY->SetValue(myRadius2);
135   GroupDimensions->SpinBox_DZ->SetValue(myHeight);
136
137   /* signals and slots connections */
138   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
139   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
140   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
141
142   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
143   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
144
145   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
146   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
147
148   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
149   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
150   connect(GroupPoints->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
151   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
152   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
153   connect(GroupDimensions->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
154
155   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
156   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
157   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DZ, SLOT(SetStep(double)));
158   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
159   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
160   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DZ, SLOT(SetStep(double)));
161   
162   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
163
164   /* displays Dialog */
165   GroupDimensions->hide();
166   GroupPoints->show();
167   this->show();
168
169   return ;
170 }
171
172
173 //=================================================================================
174 // function : ConstructorsClicked()
175 // purpose  : Radio button management
176 //=================================================================================
177 void PrimitiveGUI_ConeDlg::ConstructorsClicked(int constructorId)
178 {
179   myConstructorId = constructorId;
180   mySelection->ClearFilters();
181   myGeomBase->EraseSimulationShape();
182   disconnect(mySelection, 0, this, 0);
183   myOkHeight = myOkRadius1 = myOkRadius2 = true;
184   myRadius1 = 100.0;
185   myRadius2 = 0.0;
186   myHeight = 300.0;
187
188   switch(myConstructorId)
189     { 
190     case 0 :
191       {
192         GroupDimensions->hide();
193         resize(0, 0);
194         GroupPoints->show();
195         myOkPoint1 = myOkDir = false;
196
197         myEditCurrentArgument = GroupPoints->LineEdit1;
198         GroupPoints->LineEdit1->setText(tr(""));
199         GroupPoints->LineEdit2->setText(tr(""));
200         
201         GroupPoints->SpinBox_DX->SetValue(myRadius1);
202         GroupPoints->SpinBox_DY->SetValue(myRadius2);
203         GroupPoints->SpinBox_DZ->SetValue(myHeight);
204
205         /* filter for next selection */
206         mySelection->AddFilter(myVertexFilter);
207         connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
208         break;
209       }
210     case 1 :
211       { 
212         GroupPoints->hide();
213         resize( 0, 0 );
214         GroupDimensions->show();
215         myOkPoint1 = myOkDir = true;
216
217         GroupDimensions->SpinBox_DX->SetValue(myRadius1);
218         GroupDimensions->SpinBox_DY->SetValue(myRadius2);
219         GroupDimensions->SpinBox_DZ->SetValue(myHeight);
220
221         myPoint1.SetCoord(0.0, 0.0, 0.0);
222         myDir.SetCoord(0.0, 0.0, 1.0);
223
224         MakeConeSimulationAndDisplay();
225         break;
226       }
227     }
228   return ;
229 }
230
231
232 //=================================================================================
233 // function : ClickOnOk()
234 // purpose  :
235 //=================================================================================
236 void PrimitiveGUI_ConeDlg::ClickOnOk()
237 {
238   this->ClickOnApply();
239   ClickOnCancel();
240   return;
241 }
242
243
244 //=================================================================================
245 // function : ClickOnApply()
246 // purpose  :
247 //=================================================================================
248 void PrimitiveGUI_ConeDlg::ClickOnApply()
249 {
250   QAD_Application::getDesktop()->putInfo(tr(""));
251   if (mySimulationTopoDs.IsNull())
252     return;
253   myGeomBase->EraseSimulationShape();
254   mySimulationTopoDs.Nullify();
255  
256   if(myOkPoint1 && myOkDir && myOkRadius1 && myOkRadius2 && myOkHeight) {
257     gp_Dir aDir = myDir;
258     /* allows user to reverse direction of construction with a negative height */
259     if(this->myHeight < -Precision::Confusion())
260       aDir.Reverse();
261     myPrimitiveGUI->MakeConeAndDisplay(myPoint1, aDir, myRadius1, myRadius2, fabs(myHeight)); 
262   }
263   return ;
264 }
265
266
267 //=================================================================================
268 // function : SelectionIntoArgument()
269 // purpose  : Called when selection as changed or other case
270 //=================================================================================
271 void PrimitiveGUI_ConeDlg::SelectionIntoArgument()
272 {
273   myGeomBase->EraseSimulationShape(); 
274   myEditCurrentArgument->setText("");
275   QString aString = ""; /* name of selection */
276
277   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
278   if(nbSel != 1) {
279     if(myEditCurrentArgument == GroupPoints->LineEdit1)
280       myOkPoint1 = false;
281     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
282       myOkDir = false;
283     return;
284   }
285
286   /* nbSel == 1 */
287   TopoDS_Shape S;
288   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
289     return;
290
291   /*  gp_Pnt : not used */
292   if (myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint1)) {
293     GroupPoints->LineEdit1->setText(aString);
294     myOkPoint1 = true;
295   }    
296   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
297     BRepAdaptor_Curve curv(TopoDS::Edge(S));
298     myDir = curv.Line().Direction();
299     GroupPoints->LineEdit2->setText(aString);
300     myOkDir = true;
301   }
302
303   if(myOkPoint1 && myOkDir && myOkRadius1 && myOkRadius2 && myOkHeight)
304     MakeConeSimulationAndDisplay();
305   return;
306 }
307
308
309 //=================================================================================
310 // function : SetEditCurrentArgument()
311 // purpose  :
312 //=================================================================================
313 void PrimitiveGUI_ConeDlg::SetEditCurrentArgument()
314 {
315   if(myConstructorId != 0)
316     return;
317
318   QPushButton* send = (QPushButton*)sender();
319   mySelection->ClearFilters();
320
321   if(send == GroupPoints->PushButton1) {
322     GroupPoints->LineEdit1->setFocus();
323     myEditCurrentArgument = GroupPoints->LineEdit1;
324     mySelection->AddFilter(myVertexFilter);
325   }
326   else if(send == GroupPoints->PushButton2) {
327     GroupPoints->LineEdit2->setFocus();
328     myEditCurrentArgument = GroupPoints->LineEdit2;
329     mySelection->AddFilter(myEdgeFilter);
330   }
331   this->SelectionIntoArgument();
332
333   return;
334 }
335
336
337 //=================================================================================
338 // function : LineEditReturnPressed()
339 // purpose  :
340 //=================================================================================
341 void PrimitiveGUI_ConeDlg::LineEditReturnPressed()
342 {
343   QLineEdit* send = (QLineEdit*)sender();
344   if(send == GroupPoints->LineEdit1)
345     myEditCurrentArgument = GroupPoints->LineEdit1;
346   else if (send == GroupPoints->LineEdit2)
347     myEditCurrentArgument = GroupPoints->LineEdit2;
348   else
349     return;
350
351   GEOMBase_Skeleton::LineEditReturnPressed();
352   return;
353 }
354
355
356 //=================================================================================
357 // function : ActivateThisDialog()
358 // purpose  :
359 //=================================================================================
360 void PrimitiveGUI_ConeDlg::ActivateThisDialog()
361 {
362   GEOMBase_Skeleton::ActivateThisDialog();
363   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
364   if(myConstructorId == 0) {
365     GroupPoints->LineEdit1->setFocus();
366     myEditCurrentArgument = GroupPoints->LineEdit1;
367     mySelection->AddFilter(myVertexFilter);
368   }
369   if(!mySimulationTopoDs.IsNull())
370     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
371   return;
372 }
373
374
375 //=================================================================================
376 // function : enterEvent()
377 // purpose  :
378 //=================================================================================
379 void PrimitiveGUI_ConeDlg::enterEvent(QEvent* e)
380 {
381   if (GroupConstructors->isEnabled())
382     return;
383   this->ActivateThisDialog();
384   return;
385 }
386
387
388 //=================================================================================
389 // function : ValueChangedInSpinBox()
390 // purpose  :
391 //=================================================================================
392 void PrimitiveGUI_ConeDlg::ValueChangedInSpinBox(double newValue)
393 {
394   myGeomBase->EraseSimulationShape();
395   mySimulationTopoDs.Nullify();
396   QObject* send = (QObject*)sender();
397   
398   if(send == GroupPoints->SpinBox_DX || send == GroupDimensions->SpinBox_DX) {
399     myRadius1 = newValue;
400     myOkRadius1 = true;
401   }
402   else if(send == GroupPoints->SpinBox_DY || send == GroupDimensions->SpinBox_DY) {
403     myRadius2 = newValue;
404     myOkRadius2 = true;
405   }
406   else if(send == GroupPoints->SpinBox_DZ || send == GroupDimensions->SpinBox_DZ) {
407     myHeight = newValue;
408     myOkHeight = true;
409   }
410
411   if (myOkPoint1 && myOkDir && myOkRadius1 && myOkRadius2 && myOkHeight)
412     MakeConeSimulationAndDisplay();
413   return;
414 }
415
416
417 //=================================================================================
418 // function : MakeConeSimulationAndDisplay()
419 // purpose  :
420 //=================================================================================
421 void PrimitiveGUI_ConeDlg::MakeConeSimulationAndDisplay() 
422 {
423   myGeomBase->EraseSimulationShape();
424   mySimulationTopoDs.Nullify();
425   gp_Dir aDir = myDir;
426   
427   try {
428     /* allows user to reverse direction of construction with a negative height */
429     if(this->myHeight < -Precision::Confusion())
430       aDir.Reverse();
431
432     gp_Ax2 anAxis(this->myPoint1, aDir);
433
434     if(fabs(myRadius1 - myRadius2) <= Precision::Confusion())
435       mySimulationTopoDs = BRepPrimAPI_MakeCylinder(anAxis, (myRadius1+myRadius2)/2.0, fabs(myHeight)).Shape();
436     else {
437       if(fabs(myHeight) > Precision::Confusion())
438         mySimulationTopoDs = BRepPrimAPI_MakeCone(anAxis, myRadius1, myRadius2, fabs(myHeight)).Shape();
439     }
440     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
441   }
442   catch(Standard_Failure) {
443     MESSAGE("Exception catched in MakeConeSimulationAndDisplay");
444   }
445   return;
446 }