Salome HOME
NPAL16559: EDF507: Implementation of a more advanced system of measurement.
[modules/geom.git] / src / MeasureGUI / MeasureGUI_AngleDlg.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   : MeasureGUI_AngleDlg.cxx
25 //  Author : Julia DOROVSKIKH
26 //  Module : GEOM
27 //  $Header$
28
29 #include "MeasureGUI_AngleDlg.h"
30
31 #include "MeasureGUI_2Sel1LineEdit_QTD.h"
32
33 #include "GEOMBase.h"
34 #include "GEOM_Displayer.h"
35 #include "DlgRef_SpinBox.h"
36
37 #include "SUIT_Session.h"
38 #include "SUIT_ViewWindow.h"
39 #include "SOCC_Prs.h"
40 #include "SOCC_ViewModel.h"
41 #include "SalomeApp_Tools.h"
42
43 // OCCT Includes
44 #include <AIS_AngleDimension.hxx>
45 #include <AIS_ListIteratorOfListOfInteractive.hxx>
46 #include <BRep_Tool.hxx>
47 #include <BRepBuilderAPI_MakeEdge.hxx>
48 #include <BRepBuilderAPI_MakeVertex.hxx>
49 #include <TopExp.hxx>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <Geom_Plane.hxx>
53 #include <gce_MakePln.hxx>
54 #include <Precision.hxx>
55
56 // QT Includes
57 #include <qlineedit.h>
58 #include <qlabel.h>
59 #include <qlayout.h>
60 #include <qpushbutton.h>
61 #include <qradiobutton.h>
62 #include <qbuttongroup.h>
63
64 #include "GEOMImpl_Types.hxx"
65
66 #include "utilities.h"
67
68 #include <Standard_Failure.hxx>
69 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
70
71 //=================================================================================
72 // class    : MeasureGUI_AngleDlg()
73 // purpose  : Constructs a MeasureGUI_AngleDlg which is a child of 'parent', with the
74 //            name 'name' and widget flags set to 'f'.
75 //            The dialog will by default be modeless, unless you set 'modal' to
76 //            TRUE to construct a modal dialog.
77 //=================================================================================
78 MeasureGUI_AngleDlg::MeasureGUI_AngleDlg (GeometryGUI* GUI, QWidget* parent)
79   : MeasureGUI_Skeleton(GUI, parent, "MeasureGUI_AngleDlg")
80 {
81   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_ANGLE")));
82   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
83
84   setCaption(tr("GEOM_MEASURE_ANGLE_TITLE"));
85
86   // Widgets
87
88   GroupConstructors->setTitle(tr("GEOM_MEASURE_ANGLE_ANGLE"));
89   RadioButton1->setPixmap(image0);
90
91   myGrp = new MeasureGUI_2Sel1LineEdit_QTD(this, "myGrp");
92   myGrp->GroupBox1->setTitle(tr("GEOM_MEASURE_ANGLE_OBJ"));
93   myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
94   myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
95   myGrp->TextLabel3->setText(tr("GEOM_MEASURE_ANGLE_IS"));
96   myGrp->LineEdit3->setReadOnly(TRUE);
97   myGrp->PushButton1->setPixmap(image1);
98   myGrp->PushButton2->setPixmap(image1);
99   myGrp->LineEdit1->setReadOnly(true);
100   myGrp->LineEdit2->setReadOnly(true);
101
102   Layout1->addWidget(myGrp, 1, 0);
103
104   // Help page reference
105   myHelpFileName = "files/salome2_sp3_measuregui_functions.htm#Angle";
106
107   // Initialisation
108   Init();
109 }
110
111 //=================================================================================
112 // function : ~MeasureGUI_AngleDlg()
113 // purpose  : Destroys the object and frees any allocated resources
114 //=================================================================================
115 MeasureGUI_AngleDlg::~MeasureGUI_AngleDlg()
116 {
117 }
118
119 //=================================================================================
120 // function : Init()
121 // purpose  :
122 //=================================================================================
123 void MeasureGUI_AngleDlg::Init()
124 {
125   mySelBtn   = myGrp->PushButton1;
126   mySelEdit  = myGrp->LineEdit1;
127   mySelBtn2  = myGrp->PushButton2;
128   mySelEdit2 = myGrp->LineEdit2;
129
130   myEditCurrentArgument = mySelEdit;
131
132   connect(mySelEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
133   connect(mySelBtn2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
134
135   globalSelection(GEOM_LINE);
136   MeasureGUI_Skeleton::Init();
137 }
138
139
140 //=================================================================================
141 // function : SelectionIntoArgument()
142 // purpose  : Called when selection has changed
143 //=================================================================================
144 void MeasureGUI_AngleDlg::SelectionIntoArgument()
145 {
146   Standard_Boolean testResult = Standard_False;
147   GEOM::GEOM_Object_var aSelectedObject =
148     GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult);
149
150   if (!testResult)
151     aSelectedObject = GEOM::GEOM_Object::_nil();
152
153   if (myEditCurrentArgument == mySelEdit)
154     myObj = aSelectedObject;
155   else
156     myObj2 = aSelectedObject;
157
158   processObject();
159 }
160
161 //=================================================================================
162 // function : processObject()
163 // purpose  : Fill dialogs fields in accordance with myObj and myObj2
164 //=================================================================================
165 void MeasureGUI_AngleDlg::processObject()
166 {
167   myGrp->LineEdit1->setText(!myObj->_is_nil()  ? GEOMBase::GetName(myObj ) : "");
168   myGrp->LineEdit2->setText(!myObj2->_is_nil() ? GEOMBase::GetName(myObj2) : "");
169
170   double anAngle = 0.;
171   if (getParameters(anAngle))
172   {
173     myGrp->LineEdit3->setText(DlgRef_SpinBox::PrintDoubleValue(anAngle));
174     redisplayPreview();
175   }
176   else
177   {
178     myGrp->LineEdit3->setText("");
179     erasePreview();
180   }
181 }
182
183 //=================================================================================
184 // function : getParameters()
185 // purpose  : Get angle between objects
186 //=================================================================================
187 bool MeasureGUI_AngleDlg::getParameters (double& theAngle)
188 {
189   QString msg;
190   if (isValid(msg)) {
191     try {
192       theAngle = GEOM::GEOM_IMeasureOperations::_narrow(getOperation())->GetAngle(myObj, myObj2);
193     }
194     catch(const SALOME::SALOME_Exception& e) {
195       SalomeApp_Tools::QtCatchCorbaException(e);
196       return false;
197     }
198
199     return getOperation()->IsDone();
200   }
201
202   return false;
203 }
204
205 //=================================================================================
206 // function : SetEditCurrentArgument()
207 // purpose  :
208 //=================================================================================
209 void MeasureGUI_AngleDlg::SetEditCurrentArgument()
210 {
211   QPushButton* send = (QPushButton*)sender();
212
213   if (send == mySelBtn) {
214     mySelEdit->setFocus();
215     myEditCurrentArgument = mySelEdit;
216   }
217   else {
218     mySelEdit2->setFocus();
219     myEditCurrentArgument = mySelEdit2;
220   }
221
222   globalSelection(GEOM_LINE);
223   SelectionIntoArgument();
224 }
225
226 //=================================================================================
227 // function : LineEditReturnPressed()
228 // purpose  :
229 //=================================================================================
230 void MeasureGUI_AngleDlg::LineEditReturnPressed()
231 {
232   QLineEdit* send = (QLineEdit*)sender();
233
234   if (send == mySelEdit)
235     myEditCurrentArgument = mySelEdit;
236   else
237     myEditCurrentArgument = mySelEdit2;
238
239   if (GEOMBase::SelectionByNameInDialogs(this, mySelEdit->text(), selectedIO()))
240     mySelEdit->setText(mySelEdit->text());
241 }
242
243 //=================================================================================
244 // function : buildPrs()
245 // purpose  :
246 //=================================================================================
247 SALOME_Prs* MeasureGUI_AngleDlg::buildPrs()
248 {
249   double anAngle = 0.;
250
251   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
252
253   if (myObj->_is_nil() || myObj2->_is_nil() || !getParameters(anAngle) ||
254       vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
255     return 0;
256
257   if (anAngle > Precision::Angular())
258   {
259     try
260     {
261 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
262       OCC_CATCH_SIGNALS;
263 #endif
264       TopoDS_Shape S1, S2;
265       if (GEOMBase::GetShape(myObj , S1, TopAbs_EDGE) &&
266           GEOMBase::GetShape(myObj2, S2, TopAbs_EDGE))
267       {
268         TopoDS_Edge anEdge1 = TopoDS::Edge(S1);
269         TopoDS_Edge anEdge2 = TopoDS::Edge(S2);
270
271         // Build a plane for angle dimension presentation {P11, P12, P3}
272         TopoDS_Vertex V11, V12, V21, V22;
273         TopExp::Vertices(anEdge1, V11, V12);
274         TopExp::Vertices(anEdge2, V21, V22);
275
276         gp_Pnt aP11 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge1));
277         gp_Pnt aP12 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge1));
278
279         gp_Pnt aP21 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge2));
280         gp_Pnt aP22 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge2));
281
282         //        *P3
283         //         \
284         //          \
285         //           *P22
286         //          /
287         //         /
288         // P11    /         P12
289         // *-----/----------*
290         //  \   /
291         //   \ /
292         //    *P21
293         gp_Pnt aP3 (aP22.XYZ() + aP11.XYZ() - aP21.XYZ());
294
295         gce_MakePln gce_MP (aP11, aP12, aP3);
296         Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
297
298         // Build the angle dimension presentation
299         QString aLabel;
300         aLabel.sprintf("%.1f", anAngle);
301
302         Handle(AIS_AngleDimension) anIO = new AIS_AngleDimension
303           (anEdge1, anEdge2, aPlane, anAngle * PI180,
304            TCollection_ExtendedString((Standard_CString)aLabel.latin1()));
305
306         SOCC_Prs* aPrs =
307           dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
308
309         if (aPrs)
310           aPrs->AddObject(anIO);
311
312         return aPrs;
313       }
314     }
315     catch(Standard_Failure)
316     {
317     }
318   }
319
320   return 0;
321 }
322
323 //=================================================================================
324 // function : isValid()
325 // purpose  :
326 //=================================================================================
327 bool MeasureGUI_AngleDlg::isValid(QString& msg)
328 {
329   return MeasureGUI_Skeleton::isValid(msg) && !myObj2->_is_nil();
330 }