Back to index

salome-smesh  6.5.0
SMESHGUI_MeshPatternDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // SMESH SMESHGUI : GUI for SMESH component
00024 // File   : SMESHGUI_MeshPatternDlg.cxx
00025 // Author : Sergey LITONIN, Open CASCADE S.A.S.
00026 // SMESH includes
00027 //
00028 #include "SMESHGUI_MeshPatternDlg.h"
00029 
00030 #include "SMESHGUI.h"
00031 #include "SMESHGUI_CreatePatternDlg.h"
00032 #include "SMESHGUI_PatternWidget.h"
00033 #include "SMESHGUI_Utils.h"
00034 #include "SMESHGUI_VTKUtils.h"
00035 #include "SMESHGUI_PatternUtils.h"
00036 #include "SMESHGUI_GEOMGenUtils.h"
00037 
00038 #include <SMESH_Actor.h>
00039 #include <SMESH_ActorUtils.h>
00040 #include <SMESH_NumberFilter.hxx>
00041 
00042 #include <SMDS_Mesh.hxx>
00043 #include <SMDS_MeshElement.hxx>
00044 
00045 // SALOME GUI includes
00046 #include <SUIT_ResourceMgr.h>
00047 #include <SUIT_Desktop.h>
00048 #include <SUIT_FileDlg.h>
00049 #include <SUIT_Session.h>
00050 #include <SUIT_MessageBox.h>
00051 
00052 #include <LightApp_SelectionMgr.h>
00053 #include <SalomeApp_Tools.h>
00054 #include <LightApp_Application.h>
00055 #include <SalomeApp_IntSpinBox.h>
00056 
00057 #include <SALOME_ListIO.hxx>
00058 
00059 #include <SVTK_ViewModel.h>
00060 #include <SVTK_ViewWindow.h>
00061 #include <VTKViewer_CellLocationsArray.h>
00062 
00063 // SALOME KERNEL includes 
00064 #include <SALOMEDS_SObject.hxx>
00065 
00066 // OCCT includes
00067 #include <TColStd_MapOfInteger.hxx>
00068 
00069 // Qt includes
00070 #include <QFrame>
00071 #include <QVBoxLayout>
00072 #include <QHBoxLayout>
00073 #include <QGridLayout>
00074 #include <QLineEdit>
00075 #include <QPushButton>
00076 #include <QGroupBox>
00077 #include <QLabel>
00078 #include <QRadioButton>
00079 #include <QCheckBox>
00080 #include <QButtonGroup>
00081 #include <QList>
00082 #include <QDir>
00083 #include <QFileDialog>
00084 #include <QFileInfo>
00085 #include <QFile>
00086 #include <QKeyEvent>
00087 
00088 // VTK includes
00089 #include <vtkIdList.h>
00090 #include <vtkCellArray.h>
00091 #include <vtkUnsignedCharArray.h>
00092 #include <vtkUnstructuredGrid.h>
00093 #include <vtkDataSetMapper.h>
00094 #include <vtkProperty.h>
00095 
00096 #define SPACING 6
00097 #define MARGIN  11
00098 
00104 //=======================================================================
00105 // name    : SMESHGUI_MeshPatternDlg::SMESHGUI_MeshPatternDlg
00106 // Purpose : Constructor
00107 //=======================================================================
00108 SMESHGUI_MeshPatternDlg::SMESHGUI_MeshPatternDlg( SMESHGUI* theModule )
00109   : QDialog( SMESH::GetDesktop( theModule ) ),
00110     mySMESHGUI( theModule ),
00111     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
00112     myBusy( false ),
00113     myCreationDlg( 0 )
00114 {
00115   setModal( false );
00116   setWindowTitle( tr( "CAPTION" ) );
00117 
00118   QVBoxLayout* aDlgLay = new QVBoxLayout( this );
00119   aDlgLay->setMargin( MARGIN );
00120   aDlgLay->setSpacing( SPACING );
00121 
00122   QWidget* aMainFrame = createMainFrame  ( this );
00123   QWidget* aBtnFrame  = createButtonFrame( this );
00124 
00125   aDlgLay->addWidget( aMainFrame );
00126   aDlgLay->addWidget( aBtnFrame );
00127 
00128   aDlgLay->setStretchFactor( aMainFrame, 1 );
00129 
00130   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
00131 
00132   myHelpFileName = "pattern_mapping_page.html";
00133 
00134   Init();
00135 }
00136 
00137 //=======================================================================
00138 // name    : SMESHGUI_MeshPatternDlg::createMainFrame
00139 // Purpose : Create frame containing dialog's input fields
00140 //=======================================================================
00141 QWidget* SMESHGUI_MeshPatternDlg::createMainFrame (QWidget* theParent)
00142 {
00143   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
00144   QPixmap iconSlct ( mgr->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
00145   QPixmap icon2d   ( mgr->loadPixmap( "SMESH", tr( "ICON_PATTERN_2d" ) ) );
00146   QPixmap icon3d   ( mgr->loadPixmap( "SMESH", tr( "ICON_PATTERN_3d" ) ) );
00147   QPixmap iconOpen ( mgr->loadPixmap( "STD",   tr( "ICON_FILE_OPEN" ) ) );
00148 
00149   QPixmap iconSample2d ( mgr->loadPixmap( "SMESH", tr( "ICON_PATTERN_SAMPLE_2D" ) ) );
00150   QPixmap iconSample3d ( mgr->loadPixmap( "SMESH", tr( "ICON_PATTERN_SAMPLE_3D" ) ) );
00151 
00152   QWidget* aMainGrp = new QWidget( theParent );
00153 
00154   QVBoxLayout* aMainGrpLayout = new QVBoxLayout( aMainGrp );
00155   aMainGrpLayout->setMargin( 0 );
00156   aMainGrpLayout->setSpacing( SPACING );
00157 
00158   // Pattern type group
00159 
00160   QGroupBox* aTypeGrp = new QGroupBox( tr( "PATTERN_TYPE" ), aMainGrp );
00161   QHBoxLayout* aTypeGrpLayout = new QHBoxLayout( aTypeGrp );
00162   aTypeGrpLayout->setMargin( MARGIN );
00163   aTypeGrpLayout->setSpacing( SPACING );
00164 
00165   mySwitch2d = new QRadioButton( aTypeGrp );
00166   mySwitch3d = new QRadioButton( aTypeGrp );
00167   mySwitch2d->setIcon( icon2d );
00168   mySwitch3d->setIcon( icon3d );
00169 
00170   myTypeGrp = new QButtonGroup( aMainGrp );
00171   myTypeGrp->addButton( mySwitch2d, Type_2d );
00172   myTypeGrp->addButton( mySwitch3d, Type_3d );
00173 
00174   // ... layout widgets
00175 
00176   aTypeGrpLayout->addWidget( mySwitch2d );
00177   aTypeGrpLayout->addWidget( mySwitch3d );
00178 
00179   // Mesh group
00180 
00181   QGroupBox* aMeshGrp = new QGroupBox( tr( "SMESH_MESH" ), aMainGrp );
00182   QHBoxLayout* aMeshGrpLayout = new QHBoxLayout( aMeshGrp );
00183   aMeshGrpLayout->setMargin( MARGIN );
00184   aMeshGrpLayout->setSpacing( SPACING );
00185 
00186   QLabel* aMeshLab = new QLabel( tr( "SMESH_MESH" ), aMeshGrp );
00187   mySelBtn[ Mesh ] = new QPushButton( aMeshGrp );
00188   mySelBtn[ Mesh ]->setIcon( iconSlct );
00189   mySelEdit[ Mesh ] = new QLineEdit( aMeshGrp );
00190   mySelEdit[ Mesh ]->setReadOnly( true );
00191 
00192   // ... layout widgets
00193 
00194   aMeshGrpLayout->addWidget( aMeshLab );
00195   aMeshGrpLayout->addWidget( mySelBtn[ Mesh ] );
00196   aMeshGrpLayout->addWidget( mySelEdit[ Mesh ] );
00197 
00198   // Pattern group
00199 
00200   QGroupBox* aPatGrp = new QGroupBox( tr( "PATTERN" ), aMainGrp );
00201   QGridLayout* aPatGrpLayout = new QGridLayout( aPatGrp );
00202   aPatGrpLayout->setMargin( MARGIN );
00203   aPatGrpLayout->setSpacing( SPACING );
00204 
00205   // ... Pattern name
00206 
00207   QLabel* aNameLab = new QLabel( tr( "PATTERN" ), aPatGrp );
00208   myName = new QLineEdit( aPatGrp );
00209   myName->setReadOnly( true );
00210   myOpenBtn = new QPushButton( aPatGrp );
00211   myOpenBtn->setIcon( iconOpen );
00212   myNewBtn = new QPushButton( tr( "NEW" ), aPatGrp );
00213 
00214   // ... Mode selection check box
00215   myRefine = new QCheckBox( tr( "REFINE" ), aPatGrp );
00216 
00217   // ... selection widgets for Apply to geom mode
00218   myGeomGrp = new QFrame( aPatGrp );
00219   myGeomGrp->setFrameStyle( QFrame::NoFrame );
00220   QGridLayout* myGeomGrpLayout = new QGridLayout( myGeomGrp );
00221   myGeomGrpLayout->setMargin( 0 );
00222   myGeomGrpLayout->setSpacing( SPACING );
00223 
00224   for ( int i = Object; i <= Vertex2; i++ ) {
00225     mySelLbl[ i ] = new QLabel( myGeomGrp );
00226     mySelBtn[ i ] = new QPushButton( myGeomGrp );
00227     mySelBtn[ i ]->setIcon( iconSlct );
00228     mySelEdit[ i ] = new QLineEdit( myGeomGrp );
00229     mySelEdit[ i ]->setReadOnly( true );
00230     myGeomGrpLayout->addWidget( mySelLbl[ i ],  i - Object, 0 );
00231     myGeomGrpLayout->addWidget( mySelBtn[ i ],  i - Object, 1 );
00232     myGeomGrpLayout->addWidget( mySelEdit[ i ], i - Object, 2 );
00233   }
00234 
00235   // ... Widgets for refinement of existing mesh elements
00236 
00237   myRefineGrp = new QFrame( aPatGrp );
00238   myRefineGrp->setFrameStyle( QFrame::NoFrame );
00239 
00240   QGridLayout* myRefineGrpLayout = new QGridLayout( myRefineGrp );
00241   myRefineGrpLayout->setMargin( 0 );
00242   myRefineGrpLayout->setSpacing( SPACING );
00243 
00244   mySelLbl[ Ids ] = new QLabel( myRefineGrp );
00245   mySelBtn[ Ids ] = new QPushButton( myRefineGrp );
00246   mySelBtn[ Ids ]->setIcon( iconSlct );
00247   mySelEdit[ Ids ] = new QLineEdit( myRefineGrp );
00248 
00249   QLabel* aNodeLbl = new QLabel( tr( "NODE_1" ), myRefineGrp );
00250   myNode1          = new SalomeApp_IntSpinBox( myRefineGrp );
00251   myNode2Lbl       = new QLabel( tr( "NODE_2" ), myRefineGrp );
00252   myNode2          = new SalomeApp_IntSpinBox( myRefineGrp );
00253 
00254   myRefineGrpLayout->addWidget( mySelLbl[ Ids ],  0, 0 );
00255   myRefineGrpLayout->addWidget( mySelBtn[ Ids ],  0, 1 );
00256   myRefineGrpLayout->addWidget( mySelEdit[ Ids ], 0, 2 );
00257   myRefineGrpLayout->addWidget( aNodeLbl,         1, 0 );
00258   myRefineGrpLayout->addWidget( myNode1,          1, 1, 1, 2 );
00259   myRefineGrpLayout->addWidget( myNode2Lbl,       2, 0 );
00260   myRefineGrpLayout->addWidget( myNode2,          2, 1, 1, 2 );
00261 
00262   // ... reverse check box
00263   myReverseChk = new QCheckBox( tr( "REVERSE" ), aPatGrp );
00264 
00265   // ... CreatePoly check box
00266   myCreatePolygonsChk = new QCheckBox( tr( "CREATE_POLYGONS_NEAR_BOUNDARY" ), aPatGrp );
00267   myCreatePolyedrsChk = new QCheckBox( tr( "CREATE_POLYEDRS_NEAR_BOUNDARY" ), aPatGrp );
00268 
00269   // ... Pictures 2d and 3d
00270 
00271   myPicture2d = new SMESHGUI_PatternWidget( aPatGrp );
00272   myPicture2d->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00273 
00274   myPicture3d = new QLabel( aPatGrp );
00275   myPicture3d->setPixmap( iconSample3d );
00276   myPicture3d->setScaledContents( false );
00277   myPicture3d->setAlignment( Qt::AlignCenter );
00278   myPicture3d->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00279 
00280   // ... preview check box
00281 
00282   myPreviewChk = new QCheckBox( tr( "PREVIEW" ), aPatGrp );
00283 
00284   // ... layout widgets 
00285 
00286   aPatGrpLayout->addWidget( aNameLab,            0, 0 );
00287   aPatGrpLayout->addWidget( myName,              0, 1 );
00288   aPatGrpLayout->addWidget( myOpenBtn,           0, 2 );
00289   aPatGrpLayout->addWidget( myNewBtn,            0, 3 );
00290   aPatGrpLayout->addWidget( myRefine,            1, 0, 1, 4 );
00291   aPatGrpLayout->addWidget( myRefine,            1, 0, 1, 4 );
00292   aPatGrpLayout->addWidget( myGeomGrp,           2, 0, 1, 4 );
00293   aPatGrpLayout->addWidget( myRefineGrp,         3, 0, 1, 4 );
00294   aPatGrpLayout->addWidget( myReverseChk,        4, 0, 1, 4 );
00295   aPatGrpLayout->addWidget( myCreatePolygonsChk, 5, 0, 1, 4 );
00296   aPatGrpLayout->addWidget( myCreatePolyedrsChk, 6, 0, 1, 4 );
00297   aPatGrpLayout->addWidget( myPicture2d,         7, 0, 1, 4 );
00298   aPatGrpLayout->addWidget( myPicture3d,         8, 0, 1, 4 );
00299   aPatGrpLayout->addWidget( myPreviewChk,        9, 0, 1, 4 );
00300 
00301   // main layout
00302   
00303   aMainGrpLayout->addWidget( aTypeGrp );
00304   aMainGrpLayout->addWidget( aMeshGrp );
00305   aMainGrpLayout->addWidget( aPatGrp );
00306 
00307   // Connect signals and slots
00308 
00309   connect(myTypeGrp, SIGNAL(buttonClicked(int)), SLOT(onTypeChanged(int)));
00310   connect(myOpenBtn, SIGNAL(clicked()),    SLOT(onOpen()));
00311   connect(myNewBtn,  SIGNAL(clicked()),    SLOT(onNew()));
00312 
00313   connect(myReverseChk, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
00314   connect(myPreviewChk, SIGNAL(toggled(bool)), SLOT(onPreview(bool)));
00315   connect(myRefine,     SIGNAL(toggled(bool)), SLOT(onModeToggled(bool)));
00316 
00317   connect(myNode1, SIGNAL(valueChanged(int)), SLOT(onNodeChanged(int)));
00318   connect(myNode2, SIGNAL(valueChanged(int)), SLOT(onNodeChanged(int)));
00319 
00320   connect(mySelEdit[Ids], SIGNAL(textChanged(const QString&)), SLOT(onTextChanged(const QString&)));
00321 
00322   QMap< int, QPushButton* >::iterator anIter;
00323   for (anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter)
00324     connect(*anIter, SIGNAL(clicked()), SLOT(onSelInputChanged()));
00325 
00326   return aMainGrp;
00327 }
00328 
00329 //=======================================================================
00330 // name    : SMESHGUI_MeshPatternDlg::createButtonFrame
00331 // Purpose : Create frame containing buttons
00332 //=======================================================================
00333 QWidget* SMESHGUI_MeshPatternDlg::createButtonFrame (QWidget* theParent)
00334 {
00335   QFrame* aFrame = new QFrame(theParent);
00336   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
00337 
00338   myOkBtn     = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aFrame);
00339   myApplyBtn  = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
00340   myCloseBtn  = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
00341   myHelpBtn   = new QPushButton(tr("SMESH_BUT_HELP"),  aFrame);
00342 
00343   QHBoxLayout* aLay = new QHBoxLayout( aFrame );
00344   aLay->setMargin( MARGIN );
00345   aLay->setSpacing( SPACING );
00346 
00347   aLay->addWidget(myOkBtn);
00348   aLay->addWidget(myApplyBtn);
00349   aLay->addSpacing( 10 );
00350   aLay->addStretch();
00351   aLay->addWidget(myCloseBtn);
00352   aLay->addWidget(myHelpBtn);
00353 
00354   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
00355   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
00356   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
00357   connect(myHelpBtn,  SIGNAL(clicked()), SLOT(onHelp()));
00358 
00359   return aFrame;
00360 }
00361 
00362 //=======================================================================
00363 // name    : SMESHGUI_MeshPatternDlg::~SMESHGUI_MeshPatternDlg
00364 // Purpose : Destructor
00365 //=======================================================================
00366 SMESHGUI_MeshPatternDlg::~SMESHGUI_MeshPatternDlg()
00367 {
00368 }
00369 
00370 //=======================================================================
00371 // name    : SMESHGUI_MeshPatternDlg::Init
00372 // Purpose : Init dialog fields, connect signals and slots, show dialog
00373 //=======================================================================
00374 void SMESHGUI_MeshPatternDlg::Init()
00375 {
00376   myPattern = SMESH::GetPattern();
00377   myPreviewActor = 0;
00378   myIsCreateDlgOpen = false;
00379   mySelInput = Mesh;
00380   myType = -1;
00381   myNbPoints = -1;
00382   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
00383   myMesh = SMESH::SMESH_Mesh::_nil();
00384 
00385   myMeshShape = GEOM::GEOM_Object::_nil();
00386   myGeomObj[ Object  ] = GEOM::GEOM_Object::_nil();
00387   myGeomObj[ Vertex1 ] = GEOM::GEOM_Object::_nil();
00388   myGeomObj[ Vertex2 ] = GEOM::GEOM_Object::_nil();
00389 
00390   // selection and SMESHGUI
00391   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
00392   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
00393   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
00394 
00395   myTypeGrp->button(Type_2d)->setChecked(true);
00396   onTypeChanged(Type_2d);
00397   onModeToggled(isRefine());
00398 
00399   updateGeometry();
00400 
00401   resize(100,100);
00402 
00403   activateSelection();
00404   onSelectionDone();
00405 
00406   this->show();
00407 }
00408 
00409 //=======================================================================
00410 // name    : SMESHGUI_MeshPatternDlg::isValid
00411 // Purpose : Verify validity of entry data
00412 //=======================================================================
00413 bool SMESHGUI_MeshPatternDlg::isValid (const bool theMess)
00414 {
00415   if (isRefine())
00416   {
00417     QString msg;
00418     bool ok = true;
00419     ok = myNode1->isValid( msg, theMess ) && ok;
00420     if (myType == Type_3d)
00421       ok = myNode2->isValid( msg, theMess ) && ok;
00422     if( !ok ) {
00423       if( theMess ) {
00424         QString str( tr( "SMESH_INCORRECT_INPUT" ) );
00425         if ( !msg.isEmpty() )
00426           str += "\n" + msg;
00427         SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
00428       }
00429       return false;
00430     }
00431   }
00432 
00433   QList<int> ids;
00434   if ((isRefine() &&
00435        (myMesh->_is_nil() || !getIds(ids) || getNode(false) < 0 ||
00436         (myType == Type_3d && (getNode(true) < 0 || getNode(false) == getNode(true)))))
00437       ||
00438       (!isRefine() &&
00439        (myMesh->_is_nil() || myMeshShape->_is_nil() || myGeomObj[ Object ]->_is_nil() ||
00440         myGeomObj[ Vertex1 ]->_is_nil() || (myType == Type_3d && myGeomObj[ Vertex2 ]->_is_nil()))))
00441   {
00442     if (theMess)
00443       SUIT_MessageBox::information(this, tr("SMESH_INSUFFICIENT_DATA"),
00444                                    tr("SMESHGUI_INVALID_PARAMETERS"));
00445     return false;
00446   }
00447 
00448   if ( myName->text().isEmpty() ) {
00449     if (theMess)
00450       SUIT_MessageBox::information(this, tr("SMESH_INSUFFICIENT_DATA"),
00451                                    tr("SMESHGUI_INVALID_PARAMETERS"));
00452     return false;
00453   }
00454 
00455   return true;
00456 }
00457 
00458 //=======================================================================
00459 // name    : SMESHGUI_MeshPatternDlg::onApply
00460 // Purpose : SLOT called when "Apply" button pressed.
00461 //=======================================================================
00462 bool SMESHGUI_MeshPatternDlg::onApply()
00463 {
00464   try {
00465     if (!isValid())
00466       return false;
00467 
00468     erasePreview();
00469 
00470     if (isRefine()) { // Refining existing mesh elements
00471       {
00472         QStringList aParameters;
00473         aParameters << myNode1->text();
00474         if(myType == Type_3d )
00475           aParameters << myNode2->text();
00476         myMesh->SetParameters( aParameters.join(":").toLatin1().constData() );
00477       }
00478       QList<int> ids;
00479       getIds(ids);
00480       SMESH::long_array_var varIds = new SMESH::long_array();
00481       varIds->length(ids.count());
00482       int i = 0;
00483       for (QList<int>::iterator it = ids.begin(); it != ids.end(); ++it)
00484         varIds[i++] = *it;
00485       myType == Type_2d
00486         ? myPattern->ApplyToMeshFaces  (myMesh, varIds, getNode(false), myReverseChk->isChecked())
00487         : myPattern->ApplyToHexahedrons(myMesh, varIds, getNode(false), getNode(true));
00488 
00489     } else { // Applying a pattern to geometrical object
00490       if (myType == Type_2d)
00491         myPattern->ApplyToFace(myGeomObj[Object], myGeomObj[Vertex1], myReverseChk->isChecked());
00492       else
00493         myPattern->ApplyTo3DBlock(myGeomObj[Object], myGeomObj[Vertex1], myGeomObj[Vertex2]);
00494     }
00495 
00496     bool toCreatePolygons = myCreatePolygonsChk->isChecked();
00497     bool toCreatePolyedrs = myCreatePolyedrsChk->isChecked();
00498     if ( myPattern->MakeMesh( myMesh, toCreatePolygons, toCreatePolyedrs ) ) {
00499       //mySelectionMgr->clearSelected();
00500       bool autoUpdate = SMESHGUI::automaticUpdate();
00501       if (!isRefine() && autoUpdate) {
00502         _PTR(SObject) aSO = SMESH::FindSObject(myMesh.in());
00503         SMESH_Actor* anActor = SMESH::FindActorByEntry(aSO->GetID().c_str());
00504         if (!anActor) {
00505           anActor = SMESH::CreateActor(aSO->GetStudy(), aSO->GetID().c_str());
00506           if (anActor) {
00507             SMESH::DisplayActor(SMESH::GetActiveWindow(), anActor);
00508             SMESH::FitAll();
00509           }
00510         }
00511       }
00512       mySelectionMgr->clearSelected();
00513       SMESH::UpdateView();
00514       SMESHGUI::Modified();
00515 
00516       mySMESHGUI->updateObjBrowser(true);
00517 
00518       mySelEdit[ Ids ]->setText("");
00519 
00520       return true;
00521     } else {
00522       SUIT_MessageBox::information(this, tr("SMESH_ERROR"),
00523                                    tr("SMESH_OPERATION_FAILED"));
00524       return false;
00525     }
00526   } catch (const SALOME::SALOME_Exception& S_ex) {
00527     SalomeApp_Tools::QtCatchCorbaException(S_ex);
00528   } catch (...) {
00529   }
00530 
00531   return false;
00532 }
00533 
00534 //=======================================================================
00535 // name    : SMESHGUI_MeshPatternDlg::onOk
00536 // Purpose : SLOT called when "Ok" button pressed.
00537 //=======================================================================
00538 void SMESHGUI_MeshPatternDlg::onOk()
00539 {
00540   if (onApply())
00541     onClose();
00542 }
00543 
00544 //=======================================================================
00545 // name    : SMESHGUI_MeshPatternDlg::onClose
00546 // Purpose : SLOT called when "Close" button pressed. Close dialog
00547 //=======================================================================
00548 void SMESHGUI_MeshPatternDlg::onClose()
00549 {
00550   mySelectionMgr->clearFilters();
00551   SMESH::SetPickable();
00552   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
00553     aViewWindow->SetSelectionMode(ActorSelection);
00554   disconnect(mySelectionMgr, 0, this, 0);
00555   disconnect(mySMESHGUI, 0, this, 0);
00556   mySMESHGUI->ResetState();
00557   erasePreview();
00558   reject();
00559 }
00560 
00561 //=================================================================================
00562 // function : onHelp()
00563 // purpose  :
00564 //=================================================================================
00565 void SMESHGUI_MeshPatternDlg::onHelp()
00566 {
00567   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
00568   if (app) 
00569     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
00570   else {
00571                 QString platform;
00572 #ifdef WIN32
00573                 platform = "winapplication";
00574 #else
00575                 platform = "application";
00576 #endif
00577     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
00578                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
00579                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
00580                                                                  platform)).
00581                              arg(myHelpFileName));
00582   }
00583 }
00584 
00585 //=======================================================================
00586 // name    : SMESHGUI_MeshPatternDlg::onSelectionDone
00587 // Purpose : SLOT called when selection changed
00588 //=======================================================================
00589 void SMESHGUI_MeshPatternDlg::onSelectionDone()
00590 {
00591   if (myBusy)
00592     return;
00593 
00594   try {
00595     if (mySelInput == Mesh) {
00596       SALOME_ListIO aList;
00597       mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
00598       if (aList.Extent() != 1)
00599         return;
00600 
00601       // Retrieve mesh from selection
00602       Handle(SALOME_InteractiveObject) anIO = aList.First();
00603       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIO);
00604       if (aMesh->_is_nil())
00605         return;
00606 
00607       // Get geom object corresponding to the mesh
00608       _PTR(SObject) aSO = SMESH::FindSObject(aMesh.in());
00609       myMeshShape = SMESH::GetGeom(aSO);
00610 
00611       // Clear fields of geom objects if mesh was changed
00612       if (myMesh != aMesh) {
00613         for (int i = Object; i <= Ids; i++) {
00614           myGeomObj[ i ] = GEOM::GEOM_Object::_nil();
00615           mySelEdit[ i ]->setText("");
00616         }
00617       }
00618 
00619       myMesh = aMesh;
00620 
00621       // Set name of mesh in line edit
00622       QString aName;
00623       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
00624       mySelEdit[ Mesh ]->setText(aName);
00625 
00626     } else if (mySelInput == Ids) {
00627       SALOME_ListIO aList;
00628       mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
00629       if (aList.Extent() != 1)
00630         return;
00631 
00632       QString anIds;
00633       if (!SMESH::GetNameOfSelectedElements(mySelector, aList.First(), anIds))
00634         anIds = "";
00635 
00636       myBusy = true;
00637       mySelEdit[ Ids ]->setText(anIds);
00638       myBusy = false;
00639 
00640     } else {
00641       SALOME_ListIO aList;
00642       mySelectionMgr->selectedObjects(aList, SVTK_Viewer::Type());
00643       if (aList.Extent() != 1)
00644         return;
00645 
00646       // Get geom object from selection
00647       Handle(SALOME_InteractiveObject) anIO = aList.First();
00648       GEOM::GEOM_Object_var anObj = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
00649       if (anObj->_is_nil())
00650         return;
00651 
00652       // Clear fields of vertexes if face or 3d block was changed
00653       if (anObj != myGeomObj[ mySelInput ] && mySelInput == Object) {
00654         for (int i = Vertex1; i <= Vertex2; i++) {
00655           myGeomObj[ i ] = GEOM::GEOM_Object::_nil();
00656           mySelEdit[ i ]->setText("");
00657         }
00658       }
00659 
00660       myGeomObj[ mySelInput ] = anObj;
00661 
00662       // Set name of geom object in line edit
00663       QString aName;
00664       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
00665       mySelEdit[ mySelInput ]->setText(aName);
00666     }
00667   } catch (const SALOME::SALOME_Exception& S_ex) {
00668     SalomeApp_Tools::QtCatchCorbaException(S_ex);
00669     resetSelInput();
00670   } catch (...) {
00671     resetSelInput();
00672   }
00673 
00674   updateWgState();
00675   displayPreview();
00676 }
00677 
00678 //=======================================================================
00679 // name    : SMESHGUI_MeshPatternDlg::resetSelInput
00680 // Purpose : Reset fields corresponding to the current selection input
00681 //=======================================================================
00682 void SMESHGUI_MeshPatternDlg::resetSelInput()
00683 {
00684   if (mySelInput == Mesh)
00685   {
00686     myMesh = SMESH::SMESH_Mesh::_nil();
00687     myMeshShape = GEOM::GEOM_Object::_nil();
00688   }
00689 
00690   else
00691     myGeomObj[ mySelInput ] = GEOM::GEOM_Object::_nil();
00692 
00693   mySelEdit[ mySelInput ]->setText("");
00694 }
00695 
00696 //=======================================================================
00697 // name    : SMESHGUI_MeshPatternDlg::onDeactivate
00698 // Purpose : SLOT called when dialog must be deativated
00699 //=======================================================================
00700 void SMESHGUI_MeshPatternDlg::onDeactivate()
00701 {
00702   mySelectionMgr->clearFilters();
00703   //if (myReverseChk->isChecked())
00704   //  erasePreview();
00705   disconnect(mySelectionMgr, 0, this, 0);
00706   setEnabled(false);
00707 }
00708 
00709 //=======================================================================
00710 // name    : SMESHGUI_MeshPatternDlg::enterEvent
00711 // Purpose : Event filter
00712 //=======================================================================
00713 void SMESHGUI_MeshPatternDlg::enterEvent (QEvent*)
00714 {
00715   if (myIsCreateDlgOpen)
00716     return;
00717 
00718   if (myReverseChk->isChecked())
00719     displayPreview();
00720   mySMESHGUI->EmitSignalDeactivateDialog();
00721   setEnabled(true);
00722   activateSelection();
00723   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
00724   onTextChanged(mySelEdit[Ids]->text());
00725 }
00726 
00727 //=======================================================================
00728 // name    : SMESHGUI_MeshPatternDlg::closeEvent
00729 // Purpose :
00730 //=======================================================================
00731 void SMESHGUI_MeshPatternDlg::closeEvent (QCloseEvent*)
00732 {
00733   onClose();
00734 }
00735 
00736 //=======================================================================
00737 // name    : SMESHGUI_MeshPatternDlg::onSelInputChanged
00738 // Purpose : SLOT. Called when -> button clicked.
00739 //           Change current selection input field
00740 //=======================================================================
00741 void SMESHGUI_MeshPatternDlg::onSelInputChanged()
00742 {
00743   const QObject* aSender = sender();
00744   for (int i = Mesh; i <= Ids; i++)
00745     if (aSender == mySelBtn[ i ])
00746       mySelInput = i;
00747 
00748   activateSelection();
00749   onSelectionDone();
00750 }
00751 
00752 //=======================================================================
00753 // name    : SMESHGUI_MeshPatternDlg::prepareFilters
00754 // Purpose : Prepare filters for dialog
00755 //=======================================================================
00756 QStringList SMESHGUI_MeshPatternDlg::prepareFilters() const
00757 {
00758   static QStringList aList;
00759   if (aList.isEmpty())
00760   {
00761     aList.append(tr("PATTERN_FILT"));
00762     //aList.append(tr("ALL_FILES_FILTER"));
00763   }
00764 
00765   return aList;
00766 }
00767 
00768 //=======================================================================
00769 // name    : SMESHGUI_MeshPatternDlg::autoExtension
00770 // Purpose : Append extension to the file name
00771 //=======================================================================
00772 QString SMESHGUI_MeshPatternDlg::autoExtension (const QString& theFileName) const
00773 {
00774   QString anExt = theFileName.section('.', -1);
00775   return anExt != "smp" && anExt != "SMP" ? theFileName + ".smp" : theFileName;
00776 }
00777 
00778 //=======================================================================
00779 // name    : SMESHGUI_MeshPatternDlg::onOpen
00780 // Purpose : SLOT. Called when "Open" button clicked.
00781 //           Displays file open dialog
00782 //=======================================================================
00783 void SMESHGUI_MeshPatternDlg::onOpen()
00784 {
00785   SUIT_FileDlg* aDlg = new SUIT_FileDlg (this, true);
00786   aDlg->setWindowTitle(tr("LOAD_PATTERN"));
00787   aDlg->setFileMode(QFileDialog::ExistingFile);
00788   aDlg->setFilters(prepareFilters());
00789   if (!myName->text().isEmpty())
00790     aDlg->selectFile(myName->text() + ".smp");
00791   QPushButton* anOkBtn = qFindChild<QPushButton*>( aDlg, "OK" );
00792   if (anOkBtn != 0)
00793     anOkBtn->setText(tr("SMESH_BUT_OK"));
00794 
00795   if (aDlg->exec() != Accepted)
00796     return;
00797 
00798   QString fName = aDlg->selectedFile();
00799   if (fName.isEmpty())
00800     return;
00801 
00802   if (QFileInfo(fName).suffix().isEmpty())
00803     fName = autoExtension(fName);
00804 
00805   fName = QDir::convertSeparators(fName);
00806 
00807   QString prev = QDir::convertSeparators(myName->text());
00808   if (prev == fName)
00809     return;
00810 
00811   // Read string from file
00812   QFile aFile(fName);
00813   if (!aFile.open(QIODevice::ReadOnly)) {
00814     SUIT_MessageBox::information(this, tr("SMESH_ERROR"),
00815                                  tr("ERROR_OF_OPENING"));
00816     return;
00817   }
00818 
00819   QByteArray aDataArray = aFile.readAll();
00820   if (aDataArray.isEmpty()) {
00821     SUIT_MessageBox::information(this, tr("SMESH_ERROR"),
00822                                  tr("ERROR_OF_READING"));
00823     return;
00824   }
00825 
00826   if (loadFromFile(aDataArray))
00827     myName->setText(QFileInfo(fName).completeBaseName());
00828 
00829   updateWgState();
00830   displayPreview();
00831 }
00832 
00833 //=======================================================================
00834 // name    : SMESHGUI_MeshPatternDlg::onCloseCreationDlg
00835 // Purpose : SLOT. Called when "Pattern creation" dialog closed with "Close"
00836 //=======================================================================
00837 void SMESHGUI_MeshPatternDlg::onCloseCreationDlg()
00838 {
00839   setEnabled(true);
00840   myIsCreateDlgOpen = false;
00841 }
00842 
00843 //=======================================================================
00844 // name    : SMESHGUI_MeshPatternDlg::onOkCreationDlg
00845 // Purpose : SLOT. Called when "Pattern creation" dialog closed with OK
00846 //           or SAVE buttons. Initialize myPattern field. Redisplay preview
00847 //=======================================================================
00848 void SMESHGUI_MeshPatternDlg::onOkCreationDlg()
00849 {
00850   myPattern = SMESH::SMESH_Pattern::_duplicate(myCreationDlg->GetPattern());
00851   myName->setText(myCreationDlg->GetPatternName());
00852 
00853   updateWgState();
00854   displayPreview();
00855 
00856   setEnabled(true);
00857   myIsCreateDlgOpen = false;
00858 }
00859 
00860 //=======================================================================
00861 // name    : SMESHGUI_MeshPatternDlg::onNew
00862 // Purpose : SLOT. Called when "New..." button clicked. Create new pattern
00863 //=======================================================================
00864 void SMESHGUI_MeshPatternDlg::onNew()
00865 {
00866   setEnabled(false);
00867   myIsCreateDlgOpen = true;
00868   if (myCreationDlg == 0)
00869   {
00870     myCreationDlg = new SMESHGUI_CreatePatternDlg( mySMESHGUI, myType);
00871     connect(myCreationDlg, SIGNAL(NewPattern()), SLOT(onOkCreationDlg()));
00872     connect(myCreationDlg, SIGNAL(Close()), SLOT(onCloseCreationDlg()));
00873   }
00874   else
00875     myCreationDlg->Init(myType);
00876 
00877   myCreationDlg->SetMesh(myMesh);
00878   myCreationDlg->show();
00879 }
00880 
00881 //=======================================================================
00882 // name    : SMESHGUI_MeshPatternDlg::onReverse
00883 // Purpose : SLOT. Called when state of "Reverse order..." checkbox chaged
00884 //           Calculate new points of the mesh to be created. Redisplay preview
00885 //=======================================================================
00886 void SMESHGUI_MeshPatternDlg::onReverse (bool)
00887 {
00888   displayPreview();
00889 }
00890 
00891 //=======================================================================
00892 
00893 // name    : SMESHGUI_MeshPatternDlg::onPreview
00894 // Purpose : SLOT. Called when state of "Preview" checkbox changed
00895 //           Display/Erase preview
00896 //=======================================================================
00897 void SMESHGUI_MeshPatternDlg::onPreview (bool)
00898 {
00899   displayPreview();
00900 }
00901 
00902 //=======================================================================
00903 // name    : SMESHGUI_MeshPatternDlg::displayPreview
00904 // Purpose : Display preview
00905 //=======================================================================
00906 void SMESHGUI_MeshPatternDlg::displayPreview()
00907 {
00908   try {
00909     // Redisplay preview in dialog
00910     SMESH::point_array_var pnts = myPattern->GetPoints();
00911     SMESH::long_array_var keyPoints = myPattern->GetKeyPoints();
00912     SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints(false);
00913 
00914     if (pnts->length()       == 0 ||
00915         keyPoints->length()  == 0 ||
00916         elemPoints->length() == 0) {
00917       erasePreview();
00918       return;
00919     } else {
00920       PointVector aPoints(pnts->length());
00921       QVector<int> aKeyPoints(keyPoints->length());
00922       ConnectivityVector anElemPoints(elemPoints->length());
00923 
00924       for (int i = 0, n = pnts->length(); i < n; i++)
00925         aPoints[ i ] = pnts[ i ];
00926 
00927       for (int i2 = 0, n2 = keyPoints->length(); i2 < n2; i2++)
00928         aKeyPoints[ i2 ] = keyPoints[ i2 ];
00929 
00930       for (int i3 = 0, n3 = elemPoints->length(); i3 < n3; i3++) {
00931         QVector<int> aVec(elemPoints[ i3 ].length());
00932         for (int i4 = 0, n4 = elemPoints[ i3 ].length(); i4 < n4; i4++)
00933           aVec[ i4 ] = elemPoints[ i3 ][ i4 ];
00934 
00935         anElemPoints[ i3 ] = aVec;
00936       }
00937 
00938       myPicture2d->SetPoints(aPoints, aKeyPoints, anElemPoints);
00939     }
00940 
00941     // Redisplay preview in 3D viewer
00942     if (myPreviewActor != 0) {
00943       if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView()) {
00944         vf->RemoveActor(myPreviewActor);
00945         vf->Repaint();
00946       }
00947       myPreviewActor->Delete();
00948       myPreviewActor = 0;
00949     }
00950 
00951     if (!myPreviewChk->isChecked() || !isValid(false))
00952       return;
00953 
00954     vtkUnstructuredGrid* aGrid = getGrid();
00955     if (aGrid == 0)
00956       return;
00957 
00958     // Create and display actor
00959     vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
00960     aMapper->SetInput(aGrid);
00961 
00962     myPreviewActor = SALOME_Actor::New();
00963     myPreviewActor->PickableOff();
00964     myPreviewActor->SetMapper(aMapper);
00965 
00966     vtkProperty* aProp = vtkProperty::New();
00967     aProp->SetRepresentationToWireframe();
00968     aProp->SetColor(250, 0, 250);
00969     if (SMESH::FindActorByObject(myMesh))
00970       aProp->SetLineWidth( SMESH::GetFloat( "SMESH:element_width", 1 ) + 1 );
00971     else
00972       aProp->SetLineWidth(1);
00973     myPreviewActor->SetProperty(aProp);
00974 
00975     myPreviewActor->SetRepresentation(3);
00976 
00977     SMESH::GetCurrentVtkView()->AddActor(myPreviewActor);
00978     SMESH::GetCurrentVtkView()->Repaint();
00979 
00980     aProp->Delete();
00981     aGrid->Delete();
00982   } catch (const SALOME::SALOME_Exception& S_ex) {
00983     SalomeApp_Tools::QtCatchCorbaException(S_ex);
00984     erasePreview();
00985   } catch (...) {
00986     erasePreview();
00987   }
00988 }
00989 
00990 //=======================================================================
00991 // name    : SMESHGUI_MeshPatternDlg::erasePreview
00992 // Purpose : Erase preview
00993 //=======================================================================
00994 void SMESHGUI_MeshPatternDlg::erasePreview()
00995 {
00996   // Erase preview in 2D viewer
00997   myPicture2d->SetPoints(PointVector(), QVector<int>(), ConnectivityVector());
00998 
00999   // Erase preview in 3D viewer
01000   if (myPreviewActor == 0)
01001     return;
01002 
01003 
01004   if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView())
01005   {
01006     vf->RemoveActor(myPreviewActor);
01007     vf->Repaint();
01008   }
01009   myPreviewActor->Delete();
01010   myPreviewActor = 0;
01011 }
01012 
01013 //=======================================================================
01014 // name    : SMESHGUI_MeshPatternDlg::updateWgState
01015 // Purpose : Enable/disable selection widgets
01016 //=======================================================================
01017 void SMESHGUI_MeshPatternDlg::updateWgState()
01018 {
01019   if (myMesh->_is_nil()) {
01020     for (int i = Object; i <= Ids; i++) {
01021       mySelBtn [ i ]->setEnabled(false);
01022       mySelEdit[ i ]->setEnabled(false);
01023       mySelEdit[ i ]->setText("");
01024     }
01025     myNode1->setEnabled(false);
01026     myNode2->setEnabled(false);
01027     myNode1->setRange(0, 0);
01028     myNode2->setRange(0, 0);
01029   } else {
01030     mySelBtn [ Object ]->setEnabled(true);
01031     mySelEdit[ Object ]->setEnabled(true);
01032     mySelBtn [ Ids ]   ->setEnabled(true);
01033     mySelEdit[ Ids ]   ->setEnabled(true);
01034 
01035     if (myGeomObj[ Object ]->_is_nil()) {
01036       for (int i = Vertex1; i <= Vertex2; i++) {
01037         mySelBtn [ i ]->setEnabled(false);
01038         mySelEdit[ i ]->setEnabled(false);
01039         mySelEdit[ i ]->setText("");
01040       }
01041     } else {
01042       for (int i = Object; i <= Vertex2; i++) {
01043         mySelBtn [ i ]->setEnabled(true);
01044         mySelEdit[ i ]->setEnabled(true);
01045       }
01046     }
01047 
01048     QList<int> ids;
01049     if (!CORBA::is_nil(myPattern)/* && getIds(ids)*/) {
01050       SMESH::long_array_var keyPoints = myPattern->GetKeyPoints();
01051       if (keyPoints->length()) {
01052         myNode1->setEnabled(true);
01053         myNode2->setEnabled(true);
01054         myNode1->setRange(1, keyPoints->length());
01055         myNode2->setRange(1, keyPoints->length());
01056         return;
01057       }
01058     }
01059 
01060     myNode1->setEnabled(false);
01061     myNode2->setEnabled(false);
01062     myNode1->setRange(0, 0);
01063     myNode2->setRange(0, 0);
01064   }
01065 }
01066 
01067 //=======================================================================
01068 // name    : SMESHGUI_MeshPatternDlg::activateSelection
01069 // Purpose : Activate selection in accordance with current selection input
01070 //=======================================================================
01071 void SMESHGUI_MeshPatternDlg::activateSelection()
01072 {
01073   mySelectionMgr->clearFilters();
01074   if (mySelInput == Ids) {
01075     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
01076     if (anActor)
01077       SMESH::SetPickable(anActor);
01078 
01079     if (myType == Type_2d)
01080       {
01081         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
01082           aViewWindow->SetSelectionMode(FaceSelection);
01083       }
01084     else
01085       {
01086         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
01087           aViewWindow->SetSelectionMode(CellSelection);
01088       }
01089   }
01090   else {
01091     SMESH::SetPickable();
01092     //mySelectionMgr->setSelectionModes(ActorSelection);
01093     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
01094       aViewWindow->SetSelectionMode(ActorSelection);
01095   }
01096 
01097   if (mySelInput == Object && !myMeshShape->_is_nil()) {
01098     if (myType == Type_2d) {
01099       if (myNbPoints > 0)
01100         mySelectionMgr->installFilter
01101           (new SMESH_NumberFilter ("GEOM", TopAbs_VERTEX, myNbPoints, TopAbs_FACE, myMeshShape));
01102       else
01103         mySelectionMgr->installFilter
01104           (new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, myNbPoints, TopAbs_FACE, myMeshShape));
01105     } else {
01106       TColStd_MapOfInteger aTypes;
01107       aTypes.Add(TopAbs_SHELL);
01108       aTypes.Add(TopAbs_SOLID);
01109       mySelectionMgr->installFilter
01110         (new SMESH_NumberFilter ("GEOM", TopAbs_FACE, 6, aTypes, myMeshShape, true));
01111     }
01112   } else if ((mySelInput == Vertex1 || mySelInput == Vertex2) && !myGeomObj[ Object ]->_is_nil()) {
01113     mySelectionMgr->installFilter
01114       (new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, 1, TopAbs_VERTEX, myGeomObj[ Object ]));
01115   } else {
01116   }
01117 }
01118 
01119 //=======================================================================
01120 // name    : SMESHGUI_MeshPatternDlg::loadFromFile
01121 // Purpose : Load pattern from file
01122 //=======================================================================
01123 bool SMESHGUI_MeshPatternDlg::loadFromFile (const QString& theName)
01124 {
01125   try {
01126     SMESH::SMESH_Pattern_var aPattern = SMESH::GetPattern();
01127 
01128     if (!aPattern->LoadFromFile(theName.toLatin1().data()) ||
01129         (myType == Type_2d && !aPattern->Is2D())) {
01130       SMESH::SMESH_Pattern::ErrorCode aCode = aPattern->GetErrorCode();
01131       QString aMess;
01132       if      (aCode == SMESH::SMESH_Pattern::ERR_READ_NB_POINTS     ) aMess = tr("ERR_READ_NB_POINTS");
01133       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_POINT_COORDS  ) aMess = tr("ERR_READ_POINT_COORDS");
01134       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_TOO_FEW_POINTS) aMess = tr("ERR_READ_TOO_FEW_POINTS");
01135       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_3D_COORD      ) aMess = tr("ERR_READ_3D_COORD");
01136       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_NO_KEYPOINT   ) aMess = tr("ERR_READ_NO_KEYPOINT");
01137       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_BAD_INDEX     ) aMess = tr("ERR_READ_BAD_INDEX");
01138       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_ELEM_POINTS   ) aMess = tr("ERR_READ_ELEM_POINTS");
01139       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_NO_ELEMS      ) aMess = tr("ERR_READ_NO_ELEMS");
01140       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_BAD_KEY_POINT ) aMess = tr("ERR_READ_BAD_KEY_POINT");
01141       else                                                             aMess = tr("ERROR_OF_LOADING");
01142 
01143       SUIT_MessageBox::information(this, tr("SMESH_ERROR"), aMess);
01144       return false;
01145     } else {
01146       myPattern = aPattern;
01147       return true;
01148     }
01149   } catch (const SALOME::SALOME_Exception& S_ex) {
01150     SalomeApp_Tools::QtCatchCorbaException(S_ex);
01151     SUIT_MessageBox::information(this, tr("SMESH_ERROR"),
01152                                  tr("ERROR_OF_LOADING") );
01153     return false;
01154   }
01155 }
01156 
01157 //=======================================================================
01158 // name    : SMESHGUI_MeshPatternDlg::onTypeChanged
01159 // Purpose : SLOT. Called when pattern type changed.
01160 //           Change dialog's look and feel
01161 //=======================================================================
01162 void SMESHGUI_MeshPatternDlg::onTypeChanged (int theType)
01163 {
01164   if (myType == theType)
01165     return;
01166 
01167   myType = theType;
01168 
01169   myNbPoints = -1;
01170   myGeomObj[ Object  ] = GEOM::GEOM_Object::_nil();
01171   myGeomObj[ Vertex1 ] = GEOM::GEOM_Object::_nil();
01172   myGeomObj[ Vertex2 ] = GEOM::GEOM_Object::_nil();
01173   myPattern = SMESH::GetPattern();
01174 
01175   myName->setText("");
01176   mySelEdit[ Object  ]->setText("");
01177   mySelEdit[ Vertex1 ]->setText("");
01178   mySelEdit[ Vertex2 ]->setText("");
01179   mySelEdit[ Ids ]    ->setText("");
01180   myCreatePolygonsChk->show();
01181   myCreatePolyedrsChk->show();
01182 
01183   if (theType == Type_2d) {
01184     // Geom widgets
01185     mySelLbl [ Vertex2 ]->hide();
01186     mySelBtn [ Vertex2 ]->hide();
01187     mySelEdit[ Vertex2 ]->hide();
01188     myReverseChk->show();
01189     myPicture2d->show();
01190     myPicture3d->hide();
01191     mySelLbl[ Object  ]->setText(tr("FACE"));
01192     mySelLbl[ Vertex1 ]->setText(tr("VERTEX"));
01193     // Refine widgets
01194     mySelLbl[ Ids ]->setText(tr("MESH_FACES"));
01195     myNode2Lbl->hide();
01196     myNode2   ->hide();
01197   } else {
01198     // Geom widgets
01199     mySelLbl [ Vertex2 ]->show();
01200     mySelBtn [ Vertex2 ]->show();
01201     mySelEdit[ Vertex2 ]->show();
01202     myReverseChk->hide();
01203     myPicture2d->hide();
01204     myPicture3d->show();
01205     mySelLbl[ Object  ]->setText(tr("3D_BLOCK"));
01206     mySelLbl[ Vertex1 ]->setText(tr("VERTEX1"));
01207     mySelLbl[ Vertex2 ]->setText(tr("VERTEX2"));
01208     // Refine widgets
01209     mySelLbl[ Ids ]->setText(tr("MESH_VOLUMES"));
01210     myNode2Lbl->show();
01211     myNode2   ->show();
01212   }
01213 
01214   mySelInput = Mesh;
01215   activateSelection();
01216   updateWgState();
01217   displayPreview();
01218 }
01219 
01220 //=======================================================================
01221 // name    : SMESHGUI_MeshPatternDlg::getGrid
01222 // Purpose : Get unstructured grid for pattern
01223 //=======================================================================
01224 vtkUnstructuredGrid* SMESHGUI_MeshPatternDlg::getGrid()
01225 {
01226   try {
01227     // Get points from pattern
01228     SMESH::point_array_var pnts;
01229     QList<int> ids;
01230     if (isRefine() && getIds(ids)) {
01231       SMESH::long_array_var varIds = new SMESH::long_array();
01232       varIds->length(ids.count());
01233       int i = 0;
01234       for (QList<int>::iterator it = ids.begin(); it != ids.end(); ++it)
01235         varIds[i++] = *it;
01236       pnts = myType == Type_2d
01237         ? myPattern->ApplyToMeshFaces  (myMesh, varIds, getNode(false), myReverseChk->isChecked())
01238         : myPattern->ApplyToHexahedrons(myMesh, varIds, getNode(false), getNode(true));
01239     } else {
01240       pnts = myType == Type_2d
01241         ? myPattern->ApplyToFace   (myGeomObj[ Object ], myGeomObj[ Vertex1 ], myReverseChk->isChecked())
01242       : myPattern->ApplyTo3DBlock(myGeomObj[ Object ], myGeomObj[ Vertex1 ], myGeomObj[ Vertex2 ]);
01243     }
01244 
01245     SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints(true);
01246 
01247     if (pnts->length() == 0 || elemPoints->length() == 0)
01248       return 0;
01249 
01250     // to do : to be removed /////////////////////////////////////////////
01251 
01252 #ifdef DEB_SLN
01253     for (int i1 = 0, n1 = pnts->length(); i1 < n1; i1++)
01254       printf("%d: %g %g %g\n", i1, pnts[ i1 ].x, pnts[ i1 ].y, pnts[ i1 ].z);
01255 
01256     printf("\nELEMENTS : \n");
01257     for (int i2 = 0, n2 = elemPoints->length(); i2 < n2; i2++)
01258     {
01259 
01260       printf("%d: ", i2);
01261       for (int i3 = 0, n3 = elemPoints[ i2 ].length(); i3 < n3; i3++)
01262         printf("%d ", elemPoints[ i2 ][ i3 ]);
01263 
01264       printf("\n");
01265 
01266     }
01267 #endif
01268 
01269 
01270     // Calculate number of points used for cell
01271     vtkIdType aNbCells = elemPoints->length();
01272     vtkIdType aCellsSize = 0;
01273     for (int i = 0, n = elemPoints->length(); i < n; i++)
01274       aCellsSize += elemPoints[ i ].length();
01275 
01276     // Create unstructured grid and other  usefull arrays
01277     vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
01278 
01279     vtkCellArray* aConnectivity = vtkCellArray::New();
01280     aConnectivity->Allocate(aCellsSize, 0);
01281 
01282     vtkPoints* aPoints = vtkPoints::New();
01283     aPoints->SetNumberOfPoints(pnts->length());
01284 
01285     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
01286     aCellTypesArray->SetNumberOfComponents(1);
01287     aCellTypesArray->Allocate(aNbCells * aCellTypesArray->GetNumberOfComponents());
01288 
01289     vtkIdList *anIdList = vtkIdList::New();
01290 
01291     // Fill array of points
01292     for (int p = 0, nbPnt = pnts->length(); p < nbPnt; p++)
01293       aPoints->SetPoint(p, pnts[ p ].x, pnts[ p ].y, pnts[ p ].z);
01294 
01295     for (int e = 0, nbElem = elemPoints->length(); e < nbElem; e++) {
01296       int nbPoints = elemPoints[ e ].length();
01297       anIdList->SetNumberOfIds(nbPoints);
01298       for (int i = 0; i < nbPoints; i++)
01299         anIdList->SetId(i, elemPoints[ e ][ i ]);
01300 
01301       aConnectivity->InsertNextCell(anIdList);
01302 
01303       if      (nbPoints == 3) aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
01304       else if (nbPoints == 5) aCellTypesArray->InsertNextValue(VTK_PYRAMID);
01305       else if (nbPoints == 6) aCellTypesArray->InsertNextValue(VTK_WEDGE);
01306       else if (nbPoints == 8) aCellTypesArray->InsertNextValue(VTK_HEXAHEDRON);
01307       else if (nbPoints == 4 && myType == Type_2d) aCellTypesArray->InsertNextValue(VTK_QUAD);
01308       else if (nbPoints == 4 && myType == Type_3d) aCellTypesArray->InsertNextValue(VTK_TETRA);
01309       else aCellTypesArray->InsertNextValue(VTK_EMPTY_CELL);
01310     }
01311 
01312     VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
01313     aCellLocationsArray->SetNumberOfComponents(1);
01314     aCellLocationsArray->SetNumberOfTuples(aNbCells);
01315 
01316     aConnectivity->InitTraversal();
01317     for (vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell(npts, pts); idType++)
01318       aCellLocationsArray->SetValue(idType, aConnectivity->GetTraversalLocation(npts));
01319 
01320     aGrid->SetPoints(aPoints);
01321     aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aConnectivity);
01322 
01323     aConnectivity->Delete();
01324     aPoints->Delete();
01325     aCellTypesArray->Delete();
01326     anIdList->Delete();
01327     aCellLocationsArray->Delete();
01328 
01329     return aGrid;
01330   } catch (...) {
01331     return 0;
01332   }
01333 }
01334 
01335 //=======================================================================
01336 // name    : onModeToggled
01337 // Purpose :
01338 //=======================================================================
01339 void SMESHGUI_MeshPatternDlg::onModeToggled (bool on)
01340 {
01341   on ? myRefineGrp->show() : myRefineGrp->hide();
01342   on ? myGeomGrp->hide()   : myGeomGrp->show();
01343 
01344   displayPreview();
01345 }
01346 
01347 //=======================================================================
01348 // name    : isRefine
01349 // Purpose :
01350 //=======================================================================
01351 bool SMESHGUI_MeshPatternDlg::isRefine() const
01352 {
01353   return myRefine->isChecked();
01354 }
01355 
01356 //=======================================================================
01357 // name    : onTextChanged
01358 // Purpose :
01359 //=======================================================================
01360 void SMESHGUI_MeshPatternDlg::onTextChanged (const QString& theNewText)
01361 {
01362   if (myBusy || !isRefine())
01363     return;
01364 
01365   myBusy = true;
01366 
01367   if (mySelInput != Ids) {
01368     mySelInput = Ids;
01369     activateSelection();
01370   }
01371 
01372   // hilight entered elements/nodes
01373   SMDS_Mesh* aMesh = 0;
01374   SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
01375   if (anActor)
01376     aMesh = anActor->GetObject()->GetMesh();
01377 
01378   if (aMesh) {
01379     QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
01380 
01381     TColStd_MapOfInteger newIndices;
01382 
01383     for (int i = 0; i < aListId.count(); i++) {
01384       const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt());
01385       if (e && e->GetType() == (myType == Type_2d ? SMDSAbs_Face : SMDSAbs_Volume))
01386         newIndices.Add(e->GetID());
01387     }
01388     mySelector->AddOrRemoveIndex( anActor->getIO(), newIndices, false);
01389     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
01390       aViewWindow->highlight( anActor->getIO(), true, true );
01391   }
01392 
01393   myBusy = false;
01394 
01395   displayPreview();
01396 }
01397 
01398 //=======================================================================
01399 // name    : onNodeChanged
01400 // Purpose :
01401 //=======================================================================
01402 void SMESHGUI_MeshPatternDlg::onNodeChanged (int value)
01403 {
01404   if (myType == Type_3d) {
01405     SalomeApp_IntSpinBox* first = (SalomeApp_IntSpinBox*)sender();
01406     SalomeApp_IntSpinBox* second = first == myNode1 ? myNode2 : myNode1;
01407     int secondVal = second->value();
01408     if (secondVal == value) {
01409       secondVal = value == second->maximum() ? second->minimum() : value + 1;
01410       bool blocked = second->signalsBlocked();
01411       second->blockSignals(true);
01412       second->setValue(secondVal);
01413       second->blockSignals(blocked);
01414     }
01415   }
01416 
01417   displayPreview();
01418 }
01419 
01420 //=======================================================================
01421 // name    : getIds
01422 // Purpose :
01423 //=======================================================================
01424 bool SMESHGUI_MeshPatternDlg::getIds (QList<int>& ids) const
01425 {
01426   ids.clear();
01427   QStringList strIds = mySelEdit[Ids]->text().split(" ", QString::SkipEmptyParts );
01428   bool isOk;
01429   int val;
01430   for (QStringList::iterator it = strIds.begin(); it != strIds.end(); ++it) {
01431     val = (*it).toInt(&isOk);
01432     if (isOk)
01433       ids.append(val);
01434   }
01435 
01436   return ids.count();
01437 }
01438 
01439 //=======================================================================
01440 // name    : getNode1
01441 // Purpose :
01442 //=======================================================================
01443 int SMESHGUI_MeshPatternDlg::getNode (bool second) const
01444 {
01445   return second ? myNode2->value() - 1 : myNode1->value() - 1;
01446 }
01447 
01448 //=================================================================================
01449 // function : keyPressEvent()
01450 // purpose  :
01451 //=================================================================================
01452 void SMESHGUI_MeshPatternDlg::keyPressEvent( QKeyEvent* e )
01453 {
01454   QDialog::keyPressEvent( e );
01455   if ( e->isAccepted() )
01456     return;
01457 
01458   if ( e->key() == Qt::Key_F1 )
01459     {
01460       e->accept();
01461       onHelp();
01462     }
01463 }