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