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