Salome HOME
0021347: [CEA 497] Visualisation into SMESH and VISU of hexagonal prism cells (MED_OC...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddMeshElementDlg.cxx
index 6df9978de5b4a4e68071c822a6815e20c9e9f7d0..9ac23000f73d59e725be904fca93574405872be7 100644 (file)
@@ -1,29 +1,30 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 // SMESH SMESHGUI : GUI for SMESH component
 // File   : SMESHGUI_AddMeshElementDlg.cxx
 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
-
 //  SMESH includes
+//
 #include "SMESHGUI_AddMeshElementDlg.h"
 
 #include "SMESHGUI.h"
@@ -156,43 +157,19 @@ namespace SMESH
 
     typedef std::vector<vtkIdType> TVTKIds;
     void SetPosition (SMESH_Actor* theActor,
-                      vtkIdType theType,
-                      const TVTKIds& theIds)
+                      vtkIdType    theType,
+                      TVTKIds&     theIds)
     {
       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
       myGrid->SetPoints(aGrid->GetPoints());
+      myGrid->Reset();
 
-      const int* aConn = NULL;
-      switch (theType) {
-      case VTK_TETRA:
-        {
-          static int anIds[] = {0,2,1,3};
-          aConn = anIds;
-          break;
-        }
-      case VTK_PYRAMID:
-        {
-          static int anIds[] = {0,3,2,1,4};
-          aConn = anIds;
-          break;
-        }
-      case VTK_HEXAHEDRON:
-        {
-          static int anIds[] = {0,3,2,1,4,7,6,5};
-          aConn = anIds;
-          break;
-        }
-      }
+      const std::vector<int>& interlace = SMDS_MeshCell::toVtkOrder( VTKCellType( theType ));
+      SMDS_MeshCell::applyInterlace( interlace, theIds );
 
-      myGrid->Reset();
       vtkIdList *anIds = vtkIdList::New();
-
-      if(aConn)
-        for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
-          anIds->InsertId(i,theIds[aConn[i]]);
-      else
-        for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
-          anIds->InsertId(i,theIds[i]);
+      for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
+        anIds->InsertId(i,theIds[i]);
 
       myGrid->InsertNextCell(theType,anIds);
       anIds->Delete();
@@ -237,12 +214,12 @@ namespace SMESH
 // function : SMESHGUI_AddMeshElementDlg()
 // purpose  : constructor
 //=================================================================================
-SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
-                                                        SMDSAbs_ElementType ElementType,
-                                                        int nbNodes )
+SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI*          theModule,
+                                                        SMDSAbs_EntityType ElementType)
   : QDialog( SMESH::GetDesktop( theModule ) ),
     mySMESHGUI( theModule ),
