Salome HOME
correct previous integration (Porting to Python 2.6)
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Make2DFrom3DOp.cxx
1 //  Copyright (C) 2007-2009  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 // File   : SMESHGUI_Make2DFrom3DOp.cxx
23 // Author : Open CASCADE S.A.S.
24 // SMESH includes
25 //
26 #include "SMESHGUI_Make2DFrom3DOp.h"
27
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_Utils.h"
30 #include "SMESHGUI_VTKUtils.h"
31 #include "SMESHGUI_MeshUtils.h"
32 #include "SMESHGUI_MeshInfosBox.h"
33
34 // SALOME GUI includes
35 #include <LightApp_SelectionMgr.h>
36
37 #include <SUIT_Desktop.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_ResourceMgr.h>
40 #include <SUIT_OverrideCursor.h>
41
42 #include <SALOME_ListIO.hxx>
43
44 // SALOME KERNEL includes
45 #include <SALOMEDS_SObject.hxx>
46 #include <SALOMEDSClient_SObject.hxx>
47
48 // Qt includes
49 // IDL includes
50 #include <SALOMEconfig.h>
51 #include CORBA_SERVER_HEADER(SMESH_Gen)
52 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
53
54 #include <QFrame>
55 #include <QLabel>
56 #include <QPixmap>
57 #include <QGroupBox>
58 #include <QPushButton>
59 #include <QVBoxLayout>
60
61 // MESH includes
62 #include "SMDSAbs_ElementType.hxx"
63 #include "SMDSAbs_ElementType.hxx"
64
65
66 #define SPACING 6
67 #define MARGIN  11
68
69 // =========================================================================================
70 /*!
71  * \brief Dialog to show creted mesh statistic
72  */
73 //=======================================================================
74
75 SMESHGUI_Make2DFrom3DDlg::SMESHGUI_Make2DFrom3DDlg( QWidget* parent )
76  : SMESHGUI_Dialog( parent, false, true, Close/* | Help*/ )
77 {
78   setWindowTitle( tr("CAPTION") );
79   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
80   aDlgLay->setMargin( 0 );
81   aDlgLay->setSpacing( SPACING );
82   QFrame* aMainFrame = createMainFrame(mainFrame());
83   aDlgLay->addWidget(aMainFrame);
84   aDlgLay->setStretchFactor(aMainFrame, 1);
85 }
86
87 // =========================================================================================
88 /*!
89  * \brief Dialog destructor
90  */
91 //=======================================================================
92
93 SMESHGUI_Make2DFrom3DDlg::~SMESHGUI_Make2DFrom3DDlg()
94 {
95 }
96
97 //=======================================================================
98 // function : createMainFrame()
99 // purpose  : Create frame containing dialog's fields
100 //=======================================================================
101
102 QFrame* SMESHGUI_Make2DFrom3DDlg::createMainFrame (QWidget* theParent)
103 {
104   QFrame* aFrame = new QFrame(theParent);
105
106   SUIT_ResourceMgr* rm = resourceMgr();
107   QPixmap iconCompute (rm->loadPixmap("SMESH", tr("ICON_2D_FROM_3D")));
108
109   // Mesh name
110   QGroupBox* nameBox = new QGroupBox(tr("SMESH_MESHINFO_NAME"), aFrame );
111   QHBoxLayout* nameBoxLayout = new QHBoxLayout(nameBox);
112   nameBoxLayout->setMargin(MARGIN); nameBoxLayout->setSpacing(SPACING);
113   myMeshName = new QLabel(nameBox);
114   nameBoxLayout->addWidget(myMeshName);
115
116   // Mesh Info
117
118   myFullInfo = new SMESHGUI_MeshInfosBox(true,  aFrame);
119
120   // add all widgets to aFrame
121   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
122   aLay->setMargin( 0 );
123   aLay->setSpacing( 0 );
124   aLay->addWidget( nameBox );
125   aLay->addWidget( myFullInfo );
126
127   ((QPushButton*) button( OK ))->setDefault( true );
128   return aFrame;
129 }
130
131 //================================================================================
132 /*!
133  * \brief set name of the mesh
134 */
135 //================================================================================
136
137 void SMESHGUI_Make2DFrom3DDlg::SetMeshName(const QString& theName)
138 {
139   myMeshName->setText( theName );
140 }
141
142 //================================================================================
143 /*!
144  * \brief set mesh info
145 */
146 //================================================================================
147
148 void SMESHGUI_Make2DFrom3DDlg::SetMeshInfo(const SMESH::long_array& theInfo)
149 {
150   myFullInfo->SetMeshInfo( theInfo );
151 }
152
153 //================================================================================
154 /*!
155  * \brief Constructor
156 */
157 //================================================================================
158
159 SMESHGUI_Make2DFrom3DOp::SMESHGUI_Make2DFrom3DOp()
160  : SMESHGUI_Operation()
161 {
162   myDlg = new SMESHGUI_Make2DFrom3DDlg(desktop());
163 }
164
165 //================================================================================
166 /*!
167  * \brief Desctructor
168 */
169 //================================================================================
170
171 SMESHGUI_Make2DFrom3DOp::~SMESHGUI_Make2DFrom3DOp()
172 {
173 }
174
175 //================================================================================
176 /*!
177  * \brief perform it's intention action: compute 2D mesh on 3D
178  */
179 //================================================================================
180
181 void SMESHGUI_Make2DFrom3DOp::startOperation()
182 {
183   myMesh = SMESH::SMESH_Mesh::_nil();
184   
185   // check selection
186   LightApp_SelectionMgr *Sel = selectionMgr();
187   SALOME_ListIO selected; Sel->selectedObjects( selected );
188
189   int nbSel = selected.Extent();
190   if (nbSel != 1) {
191     SUIT_MessageBox::warning(desktop(),
192                              tr("SMESH_WRN_WARNING"),
193                              tr("SMESH_WRN_NO_AVAILABLE_DATA"));
194     onCancel();
195     return;
196   }
197
198   Handle(SALOME_InteractiveObject) anIO = selected.First();
199   myMesh = SMESH::GetMeshByIO(anIO);
200   if (myMesh->_is_nil()) {
201     SUIT_MessageBox::warning(desktop(),
202                              tr("SMESH_WRN_WARNING"),
203                              tr("SMESH_WRN_NO_AVAILABLE_DATA"));
204     onCancel();
205     return;
206   }
207
208   SMESHGUI_Operation::startOperation();
209
210
211   // backup mesh info before 2D mesh computation
212   SMESH::long_array_var anOldInfo = myMesh->GetMeshInfo();
213   
214
215   if (!compute2DMesh()) {
216     SUIT_MessageBox::warning(desktop(),
217                              tr("SMESH_WRN_WARNING"),
218                              tr("SMESH_WRN_COMPUTE_FAILED"));
219     onCancel();
220     return;
221   }
222   // get new mesh statistic
223   SMESH::long_array_var aNewInfo = myMesh->GetMeshInfo();
224   // get difference in mesh statistic from old to new
225   for ( int i = SMDSEntity_Node; i < SMDSEntity_Last; i++ )
226     aNewInfo[i] -= anOldInfo[i];
227
228   // update presentation
229   SMESH::Update(anIO, SMESH::eDisplay);
230
231   // show computated result
232   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
233   if ( aMeshSObj )
234     myDlg->SetMeshName( aMeshSObj->GetName().c_str() );
235   myDlg->SetMeshInfo( aNewInfo );
236   myDlg->show(); /*exec();*/
237   commit();
238 }
239
240 //================================================================================
241 /*!
242  * \brief compute 2D mesh on initial 3D
243  */
244 //================================================================================
245
246 bool SMESHGUI_Make2DFrom3DOp::compute2DMesh()
247 {
248   SUIT_OverrideCursor wc;
249   SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
250   return aMeshEditor->Make2DMeshFrom3D();
251 }