Salome HOME
Bug IPAL19097 NPAL16768 is not implemented for "Normal to a face"
[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 //  File   : MeasureGUI_AngleDlg.cxx
23 //  Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
24 //
25
26 #include "MeasureGUI_AngleDlg.h"
27 #include "MeasureGUI_Widgets.h"
28
29 #include <GEOMBase.h>
30 #include <DlgRef.h>
31
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Tools.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_ViewWindow.h>
38 #include <SUIT_ViewManager.h>
39 #include <SOCC_Prs.h>
40 #include <SOCC_ViewModel.h>
41 //#include "SalomeApp_Application.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 #include <AIS.hxx>
56
57 // QT Includes
58 #include <qlineedit.h>
59 #include <qlabel.h>
60 #include <qlayout.h>
61 #include <qpushbutton.h>
62 #include <qradiobutton.h>
63 #include <qbuttongroup.h>
64
65 #include "GEOMImpl_Types.hxx"
66
67 #include "utilities.h"
68
69 #include <Standard_Failure.hxx>
70 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
71
72 //=================================================================================
73 // class    : MeasureGUI_AngleDlg()
74 // purpose  : Constructs a MeasureGUI_AngleDlg which is a child of 'parent', with the
75 //            name 'name' and widget flags set to 'f'.
76 //            The dialog will by default be modeless, unless you set 'modal' to
77 //            TRUE to construct a modal dialog.
78 //=================================================================================
79 MeasureGUI_AngleDlg::MeasureGUI_AngleDlg( GeometryGUI* GUI, QWidget* parent )
80   : MeasureGUI_Skeleton( GUI, parent )
81 {
82   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_ANGLE" ) ) );
83   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
84
85   setWindowTitle( tr( "GEOM_MEASURE_ANGLE_TITLE" ) );
86
87   // Widgets
88
89   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_MEASURE_ANGLE_ANGLE" ) );
90   mainFrame()->RadioButton1->setIcon( image0 );
91
92   myGrp = new MeasureGUI_2Sel1LineEdit( centralWidget() );
93   myGrp->GroupBox1->setTitle( tr( "GEOM_MEASURE_ANGLE_OBJ" ) );
94   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT_I" ).arg( "1" ) );
95   myGrp->TextLabel2->setText( tr( "GEOM_OBJECT_I" ).arg( "2" ) );
96   myGrp->TextLabel3->setText( tr( "GEOM_MEASURE_ANGLE_IS" ) );
97   myGrp->LineEdit3->setReadOnly( true );
98   myGrp->PushButton1->setIcon( image1 );
99   myGrp->PushButton2->setIcon( image1 );
100   myGrp->LineEdit1->setReadOnly( true );
101   myGrp->LineEdit2->setReadOnly( true );
102
103   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
104   layout->setMargin( 0 ); layout->setSpacing( 6 );
105   layout->addWidget( myGrp );
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     myGrp->LineEdit3->setText( DlgRef::PrintDoubleValue( anAngle ) );
176     redisplayPreview();
177   }
178   else {
179     myGrp->LineEdit3->setText( "" );
180     erasePreview();
181   }
182 }
183
184 //=================================================================================
185 // function : getParameters()
186 // purpose  : Get angle between objects
187 //=================================================================================
188 bool MeasureGUI_AngleDlg::getParameters( double& theAngle )
189 {
190   QString msg;
191   if ( isValid( msg ) ) {
192     try {
193       theAngle = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetAngle( myObj, myObj2 );
194     }
195     catch( const SALOME::SALOME_Exception& e ) {
196       SalomeApp_Tools::QtCatchCorbaException( e );
197       return false;
198     }
199     
200     bool isDone = getOperation()->IsDone();
201     if ( !isDone ) {
202       CORBA::String_var aMsg = getOperation()->GetErrorCode();
203       SUIT_MessageBox::warning( this, 
204                                 QObject::tr( "WRN_WARNING" ),
205                                 QObject::tr( aMsg.in() ) );
206     }
207     return isDone;
208   }
209
210   return false;
211 }
212
213 //=================================================================================
214 // function : SetEditCurrentArgument()
215 // purpose  :
216 //=================================================================================
217 void MeasureGUI_AngleDlg::SetEditCurrentArgument()
218 {
219   QPushButton* send = (QPushButton*)sender();
220
221   if ( send == mySelBtn ) {
222     mySelEdit->setFocus();
223     myEditCurrentArgument = mySelEdit;
224   }
225   else {
226     mySelEdit2->setFocus();
227     myEditCurrentArgument = mySelEdit2;
228   }
229
230   globalSelection( GEOM_LINE );
231   SelectionIntoArgument();
232 }
233
234 //=================================================================================
235 // function : LineEditReturnPressed()
236 // purpose  :
237 //=================================================================================
238 void MeasureGUI_AngleDlg::LineEditReturnPressed()
239 {
240   QLineEdit* send = (QLineEdit*)sender();
241
242   if ( send == mySelEdit )
243     myEditCurrentArgument = mySelEdit;
244   else
245     myEditCurrentArgument = mySelEdit2;
246
247   if ( GEOMBase::SelectionByNameInDialogs( this, mySelEdit->text(), selectedIO() ) )
248     mySelEdit->setText( mySelEdit->text() );
249 }
250
251 //=================================================================================
252 // function : buildPrs()
253 // purpose  :
254 //=================================================================================
255 SALOME_Prs* MeasureGUI_AngleDlg::buildPrs()
256 {
257   double anAngle = 0.;
258
259   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
260
261   if ( myObj->_is_nil() || myObj2->_is_nil() || !getParameters( anAngle ) ||
262        vw->getViewManager()->getType() != OCCViewer_Viewer::Type() )
263     return 0;
264   
265   if ( anAngle > Precision::Angular() ) {
266     try {
267 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
268       OCC_CATCH_SIGNALS;
269 #endif
270       TopoDS_Shape S1, S2;
271       if ( GEOMBase::GetShape( myObj , S1, TopAbs_EDGE ) &&
272            GEOMBase::GetShape( myObj2, S2, TopAbs_EDGE ) ) {
273         TopoDS_Edge anEdge1 = TopoDS::Edge( S1 );
274         TopoDS_Edge anEdge2 = TopoDS::Edge( S2 );
275         
276         // Build a plane for angle dimension presentation {P11, P12, P3}
277         TopoDS_Vertex V11, V12, V21, V22;
278         TopExp::Vertices( anEdge1, V11, V12 );
279         TopExp::Vertices( anEdge2, V21, V22 );
280         
281         gp_Pnt aP11 = BRep_Tool::Pnt( TopExp::FirstVertex( anEdge1 ) );
282         gp_Pnt aP12 = BRep_Tool::Pnt( TopExp::LastVertex ( anEdge1 ) );
283
284         gp_Pnt aP21 = BRep_Tool::Pnt( TopExp::FirstVertex( anEdge2 ) );
285         gp_Pnt aP22 = BRep_Tool::Pnt( TopExp::LastVertex ( anEdge2 ) );
286
287         //        *P3
288         //         \
289         //          \
290         //           *P22
291         //          /
292         //         /
293         // P11    /         P12
294         // *-----/----------*
295         //  \   /
296         //   \ /
297         //    *P21
298         gp_Pnt aP3 ( aP22.XYZ() + aP11.XYZ() - aP21.XYZ() );
299
300         gce_MakePln gce_MP( aP11, aP12, aP3 );
301         Handle(Geom_Plane) aPlane = new Geom_Plane( gce_MP.Value() );
302
303         // Build the angle dimension presentation
304         QString aLabel;
305         aLabel.sprintf( "%.1f", anAngle );
306
307         Handle(AIS_AngleDimension) anIO = new AIS_AngleDimension
308           ( anEdge1, anEdge2, aPlane, anAngle * PI180,
309             TCollection_ExtendedString( (Standard_CString)aLabel.toLatin1().data() ) );
310         Handle(Geom_Line) geom_lin1,geom_lin2;
311         gp_Pnt ptat11,ptat12,ptat21,ptat22;
312         Standard_Boolean isInfinite1,isInfinite2;
313         Handle(Geom_Curve) extCurv;
314         Standard_Integer extShape;
315         if ( AIS::ComputeGeometry(anEdge1,
316                                   anEdge2,
317                                   extShape,
318                                   geom_lin1,
319                                   geom_lin2,
320                                   ptat11,
321                                   ptat12,
322                                   ptat21,
323                                   ptat22,
324                                   extCurv,
325                                   isInfinite1,
326                                   isInfinite2,
327                                   aPlane)) {
328           Standard_Real arrSize1 = anIO->ArrowSize();
329           Standard_Real arrSize2 = anIO->ArrowSize();
330           if (!isInfinite1) arrSize1 = ptat11.Distance(ptat12)/10.;
331           if (!isInfinite2) arrSize2 = ptat21.Distance(ptat22)/10.;
332           Standard_Real arrowSize = Max(arrSize1,arrSize2);
333           anIO->SetArrowSize(arrowSize);
334         }
335
336         SOCC_Prs* aPrs =
337           dynamic_cast<SOCC_Prs*>( ( (SOCC_Viewer*)( vw->getViewManager()->getViewModel() ) )->CreatePrs( 0 ) );
338
339         if ( aPrs )
340           aPrs->AddObject( anIO );
341
342         return aPrs;
343       }
344     }
345     catch( Standard_Failure ) {
346     }
347   }
348
349   return 0;
350 }
351
352 //=================================================================================
353 // function : isValid()
354 // purpose  :
355 //=================================================================================
356 bool MeasureGUI_AngleDlg::isValid( QString& msg )
357 {
358   return MeasureGUI_Skeleton::isValid( msg ) && !myObj2->_is_nil();
359 }