Salome HOME
968e4df57e8b74ec6d004d3dd9d49dc4d9ae7de8
[modules/geom.git] / src / GenerationGUI / GenerationGUI_FillingDlg.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   : GenerationGUI_FillingDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "GenerationGUI_FillingDlg.h"
31
32 #include "QAD_Config.h"
33
34 #include <GeomFill_SectionGenerator.hxx>
35 #include <GeomFill_Line.hxx>
36 #include <GeomFill_AppSurf.hxx>
37 #include <Geom_BSplineSurface.hxx>
38 #include <Geom_TrimmedCurve.hxx>
39 #include <BRepBuilderAPI_MakeFace.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <BRep_Tool.hxx>
42 #include <Precision.hxx>
43
44 //=================================================================================
45 // class    : GenerationGUI_FillingDlg()
46 // purpose  : Constructs a GenerationGUI_FillingDlg which is a child of 'parent', with the 
47 //            name 'name' and widget flags set to 'f'.
48 //            The dialog will by default be modeless, unless you set 'modal' to
49 //            TRUE to construct a modal dialog.
50 //=================================================================================
51 GenerationGUI_FillingDlg::GenerationGUI_FillingDlg(QWidget* parent, const char* name, GenerationGUI* theGenerationGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
52   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
53 {
54   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_FILLING")));
55   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
56
57   setCaption(tr("GEOM_FILLING_TITLE"));
58
59   /***************************************************************/
60   GroupConstructors->setTitle(tr("GEOM_FILLING"));
61   RadioButton1->setPixmap(image0);
62   RadioButton2->close(TRUE);
63   RadioButton3->close(TRUE);
64
65   GroupPoints = new DlgRef_1Sel5Spin(this, "GroupPoints");
66   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
67   GroupPoints->TextLabel1->setText(tr("GEOM_FILLING_COMPOUND"));
68   GroupPoints->TextLabel2->setText(tr("GEOM_FILLING_MIN_DEG"));
69   GroupPoints->TextLabel3->setText(tr("GEOM_FILLING_TOL_2D"));
70   GroupPoints->TextLabel4->setText(tr("GEOM_FILLING_NB_ITER"));
71   GroupPoints->TextLabel5->setText(tr("GEOM_FILLING_MAX_DEG"));
72   GroupPoints->TextLabel6->setText(tr("GEOM_FILLING_TOL_3D"));
73   GroupPoints->PushButton1->setPixmap(image1);
74
75   Layout1->addWidget(GroupPoints, 1, 0);
76   /***************************************************************/
77
78   /* Initialisations */
79   myGenerationGUI = theGenerationGUI;
80   Init();
81 }
82
83
84 //=================================================================================
85 // function : ~GenerationGUI_FillingDlg()
86 // purpose  : Destroys the object and frees any allocated resources
87 //=================================================================================
88 GenerationGUI_FillingDlg::~GenerationGUI_FillingDlg()
89 {
90     // no need to delete child widgets, Qt does it all for us
91 }
92
93
94 //=================================================================================
95 // function : Init()
96 // purpose  :
97 //=================================================================================
98 void GenerationGUI_FillingDlg::Init()
99 {
100   /* init variables */
101   myEditCurrentArgument = GroupPoints->LineEdit1;
102
103   myMinDeg = 2;
104   myMaxDeg = 5;
105   myTol3D = 0.0001;
106   myTol2D = 0.0001;
107   myNbIter = 5;
108   myOkSectionShape = false;
109
110   myCompoundFilter = new GEOM_ShapeTypeFilter(TopAbs_COMPOUND, myGeom);
111   mySelection->AddFilter(myCompoundFilter);
112
113   double SpecificStep1 = 1;
114   double SpecificStep2 = 0.0001;
115   /* min, max, step and decimals for spin boxes & initial values */
116   GroupPoints->SpinBox_1->RangeStepAndValidator(2.0, 999.999, SpecificStep1, 3);
117   GroupPoints->SpinBox_2->RangeStepAndValidator(0.00001, 10000.0, SpecificStep2, 5);
118   GroupPoints->SpinBox_3->RangeStepAndValidator(1.0, 999.999, SpecificStep1, 3);
119   GroupPoints->SpinBox_4->RangeStepAndValidator(1.0, 999.999, SpecificStep1, 3);
120   GroupPoints->SpinBox_5->RangeStepAndValidator(0.00001, 10000.0, SpecificStep2, 5);
121
122   GroupPoints->SpinBox_1->SetValue(myMinDeg);
123   GroupPoints->SpinBox_2->SetValue(myTol2D);
124   GroupPoints->SpinBox_3->SetValue(myNbIter);
125   GroupPoints->SpinBox_4->SetValue(myMaxDeg);
126   GroupPoints->SpinBox_5->SetValue(myTol3D);
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->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
134   
135   connect(GroupPoints->SpinBox_1, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
136   connect(GroupPoints->SpinBox_2, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
137   connect(GroupPoints->SpinBox_3, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
138   connect(GroupPoints->SpinBox_4, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
139   connect(GroupPoints->SpinBox_5, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
140
141   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_1, SLOT(SetStep(double)));
142   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_2, SLOT(SetStep(double)));
143   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_3, SLOT(SetStep(double)));
144   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_4, SLOT(SetStep(double)));
145   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_5, SLOT(SetStep(double)));
146
147   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
148
149   /* displays Dialog */
150   GroupPoints->show();
151   this->show();
152
153   return;
154 }
155
156
157 //=================================================================================
158 // function : ClickOnOk()
159 // purpose  :
160 //=================================================================================
161 void GenerationGUI_FillingDlg::ClickOnOk()
162 {
163   this->ClickOnApply();
164   ClickOnCancel();
165   return;
166 }
167
168
169 //=================================================================================
170 // function : ClickOnApply()
171 // purpose  :
172 //=================================================================================
173 void GenerationGUI_FillingDlg::ClickOnApply()
174 {
175   QAD_Application::getDesktop()->putInfo(tr(""));
176   if (mySimulationTopoDs.IsNull())
177     return;
178   myGeomBase->EraseSimulationShape();
179   mySimulationTopoDs.Nullify();
180
181   if(myOkSectionShape)    
182     myGenerationGUI->MakeFillingAndDisplay(myGeomShape, myMinDeg, myMaxDeg, myTol3D, myTol2D, myNbIter);
183   return;
184 }
185
186
187 //=================================================================================
188 // function : SelectionIntoArgument()
189 // purpose  : Called when selection as changed or other case
190 //=================================================================================
191 void GenerationGUI_FillingDlg::SelectionIntoArgument()
192 {
193   myGeomBase->EraseSimulationShape();
194   mySimulationTopoDs.Nullify();
195   myEditCurrentArgument->setText("");
196   QString aString = ""; /* name of selection */
197   
198   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
199   if(nbSel != 1) {
200     if(myEditCurrentArgument == GroupPoints->LineEdit1)
201       myOkSectionShape = false;
202     return;
203   }
204   
205   // nbSel == 1
206   Standard_Boolean testResult;
207   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
208   if(!myGeomBase->GetTopoFromSelection(mySelection, mySectionShape))
209     return;
210   
211   if(myEditCurrentArgument == GroupPoints->LineEdit1 && mySectionShape.ShapeType() == TopAbs_COMPOUND) {
212     myEditCurrentArgument->setText(aString);
213     myGeomShape = myGeomBase->ConvertIOinGEOMShape(IO, testResult);
214     if(!testResult)
215       return;
216     myOkSectionShape = true;
217   }
218
219   if(myOkSectionShape)
220     this->MakeFillingSimulationAndDisplay();
221   return;
222 }
223
224
225 //=================================================================================
226 // function : SetEditCurrentArgument()
227 // purpose  :
228 //=================================================================================
229 void GenerationGUI_FillingDlg::SetEditCurrentArgument()
230 {
231   QPushButton* send = (QPushButton*)sender();
232   mySelection->ClearFilters();
233
234   if(send == GroupPoints->PushButton1) {
235     GroupPoints->LineEdit1->setFocus();
236     myEditCurrentArgument = GroupPoints->LineEdit1;
237     mySelection->AddFilter(myCompoundFilter);
238     this->SelectionIntoArgument();
239   }
240
241   return;
242 }
243
244
245 //=================================================================================
246 // function : LineEditReturnPressed()
247 // purpose  :
248 //=================================================================================
249 void GenerationGUI_FillingDlg::LineEditReturnPressed()
250 {  
251   QLineEdit* send = (QLineEdit*)sender();
252   if(send == GroupPoints->LineEdit1)
253     myEditCurrentArgument = GroupPoints->LineEdit1;
254   else
255     return;
256
257   GEOMBase_Skeleton::LineEditReturnPressed();
258   return;
259 }
260
261
262 //=================================================================================
263 // function : ActivateThisDialog()
264 // purpose  :
265 //=================================================================================
266 void GenerationGUI_FillingDlg::ActivateThisDialog()
267 {
268   GEOMBase_Skeleton::ActivateThisDialog();
269   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
270   mySelection->AddFilter(myCompoundFilter);
271   if(!mySimulationTopoDs.IsNull())
272     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
273   return;
274 }
275
276
277 //=================================================================================
278 // function : enterEvent()
279 // purpose  :
280 //=================================================================================
281 void GenerationGUI_FillingDlg::enterEvent(QEvent* e)
282 {
283   if (GroupConstructors->isEnabled())
284     return;
285   this->ActivateThisDialog();
286   return;
287 }
288
289
290 //=================================================================================
291 // function : ValueChangedInSpinBox()
292 // purpose  :
293 //=================================================================================
294 void GenerationGUI_FillingDlg::ValueChangedInSpinBox(double newValue)
295 {
296   QObject* send = (QObject*)sender();
297
298   if(send == GroupPoints->SpinBox_1)
299     myMinDeg = int(newValue);
300   else if(send == GroupPoints->SpinBox_2)
301     myTol2D = newValue;
302   else if(send == GroupPoints->SpinBox_3)
303     myNbIter = int(newValue);
304   else if(send == GroupPoints->SpinBox_4)
305     myMaxDeg = int(newValue);
306   else if(send == GroupPoints->SpinBox_5)
307     myTol3D = newValue;
308
309   if(myOkSectionShape)
310     this->MakeFillingSimulationAndDisplay();
311   return;
312 }
313
314
315 //=================================================================================
316 // function : MakeFillingSimulationAndDisplay()
317 // purpose  :
318 //=================================================================================
319 void GenerationGUI_FillingDlg::MakeFillingSimulationAndDisplay()
320 {
321   myGeomBase->EraseSimulationShape();
322   mySimulationTopoDs.Nullify();
323
324   try {
325     /* we verify the contents of the shape */
326     TopExp_Explorer Ex;
327     TopoDS_Shape Scurrent;      
328     Standard_Real First, Last;
329     Handle(Geom_Curve) C;
330     GeomFill_SectionGenerator Section;
331     
332     Standard_Integer i = 0;
333     for(Ex.Init(mySectionShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
334       Scurrent = Ex.Current();
335       if( Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE)
336         return;
337       C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
338       C = new Geom_TrimmedCurve(C, First, Last);
339       Section.AddCurve(C) ;
340       i++ ;
341     }
342     
343     /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
344     /* We set 'tolerance' = tol3d                                            */
345     // Section.Perform( tol3d ) ; NRI */
346     Section.Perform(Precision::Confusion());
347     Handle(GeomFill_Line) Line = new GeomFill_Line(i);
348
349     GeomFill_AppSurf App(myMinDeg, myMaxDeg, myTol3D, myTol2D, myNbIter) ; /* user parameters */
350     App.Perform(Line, Section);
351     
352     if(!App.IsDone())
353       return;
354
355     Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
356     App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);    
357     Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface(App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(), App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
358     
359     if(GBS.IsNull())
360       return;
361     mySimulationTopoDs  = BRepBuilderAPI_MakeFace(GBS);    
362     if(mySimulationTopoDs.IsNull())
363       return;
364     else
365       myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
366   }
367   catch(Standard_Failure) {
368     MESSAGE("Exception catched in MakePrismSimulationAndDisplay" << endl);
369     return;
370   }
371   return;
372 }