-    mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
+    mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
+    myBusy ( false )
 {
   setModal( false );
   setAttribute( Qt::WA_DeleteOnClose, true );
@@ -252,62 +229,72 @@ SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
   myIsPoly = false;
   mySimulation = new SMESH::TElementSimulation (anApp);
   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
+  myGeomType = ElementType;
+  myElementType = SMDSAbs_Volume;
 
   // verify nb nodes and type
-  myNbNodes = nbNodes;
-  myElementType = ElementType;
-  switch (ElementType) {
-  case SMDSAbs_0DElement:
-    if (myNbNodes != 1)
-      myNbNodes = 1;
-    break;
-  case SMDSAbs_Face:
-    //     if (myNbNodes != 3 && myNbNodes != 4)
-    //       myNbNodes = 3;
-    //     break;
-  case SMDSAbs_Volume:
-    //     if (myNbNodes != 4 && myNbNodes != 8) //(nbNodes < 4 || nbNodes > 8 || nbNodes == 7)
-    //       myNbNodes = 4;
-    break;
-  default:
-    myElementType = SMDSAbs_Edge;
-    myNbNodes = 2;
-  }
-
   QString elemName;
-  if (myNbNodes == 1) {
+  switch ( myGeomType ) {
+  case SMDSEntity_0D:
+    myNbNodes = 1;
+    myElementType = SMDSAbs_0DElement;
     elemName = "ELEM0D";
     myHelpFileName = "adding_nodes_and_elements_page.html#adding_0delems_anchor";
-  }
-  else if (myNbNodes == 2) {
+    break;
+  case SMDSEntity_Edge:
+    myNbNodes = 2;
+    myElementType = SMDSAbs_Edge;
     elemName = "EDGE";
     myHelpFileName = "adding_nodes_and_elements_page.html#adding_edges_anchor";
-  }
-  else if (myNbNodes == 3) {
+    break;
+  case SMDSEntity_Triangle:
+    myNbNodes = 3;
     elemName = "TRIANGLE";
+    myElementType = SMDSAbs_Face;
     myHelpFileName = "adding_nodes_and_elements_page.html#adding_triangles_anchor";
-  }
-  else if (myNbNodes == 4) {
-    if (myElementType == SMDSAbs_Face) {
-      elemName = "QUADRANGLE";
-      myHelpFileName = "adding_nodes_and_elements_page.html#adding_quadrangles_anchor";
-    }
-    else {
-      elemName = "TETRAS";
-      myHelpFileName = "adding_nodes_and_elements_page.html#adding_tetrahedrons_anchor";
-    }
-  }
-  else if (myNbNodes == 8) {
-    elemName = "HEXAS";
-    myHelpFileName = "adding_nodes_and_elements_page.html#adding_hexahedrons_anchor";
-  }
-  else if (myElementType == SMDSAbs_Face) {
+    break;
+  case SMDSEntity_Quadrangle:
+    myNbNodes = 4;
+    myElementType = SMDSAbs_Face;
+    elemName = "QUADRANGLE";
+    myHelpFileName = "adding_nodes_and_elements_page.html#adding_quadrangles_anchor";
+    break;
+  case SMDSEntity_Polygon:
+    myNbNodes = 0;
+    myElementType = SMDSAbs_Face;
     elemName = "POLYGON";
     myIsPoly = true;
     myHelpFileName = "adding_nodes_and_elements_page.html#adding_polygons_anchor";
-  }
-  else if (myElementType == SMDSAbs_Volume) {
-    myHelpFileName = "adding_nodes_and_elements_page.html#adding_polyhedrons_anchor";
+    break;
+  case SMDSEntity_Tetra:
+    myNbNodes = 4;
+    elemName = "TETRAS";
+    myHelpFileName = "adding_nodes_and_elements_page.html#adding_tetrahedrons_anchor";
+    break;
+  case SMDSEntity_Pyramid:
+    myNbNodes = 5;
+    elemName = "PYRAMID";
+    myHelpFileName = "adding_nodes_and_elements_page.html#adding_pyramids_anchor";
+    break;
+  case SMDSEntity_Hexa:
+    myNbNodes = 8;
+    elemName = "HEXAS";
+    myHelpFileName = "adding_nodes_and_elements_page.html#adding_hexahedrons_anchor";
+    break;
+  case SMDSEntity_Penta:
+    myNbNodes = 6;
+    elemName = "PENTA";
+    myHelpFileName = "adding_nodes_and_elements_page.html#adding_pentahedrons_anchor";
+    break;
+  case SMDSEntity_Hexagonal_Prism:
+    myNbNodes = 12;
+    elemName = "OCTA";
+    myHelpFileName = "adding_nodes_and_elements_page.html#adding_octahedrons_anchor";
+    break;
+  default:
+    myNbNodes = 2;
+    elemName = "EDGE";
+    myHelpFileName = "adding_nodes_and_elements_page.html#adding_edges_anchor";
   }
 
   QString iconName      = tr(QString("ICON_DLG_%1").arg(elemName).toLatin1().data());
@@ -349,11 +336,9 @@ SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
   SelectButtonC1A1 = new QPushButton(GroupC1);
   SelectButtonC1A1->setIcon(image1);
   LineEditC1A1 = new QLineEdit(GroupC1);
-  //  LineEditC1A1->setReadOnly(true);
-  if (!myIsPoly)
-    LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, myNbNodes));
+  LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, myIsPoly ? 1000 : myNbNodes));
 
