Salome HOME
Fix of issue 0020614: EDF 1133 SMESH : Put new added elements and nodes to group
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddQuadraticElementDlg.cxx
index 4a9fa18acb5efd73a78a38d9d403b79a94a6d581..23b49bc1223a5bd21dab2019737eff33b4f1bea1 100644 (file)
@@ -30,6 +30,7 @@
 #include "SMESHGUI_Utils.h"
 #include "SMESHGUI_VTKUtils.h"
 #include "SMESHGUI_MeshUtils.h"
+#include "SMESHGUI_GroupUtils.h"
 #include "SMESHGUI_IdValidator.h"
 
 #include <SMESH_Actor.h>
@@ -69,6 +70,7 @@
 #include <vtkCellType.h>
 
 // Qt includes
+#include <QComboBox>
 #include <QGroupBox>
 #include <QLabel>
 #include <QLineEdit>
@@ -505,6 +507,20 @@ SMESHGUI_AddQuadraticElementDlg::SMESHGUI_AddQuadraticElementDlg( SMESHGUI* theM
   aGroupArgumentsLayout->addWidget(myTable,           1, 0, 1, 3); 
   aGroupArgumentsLayout->addWidget(myReverseCB,       2, 0, 1, 3);
   
+  /***************************************************************/
+  GroupGroups = new QGroupBox( tr( "SMESH_ADD_TO_GROUP" ), this );
+  GroupGroups->setCheckable( true );
+  QHBoxLayout* GroupGroupsLayout = new QHBoxLayout(GroupGroups);
+  GroupGroupsLayout->setSpacing(SPACING);
+  GroupGroupsLayout->setMargin(MARGIN);
+
+  TextLabel_GroupName = new QLabel( tr( "SMESH_GROUP" ), GroupGroups );
+  ComboBox_GroupName = new QComboBox( GroupGroups );
+  ComboBox_GroupName->setEditable( true );
+
+  GroupGroupsLayout->addWidget( TextLabel_GroupName );
+  GroupGroupsLayout->addWidget( ComboBox_GroupName, 1 );
+
   /***************************************************************/
   GroupButtons = new QGroupBox(this);
   QHBoxLayout* aGroupButtonsLayout = new QHBoxLayout(GroupButtons);
@@ -532,6 +548,7 @@ SMESHGUI_AddQuadraticElementDlg::SMESHGUI_AddQuadraticElementDlg( SMESHGUI* theM
   /***************************************************************/
   aDialogLayout->addWidget(GroupConstructors);
   aDialogLayout->addWidget(GroupArguments);
+  aDialogLayout->addWidget(GroupGroups);
   aDialogLayout->addWidget(GroupButtons);
 
   Init(); /* Initialisations */
@@ -555,6 +572,9 @@ void SMESHGUI_AddQuadraticElementDlg::Init()
   myRadioButton1->setChecked(true);
   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
   
+  /* reset "Add to group" control */
+  GroupGroups->setChecked( false );
+
   myActor = 0;
 
   int aNumRows;
@@ -661,6 +681,9 @@ void SMESHGUI_AddQuadraticElementDlg::Init()
 //=================================================================================
 void SMESHGUI_AddQuadraticElementDlg::ClickOnApply()
 {
+  if( !isValid() )
+    return;
+
   if ( mySMESHGUI->isActiveStudyLocked() || myBusy || !IsValid() )
     return;
 
@@ -696,20 +719,53 @@ void SMESHGUI_AddQuadraticElementDlg::ClickOnApply()
   for (int i = 0; i < aNumberOfIds; i++)
     anArrayOfIdeces[i] = anIds[ i ];
 
+  SMESH::ElementType anElementType;
+  long anElemId = -1;
   SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
   switch (myType) {
   case QUAD_EDGE:
-    aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
+    anElementType = SMESH::EDGE;
+    anElemId = aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
   case QUAD_TRIANGLE:
   case QUAD_QUADRANGLE:
-    aMeshEditor->AddFace(anArrayOfIdeces.inout()); break;
+    anElementType = SMESH::FACE;
+    anElemId = aMeshEditor->AddFace(anArrayOfIdeces.inout()); break;
   case QUAD_TETRAHEDRON:
   case QUAD_PYRAMID:
   case QUAD_PENTAHEDRON: 
   case QUAD_HEXAHEDRON:
-    aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
+    anElementType = SMESH::VOLUME;
+    anElemId = aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
   }
     
+  if( anElemId != -1 && GroupGroups->isChecked() ) {
+    SMESH::SMESH_Group_var aGroup;
+    QString aGroupName = ComboBox_GroupName->currentText();
+    SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
+    for( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
+      SMESH::SMESH_GroupBase_var aGroupBase = aListOfGroups[i];
+      if( !aGroupBase->_is_nil() ) {
+        SMESH::SMESH_Group_var aRefGroup = SMESH::SMESH_Group::_narrow( aGroupBase );
+        if( !aRefGroup->_is_nil() ) {
+          QString aRefGroupName( aRefGroup->GetName() );
+          if( aRefGroupName == aGroupName ) {
+            aGroup = aRefGroup; // // add node to existing group
+            break;
+          }
+        }
+      }
+    }
+    if( aGroup->_is_nil() ) // create new group
+      aGroup = SMESH::AddGroup( myMesh, anElementType, aGroupName );
+
+    if( !aGroup->_is_nil() ) {
+      SMESH::long_array_var anIdList = new SMESH::long_array;
+      anIdList->length( 1 );
+      anIdList[0] = anElemId;
+      aGroup->Add( anIdList.inout() );
+    }
+  }
+
   SALOME_ListIO aList; aList.Append( myActor->getIO() );
   mySelector->ClearIndex();
   mySelectionMgr->setSelectedObjects( aList, false );
@@ -827,6 +883,8 @@ void SMESHGUI_AddQuadraticElementDlg::SelectionIntoArgument()
   if (myBusy) return;
   BusyLocker lock( myBusy );
   
+  QString aCurrentEntry = myEntry;
+
   if ( myIsEditCorners )
   {
     // clear
@@ -851,6 +909,7 @@ void SMESHGUI_AddQuadraticElementDlg::SelectionIntoArgument()
     }
       
     Handle(SALOME_InteractiveObject) anIO = aList.First();
+    myEntry = anIO->getEntry();
     myMesh = SMESH::GetMeshByIO(anIO);
     if (myMesh->_is_nil()) {
       updateButtons();
@@ -860,6 +919,37 @@ void SMESHGUI_AddQuadraticElementDlg::SelectionIntoArgument()
     myActor = SMESH::FindActorByEntry(anIO->getEntry());
   
   }
+
+  // process groups
+  if ( !myMesh->_is_nil() && myEntry != aCurrentEntry ) {
+    SMESH::ElementType anElementType;
+    switch ( myType ) {
+    case QUAD_EDGE:
+      anElementType = SMESH::EDGE; break;
+    case QUAD_TRIANGLE:
+    case QUAD_QUADRANGLE:
+      anElementType = SMESH::FACE; break;
+    case QUAD_TETRAHEDRON:
+    case QUAD_PYRAMID:
+    case QUAD_PENTAHEDRON: 
+    case QUAD_HEXAHEDRON:
+      anElementType = SMESH::VOLUME; break;
+    }
+    ComboBox_GroupName->clear();
+    ComboBox_GroupName->addItem( QString() );
+    SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
+    for ( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
+      SMESH::SMESH_GroupBase_var aGroupBase = aListOfGroups[i];
+      if ( !aGroupBase->_is_nil() && aGroupBase->GetType() == anElementType ) {
+        SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupBase );
+        if ( !aGroup->_is_nil() ) {
+          QString aGroupName( aGroup->GetName() );
+          if ( !aGroupName.isEmpty() )
+            ComboBox_GroupName->addItem( aGroupName );
+        }
+      }
+    }
+  }
   
   if (!myActor) {
     updateButtons();
@@ -1169,3 +1259,16 @@ void SMESHGUI_AddQuadraticElementDlg::updateButtons()
   buttonOk->setEnabled( valid );
   buttonApply->setEnabled( valid );
 }
+
+//=================================================================================
+// function : isValid
+// purpose  :
+//=================================================================================
+bool SMESHGUI_AddQuadraticElementDlg::isValid()
+{
+  if( GroupGroups->isChecked() && ComboBox_GroupName->currentText().isEmpty() ) {
+    SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ), tr( "GROUP_NAME_IS_EMPTY" ) );
+    return false;
+  }
+  return true;
+}