1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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, or (at your option) any later version.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESHGUI_MG_ADAPTDRIVER.cxx
24 #include "SMESHGUI_MG_ADAPTDRIVER.h"
26 #include "SUIT_Desktop.h"
27 #include "SUIT_Application.h"
28 #include "SUIT_Session.h"
30 #include "SalomeApp_Application.h"
31 #include "SalomeApp_Module.h"
32 #include "SalomeApp_Study.h"
34 #include "SMESH_Comment.hxx"
35 #include "SMESH_Actor.h"
37 #include "SMESHGUI_FilterDlg.h"
38 #include "SMESHGUI_Selection.h"
39 #include <SUIT_MessageBox.h>
40 #include "SMESHGUI_IdValidator.h"
41 #include "SMESHGUI_Utils.h"
42 #include "SMESHGUI_MeshEditPreview.h"
43 #include "SMESHGUI_VTKUtils.h"
44 #include <SMESH_TypeFilter.hxx>
45 #include <SMESH_MeshAlgos.hxx>
46 #include <SMESH_LogicalFilter.hxx>
47 #include <SMDS_Mesh.hxx>
48 #include <SMDS_MeshNode.hxx>
49 #include "SMESHGUI_SpinBox.h"
51 #include <LightApp_SelectionMgr.h>
52 #include <SUIT_OverrideCursor.h>
53 #include <SUIT_ResourceMgr.h>
54 #include <SVTK_ViewWindow.h>
55 #include <SALOME_ListIO.hxx>
56 #include <SUIT_FileDlg.h>
57 #include "SMESHGUI_MeshUtils.h"
60 #include <QApplication>
61 #include <QButtonGroup>
62 #include <QGridLayout>
64 #include <QHBoxLayout>
69 #include <QPushButton>
70 #include <QRadioButton>
72 #include <QVBoxLayout>
73 #include <QDoubleSpinBox>
75 #include <QTreeWidget>
76 #include <QTreeWidgetItem>
77 #include <QSpacerItem>
79 #include <QHeaderView>
80 #include <QItemDelegate>
81 #include <QFileDialog>
82 #include <QMessageBox>
85 #include <vtkPoints.h>
86 #include <vtkUnstructuredGrid.h>
87 #include <vtkIdList.h>
88 #include <vtkCellArray.h>
89 #include <vtkUnsignedCharArray.h>
90 #include <vtkDataSetMapper.h>
91 #include <VTKViewer_CellLocationsArray.h>
92 #include <vtkProperty.h>
95 // SALOME KERNEL includes
96 #include <SALOMEDS_SComponent.hxx>
97 #include <SALOMEDS_SObject.hxx>
98 #include <SALOMEDS_Study.hxx>
99 #include <SALOMEDS_wrap.hxx>
100 #include "SalomeApp_Tools.h"
101 #include <SALOMEconfig.h>
103 #include <utilities.h>
105 #include <TCollection_AsciiString.hxx>
107 const int SPACING = 6; // layout spacing
108 const int MARGIN = 9; // layout margin
110 SALOME_ListIO mySelected;
113 //================================================================
114 // Function : firstIObject
115 // Purpose : Return the first selected object in the selected object list
116 //================================================================
117 Handle(SALOME_InteractiveObject) firstIObject()
119 const SALOME_ListIO& aList = selectedIO();
120 return aList.Extent() > 0 ? aList.First() : Handle(SALOME_InteractiveObject)();
122 //================================================================
123 // Function : selectedIO
124 // Return the list of selected SALOME_InteractiveObject's
125 //================================================================
126 const SALOME_ListIO& selectedIO()
128 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* > ( SUIT_Session::session()->activeApplication() );
129 LightApp_SelectionMgr* aSelectionMgr = app->selectionMgr();
132 aSelectionMgr->selectedObjects( mySelected );
133 for (SALOME_ListIteratorOfListIO it (mySelected); it.More(); it.Next())
134 SCRUTE(it.Value()->getEntry());
138 //================================================================
139 // Function : getStudy
140 // Returne un pointeur sur l'etude active
141 //================================================================
142 _PTR(Study) getStudy()
144 static _PTR(Study) _study;
146 _study = SalomeApp_Application::getStudy();
150 bool createAndPublishMed(QString fileName)
153 SMESH::DriverMED_ReadStatus res;
154 SMESH::mesh_array_var aMeshes = new SMESH::mesh_array;
157 aMeshes = SMESHGUI::GetSMESHGen()->CreateMeshesFromMED( fileName.toUtf8().constData(), res );
158 _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshes[0] );
159 _PTR(Study) aStudy = SMESH::getStudy();
160 QStringList anEntryList;
164 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
165 _PTR(AttributePixMap) aPixmap = aBuilder->FindOrCreateAttribute( aMeshSO, "AttributePixMap" );
166 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH_IMPORTED" );
167 anEntryList.append( aMeshSO->GetID().c_str() );
174 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
176 // browse to the published meshes
177 if( LightApp_Application* anApp =
178 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
179 anApp->browseObjects( anEntryList );
182 bool createMgAdaptObject(MgAdapt *myMgAdapt )
184 // SMESH::SMESH_Mesh_var newMesh = SMESHGUI::GetSMESHGen()->CreateEmptyMesh();
186 // _PTR(SObject) aHypothesis;
187 _PTR(Study) aStudy = SMESH::getStudy();
188 QStringList anEntryList;
189 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
190 _PTR(SComponent) mgadapt = aStudy->FindComponent("MG-ADAPT");
191 _PTR(GenericAttribute) ga;
192 if (!aBuilder->FindAttribute(mgadapt, ga, "AttributeName") )
194 mgadapt = aBuilder->NewComponent("MG-ADAPT");
195 _PTR(AttributeName) Name = aBuilder->FindOrCreateAttribute(mgadapt, "AttributeName");
196 Name->SetValue("MG-ADAPT");
197 _PTR(AttributePixMap) myPixmap = aBuilder->FindOrCreateAttribute( mgadapt, "AttributePixMap" );
198 myPixmap->SetPixMap( "ICON_MG_ADAPT" );
199 anEntryList.append( mgadapt->GetID().c_str() );
202 _PTR(SObject) obj = aBuilder->NewObject(mgadapt);
203 _PTR(AttributeName) myName = aBuilder->FindOrCreateAttribute(obj, "AttributeName");
204 myName->SetValue("hypo");
205 _PTR(AttributePixMap) aPixmap = aBuilder->FindOrCreateAttribute( obj, "AttributePixMap" );
206 aPixmap->SetPixMap( "ICON_SMESH_TREE_HYPO" );
207 anEntryList.append( obj->GetID().c_str() );
209 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
211 // // browse to the published meshes
212 if( LightApp_Application* anApp =
213 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
214 anApp->browseObjects( anEntryList );
220 //================================================================
221 // Function : IObjectCount
222 // Return the number of selected objects
223 //================================================================
226 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
227 LightApp_SelectionMgr* aSelectionMgr = app->selectionMgr();
230 aSelectionMgr->selectedObjects( mySelected );
231 SCRUTE(mySelected.Extent());
232 return mySelected.Extent();
239 SMESHGUI_MG_ADAPTDRIVER::SMESHGUI_MG_ADAPTDRIVER( SMESHGUI* theModule, SMESH::MG_ADAPT_ptr myModel, bool isCreation )
240 : mySMESHGUI( theModule ),
242 myIsApplyAndClose( false ),
243 SMESHGUI_MgAdaptDlg((SalomeApp_Module*)theModule, myModel, SMESHGUI::desktop(), isCreation)
246 resMgr = resourceMgr();
248 selMgr = selectionMgr();
251 connect(myArgs, SIGNAL(updateSelection()), this, SLOT(updateSelection()));
252 connect(myArgs, SIGNAL(toExportMED(const char*)), this, SLOT(exportMED(const char*)));
255 SUIT_ResourceMgr* SMESHGUI_MG_ADAPTDRIVER::resourceMgr()
257 return dynamic_cast<SUIT_ResourceMgr*>( SUIT_Session::session()->resourceMgr() );
260 LightApp_SelectionMgr* SMESHGUI_MG_ADAPTDRIVER::selectionMgr()
262 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
264 return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
269 void SMESHGUI_MG_ADAPTDRIVER::updateSelection()
271 disconnect( selMgr, 0, this, 0 );
272 selMgr->clearFilters();
274 SMESH::SetPointRepresentation( true );
275 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
276 aViewWindow->SetSelectionMode( ActorSelection );
277 if (myArgs->aBrowser->isChecked())
279 connect( selMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( selectionChanged() ));
284 void SMESHGUI_MG_ADAPTDRIVER::selectionChanged()
286 //~ get selected mesh
288 selMgr->selectedObjects(aList);
289 QString aString = "";
290 int nbSel = aList.Extent();
294 Handle(SALOME_InteractiveObject) IO = aList.First();
295 SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(IO);
296 if ( !mesh->_is_nil() )
300 mySelectedObject = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
301 if ( mySelectedObject->_is_nil() )
307 SMESH::GetNameOfSelectedIObjects( selMgr, aString );
308 if ( aString.isEmpty() ) aString = " ";
309 else aString = aString.trimmed();
312 bool ok = !aString.isEmpty();
313 if ( !mesh->_is_nil() )
315 myArgs->aBrowserObject->setText( aString );
316 myArgs->meshNameLineEdit->setText( aString );
317 myArgs->selectOutMedFileLineEdit->setText(aString+QString(".med"));
318 ADAPTATION_MODE aMode;
319 int nbVolumes = myMesh->NbVolumes();
320 int nbFaces = myMesh->NbFaces();
321 if(nbFaces > 0 && nbVolumes > 0) aMode = ADAPTATION_MODE::BOTH;
322 else if(nbFaces > 0) aMode = ADAPTATION_MODE::SURFACE;
323 else aMode = ADAPTATION_MODE::VOLUME;
324 emit myArgs->meshDimSignal(aMode);
328 void SMESHGUI_MG_ADAPTDRIVER::exportMED(const char* tmp_file)
330 bool toOverwrite = true;
331 bool toFindOutDim = true;
332 myMesh->ExportMED(tmp_file, false, -1, toOverwrite, toFindOutDim);
334 void SMESHGUI_MG_ADAPTDRIVER::setMyMesh(SMESH::SMESH_Mesh_var mesh)
338 SMESH::SMESH_Mesh_var SMESHGUI_MG_ADAPTDRIVER::getMyMesh()
343 //=================================================================================
344 // function : PushOnOk()
346 //=================================================================================
347 void SMESHGUI_MG_ADAPTDRIVER::PushOnOK()
349 setIsApplyAndClose( true );
350 bool ret = PushOnApply();
351 // std::cout << "SMESHGUI_MG_ADAPTDRIVER::PushOnOK ret : " <<ret<<std::endl;
354 bool SMESHGUI_MG_ADAPTDRIVER::PushOnApply()
356 MESSAGE("PushOnApply");
358 if ( SMESHGUI::isStudyLocked() )
363 bool ok = SMESHGUI_MgAdaptDlg::PushOnApply();
364 // std::cout << "SMESHGUI_MG_ADAPTDRIVER::PushOnApply ok 1 : " <<ok<<std::endl;
369 if (getModel()->getPublish()) this->createMeshInObjectBrowser();
370 // std::cout << "SMESHGUI_MG_ADAPTDRIVER::PushOnApply ok 2 : " <<ok<<std::endl;
373 QMessageBox::information( 0, QObject::tr(""),
374 QObject::tr("MG_ADAPT_DIAG_1") );
378 QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
379 QObject::tr("MG_ADAPT_DIAG_2") );
386 bool SMESHGUI_MG_ADAPTDRIVER::execute()
392 getModel()->compute();
394 errStr = getModel()->getErrMsg();
395 std::string msg = err == 0 ? " ok" : std::string("Not ok \n")+CORBA::string_dup(errStr) ;
397 catch (const std::exception& e)
404 //=================================================================================
407 //=================================================================================
408 void SMESHGUI_MG_ADAPTDRIVER::Init (bool ResetControls)
414 myLineEditElements->clear();
417 buttonOk->setEnabled(false);
418 buttonApply->setEnabled(false);
421 myMesh = SMESH::SMESH_Mesh::_nil();
423 myIdSourceCheck->setChecked(true);
430 //=======================================================================
431 //function : onConstructor
432 //purpose : switch operation mode
433 //=======================================================================
435 void SMESHGUI_MG_ADAPTDRIVER::onConstructor( int withGeom )
438 myGeomLabel ->setVisible( withGeom );
439 myGeomNameEdit ->setVisible( withGeom );
440 myReuseHypCheck ->setVisible( withGeom );
441 myCopyElementsCheck->setVisible( withGeom );
442 myFilterBtn ->setVisible( !withGeom );
443 myIdSourceCheck ->setVisible( !withGeom );
446 myMeshNameEdit->setText( SMESH::UniqueMeshName("Mesh"));
450 //~void SMESHGUI_MG_ADAPTDRIVER::onSelectIdSource( bool )
453 //=================================================================================
454 // function : enterEvent()
456 //=================================================================================
457 void SMESHGUI_MG_ADAPTDRIVER::enterEvent (QEvent*)
460 // if ( !ConstructorsBox->isEnabled() ) {
461 // SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
462 // if ( aViewWindow && !mySelector ) {
463 // mySelector = aViewWindow->GetSelector();
465 // activateThisDialog();
470 //=================================================================================
471 // function : keyPressEvent()
473 //=================================================================================
474 void SMESHGUI_MG_ADAPTDRIVER::keyPressEvent( QKeyEvent* e )
477 QDialog::keyPressEvent( e );
478 if ( e->isAccepted() )
481 if ( e->key() == Qt::Key_F1 )
489 //=================================================================================
490 // function : PushOnHelp()
492 //=================================================================================
493 void SMESHGUI_MG_ADAPTDRIVER::PushOnHelp()
496 QString aHelpFile = "adaptation.html#_mg_adapt_anchor";
498 SMESH::ShowHelpFile( aHelpFile );
502 //=======================================================================
503 //function : getErrorMsg
504 //purpose : Return an error message and entries of invalid smesh object
505 //=======================================================================
507 QString SMESHGUI_MG_ADAPTDRIVER::getErrorMsg( SMESH::string_array_var theInvalidEntries,
508 QStringList & theEntriesToBrowse )
511 if ( theInvalidEntries->length() == 0 )
512 return tr("OPERATION_FAILED");
514 // theInvalidEntries - SObject's that hold geometry objects whose
515 // counterparts are not found in the newGeometry, followed by SObject's
516 // holding mesh sub-objects that are invalid because they depend on a not found
517 // preceding sub-shape
519 QString msg = tr("SUBSHAPES_NOT_FOUND_MSG") + "\n";
522 for ( CORBA::ULong i = 0; i < theInvalidEntries->length(); ++i )
524 _PTR(SObject) so = SMESH::getStudy()->FindObjectID( theInvalidEntries[i].in() );
526 int objType = SMESHGUI_Selection::type( theInvalidEntries[i].in() );
527 if ( objType < 0 ) // geom object
531 objString += so->GetName().c_str();
533 objString += theInvalidEntries[i].in(); // it's something like "FACE #2"
537 theEntriesToBrowse.push_back( theInvalidEntries[i].in() );
542 objString += tr("SMESH_MESH");
544 case SMESH::HYPOTHESIS:
545 objString += tr("SMESH_HYPOTHESIS");
547 case SMESH::ALGORITHM:
548 objString += tr("SMESH_ALGORITHM");
550 case SMESH::SUBMESH_VERTEX:
551 case SMESH::SUBMESH_EDGE:
552 case SMESH::SUBMESH_FACE:
553 case SMESH::SUBMESH_SOLID:
554 case SMESH::SUBMESH_COMPOUND:
556 objString += tr("SMESH_SUBMESH");
559 objString += tr("SMESH_GROUP");
566 objString += so->GetName().c_str();
568 objString += theInvalidEntries[i].in();
572 if ( !objString.isEmpty() )
578 //=================================================================================
579 // function : isValid
581 //=================================================================================
583 bool SMESHGUI_MG_ADAPTDRIVER::isValid()
589 bool SMESHGUI_MG_ADAPTDRIVER::createMeshInObjectBrowser()
591 QString filename(getModel()->getMedFileOut());
593 QStringList anEntryList;
594 bool isEmpty = false;
596 SMESH::SMESH_Gen_var SMESH_Gen_ptr = SMESHGUI::GetSMESHGen();
597 if (!SMESH_Gen_ptr) {
598 std::cerr << "Could not retrieve SMESH_Gen_ptr" << std::endl;
599 throw SALOME_Exception(LOCALIZED("Could not retrieve SMESH::GetSMESHGen()"));
601 SMESH::mesh_array_var aMeshes = new SMESH::mesh_array;
602 aMeshes->length( 1 ); // one mesh only
603 SMESH::DriverMED_ReadStatus res;
604 aMeshes = SMESH_Gen_ptr->CreateMeshesFromMED( filename.toUtf8().constData(), res );
605 if ( res != SMESH::DRS_OK ) {
606 errors.append( QString( "%1 :\n\t%2" ).arg( filename ).arg( QObject::tr( QString( "SMESH_DRS_%1" ).arg( res ).toLatin1().data() ) ) );
608 _PTR(Study) aStudy = SMESH::getStudy();
609 for ( int i = 0, iEnd = aMeshes->length(); i < iEnd; i++ )
611 _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshes[i] );
614 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
615 _PTR(AttributePixMap) aPixmap = aBuilder->FindOrCreateAttribute( aMeshSO, "AttributePixMap" );
616 aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH_IMPORTED" ); // put REFINED mesh ico
617 anEntryList.append( aMeshSO->GetID().c_str() );
624 // update Object browser
625 SMESHGUI::GetSMESHGUI()->updateObjBrowser();
626 // browse to the published meshes
627 if( LightApp_Application* anApp =
628 dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
629 anApp->browseObjects( anEntryList );
631 // show Error message box if there were errors
632 if ( errors.count() > 0 ) {
633 SUIT_MessageBox::critical( SMESHGUI::desktop(),
634 QObject::tr( "SMESH_ERROR" ),
635 QObject::tr( "SMESH_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
638 // show warning message box, if some imported mesh is empty
640 SUIT_MessageBox::warning( SMESHGUI::desktop(),
641 QObject::tr( "SMESH_WRN_WARNING" ),
642 QObject::tr( "SMESH_DRS_SOME_EMPTY" ) );
647 //================================================================
648 // function : setIsApplyAndClose
649 // Purpose : Set value of the flag indicating that the dialog is
650 // accepted by Apply & Close button
651 //================================================================
652 void SMESHGUI_MG_ADAPTDRIVER::setIsApplyAndClose( const bool theFlag )
654 myIsApplyAndClose = theFlag;
655 }//================================================================
656 // function : isApplyAndClose
657 // Purpose : Get value of the flag indicating that the dialog is
658 // accepted by Apply & Close button
659 //================================================================
660 bool SMESHGUI_MG_ADAPTDRIVER::isApplyAndClose() const
662 return myIsApplyAndClose;
665 //=================================================================================
666 // function : DeactivateActiveDialog()
668 //=================================================================================
669 void SMESHGUI_MG_ADAPTDRIVER::deactivateActiveDialog()
671 if (ConstructorsBox->isEnabled())
673 ConstructorsBox->setEnabled(false);
674 GroupArguments->setEnabled(false);
675 GroupButtons->setEnabled(false);
676 mySMESHGUI->ResetState();
677 mySMESHGUI->SetActiveDialogBox(0);
679 selMgr->removeFilter( myIdSourceFilter );
683 //=================================================================================
684 // function : ActivateThisDialog()
686 //=================================================================================
687 void SMESHGUI_MG_ADAPTDRIVER::activateThisDialog()
690 /* Emit a signal to deactivate the active dialog */
691 // mySMESHGUI->EmitSignalDeactivateDialog();
692 // ConstructorsBox->setEnabled(true);
693 // GroupArguments->setEnabled(true);
694 // GroupButtons->setEnabled(true);
696 // mySMESHGUI->SetActiveDialogBox((QDialog*)this);
698 // onSelectIdSource( myIdSourceCheck->isChecked() );
700 // SelectionIntoArgument();
703 //=================================================================================
704 // function : setFilters()
705 // purpose : SLOT. Called when "Filter" button pressed.
706 //=================================================================================
707 void SMESHGUI_MG_ADAPTDRIVER::setFilters()
709 if(myMesh->_is_nil())
711 SUIT_MessageBox::critical(this,
713 tr("NO_MESH_SELECTED"));
717 myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, SMESH::ALL );
720 if ( myMesh->NbEdges() ) types << SMESH::EDGE;
721 if ( myMesh->NbFaces() ) types << SMESH::FACE;
722 if ( myMesh->NbVolumes() ) types << SMESH::VOLUME;
723 if ( myMesh->NbBalls() ) types << SMESH::BALL;
724 if ( myMesh->Nb0DElements()) types << SMESH::ELEM0D;
725 if ( types.count() > 1 ) types << SMESH::ALL;
727 myFilterDlg->Init( types );
728 myFilterDlg->SetSelection();
729 myFilterDlg->SetMesh( myMesh );
730 myFilterDlg->SetSourceWg( myLineEditElements );
735 //=================================================================================
736 // function : onOpenView()
738 //=================================================================================
739 void SMESHGUI_MG_ADAPTDRIVER::onOpenView()
742 SMESH::SetPointRepresentation(false);
745 mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
746 activateThisDialog();
750 //=================================================================================
751 // function : onCloseView()
753 //=================================================================================
754 void SMESHGUI_MG_ADAPTDRIVER::onCloseView()
756 deactivateActiveDialog();