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