Salome HOME
Merge with version on tag OCC-V2_1_0d
[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     QWidget* aPreview, *aPicture;\r
180     if ( i == 0 )\r
181     {\r
182       myPicture2d = new SMESHGUI_PatternWidget( aPatGrp ),\r
183       myPicture2d->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );\r
184     }\r
185     else\r
186     {\r
187       myPicture3d = new QFrame( aPatGrp ),\r
188       myPreview3d = new QLabel( myPicture3d );\r
189       myPreview3d->setPixmap( iconSample3d );\r
190       QGridLayout* aLay = new QGridLayout( myPicture3d, 3, 3, 0, 0 );\r
191       QSpacerItem* aSpacerH1 = new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum );\r
192       QSpacerItem* aSpacerH2 = new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum );\r
193       QSpacerItem* aSpacerV1 = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );\r
194       QSpacerItem* aSpacerV2 = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );\r
195       aLay->addItem( aSpacerH1, 1, 0 );\r
196       aLay->addItem( aSpacerH2, 1, 2 );\r
197       aLay->addItem( aSpacerV1, 0, 1 );\r
198       aLay->addItem( aSpacerV2, 2, 1 );\r
199       aLay->addWidget( myPreview3d, 1, 1 );\r
200     }\r
201   }\r
202 \r
203   myPreviewChk = new QCheckBox( tr( "PREVIEW" ), aPatGrp );\r
204 \r
205   // Connect signals and slots\r
206 \r
207   connect( myTypeGrp,    SIGNAL( clicked( int )  ), SLOT( onTypeChanged( int ) ) );\r
208   connect( myOpenBtn,    SIGNAL( clicked()       ), SLOT( onOpen()             ) );\r
209   connect( myNewBtn,     SIGNAL( clicked()       ), SLOT( onNew()              ) );\r
210   connect( myReverseChk, SIGNAL( toggled( bool ) ), SLOT( onReverse( bool )    ) );\r
211   connect( myPreviewChk, SIGNAL( toggled( bool ) ), SLOT( onPreview( bool )    ) );\r
212 \r
213   QMap< int, QPushButton* >::iterator anIter;\r
214   for ( anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter )\r
215     connect( *anIter, SIGNAL( clicked() ), SLOT( onSelInputChanged() ) );\r
216 \r
217   return aMainGrp;\r
218 }\r
219 \r
220 //=======================================================================\r
221 \r
222 // name    : SMESHGUI_MeshPatternDlg::createButtonFrame\r
223 // Purpose : Create frame containing buttons\r
224 //=======================================================================\r
225 QFrame* SMESHGUI_MeshPatternDlg::createButtonFrame( QWidget* theParent )\r
226 {\r
227   QFrame* aFrame = new QFrame( theParent );\r
228   aFrame->setFrameStyle( QFrame::Box | QFrame::Sunken );\r
229 \r
230   myOkBtn     = new QPushButton( tr( "SMESH_BUT_OK"    ), aFrame );\r
231   myApplyBtn  = new QPushButton( tr( "SMESH_BUT_APPLY" ), aFrame );\r
232   myCloseBtn  = new QPushButton( tr( "SMESH_BUT_CLOSE" ), aFrame );\r
233 \r
234   QSpacerItem* aSpacer = new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum );\r
235 \r
236   QHBoxLayout* aLay = new QHBoxLayout( aFrame, MARGIN, SPACING );\r
237 \r
238   aLay->addWidget( myOkBtn );\r
239   aLay->addWidget( myApplyBtn );\r
240   aLay->addItem( aSpacer);\r
241   aLay->addWidget( myCloseBtn );\r
242 \r
243   connect( myOkBtn,    SIGNAL( clicked() ), SLOT( onOk() ) );\r
244   connect( myCloseBtn, SIGNAL( clicked() ), SLOT( onClose() ) ) ;\r
245   connect( myApplyBtn, SIGNAL( clicked() ), SLOT( onApply() ) );\r
246 \r
247   return aFrame;\r
248 }\r
249 \r
250 //=======================================================================\r
251 // name    : SMESHGUI_MeshPatternDlg::~SMESHGUI_MeshPatternDlg\r
252 // Purpose : Destructor\r
253 //=======================================================================\r
254 SMESHGUI_MeshPatternDlg::~SMESHGUI_MeshPatternDlg()\r
255 {\r
256 }\r
257 \r
258 //=======================================================================\r
259 // name    : SMESHGUI_MeshPatternDlg::Init\r
260 // Purpose : Init dialog fields, connect signals and slots, show dialog\r
261 //=======================================================================\r
262 void SMESHGUI_MeshPatternDlg::Init( SALOME_Selection* theSelection )\r
263 {\r
264   myPattern = SMESH::GetPattern();\r
265   myPreviewActor = 0;\r
266   myIsCreateDlgOpen = false;\r
267   mySelInput = Mesh;\r
268   myType = -1;\r
269   myNbPoints = -1;\r
270   mySelection = theSelection;\r
271   SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI();\r
272   aSMESHGUI->SetActiveDialogBox( ( QDialog* )this );\r
273   myMesh = SMESH::SMESH_Mesh::_nil();\r
274 \r
275   myMeshShape = GEOM::GEOM_Object::_nil();\r
276   myGeomObj[ Object  ] = GEOM::GEOM_Object::_nil();\r
277   myGeomObj[ Vertex1 ] = GEOM::GEOM_Object::_nil();\r
278   myGeomObj[ Vertex2 ] = GEOM::GEOM_Object::_nil();\r
279 \r
280   // selection and SMESHGUI\r
281   connect( mySelection, SIGNAL( currentSelectionChanged() ), SLOT( onSelectionDone() ) );\r
282   connect( aSMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), SLOT( onDeactivate() ) );\r
283   connect( aSMESHGUI, SIGNAL( SignalCloseAllDialogs() ), SLOT( onClose() ) );\r
284 \r
285   myTypeGrp->setButton( Type_2d );\r
286   onTypeChanged( Type_2d );\r
287 \r
288   updateGeometry();\r
289 \r
290   resize( minimumSize() );\r
291 \r
292   activateSelection();\r
293   onSelectionDone();\r
294 \r
295   int x, y ;\r
296   aSMESHGUI->DefineDlgPosition( this, x, y );\r
297   this->move( x, y );\r
298   this->show();\r
299 }\r
300 \r
301 //=======================================================================\r
302 // name    : SMESHGUI_MeshPatternDlg::isValid\r
303 // Purpose : Verify validity of entry data\r
304 //=======================================================================\r
305 bool SMESHGUI_MeshPatternDlg::isValid( const bool theMess )\r
306 {\r
307   if ( myMesh->_is_nil() || myMeshShape->_is_nil() || myGeomObj[ Object ]->_is_nil() ||\r
308        myGeomObj[ Vertex1 ]->_is_nil() || myType == Type_3d && myGeomObj[ Vertex2 ]->_is_nil() )\r
309   {\r
310     if ( theMess )\r
311       QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
312         tr( "SMESH_INSUFFICIENT_DATA" ), tr( "SMESHGUI_INVALID_PARAMETERS" ), QMessageBox::Ok );\r
313     return false;\r
314   }\r
315   else\r
316     return true;\r
317 }\r
318 \r
319 //=======================================================================\r
320 // name    : SMESHGUI_MeshPatternDlg::onApply\r
321 // Purpose : SLOT called when "Apply" button pressed.\r
322 //=======================================================================\r
323 bool SMESHGUI_MeshPatternDlg::onApply()\r
324 {\r
325   try\r
326   {\r
327     if ( !isValid() )\r
328       return false;\r
329 \r
330     if ( myType == Type_2d )\r
331       myPattern->ApplyToFace(\r
332         myGeomObj[ Object ], myGeomObj[ Vertex1 ], myReverseChk->isChecked() );\r
333     else\r
334       myPattern->ApplyTo3DBlock(\r
335         myGeomObj[ Object ], myGeomObj[ Vertex1 ], myGeomObj[ Vertex2 ] );\r
336 \r
337     if ( myPattern->MakeMesh( myMesh ) )\r
338     {\r
339       mySelection->ClearIObjects();\r
340       SMESH::UpdateView();\r
341       SMESHGUI::GetSMESHGUI()->GetActiveStudy()->updateObjBrowser( true );\r
342       return true;\r
343     }\r
344     else\r
345     {\r
346       QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
347         tr( "SMESH_ERROR" ), tr( "SMESH_OPERATION_FAILED" ), QMessageBox::Ok );\r
348       return false;\r
349     }\r
350   }\r
351   catch( const SALOME::SALOME_Exception& S_ex )\r
352   {\r
353     QtCatchCorbaException( S_ex );    \r
354   }\r
355   catch( ... )\r
356   {\r
357   }\r
358 \r
359   return false;\r
360 }\r
361 \r
362 //=======================================================================\r
363 // name    : SMESHGUI_MeshPatternDlg::onOk\r
364 // Purpose : SLOT called when "Ok" button pressed.\r
365 //=======================================================================\r
366 void SMESHGUI_MeshPatternDlg::onOk()\r
367 {\r
368   if ( onApply() )\r
369     onClose();\r
370 }\r
371 \r
372 //=======================================================================\r
373 // name    : SMESHGUI_MeshPatternDlg::onClose\r
374 // Purpose : SLOT called when "Close" button pressed. Close dialog\r
375 //=======================================================================\r
376 void SMESHGUI_MeshPatternDlg::onClose()\r
377 {\r
378   mySelection->ClearFilters();\r
379   QAD_Application::getDesktop()->SetSelectionMode( ActorSelection );\r
380   disconnect( mySelection, 0, this, 0 );\r
381   disconnect( SMESHGUI::GetSMESHGUI(), 0, this, 0 );\r
382   SMESHGUI::GetSMESHGUI()->ResetState();\r
383   erasePreview();\r
384   reject();\r
385 }\r
386 \r
387 \r
388 \r
389 //=======================================================================\r
390 // name    : SMESHGUI_MeshPatternDlg::onSelectionDone\r
391 // Purpose : SLOT called when selection changed\r
392 //=======================================================================\r
393 void SMESHGUI_MeshPatternDlg::onSelectionDone()\r
394 {\r
395   try\r
396   {\r
397     if ( mySelection->IObjectCount() != 1 )\r
398       return;\r
399 \r
400     if ( mySelInput == Mesh )\r
401     {\r
402       // Retrieve mesh from selection\r
403       Handle(SALOME_InteractiveObject) anIO = mySelection->firstIObject();\r
404       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>( anIO );\r
405       if ( aMesh->_is_nil() )\r
406         return;\r
407 \r
408       // Get geom object corresponding to the mesh\r
409       SALOMEDS::Study_var aStudy =\r
410         SMESHGUI::GetSMESHGUI()->GetActiveStudy()->getStudyDocument();\r
411 \r
412       SALOMEDS::SObject_var aSO = SMESH::FindSObject( aMesh.in() );\r
413       if ( aSO->_is_nil() )\r
414         return;\r
415 \r
416       bool isFound = false;\r
417       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( aSO );\r
418       for( ; anIter->More(); anIter->Next() )\r
419       {\r
420         SALOMEDS::SObject_var aSO = anIter->Value();\r
421         SALOMEDS::SObject_var aRefSO;\r
422 \r
423         GEOM::GEOM_Object_var aMeshShape = GEOM::GEOM_Object::_narrow(\r
424           aSO->ReferencedObject( aRefSO )? aRefSO->GetObject() : aSO->GetObject() );\r
425 \r
426         if ( !aMeshShape->_is_nil() )\r
427         {\r
428           isFound = true;\r
429           myMeshShape = aMeshShape;\r
430           break;\r
431 \r
432         }\r
433       }\r
434 \r
435       if ( !isFound )\r
436         return;\r
437 \r
438       // Clear fields of geom objects if mesh was changed\r
439       if ( myMesh != aMesh )\r
440       {\r
441         for ( int i = Object; i <= Vertex2; i++ )\r
442         {\r
443           myGeomObj[ i ] = GEOM::GEOM_Object::_nil();\r
444           mySelEdit[ i ]->setText( "" );\r
445         }\r
446       }\r
447 \r
448       myMesh = aMesh;\r
449 \r
450       // Set name of mesh in line edit\r
451       QString aName;\r
452       SMESH::GetNameOfSelectedIObjects( mySelection, aName );\r
453       mySelEdit[ Mesh ]->setText( aName );\r
454     }\r
455     else\r
456     {\r
457       // Get geom object from selection\r
458       Handle(SALOME_InteractiveObject) anIO = mySelection->firstIObject();\r
459       GEOM::GEOM_Object_var anObj = SMESH::IObjectToInterface<GEOM::GEOM_Object>( anIO );\r
460       if ( anObj->_is_nil() )\r
461         return;\r
462 \r
463       // Clear fields of vertexes if face or 3d block was changed\r
464       if ( anObj != myGeomObj[ mySelInput ] && mySelInput == Object )\r
465       {\r
466         for ( int i = Vertex1; i <= Vertex2; i++ )\r
467         {\r
468           myGeomObj[ i ] = GEOM::GEOM_Object::_nil();\r
469           mySelEdit[ i ]->setText( "" );\r
470         }\r
471       }\r
472 \r
473       myGeomObj[ mySelInput ] = anObj;\r
474 \r
475       // Set name of geom object in line edit\r
476       QString aName;\r
477       SMESH::GetNameOfSelectedIObjects( mySelection, aName );\r
478       mySelEdit[ mySelInput ]->setText( aName );\r
479     }\r
480   }\r
481   catch( const SALOME::SALOME_Exception& S_ex )\r
482   {\r
483     QtCatchCorbaException( S_ex );\r
484     resetSelInput();\r
485   }\r
486   catch( ... )\r
487   {\r
488     resetSelInput();\r
489   }\r
490 \r
491   updateWgState();\r
492   displayPreview();\r
493 }\r
494 \r
495 //=======================================================================\r
496 // name    : SMESHGUI_MeshPatternDlg::resetSelInput\r
497 // Purpose : Reset fields corresponding to the current selection input\r
498 //=======================================================================\r
499 void SMESHGUI_MeshPatternDlg::resetSelInput()\r
500 {\r
501   if ( mySelInput == Mesh )\r
502   {\r
503     myMesh = SMESH::SMESH_Mesh::_nil();\r
504     myMeshShape = GEOM::GEOM_Object::_nil();\r
505   }\r
506 \r
507   else\r
508     myGeomObj[ mySelInput ] = GEOM::GEOM_Object::_nil();\r
509 \r
510   mySelEdit[ mySelInput ]->setText( "" );\r
511 }\r
512 \r
513 //=======================================================================\r
514 // name    : SMESHGUI_MeshPatternDlg::onDeactivate\r
515 // Purpose : SLOT called when dialog must be deativated\r
516 //=======================================================================\r
517 void SMESHGUI_MeshPatternDlg::onDeactivate()\r
518 {\r
519   mySelection->ClearFilters();\r
520   //if ( myReverseChk->isChecked() )\r
521   //  erasePreview();\r
522   disconnect( mySelection, 0, this, 0 );\r
523   setEnabled( false );\r
524 }\r
525 \r
526 //=======================================================================\r
527 // name    : SMESHGUI_MeshPatternDlg::enterEvent\r
528 // Purpose : Event filter\r
529 //=======================================================================\r
530 void SMESHGUI_MeshPatternDlg::enterEvent( QEvent* )\r
531 {\r
532   if ( myIsCreateDlgOpen )\r
533     return;\r
534     \r
535   if ( myReverseChk->isChecked() )\r
536     displayPreview();\r
537   SMESHGUI::GetSMESHGUI()->EmitSignalDeactivateDialog() ;\r
538   setEnabled( true );\r
539   activateSelection();\r
540   connect( mySelection, SIGNAL( currentSelectionChanged() ), SLOT( onSelectionDone() ) );\r
541 }\r
542 \r
543 \r
544 //=================================================================================\r
545 // function : closeEvent()\r
546 // purpose  :\r
547 //=================================================================================\r
548 void SMESHGUI_MeshPatternDlg::closeEvent( QCloseEvent* e )\r
549 {\r
550   onClose() ;\r
551 }\r
552 \r
553 //=======================================================================\r
554 // name    : SMESHGUI_MeshPatternDlg::onSelInputChanged\r
555 // Purpose : SLOT. Called when -> button clicked.\r
556 //           Change current selection input field\r
557 //=======================================================================\r
558 void SMESHGUI_MeshPatternDlg::onSelInputChanged()\r
559 {\r
560   const QObject* aSender = sender();\r
561   for ( int i = Mesh; i <= Vertex2; i++ )\r
562     if ( aSender == mySelBtn[ i ] )\r
563       mySelInput = i;\r
564 \r
565   activateSelection();\r
566   onSelectionDone();\r
567 }\r
568 \r
569 //=======================================================================\r
570 // name    : SMESHGUI_MeshPatternDlg::prepareFilters\r
571 // Purpose : Prepare filters for dialog\r
572 //=======================================================================\r
573 \r
574 QStringList SMESHGUI_MeshPatternDlg::prepareFilters() const\r
575 {\r
576   static QStringList aList;\r
577   if ( aList.isEmpty() )\r
578   {\r
579     aList.append( tr( "PATTERN_FILT" ) );\r
580     //aList.append( tr( "ALL_FILES_FILTER" ) );\r
581   }\r
582 \r
583   return aList;\r
584 }\r
585 \r
586 //================================================================\r
587 \r
588 // Function : SMESHGUI_MeshPatternDlg::autoExtension\r
589 // Purpose  : Append extension to the file name\r
590 //================================================================\r
591 QString SMESHGUI_MeshPatternDlg::autoExtension( const QString& theFileName ) const\r
592 {\r
593   QString anExt = theFileName.section( '.', -1 );\r
594   return anExt != "smp" && anExt != "SMP" ? theFileName + ".smp" : theFileName;\r
595 }\r
596 \r
597 //=======================================================================\r
598 // name    : SMESHGUI_MeshPatternDlg::onOpen\r
599 // Purpose : SLOT. Called when "Open" button clicked.\r
600 //           Displays file open dialog\r
601 //=======================================================================\r
602 void SMESHGUI_MeshPatternDlg::onOpen()\r
603 {\r
604   QAD_FileDlg* aDlg = new QAD_FileDlg( this, true );\r
605   aDlg->setCaption( tr( "LOAD_PATTERN" ) );\r
606   aDlg->setMode( QFileDialogP::ExistingFile );\r
607   aDlg->setFilters( prepareFilters() );\r
608   if ( myName->text() != "" )\r
609     aDlg->setSelection( myName->text() + ".smp" );\r
610   QPushButton* anOkBtn = ( QPushButton* )aDlg->child( "OK", "QPushButton" );\r
611   if ( anOkBtn != 0 )\r
612     anOkBtn->setText( tr( "SMESH_BUT_OK" ) );\r
613 \r
614   if ( aDlg->exec() != Accepted )\r
615     return;\r
616 \r
617   QString fName = aDlg->selectedFile();\r
618   if ( fName.isEmpty() )\r
619     return;\r
620 \r
621   if ( QFileInfo( fName ).extension().isEmpty() )\r
622     fName = autoExtension( fName );\r
623 \r
624   fName = QDir::convertSeparators( fName );\r
625   \r
626   QString prev = QDir::convertSeparators( myName->text() );\r
627   if ( prev == fName )\r
628     return;\r
629 \r
630   // Read string from file\r
631   QFile aFile( fName );\r
632   if ( !aFile.open( IO_ReadOnly ) )\r
633   {\r
634     QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
635       tr( "SMESH_ERROR" ), tr( "ERROR_OF_OPENING" ), QMessageBox::Ok );\r
636     return;\r
637   }\r
638   \r
639   QByteArray aDataArray = aFile.readAll();\r
640   const char* aData = aDataArray.data();\r
641   if ( aData == 0 )\r
642   {\r
643     QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
644       tr( "SMESH_ERROR" ), tr( "ERROR_OF_READING" ), QMessageBox::Ok );\r
645     return;\r
646   }\r
647   \r
648   if ( loadFromFile( aData ) )\r
649     myName->setText( QFileInfo( fName ).baseName() );\r
650     \r
651   updateWgState();\r
652   displayPreview();\r
653 }\r
654 \r
655 //=======================================================================\r
656 // name    : SMESHGUI_MeshPatternDlg::onCloseCreationDlg\r
657 // Purpose : SLOT. Called when "Pattern creation" dialog closed with "Close"\r
658 //=======================================================================\r
659 void SMESHGUI_MeshPatternDlg::onCloseCreationDlg()\r
660 {\r
661   setEnabled( true );\r
662   myIsCreateDlgOpen = false;\r
663 }\r
664 \r
665 \r
666 //=======================================================================\r
667 // name    : SMESHGUI_MeshPatternDlg::onOkCreationDlg\r
668 // Purpose : SLOT. Called when "Pattern creation" dialog closed with OK\r
669 //           or SAVE buttons. Initialize myPattern field. Redisplay preview\r
670 //=======================================================================\r
671 void SMESHGUI_MeshPatternDlg::onOkCreationDlg()\r
672 {\r
673   myPattern = SMESH::SMESH_Pattern::_duplicate( myCreationDlg->GetPattern() );\r
674   myName->setText( myCreationDlg->GetPatternName() );\r
675   displayPreview();\r
676   setEnabled( true );\r
677   myIsCreateDlgOpen = false;\r
678 }\r
679 \r
680 \r
681 //=======================================================================\r
682 // name    : SMESHGUI_MeshPatternDlg::onNew\r
683 // Purpose : SLOT. Called when "New..." button clicked. Create new pattern\r
684 //=======================================================================\r
685 void SMESHGUI_MeshPatternDlg::onNew()\r
686 {\r
687   setEnabled( false );\r
688   myIsCreateDlgOpen = true;\r
689   if ( myCreationDlg == 0 )\r
690   {\r
691     myCreationDlg = new SMESHGUI_CreatePatternDlg( this, mySelection, myType );\r
692     connect( myCreationDlg, SIGNAL( NewPattern() ), SLOT( onOkCreationDlg() ) );\r
693     connect( myCreationDlg, SIGNAL( Close() ), SLOT( onCloseCreationDlg() ) );\r
694   }\r
695   else\r
696     myCreationDlg->Init( mySelection, myType );\r
697 \r
698   myCreationDlg->SetMesh( myMesh );\r
699   myCreationDlg->show();\r
700 }\r
701 \r
702 //=======================================================================\r
703 // name    : SMESHGUI_MeshPatternDlg::onReverse\r
704 // Purpose : SLOT. Called when state of "Reverse order..." checkbox chaged\r
705 //           Calculate new points of the mesh to be created. Redisplay preview\r
706 //=======================================================================\r
707 void SMESHGUI_MeshPatternDlg::onReverse( bool )\r
708 {\r
709   displayPreview();\r
710 }\r
711 \r
712 //=======================================================================\r
713 \r
714 // name    : SMESHGUI_MeshPatternDlg::onPreview\r
715 // Purpose : SLOT. Called when state of "Preview" checkbox changed\r
716 //           Display/Erase preview\r
717 //=======================================================================\r
718 void SMESHGUI_MeshPatternDlg::onPreview( bool )\r
719 {\r
720   displayPreview();\r
721 }\r
722 \r
723 //=======================================================================\r
724 // name    : SMESHGUI_MeshPatternDlg::displayPreview\r
725 // Purpose : Display preview\r
726 //=======================================================================\r
727 void SMESHGUI_MeshPatternDlg::displayPreview()\r
728 {\r
729   try\r
730   {\r
731     // Redisplay preview in dialog\r
732 \r
733     SMESH::point_array_var pnts = myPattern->GetPoints();\r
734     SMESH::long_array_var keyPoints = myPattern->GetKeyPoints();\r
735     SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints();\r
736 \r
737     if ( pnts->length()       == 0 ||\r
738          keyPoints->length()  == 0 ||\r
739          elemPoints->length() == 0 )\r
740     {\r
741       erasePreview();\r
742       return;\r
743     }\r
744 \r
745     else\r
746     {\r
747       PointVector aPoints( pnts->length() );\r
748       QValueVector<int> aKeyPoints( keyPoints->length() );\r
749       ConnectivityVector anElemPoints( elemPoints->length() );\r
750 \r
751       for ( int i = 0, n = pnts->length(); i < n; i++ )\r
752         aPoints[ i ] = pnts[ i ];\r
753 \r
754       for ( int i2 = 0, n2 = keyPoints->length(); i2 < n2; i2++ )\r
755         aKeyPoints[ i2 ] = keyPoints[ i2 ];\r
756 \r
757       for ( int i3 = 0, n3 = elemPoints->length(); i3 < n3; i3++ )\r
758       {\r
759         QValueVector<int> aVec( elemPoints[ i3 ].length() );\r
760         for ( int i4 = 0, n4 = elemPoints[ i3 ].length(); i4 < n4; i4++ )\r
761           aVec[ i4 ] = elemPoints[ i3 ][ i4 ];\r
762 \r
763         anElemPoints[ i3 ] = aVec;\r
764       }\r
765 \r
766       myPicture2d->SetPoints( aPoints, aKeyPoints, anElemPoints );\r
767     }\r
768 \r
769 \r
770     // Redisplay preview in 3D viewer\r
771 \r
772     if ( myPreviewActor != 0 )\r
773     {\r
774       if ( VTKViewer_ViewFrame* vf = SMESH::GetCurrentVtkView() )\r
775       {\r
776         vf->RemoveActor(myPreviewActor);\r
777         vf->Repaint();\r
778       }\r
779       myPreviewActor->Delete();\r
780       myPreviewActor = 0;\r
781     }\r
782 \r
783     if ( !myPreviewChk->isChecked() || !isValid( false ) )\r
784       return;\r
785 \r
786     vtkUnstructuredGrid* aGrid = getGrid();\r
787     if ( aGrid == 0 )\r
788       return;\r
789 \r
790     // Create and display actor\r
791     vtkDataSetMapper* aMapper = vtkDataSetMapper::New();\r
792     aMapper->SetInput( aGrid );\r
793 \r
794     myPreviewActor = SALOME_Actor::New();\r
795     myPreviewActor->PickableOff();\r
796     myPreviewActor->SetMapper( aMapper );\r
797 \r
798     vtkProperty* aProp = vtkProperty::New();\r
799     aProp->SetRepresentationToWireframe();\r
800     aProp->SetColor( 250, 0, 250 );\r
801     if ( SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh ) )\r
802       aProp->SetLineWidth( SMESH::GetFloat( "SMESH:SettingsWidth", 1 ) +1 );\r
803     else\r
804       aProp->SetLineWidth( 1 );\r
805     myPreviewActor->SetProperty( aProp );\r
806 \r
807     myPreviewActor->SetRepresentation( 3 );\r
808 \r
809     SMESH::GetCurrentVtkView()->AddActor( myPreviewActor );\r
810     SMESH::GetCurrentVtkView()->Repaint();\r
811 \r
812     aProp->Delete();\r
813     aGrid->Delete();\r
814   }\r
815   catch( const SALOME::SALOME_Exception& S_ex )\r
816   {\r
817     QtCatchCorbaException( S_ex );\r
818     erasePreview();\r
819   }\r
820   catch( ... )\r
821   {\r
822     erasePreview();\r
823   }\r
824 }\r
825 \r
826 //=======================================================================\r
827 // name    : SMESHGUI_MeshPatternDlg::erasePreview\r
828 // Purpose : Erase preview\r
829 //=======================================================================\r
830 void SMESHGUI_MeshPatternDlg::erasePreview()\r
831 {\r
832   // Erase preview in 2D viewer\r
833   myPicture2d->SetPoints( PointVector(), QValueVector<int>(), ConnectivityVector() );\r
834 \r
835   // Erase preview in 3D viewer\r
836   if ( myPreviewActor == 0 )\r
837     return;\r
838 \r
839 \r
840   if ( VTKViewer_ViewFrame* vf = SMESH::GetCurrentVtkView() )\r
841   {\r
842     vf->RemoveActor(myPreviewActor);\r
843     vf->Repaint();\r
844   }\r
845   myPreviewActor->Delete();\r
846   myPreviewActor = 0;\r
847 }\r
848 \r
849 //=======================================================================\r
850 // name    : SMESHGUI_MeshPatternDlg::updateWgState\r
851 // Purpose : Enable/disable selection widgets\r
852 //=======================================================================\r
853 void SMESHGUI_MeshPatternDlg::updateWgState()\r
854 {\r
855   if ( myMesh->_is_nil() )\r
856   {\r
857     for ( int i = Object; i <= Vertex2; i++ )\r
858     {\r
859       mySelBtn [ i ]->setEnabled( false );\r
860       mySelEdit[ i ]->setEnabled( false );\r
861       mySelEdit[ i ]->setText( "" );\r
862     }\r
863   }\r
864   else\r
865   {\r
866     mySelBtn [ Object ]->setEnabled( true );\r
867     mySelEdit[ Object ]->setEnabled( true );\r
868     \r
869     if ( myGeomObj[ Object ]->_is_nil() )\r
870     {\r
871       for ( int i = Vertex1; i <= Vertex2; i++ )\r
872       {\r
873         mySelBtn [ i ]->setEnabled( false );\r
874         mySelEdit[ i ]->setEnabled( false );\r
875         mySelEdit[ i ]->setText( "" );\r
876       }\r
877     }\r
878     else\r
879     {\r
880       for ( int i = Object; i <= Vertex2; i++ )\r
881       {\r
882         mySelBtn [ i ]->setEnabled( true );\r
883         mySelEdit[ i ]->setEnabled( true );\r
884       }\r
885     }\r
886   }\r
887 }\r
888 \r
889 //=======================================================================\r
890 // name    : SMESHGUI_MeshPatternDlg::activateSelection\r
891 // Purpose : Activate selection in accordance with current selection input\r
892 //=======================================================================\r
893 void SMESHGUI_MeshPatternDlg::activateSelection()\r
894 {\r
895   mySelection->ClearFilters();\r
896   QAD_Application::getDesktop()->SetSelectionMode( ActorSelection );\r
897   \r
898   if ( mySelInput == Object && !myMeshShape->_is_nil() )\r
899   {\r
900     if ( myType == Type_2d )\r
901     {\r
902       if ( myNbPoints > 0 )\r
903         mySelection->AddFilter( new SMESH_NumberFilter( \r
904 \r
905           "GEOM", TopAbs_VERTEX, myNbPoints, TopAbs_FACE, myMeshShape ) );\r
906       else\r
907         mySelection->AddFilter( new SMESH_NumberFilter(\r
908           "GEOM", TopAbs_SHAPE, myNbPoints, TopAbs_FACE, myMeshShape ) );\r
909     }\r
910     else\r
911     {\r
912       TColStd_MapOfInteger aTypes;\r
913       aTypes.Add( TopAbs_SHELL );\r
914       aTypes.Add( TopAbs_SOLID );\r
915       mySelection->AddFilter( new SMESH_NumberFilter(\r
916         "GEOM", TopAbs_FACE, 6, aTypes, myMeshShape, true ) );\r
917     }\r
918   }\r
919   else if ( ( mySelInput == Vertex1 || mySelInput == Vertex2 ) && !myGeomObj[ Object ]->_is_nil()  )\r
920   {\r
921     mySelection->AddFilter( new SMESH_NumberFilter(\r
922       "GEOM", TopAbs_SHAPE, 1, TopAbs_VERTEX, myGeomObj[ Object ] ) );\r
923   }\r
924 }\r
925 \r
926 //=======================================================================\r
927 // name    : SMESHGUI_MeshPatternDlg::loadFromFile\r
928 // Purpose : Load pattern from file\r
929 //=======================================================================\r
930 bool SMESHGUI_MeshPatternDlg::loadFromFile( const QString& theName )\r
931 {\r
932   try\r
933   {\r
934     SMESH::SMESH_Pattern_var aPattern = SMESH::GetPattern();\r
935 \r
936     if ( !aPattern->LoadFromFile( theName.latin1() ) ||\r
937          myType == Type_2d && !aPattern->Is2D())\r
938     {\r
939       SMESH::SMESH_Pattern::ErrorCode aCode = aPattern->GetErrorCode();\r
940       QString aMess;\r
941       if      ( aCode == SMESH::SMESH_Pattern::ERR_READ_NB_POINTS      ) aMess = tr( "ERR_READ_NB_POINTS" );\r
942       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_POINT_COORDS   ) aMess = tr( "ERR_READ_POINT_COORDS" );\r
943       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_TOO_FEW_POINTS ) aMess = tr( "ERR_READ_TOO_FEW_POINTS" );\r
944       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_3D_COORD       ) aMess = tr( "ERR_READ_3D_COORD" );\r
945       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_NO_KEYPOINT    ) aMess = tr( "ERR_READ_NO_KEYPOINT" );\r
946       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_BAD_INDEX      ) aMess = tr( "ERR_READ_BAD_INDEX" );\r
947       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_ELEM_POINTS    ) aMess = tr( "ERR_READ_ELEM_POINTS" );\r
948       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_NO_ELEMS       ) aMess = tr( "ERR_READ_NO_ELEMS" );\r
949       else if ( aCode == SMESH::SMESH_Pattern::ERR_READ_BAD_KEY_POINT  ) aMess = tr( "ERR_READ_BAD_KEY_POINT" );\r
950       else                                                               aMess = tr( "ERROR_OF_LOADING" );\r
951 \r
952       QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
953         tr( "SMESH_ERROR" ), aMess, QMessageBox::Ok );\r
954       return false;\r
955     }\r
956     else\r
957     {\r
958       myPattern = aPattern;\r
959       return true;\r
960     }\r
961   }\r
962   catch( const SALOME::SALOME_Exception& S_ex )\r
963   {\r
964     QtCatchCorbaException( S_ex );\r
965     QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),\r
966         tr( "SMESH_ERROR" ), tr( "ERROR_OF_LOADING" ), QMessageBox::Ok );\r
967       return false;\r
968   }\r
969 }\r
970 \r
971 //=======================================================================\r
972 // name    : SMESHGUI_MeshPatternDlg::onTypeChanged\r
973 \r
974 // Purpose : SLOT. Called when pattern type changed.\r
975 //           Change dialog's look and feel\r
976 //=======================================================================\r
977 void SMESHGUI_MeshPatternDlg::onTypeChanged( int theType )\r
978 {\r
979   if ( myType == theType )\r
980     return;\r
981 \r
982   myType = theType;\r
983 \r
984   myNbPoints = -1;\r
985   myGeomObj[ Object  ] = GEOM::GEOM_Object::_nil();\r
986   myGeomObj[ Vertex1 ] = GEOM::GEOM_Object::_nil();\r
987   myGeomObj[ Vertex2 ] = GEOM::GEOM_Object::_nil();\r
988   myPattern = SMESH::GetPattern();\r
989 \r
990   myName->setText( "" );\r
991   mySelEdit[ Object  ]->setText( "" );\r
992   mySelEdit[ Vertex1 ]->setText( "" );\r
993   mySelEdit[ Vertex2 ]->setText( "" );\r
994 \r
995   if ( theType == Type_2d )\r
996   {\r
997     mySelLbl [ Vertex2 ]->hide();\r
998     mySelBtn [ Vertex2 ]->hide();\r
999     mySelEdit[ Vertex2 ]->hide();\r
1000     myReverseChk->show();\r
1001     myPicture2d->show();\r
1002     myPicture3d->hide();\r
1003     mySelLbl[ Object  ]->setText( tr( "FACE" ) );\r
1004     mySelLbl[ Vertex1 ]->setText( tr( "VERTEX" ) );\r
1005   }\r
1006   else\r
1007   {\r
1008     mySelLbl [ Vertex2 ]->show();\r
1009     mySelBtn [ Vertex2 ]->show();\r
1010     mySelEdit[ Vertex2 ]->show();\r
1011     myReverseChk->hide();\r
1012     myPicture2d->hide();\r
1013     myPicture3d->show();\r
1014     mySelLbl[ Object  ]->setText( tr( "3D_BLOCK" ) );\r
1015     mySelLbl[ Vertex1 ]->setText( tr( "VERTEX1" ) );\r
1016     mySelLbl[ Vertex2 ]->setText( tr( "VERTEX2" ) );\r
1017   }\r
1018 \r
1019   mySelInput = Mesh;\r
1020   activateSelection();\r
1021   updateWgState();\r
1022   displayPreview();\r
1023 }\r
1024 \r
1025 //=======================================================================\r
1026 // name    : SMESHGUI_MeshPatternDlg::getGrid\r
1027 // Purpose : Get unstructured grid for pattern\r
1028 //=======================================================================\r
1029 vtkUnstructuredGrid* SMESHGUI_MeshPatternDlg::getGrid()\r
1030 {\r
1031   try\r
1032   {\r
1033     // Get points from pattern\r
1034     SMESH::point_array_var pnts = myType == Type_2d\r
1035       ? myPattern->ApplyToFace( myGeomObj[ Object ], myGeomObj[ Vertex1 ], myReverseChk->isChecked() )\r
1036       : myPattern->ApplyTo3DBlock( myGeomObj[ Object ], myGeomObj[ Vertex1 ], myGeomObj[ Vertex2 ] );\r
1037 \r
1038     SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints();\r
1039 \r
1040     if ( pnts->length() == 0 || elemPoints->length() == 0 )\r
1041       return 0;\r
1042 \r
1043     \r
1044 \r
1045     // to do : to be removed /////////////////////////////////////////////\r
1046 \r
1047 #ifdef DEB_SLN\r
1048     for ( int i1 = 0, n1 = pnts->length(); i1 < n1; i1++ )\r
1049       printf( "%d: %g %g %g\n", i1, pnts[ i1 ].x, pnts[ i1 ].y, pnts[ i1 ].z );\r
1050 \r
1051     printf( "\nELEMENTS : \n" );\r
1052     for ( int i2 = 0, n2 = elemPoints->length(); i2 < n2; i2++ )\r
1053     {\r
1054 \r
1055       printf( "%d: ", i2 );\r
1056       for ( int i3 = 0, n3 = elemPoints[ i2 ].length(); i3 < n3; i3++ )\r
1057         printf( "%d ", elemPoints[ i2 ][ i3 ] );\r
1058 \r
1059       printf( "\n" );\r
1060 \r
1061     }\r
1062 #endif\r
1063     //////////////////////////////////////////////////////////////////////\r
1064 \r
1065     // Calculate number of points used for cell\r
1066     vtkIdType aNbCells = elemPoints->length();\r
1067     vtkIdType aCellsSize = 0;\r
1068     for ( int i = 0, n = elemPoints->length(); i < n; i++ )\r
1069       aCellsSize += elemPoints[ i ].length();\r
1070 \r
1071     // Create unstructured grid and other  usefull arrays\r
1072     vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();\r
1073 \r
1074     vtkCellArray* aConnectivity = vtkCellArray::New();\r
1075     aConnectivity->Allocate( aCellsSize, 0 );\r
1076 \r
1077     vtkPoints* aPoints = vtkPoints::New();\r
1078     aPoints->SetNumberOfPoints( pnts->length() );\r
1079 \r
1080     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();\r
1081     aCellTypesArray->SetNumberOfComponents( 1 );\r
1082     aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );\r
1083 \r
1084     vtkIdList *anIdList = vtkIdList::New();\r
1085 \r
1086     // Fill array of points\r
1087     for ( int p = 0, nbPnt = pnts->length(); p < nbPnt; p++ )\r
1088       aPoints->SetPoint( p, pnts[ p ].x, pnts[ p ].y, pnts[ p ].z );\r
1089 \r
1090     for ( int e = 0, nbElem = elemPoints->length(); e < nbElem; e++ )\r
1091     {\r
1092       int nbPoints = elemPoints[ e ].length();\r
1093       anIdList->SetNumberOfIds( nbPoints );\r
1094       for ( int i = 0; i < nbPoints; i++ )\r
1095         anIdList->SetId( i, elemPoints[ e ][ i ] );\r
1096 \r
1097       aConnectivity->InsertNextCell( anIdList );\r
1098 \r
1099       if      ( nbPoints == 3 ) aCellTypesArray->InsertNextValue( VTK_TRIANGLE );\r
1100       else if ( nbPoints == 5 ) aCellTypesArray->InsertNextValue( VTK_PYRAMID );\r
1101       else if ( nbPoints == 6 ) aCellTypesArray->InsertNextValue( VTK_WEDGE );\r
1102       else if ( nbPoints == 8 ) aCellTypesArray->InsertNextValue( VTK_HEXAHEDRON );\r
1103       else if ( nbPoints == 4 && myType == Type_2d ) aCellTypesArray->InsertNextValue( VTK_QUAD );\r
1104       else if ( nbPoints == 4 && myType == Type_3d ) aCellTypesArray->InsertNextValue( VTK_TETRA );\r
1105       else aCellTypesArray->InsertNextValue( VTK_EMPTY_CELL );\r
1106     }\r
1107 \r
1108     vtkIntArray* aCellLocationsArray = vtkIntArray::New();\r
1109     aCellLocationsArray->SetNumberOfComponents( 1 );\r
1110     aCellLocationsArray->SetNumberOfTuples( aNbCells );\r
1111 \r
1112     aConnectivity->InitTraversal();\r
1113     for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )\r
1114       aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );\r
1115 \r
1116     aGrid->SetPoints( aPoints );\r
1117     aGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );\r
1118 \r
1119     aConnectivity->Delete();\r
1120     aPoints->Delete();\r
1121     aCellTypesArray->Delete();\r
1122     anIdList->Delete();\r
1123     aCellLocationsArray->Delete();\r
1124 \r
1125     return aGrid;\r
1126   }\r
1127   catch( ... )\r
1128   {\r
1129     return 0;\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
1151 \r