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