-  Reverse = myElementType == SMDSAbs_Face ? new QCheckBox(tr("SMESH_REVERSE"), GroupC1) : 0;
+  Reverse = (myElementType == SMDSAbs_Face || myElementType == SMDSAbs_Volume ) ? new QCheckBox(tr("SMESH_REVERSE"), GroupC1) : 0;
 
   GroupC1Layout->addWidget(TextLabelC1A1,    0, 0);
   GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
@@ -370,6 +355,7 @@ SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
   TextLabel_GroupName = new QLabel( tr( "SMESH_GROUP" ), GroupGroups );
   ComboBox_GroupName = new QComboBox( GroupGroups );
   ComboBox_GroupName->setEditable( true );
+  ComboBox_GroupName->setInsertPolicy( QComboBox::NoInsert );
 
   GroupGroupsLayout->addWidget( TextLabel_GroupName );
   GroupGroupsLayout->addWidget( ComboBox_GroupName, 1 );
@@ -429,6 +415,7 @@ void SMESHGUI_AddMeshElementDlg::Init()
 
   /* reset "Add to group" control */
   GroupGroups->setChecked( false );
+  GroupGroups->setVisible( myElementType != SMDSAbs_0DElement );
 
   myNbOkNodes = 0;
   myActor = 0;
@@ -445,6 +432,7 @@ void SMESHGUI_AddMeshElementDlg::Init()
   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
   /* to close dialog if study frame change */
   connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
+  connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), SLOT(ClickOnCancel()));    
 
   if (Reverse)
     connect(Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)));
@@ -473,14 +461,42 @@ void SMESHGUI_AddMeshElementDlg::ClickOnApply()
     myBusy = true;
     SMESH::long_array_var anArrayOfIndices = new SMESH::long_array;
     anArrayOfIndices->length(myNbNodes);
-    bool reverse = (Reverse && Reverse->isChecked());
     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
-    for (int i = 0; i < aListId.count(); i++)
-      if (reverse)
-        anArrayOfIndices[i] = aListId[ myNbNodes - i - 1 ].toInt();
-      else
+    const std::vector<int>& revIndex = SMDS_MeshCell::reverseSmdsOrder( myGeomType );
+    if ( Reverse && Reverse->isChecked() && !revIndex.empty() )
+      for (int i = 0; i < aListId.count(); i++)
+        anArrayOfIndices[i] = aListId[ revIndex[i] ].toInt();
+    else if ( Reverse && Reverse->isChecked() && revIndex.empty() ) // polygon
+      for (int i = 0; i < aListId.count(); i++)
+        anArrayOfIndices[i] = aListId[ aListId.count()-1 - i ].toInt();
+    else
+      for (int i = 0; i < aListId.count(); i++)
         anArrayOfIndices[i] = aListId[ i ].toInt();
 
+    bool addToGroup = GroupGroups->isChecked();
+    QString aGroupName;
+
+    SMESH::SMESH_GroupBase_var aGroup;
+    int idx = 0;
+    if( addToGroup ) {
+      aGroupName = ComboBox_GroupName->currentText();
+      for ( int i = 1; i < ComboBox_GroupName->count(); i++ ) {
+        QString aName = ComboBox_GroupName->itemText( i );
+        if ( aGroupName == aName && ( i == ComboBox_GroupName->currentIndex() || idx == 0 ) )
+          idx = i;
+      }
+      if ( idx > 0 && idx < myGroups.count() ) {
+        SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( myGroups[idx-1] );
+        if ( !aGeomGroup->_is_nil() ) {
+          int res = SUIT_MessageBox::question( this, tr( "SMESH_WRN_WARNING" ),
+                                               tr( "MESH_STANDALONE_GRP_CHOSEN" ).arg( aGroupName ),
+                                               tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 );
+          if ( res == 1 ) return;
+        }
+        aGroup = myGroups[idx-1];
+      }
+    }
+
     long anElemId = -1;
     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
     switch (myElementType) {
@@ -488,43 +504,44 @@ void SMESHGUI_AddMeshElementDlg::ClickOnApply()
       anElemId = aMeshEditor->Add0DElement(anArrayOfIndices[0]); break;
     case SMDSAbs_Edge:
       anElemId = aMeshEditor->AddEdge(anArrayOfIndices.inout()); break;
-    case SMDSAbs_Face: {
-      if(myIsPoly)
+    case SMDSAbs_Face:
+      if ( myIsPoly )
         anElemId = aMeshEditor->AddPolygonalFace(anArrayOfIndices.inout());
       else
         anElemId = aMeshEditor->AddFace(anArrayOfIndices.inout());
       break;
-    }
-    case SMDSAbs_Volume:
+    default:
       anElemId = aMeshEditor->AddVolume(anArrayOfIndices.inout()); break;
-    default:;
     }
 
