Salome HOME
3544009ac56527bc54f90a29eaedc62a6d77b5e3
[modules/geom.git] / src / MeasureGUI / MeasureGUI_BndBoxDlg.cxx
1 // Copyright (C) 2007-2012  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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : MeasureGUI_BndBoxDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 //
27 #include "MeasureGUI_BndBoxDlg.h"
28 #include "MeasureGUI_Widgets.h"
29
30 #include "GeometryGUI.h"
31
32 #include <GEOMBase.h>
33 #include <DlgRef.h>
34
35 #include <GEOM_Function.hxx>
36 #include <GEOM_Object.hxx>
37
38 #include <BRepPrimAPI_MakeBox.hxx>
39 #include <BRepAdaptor_Surface.hxx>
40 #include <BRep_Tool.hxx>
41 #include <BRep_TFace.hxx>
42 #include <TopoDS.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopExp_Explorer.hxx>
45
46 #include <SUIT_Session.h>
47 #include <SUIT_ResourceMgr.h>
48 #include <SalomeApp_Tools.h>
49
50 // #include <qlineedit.h>
51 // #include <qlabel.h>
52 // #include <qlayout.h>
53 // #include <qpushbutton.h>
54 // #include <qradiobutton.h>
55 // #include <qbuttongroup.h>
56
57 //=================================================================================
58 // class    : MeasureGUI_BndBoxDlg()
59 // purpose  : Constructs a MeasureGUI_BndBoxDlg which is a child of 'parent', with the 
60 //            name 'name' and widget flags set to 'f'.
61 //            The dialog will by default be modeless, unless you set 'modal' to
62 //            true to construct a modal dialog.
63 //=================================================================================
64 MeasureGUI_BndBoxDlg::MeasureGUI_BndBoxDlg( GeometryGUI* GUI, QWidget* parent )
65   : MeasureGUI_Skeleton( GUI, parent )
66 {
67   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap(
68     "GEOM", tr( "ICON_DLG_BOUNDING_BOX" ) ) );
69   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap(
70     "GEOM", tr( "ICON_SELECT" ) ) );
71
72   setWindowTitle( tr( "GEOM_BNDBOX_TITLE" ) );
73
74   /***************************************************************/
75   
76   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_BNDBOX" ) );
77   mainFrame()->RadioButton1->setIcon( image0 );
78
79   myGrp = new MeasureGUI_1Sel6LineEdit( centralWidget() );
80   myGrp->GroupBox1->setTitle( tr( "GEOM_BNDBOX_OBJDIM" ) );
81   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
82   myGrp->TextLabel2->setText( tr( "GEOM_MIN" ) );
83   myGrp->TextLabel3->setText( tr( "GEOM_MAX" ) );
84   myGrp->TextLabel4->setText( tr( "GEOM_X" ) );
85   myGrp->TextLabel5->setText( tr( "GEOM_Y" ) );
86   myGrp->TextLabel6->setText( tr( "GEOM_Z" ) );
87   myGrp->LineEdit11->setReadOnly( true );
88   myGrp->LineEdit12->setReadOnly( true );
89   myGrp->LineEdit21->setReadOnly( true );
90   myGrp->LineEdit22->setReadOnly( true );
91   myGrp->LineEdit31->setReadOnly( true );
92   myGrp->LineEdit32->setReadOnly( true );
93   myGrp->LineEdit11->setMinimumWidth(120);
94   myGrp->LineEdit12->setMinimumWidth(120);
95   myGrp->LineEdit21->setMinimumWidth(120);
96   myGrp->LineEdit22->setMinimumWidth(120);
97   myGrp->LineEdit31->setMinimumWidth(120);
98   myGrp->LineEdit32->setMinimumWidth(120);
99   myGrp->PushButton1->setIcon( image1 );
100   myGrp->LineEdit1->setReadOnly( true );
101
102   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
103   layout->setMargin( 0 ); layout->setSpacing( 6 );
104   layout->addWidget( myGrp );
105   
106   /***************************************************************/
107
108   myHelpFileName = "using_measurement_tools_page.html#bounding_box_anchor";
109
110   /* Initialisation */
111   Init();
112 }
113
114
115 //=================================================================================
116 // function : ~MeasureGUI_BndBoxDlg()
117 // purpose  : Destroys the object and frees any allocated resources
118 //=================================================================================
119 MeasureGUI_BndBoxDlg::~MeasureGUI_BndBoxDlg()
120 {
121 }
122
123
124 //=================================================================================
125 // function : Init()
126 // purpose  :
127 //=================================================================================
128 void MeasureGUI_BndBoxDlg::Init()
129 {
130   mySelBtn = myGrp->PushButton1;
131   mySelEdit = myGrp->LineEdit1;
132   MeasureGUI_Skeleton::Init();
133 }
134
135 //=================================================================================
136 // function : processObject
137 // purpose  :
138 //=================================================================================
139 void MeasureGUI_BndBoxDlg::processObject()
140 {
141   double aXMin, aXMax, aYMin, aYMax, aZMin, aZMax;
142
143   if ( !getParameters( aXMin, aXMax, aYMin, aYMax, aZMin, aZMax ) ) {
144     mySelEdit->setText( "" );
145     myGrp->LineEdit11->setText( "" );
146     myGrp->LineEdit12->setText( "" );
147     myGrp->LineEdit21->setText( "" );
148     myGrp->LineEdit22->setText( "" );
149     myGrp->LineEdit31->setText( "" );
150     myGrp->LineEdit32->setText( "" );
151   }
152   else {
153     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
154     int aPrecision = resMgr->integerValue( "Geometry", "length_precision", 6 );
155
156     myGrp->LineEdit11->setText( DlgRef::PrintDoubleValue( aXMin, aPrecision ) );
157     myGrp->LineEdit12->setText( DlgRef::PrintDoubleValue( aXMax, aPrecision ) );
158
159     myGrp->LineEdit21->setText( DlgRef::PrintDoubleValue( aYMin, aPrecision ) );
160     myGrp->LineEdit22->setText( DlgRef::PrintDoubleValue( aYMax, aPrecision ) );
161
162     myGrp->LineEdit31->setText( DlgRef::PrintDoubleValue( aZMin, aPrecision ) );
163     myGrp->LineEdit32->setText( DlgRef::PrintDoubleValue( aZMax, aPrecision ) );
164   }
165 }
166
167 //=================================================================================
168 // function : getParameters
169 // purpose  :
170 //=================================================================================
171 bool MeasureGUI_BndBoxDlg::getParameters( double& theXmin, double& theXmax,
172                                           double& theYmin, double& theYmax,
173                                           double& theZmin, double& theZmax )
174 {
175   if ( myObj->_is_nil() )
176     return false;
177   else {
178     GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
179     try {
180       Handle(Poly_Triangulation) Trtn = 0; 
181
182       GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
183       if ( CORBA::is_nil(aGeomGen) )
184         return false;
185
186       QString IOR = GEOMBase::GetIORFromObject( myObj );
187       GEOM::GEOM_Object_var anObject = aGeomGen->GetIORFromString( IOR.toLatin1().constData() );
188       if ( CORBA::is_nil(anObject) )
189         return false;
190
191       TopoDS_Shape aShape;
192       GEOMBase::GetShape(anObject, aShape, TopAbs_SHAPE);
193       if ( aShape.IsNull() )
194         return false;
195       
196       TopLoc_Location l;
197       Handle(Poly_Triangulation) T;
198       TopExp_Explorer ex;
199       for (ex.Init(aShape,TopAbs_FACE); ex.More(); ex.Next()) {
200         const TopoDS_Face& F = TopoDS::Face(ex.Current());
201         BRepAdaptor_Surface surf(F);
202         if (surf.GetType() == GeomAbs_Sphere) {
203           T = BRep_Tool::Triangulation(F, l);
204           if (!T.IsNull()) {
205             Handle(Poly_Triangulation) NullTrtn = 0;
206             (*((Handle(BRep_TFace)*)&F.TShape()))->Triangulation(NullTrtn);
207             Trtn = T;
208             break;
209           }
210         }
211         else
212           break;
213       }
214       
215       anOper->GetBoundingBox( myObj, theXmin, theXmax, theYmin, theYmax, theZmin, theZmax );
216       
217       if (!Trtn.IsNull()) {
218         TopLoc_Location l;
219         Handle(Poly_Triangulation) T;
220         TopExp_Explorer ex;
221         for (ex.Init(aShape,TopAbs_FACE); ex.More(); ex.Next()) {
222           const TopoDS_Face& F = TopoDS::Face(ex.Current());
223           (*((Handle(BRep_TFace)*)&F.TShape()))->Triangulation(Trtn);
224           break;
225         }
226       }
227       
228     }
229     catch( const SALOME::SALOME_Exception& e ) {
230       SalomeApp_Tools::QtCatchCorbaException( e );
231       return false;
232     }
233
234     return anOper->IsDone();
235   }
236 }
237
238 //=================================================================================
239 // function : buildPrs
240 // purpose  :
241 //=================================================================================
242 SALOME_Prs* MeasureGUI_BndBoxDlg::buildPrs()
243 {
244   double aXMin, aYMin, aZMin, aXMax, aYMax, aZMax;
245
246   if ( myObj->_is_nil() || !getParameters( aXMin, aXMax, aYMin, aYMax, aZMin, aZMax ) )
247     return 0;
248
249   TopoDS_Shape aShape = BRepPrimAPI_MakeBox( gp_Pnt( aXMin, aYMin, aZMin ),
250                                              gp_Pnt( aXMax, aYMax, aZMax ) ).Shape();
251        
252   return !aShape.IsNull() ? getDisplayer()->BuildPrs( aShape ) : 0;
253
254 }