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