]> SALOME platform Git repositories - modules/geom.git/blob - src/RepairGUI/RepairGUI_OrientationDlg.cxx
Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / RepairGUI / RepairGUI_OrientationDlg.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   : RepairGUI_OrientationDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "RepairGUI_OrientationDlg.h"
30
31 #include <Precision.hxx>
32 #include <TopoDS_Compound.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRepBuilderAPI_MakeEdge.hxx>
35 #include <BRepAdaptor_Surface.hxx>
36 #include <TopExp_Explorer.hxx>
37
38 #include "utilities.h"
39
40 using namespace std;
41
42 //=================================================================================
43 // class    : RepairGUI_OrientationDlg()
44 // purpose  : Constructs a RepairGUI_OrientationDlg which is a child of 'parent', with the 
45 //            name 'name' and widget flags set to 'f'.
46 //            The dialog will by default be modeless, unless you set 'modal' to
47 //            TRUE to construct a modal dialog.
48 //=================================================================================
49 RepairGUI_OrientationDlg::RepairGUI_OrientationDlg(QWidget* parent, const char* name, RepairGUI* theRepairGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
50   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
51 {
52   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_ORIENTATION")));
53   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
54
55   setCaption(tr("GEOM_ORIENTATION_TITLE"));
56
57   /***************************************************************/
58   GroupConstructors->setTitle(tr("GEOM_ORIENTATION"));
59   RadioButton1->setPixmap(image0);
60   RadioButton2->close(TRUE);
61   RadioButton3->close(TRUE);
62
63   GroupPoints = new DlgRef_1Sel1Spin1Check(this, "GroupPoints");
64   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
65   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECT"));
66   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR_LENGTH"));
67   GroupPoints->CheckButton1->setText(tr("GEOM_ORIENTATION_OPT"));
68   GroupPoints->PushButton1->setPixmap(image1);
69
70   Layout1->addWidget(GroupPoints, 1, 0);
71   /***************************************************************/
72
73   /* Initialisations */
74   myRepairGUI = theRepairGUI;
75   Init();
76 }
77
78
79 //=================================================================================
80 // function : ~RepairGUI_OrientationDlg()
81 // purpose  : Destroys the object and frees any allocated resources
82 //=================================================================================
83 RepairGUI_OrientationDlg::~RepairGUI_OrientationDlg()
84 {
85   // no need to delete child widgets, Qt does it all for us
86 }
87
88
89 //=================================================================================
90 // function : Init()
91 // purpose  :
92 //=================================================================================
93 void RepairGUI_OrientationDlg::Init()
94 {
95   /* init variables */
96   myEditCurrentArgument = GroupPoints->LineEdit1;
97
98   myLength = 25.0;
99   myOkShape = false;
100
101   double SpecificStep = 10;
102   /* min, max, step and decimals for spin boxes & initial values */
103   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, SpecificStep, 3);
104   GroupPoints->SpinBox_DX->SetValue(myLength);
105
106   /* signals and slots connections */
107   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
108   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
109
110   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
111   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
112
113   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
114   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
115
116   connect(GroupPoints->CheckButton1, SIGNAL(stateChanged(int)), this, SLOT(ReverseAngle(int)));
117   
118   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
119
120   /* displays Dialog */
121   GroupPoints->show();
122   this->show();
123
124   return;
125 }
126
127
128 //=================================================================================
129 // function : ClickOnOk()
130 // purpose  :
131 //=================================================================================
132 void RepairGUI_OrientationDlg::ClickOnOk()
133 {
134   this->ClickOnApply();
135   ClickOnCancel();
136   return;
137 }
138
139
140 //=================================================================================
141 // function : ClickOnApply()
142 // purpose  :
143 //=================================================================================
144 void RepairGUI_OrientationDlg::ClickOnApply()
145 {
146   buttonApply->setFocus();
147   QAD_Application::getDesktop()->putInfo(tr(""));
148   if (mySimulationTopoDs.IsNull())
149     return;
150   myGeomBase->EraseSimulationShape();
151   mySimulationTopoDs.Nullify();
152
153   if(myOkShape && GroupPoints->CheckButton1->isChecked()) {       
154     myRepairGUI->MakeOrientationChangeAndDisplay(myGeomShape);
155     GroupPoints->CheckButton1->setChecked(FALSE);
156   }
157   return;
158 }
159
160
161 //=================================================================================
162 // function : SelectionIntoArgument()
163 // purpose  : Called when selection as changed or other case
164 //=================================================================================
165 void RepairGUI_OrientationDlg::SelectionIntoArgument()
166 {
167   myGeomBase->EraseSimulationShape();
168   mySimulationTopoDs.Nullify();
169   myEditCurrentArgument->setText("");
170   QString aString = ""; /* name of selection */
171   
172   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
173   if(nbSel != 1) {
174     if(myEditCurrentArgument == GroupPoints->LineEdit1)
175       myOkShape = false;
176     return;
177   }
178
179   // nbSel == 1
180   TopoDS_Shape S; 
181   Standard_Boolean testResult;
182   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
183   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
184     return;
185
186   if(myEditCurrentArgument == GroupPoints->LineEdit1) {
187     myGeomShape = myGeomBase->ConvertIOinGEOMShape(IO, testResult);
188     if(!testResult)
189       return;
190     myEditCurrentArgument->setText(aString);
191     myShape = S;
192     myOkShape = true;
193   }
194
195   if(myOkShape)
196     this->MakeOrientationSimulationAndDisplay();
197   return; 
198 }
199
200
201 //=================================================================================
202 // function : LineEditReturnPressed()
203 // purpose  :
204 //=================================================================================
205 void RepairGUI_OrientationDlg::LineEditReturnPressed()
206 {
207   QLineEdit* send = (QLineEdit*)sender();
208   if(send == GroupPoints->LineEdit1)
209     myEditCurrentArgument = GroupPoints->LineEdit1;
210   else
211     return;
212
213   GEOMBase_Skeleton::LineEditReturnPressed();
214   return;
215 }
216
217
218 //=================================================================================
219 // function : SetEditCurrentArgument()
220 // purpose  :
221 //=================================================================================
222 void RepairGUI_OrientationDlg::SetEditCurrentArgument()
223 {
224   QPushButton* send = (QPushButton*)sender();
225
226   if(send == GroupPoints->PushButton1) {
227     GroupPoints->LineEdit1->setFocus();
228     myEditCurrentArgument = GroupPoints->LineEdit1;
229   }
230   this->SelectionIntoArgument();
231
232   return;
233 }
234
235
236 //=================================================================================
237 // function : ActivateThisDialog()
238 // purpose  :
239 //=================================================================================
240 void RepairGUI_OrientationDlg::ActivateThisDialog()
241 {
242   GEOMBase_Skeleton::ActivateThisDialog();
243   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
244   GroupPoints->LineEdit1->setFocus();
245   myEditCurrentArgument = GroupPoints->LineEdit1;
246   if(!mySimulationTopoDs.IsNull())
247     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
248   return;
249 }
250
251
252 //=================================================================================
253 // function : enterEvent()
254 // purpose  :
255 //=================================================================================
256 void RepairGUI_OrientationDlg::enterEvent(QEvent* e)
257 {
258   if (GroupConstructors->isEnabled())
259     return;
260   this->ActivateThisDialog();
261   return;
262 }
263
264
265 //=================================================================================
266 // function : ValueChangedInSpinBox()
267 // purpose  :
268 //=================================================================================
269 void RepairGUI_OrientationDlg::ValueChangedInSpinBox(double newValue)
270 {
271   myLength = newValue;
272   if(myOkShape)
273     this->MakeOrientationSimulationAndDisplay();
274   return;
275 }
276
277
278 //=================================================================================
279 // function : ReverseOrientation()
280 // purpose  :
281 //=================================================================================
282 void RepairGUI_OrientationDlg::ReverseAngle(int state)
283 {
284   if(myOkShape)
285     this->MakeOrientationSimulationAndDisplay();
286   return;
287 }
288
289
290 //==================================================================================
291 // function : MakeOrientationSimulationAndDisplay()
292 // purpose  : Create 'aCompound1' and 'aCompound2' each contains edges oriented
293 //          : respectively FORWARD and REVERSE for all faces of 'aTopoDS'
294 //          : These edges represent normal vectors on faces of 'aTopoDS'
295 //          : For a unique edge an arrow is displayed to show its orientation.
296 //==================================================================================
297 void RepairGUI_OrientationDlg::MakeOrientationSimulationAndDisplay()
298 {
299   myGeomBase->EraseSimulationShape();
300   mySimulationTopoDs.Nullify();
301   
302   TopoDS_Compound aCompound1, aCompound2;
303   TopoDS_Compound NullComp;
304   BRep_Builder aBuilder1, aBuilder2;
305   aCompound1 = aCompound2 = NullComp;
306   aBuilder1.MakeCompound(aCompound1);
307   aBuilder2.MakeCompound(aCompound2);
308   
309   if(myShape.IsNull())
310     return;
311
312   try {
313     /* Case of an edge */
314     if(myShape.ShapeType() == TopAbs_EDGE) {
315       /* Try to display a cone simulation shape to show direction of edge */
316       TopoDS_Shape tmpShape = myShape ;
317       if(GroupPoints->CheckButton1->isChecked()) {
318         if(myShape.Orientation() == TopAbs_FORWARD)
319           tmpShape.Orientation(TopAbs_REVERSED);
320         else
321           tmpShape.Orientation(TopAbs_FORWARD);
322       }
323       if(myGeomBase->CreateArrowForLinearEdge(tmpShape, mySimulationTopoDs))
324         myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
325       return;
326     }
327
328     gp_Pnt P1, P2 ;
329     gp_Vec V, V1, V2;
330     TopExp_Explorer ex(myShape, TopAbs_FACE);
331     int test = 0;
332     while(ex.More()) {
333       const TopoDS_Face& F = TopoDS::Face(ex.Current());
334       /* find the center of the minmax */
335       BRepAdaptor_Surface SF(F);
336       Standard_Real u, v, x;
337     
338       u = SF.FirstUParameter();
339       x = SF.LastUParameter();
340       if(Precision::IsInfinite(u)) {
341         if(Precision::IsInfinite(x))
342           u = 0.0;
343         else
344           u = x;
345       }
346       else if(!Precision::IsInfinite(x))
347         u = (u+x) / 2.0;
348     
349       v = SF.FirstVParameter();
350       x = SF.LastVParameter();
351       if(Precision::IsInfinite(v)) {
352         if(Precision::IsInfinite(x))
353           v = 0.0;
354         else
355           v = x;
356       }
357       else if(!Precision::IsInfinite(x))
358         v = (v+x) / 2.0;
359     
360       SF.D1(u, v, P1, V1, V2);
361       V = V1.Crossed(V2);
362       x = V.Magnitude();
363       if(x > 1.e-10)
364         V.Multiply(myLength/x);
365       else {
366         V.SetCoord(myLength/2.0, 0.0, 0.0);
367         MESSAGE ("Null normal in Orientation " << endl);
368       }
369     
370       P2 = P1;
371       /* test orientation of each face and displays forward (aCompound1) */
372       if(F.Orientation() == TopAbs_FORWARD)
373         P2.Translate(V);
374       else
375         P2.Translate(-V);    
376       BRepBuilderAPI_MakeEdge anEdge(P1, P2);
377       aBuilder1.Add(aCompound1, anEdge);
378     
379       P2 = P1;
380       /* test orientation of each face and displays forward (aCompound2) */
381       if(F.Orientation() == TopAbs_FORWARD)
382         P2.Translate(-V);
383       else
384         P2.Translate(V);    
385       anEdge = BRepBuilderAPI_MakeEdge(P1, P2);
386       aBuilder2.Add(aCompound2, anEdge);
387     
388       ex.Next();
389       test++;
390     }
391   
392     /* display simulation compounds */
393     if(test > 0 && GroupPoints->CheckButton1->isChecked())
394       mySimulationTopoDs = aCompound1;    
395     else if(test > 0 && !GroupPoints->CheckButton1->isChecked())
396       mySimulationTopoDs = aCompound2;
397
398     if(!mySimulationTopoDs.IsNull())
399       myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
400   }
401   catch(Standard_Failure)
402     MESSAGE("Exception catched in MakeOrientationSimulationAndDisplay");
403   return;
404 }