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