]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHGUI/SMESHGUI_MeshPatternDlg.cxx
Salome HOME
bdb3a8fd795f4a9cade4dfb5804520abc6c1e01a
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshPatternDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component\r
2 //\r
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,\r
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS \r
5 // \r
6 //  This library is free software; you can redistribute it and/or \r
7 //  modify it under the terms of the GNU Lesser General Public \r
8 //  License as published by the Free Software Foundation; either \r
9 //  version 2.1 of the License. \r
10 // \r
11 //  This library is distributed in the hope that it will be useful, \r
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of \r
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU \r
14 //  Lesser General Public License for more details. \r
15 // \r
16 //  You should have received a copy of the GNU Lesser General Public \r
17 //  License along with this library; if not, write to the Free Software \r
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA \r
19 // \r
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org \r
21 //\r
22 //\r
23 //\r
24 //  File   : SMESHGUI_MeshPatternDlg.cxx\r
25 //  Author : Sergey LITONIN\r
26 //  Module : SMESH\r
27 \r
28 #include "SMESHGUI_MeshPatternDlg.h"\r
29 \r
30 #include "QAD_Desktop.h"\r
31 #include "QAD_FileDlg.h"\r
32 \r
33 #include "SMESHGUI_SpinBox.h"\r
34 #include "SMESHGUI.h"\r
35 #include "SALOME_Selection.h"\r
36 #include "SMESH_NumberFilter.hxx"\r
37 #include "SMESHGUI_Utils.h"\r
38 #include "SMESHGUI_VTKUtils.h"\r
39 #include "SMESHGUI_CreatePatternDlg.h"\r
40 #include "SMESHGUI_PatternWidget.h"\r
41 #include "SMESH_Actor.h"\r
42 #include "SALOMEGUI_QtCatchCorbaException.hxx"\r
43 #include "VTKViewer_ViewFrame.h"\r
44 #include "SMESHGUI_PatternUtils.h"\r
45 #include "SMESH_ActorUtils.h"\r
46 \r
47 #include <TColStd_MapOfInteger.hxx>\r
48 \r
49 #include <qframe.h>\r
50 #include <qlayout.h>\r
51 #include <qlineedit.h>\r
52 #include <qpushbutton.h>\r
53 #include <qgroupbox.h>\r
54 #include <qlabel.h>\r
55 #include <qradiobutton.h>\r
56 #include <qcheckbox.h>\r
57 #include <qbuttongroup.h>\r
58 #include <qmessagebox.h>\r
59 #include <qcstring.h>\r
60 \r
61 #include <vtkCell.h>\r
62 #include <vtkIdList.h>\r
63 #include <vtkIntArray.h>\r
64 #include <vtkCellArray.h>\r
65 #include <vtkUnsignedCharArray.h>\r
66 #include <vtkUnstructuredGrid.h>\r
67 #include <vtkDataSetMapper.h>\r
68 \r
69 #define SPACING 5\r
70 #define MARGIN  10\r
71 \r
72 /*\r
73   Class       : SMESHGUI_MeshPatternDlg\r
74   Description : Dialog to specify filters for VTK viewer\r
75 */\r
76 \r
77 //=======================================================================\r
78 // name    : SMESHGUI_MeshPatternDlg::SMESHGUI_MeshPatternDlg\r
79 // Purpose : Constructor\r
80 //=======================================================================\r
81 SMESHGUI_MeshPatternDlg::SMESHGUI_MeshPatternDlg( QWidget*          theParent,\r
82                                             SALOME_Selection* theSelection,\r
83                                             const char*       theName )\r
84 : QDialog( theParent, theName, false,\r
85            WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )\r
86 {\r
87   setCaption( tr( "CAPTION" ) );\r
88 \r
89   QVBoxLayout* aDlgLay = new QVBoxLayout( this, MARGIN, SPACING );\r
90 \r
91   QFrame* aMainFrame = createMainFrame  ( this );\r
92   QFrame* aBtnFrame  = createButtonFrame( this );\r
93 \r
94 \r
95 \r
96 \r
97   aDlgLay->addWidget( aMainFrame );\r
98   aDlgLay->addWidget( aBtnFrame );\r
99 \r
100   aDlgLay->setStretchFactor( aMainFrame, 1 );\r
101 \r
102   myCreationDlg = 0;\r
103   Init( theSelection );\r
104 }\r
105 \r
106 //=======================================================================\r
107 // name    : SMESHGUI_MeshPatternDlg::createMainFrame\r
108 // Purpose : Create frame containing dialog's input fields\r
109 //=======================================================================\r
110 QFrame* SMESHGUI_MeshPatternDlg::createMainFrame( QWidget* theParent )\r
111 {\r
112   QPixmap iconSlct( QAD_Desktop::getResourceManager()->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );\r
113   QPixmap icon2d  ( QAD_Desktop::getResourceManager()->loadPixmap( "SMESH", tr( "ICON_PATTERN_2d" ) ) );\r
114   QPixmap icon3d  ( QAD_Desktop::getResourceManager()->loadPixmap( "SMESH", tr( "ICON_PATTERN_3d" ) ) );\r
115   QPixmap iconOpen( QAD_Desktop::getResourceManager()->loadPixmap( "SMESH", tr( "ICON_FILE_OPEN" ) ) );\r
116 \r
117   QPixmap iconSample2d( QAD_Desktop::getResourceManager()->loadPixmap( "SMESH", tr( "ICON_PATTERN_SAMPLE_2D" ) ) );\r
118   QPixmap iconSample3d( QAD_Desktop::getResourceManager()->loadPixmap( "SMESH", tr( "ICON_PATTERN_SAMPLE_3D" ) ) );\r
119 \r
120   QGroupBox* aMainGrp = new QGroupBox( 1, Qt::Horizontal, theParent );\r
121   aMainGrp->setFrameStyle( QFrame::NoFrame );\r
122   aMainGrp->setInsideMargin( 0 );\r
123 \r
124   // Pattern type group\r
125   \r
126   myTypeGrp = new QButtonGroup( 1, Qt::Vertical, tr( "PATTERN_TYPE" ), aMainGrp );\r
127   mySwitch2d = new QRadioButton( myTypeGrp );\r
128   mySwitch3d = new QRadioButton( myTypeGrp );\r
129   mySwitch2d->setPixmap( icon2d );\r
130   mySwitch3d->setPixmap( icon3d );\r
131   myTypeGrp->insert( mySwitch2d, Type_2d );\r
132   myTypeGrp->insert( mySwitch3d, Type_3d );\r
133 \r
134 \r
135   // Mesh group\r
136   \r
137   QGroupBox* aMeshGrp = new QGroupBox( 1, Qt::Vertical, tr( "SMESH_MESH" ), aMainGrp );\r
138   new QLabel( tr( "SMESH_MESH" ), aMeshGrp );\r
139   mySelBtn[ Mesh ] = new QPushButton( aMeshGrp );\r
140   mySelBtn[ Mesh ]->setPixmap( iconSlct );\r
141   mySelEdit[ Mesh ] = new QLineEdit( aMeshGrp );\r
142   mySelEdit[ Mesh ]->setReadOnly( true );\r
143 \r
144   // Pattern group\r
145   \r
146   QGroupBox* aPatGrp = new QGroupBox( 1, Qt::Horizontal, tr( "PATTERN" ), aMainGrp );\r
147 \r
148   // pattern name\r
149   QGroupBox* aNameGrp = new QGroupBox( 1, Qt::Vertical, aPatGrp );\r
150   aNameGrp->setFrameStyle( QFrame::NoFrame );\r
151   aNameGrp->setInsideMargin( 0 );\r
152   new QLabel( tr( "PATTERN" ), aNameGrp );\r
153   myName = new QLineEdit( aNameGrp );\r
154   myName->setReadOnly( true );\r
155   myOpenBtn = new QPushButton( aNameGrp );\r
156   myOpenBtn->setPixmap( iconOpen );\r
157   myNewBtn = new QPushButton( tr( "NEW" ), aNameGrp );\r
158 \r
159   // selection widgets\r
160   QGroupBox* aGrp = new QGroupBox( 3, Qt::Horizontal, aPatGrp );\r
161   aGrp->setFrameStyle( QFrame::NoFrame );\r
162   aGrp->setInsideMargin( 0 );\r
163 \r
164   for ( int i = Object; i <= Vertex2; i++ )\r
165   {\r
166     mySelLbl[ i ] = new QLabel( aGrp );\r
167     mySelBtn[ i ] = new QPushButton( aGrp );\r
168     mySelBtn[ i ]->setPixmap( iconSlct );\r
169     mySelEdit[ i ] = new QLineEdit( aGrp );\r
170     mySelEdit[ i ]->setReadOnly( true );\r
171   }\r
172 \r
173   // reverse check box\r
174   myReverseChk = new QCheckBox( tr( "REVERSE" ), aPatGrp );\r
175 \r
176   // Pictures 2d and 3d\r
177   for ( int i = 0; i < 2; i++ )\r
178   {\r
179     if ( i == 0 )\r
180     {\r
181       myPicture2d = new SMESHGUI_PatternWidget( aPatGrp ),\r
182       myPicture2d->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );\r
183     }\r
184     else\r
185     {\r
186       myPicture3d = new QFrame( aPatGrp ),\r
187       myPreview3d = new QLabel( myPicture3d );\r
188       myPreview3d->setPixmap( iconSample3d );\r
189       QGridLayout* aLay = new QGridLayout( myPicture3d, 3, 3, 0, 0 );\r
190       QSpacerItem* aSpacerH1 = new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum );\r
191       QSpacerItem* aSpacerH2 = new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum );\r
192       QSpacerItem* aSpacerV1 = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );\r
193       QSpacerItem* aSpacerV2 = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );\r
194       aLay->addItem( aSpacerH1, 1, 0 );\r
195       aLay->addItem( aSpacerH2, 1, 2 );\r
196       aLay->addItem( aSpacerV1, 0, 1 );\r
197       aLay->addItem( aSpacerV2, 2, 1 );\r
198       aLay->addWidget( myPreview3d, 1, 1 );\r
199     }\r
200   }\r
201 \r
202   myPreviewChk = new QCheckBox( tr( "PREVIEW" ), aPatGrp );\r
203 \r
204   // Connect signals and slots\r
205 \r
206   connect( myTypeGrp,    SIGNAL( clicked( int )  ), SLOT( onTypeChanged( int ) ) );\r
207   connect( myOpenBtn,    SIGNAL( clicked()       ), SLOT( onOpen()             ) );\r
208   connect( myNewBtn,     SIGNAL( clicked()       ), SLOT( onNew()              ) );\r
209   connect( myReverseChk, SIGNAL( toggled( bool ) ), SLOT( onReverse( bool )    ) );\r
210   connect( myPreviewChk, SIGNAL( toggled( bool ) ), SLOT( onPreview( bool )    ) );\r
211 \r
212   QMap< int, QPushButton* >::iterator anIter;\r
213   for ( anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter )\r
214     connect( *anIter, SIGNAL( clicked() ), SLOT( onSelInputChanged() ) );\r
215 \r
216   return aMainGrp;\r
217 }\r
218 \r
219 //=======================================================================\r
220 \r
221 // name    : SMESHGUI_MeshPatternDlg::createButtonFrame\r
222 // Purpose : Create frame containing buttons\r
223 //=======================================================================\r
224 QFrame* SMESHGUI_MeshPatternDlg::createButtonFrame( QWidget* theParent )\r
225 {\r
226   QFrame* aFrame = new QFrame( theParent );\r
227   aFrame->setFrameStyle( QFrame::Box | QFrame::Sunken );\r
228 \r
229   myOkBtn     = new QPushButton( tr( "SMESH_BUT_OK"    ), aFrame );\r
230   myApplyBtn  = new QPushButton( tr( "SMESH_BUT_APPLY" ), aFrame );\r
231   myCloseBtn  = new QPushButton( tr( "SMESH_BUT_CLOSE" ), aFrame );\r
232 \r
233   QSpacerItem* aSpacer = new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum );\r
234 \r
235   QHBoxLayout* aLay = new QHBoxLayout( aFrame, MARGIN, SPACING );\r
236 \r
237   aLay->addWidget( myOkBtn );\r
238   aLay->addWidget( myApplyBtn );\r
239   aLay->addItem( aSpacer);\r
240   aLay->addWidget( myCloseBtn );\r
241 \r
242   connect( myOkBtn,    SIGNAL( clicked() ), SLOT( onOk() ) );\r
243   connect( myCloseBtn, SIGNAL( clicked() ), SLOT( onClose() ) ) ;\r
244   connect( myApplyBtn, SIGNAL( clicked() ), SLOT( onApply() ) );\r
245 \r
246   return aFrame;\r
247 }\r
248 \r
249 //=======================================================================\r
250 // name    : SMESHGUI_MeshPatternDlg::~SMESHGUI_MeshPatternDlg\r
251 // Purpose : Destructor\r
252 //=======================================================================\r
253 SMESHGUI_MeshPatternDlg::~SMESHGUI_MeshPatternDlg()\r
254 {\r
255 }\r
256 \r
257 //=======================================================================\r
258 // name    : SMESHGUI_MeshPatternDlg::Init\r
259 // Purpose : Init dialog fields, connect signals and slots, show dialog\r
260 //=======================================================================\r
261 void SMESHGUI_MeshPatternDlg::Init( SALOME_Selection* theSelection )\r
262 {\r
263   myPattern = SMESH::GetPattern();\r
264   myPreviewActor = 0;\r
265   myIsCreateDlgOpen = false;\r
266   mySelInput = Mesh;\r
267   myType = -1;\r
268   myNbPoints = -1;\r
269   mySelection = theSelection;\r
270   SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI();\r
271   aSMESHGUI->SetActiveDialogBox( ( QDialog* )this );\r
272   myMesh = SMESH::SMESH_Mesh::_nil();\r
273 \r
274   myMeshShape = GEOM::GEOM_Object::_nil();\r
275   myGeomObj[ Object  ] = GEOM::GEOM_Object::_nil();\r
276   myGeomObj[ Vertex1 ] = GEOM::GEOM_Object::_nil();\r
277   myGeomObj[ Vertex2 ] = GEOM::GEOM_Object::_nil();\r
278 \r
279   // selection and SMESHGUI\r
280   connect( mySelection, SIGNAL( currentSelectionChanged() ), SLOT( onSelectionDone() ) );\r
281   connect( aSMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT( onDeactivate() ) );\r
282   connect( aSMESHGUI, SIGNAL( SignalCloseAllDialogs() ), SLOT( onClose() ) );\r
283 \r
284   myTypeGrp->setButton( Type_2d );\r
285   onTypeChanged( Type_2d );\r
286 \r
287   updateGeometry();\r
288 \r
289   resize( minimumSize() );\r
290 \r
291   activateSelection();\r
292   onSelectionDone();\r
293 \r
294   int x, y ;\r
295   aSMESHGUI->DefineDlgPosition( this, x, y );\r
296   this->move( x, y );\r
297   this->show();\r
298 }\r
299 \r
300 //=======================================================================\r
301 // name    : SMESHGUI_MeshPatternDlg::isValid\r
302 // Purpose : Verify validity of entry data\r
303 //=======================================================================\r
304 bool SMESHGUI_MeshPatternDlg::isValid( const bool theMess )\r
305 {\r
306   if ( myMesh->_is_nil() || myMeshShape->_is_nil() || myGeomObj[ Object ]->_is_nil() ||\r
307        myGeomObj[ Vertex1 ]->_is_nil() || myType == Type_3d && myGeomObj[ Vertex2 ]->_is_nil() )\r
308   {\r
309     if ( theMess )\r
310       QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
311         tr( "SMESH_INSUFFICIENT_DATA" ), tr( "SMESHGUI_INVALID_PARAMETERS" ), QMessageBox::Ok );\r
312     return false;\r
313   }\r
314   else\r
315     return true;\r
316 }\r
317 \r
318 //=======================================================================\r
319 // name    : SMESHGUI_MeshPatternDlg::onApply\r
320 // Purpose : SLOT called when "Apply" button pressed.\r
321 //=======================================================================\r
322 bool SMESHGUI_MeshPatternDlg::onApply()\r
323 {\r
324   try\r
325   {\r
326     if ( !isValid() )\r
327       return false;\r
328 \r
329     if ( myType == Type_2d )\r
330       myPattern->ApplyToFace(\r
331         myGeomObj[ Object ], myGeomObj[ Vertex1 ], myReverseChk->isChecked() );\r
332     else\r
333       myPattern->ApplyTo3DBlock(\r
334         myGeomObj[ Object ], myGeomObj[ Vertex1 ], myGeomObj[ Vertex2 ] );\r
335 \r
336     if ( myPattern->MakeMesh( myMesh ) )\r
337     {\r
338       mySelection->ClearIObjects();\r
339       SMESH::UpdateView();\r
340       SMESHGUI::GetSMESHGUI()->GetActiveStudy()->updateObjBrowser( true );\r
341       return true;\r
342     }\r
343     else\r
344     {\r
345       QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
346         tr( "SMESH_ERROR" ), tr( "SMESH_OPERATION_FAILED" ), QMessageBox::Ok );\r
347       return false;\r
348     }\r
349   }\r
350   catch( const SALOME::SALOME_Exception& S_ex )\r
351   {\r
352     QtCatchCorbaException( S_ex );    \r
353   }\r
354   catch( ... )\r
355   {\r
356   }\r
357 \r
358   return false;\r
359 }\r
360 \r
361 //=======================================================================\r
362 // name    : SMESHGUI_MeshPatternDlg::onOk\r
363 // Purpose : SLOT called when "Ok" button pressed.\r
364 //=======================================================================\r
365 void SMESHGUI_MeshPatternDlg::onOk()\r
366 {\r
367   if ( onApply() )\r
368     onClose();\r
369 }\r
370 \r
371 //=======================================================================\r
372 // name    : SMESHGUI_MeshPatternDlg::onClose\r
373 // Purpose : SLOT called when "Close" button pressed. Close dialog\r
374 //=======================================================================\r
375 void SMESHGUI_MeshPatternDlg::onClose()\r
376 {\r
377   mySelection->ClearFilters();\r
378   QAD_Application::getDesktop()->SetSelectionMode( ActorSelection );\r
379   disconnect( mySelection, 0, this, 0 );\r
380   disconnect( SMESHGUI::GetSMESHGUI(), 0, this, 0 );\r
381   SMESHGUI::GetSMESHGUI()->ResetState();\r
382   erasePreview();\r
383   reject();\r
384 }\r
385 \r
386 \r
387 \r
388 //=======================================================================\r
389 // name    : SMESHGUI_MeshPatternDlg::onSelectionDone\r
390 // Purpose : SLOT called when selection changed\r
391 //=======================================================================\r
392 void SMESHGUI_MeshPatternDlg::onSelectionDone()\r
393 {\r
394   try\r
395   {\r
396     if ( mySelection->IObjectCount() != 1 )\r
397       return;\r
398 \r
399     if ( mySelInput == Mesh )\r
400     {\r
401       // Retrieve mesh from selection\r
402       Handle(SALOME_InteractiveObject) anIO = mySelection->firstIObject();\r
403       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>( anIO );\r
404       if ( aMesh->_is_nil() )\r
405         return;\r
406 \r
407       // Get geom object corresponding to the mesh\r
408       SALOMEDS::Study_var aStudy =\r
409         SMESHGUI::GetSMESHGUI()->GetActiveStudy()->getStudyDocument();\r
410 \r
411       SALOMEDS::SObject_var aSO = SMESH::FindSObject( aMesh.in() );\r
412       if ( aSO->_is_nil() )\r
413         return;\r
414 \r
415       bool isFound = false;\r
416       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( aSO );\r
417       for( ; anIter->More(); anIter->Next() )\r
418       {\r
419         SALOMEDS::SObject_var aSO = anIter->Value();\r
420         SALOMEDS::SObject_var aRefSO;\r
421 \r
422         GEOM::GEOM_Object_var aMeshShape = GEOM::GEOM_Object::_narrow(\r
423           aSO->ReferencedObject( aRefSO )? aRefSO->GetObject() : aSO->GetObject() );\r
424 \r
425         if ( !aMeshShape->_is_nil() )\r
426         {\r
427           isFound = true;\r
428           myMeshShape = aMeshShape;\r
429           break;\r
430 \r
431         }\r
432       }\r
433 \r
434       if ( !isFound )\r
435         return;\r
436 \r
437       // Clear fields of geom objects if mesh was changed\r
438       if ( myMesh != aMesh )\r
439       {\r
440         for ( int i = Object; i <= Vertex2; i++ )\r
441         {\r
442           myGeomObj[ i ] = GEOM::GEOM_Object::_nil();\r
443           mySelEdit[ i ]->setText( "" );\r
444         }\r
445       }\r
446 \r
447       myMesh = aMesh;\r
448 \r
449       // Set name of mesh in line edit\r
450       QString aName;\r
451       SMESH::GetNameOfSelectedIObjects( mySelection, aName );\r
452       mySelEdit[ Mesh ]->setText( aName );\r
453     }\r
454     else\r
455     {\r
456       // Get geom object from selection\r
457       Handle(SALOME_InteractiveObject) anIO = mySelection->firstIObject();\r
458       GEOM::GEOM_Object_var anObj = SMESH::IObjectToInterface<GEOM::GEOM_Object>( anIO );\r
459       if ( anObj->_is_nil() )\r
460         return;\r
461 \r
462       // Clear fields of vertexes if face or 3d block was changed\r
463       if ( anObj != myGeomObj[ mySelInput ] && mySelInput == Object )\r
464       {\r
465         for ( int i = Vertex1; i <= Vertex2; i++ )\r
466         {\r
467           myGeomObj[ i ] = GEOM::GEOM_Object::_nil();\r
468           mySelEdit[ i ]->setText( "" );\r
469         }\r
470       }\r
471 \r
472       myGeomObj[ mySelInput ] = anObj;\r
473 \r
474       // Set name of geom object in line edit\r
475       QString aName;\r
476       SMESH::GetNameOfSelectedIObjects( mySelection, aName );\r
477       mySelEdit[ mySelInput ]->setText( aName );\r
478     }\r
479   }\r
480   catch( const SALOME::SALOME_Exception& S_ex )\r
481   {\r
482     QtCatchCorbaException( S_ex );\r
483     resetSelInput();\r
484   }\r
485   catch( ... )\r
486   {\r
487     resetSelInput();\r
488   }\r
489 \r
490   updateWgState();\r
491   displayPreview();\r
492 }\r
493 \r
494 //=======================================================================\r
495 // name    : SMESHGUI_MeshPatternDlg::resetSelInput\r
496 // Purpose : Reset fields corresponding to the current selection input\r
497 //=======================================================================\r
498 void SMESHGUI_MeshPatternDlg::resetSelInput()\r
499 {\r
500   if ( mySelInput == Mesh )\r
501   {\r
502     myMesh = SMESH::SMESH_Mesh::_nil();\r
503     myMeshShape = GEOM::GEOM_Object::_nil();\r
504   }\r
505 \r
506   else\r
507     myGeomObj[ mySelInput ] = GEOM::GEOM_Object::_nil();\r
508 \r
509   mySelEdit[ mySelInput ]->setText( "" );\r
510 }\r
511 \r
512 //=======================================================================\r
513 // name    : SMESHGUI_MeshPatternDlg::onDeactivate\r
514 // Purpose : SLOT called when dialog must be deativated\r
515 //=======================================================================\r
516 void SMESHGUI_MeshPatternDlg::onDeactivate()\r
517 {\r
518   mySelection->ClearFilters();\r
519   //if ( myReverseChk->isChecked() )\r
520   //  erasePreview();\r
521   disconnect( mySelection, 0, this, 0 );\r
522   setEnabled( false );\r
523 }\r
524 \r
525 //=======================================================================\r
526 // name    : SMESHGUI_MeshPatternDlg::enterEvent\r
527 // Purpose : Event filter\r
528 //=======================================================================\r
529 void SMESHGUI_MeshPatternDlg::enterEvent( QEvent* )\r
530 {\r
531   if ( myIsCreateDlgOpen )\r
532     return;\r
533     \r
534   if ( myReverseChk->isChecked() )\r
535     displayPreview();\r
536   SMESHGUI::GetSMESHGUI()->EmitSignalDeactivateDialog() ;\r
537   setEnabled( true );\r
538   activateSelection();\r
539   connect( mySelection, SIGNAL( currentSelectionChanged() ), SLOT( onSelectionDone() ) );\r
540 }\r
541 \r
542 \r
543 //=================================================================================\r
544 // function : closeEvent()\r
545 // purpose  :\r
546 //=================================================================================\r
547 void SMESHGUI_MeshPatternDlg::closeEvent( QCloseEvent* e )\r
548 {\r
549   onClose() ;\r
550 }\r
551 \r
552 //=======================================================================\r
553 // name    : SMESHGUI_MeshPatternDlg::onSelInputChanged\r
554 // Purpose : SLOT. Called when -> button clicked.\r
555 //           Change current selection input field\r
556 //=======================================================================\r
557 void SMESHGUI_MeshPatternDlg::onSelInputChanged()\r
558 {\r
559   const QObject* aSender = sender();\r
560   for ( int i = Mesh; i <= Vertex2; i++ )\r
561     if ( aSender == mySelBtn[ i ] )\r
562       mySelInput = i;\r
563 \r
564   activateSelection();\r
565   onSelectionDone();\r
566 }\r
567 \r
568 //=======================================================================\r
569 // name    : SMESHGUI_MeshPatternDlg::prepareFilters\r
570 // Purpose : Prepare filters for dialog\r
571 //=======================================================================\r
572 \r
573 QStringList SMESHGUI_MeshPatternDlg::prepareFilters() const\r
574 {\r
575   static QStringList aList;\r
576   if ( aList.isEmpty() )\r
577   {\r
578     aList.append( tr( "PATTERN_FILT" ) );\r
579     //aList.append( tr( "ALL_FILES_FILTER" ) );\r
580   }\r
581 \r
582   return aList;\r
583 }\r
584 \r
585 //================================================================\r
586 \r
587 // Function : SMESHGUI_MeshPatternDlg::autoExtension\r
588 // Purpose  : Append extension to the file name\r
589 //================================================================\r
590 QString SMESHGUI_MeshPatternDlg::autoExtension( const QString& theFileName ) const\r
591 {\r
592   QString anExt = theFileName.section( '.', -1 );\r
593   return anExt != "smp" && anExt != "SMP" ? theFileName + ".smp" : theFileName;\r
594 }\r
595 \r
596 //=======================================================================\r
597 // name    : SMESHGUI_MeshPatternDlg::onOpen\r
598 // Purpose : SLOT. Called when "Open" button clicked.\r
599 //           Displays file open dialog\r
600 //=======================================================================\r
601 void SMESHGUI_MeshPatternDlg::onOpen()\r
602 {\r
603   QAD_FileDlg* aDlg = new QAD_FileDlg( this, true );\r
604   aDlg->setCaption( tr( "LOAD_PATTERN" ) );\r
605   aDlg->setMode( QFileDialogP::ExistingFile );\r
606   aDlg->setFilters( prepareFilters() );\r
607   if ( myName->text() != "" )\r
608     aDlg->setSelection( myName->text() + ".smp" );\r
609   QPushButton* anOkBtn = ( QPushButton* )aDlg->child( "OK", "QPushButton" );\r
610   if ( anOkBtn != 0 )\r
611     anOkBtn->setText( tr( "SMESH_BUT_OK" ) );\r
612 \r
613   if ( aDlg->exec() != Accepted )\r
614     return;\r
615 \r
616   QString fName = aDlg->selectedFile();\r
617   if ( fName.isEmpty() )\r
618     return;\r
619 \r
620   if ( QFileInfo( fName ).extension().isEmpty() )\r
621     fName = autoExtension( fName );\r
622 \r
623   fName = QDir::convertSeparators( fName );\r
624   \r
625   QString prev = QDir::convertSeparators( myName->text() );\r
626   if ( prev == fName )\r
627     return;\r
628 \r
629   // Read string from file\r
630   QFile aFile( fName );\r
631   if ( !aFile.open( IO_ReadOnly ) )\r
632   {\r
633     QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
634       tr( "SMESH_ERROR" ), tr( "ERROR_OF_OPENING" ), QMessageBox::Ok );\r
635     return;\r
636   }\r
637   \r
638   QByteArray aDataArray = aFile.readAll();\r
639   const char* aData = aDataArray.data();\r
640   if ( aData == 0 )\r
641   {\r
642     QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
643       tr( "SMESH_ERROR" ), tr( "ERROR_OF_READING" ), QMessageBox::Ok );\r
644     return;\r
645   }\r
646   \r
647   if ( loadFromFile( aData ) )\r
648     myName->setText( QFileInfo( fName ).baseName() );\r
649     \r
650   updateWgState();\r
651   displayPreview();\r
652 }\r
653 \r
654 //=======================================================================\r
655 // name    : SMESHGUI_MeshPatternDlg::onCloseCreationDlg\r
656 // Purpose : SLOT. Called when "Pattern creation" dialog closed with "Close"\r
657 //=======================================================================\r
658 void SMESHGUI_MeshPatternDlg::onCloseCreationDlg()\r
659 {\r
660   setEnabled( true );\r
661   myIsCreateDlgOpen = false;\r
662 }\r
663 \r
664 \r
665 //=======================================================================\r
666 // name    : SMESHGUI_MeshPatternDlg::onOkCreationDlg\r
667 // Purpose : SLOT. Called when "Pattern creation" dialog closed with OK\r
668 //           or SAVE buttons. Initialize myPattern field. Redisplay preview\r
669 //=======================================================================\r
670 void SMESHGUI_MeshPatternDlg::onOkCreationDlg()\r
671 {\r
672   myPattern = SMESH::SMESH_Pattern::_duplicate( myCreationDlg->GetPattern() );\r
673   myName->setText( myCreationDlg->GetPatternName() );\r
674   displayPreview();\r
675   setEnabled( true );\r
676   myIsCreateDlgOpen = false;\r
677 }\r
678 \r
679 \r
680 //=======================================================================\r
681 // name    : SMESHGUI_MeshPatternDlg::onNew\r
682 // Purpose : SLOT. Called when "New..." button clicked. Create new pattern\r
683 //=======================================================================\r
684 void SMESHGUI_MeshPatternDlg::onNew()\r
685 {\r
686   setEnabled( false );\r
687   myIsCreateDlgOpen = true;\r
688   if ( myCreationDlg == 0 )\r
689   {\r
690     myCreationDlg = new SMESHGUI_CreatePatternDlg( this, mySelection, myType );\r
691     connect( myCreationDlg, SIGNAL( NewPattern() ), SLOT( onOkCreationDlg() ) );\r
692     connect( myCreationDlg, SIGNAL( Close() ), SLOT( onCloseCreationDlg() ) );\r
693   }\r
694   else\r
695     myCreationDlg->Init( mySelection, myType );\r
696 \r
697   myCreationDlg->SetMesh( myMesh );\r
698   myCreationDlg->show();\r
699 }\r
700 \r
701 //=======================================================================\r
702 // name    : SMESHGUI_MeshPatternDlg::onReverse\r
703 // Purpose : SLOT. Called when state of "Reverse order..." checkbox chaged\r
704 //           Calculate new points of the mesh to be created. Redisplay preview\r
705 //=======================================================================\r
706 void SMESHGUI_MeshPatternDlg::onReverse( bool )\r
707 {\r
708   displayPreview();\r
709 }\r
710 \r
711 //=======================================================================\r
712 \r
713 // name    : SMESHGUI_MeshPatternDlg::onPreview\r
714 // Purpose : SLOT. Called when state of "Preview" checkbox changed\r
715 //           Display/Erase preview\r
716 //=======================================================================\r
717 void SMESHGUI_MeshPatternDlg::onPreview( bool )\r
718 {\r
719   displayPreview();\r
720 }\r
721 \r
722 //=======================================================================\r
723 // name    : SMESHGUI_MeshPatternDlg::displayPreview\r
724 // Purpose : Display preview\r
725 //=======================================================================\r
726 void SMESHGUI_MeshPatternDlg::displayPreview()\r
727 {\r
728   try\r
729   {\r
730     // Redisplay preview in dialog\r
731 \r
732     SMESH::point_array_var pnts = myPattern->GetPoints();\r
733     SMESH::long_array_var keyPoints = myPattern->GetKeyPoints();\r
734     SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints();\r
735 \r
736     if ( pnts->length()       == 0 ||\r
737          keyPoints->length()  == 0 ||\r
738          elemPoints->length() == 0 )\r
739     {\r
740       erasePreview();\r
741       return;\r
742     }\r
743 \r
744     else\r
745     {\r
746       PointVector aPoints( pnts->length() );\r
747       QValueVector<int> aKeyPoints( keyPoints->length() );\r
748       ConnectivityVector anElemPoints( elemPoints->length() );\r
749 \r
750       for ( int i = 0, n = pnts->length(); i < n; i++ )\r
751         aPoints[ i ] = pnts[ i ];\r
752 \r
753       for ( int i2 = 0, n2 = keyPoints->length(); i2 < n2; i2++ )\r
754         aKeyPoints[ i2 ] = keyPoints[ i2 ];\r
755 \r
756       for ( int i3 = 0, n3 = elemPoints->length(); i3 < n3; i3++ )\r
757       {\r
758         QValueVector<int> aVec( elemPoints[ i3 ].length() );\r
759         for ( int i4 = 0, n4 = elemPoints[ i3 ].length(); i4 < n4; i4++ )\r
760           aVec[ i4 ] = elemPoints[ i3 ][ i4 ];\r
761 \r
762         anElemPoints[ i3 ] = aVec;\r
763       }\r
764 \r
765       myPicture2d->SetPoints( aPoints, aKeyPoints, anElemPoints );\r
766     }\r
767 \r
768 \r
769     // Redisplay preview in 3D viewer\r
770 \r
771     if ( myPreviewActor != 0 )\r
772     {\r
773       if ( VTKViewer_ViewFrame* vf = SMESH::GetCurrentVtkView() )\r
774       {\r
775         vf->RemoveActor(myPreviewActor);\r
776         vf->Repaint();\r
777       }\r
778       myPreviewActor->Delete();\r
779       myPreviewActor = 0;\r
780     }\r
781 \r
782     if ( !myPreviewChk->isChecked() || !isValid( false ) )\r
783       return;\r
784 \r
785     vtkUnstructuredGrid* aGrid = getGrid();\r
786     if ( aGrid == 0 )\r
787       return;\r
788 \r
789     // Create and display actor\r
790     vtkDataSetMapper* aMapper = vtkDataSetMapper::New();\r
791     aMapper->SetInput( aGrid );\r
792 \r
793     myPreviewActor = SALOME_Actor::New();\r
794     myPreviewActor->PickableOff();\r
795     myPreviewActor->SetMapper( aMapper );\r
796 \r
797     vtkProperty* aProp = vtkProperty::New();\r
798     aProp->SetRepresentationToWireframe();\r
799     aProp->SetColor( 250, 0, 250 );\r
800     if ( SMESH::FindActorByObject( myMesh ) )\r
801       aProp->SetLineWidth( SMESH::GetFloat( "SMESH:SettingsWidth", 1 ) +1 );\r
802     else\r
803       aProp->SetLineWidth( 1 );\r
804     myPreviewActor->SetProperty( aProp );\r
805 \r
806     myPreviewActor->SetRepresentation( 3 );\r
807 \r
808     SMESH::GetCurrentVtkView()->AddActor( myPreviewActor );\r
809     SMESH::GetCurrentVtkView()->Repaint();\r
810 \r
811     aProp->Delete();\r
812     aGrid->Delete();\r
813   }\r
814   catch( const SALOME::SALOME_Exception& S_ex )\r
815   {\r
816     QtCatchCorbaException( S_ex );\r
817     erasePreview();\r
818   }\r
819   catch( ... )\r
820   {\r
821     erasePreview();\r
822   }\r
823 }\r
824 \r
825 //=======================================================================\r
826 // name    : SMESHGUI_MeshPatternDlg::erasePreview\r
827 // Purpose : Erase preview\r
828 //=======================================================================\r
829 void SMESHGUI_MeshPatternDlg::erasePreview()\r
830 {\r
831   // Erase preview in 2D viewer\r
832   myPicture2d->SetPoints( PointVector(), QValueVector<int>(), ConnectivityVector() );\r
833 \r
834   // Erase preview in 3D viewer\r
835   if ( myPreviewActor == 0 )\r
836     return;\r
837 \r
838 \r
839   if ( VTKViewer_ViewFrame* vf = SMESH::GetCurrentVtkView() )\r
840   {\r
841     vf->RemoveActor(myPreviewActor);\r
842     vf->Repaint();\r
843   }\r
844   myPreviewActor->Delete();\r
845   myPreviewActor = 0;\r
846 }\r
847 \r
848 //=======================================================================\r
849 // name    : SMESHGUI_MeshPatternDlg::updateWgState\r
850 // Purpose : Enable/disable selection widgets\r
851 //=======================================================================\r
852 void SMESHGUI_MeshPatternDlg::updateWgState()\r
853 {\r
854   if ( myMesh->_is_nil() )\r
855   {\r
856     for ( int i = Object; i <= Vertex2; i++ )\r
857     {\r
858       mySelBtn [ i ]->setEnabled( false );\r
859       mySelEdit[ i ]->setEnabled( false );\r
860       mySelEdit[ i ]->setText( "" );\r
861     }\r
862   }\r
863   else\r
864   {\r
865     mySelBtn [ Object ]->setEnabled( true );\r
866     mySelEdit[ Object ]->setEnabled( true );\r
867     \r
868     if ( myGeomObj[ Object ]->_is_nil() )\r
869     {\r
870       for ( int i = Vertex1; i <= Vertex2; i++ )\r
871       {\r
872         mySelBtn [ i ]->setEnabled( false );\r
873         mySelEdit[ i ]->setEnabled( false );\r
874         mySelEdit[ i ]->setText( "" );\r
875       }\r
876     }\r
877     else\r
878     {\r
879       for ( int i = Object; i <= Vertex2; i++ )\r
880       {\r
881         mySelBtn [ i ]->setEnabled( true );\r
882         mySelEdit[ i ]->setEnabled( true );\r
883       }\r
884     }\r
885   }\r
886 }\r
887 \r
888 //=======================================================================\r
889 // name    : SMESHGUI_MeshPatternDlg::activateSelection\r
890 // Purpose : Activate selection in accordance with current selection input\r
891 //=======================================================================\r
892 void SMESHGUI_MeshPatternDlg::activateSelection()\r
893 {\r
894   mySelection->ClearFilters();\r
895   QAD_Application::getDesktop()->SetSelectionMode( ActorSelection );\r
896   \r
897   if ( mySelInput == Object && !myMeshShape->_is_nil() )\r
898   {\r
899     if ( myType == Type_2d )\r
900     {\r
901       if ( myNbPoints > 0 )\r
902         mySelection->AddFilter( new SMESH_NumberFilter( \r
903 \r
904           "GEOM", TopAbs_VERTEX, myNbPoints, TopAbs_FACE, myMeshShape ) );\r
905       else\r
906         mySelection->AddFilter( new SMESH_NumberFilter(\r
907           "GEOM", TopAbs_SHAPE, myNbPoints, TopAbs_FACE, myMeshShape ) );\r
908     }\r
909     else\r
910     {\r
911       TColStd_MapOfInteger aTypes;\r
912       aTypes.Add( TopAbs_SHELL );\r
913       aTypes.Add( TopAbs_SOLID );\r
914       mySelection->AddFilter( new SMESH_NumberFilter(\r
915         "GEOM", TopAbs_FACE, 6, aTypes, myMeshShape, true ) );\r
916     }\r
917   }\r
918   else if ( ( mySelInput == Vertex1 || mySelInput == Vertex2 ) && !myGeomObj[ Object ]->_is_nil()  )\r
919   {\r
920     mySelection->AddFilter( new SMESH_NumberFilter(\r
921       "GEOM", TopAbs_SHAPE, 1, TopAbs_VERTEX, myGeomObj[ Object ] ) );\r
922   }\r
923 }\r
924 \r
925 //=======================================================================\r
926 // name    : SMESHGUI_MeshPatternDlg::loadFromFile\r
927 // Purpose : Load pattern from file\r
928 //=======================================================================\r
929 bool SMESHGUI_MeshPatternDlg::loadFromFile( const QString& theName )\r
930 {\r
931   try\r
932   {\r
933     SMESH::SMESH_Pattern_var aPattern = SMESH::GetPattern();\r
934 \r
935     if ( !aPattern->LoadFromFile( theName.latin1() ) ||\r
936          myType == Type_2d && !aPattern->Is2D())\r
937     {\r
938       SMESH::SMESH_Pattern::ErrorCode aCode = aPattern->GetErrorCode();\r
939       QString aMess;\r
940       if      ( aCode == SMESH::SMESH_Pattern::ERR_READ_NB_POINTS      ) aMess = tr( "ERR_READ_NB_POINTS" );\r
941       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_POINT_COORDS   ) aMess = tr( "ERR_READ_POINT_COORDS" );\r
942       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_TOO_FEW_POINTS ) aMess = tr( "ERR_READ_TOO_FEW_POINTS" );\r
943       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_3D_COORD       ) aMess = tr( "ERR_READ_3D_COORD" );\r
944       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_NO_KEYPOINT    ) aMess = tr( "ERR_READ_NO_KEYPOINT" );\r
945       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_BAD_INDEX      ) aMess = tr( "ERR_READ_BAD_INDEX" );\r
946       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_ELEM_POINTS    ) aMess = tr( "ERR_READ_ELEM_POINTS" );\r
947       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_NO_ELEMS       ) aMess = tr( "ERR_READ_NO_ELEMS" );\r
948       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_BAD_KEY_POINT  ) aMess = tr( "ERR_READ_BAD_KEY_POINT" );\r
949       else                                                               aMess = tr( "ERROR_OF_LOADING" );\r
950 \r
951       QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
952         tr( "SMESH_ERROR" ), aMess, QMessageBox::Ok );\r
953       return false;\r
954     }\r
955     else\r
956     {\r
957       myPattern = aPattern;\r
958       return true;\r
959     }\r
960   }\r
961   catch( const SALOME::SALOME_Exception& S_ex )\r
962   {\r
963     QtCatchCorbaException( S_ex );\r
964     QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
965         tr( "SMESH_ERROR" ), tr( "ERROR_OF_LOADING" ), QMessageBox::Ok );\r
966       return false;\r
967   }\r
968 }\r
969 \r
970 //=======================================================================\r
971 // name    : SMESHGUI_MeshPatternDlg::onTypeChanged\r
972 \r
973 // Purpose : SLOT. Called when pattern type changed.\r
974 //           Change dialog's look and feel\r
975 //=======================================================================\r
976 void SMESHGUI_MeshPatternDlg::onTypeChanged( int theType )\r
977 {\r
978   if ( myType == theType )\r
979     return;\r
980 \r
981   myType = theType;\r
982 \r
983   myNbPoints = -1;\r
984   myGeomObj[ Object  ] = GEOM::GEOM_Object::_nil();\r
985   myGeomObj[ Vertex1 ] = GEOM::GEOM_Object::_nil();\r
986   myGeomObj[ Vertex2 ] = GEOM::GEOM_Object::_nil();\r
987   myPattern = SMESH::GetPattern();\r
988 \r
989   myName->setText( "" );\r
990   mySelEdit[ Object  ]->setText( "" );\r
991   mySelEdit[ Vertex1 ]->setText( "" );\r
992   mySelEdit[ Vertex2 ]->setText( "" );\r
993 \r
994   if ( theType == Type_2d )\r
995   {\r
996     mySelLbl [ Vertex2 ]->hide();\r
997     mySelBtn [ Vertex2 ]->hide();\r
998     mySelEdit[ Vertex2 ]->hide();\r
999     myReverseChk->show();\r
1000     myPicture2d->show();\r
1001     myPicture3d->hide();\r
1002     mySelLbl[ Object  ]->setText( tr( "FACE" ) );\r
1003     mySelLbl[ Vertex1 ]->setText( tr( "VERTEX" ) );\r
1004   }\r
1005   else\r
1006   {\r
1007     mySelLbl [ Vertex2 ]->show();\r
1008     mySelBtn [ Vertex2 ]->show();\r
1009     mySelEdit[ Vertex2 ]->show();\r
1010     myReverseChk->hide();\r
1011     myPicture2d->hide();\r
1012     myPicture3d->show();\r
1013     mySelLbl[ Object  ]->setText( tr( "3D_BLOCK" ) );\r
1014     mySelLbl[ Vertex1 ]->setText( tr( "VERTEX1" ) );\r
1015     mySelLbl[ Vertex2 ]->setText( tr( "VERTEX2" ) );\r
1016   }\r
1017 \r
1018   mySelInput = Mesh;\r
1019   activateSelection();\r
1020   updateWgState();\r
1021   displayPreview();\r
1022 }\r
1023 \r
1024 //=======================================================================\r
1025 // name    : SMESHGUI_MeshPatternDlg::getGrid\r
1026 // Purpose : Get unstructured grid for pattern\r
1027 //=======================================================================\r
1028 vtkUnstructuredGrid* SMESHGUI_MeshPatternDlg::getGrid()\r
1029 {\r
1030   try\r
1031   {\r
1032     // Get points from pattern\r
1033     SMESH::point_array_var pnts = myType == Type_2d\r
1034       ? myPattern->ApplyToFace( myGeomObj[ Object ], myGeomObj[ Vertex1 ], myReverseChk->isChecked() )\r
1035       : myPattern->ApplyTo3DBlock( myGeomObj[ Object ], myGeomObj[ Vertex1 ], myGeomObj[ Vertex2 ] );\r
1036 \r
1037     SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints();\r
1038 \r
1039     if ( pnts->length() == 0 || elemPoints->length() == 0 )\r
1040       return 0;\r
1041 \r
1042     \r
1043 \r
1044     // to do : to be removed /////////////////////////////////////////////\r
1045 \r
1046 #ifdef DEB_SLN\r
1047     for ( int i1 = 0, n1 = pnts->length(); i1 < n1; i1++ )\r
1048       printf( "%d: %g %g %g\n", i1, pnts[ i1 ].x, pnts[ i1 ].y, pnts[ i1 ].z );\r
1049 \r
1050     printf( "\nELEMENTS : \n" );\r
1051     for ( int i2 = 0, n2 = elemPoints->length(); i2 < n2; i2++ )\r
1052     {\r
1053 \r
1054       printf( "%d: ", i2 );\r
1055       for ( int i3 = 0, n3 = elemPoints[ i2 ].length(); i3 < n3; i3++ )\r
1056         printf( "%d ", elemPoints[ i2 ][ i3 ] );\r
1057 \r
1058       printf( "\n" );\r
1059 \r
1060     }\r
1061 #endif\r
1062     //////////////////////////////////////////////////////////////////////\r
1063 \r
1064     // Calculate number of points used for cell\r
1065     vtkIdType aNbCells = elemPoints->length();\r
1066     vtkIdType aCellsSize = 0;\r
1067     for ( int i = 0, n = elemPoints->length(); i < n; i++ )\r
1068       aCellsSize += elemPoints[ i ].length();\r
1069 \r
1070     // Create unstructured grid and other  usefull arrays\r
1071     vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();\r
1072 \r
1073     vtkCellArray* aConnectivity = vtkCellArray::New();\r
1074     aConnectivity->Allocate( aCellsSize, 0 );\r
1075 \r
1076     vtkPoints* aPoints = vtkPoints::New();\r
1077     aPoints->SetNumberOfPoints( pnts->length() );\r
1078 \r
1079     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();\r
1080     aCellTypesArray->SetNumberOfComponents( 1 );\r
1081     aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );\r
1082 \r
1083     vtkIdList *anIdList = vtkIdList::New();\r
1084 \r
1085     // Fill array of points\r
1086     for ( int p = 0, nbPnt = pnts->length(); p < nbPnt; p++ )\r
1087       aPoints->SetPoint( p, pnts[ p ].x, pnts[ p ].y, pnts[ p ].z );\r
1088 \r
1089     for ( int e = 0, nbElem = elemPoints->length(); e < nbElem; e++ )\r
1090     {\r
1091       int nbPoints = elemPoints[ e ].length();\r
1092       anIdList->SetNumberOfIds( nbPoints );\r
1093       for ( int i = 0; i < nbPoints; i++ )\r
1094         anIdList->SetId( i, elemPoints[ e ][ i ] );\r
1095 \r
1096       aConnectivity->InsertNextCell( anIdList );\r
1097 \r
1098       if      ( nbPoints == 3 ) aCellTypesArray->InsertNextValue( VTK_TRIANGLE );\r
1099       else if ( nbPoints == 5 ) aCellTypesArray->InsertNextValue( VTK_PYRAMID );\r
1100       else if ( nbPoints == 6 ) aCellTypesArray->InsertNextValue( VTK_WEDGE );\r
1101       else if ( nbPoints == 8 ) aCellTypesArray->InsertNextValue( VTK_HEXAHEDRON );\r
1102       else if ( nbPoints == 4 && myType == Type_2d ) aCellTypesArray->InsertNextValue( VTK_QUAD );\r
1103       else if ( nbPoints == 4 && myType == Type_3d ) aCellTypesArray->InsertNextValue( VTK_TETRA );\r
1104       else aCellTypesArray->InsertNextValue( VTK_EMPTY_CELL );\r
1105     }\r
1106 \r
1107     vtkIntArray* aCellLocationsArray = vtkIntArray::New();\r
1108     aCellLocationsArray->SetNumberOfComponents( 1 );\r
1109     aCellLocationsArray->SetNumberOfTuples( aNbCells );\r
1110 \r
1111     aConnectivity->InitTraversal();\r
1112     for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )\r
1113       aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );\r
1114 \r
1115     aGrid->SetPoints( aPoints );\r
1116     aGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );\r
1117 \r
1118     aConnectivity->Delete();\r
1119     aPoints->Delete();\r
1120     aCellTypesArray->Delete();\r
1121     anIdList->Delete();\r
1122     aCellLocationsArray->Delete();\r
1123 \r
1124     return aGrid;\r
1125   }\r
1126   catch( ... )\r
1127   {\r
1128     return 0;\r
1129   }\r
1130 }\r
1131 \r
1132 \r
1133 \r
1134 \r
1135 \r
1136 \r
1137 \r
1138 \r
1139 \r
1140 \r
1141 \r
1142 \r
1143 \r
1144 \r
1145 \r
1146 \r
1147 \r
1148 \r
1149 \r
1150 \r