Salome HOME
integration of modifications from Gerald Nicolas
[modules/homard.git] / src / HOMARDGUI / MonCreateBoundaryAn.cxx
1 // Copyright (C) 2011-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 using namespace std;
21
22 #include "MonCreateBoundaryAn.h"
23 #include "MonCreateCase.h"
24
25 #include <QFileDialog>
26 #include <QMessageBox>
27
28 #include "SalomeApp_Tools.h"
29 #include "HOMARDGUI_Utils.h"
30 #include "HomardQtCommun.h"
31 #include <utilities.h>
32
33 #include <SUIT_Desktop.h>
34 #include <SUIT_MessageBox.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_ViewManager.h>
38
39 // ------------------------------------------------------------------------------------------------------------------------
40 MonCreateBoundaryAn::MonCreateBoundaryAn(MonCreateCase* parent, bool modal,
41                              HOMARD::HOMARD_Gen_var myHomardGen,
42                              QString caseName) :
43 // ------------------------------------------------------------------------------------------------------------------------------
44 /* Constructs a MonCreateBoundaryAn
45    appele pour une vraie creation
46    initialise un cylindre et non une sphere
47 */
48     QDialog(0), Ui_CreateBoundaryAn(),
49     _parent(parent),
50     _aName (""),
51     _aCaseName(caseName),
52     _Type(1),
53     _BoundaryAnXcentre(0), _BoundaryAnYcentre(0), _BoundaryAnZcentre(0), _BoundaryAnRayon(0),
54     _BoundaryAnXaxis(0), _BoundaryAnYaxis(0), _BoundaryAnZaxis(0),
55     _Xcentre(0), _Ycentre(0), _Zcentre(0), _Rayon(0),
56     _Xmin(0), _Xmax(0), _Xincr(0), _Ymin(0), _Ymax(0), _Yincr(0), _Zmin(0), _Zmax(0), _Zincr(0), _DMax(0),
57     Chgt (false)
58     {
59       MESSAGE("Constructeur") ;
60       _myHomardGen=HOMARD::HOMARD_Gen::_duplicate(myHomardGen);
61       setupUi(this);
62       setModal(modal);
63
64     //  Gestion des icones
65       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
66       QPixmap pix = resMgr->loadPixmap( "HOMARD", "spherepoint.png" );
67       QIcon IS=QIcon(pix);
68       RBSphere->setIcon(IS);
69       QPixmap pix2 = resMgr->loadPixmap( "HOMARD", "cylinderpointvector.png" );
70       QIcon IS2=QIcon(pix2);
71       RBCylindre->setIcon(IS2);
72
73       InitConnect( );
74
75       SetNewBoundaryAnName() ;
76       InitValBoundaryAn();          // Cherche les valeurs de la boite englobante le maillage
77       InitMinMax();                 // Initialise les bornes des boutons
78       SetCylinder();                // Propose un cylindre en premier choix
79     }
80 // --------------------------------------------------------------------------------------------------------------
81 MonCreateBoundaryAn::MonCreateBoundaryAn(MonCreateCase* parent,
82                              HOMARD::HOMARD_Gen_var myHomardGen,
83                              QString caseName):
84 // --------------------------------------------------------------------------------------------------------------
85 // Constructeur appele par MonEditBoundaryAn
86 //
87     QDialog(0), Ui_CreateBoundaryAn(),
88      _myHomardGen(myHomardGen),
89     _parent(parent),
90     _aName (""),
91     _aCaseName(caseName),
92     _Type(1),
93     _BoundaryAnXcentre(0), _BoundaryAnYcentre(0), _BoundaryAnZcentre(0), _BoundaryAnRayon(0),
94     _BoundaryAnXaxis(0), _BoundaryAnYaxis(0), _BoundaryAnZaxis(0),
95     // Pour affichage lors de l edition d une BoundaryAn sans nom de Cas
96     _Xcentre(0), _Ycentre(0), _Zcentre(0), _Rayon(0),
97     _Xmin(1), _Xmax(1), _Xincr(1), _Ymin(1), _Ymax(1), _Yincr(1), _Zmin(1), _Zmax(1), _Zincr(1), _DMax(1),
98     Chgt (false)
99     {
100   //  MESSAGE("Debut de  MonCreateBoundaryAn")
101       setupUi(this);
102       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
103       QPixmap pix = resMgr->loadPixmap( "HOMARD", "spherepoint.png" );
104       QIcon IS=QIcon(pix);
105       RBSphere->setIcon(IS);
106       QPixmap pix2 = resMgr->loadPixmap( "HOMARD", "cylinderpointvector.png" );
107       QIcon IS2=QIcon(pix2);
108       RBCylindre->setIcon(IS2);
109       setModal(true);
110       InitConnect();
111     }
112
113 // ------------------------------------------------------------------------
114 MonCreateBoundaryAn::~MonCreateBoundaryAn()
115 // ------------------------------------------------------------------------
116 {
117     // no need to delete child widgets, Qt does it all for us
118 }
119 // ------------------------------------------------------------------------
120 void MonCreateBoundaryAn::InitConnect()
121 // ------------------------------------------------------------------------
122 {
123     connect( RBCylindre,   SIGNAL(clicked()) , this, SLOT(SetCylinder()) ) ;
124     connect( RBSphere,     SIGNAL(clicked()) , this, SLOT(SetSphere()) ) ;
125     connect( buttonOk,     SIGNAL( pressed() ), this, SLOT( PushOnOK() ) );
126     connect( buttonApply,  SIGNAL( pressed() ), this, SLOT( PushOnApply() ) );
127     connect( buttonCancel, SIGNAL( pressed() ), this, SLOT( close() ) );
128     connect( buttonHelp,   SIGNAL( pressed() ), this, SLOT( PushOnHelp() ) );
129 }
130 // ------------------------------------------------------------------------
131 void MonCreateBoundaryAn::InitValBoundaryAn()
132 // ------------------------------------------------------------------------
133 {
134 //
135 //  1. Les coordonnees extremes du maillage
136 //
137     if (_aCaseName == QString("")) { return; }
138
139     HOMARD::HOMARD_Cas_var aCas = _myHomardGen->GetCase(_aCaseName.toStdString().c_str());
140     HOMARD::extrema_var  MesExtremes = aCas->GetBoundingBox();
141     int num = MesExtremes->length() ;
142     ASSERT(num == 10);
143     _Xmin=MesExtremes[0]; _Xmax=MesExtremes[1]; _Xincr=MesExtremes[2];
144     _Ymin=MesExtremes[3]; _Ymax=MesExtremes[4]; _Yincr=MesExtremes[5];
145     _Zmin=MesExtremes[6]; _Zmax=MesExtremes[7]; _Zincr=MesExtremes[8];
146     _DMax=MesExtremes[9];
147      MESSAGE ("_Xmin : " << _Xmin << " _Xmax : " << _Xmax << " _Xincr : " << _Xincr ) ;
148      MESSAGE ("_Ymin : " << _Ymin << " _Ymax : " << _Ymax << " _Yincr : " << _Yincr ) ;
149      MESSAGE ("_Zmin : " << _Zmin << " _Zmax : " << _Zmax << " _Zincr : " << _Zincr) ;
150      MESSAGE ("_DMax : " << _DMax);
151
152 //  2. Caracteristiques des frontieres
153 // en X
154     _Xcentre=(_Xmin + _Xmax)/2.;
155 // en Y
156     _Ycentre=(_Ymin + _Ymax)/2.;
157 // en Z
158     _Zcentre=(_Zmin + _Zmax)/2.;
159 // Rayon
160     _Rayon= _DMax/4.;
161 }
162
163 // ------------------------------------------------------------------------
164 void MonCreateBoundaryAn::InitMinMax()
165 // ------------------------------------------------------------------------
166 {
167 // en X
168   SpinBox_Xaxis->setSingleStep(0.1);
169   SpinBox_Xcentre->setSingleStep(_Xincr);
170 //en Y
171   SpinBox_Yaxis->setSingleStep(0.1);
172   SpinBox_Ycentre->setSingleStep(_Yincr);
173 //en Z
174   SpinBox_Zaxis->setSingleStep(0.1);
175   SpinBox_Zcentre->setSingleStep(_Zincr);
176 // Rayon
177   SpinBox_Radius->setSingleStep(_Rayon/10.);
178   SpinBox_Radius->setMinimum(0.);
179   SpinBox_Rayon->setSingleStep(_Rayon/10.);
180   SpinBox_Rayon->setMinimum(0.);
181 }
182 // ------------------------------------------------------------------------
183 bool MonCreateBoundaryAn::PushOnApply()
184 // ------------------------------------------------------------------------
185 // Appele lorsque l'un des boutons Ok ou Apply est presse
186 //
187 {
188   std::cerr << LEBoundaryName->text().trimmed().toStdString() << std::endl;
189   if (LEBoundaryName->text().trimmed()=="")
190   {
191     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
192                               QObject::tr("HOM_BOUN_NAME") );
193     return false;
194   }
195
196   switch (_Type)
197   {
198       case 1 : // il s agit d un cylindre
199       {
200         if ((_BoundaryAnXcentre != SpinBox_Xcent->value())  or
201             (_BoundaryAnYcentre != SpinBox_Ycent->value())  or
202             (_BoundaryAnZcentre != SpinBox_Zcent->value())  or
203             (_BoundaryAnRayon   != SpinBox_Radius->value()) or
204             (_BoundaryAnXaxis   != SpinBox_Xaxis->value()) or
205             (_BoundaryAnYaxis   != SpinBox_Yaxis->value()) or
206             (_BoundaryAnZaxis   != SpinBox_Zaxis->value()) )
207         {
208           Chgt = true;
209           _BoundaryAnXaxis= SpinBox_Xaxis->value();
210           _BoundaryAnYaxis= SpinBox_Yaxis->value();
211           _BoundaryAnZaxis= SpinBox_Zaxis->value();
212           _BoundaryAnXcentre=SpinBox_Xcent->value();
213           _BoundaryAnYcentre=SpinBox_Ycent->value();
214           _BoundaryAnZcentre=SpinBox_Zcent->value();
215           _BoundaryAnRayon=SpinBox_Radius->value();
216           double daux = _BoundaryAnXaxis*_BoundaryAnXaxis + _BoundaryAnYaxis*_BoundaryAnYaxis + _BoundaryAnZaxis*_BoundaryAnZaxis ;
217           if ( daux < 0.0000001 )
218           {
219             QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
220                                       QObject::tr("HOM_AXE") );
221             return false;
222           }
223         }
224         break;
225       }
226
227       case 2 : // il s agit d une sphere
228       {
229         if ((_BoundaryAnXcentre != SpinBox_Xcentre->value()) or
230             (_BoundaryAnYcentre != SpinBox_Ycentre->value()) or
231             (_BoundaryAnZcentre != SpinBox_Zcentre->value()) or
232             (_BoundaryAnRayon   != SpinBox_Rayon->value())  )
233         {
234            Chgt = true;
235           _BoundaryAnXcentre=SpinBox_Xcentre->value();
236           _BoundaryAnYcentre=SpinBox_Ycentre->value();
237           _BoundaryAnZcentre=SpinBox_Zcentre->value();
238           _BoundaryAnRayon=SpinBox_Rayon->value();
239         }
240         break;
241       }
242   }
243
244   bool bOK = CreateOrUpdateBoundaryAn();
245   return bOK;
246
247 }
248 // ---------------------------------------------------
249 bool MonCreateBoundaryAn:: CreateOrUpdateBoundaryAn()
250 //----------------------------------------------------
251 //  Creation de l'objet boundary
252 {
253   if (_aName != LEBoundaryName->text().trimmed())
254   {
255     _aName = LEBoundaryName->text().trimmed();
256     switch (_Type)
257     {
258       case 1 : // il s agit d un cylindre
259       {
260         aBoundaryAn = _myHomardGen->CreateBoundaryCylinder(CORBA::string_dup(_aName.toStdString().c_str()), \
261         _BoundaryAnXcentre, _BoundaryAnYcentre, _BoundaryAnZcentre, _BoundaryAnXaxis, _BoundaryAnYaxis, _BoundaryAnZaxis, _BoundaryAnRayon );
262         break;
263       }
264       case 2 : // il s agit d une sphere
265       {
266         aBoundaryAn = _myHomardGen->CreateBoundarySphere(CORBA::string_dup(_aName.toStdString().c_str()), \
267         _BoundaryAnXcentre, _BoundaryAnYcentre, _BoundaryAnZcentre, _BoundaryAnRayon);
268         break;
269       }
270     }
271     _parent->AddBoundaryAn(_aName);
272   }
273 // Mise en place des attributs
274   aBoundaryAn->SetLimit(_Xincr, _Yincr, _Zincr);
275
276   HOMARD_UTILS::updateObjBrowser();
277   return true;
278 }
279 // ------------------------------------------------------------------------
280 void MonCreateBoundaryAn::PushOnOK()
281 // ------------------------------------------------------------------------
282 {
283      if (PushOnApply()) this->close();
284 }
285 // ------------------------------------------------------------------------
286 void MonCreateBoundaryAn::PushOnHelp()
287 // ------------------------------------------------------------------------
288 {
289   HOMARD_UTILS::PushOnHelp(QString("gui_create_boundary.html#un-cylindre") ) ;
290 }
291
292 // -----------------------------------
293 void MonCreateBoundaryAn::SetNewBoundaryAnName()
294 // -----------------------------------
295 {
296 // Recherche d'un nom par defaut qui n'existe pas encore
297
298   HOMARD::listeBoundarys_var  MyBoundaryAns = _myHomardGen->GetAllBoundarysName();
299   int num = 0; QString aBoundaryAnName="";
300   while (aBoundaryAnName=="" )
301   {
302     aBoundaryAnName.setNum(num+1) ;
303     aBoundaryAnName.insert(0, QString("Boun_")) ;
304     for ( int i=0; i<MyBoundaryAns->length(); i++)
305     {
306       if ( aBoundaryAnName ==  QString(MyBoundaryAns[i]))
307       {
308           num=num+1;
309           aBoundaryAnName="";
310           break;
311       }
312    }
313   }
314   LEBoundaryName->clear() ;
315   LEBoundaryName->insert(aBoundaryAnName);
316 }
317 // ------------------------------------------------------------------------
318 void MonCreateBoundaryAn::SetCylinder()
319 // ------------------------------------------------------------------------
320 {
321   MESSAGE("Debut de SetCylinder")
322   gBCylindre->setVisible(1);
323   gBSphere->setVisible(0);
324   adjustSize();
325   _Type=1;
326   SpinBox_Xcent->setValue(_Xcentre);
327   SpinBox_Xaxis->setValue(0.);
328   SpinBox_Ycent->setValue(_Ycentre);
329   SpinBox_Yaxis->setValue(0.);
330   SpinBox_Zcent->setValue(_Zcentre);
331   SpinBox_Zaxis->setValue(1.);
332   SpinBox_Radius->setValue(_Rayon);
333 //   MESSAGE("Fin de SetCylinder")
334 }
335 // ------------------------------------------------------------------------
336 void MonCreateBoundaryAn::SetSphere()
337 // ------------------------------------------------------------------------
338 {
339   MESSAGE("Debut de SetSphere")
340   gBCylindre->setVisible(0);
341   gBSphere->setVisible(1);
342   adjustSize();
343   _Type=2;
344   SpinBox_Xcentre->setValue(_Xcentre);
345   SpinBox_Ycentre->setValue(_Ycentre);
346   SpinBox_Zcentre->setValue(_Zcentre);
347   SpinBox_Rayon->setValue(_Rayon);
348 //   MESSAGE("Fin de SetSphere")
349 }
350
351