Salome HOME
Copyright update 2021
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_FaceGroupsSeparatedByEdgesDlg.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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 // File   : SMESHGUI_FaceGroupsSeparatedByEdges.cxx
20
21 #include "SMESHGUI_FaceGroupsSeparatedByEdgesDlg.h"
22
23 #include "SMESHGUI.h"
24 #include "SMESHGUI_SpinBox.h"
25 #include "SMESHGUI_MeshEditPreview.h"
26 #include "SMESHGUI_Utils.h"
27
28 // SALOME GUI includes
29 #include <LightApp_Application.h>
30 #include <LightApp_SelectionMgr.h>
31 #include <SALOME_Actor.h>
32 #include <SALOME_ListIO.hxx>
33 #include <SUIT_MessageBox.h>
34 #include <SUIT_OverrideCursor.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_Session.h>
37 #include <SalomeApp_Tools.h>
38
39 #include <SALOMEDS_SObject.hxx>
40
41 // IDL includes
42 #include <SALOMEconfig.h>
43 #include CORBA_SERVER_HEADER(SMESH_Group)
44
45 // Qt includes
46 #include <QCheckBox>
47 #include <QGridLayout>
48 #include <QGroupBox>
49 #include <QHBoxLayout>
50 #include <QLineEdit>
51 #include <QPushButton>
52 #include <QRadioButton>
53 #include <QToolButton>
54
55 #include <Standard_ErrorHandler.hxx>
56
57 #define SPACING 6
58 #define MARGIN  11
59
60 /*!
61   \class SMESHGUI_FaceGroupsSeparatedByEdgesDlg
62   \brief Dialog to create face groups divided by sharp edges
63 */
64
65 SMESHGUI_FaceGroupsSeparatedByEdgesDlg::
66 SMESHGUI_FaceGroupsSeparatedByEdgesDlg( SMESHGUI* theModule ) :
67   SMESHGUI_PreviewDlg( theModule ),
68   mySelectionMgr( SMESH::GetSelectionMgr( theModule ))
69 {
70   setModal(false);
71   setAttribute(Qt::WA_DeleteOnClose, true);
72   setWindowTitle(tr("CAPTION"));
73   setSizeGripEnabled(true);
74
75   QVBoxLayout* dlgLayout = new QVBoxLayout(this);
76   dlgLayout->setSpacing(SPACING);
77   dlgLayout->setMargin(MARGIN);
78
79   /***************************************************************/
80   GroupArgs = new QGroupBox(tr("SMESH_ARGUMENTS"), this);
81   QGridLayout* GroupArgsLayout = new QGridLayout(GroupArgs);
82   GroupArgsLayout->setSpacing(SPACING);
83   GroupArgsLayout->setMargin(MARGIN);
84
85   // mesh
86   QLabel* meshNameLabel = new QLabel(tr("SMESH_MESH"), GroupArgs);
87   myMeshName = new QLineEdit( GroupArgs );
88   myMeshName->setReadOnly( true );
89
90   // angle
91   QLabel* angleLabel = new QLabel( tr("SHARP_ANGLE"), GroupArgs );
92   myAngle = new SMESHGUI_SpinBox( GroupArgs );
93   myAngle->RangeStepAndValidator( 0, 180.0, 10.0, "angle_precision");
94
95   // check-boxes
96   myCreateEdgesCheck = new QCheckBox( tr("CREATE_EDGES" ), GroupArgs);
97   myUseExistingCheck = new QCheckBox( tr("USE_EXISTING_EDGES"), GroupArgs);
98   myPreviewCheckBox  = new QCheckBox(tr("PREVIEW"), GroupArgs);
99
100   // set previous values
101   if ( SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr() )
102   {
103     myAngle->setValue( resMgr->doubleValue( "SMESH", "prev_sharp_angle", 30. ));
104     myCreateEdgesCheck->setChecked( resMgr->booleanValue( "SMESH", "prev_create_edges", false ));
105     myUseExistingCheck->setChecked( resMgr->booleanValue( "SMESH", "prev_use_existing_edges", false ));
106   }
107
108   // layout
109   GroupArgsLayout->addWidget( meshNameLabel,      0, 0);
110   GroupArgsLayout->addWidget( myMeshName,         0, 1);
111   GroupArgsLayout->addWidget( angleLabel,         1, 0);
112   GroupArgsLayout->addWidget( myAngle,            1, 1);
113   GroupArgsLayout->addWidget( myCreateEdgesCheck, 2, 0);
114   GroupArgsLayout->addWidget( myUseExistingCheck, 3, 0);
115   GroupArgsLayout->addWidget( myPreviewCheckBox,  4, 0);
116
117   /***************************************************************/
118   GroupButtons = new QGroupBox(this);
119   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
120   GroupButtonsLayout->setSpacing(SPACING);
121   GroupButtonsLayout->setMargin(MARGIN);
122
123   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
124   buttonOk->setAutoDefault(true);
125   buttonOk->setDefault(true);
126   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
127   buttonApply->setAutoDefault(true);
128   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
129   buttonCancel->setAutoDefault(true);
130   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
131   buttonHelp->setAutoDefault(true);
132
133   GroupButtonsLayout->addWidget(buttonOk);
134   GroupButtonsLayout->addSpacing(10);
135   GroupButtonsLayout->addWidget(buttonApply);
136   GroupButtonsLayout->addSpacing(10);
137   GroupButtonsLayout->addStretch();
138   GroupButtonsLayout->addWidget(buttonCancel);
139   GroupButtonsLayout->addWidget(buttonHelp);
140
141   /***************************************************************/
142   dlgLayout->addWidget(GroupArgs);
143   dlgLayout->addWidget(GroupButtons);
144
145   mySMESHGUI->SetActiveDialogBox(this);
146
147   /* signals and slots connections */
148   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
149   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
150   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
151   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
152
153   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
154   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()),   this, SLOT(SelectionIntoArgument()));
155   /* to close dialog if study change */
156   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()),      this, SLOT(reject()));
157   connect(mySMESHGUI, SIGNAL(SignalActivatedViewManager()), this, SLOT(onOpenView()));
158   connect(mySMESHGUI, SIGNAL(SignalCloseView()),            this, SLOT(onCloseView()));
159
160   connect( myAngle, SIGNAL(valueChanged(double)),     this, SLOT( onArgChange() ));
161   connect( myCreateEdgesCheck, SIGNAL(toggled(bool)), this, SLOT( onArgChange() ));
162   connect( myUseExistingCheck, SIGNAL(toggled(bool)), this, SLOT( onArgChange() ));
163
164   connectPreviewControl();
165   mySimulation->GetActor()->GetProperty()->SetLineWidth( 5 );
166
167   SelectionIntoArgument();
168 }
169
170 SMESHGUI_FaceGroupsSeparatedByEdgesDlg::~SMESHGUI_FaceGroupsSeparatedByEdgesDlg()
171 {
172 }
173
174 //=================================================================================
175 // function : ClickOnApply()
176 // purpose  :
177 //=================================================================================
178
179 bool SMESHGUI_FaceGroupsSeparatedByEdgesDlg::ClickOnApply()
180 {
181   if ( mySMESHGUI->isStudyLocked() )
182     return false;
183
184   SUIT_OverrideCursor aWaitCursor;
185
186   QStringList aParameters, anEntryList;
187   aParameters << myAngle->text();
188
189   try
190   {
191     SMESH::ListOfGroups_var groups =
192       myMesh->FaceGroupsSeparatedByEdges( myAngle->value(),
193                                           myCreateEdgesCheck->isChecked(),
194                                           myUseExistingCheck->isChecked());
195
196     for ( CORBA::ULong i = 0; i < groups->length(); ++i )
197       if( _PTR(SObject) aSObject = SMESH::ObjectToSObject( groups[i].in() ))
198         anEntryList.append( aSObject->GetID().c_str() );
199
200     SUIT_MessageBox::information(this, tr("SMESH_INFORMATION"),
201                                  tr("NB_GROUPS_CREATED").arg( groups->length() ));
202   }
203   catch ( const SALOME::SALOME_Exception& S_ex ) {
204     SalomeApp_Tools::QtCatchCorbaException( S_ex );
205   }
206   catch (...) {
207   }
208
209   mySMESHGUI->updateObjBrowser(true); // new groups may appear
210   if( LightApp_Application* anApp =
211       dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
212   {
213     anApp->browseObjects( anEntryList, isApplyAndClose() );
214   }
215
216   SMESHGUI::Modified();
217
218   return true;
219 }
220 //=================================================================================
221 // function : ClickOnOk()
222 // purpose  :
223 //=================================================================================
224
225 void SMESHGUI_FaceGroupsSeparatedByEdgesDlg::ClickOnOk()
226 {
227   setIsApplyAndClose( true );
228   if( ClickOnApply() )
229     reject();
230 }
231
232 //=================================================================================
233 // function : reject()
234 // purpose  :
235 //=================================================================================
236 void SMESHGUI_FaceGroupsSeparatedByEdgesDlg::reject()
237 {
238   disconnect(mySelectionMgr, 0, this, 0);
239   mySMESHGUI->ResetState();
240   QDialog::reject();
241 }
242 //=================================================================================
243 // function : ClickOnHelp()
244 // purpose  :
245 //=================================================================================
246
247 void SMESHGUI_FaceGroupsSeparatedByEdgesDlg::ClickOnHelp()
248 {
249   static QString myHelpFileName( "face_groups_by_sharp_edges.html" );
250
251   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
252   if (app)
253     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
254   else {
255     QString platform;
256 #ifdef WIN32
257     platform = "winapplication";
258 #else
259     platform = "application";
260 #endif
261     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
262                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
263                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
264                                                                  platform)).
265                              arg(myHelpFileName));
266   }
267 }
268
269 //=================================================================================
270 // function : SelectionIntoArgument()
271 // purpose  : Called when selection as changed or other case
272 //=================================================================================
273
274 void SMESHGUI_FaceGroupsSeparatedByEdgesDlg::SelectionIntoArgument()
275 {
276   if (!GroupButtons->isEnabled()) // inactive
277     return;
278
279   // get selected mesh
280   SALOME_ListIO aList;
281   mySelectionMgr->selectedObjects(aList);
282
283   for ( SALOME_ListIteratorOfListIO it( aList ); it.More(); it.Next() )
284   {
285     Handle(SALOME_InteractiveObject) IO = it.Value();
286     CORBA::Object_var anObj = SMESH::IObjectToObject( IO );
287     SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( anObj );
288     if ( aMesh->_is_nil() || aMesh->NbFaces() == 0 )
289       continue;
290
291     if ( !aMesh->_is_equivalent( myMesh ))
292       onDisplaySimulation( false );
293
294     myMesh = aMesh;
295     myMeshName->setText( IO->getName() );
296   }
297
298   bool ok = !myMesh->_is_nil();
299
300   buttonOk->setEnabled( ok );
301   buttonApply->setEnabled( ok );
302
303   onDisplaySimulation( ok );
304 }
305
306 //=================================================================================
307 // function : DeactivateActiveDialog()
308 // purpose  :
309 //=================================================================================
310
311 void SMESHGUI_FaceGroupsSeparatedByEdgesDlg::DeactivateActiveDialog()
312 {
313   if (GroupArgs->isEnabled())
314   {
315     GroupArgs->setEnabled(false);
316     GroupButtons->setEnabled(false);
317     mySMESHGUI->ResetState();
318     mySMESHGUI->SetActiveDialogBox(0);
319   }
320 }
321
322 //=================================================================================
323 // function : ActivateThisDialog()
324 // purpose  :
325 //=================================================================================
326
327 void SMESHGUI_FaceGroupsSeparatedByEdgesDlg::ActivateThisDialog()
328 {
329   /* Emit a signal to deactivate the active dialog */
330   mySMESHGUI->EmitSignalDeactivateDialog();
331   GroupArgs->setEnabled(true);
332   GroupButtons->setEnabled(true);
333
334   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
335
336   SelectionIntoArgument();
337 }
338
339 //=================================================================================
340 // function : onDisplaySimulation
341 // purpose  : Show/Hide preview
342 //=================================================================================
343
344 void SMESHGUI_FaceGroupsSeparatedByEdgesDlg::onDisplaySimulation( bool toDisplayPreview )
345 {
346   SUIT_OverrideCursor aWaitCursor;
347
348   // save current values
349   if ( SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr() )
350   {
351     resMgr->setValue( "SMESH", "prev_sharp_angle",        myAngle->value() );
352     resMgr->setValue( "SMESH", "prev_create_edges",       myCreateEdgesCheck->isChecked() );
353     resMgr->setValue( "SMESH", "prev_use_existing_edges", myUseExistingCheck->isChecked() );
354   }
355
356   if ( myPreviewCheckBox->isChecked() && toDisplayPreview && !myMesh->_is_nil() )
357   {
358     try
359     {
360       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditPreviewer();
361       SMESH::ListOfEdges_var edges = aMeshEditor->FindSharpEdges( myAngle->value(),
362                                                                   myUseExistingCheck->isChecked());
363       SMESH::MeshPreviewStruct_var previewData = aMeshEditor->GetPreviewData();
364
365       mySimulation->SetData( previewData.in() );
366       showPreview();
367
368     } catch (...) {
369       hidePreview();
370     }
371   }
372   else
373   {
374     hidePreview();
375   }
376 }