1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : MeasureGUI_DistanceDlg.cxx
23 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 #include "MeasureGUI_DistanceDlg.h"
27 #include "MeasureGUI_Widgets.h"
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>
38 #include <SOCC_ViewModel.h>
39 #include <SalomeApp_Tools.h>
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>
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 )
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" ) ) );
63 setWindowTitle( tr( "GEOM_MINDIST_TITLE" ) );
65 /***************************************************************/
67 mainFrame()->GroupConstructors->setTitle( tr( "GEOM_DISTANCE" ) );
68 mainFrame()->RadioButton1->setIcon( image0 );
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 );
87 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
88 layout->setMargin( 0 ); layout->setSpacing( 6 );
89 layout->addWidget( myGrp );
91 /***************************************************************/
93 myHelpFileName = "using_measurement_tools_page.html#min_distance_anchor";
100 //=================================================================================
101 // function : ~MeasureGUI_DistanceDlg()
102 // purpose : Destroys the object and frees any allocated resources
103 //=================================================================================
104 MeasureGUI_DistanceDlg::~MeasureGUI_DistanceDlg()
109 //=================================================================================
112 //=================================================================================
113 void MeasureGUI_DistanceDlg::Init()
115 mySelBtn = myGrp->PushButton1;
116 mySelEdit = myGrp->LineEdit1;
117 mySelBtn2 = myGrp->PushButton2;
118 mySelEdit2 = myGrp->LineEdit2;
120 myEditCurrentArgument = mySelEdit;
122 connect( mySelEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
123 connect( mySelBtn2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
125 MeasureGUI_Skeleton::Init();
129 //=================================================================================
130 // function : SelectionIntoArgument()
131 // purpose : Called when selection has changed
132 //=================================================================================
133 void MeasureGUI_DistanceDlg::SelectionIntoArgument()
135 Standard_Boolean testResult = Standard_False;
136 GEOM::GEOM_Object_var aSelectedObject =
137 GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
140 aSelectedObject = GEOM::GEOM_Object::_nil();
142 if ( myEditCurrentArgument == mySelEdit )
143 myObj = aSelectedObject;
145 myObj2 = aSelectedObject;
150 //=================================================================================
151 // function : processObject()
152 // purpose : Fill dialogs fields in accordance with myObj and myObj2
153 //=================================================================================
154 void MeasureGUI_DistanceDlg::processObject()
156 myGrp->LineEdit1->setText( !myObj->_is_nil() ? GEOMBase::GetName( myObj ) : "" );
157 myGrp->LineEdit2->setText( !myObj2->_is_nil() ? GEOMBase::GetName( myObj2 ) : "" );
161 if ( getParameters( aDist, aPnt1, aPnt2 ) ) {
162 myGrp->LineEdit3->setText( DlgRef::PrintDoubleValue( aDist ) );
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() ) );
172 myGrp->LineEdit3->setText( "" );
173 myGrp->LineEdit4->setText( "" );
174 myGrp->LineEdit5->setText( "" );
175 myGrp->LineEdit6->setText( "" );
180 //=================================================================================
181 // function : getParameters()
182 // purpose : Get distance between objects
183 //=================================================================================
184 bool MeasureGUI_DistanceDlg::getParameters( double& theDistance,
189 if ( !isValid( msg ) )
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 );
197 thePnt1.SetCoord( x1, y1, z1 );
198 thePnt2.SetCoord( x2, y2, z2 );
200 catch( const SALOME::SALOME_Exception& e ) {
201 SalomeApp_Tools::QtCatchCorbaException( e );
205 return getOperation()->IsDone();
210 //=================================================================================
211 // function : SetEditCurrentArgument()
213 //=================================================================================
214 void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
216 QPushButton* send = ( QPushButton* )sender();
218 if ( send == mySelBtn ) {
219 mySelEdit->setFocus();
220 myEditCurrentArgument = mySelEdit;
223 mySelEdit2->setFocus();
224 myEditCurrentArgument = mySelEdit2;
227 SelectionIntoArgument();
231 //=================================================================================
232 // function : LineEditReturnPressed()
234 //=================================================================================
235 void MeasureGUI_DistanceDlg::LineEditReturnPressed()
237 QLineEdit* send = ( QLineEdit* )sender();
239 if ( send == mySelEdit )
240 myEditCurrentArgument = mySelEdit;
242 myEditCurrentArgument = mySelEdit2;
244 if ( GEOMBase::SelectionByNameInDialogs( this, mySelEdit->text(), selectedIO() ) )
245 mySelEdit->setText( mySelEdit->text() );
249 //=================================================================================
250 // function : buildPrs()
252 //=================================================================================
253 SALOME_Prs* MeasureGUI_DistanceDlg::buildPrs()
256 gp_Pnt aPnt1( 0, 0, 0 ), aPnt2( 0, 0, 0 );
259 SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
261 if ( myObj->_is_nil() || myObj2->_is_nil() ||
262 !getParameters( aDist, aPnt1, aPnt2 ) ||
263 vw->getViewManager()->getType() != OCCViewer_Viewer::Type() )
268 if ( aDist <= 1.e-9 ) {
269 BRepBuilderAPI_MakeVertex aMaker( aPnt1 );
270 return getDisplayer()->BuildPrs( aMaker.Vertex() );
273 BRepBuilderAPI_MakeEdge MakeEdge( aPnt1, aPnt2 );
274 TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex( aPnt1 );
275 TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex( aPnt2 );
278 aLabel.sprintf( "%.1f", aDist );
280 gp_Pnt aPnt3( ( aPnt1.X() + aPnt2.X() ) / 2,
281 ( aPnt1.Y() + aPnt2.Y() ) / 2,
282 ( aPnt1.Z() + aPnt2.Z() ) / 2 + 100 );
284 gp_Vec va( aPnt3, aPnt1 );
285 gp_Vec vb( aPnt3, aPnt2 );
287 if ( va.IsParallel( vb, Precision::Angular() ) ) {
288 aPnt3.SetY( ( aPnt1.Y() + aPnt2.Y() ) / 2 + 100 );
289 aPnt3.SetZ( ( aPnt1.Z() + aPnt2.Z() ) / 2 );
292 gce_MakePln gce_MP( aPnt1, aPnt2, aPnt3 );
293 Handle( Geom_Plane ) P = new Geom_Plane( gce_MP.Value() );
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 );
299 SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>( ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs( 0 ) );
302 aPrs->AddObject( anIO );
307 catch( Standard_Failure ) {
312 //=================================================================================
313 // function : isValid()
315 //=================================================================================
316 bool MeasureGUI_DistanceDlg::isValid( QString& msg )
318 return MeasureGUI_Skeleton::isValid( msg ) && !myObj2->_is_nil();