Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MG_ADAPTDRIVER.cxx
1 // Copyright (C) 2020-2022  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, 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
20 #include "SMESHGUI_MG_ADAPTDRIVER.h"
21
22 #include "SMESHGUI.h"
23 #include "SMESHGUI_VTKUtils.h"
24 #include "SMESHGUI_MeshUtils.h"
25 #include "SMESH_TryCatch.hxx"
26
27 #include <LightApp_SelectionMgr.h>
28 #include <SUIT_Application.h>
29 #include <SUIT_Desktop.h>
30 #include <SUIT_MessageBox.h>
31 #include <SUIT_Session.h>
32 #include <SVTK_ViewWindow.h>
33 #include <SalomeApp_Application.h>
34
35 #include <QLineEdit>
36 #include <QRadioButton>
37 #include <QKeyEvent>
38
39 // SALOME KERNEL includes
40
41 const int SPACING = 6; // layout spacing
42 const int MARGIN  = 9; // layout margin
43
44 // bool createMgAdaptObject(MgAdapt *myMgAdapt )
45 // {
46 //   // SMESH::SMESH_Mesh_var newMesh = SMESHGUI::GetSMESHGen()->CreateEmptyMesh();
47
48 //   // _PTR(SObject) aHypothesis;
49 //   _PTR(Study) aStudy = SMESH::getStudy();
50 //   QStringList anEntryList;
51 //   _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
52 //   _PTR(SComponent) mgadapt = aStudy->FindComponent("MG-ADAPT");
53 //   _PTR(GenericAttribute) ga;
54 //   if (!aBuilder->FindAttribute(mgadapt, ga, "AttributeName") )
55 //   {
56 //     mgadapt = aBuilder->NewComponent("MG-ADAPT");
57 //     _PTR(AttributeName) Name = aBuilder->FindOrCreateAttribute(mgadapt, "AttributeName");
58 //     Name->SetValue("MG-ADAPT");
59 //     _PTR(AttributePixMap) myPixmap = aBuilder->FindOrCreateAttribute( mgadapt, "AttributePixMap" );
60 //     myPixmap->SetPixMap( "ICON_MG_ADAPT" );
61 //     anEntryList.append( mgadapt->GetID().c_str() );
62 //   }
63
64 //   _PTR(SObject) obj = aBuilder->NewObject(mgadapt);
65 //   _PTR(AttributeName) myName = aBuilder->FindOrCreateAttribute(obj, "AttributeName");
66 //   myName->SetValue("hypo");
67 //   _PTR(AttributePixMap) aPixmap = aBuilder->FindOrCreateAttribute( obj, "AttributePixMap" );
68 //   aPixmap->SetPixMap( "ICON_SMESH_TREE_HYPO" );
69 //   anEntryList.append( obj->GetID().c_str() );
70
71 //   SMESHGUI::GetSMESHGUI()->updateObjBrowser();
72
73 //   // // browse to the published meshes
74 //   if( LightApp_Application* anApp =
75 //               dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
76 //       anApp->browseObjects( anEntryList );
77 //   return true;
78 // }
79
80
81 SMESHGUI_MG_ADAPTDRIVER::SMESHGUI_MG_ADAPTDRIVER( SMESHGUI* theModule, SMESH::MG_ADAPT_ptr myModel, bool isCreation )
82   : SMESHGUI_MgAdaptDlg((SalomeApp_Module*)theModule, myModel, SMESHGUI::desktop(), isCreation),
83     mySMESHGUI( theModule ),
84     myIsApplyAndClose( false )
85 {
86
87   //resMgr = mySMESHGUI->resourceMgr();
88
89   selMgr = selectionMgr();
90
91   // connections
92   connect(myArgs, SIGNAL(updateSelection()), this, SLOT(updateSelection()));
93   connect(myArgs, SIGNAL(toExportMED(const char*)), this, SLOT(exportMED(const char*)));
94 }
95
96 // SUIT_ResourceMgr* SMESHGUI_MG_ADAPTDRIVER::resourceMgr()
97 // {
98 //   return dynamic_cast<SUIT_ResourceMgr*>( SUIT_Session::session()->resourceMgr() );
99 // }
100
101 LightApp_SelectionMgr* SMESHGUI_MG_ADAPTDRIVER::selectionMgr()
102 {
103   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
104   if( anApp )
105     return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
106   else
107     return 0;
108 }
109
110 void SMESHGUI_MG_ADAPTDRIVER::updateSelection()
111 {
112   disconnect( selMgr, 0, this, 0 );
113   selMgr->clearFilters();
114
115   SMESH::SetPointRepresentation( false );
116   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
117     aViewWindow->SetSelectionMode( ActorSelection );
118   if (myArgs->aBrowser->isChecked())
119   {
120     connect( selMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( selectionChanged() ));
121     selectionChanged();
122   }
123
124 }
125 void SMESHGUI_MG_ADAPTDRIVER::selectionChanged()
126 {
127   //~ get selected mesh
128   SALOME_ListIO aList;
129   selMgr->selectedObjects(aList);
130   QString aString = "";
131   int nbSel = aList.Extent();
132   if (nbSel != 1)
133     return;
134
135   Handle(SALOME_InteractiveObject) IO = aList.First();
136   SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(IO);
137   if ( !mesh->_is_nil() )
138   {
139     myMesh = mesh;
140
141     SMESH::SMESH_IDSource_var sSelectedObj = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
142     if ( sSelectedObj->_is_nil() )
143       return;
144   }
145   else
146     return;
147
148   SMESH::GetNameOfSelectedIObjects( selMgr, aString );
149   if ( aString.isEmpty() ) aString = " ";
150   else                     aString = aString.trimmed();
151
152
153   //bool ok = !aString.isEmpty();
154   if ( !mesh->_is_nil() )
155   {
156     myArgs->aBrowserObject->setText( aString );
157     myArgs->meshNameLineEdit->setText( aString );
158     myArgs->selectOutMedFileLineEdit->setText(aString+QString(".med"));
159     ADAPTATION_MODE aMode;
160     int nbVolumes = myMesh->NbVolumes();
161     int nbFaces = myMesh->NbFaces();
162     if(nbFaces > 0 && nbVolumes > 0) aMode = ADAPTATION_MODE::BOTH;
163     else if(nbFaces > 0) aMode = ADAPTATION_MODE::SURFACE;
164     else aMode = ADAPTATION_MODE::VOLUME;
165     emit myArgs->meshDimSignal(aMode);
166   }
167
168 }
169 void SMESHGUI_MG_ADAPTDRIVER::exportMED(const char* tmp_file)
170 {
171   bool toOverwrite = true;
172   bool toFindOutDim = true;
173   myMesh->ExportMED(tmp_file, false, -1, toOverwrite, toFindOutDim);
174 }
175 void SMESHGUI_MG_ADAPTDRIVER::setMyMesh(SMESH::SMESH_Mesh_var mesh)
176 {
177   myMesh = mesh;
178 }
179 SMESH::SMESH_Mesh_var SMESHGUI_MG_ADAPTDRIVER::getMyMesh()
180 {
181   return myMesh;
182 }
183
184 //=================================================================================
185 // function : PushOnOk()
186 // purpose  :
187 //=================================================================================
188 void SMESHGUI_MG_ADAPTDRIVER::PushOnOK()
189 {
190   setIsApplyAndClose( true );
191   bool ret = PushOnApply();
192 //   std::cout  << "SMESHGUI_MG_ADAPTDRIVER::PushOnOK ret : " <<ret<<std::endl;
193   if ( ret ) reject();
194 }
195
196 bool SMESHGUI_MG_ADAPTDRIVER::PushOnApply()
197 {
198   if ( SMESHGUI::isStudyLocked() )
199     return false;
200   if( !isValid() )
201     return false;
202
203   bool ok = SMESHGUI_MgAdaptDlg::PushOnApply();
204 //   std::cout  << "SMESHGUI_MG_ADAPTDRIVER::PushOnApply ok 1 : " <<ok<<std::endl;
205
206   if ( ok )
207   {
208     ok = execute();
209     if (getModel()->getPublish()) this->createMeshInObjectBrowser();
210 //     std::cout  << "SMESHGUI_MG_ADAPTDRIVER::PushOnApply ok 2 : " <<ok<<std::endl;
211     if ( ok )
212     {
213       QMessageBox::information( 0, QObject::tr(""),
214                                    QObject::tr("MG_ADAPT_DIAG_1") );
215     }
216     else
217     {
218       QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
219                                 QObject::tr("MG_ADAPT_DIAG_2") );
220     }
221   }
222
223   return ok;
224 }
225
226 // macro used to initialize errStr by exception description
227 // returned by SMESH_CATCH( SMESH::returnError )
228 #undef SMESH_CAUGHT
229 #define SMESH_CAUGHT errStr =
230
231 #undef SMY_OWN_CATCH
232 #define SMY_OWN_CATCH catch ( SALOME::SALOME_Exception & e ) { errStr = e.details.text; }
233
234 bool SMESHGUI_MG_ADAPTDRIVER::execute()
235 {
236   int err = 1;
237   std::string errStr;
238   SMESH_TRY;
239   {
240     getModel()->compute();
241     err = 0;
242     errStr = SMESH::toStdStr( getModel()->getErrMsg() );
243   }
244   SMESH_CATCH( SMESH::returnError );
245
246   std::string msg = " ok";
247   if ( !errStr.empty() || err != 0 )
248   {
249     msg = "Not ok \n" + errStr + "\n";
250     std::cerr << msg;
251     err = 1;
252   }
253   return err == 0;
254 }
255 #undef SMESH_CAUGHT
256 #define SMESH_CAUGHT
257
258 //=================================================================================
259 // function : enterEvent()
260 // purpose  :
261 //=================================================================================
262 void SMESHGUI_MG_ADAPTDRIVER::enterEvent (QEvent*)
263 {
264
265   // if ( !ConstructorsBox->isEnabled() ) {
266   //   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
267   //   if ( aViewWindow && !mySelector ) {
268   //     mySelector = aViewWindow->GetSelector();
269   //   }
270   //   activateThisDialog();
271   // }
272
273 }
274
275 //=================================================================================
276 // function : keyPressEvent()
277 // purpose  :
278 //=================================================================================
279 void SMESHGUI_MG_ADAPTDRIVER::keyPressEvent( QKeyEvent* e )
280 {
281
282   QDialog::keyPressEvent( e );
283   if ( e->isAccepted() )
284       return;
285
286   if ( e->key() == Qt::Key_F1 )
287   {
288     e->accept();
289     PushOnHelp();
290   }
291
292 }
293
294 //=================================================================================
295 // function : PushOnHelp()
296 // purpose  :
297 //=================================================================================
298 void SMESHGUI_MG_ADAPTDRIVER::PushOnHelp()
299 {
300
301   QString aHelpFile = "adaptation.html#_mg_adapt_anchor";
302
303   SMESH::ShowHelpFile( aHelpFile );
304
305 }
306
307 //=================================================================================
308 // function : isValid
309 // purpose  :
310 //=================================================================================
311
312 bool SMESHGUI_MG_ADAPTDRIVER::isValid()
313 {
314   bool ok = true;
315   return ok;
316 }
317
318 bool SMESHGUI_MG_ADAPTDRIVER::createMeshInObjectBrowser()
319 {
320   QString filename( SMESH::toQStr( getModel()->getMedFileOut() ));
321   QStringList errors;
322   QStringList anEntryList;
323   bool isEmpty = false;
324   //  bool ok = false;
325   SMESH::SMESH_Gen_var SMESH_Gen_ptr = SMESHGUI::GetSMESHGen();
326   if ( SMESH_Gen_ptr->_is_nil() ) {
327     QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
328                            QObject::tr("Could not retrieve SMESH_Gen_ptr") );
329     return false;
330   }
331   SMESH::mesh_array_var aMeshes = new SMESH::mesh_array;
332   aMeshes->length( 1 ); // one mesh only
333   SMESH::DriverMED_ReadStatus res;
334   aMeshes = SMESH_Gen_ptr->CreateMeshesFromMED( filename.toUtf8().constData(), res );
335   if ( res != SMESH::DRS_OK ) {
336     errors.append( QString( "%1 :\n\t%2" ).arg( filename ).arg( QObject::tr( QString( "SMESH_DRS_%1" ).arg( res ).toLatin1().data() ) ) );
337   }
338   _PTR(Study) aStudy = SMESH::getStudy();
339   for ( int i = 0, iEnd = aMeshes->length(); i < iEnd; i++ )
340   {
341     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshes[i] );
342     if ( aMeshSO )
343     {
344       _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
345       _PTR(AttributePixMap) aPixmap = aBuilder->FindOrCreateAttribute( aMeshSO, "AttributePixMap" );
346       aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH_IMPORTED" ); // put REFINED mesh ico
347       anEntryList.append( aMeshSO->GetID().c_str() );
348     }
349     else
350     {
351       isEmpty = true;
352     }
353   }
354   // update Object browser
355   SMESHGUI::GetSMESHGUI()->updateObjBrowser();
356   // browse to the published meshes
357   if( LightApp_Application* anApp =
358       dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
359     anApp->browseObjects( anEntryList );
360
361   // show Error message box if there were errors
362   if ( errors.count() > 0 ) {
363     SUIT_MessageBox::critical( SMESHGUI::desktop(),
364                                QObject::tr( "SMESH_ERROR" ),
365                                QObject::tr( "SMESH_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
366   }
367
368   // show warning message box, if some imported mesh is empty
369   if ( isEmpty ) {
370     SUIT_MessageBox::warning( SMESHGUI::desktop(),
371                               QObject::tr( "SMESH_WRN_WARNING" ),
372                               QObject::tr( "SMESH_DRS_SOME_EMPTY" ) );
373   }
374   return true;
375 }
376
377 //================================================================
378 // function : setIsApplyAndClose
379 // Purpose  : Set value of the flag indicating that the dialog is
380 //            accepted by Apply & Close button
381 //================================================================
382 void SMESHGUI_MG_ADAPTDRIVER::setIsApplyAndClose( const bool theFlag )
383 {
384   myIsApplyAndClose = theFlag;
385 }
386 //================================================================
387 // function : isApplyAndClose
388 // Purpose  : Get value of the flag indicating that the dialog is
389 //            accepted by Apply & Close button
390 //================================================================
391 bool SMESHGUI_MG_ADAPTDRIVER::isApplyAndClose() const
392 {
393   return myIsApplyAndClose;
394 }
395
396 //=================================================================================
397 // function : DeactivateActiveDialog()
398 // purpose  :
399 //=================================================================================
400 void SMESHGUI_MG_ADAPTDRIVER::deactivateActiveDialog()
401 {
402   // if (isEnabled())
403   // {
404   //   mySMESHGUI->ResetState();
405   //   mySMESHGUI->SetActiveDialogBox(0);
406   //   setEnabled( false );
407   // }
408 }
409
410 //=================================================================================
411 // function : ActivateThisDialog()
412 // purpose  :
413 //=================================================================================
414 void SMESHGUI_MG_ADAPTDRIVER::activateThisDialog()
415 {
416
417   /* Emit a signal to deactivate the active dialog */
418   // mySMESHGUI->EmitSignalDeactivateDialog();
419   // ConstructorsBox->setEnabled(true);
420   // GroupArguments->setEnabled(true);
421   // GroupButtons->setEnabled(true);
422
423   // mySMESHGUI->SetActiveDialogBox((QDialog*)this);
424
425   // onSelectIdSource( myIdSourceCheck->isChecked() );
426
427   // SelectionIntoArgument();
428 }