-    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 ( anElemId != -1 && addToGroup && !aGroupName.isEmpty() ) {
+      SMESH::SMESH_Group_var aGroupUsed;
+      if ( aGroup->_is_nil() ) {
+        // create new group 
+        aGroupUsed = SMESH::AddGroup( myMesh, (SMESH::ElementType)myElementType, aGroupName );
+        if ( !aGroupUsed->_is_nil() ) {
+          myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroupUsed));
+          ComboBox_GroupName->addItem( aGroupName );
+        }
+      }
+      else {
+        SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
+        if ( !aGeomGroup->_is_nil() ) {
+          aGroupUsed = myMesh->ConvertToStandalone( aGeomGroup );
+          if ( !aGroupUsed->_is_nil() && idx > 0 ) {
+            myGroups[idx-1] = SMESH::SMESH_GroupBase::_duplicate(aGroupUsed);
+            SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser();
           }
         }
+        else
+          aGroupUsed = SMESH::SMESH_Group::_narrow( aGroup );
       }
-      if( aGroup->_is_nil() ) // create new group
-        aGroup = SMESH::AddGroup( myMesh, (SMESH::ElementType)myElementType, aGroupName );
 
-      if( !aGroup->_is_nil() ) {
+      if ( !aGroupUsed->_is_nil() ) {
         SMESH::long_array_var anIdList = new SMESH::long_array;
         anIdList->length( 1 );
         anIdList[0] = anElemId;
-        aGroup->Add( anIdList.inout() );
+        aGroupUsed->Add( anIdList.inout() );
       }
     }
 
@@ -541,6 +558,8 @@ void SMESHGUI_AddMeshElementDlg::ClickOnApply()
     myEditCurrentArgument->setText("");
 
     myBusy = false;
+
+    SMESHGUI::Modified();
   }
 }
 
@@ -697,17 +716,17 @@ void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
 
   // process groups
   if ( !myMesh->_is_nil() && myEntry != aCurrentEntry ) {
+    myGroups.clear();
     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() == (SMESH::ElementType)myElementType ) {
-        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 );
+      SMESH::SMESH_GroupBase_var aGroup = aListOfGroups[i];
+      if ( !aGroup->_is_nil() && aGroup->GetType() == (SMESH::ElementType)myElementType ) {
+        QString aGroupName( aGroup->GetName() );
+        if ( !aGroupName.isEmpty() ) {
+          myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroup));
+          ComboBox_GroupName->addItem( aGroupName );
         }
       }
     }
@@ -751,24 +770,15 @@ void SMESHGUI_AddMeshElementDlg::displaySimulation()
       anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
 
     if (Reverse && Reverse->isChecked())
-      reverse(anIds.begin(),anIds.end());
-
-    vtkIdType aType = 0;
-    if (myIsPoly)
-      switch ( myElementType ) {
-      case SMDSAbs_Face  : aType = VTK_POLYGON; break;
-      default: return;
-      }
-    else {
-      switch (myNbNodes) {
-      case 2: aType = VTK_LINE; break;
-      case 3: aType = VTK_TRIANGLE; break;
-      case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
-      case 8: aType = VTK_HEXAHEDRON; break;
-      default: return;
-      }
+    {
+      const std::vector<int>& i = SMDS_MeshCell::reverseSmdsOrder( myGeomType );
+      if ( i.empty() ) // polygon
+        std::reverse( anIds.begin(), anIds.end() );
+      else
+        SMDS_MeshCell::applyInterlace( i, anIds );
     }
 
+    vtkIdType aType = SMDS_MeshCell::toVtkType( myGeomType );
     mySimulation->SetPosition(myActor,aType,anIds);
     SMESH::UpdateView();
   }