Salome HOME
Bug IPAL19097 NPAL16768 is not implemented for "Normal to a face"
[modules/geom.git] / src / MeasureGUI / MeasureGUI_DistanceDlg.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_DistanceDlg.cxx
23 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
24 //
25
26 #include "MeasureGUI_DistanceDlg.h"
27 #include "MeasureGUI_Widgets.h"
28
29 #include <GEOMBase.h>
30 #include <DlgRef.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_ViewManager.h>
37 #include <SOCC_Prs.h>
38 #include <SOCC_ViewModel.h>
39 #include <SalomeApp_Tools.h>
40
41 #include <Geom_Plane.hxx>
42 #include <AIS_LengthDimension.hxx>
43 #include <BRepBuilderAPI_MakeEdge.hxx>
44 #include <BRepBuilderAPI_MakeVertex.hxx>
45 #include <gce_MakePln.hxx>
46 #include <Precision.hxx>
47
48 //=================================================================================
49 // class    : MeasureGUI_DistanceDlg()
50 // purpose  : Constructs a MeasureGUI_DistanceDlg which is a child of 'parent', with the
51 //            name 'name' and widget flags set to 'f'.
52 //            The dialog will by default be modeless, unless you set 'modal' to
53 //            true to construct a modal dialog.
54 //=================================================================================
55 MeasureGUI_DistanceDlg::MeasureGUI_DistanceDlg( GeometryGUI* GUI, QWidget* parent )
56   : MeasureGUI_Skeleton( GUI, parent )
57 {
58   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap(
59     "GEOM", tr( "ICON_DLG_MINDIST" ) ) );
60   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap(
61     "GEOM", tr( "ICON_SELECT" ) ) );
62
63   setWindowTitle( tr( "GEOM_MINDIST_TITLE" ) );
64
65   /***************************************************************/
66   
67   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_DISTANCE" ) );
68   mainFrame()->RadioButton1->setIcon( image0 );
69
70   myGrp = new MeasureGUI_2Sel4LineEdit( centralWidget() );
71   myGrp->GroupBox1->setTitle( tr( "GEOM_MINDIST_OBJ" ) );
72   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT_I" ).arg( "1" ) );
73   myGrp->TextLabel2->setText( tr( "GEOM_OBJECT_I" ).arg( "2" ) );
74   myGrp->TextLabel3->setText( tr( "GEOM_LENGTH" ) );
75   myGrp->TextLabel4->setText( tr( "GEOM_DX" ) );
76   myGrp->TextLabel5->setText( tr( "GEOM_DY" ) );
77   myGrp->TextLabel6->setText( tr( "GEOM_DZ" ) );
78   myGrp->LineEdit3->setReadOnly( true );
79   myGrp->PushButton1->setIcon( image1 );
80   myGrp->PushButton2->setIcon( image1 );
81   myGrp->LineEdit1->setReadOnly( true );
82   myGrp->LineEdit2->setReadOnly( true );
83   myGrp->LineEdit4->setReadOnly( true );
84   myGrp->LineEdit5->setReadOnly( true );
85   myGrp->LineEdit6->setReadOnly( true );
86
87   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
88   layout->setMargin( 0 ); layout->setSpacing( 6 );
89   layout->addWidget( myGrp );
90
91   /***************************************************************/
92
93   myHelpFileName = "using_measurement_tools_page.html#min_distance_anchor";
94
95   /* Initialisation */
96   Init();
97 }
98
99
100 //=================================================================================
101 // function : ~MeasureGUI_DistanceDlg()
102 // purpose  : Destroys the object and frees any allocated resources
103 //=================================================================================
104 MeasureGUI_DistanceDlg::~MeasureGUI_DistanceDlg()
105 {
106 }
107
108
109 //=================================================================================
110 // function : Init()
111 // purpose  :
112 //=================================================================================
113 void MeasureGUI_DistanceDlg::Init()
114 {
115   mySelBtn   = myGrp->PushButton1;
116   mySelEdit  = myGrp->LineEdit1;
117   mySelBtn2  = myGrp->PushButton2;
118   mySelEdit2 = myGrp->LineEdit2;
119   
120   myEditCurrentArgument = mySelEdit;
121
122   connect( mySelEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
123   connect( mySelBtn2,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
124
125   MeasureGUI_Skeleton::Init();
126 }
127
128
129 //=================================================================================
130 // function : SelectionIntoArgument()
131 // purpose  : Called when selection has changed
132 //=================================================================================
133 void MeasureGUI_DistanceDlg::SelectionIntoArgument()
134 {
135   Standard_Boolean testResult = Standard_False;
136   GEOM::GEOM_Object_var aSelectedObject =
137     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
138
139   if ( !testResult )
140     aSelectedObject = GEOM::GEOM_Object::_nil();
141
142   if ( myEditCurrentArgument == mySelEdit )
143     myObj = aSelectedObject;
144   else
145     myObj2 = aSelectedObject;
146
147   processObject();
148 }
149
150 //=================================================================================
151 // function : processObject()
152 // purpose  : Fill dialogs fields in accordance with myObj and myObj2
153 //=================================================================================
154 void MeasureGUI_DistanceDlg::processObject()
155 {
156   myGrp->LineEdit1->setText( !myObj->_is_nil()  ? GEOMBase::GetName( myObj  ) : "" );
157   myGrp->LineEdit2->setText( !myObj2->_is_nil() ? GEOMBase::GetName( myObj2 ) : "" );
158
159   gp_Pnt aPnt1, aPnt2;
160   double aDist = 0.;
161   if ( getParameters( aDist, aPnt1, aPnt2 ) ) {
162     myGrp->LineEdit3->setText( DlgRef::PrintDoubleValue( aDist ) );
163
164     gp_XYZ aVec = aPnt2.XYZ() - aPnt1.XYZ();
165     myGrp->LineEdit4->setText( DlgRef::PrintDoubleValue( aVec.X() ) );
166     myGrp->LineEdit5->setText( DlgRef::PrintDoubleValue( aVec.Y() ) );
167     myGrp->LineEdit6->setText( DlgRef::PrintDoubleValue( aVec.Z() ) );
168
169     redisplayPreview();
170   }
171   else {
172     myGrp->LineEdit3->setText( "" );
173     myGrp->LineEdit4->setText( "" );
174     myGrp->LineEdit5->setText( "" );
175     myGrp->LineEdit6->setText( "" );
176     erasePreview();
177   }
178 }
179
180 //=================================================================================
181 // function : getParameters()
182 // purpose  : Get distance between objects
183 //=================================================================================
184 bool MeasureGUI_DistanceDlg::getParameters( double& theDistance,
185                                             gp_Pnt& thePnt1,
186                                             gp_Pnt& thePnt2 )
187 {
188   QString msg;
189   if ( !isValid( msg ) )
190     return false;
191   else {
192     try {
193       double x1, y1, z1, x2, y2, z2;
194       theDistance = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetMinDistance(
195         myObj, myObj2, x1, y1, z1, x2, y2, z2 );
196
197       thePnt1.SetCoord( x1, y1, z1 );
198       thePnt2.SetCoord( x2, y2, z2 );
199     }
200     catch( const SALOME::SALOME_Exception& e ) {
201       SalomeApp_Tools::QtCatchCorbaException( e );
202       return false;
203     }
204
205     return getOperation()->IsDone();
206   }
207 }
208
209
210 //=================================================================================
211 // function : SetEditCurrentArgument()
212 // purpose  :
213 //=================================================================================
214 void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
215 {
216   QPushButton* send = ( QPushButton* )sender();
217
218   if ( send == mySelBtn ) {
219     mySelEdit->setFocus();
220     myEditCurrentArgument = mySelEdit;
221   }
222   else {
223     mySelEdit2->setFocus();
224     myEditCurrentArgument = mySelEdit2;
225   }
226
227   SelectionIntoArgument();
228 }
229
230
231 //=================================================================================
232 // function : LineEditReturnPressed()
233 // purpose  :
234 //=================================================================================
235 void MeasureGUI_DistanceDlg::LineEditReturnPressed()
236 {
237   QLineEdit* send = ( QLineEdit* )sender();
238
239   if ( send == mySelEdit )
240     myEditCurrentArgument = mySelEdit;
241   else
242     myEditCurrentArgument = mySelEdit2;
243
244   if ( GEOMBase::SelectionByNameInDialogs( this, mySelEdit->text(), selectedIO() ) )
245     mySelEdit->setText( mySelEdit->text() );
246 }
247
248
249 //=================================================================================
250 // function : buildPrs()
251 // purpose  :
252 //=================================================================================
253 SALOME_Prs* MeasureGUI_DistanceDlg::buildPrs()
254 {
255   double aDist = 0.;
256   gp_Pnt aPnt1( 0, 0, 0 ), aPnt2( 0, 0, 0 );
257   
258
259   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
260
261   if ( myObj->_is_nil() || myObj2->_is_nil() ||
262        !getParameters( aDist, aPnt1, aPnt2 ) ||
263        vw->getViewManager()->getType() != OCCViewer_Viewer::Type() )
264     return 0;
265   
266   try
267   {
268     if ( aDist <= 1.e-9 ) {
269       BRepBuilderAPI_MakeVertex aMaker( aPnt1 );
270       return getDisplayer()->BuildPrs( aMaker.Vertex() );
271     }
272     else {
273       BRepBuilderAPI_MakeEdge MakeEdge( aPnt1, aPnt2 );
274       TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex( aPnt1 );
275       TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex( aPnt2 );
276
277       QString aLabel;
278       aLabel.sprintf( "%.1f", aDist );
279
280       gp_Pnt aPnt3( ( aPnt1.X() + aPnt2.X() ) / 2,
281                     ( aPnt1.Y() + aPnt2.Y() ) / 2,
282                     ( aPnt1.Z() + aPnt2.Z() ) / 2 + 100 );
283
284       gp_Vec va( aPnt3, aPnt1 );
285       gp_Vec vb( aPnt3, aPnt2 );
286
287       if ( va.IsParallel( vb, Precision::Angular() ) ) {
288         aPnt3.SetY( ( aPnt1.Y() + aPnt2.Y() ) / 2 + 100 );
289         aPnt3.SetZ( ( aPnt1.Z() + aPnt2.Z() ) / 2 );
290       }
291       
292       gce_MakePln gce_MP( aPnt1, aPnt2, aPnt3 );
293       Handle( Geom_Plane ) P = new Geom_Plane( gce_MP.Value() );
294
295       Handle( AIS_LengthDimension ) anIO = new AIS_LengthDimension(
296         aVert1, aVert2, P, aDist, TCollection_ExtendedString( (Standard_CString)aLabel.toLatin1().constData() ) );
297       anIO->SetArrowSize( aDist/20 );
298
299       SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>( ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs( 0 ) );
300
301       if ( aPrs )
302         aPrs->AddObject( anIO );
303
304       return aPrs;
305     }
306   }
307   catch( Standard_Failure ) {
308     return 0;
309   }
310 }
311
312 //=================================================================================
313 // function : isValid()
314 // purpose  :
315 //=================================================================================
316 bool MeasureGUI_DistanceDlg::isValid( QString& msg )
317 {
318   return MeasureGUI_Skeleton::isValid( msg ) && !myObj2->_is_nil();
319 }