1 // Copyright (C) 2020-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SMESHGUI_MG_ADAPTDRIVER.h"
23 #include "SMESHGUI_VTKUtils.h"
24 #include "SMESHGUI_MeshUtils.h"
25 #include "SMESH_TryCatch.hxx"
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>
36 #include <QRadioButton>
39 // SALOME KERNEL includes
41 const int SPACING = 6; // layout spacing
42 const int MARGIN = 9; // layout margin
44 // bool createMgAdaptObject(MgAdapt *myMgAdapt )
46 // // SMESH::SMESH_Mesh_var newMesh = SMESHGUI::GetSMESHGen()->CreateEmptyMesh();
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") )
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() );
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() );
71 // SMESHGUI::GetSMESHGUI()->updateObjBrowser();
73 // // // browse to the published meshes
74 // if( LightApp_Application* anApp =
75 // dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
76 // anApp->browseObjects( anEntryList );
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 )
87 //resMgr = mySMESHGUI->resourceMgr();
89 selMgr = selectionMgr();
92 connect(myArgs, SIGNAL(updateSelection()), this, SLOT(updateSelection()));
93 connect(myArgs, SIGNAL(toExportMED(const char*)), this, SLOT(exportMED(const char*)));
96 // SUIT_ResourceMgr* SMESHGUI_MG_ADAPTDRIVER::resourceMgr()
98 // return dynamic_cast<SUIT_ResourceMgr*>( SUIT_Session::session()->resourceMgr() );
101 LightApp_SelectionMgr* SMESHGUI_MG_ADAPTDRIVER::selectionMgr()
103 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
105 return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
110 void SMESHGUI_MG_ADAPTDRIVER::updateSelection()
112 disconnect( selMgr, 0, this, 0 );
113 selMgr->clearFilters();
115 SMESH::SetPointRepresentation( false );
116 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
117 aViewWindow->SetSelectionMode( ActorSelection );
118 if (myArgs->aBrowser->isChecked())
120 connect( selMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( selectionChanged() ));
125 void SMESHGUI_MG_ADAPTDRIVER::selectionChanged()
127 //~ get selected mesh
129 selMgr->selectedObjects(aList);
130 QString aString = "";
131 int nbSel = aList.Extent();
135 Handle(SALOME_InteractiveObject) IO = aList.First();
136 SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(IO);
137 if ( !mesh->_is_nil() )
141 SMESH::SMESH_IDSource_var sSelectedObj = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
142 if ( sSelectedObj->_is_nil() )
148 SMESH::GetNameOfSelectedIObjects( selMgr, aString );
149 if ( aString.isEmpty() ) aString = " ";
150 else aString = aString.trimmed();
153 //bool ok = !aString.isEmpty();
154 if ( !mesh->_is_nil() )
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);
169 void SMESHGUI_MG_ADAPTDRIVER::exportMED(const char* tmp_file)
171 bool toOverwrite = true;
172 bool toFindOutDim = true;
173 myMesh->ExportMED(tmp_file, false, -1, toOverwrite, toFindOutDim);
175 void SMESHGUI_MG_ADAPTDRIVER::setMyMesh(SMESH::SMESH_Mesh_var mesh)
179 SMESH::SMESH_Mesh_var SMESHGUI_MG_ADAPTDRIVER::getMyMesh()
184 //=================================================================================
185 // function : PushOnOk()
187 //=================================================================================
188 void SMESHGUI_MG_ADAPTDRIVER::PushOnOK()
190 setIsApplyAndClose( true );
191 bool ret = PushOnApply();
192 // std::cout << "SMESHGUI_MG_ADAPTDRIVER::PushOnOK ret : " <<ret<<std::endl;
196 bool SMESHGUI_MG_ADAPTDRIVER::PushOnApply()
198 if ( SMESHGUI::isStudyLocked() )
203 bool ok = SMESHGUI_MgAdaptDlg::PushOnApply();
204 // std::cout << "SMESHGUI_MG_ADAPTDRIVER::PushOnApply ok 1 : " <<ok<<std::endl;
209 if (getModel()->getPublish()) this->createMeshInObjectBrowser();
210 // std::cout << "SMESHGUI_MG_ADAPTDRIVER::PushOnApply ok 2 : " <<ok<<std::endl;
213 QMessageBox::information( 0, QObject::tr(""),
214 QObject::tr("MG_ADAPT_DIAG_1") );
218 QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
219 QObject::tr("MG_ADAPT_DIAG_2") );
226 // macro used to initialize errStr by exception description
227 // returned by SMESH_CATCH( SMESH::returnError )
229 #define SMESH_CAUGHT errStr =
232 #define SMY_OWN_CATCH catch ( SALOME::SALOME_Exception & e ) { errStr = e.details.text; }
234 bool SMESHGUI_MG_ADAPTDRIVER::execute()
240 getModel()->compute();
242 errStr = SMESH::toStdStr( getModel()->getErrMsg() );
244 SMESH_CATCH( SMESH::returnError );
246 std::string msg = " ok";
247 if ( !errStr.empty() || err != 0 )
249 msg = "Not ok \n" + errStr + "\n";
258 //=================================================================================
259 // function : enterEvent()
261 //=================================================================================
262 void SMESHGUI_MG_ADAPTDRIVER::enterEvent (QEvent*)
265 // if ( !ConstructorsBox->isEnabled() ) {
266 // SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
267 // if ( aViewWindow && !mySelector ) {
268 // mySelector = aViewWindow->GetSelector();
270 // activateThisDialog();
275 //=================================================================================
276 // function : keyPressEvent()
278 //=================================================================================
279 void SMESHGUI_MG_ADAPTDRIVER::keyPressEvent( QKeyEvent* e )
282 QDialog::keyPressEvent( e );
283 if ( e->isAccepted() )
286 if ( e->key() == Qt::Key_F1 )
294 //=================================================================================
295 // function : PushOnHelp()
297 //=================================================================================
298 void SMESHGUI_MG_ADAPTDRIVER::PushOnHelp()
301 QString aHelpFile = "adaptation.html#_mg_adapt_anchor";
303 SMESH::ShowHelpFile( aHelpFile );
307 //=================================================================================
308 // function : isValid
310 //=================================================================================
312 bool SMESHGUI_MG_ADAPTDRIVER::isValid()
318 bool SMESHGUI_MG_ADAPTDRIVER::createMeshInObjectBrowser()
320 QString filename( SMESH::toQStr( getModel()->getMedFileOut() ));
322 QStringList anEntryList;
323 bool isEmpty = 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") );
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() ) ) );
338 _PTR(Study) aStudy = SMESH::getStudy();
339 for ( int i = 0, iEnd = aMeshes->length(); i < iEnd; i++ )
341 _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshes[i] );
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() );
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 );
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" ) );
368 // show warning message box, if some imported mesh is empty
370 SUIT_MessageBox::warning( SMESHGUI::desktop(),
371 QObject::tr( "SMESH_WRN_WARNING" ),
372 QObject::tr( "SMESH_DRS_SOME_EMPTY" ) );
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 )
384 myIsApplyAndClose = theFlag;
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
393 return myIsApplyAndClose;
396 //=================================================================================
397 // function : DeactivateActiveDialog()
399 //=================================================================================
400 void SMESHGUI_MG_ADAPTDRIVER::deactivateActiveDialog()
404 // mySMESHGUI->ResetState();
405 // mySMESHGUI->SetActiveDialogBox(0);
406 // setEnabled( false );
410 //=================================================================================
411 // function : ActivateThisDialog()
413 //=================================================================================
414 void SMESHGUI_MG_ADAPTDRIVER::activateThisDialog()
417 /* Emit a signal to deactivate the active dialog */
418 // mySMESHGUI->EmitSignalDeactivateDialog();
419 // ConstructorsBox->setEnabled(true);
420 // GroupArguments->setEnabled(true);
421 // GroupButtons->setEnabled(true);
423 // mySMESHGUI->SetActiveDialogBox((QDialog*)this);
425 // onSelectIdSource( myIdSourceCheck->isChecked() );
427 // SelectionIntoArgument();