Salome HOME
0022169: [CEA 750] Chose before visualization mesh element type to display
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ComputeDlg.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File   : SMESHGUI_ComputeDlg.cxx
21 // Author : Edward AGAPOV, Open CASCADE S.A.S.
22 // SMESH includes
23 //
24 #include "SMESHGUI_ComputeDlg.h"
25
26 #include "SMESHGUI.h"
27 #include "SMESHGUI_GEOMGenUtils.h"
28 #include "SMESHGUI_MeshUtils.h"
29 #include "SMESHGUI_VTKUtils.h"
30 #include "SMESHGUI_MeshInfosBox.h"
31 #include "SMESHGUI_HypothesesUtils.h"
32 #include "SMESHGUI_MeshEditPreview.h"
33 #include "SMESHGUI_MeshOrderOp.h"
34 #include "SMESHGUI_MeshOrderDlg.h"
35
36 #include "SMESH_Actor.h"
37 #include "SMESH_ActorUtils.h"
38
39 #include <SMDS_SetIterator.hxx>
40 #include <SMDS_Mesh.hxx>
41
42 // SALOME GEOM includes
43 #include <GEOMBase.h>
44 #include <GEOM_Actor.h>
45 #include <GEOM_wrap.hxx>
46
47 // SALOME GUI includes
48 #include <LightApp_SelectionMgr.h>
49 #include <LightApp_UpdateFlags.h>
50 #include <SALOME_ListIO.hxx>
51 #include <SVTK_ViewWindow.h>
52 #include <SVTK_ViewModel.h>
53 #include <SalomeApp_Application.h>
54 #include <SUIT_ResourceMgr.h>
55 #include <SUIT_OverrideCursor.h>
56 #include <SUIT_MessageBox.h>
57 #include <SUIT_Desktop.h>
58 #include <QtxComboBox.h>
59
60 // SALOME KERNEL includes
61 #include <SALOMEDS_SObject.hxx>
62 #include <SALOMEDSClient_SObject.hxx>
63 #include <SALOMEDS_wrap.hxx>
64
65 #include CORBA_SERVER_HEADER(SMESH_Group)
66
67 // OCCT includes
68 #include <BRep_Tool.hxx>
69 #include <TopExp.hxx>
70 #include <TopExp_Explorer.hxx>
71 #include <TopTools_IndexedMapOfShape.hxx>
72 #include <TopoDS.hxx>
73
74 #include <TopLoc_Location.hxx>
75 #include <Poly_Triangulation.hxx>
76 #include <Bnd_Box.hxx>
77 #include <BRepBndLib.hxx>
78 #include <BRepMesh_IncrementalMesh.hxx>
79
80 #include <Standard_ErrorHandler.hxx>
81
82 // Qt includes
83 #include <QFrame>
84 #include <QPushButton>
85 #include <QLabel>
86 #include <QRadioButton>
87 #include <QTableWidget>
88 #include <QHeaderView>
89 #include <QGridLayout>
90 #include <QHBoxLayout>
91 #include <QVBoxLayout>
92 #include <QButtonGroup>
93 #include <QCloseEvent>
94 #include <QTimerEvent>
95
96 // VTK includes
97 #include <vtkProperty.h>
98
99 // STL includes
100 #include <vector>
101 #include <set>
102
103 #ifndef WIN32
104 #include <sys/sysinfo.h>
105 #endif
106
107 #define SPACING 6
108 #define MARGIN  11
109
110 #define COLONIZE(str)   (QString(str).contains(":") > 0 ? QString(str) : QString(str) + " :" )
111
112 /* OBSOLETE
113 static void addSeparator( QWidget* parent )
114 {
115   QGridLayout* l = qobject_cast<QGridLayout*>( parent->layout() );
116   int row  = l->rowCount();
117   int cols = l->columnCount();
118   for ( int i = 0; i < cols; i++ ) {
119     QFrame* hline = new QFrame( parent );
120     hline->setFrameStyle( QFrame::HLine | QFrame::Sunken );
121     l->addWidget( hline, row, i );
122   }
123 }
124 */
125
126 enum TCol {
127   COL_ALGO = 0, COL_SHAPE, COL_ERROR, COL_SHAPEID, COL_PUBLISHED, COL_BAD_MESH, NB_COLUMNS
128 };
129
130 //using namespace SMESH;
131
132 namespace SMESH
133 {
134   //=============================================================================
135   /*!
136    * \brief Allocate some memory at construction and release it at destruction.
137    * Is used to be able to continue working after mesh generation or visualization
138    * break due to lack of memory
139    */
140   //=============================================================================
141
142   struct MemoryReserve
143   {
144     char* myBuf;
145     MemoryReserve(): myBuf( new char[1024*1024*1] ){} // 1M
146     void release() { delete [] myBuf; myBuf = 0; }
147     ~MemoryReserve() { release(); }
148   };
149
150   // =========================================================================================
151   /*!
152    * \brief Class showing shapes without publishing
153    */
154   // =========================================================================================
155
156   class TShapeDisplayer
157   {
158   public:
159     // -----------------------------------------------------------------------
160     TShapeDisplayer(): myViewWindow(0)
161     {
162       myProperty = vtkProperty::New();
163       myProperty->SetRepresentationToWireframe();
164       myProperty->SetColor( 250, 0, 250 );
165       myProperty->SetAmbientColor( 250, 0, 250 );
166       myProperty->SetDiffuseColor( 250, 0, 250 );
167       //myProperty->SetSpecularColor( 250, 0, 250 );
168       myProperty->SetLineWidth( 5 );
169     }
170     // -----------------------------------------------------------------------
171     ~TShapeDisplayer()
172     {
173       DeleteActors();
174       myProperty->Delete();
175     }
176     // -----------------------------------------------------------------------
177     void DeleteActors()
178     {
179       if ( hasViewWindow() ) {
180         TActorIterator actorIt = actorIterator();
181         while ( actorIt.more() )
182           if (VTKViewer_Actor* anActor = actorIt.next()) {
183             myViewWindow->RemoveActor( anActor );
184             //anActor->Delete();
185           }
186       }
187       myIndexToShape.Clear();
188       myActors.clear();
189       myShownActors.clear();
190       myBuiltSubs.clear();
191     }
192     // -----------------------------------------------------------------------
193     void SetVisibility (bool theVisibility)
194     {
195       TActorIterator actorIt = shownIterator();
196       while ( actorIt.more() )
197         if (VTKViewer_Actor* anActor = actorIt.next())
198           anActor->SetVisibility(theVisibility);
199       SMESH::RepaintCurrentView();
200     }
201     // -----------------------------------------------------------------------
202     bool HasReadyActorsFor (int subShapeID, GEOM::GEOM_Object_var aMainShape )
203     {
204       std::string mainEntry;
205       if ( !aMainShape->_is_nil() )
206         mainEntry = aMainShape->GetStudyEntry();
207       return ( myMainEntry == mainEntry &&
208                myBuiltSubs.find( subShapeID ) != myBuiltSubs.end() );
209     }
210     // -----------------------------------------------------------------------
211     void Show( int subShapeID, GEOM::GEOM_Object_var aMainShape, bool only = false)
212     {
213       SVTK_ViewWindow* aViewWindow  = SMESH::GetViewWindow( SMESHGUI::GetSMESHGUI() );
214       std::string mainEntry;
215       if ( !aMainShape->_is_nil() )
216         mainEntry = aMainShape->GetStudyEntry();
217       if ( myMainEntry != mainEntry || aViewWindow != myViewWindow ) { // remove actors
218         DeleteActors();
219         TopoDS_Shape aShape;
220         if ( !aMainShape->_is_nil() && GEOMBase::GetShape(aMainShape, aShape)) {
221           checkTriangulation( aShape );
222           TopExp::MapShapes(aShape, myIndexToShape);
223           myActors.resize( myIndexToShape.Extent(), 0 );
224           myShownActors.reserve( myIndexToShape.Extent() );
225         }
226         myMainEntry  = mainEntry;
227         myViewWindow = aViewWindow;
228       }
229       if ( only ) { // hide shown actors
230         TActorIterator actorIt = shownIterator();
231         while ( actorIt.more() )
232           if (VTKViewer_Actor* anActor = actorIt.next())
233             anActor->SetVisibility(false);
234         myShownActors.clear();
235       }
236       // find actors to show
237       TopoDS_Shape aShape = myIndexToShape( subShapeID );
238       if ( !aShape.IsNull() ) {
239         TopAbs_ShapeEnum type( aShape.ShapeType() >= TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE );
240         for ( TopExp_Explorer exp( aShape, type ); exp.More(); exp.Next() ) {
241           //checkTriangulation( exp.Current() );
242           if ( GEOM_Actor* anActor = getActor( exp.Current() ))
243             myShownActors.push_back( anActor );
244         }
245         if ( type == TopAbs_FACE ) {
246           for ( TopExp_Explorer exp( aShape, TopAbs_EDGE ); exp.More(); exp.Next() ) {
247             const TopoDS_Edge & edge = TopoDS::Edge( exp.Current() );
248             if ( !BRep_Tool::Degenerated( edge ))
249               if ( GEOM_Actor* anActor = getActor( exp.Current() ))
250                 myShownActors.push_back( anActor );
251           }
252         }
253       }
254       myBuiltSubs.insert( subShapeID );
255       SetVisibility(true);
256     }
257     // -----------------------------------------------------------------------
258
259   private:
260
261     typedef std::vector<GEOM_Actor*> TActorVec;
262     TActorVec                  myActors;
263     TActorVec                  myShownActors;
264     TopTools_IndexedMapOfShape myIndexToShape;
265     std::string                myMainEntry;
266     SVTK_ViewWindow*           myViewWindow;
267     vtkProperty*               myProperty;
268     std::set<int>              myBuiltSubs;
269
270     // -----------------------------------------------------------------------
271     typedef SMDS_SetIterator< GEOM_Actor*, TActorVec::const_iterator> TActorIterator;
272     TActorIterator actorIterator() {
273       return TActorIterator( myActors.begin(), myActors.end() );
274     }
275     TActorIterator shownIterator() {
276       return TActorIterator( myShownActors.begin(), myShownActors.end() );
277     }
278     // -----------------------------------------------------------------------
279     GEOM_Actor* getActor(const TopoDS_Shape& shape)
280     {
281       int index = myIndexToShape.FindIndex( shape ) - 1;
282       if ( index < 0 || index >= myActors.size() )
283         return 0;
284       GEOM_Actor* & actor = myActors[ index ];
285       if ( !actor ) {
286         actor = GEOM_Actor::New();
287         if ( actor ) {
288           actor->SetShape(shape,0,0);
289           actor->SetProperty(myProperty);
290           actor->SetShadingProperty(myProperty);
291           actor->SetWireframeProperty(myProperty);
292           actor->SetPreviewProperty(myProperty);
293           actor->PickableOff();
294           //         if ( shape.ShapeType() == TopAbs_EDGE )
295           //           actor->SubShapeOn();
296           myViewWindow->AddActor( actor );
297         }
298       }
299       return actor;
300     }
301     // -----------------------------------------------------------------------
302     void checkTriangulation(const TopoDS_Shape& shape)
303     {
304       TopLoc_Location aLoc;
305       Standard_Boolean alreadymesh = Standard_True;
306       TopExp_Explorer ex(shape, TopAbs_FACE);
307       if ( ex.More() )
308         for ( ; ex.More(); ex.Next()) {
309           const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
310           Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
311           if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
312         }
313       else
314         for (ex.Init(shape, TopAbs_EDGE); ex.More(); ex.Next()) {
315           const TopoDS_Edge& edge = TopoDS::Edge(ex.Current());
316           Handle(Poly_Polygon3D) aPoly = BRep_Tool::Polygon3D(edge, aLoc);
317           if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
318         }
319       if (alreadymesh) return;
320       // Compute default deflection
321       Bnd_Box B;
322       BRepBndLib::Add(shape, B);
323       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
324       B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
325       double deflection = Max( aXmax-aXmin, Max ( aYmax-aYmin, aZmax-aZmin)) * 0.01 *4;
326       BRepMesh_IncrementalMesh MESH(shape,deflection);
327     }
328     // -----------------------------------------------------------------------
329     bool hasViewWindow() const
330     {
331       if ( !myViewWindow ) return false;
332
333       if ( SalomeApp_Application* anApp = SMESHGUI::GetSMESHGUI()->getApp() )
334         return FindVtkViewWindow( anApp->getViewManager(SVTK_Viewer::Type(), false ),
335                                   myViewWindow );
336       return false;
337     }
338   };
339
340   // =========================================================================================
341   /*!
342    * \brief Return text describing an error
343    */
344 #define CASE2TEXT(enum) case SMESH::enum: text = QObject::tr( #enum ); break;
345   QString errorText(int errCode, const char* comment)
346   {
347     QString text;
348     switch ( errCode ) {
349       CASE2TEXT( COMPERR_OK               );
350       CASE2TEXT( COMPERR_BAD_INPUT_MESH   );
351       CASE2TEXT( COMPERR_STD_EXCEPTION    );
352       CASE2TEXT( COMPERR_OCC_EXCEPTION    );
353     case SMESH::COMPERR_SLM_EXCEPTION: break; // avoid double "Salome exception"
354       CASE2TEXT( COMPERR_EXCEPTION        );
355       CASE2TEXT( COMPERR_MEMORY_PB        );
356       CASE2TEXT( COMPERR_BAD_SHAPE        );
357       CASE2TEXT( COMPERR_CANCELED         );
358       CASE2TEXT( COMPERR_NO_MESH_ON_SHAPE );
359     case SMESH::COMPERR_ALGO_FAILED:
360       if ( strlen(comment) == 0 )
361         text = QObject::tr("COMPERR_ALGO_FAILED");
362       break;
363     case SMESH::COMPERR_WARNING:
364       text = QObject::tr( (comment && strlen(comment)) ? "COMPERR_WARNING" : "COMPERR_UNKNOWN");
365       break;
366     default:
367       text = QString("#%1").arg( -errCode );
368     }
369     if ( text.length() > 0 ) text += ". ";
370     return text + comment;
371   }
372   // -----------------------------------------------------------------------
373   /*!
374    * \brief Return SO of a sub-shape
375    */
376   _PTR(SObject) getSubShapeSO( int subShapeID, GEOM::GEOM_Object_var aMainShape)
377   {
378     _PTR(SObject) so = SMESH::FindSObject(aMainShape);
379     if ( subShapeID == 1 || !so )
380       return so;
381     _PTR(ChildIterator) it;
382     if (_PTR(Study) study = SMESH::GetActiveStudyDocument())
383       it =  study->NewChildIterator(so);
384     _PTR(SObject) subSO;
385     if ( it ) {
386       for ( it->InitEx(true); !subSO && it->More(); it->Next() ) {
387         GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( it->Value() );
388         if ( !geom->_is_nil() ) {
389           GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
390           if ( list->length() == 1 && list[0] == subShapeID )
391             subSO = it->Value();
392         }
393       }
394     }
395     return subSO;
396   }
397   // -----------------------------------------------------------------------
398   /*!
399    * \brief Return sub-shape by ID. WARNING: UnRegister() must be called on a result
400    */
401   GEOM::GEOM_Object_ptr getSubShape( int subShapeID, GEOM::GEOM_Object_var aMainShape)
402   {
403     GEOM::GEOM_Object_var aSubShape;
404     if ( subShapeID == 1 ) {
405       aSubShape = aMainShape;
406       aSubShape->Register();
407     }
408     else if ( _PTR(SObject) so = getSubShapeSO( subShapeID, aMainShape )) {
409       aSubShape = SMESH::SObjectToInterface<GEOM::GEOM_Object>( so );
410       aSubShape->Register();
411     }
412     else {
413       aSubShape = SMESH::GetSubShape( aMainShape, subShapeID );
414       // future call of UnRegister() will delete a servant of this new object
415     }
416     return aSubShape._retn();
417   }
418   // -----------------------------------------------------------------------
419   /*!
420    * \brief Return shape type name
421    */
422 #define CASE2NAME(enum) case GEOM::enum: name = QObject::tr( "GEOM_" #enum ); break;
423   QString shapeTypeName(GEOM::GEOM_Object_var aShape, const char* dflt = "" )
424   {
425     QString name = dflt;
426     if ( !aShape->_is_nil() ) {
427       switch ( aShape->GetShapeType() ) {
428       CASE2NAME( VERTEX    );
429       CASE2NAME( EDGE      );
430       CASE2NAME( WIRE      );
431       CASE2NAME( FACE      );
432       CASE2NAME( SHELL     );
433       CASE2NAME( SOLID     );
434       CASE2NAME( COMPSOLID );
435       CASE2NAME( COMPOUND  );
436       default:;
437       }
438     }
439     return name;
440   }
441   // -----------------------------------------------------------------------
442   /*!
443    * \brief Return text describing a sub-shape
444    */
445   QString shapeText(int subShapeID, GEOM::GEOM_Object_var aMainShape )
446   {
447     QString text;
448     if ( _PTR(SObject) aSO = getSubShapeSO( subShapeID, aMainShape ))
449       text = aSO->GetName().c_str();
450     else {
451       text = QString("#%1").arg( subShapeID );
452       GEOM::GEOM_Object_wrap shape = getSubShape( subShapeID, aMainShape );
453       QString typeName = shapeTypeName( shape );
454       if ( typeName.length() )
455         text += QString(" (%1)").arg(typeName);
456     }
457     return text;
458   }
459   // -----------------------------------------------------------------------
460   /*!
461    * \brief Return a list of selected rows
462    */
463   int getSelectedRows(QTableWidget* table, QList<int>& rows)
464   {
465     rows.clear();
466     QList<QTableWidgetSelectionRange> selRanges = table->selectedRanges();
467     QTableWidgetSelectionRange range;
468     foreach( range, selRanges )
469     {
470       for ( int row = range.topRow(); row <= range.bottomRow(); ++row )
471         if ( !rows.count( row ))
472              rows.append( row );
473     }
474     if ( rows.isEmpty() && table->currentRow() > -1 )
475       if ( !rows.count( table->currentRow() ))
476         rows.append( table->currentRow() );
477
478     return rows.count();
479   }
480
481 } // namespace SMESH
482
483
484 // =========================================================================================
485 /*!
486  * \brief Dialog to compute a mesh and show computation errors
487  */
488 //=======================================================================
489
490 SMESHGUI_ComputeDlg::SMESHGUI_ComputeDlg( QWidget* parent, bool ForEval )
491  : SMESHGUI_Dialog( parent, false, true, Close/* | Help*/ )
492 {
493   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
494   aDlgLay->setMargin( 0 );
495   aDlgLay->setSpacing( SPACING );
496
497   QFrame* aMainFrame = createMainFrame(mainFrame(),ForEval);
498
499   aDlgLay->addWidget(aMainFrame);
500
501   aDlgLay->setStretchFactor(aMainFrame, 1);
502 }
503
504 // =========================================================================================
505 /*!
506  * \brief Destructor
507  */
508 //=======================================================================
509
510 SMESHGUI_ComputeDlg::~SMESHGUI_ComputeDlg()
511 {
512 }
513
514 //=======================================================================
515 // function : createMainFrame()
516 // purpose  : Create frame containing dialog's fields
517 //=======================================================================
518
519 QFrame* SMESHGUI_ComputeDlg::createMainFrame (QWidget* theParent, bool ForEval)
520 {
521   QFrame* aFrame = new QFrame(theParent);
522
523   SUIT_ResourceMgr* rm = resourceMgr();
524   QPixmap iconCompute (rm->loadPixmap("SMESH", tr("ICON_COMPUTE")));
525
526   // constructor
527
528   QGroupBox* aPixGrp;
529   if(ForEval) {
530     aPixGrp = new QGroupBox(tr("EVAL_DLG"), aFrame);
531   }
532   else {
533     aPixGrp = new QGroupBox(tr("CONSTRUCTOR"), aFrame);
534   }
535   QButtonGroup* aBtnGrp = new QButtonGroup(this);
536   QHBoxLayout* aPixGrpLayout = new QHBoxLayout(aPixGrp);
537   aPixGrpLayout->setMargin(MARGIN); aPixGrpLayout->setSpacing(SPACING);
538
539   QRadioButton* aRBut = new QRadioButton(aPixGrp);
540   aRBut->setIcon(iconCompute);
541   aRBut->setChecked(true);
542   aPixGrpLayout->addWidget(aRBut);
543   aBtnGrp->addButton(aRBut, 0);
544
545   // Mesh name
546
547   QGroupBox* nameBox = new QGroupBox(tr("SMESH_MESHINFO_NAME"), aFrame );
548   QHBoxLayout* nameBoxLayout = new QHBoxLayout(nameBox);
549   nameBoxLayout->setMargin(MARGIN); nameBoxLayout->setSpacing(SPACING);
550   myMeshName = new QLabel(nameBox);
551   nameBoxLayout->addWidget(myMeshName);
552
553   // Mesh Info
554
555   myBriefInfo = new SMESHGUI_MeshInfosBox(false, aFrame);
556   myFullInfo  = new SMESHGUI_MeshInfosBox(true,  aFrame);
557
558   // Computation errors
559
560   myCompErrorGroup = new QGroupBox(tr("ERRORS"), aFrame);
561   myWarningLabel = new QLabel(QString("<b>%1</b>").arg(tr("COMPUTE_WARNING")), myCompErrorGroup);
562   myTable        = new QTableWidget( 1, NB_COLUMNS, myCompErrorGroup);
563   myShowBtn      = new QPushButton(tr("SHOW_SHAPE"), myCompErrorGroup);
564   myPublishBtn   = new QPushButton(tr("PUBLISH_SHAPE"), myCompErrorGroup);
565   myBadMeshBtn   = new QPushButton(tr("SHOW_BAD_MESH"), myCompErrorGroup);
566   myBadMeshToGroupBtn = new QPushButton(tr("GROUP_OF_BAD_MESH"), myCompErrorGroup);
567
568   //myTable->setReadOnly( true ); // VSR: check
569   myTable->setEditTriggers( QAbstractItemView::NoEditTriggers );
570   myTable->hideColumn( COL_PUBLISHED );
571   myTable->hideColumn( COL_SHAPEID );
572   myTable->hideColumn( COL_BAD_MESH );
573   myTable->horizontalHeader()->setResizeMode( COL_ERROR, QHeaderView::Interactive );
574   myTable->setWordWrap( true );
575   myTable->horizontalHeader()->setStretchLastSection( true );
576   myTable->setMinimumWidth( 500 );
577
578   QStringList headers;
579   headers << tr( "COL_ALGO_HEADER" );
580   headers << tr( "COL_SHAPE_HEADER" );
581   headers << tr( "COL_ERROR_HEADER" );
582   headers << tr( "COL_SHAPEID_HEADER" );
583   headers << tr( "COL_PUBLISHED_HEADER" );
584
585   myTable->setHorizontalHeaderLabels( headers );
586
587   // layouting
588   QGridLayout* grpLayout = new QGridLayout(myCompErrorGroup);
589   grpLayout->setSpacing(SPACING);
590   grpLayout->setMargin(MARGIN);
591   grpLayout->addWidget( myWarningLabel,      0, 0, 1, 4 );
592   grpLayout->addWidget( myTable,             1, 0, 1, 4 );
593   grpLayout->addWidget( myShowBtn,           2, 0 );
594   grpLayout->addWidget( myPublishBtn,        2, 1 );
595   grpLayout->addWidget( myBadMeshBtn,        2, 2 );
596   grpLayout->addWidget( myBadMeshToGroupBtn, 2, 3 );
597   grpLayout->setColumnStretch( 3, 1 );
598
599   // Hypothesis definition errors
600
601   myHypErrorGroup = new QGroupBox(tr("SMESH_WRN_MISSING_PARAMETERS"), aFrame);
602   QHBoxLayout* myHypErrorGroupLayout = new QHBoxLayout(myHypErrorGroup);
603   myHypErrorGroupLayout->setMargin(MARGIN);
604   myHypErrorGroupLayout->setSpacing(SPACING);
605   myHypErrorLabel = new QLabel(myHypErrorGroup);
606   myHypErrorGroupLayout->addWidget(myHypErrorLabel);
607
608   // Memory Lack Label
609
610   myMemoryLackGroup = new QGroupBox(tr("ERRORS"), aFrame);
611   QVBoxLayout* myMemoryLackGroupLayout = new QVBoxLayout(myMemoryLackGroup);
612   myMemoryLackGroupLayout->setMargin(MARGIN);
613   myMemoryLackGroupLayout->setSpacing(SPACING);
614   QLabel* memLackLabel = new QLabel(tr("MEMORY_LACK"), myMemoryLackGroup);
615   QFont bold = memLackLabel->font(); bold.setBold(true);
616   memLackLabel->setFont( bold );
617   memLackLabel->setMinimumWidth(300);
618   myMemoryLackGroupLayout->addWidget(memLackLabel);
619
620   // add all widgets to aFrame
621   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
622   aLay->setMargin( 0 );
623   aLay->setSpacing( 0 );
624   aLay->addWidget( aPixGrp );
625   aLay->addWidget( nameBox );
626   aLay->addWidget( myBriefInfo );
627   aLay->addWidget( myFullInfo );
628   aLay->addWidget( myHypErrorGroup );
629   aLay->addWidget( myCompErrorGroup );
630   aLay->addWidget( myMemoryLackGroup );
631   aLay->setStretchFactor( myCompErrorGroup, 1 );
632
633   ((QPushButton*) button( OK ))->setDefault( true );
634
635   return aFrame;
636 }
637
638 //================================================================================
639 /*!
640  * \brief Constructor
641 */
642 //================================================================================
643
644 SMESHGUI_BaseComputeOp::SMESHGUI_BaseComputeOp()
645   : SMESHGUI_Operation(), myCompDlg( 0 )
646 {
647   myTShapeDisplayer = new SMESH::TShapeDisplayer();
648   myBadMeshDisplayer = 0;
649
650   //myHelpFileName = "/files/about_meshes.htm"; // V3
651   myHelpFileName = "about_meshes_page.html"; // V4
652 }
653
654 SMESH::SMESH_Mesh_ptr SMESHGUI_BaseComputeOp::getMesh()
655 {
656   LightApp_SelectionMgr* Sel = selectionMgr();
657   SALOME_ListIO selected; Sel->selectedObjects( selected );
658   Handle(SALOME_InteractiveObject) anIO = selected.First();
659   SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(anIO);
660   return myMesh->_is_nil() ? aMesh._retn() : SMESH::SMESH_Mesh::_duplicate( myMesh );
661 }
662
663 //================================================================================
664 /*!
665  * \brief Start operation
666  * \purpose Init dialog fields, connect signals and slots, show dialog
667  */
668 //================================================================================
669
670 void SMESHGUI_BaseComputeOp::startOperation()
671 {
672   // create compute dialog if not created before
673   computeDlg();
674
675   myMesh      = SMESH::SMESH_Mesh::_nil();
676   myMainShape = GEOM::GEOM_Object::_nil();
677
678   // check selection
679   LightApp_SelectionMgr *Sel = selectionMgr();
680   SALOME_ListIO selected; Sel->selectedObjects( selected );
681
682   int nbSel = selected.Extent();
683   if (nbSel != 1) {
684     SUIT_MessageBox::warning(desktop(),
685                              tr("SMESH_WRN_WARNING"),
686                              tr("SMESH_WRN_NO_AVAILABLE_DATA"));
687     onCancel();
688     return;
689   }
690
691   myIObject = selected.First();
692   myMesh = SMESH::GetMeshByIO(myIObject);
693   if (myMesh->_is_nil()) {
694     SUIT_MessageBox::warning(desktop(),
695                              tr("SMESH_WRN_WARNING"),
696                              tr("SMESH_WRN_NO_AVAILABLE_DATA"));
697     onCancel();
698     return;
699   }
700   myMainShape = myMesh->GetShapeToMesh();
701
702   SMESHGUI_Operation::startOperation();
703 }
704
705 //================================================================================
706 //================================================================================
707
708 SMESHGUI_ComputeDlg_QThread::SMESHGUI_ComputeDlg_QThread(SMESH::SMESH_Gen_var gen,
709                                                          SMESH::SMESH_Mesh_var mesh,
710                                                          GEOM::GEOM_Object_var mainShape)
711 {
712   myResult = false;
713   myGen = gen;
714   myMesh = mesh;
715   myMainShape = mainShape;
716 }
717
718 void SMESHGUI_ComputeDlg_QThread::run()
719 {
720   myResult = myGen->Compute(myMesh, myMainShape);
721 }
722
723 bool SMESHGUI_ComputeDlg_QThread::result()
724 {
725   return myResult;
726 }
727
728 void SMESHGUI_ComputeDlg_QThread::cancel()
729 {
730   myGen->CancelCompute(myMesh, myMainShape);
731 }
732
733 //================================================================================
734 //================================================================================
735
736 SMESHGUI_ComputeDlg_QThreadQDialog::SMESHGUI_ComputeDlg_QThreadQDialog(QWidget             * parent,
737                                                                        SMESH::SMESH_Gen_var  gen,
738                                                                        SMESH::SMESH_Mesh_var mesh,
739                                                                        GEOM::GEOM_Object_var mainShape)
740   : QDialog(parent,
741             Qt::WindowSystemMenuHint |
742             Qt::WindowCloseButtonHint |
743             Qt::Dialog |
744             Qt::WindowMaximizeButtonHint),
745     qthread(gen, mesh, mainShape)
746 {
747   // --
748   setWindowTitle(tr("Compute"));
749   setMinimumWidth( 200 );
750
751   cancelButton = new QPushButton(tr("Cancel"));
752   cancelButton->setDefault(true);
753
754   QLabel * nbNodesName = new QLabel(tr("SMESH_MESHINFO_NODES"), this );
755   QLabel * nbElemsName = new QLabel(tr("SMESH_MESHINFO_ELEMENTS"), this );
756   QLabel * freeRAMName = new QLabel(tr("SMESH_FREERAM"), this );
757   nbNodesLabel = new QLabel("0", this );
758   nbElemsLabel = new QLabel("0", this );
759   freeRAMLabel = new QLabel("", this );
760
761   QGridLayout* layout = new QGridLayout(this);
762   layout->setMargin( MARGIN );
763   layout->setSpacing( SPACING );
764   int row = 0;
765   layout->addWidget(nbNodesName,  row,   0);
766   layout->addWidget(nbNodesLabel, row++, 1);
767   layout->addWidget(nbElemsName,  row,   0);
768   layout->addWidget(nbElemsLabel, row++, 1);
769 #ifndef WNT
770   layout->addWidget(freeRAMName,  row,   0);
771   layout->addWidget(freeRAMLabel, row++, 1);
772 #endif
773   layout->addWidget(cancelButton, row,   0, 1, 2);
774   adjustSize();
775   update();
776
777   connect(cancelButton, SIGNAL(clicked()), this, SLOT(onCancel()));
778   // --
779   startTimer(300); // millisecs
780   qthread.start();
781 }
782
783 bool SMESHGUI_ComputeDlg_QThreadQDialog::result()
784 {
785   return qthread.result();
786 }
787
788 void SMESHGUI_ComputeDlg_QThreadQDialog::onCancel()
789 {
790   qthread.cancel();
791 }  
792
793 void SMESHGUI_ComputeDlg_QThreadQDialog::timerEvent(QTimerEvent *event)
794 {
795   if(qthread.isFinished())
796   {
797     close();
798   }
799   else
800   {
801     nbNodesLabel->setText( QString("%1").arg( qthread.getMesh()->NbNodes() ));
802     nbElemsLabel->setText( QString("%1").arg( qthread.getMesh()->NbElements() ));
803 #ifndef WNT
804     struct sysinfo si;
805     const int err = sysinfo( &si );
806     if ( err )
807       freeRAMLabel->setText("");
808     else
809       freeRAMLabel->setText( tr("SMESH_GIGABYTE").arg
810                              ( si.freeram * si.mem_unit /1024./1024./1024., 0, 'f', 2 ));
811 #endif
812   }
813   event->accept();
814 }
815
816 void SMESHGUI_ComputeDlg_QThreadQDialog::closeEvent(QCloseEvent *event)
817 {
818   if(qthread.isRunning())
819   {
820       event->ignore();
821       return;
822     }
823   event->accept();
824 }
825
826 //================================================================================
827 /*!
828  * \brief computeMesh()
829 */
830 //================================================================================
831
832 void SMESHGUI_BaseComputeOp::computeMesh()
833 {
834   // COMPUTE MESH
835
836   SMESH::MemoryReserve aMemoryReserve;
837
838   SMESH::compute_error_array_var aCompErrors;
839   QString                        aHypErrors;
840
841   bool computeFailed = true, memoryLack = false;
842
843   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
844   if ( !aMeshSObj ) // IPAL 21340
845     return;
846   bool hasShape = myMesh->HasShapeToMesh();
847   bool shapeOK = myMainShape->_is_nil() ? !hasShape : hasShape;
848   if ( shapeOK )
849   {
850     myCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
851     SMESH::SMESH_Gen_var gen = getSMESHGUI()->GetSMESHGen();
852     SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
853     if ( errors->length() > 0 ) {
854       aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
855     }
856     if ( myMesh->HasModificationsToDiscard() && // issue 0020693
857          SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
858                                     tr( "FULL_RECOMPUTE_QUESTION" ),
859                                     tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 1, 0 ) == 0 )
860       myMesh->Clear();
861     SUIT_OverrideCursor aWaitCursor;
862     try {
863 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
864       OCC_CATCH_SIGNALS;
865 #endif
866       bool res;
867       SMESHGUI_ComputeDlg_QThreadQDialog qthreaddialog(desktop(), gen, myMesh, myMainShape);
868       qthreaddialog.exec();
869       res = qthreaddialog.result();
870       res = gen->Compute(myMesh, myMainShape);
871       if (res)
872         computeFailed = false;
873     }
874     catch(const SALOME::SALOME_Exception & S_ex) {
875       memoryLack = true;
876     }
877     try {
878 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
879       OCC_CATCH_SIGNALS;
880 #endif
881       aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
882       // check if there are memory problems
883       for ( int i = 0; (i < aCompErrors->length()) && !memoryLack; ++i )
884         memoryLack = ( aCompErrors[ i ].code == SMESH::COMPERR_MEMORY_PB );
885     }
886     catch(const SALOME::SALOME_Exception & S_ex) {
887       memoryLack = true;
888     }
889
890     if ( !memoryLack && !SMDS_Mesh::CheckMemory(true) ) { // has memory to show dialog boxes?
891       memoryLack = true;
892     }
893
894     // NPAL16631: if ( !memoryLack )
895     {
896       SMESH::ModifiedMesh(aMeshSObj, !computeFailed, myMesh->NbNodes() == 0);
897       update( UF_ObjBrowser | UF_Model );
898
899       // SHOW MESH
900       // NPAL16631: if ( getSMESHGUI()->automaticUpdate() )
901       SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
902       bool limitExceeded;
903       long limitSize = resMgr->integerValue( "SMESH", "update_limit", 500000 );
904       int entities = SMESH_Actor::eAllEntity;
905       if ( !memoryLack )
906       {
907         if ( getSMESHGUI()->automaticUpdate( myMesh, &entities, &limitExceeded ) )
908         {
909           try {
910 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
911             OCC_CATCH_SIGNALS;
912 #endif
913             SMESH_Actor *anActor = SMESH::FindActorByObject( myMesh );
914             if ( !anActor ) anActor = SMESH::CreateActor( aMeshSObj->GetStudy(), aMeshSObj->GetID().c_str(), true );    
915
916             anActor->SetEntityMode( entities );
917             SMESH::DisplayActor( SMESH::GetActiveWindow(), anActor );
918
919             SMESH::Update(myIObject, true);
920
921             if ( limitExceeded )
922             {
923               SUIT_MessageBox::warning( desktop(),
924                                         tr( "SMESH_WRN_WARNING" ),
925                                         tr( "SMESH_WRN_SIZE_INC_LIMIT_EXCEEDED" ).arg( myMesh->NbElements() ).arg( limitSize ) );
926             }
927           }
928           catch (...) {
929 #ifdef _DEBUG_
930             MESSAGE ( "Exception thrown during mesh visualization" );
931 #endif
932             if ( SMDS_Mesh::CheckMemory(true) ) { // has memory to show warning?
933               SMESH::OnVisuException();
934             }
935             else {
936               memoryLack = true;
937             }
938           }
939         }
940         else if ( limitExceeded )
941         {
942           SUIT_MessageBox::warning( desktop(),
943                                     tr( "SMESH_WRN_WARNING" ),
944                                     tr( "SMESH_WRN_SIZE_LIMIT_EXCEEDED" ).arg( myMesh->NbElements() ).arg( limitSize ) );
945         }
946       }
947       LightApp_SelectionMgr *Sel = selectionMgr();
948       if ( Sel )
949       {
950         SALOME_ListIO selected;
951         selected.Append( myIObject );
952         Sel->setSelectedObjects( selected );
953       }
954     }
955   }
956
957   if ( memoryLack )
958     aMemoryReserve.release();
959
960   myCompDlg->setWindowTitle(tr( computeFailed ? "SMESH_WRN_COMPUTE_FAILED" : "SMESH_COMPUTE_SUCCEED"));
961
962   // SHOW ERRORS
963   
964   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
965   bool noHypoError = ( aHypErrors.isEmpty() );
966
967   SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
968   int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
969
970   bool isShowResultDlg = true;
971   switch( aNotifyMode ) {
972   case 0: // show the mesh computation result dialog NEVER
973     isShowResultDlg = false;
974     commit();
975     break;
976   case 1: // show the mesh computation result dialog if there are some errors
977     if ( memoryLack || !noCompError || !noHypoError )
978       isShowResultDlg = true;
979     else
980     {
981       isShowResultDlg = false;
982       commit();
983     }
984     break;
985   default: // show the result dialog after each mesh computation
986     isShowResultDlg = true;
987   }
988
989   // SHOW RESULTS
990   if ( isShowResultDlg )
991     showComputeResult( memoryLack, noCompError,aCompErrors, noHypoError, aHypErrors );
992 }
993
994 void SMESHGUI_BaseComputeOp::showComputeResult( const bool theMemoryLack,
995                                                 const bool theNoCompError,
996                                                 SMESH::compute_error_array_var& theCompErrors,
997                                                 const bool theNoHypoError,
998                                                 const QString& theHypErrors )
999 {
1000   bool hasShape = myMesh->HasShapeToMesh();
1001   SMESHGUI_ComputeDlg* aCompDlg = computeDlg();
1002   aCompDlg->myMemoryLackGroup->hide();
1003
1004   if ( theMemoryLack )
1005   {
1006     aCompDlg->myMemoryLackGroup->show();
1007     aCompDlg->myFullInfo->hide();
1008     aCompDlg->myBriefInfo->hide();
1009     aCompDlg->myHypErrorGroup->hide();
1010     aCompDlg->myCompErrorGroup->hide();
1011   }
1012   else if ( theNoCompError && theNoHypoError )
1013   {
1014     SMESH::long_array_var aRes = myMesh->GetMeshInfo();
1015     aCompDlg->myFullInfo->SetMeshInfo( aRes );
1016     aCompDlg->myFullInfo->show();
1017     aCompDlg->myBriefInfo->hide();
1018     aCompDlg->myHypErrorGroup->hide();
1019     aCompDlg->myCompErrorGroup->hide();
1020   }
1021   else
1022   {
1023     bool onlyWarnings = !theNoCompError; // == valid mesh computed but there are errors reported
1024     for ( int i = 0; i < theCompErrors->length() && onlyWarnings; ++i )
1025       onlyWarnings = ( theCompErrors[ i ].code == SMESH::COMPERR_WARNING ||
1026                        theCompErrors[ i ].code == SMESH::COMPERR_NO_MESH_ON_SHAPE );
1027
1028     // full or brief mesh info
1029     SMESH::long_array_var aRes = myMesh->GetMeshInfo();
1030     if ( onlyWarnings ) {
1031       aCompDlg->myFullInfo->SetMeshInfo( aRes );
1032       aCompDlg->myFullInfo->show();
1033       aCompDlg->myBriefInfo->hide();
1034     } else {
1035       aCompDlg->myBriefInfo->SetMeshInfo( aRes );
1036       aCompDlg->myBriefInfo->show();
1037       aCompDlg->myFullInfo->hide();
1038     }
1039
1040     // pbs of hypo dfinitions
1041     if ( theNoHypoError ) {
1042       aCompDlg->myHypErrorGroup->hide();
1043     } else {
1044       aCompDlg->myHypErrorGroup->show();
1045       aCompDlg->myHypErrorLabel->setText( theHypErrors );
1046     }
1047
1048     // table of errors
1049     if ( theNoCompError )
1050     {
1051       aCompDlg->myCompErrorGroup->hide();
1052     }
1053     else
1054     {
1055       aCompDlg->myCompErrorGroup->show();
1056
1057       if ( onlyWarnings )
1058         aCompDlg->myWarningLabel->show();
1059       else
1060         aCompDlg->myWarningLabel->hide();
1061
1062       if ( !hasShape ) {
1063         aCompDlg->myPublishBtn->hide();
1064         aCompDlg->myShowBtn->hide();
1065       }
1066       else {
1067         aCompDlg->myPublishBtn->show();
1068         aCompDlg->myShowBtn->show();
1069       }
1070
1071       // fill table of errors
1072       QTableWidget* tbl = aCompDlg->myTable;
1073       tbl->setRowCount( theCompErrors->length() );
1074       if ( !hasShape ) tbl->hideColumn( COL_SHAPE );
1075       else             tbl->showColumn( COL_SHAPE );
1076       tbl->setColumnWidth( COL_ERROR, 200 );
1077
1078       bool hasBadMesh = false;
1079       for ( int row = 0; row < theCompErrors->length(); ++row )
1080       {
1081         SMESH::ComputeError & err = theCompErrors[ row ];
1082
1083         QString text = err.algoName.in();
1084         if ( !tbl->item( row, COL_ALGO ) ) tbl->setItem( row, COL_ALGO, new QTableWidgetItem( text ) );
1085         else tbl->item( row, COL_ALGO )->setText( text );
1086
1087         text = SMESH::errorText( err.code, err.comment.in() );
1088         if ( !tbl->item( row, COL_ERROR ) ) tbl->setItem( row, COL_ERROR, new QTableWidgetItem( text ) );
1089         else tbl->item( row, COL_ERROR )->setText( text );
1090
1091         text = QString("%1").arg( err.subShapeID );
1092         if ( !tbl->item( row, COL_SHAPEID ) ) tbl->setItem( row, COL_SHAPEID, new QTableWidgetItem( text ) );
1093         else tbl->item( row, COL_SHAPEID )->setText( text );
1094
1095         text = hasShape ? SMESH::shapeText( err.subShapeID, myMainShape ) : QString("");
1096         if ( !tbl->item( row, COL_SHAPE ) ) tbl->setItem( row, COL_SHAPE, new QTableWidgetItem( text ) );
1097         else tbl->item( row, COL_SHAPE )->setText( text );
1098
1099         text = ( !hasShape || SMESH::getSubShapeSO( err.subShapeID, myMainShape )) ? "PUBLISHED" : "";
1100         if ( !tbl->item( row, COL_PUBLISHED ) ) tbl->setItem( row, COL_PUBLISHED, new QTableWidgetItem( text ) );
1101         else tbl->item( row, COL_PUBLISHED )->setText( text ); // if text=="", "PUBLISH" button enabled
1102
1103         text = err.hasBadMesh ? "hasBadMesh" : "";
1104         if ( !tbl->item( row, COL_BAD_MESH ) ) tbl->setItem( row, COL_BAD_MESH, new QTableWidgetItem( text ) );
1105         else tbl->item( row, COL_BAD_MESH )->setText( text );
1106         if ( err.hasBadMesh ) hasBadMesh = true;
1107
1108         //tbl->item( row, COL_ERROR )->setWordWrap( true ); // VSR: TODO ???
1109         tbl->resizeRowToContents( row );
1110       }
1111       tbl->resizeColumnToContents( COL_ALGO );
1112       tbl->resizeColumnToContents( COL_SHAPE );
1113       tbl->setWordWrap( true );
1114
1115       if ( hasBadMesh ) {
1116         aCompDlg->myBadMeshBtn->show();
1117         aCompDlg->myBadMeshToGroupBtn->show();
1118       }
1119       else {
1120         aCompDlg->myBadMeshBtn->hide();
1121         aCompDlg->myBadMeshToGroupBtn->hide();
1122       }
1123       tbl->setCurrentCell(0,0);
1124       currentCellChanged(); // to update buttons
1125     }
1126   }
1127   // show dialog and wait, becase Compute can be invoked from Preview operation
1128   //aCompDlg->exec(); // this way it becomes modal - impossible to rotate model in the Viewer
1129   aCompDlg->show();
1130 }
1131
1132 //================================================================================
1133 /*!
1134  * \brief Stops operation
1135  */
1136 //================================================================================
1137
1138 void SMESHGUI_BaseComputeOp::stopOperation()
1139 {
1140   SMESHGUI_Operation::stopOperation();
1141   if ( myTShapeDisplayer )
1142     myTShapeDisplayer->SetVisibility( false );
1143   if ( myBadMeshDisplayer ) {
1144     myBadMeshDisplayer->SetVisibility( false );
1145     // delete it in order not to have problems at its destruction when the viewer
1146     // where it worked is dead due to e.g. study closing
1147     delete myBadMeshDisplayer;
1148     myBadMeshDisplayer = 0;
1149   }
1150   myIObject.Nullify();
1151 }
1152
1153 //================================================================================
1154 /*!
1155  * \brief publish selected sub-shape
1156  */
1157 //================================================================================
1158
1159 void SMESHGUI_BaseComputeOp::onPublishShape()
1160 {
1161   GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1162   SALOMEDS::Study_var study = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
1163
1164   QList<int> rows;
1165   SMESH::getSelectedRows( table(), rows );
1166   int row;
1167   foreach ( row, rows )
1168   {
1169     int curSub = table()->item(row, COL_SHAPEID)->text().toInt();
1170     GEOM::GEOM_Object_wrap shape = SMESH::getSubShape( curSub, myMainShape );
1171     if ( !shape->_is_nil() && ! SMESH::getSubShapeSO( curSub, myMainShape ))
1172     {
1173       if ( !SMESH::getSubShapeSO( 1, myMainShape )) // the main shape not published
1174       {
1175         QString name = GEOMBase::GetDefaultName( SMESH::shapeTypeName( myMainShape, "MAIN_SHAPE" ));
1176         SALOMEDS::SObject_wrap so =
1177           geomGen->AddInStudy( study, myMainShape, name.toLatin1().data(), GEOM::GEOM_Object::_nil());
1178         // look for myMainShape in the table
1179         for ( int r = 0, nr = table()->rowCount(); r < nr; ++r ) {
1180           if ( table()->item( r, COL_SHAPEID )->text() == "1" ) {
1181             if ( so->_is_nil() ) {
1182               CORBA::String_var name  = so->GetName();
1183               CORBA::String_var entry = so->GetID();
1184               table()->item( r, COL_SHAPE     )->setText( name.in() );
1185               table()->item( r, COL_PUBLISHED )->setText( entry.in() );
1186             }
1187             break;
1188           }
1189         }
1190         if ( curSub == 1 ) continue;
1191       }
1192       QString name = GEOMBase::GetDefaultName( SMESH::shapeTypeName( shape, "ERROR_SHAPE" ));
1193       SALOMEDS::SObject_wrap so = geomGen->AddInStudy( study, shape, name.toLatin1().data(), myMainShape);
1194       if ( !so->_is_nil() ) {
1195         CORBA::String_var name  = so->GetName();
1196         CORBA::String_var entry = so->GetID();
1197         table()->item( row, COL_SHAPE     )->setText( name.in() );
1198         table()->item( row, COL_PUBLISHED )->setText( entry.in() );
1199       }
1200     }
1201   }
1202   getSMESHGUI()->getApp()->updateObjectBrowser();
1203   currentCellChanged(); // to update buttons
1204 }
1205
1206 //================================================================================
1207 /*!
1208  * \brief show mesh elements preventing computation of a submesh of current row
1209  */
1210 //================================================================================
1211
1212 void SMESHGUI_BaseComputeOp::onShowBadMesh()
1213 {
1214   myTShapeDisplayer->SetVisibility( false );
1215   QList<int> rows;
1216   if ( SMESH::getSelectedRows( table(), rows ) == 1 ) {
1217     bool hasBadMesh = ( !table()->item(rows.front(), COL_BAD_MESH)->text().isEmpty() );
1218     if ( hasBadMesh ) {
1219       int curSub = table()->item(rows.front(), COL_SHAPEID)->text().toInt();
1220       SMESHGUI* gui = getSMESHGUI();
1221       SMESH::SMESH_Gen_var gen = gui->GetSMESHGen();
1222       SVTK_ViewWindow*    view = SMESH::GetViewWindow( gui );
1223       if ( myBadMeshDisplayer ) delete myBadMeshDisplayer;
1224       myBadMeshDisplayer = new SMESHGUI_MeshEditPreview( view );
1225       SMESH::MeshPreviewStruct_var aMeshData = gen->GetBadInputElements(myMesh,curSub);
1226       double aPointSize = SMESH::GetFloat("SMESH:node_size",3);
1227       double aLineWidth = SMESH::GetFloat("SMESH:element_width",1);
1228       vtkProperty* prop = vtkProperty::New();
1229       prop->SetLineWidth( aLineWidth * 3 );
1230       prop->SetPointSize( aPointSize * 3 );
1231       prop->SetColor( 250, 0, 250 );
1232       myBadMeshDisplayer->GetActor()->SetProperty( prop );
1233       myBadMeshDisplayer->SetData( aMeshData._retn() );
1234       prop->Delete();
1235     }
1236   }
1237 }
1238
1239 //================================================================================
1240 /*!
1241  * \brief create groups of bad mesh elements preventing computation of a submesh of current row
1242  */
1243 //================================================================================
1244
1245 void SMESHGUI_BaseComputeOp::onGroupOfBadMesh()
1246 {
1247   QList<int> rows;
1248   SMESH::getSelectedRows( table(), rows );
1249   int row;
1250   foreach ( row, rows )
1251   {
1252     bool hasBadMesh = ( !table()->item(row, COL_BAD_MESH)->text().isEmpty() );
1253     if ( hasBadMesh ) {
1254       int     curSub = table()->item(rows.front(), COL_SHAPEID)->text().toInt();
1255       QString grName = table()->item(rows.front(), COL_SHAPE)->text();
1256       if ( grName.isEmpty() ) grName = "bad mesh";
1257       else                    grName = "bad mesh of " + grName;
1258       SMESH::SMESH_Gen_var gen = getSMESHGUI()->GetSMESHGen();
1259       SMESH::ListOfGroups_var groups
1260         ( gen->MakeGroupsOfBadInputElements(myMesh,curSub,grName.toLatin1().data()) );
1261       update( UF_ObjBrowser | UF_Model );
1262       if( LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( application() ))
1263       {
1264         QStringList anEntryList;
1265         for ( size_t i = 0; i < groups->length(); ++i )
1266           if ( _PTR(SObject) so = SMESH::FindSObject( groups[i] ))
1267             anEntryList.append( so->GetID().c_str() );
1268
1269         if ( !anEntryList.isEmpty())
1270           anApp->browseObjects( anEntryList, true, false );
1271       }
1272     }
1273   }
1274 }
1275
1276 //================================================================================
1277 /*!
1278  * \brief SLOT called when a selected cell in table() changed
1279  */
1280 //================================================================================
1281
1282 void SMESHGUI_BaseComputeOp::currentCellChanged()
1283 {
1284   myTShapeDisplayer->SetVisibility( false );
1285   if ( myBadMeshDisplayer )
1286     myBadMeshDisplayer->SetVisibility( false );
1287
1288   bool publishEnable = 0, showEnable = 0, showOnly = 1, hasBadMesh = 0;
1289   QList<int> rows;
1290   int nbSelected = SMESH::getSelectedRows( table(), rows );
1291   int row;
1292   foreach ( row, rows )
1293   {
1294     bool hasData     = ( !table()->item( row, COL_SHAPE )->text().isEmpty() );
1295     bool isPublished = ( !table()->item( row, COL_PUBLISHED )->text().isEmpty() );
1296     if ( hasData && !isPublished )
1297       publishEnable = true;
1298
1299     int curSub = table()->item( row, COL_SHAPEID )->text().toInt();
1300     bool prsReady = myTShapeDisplayer->HasReadyActorsFor( curSub, myMainShape );
1301     if ( prsReady ) {
1302       myTShapeDisplayer->Show( curSub, myMainShape, showOnly );
1303       showOnly = false;
1304     }
1305     else {
1306       showEnable = true;
1307     }
1308
1309     if ( !table()->item(row, COL_BAD_MESH)->text().isEmpty() )
1310       hasBadMesh = true;
1311   }
1312   myCompDlg->myPublishBtn->setEnabled( publishEnable );
1313   myCompDlg->myShowBtn   ->setEnabled( showEnable );
1314   myCompDlg->myBadMeshBtn->setEnabled( hasBadMesh && ( nbSelected == 1 ));
1315 }
1316
1317 //================================================================================
1318 /*!
1319  * \brief update preview
1320  */
1321 //================================================================================
1322
1323 void SMESHGUI_BaseComputeOp::onPreviewShape()
1324 {
1325   if ( myTShapeDisplayer )
1326   {
1327     SUIT_OverrideCursor aWaitCursor;
1328     QList<int> rows;
1329     SMESH::getSelectedRows( table(), rows );
1330
1331     bool showOnly = true;
1332     int row;
1333     foreach ( row, rows )
1334     {
1335       int curSub = table()->item( row, COL_SHAPEID )->text().toInt();
1336       if ( curSub > 0 ) {
1337         myTShapeDisplayer->Show( curSub, myMainShape, showOnly );
1338         showOnly = false;
1339       }
1340     }
1341     currentCellChanged(); // to update buttons
1342   }
1343 }
1344
1345 //================================================================================
1346 /*!
1347  * \brief Destructor
1348  */
1349 //================================================================================
1350
1351 SMESHGUI_BaseComputeOp::~SMESHGUI_BaseComputeOp()
1352 {
1353   delete myCompDlg;
1354   myCompDlg = 0;
1355   delete myTShapeDisplayer;
1356   if ( myBadMeshDisplayer )
1357     delete myBadMeshDisplayer;
1358 }
1359
1360 //================================================================================
1361 /*!
1362  * \brief Gets dialog of compute operation
1363  * \retval SMESHGUI_ComputeDlg* - pointer to dialog of this operation
1364  */
1365 //================================================================================
1366
1367 SMESHGUI_ComputeDlg* SMESHGUI_BaseComputeOp::computeDlg() const
1368 {
1369   if ( !myCompDlg )
1370   {
1371     SMESHGUI_BaseComputeOp* me = (SMESHGUI_BaseComputeOp*)this;
1372     me->myCompDlg = new SMESHGUI_ComputeDlg( desktop(), false );
1373     // connect signals and slots
1374     connect(myCompDlg->myShowBtn,           SIGNAL (clicked()), SLOT(onPreviewShape()));
1375     connect(myCompDlg->myPublishBtn,        SIGNAL (clicked()), SLOT(onPublishShape()));
1376     connect(myCompDlg->myBadMeshBtn,        SIGNAL (clicked()), SLOT(onShowBadMesh()));
1377     connect(myCompDlg->myBadMeshToGroupBtn, SIGNAL (clicked()), SLOT(onGroupOfBadMesh()));
1378
1379     QTableWidget* aTable = me->table();
1380     connect(aTable, SIGNAL(itemSelectionChanged()), SLOT(currentCellChanged()));
1381     connect(aTable, SIGNAL(currentCellChanged(int,int,int,int)), SLOT(currentCellChanged()));
1382   }
1383   return myCompDlg;
1384 }
1385
1386 //================================================================================
1387 /*!
1388  * \brief returns from compute mesh result dialog
1389  */
1390 //================================================================================
1391
1392 bool SMESHGUI_BaseComputeOp::onApply()
1393 {
1394   return true;
1395 }
1396
1397 //================================================================================
1398 /*!
1399  * \brief Return a table
1400  */
1401 //================================================================================
1402
1403 QTableWidget* SMESHGUI_BaseComputeOp::table()
1404 {
1405   return myCompDlg->myTable;
1406 }
1407
1408
1409 //================================================================================
1410 /*!
1411  * \brief Constructor
1412 */
1413 //================================================================================
1414
1415 SMESHGUI_ComputeOp::SMESHGUI_ComputeOp()
1416  : SMESHGUI_BaseComputeOp()
1417 {
1418 }
1419
1420
1421 //================================================================================
1422 /*!
1423  * \brief Desctructor
1424 */
1425 //================================================================================
1426
1427 SMESHGUI_ComputeOp::~SMESHGUI_ComputeOp()
1428 {
1429 }
1430
1431 //================================================================================
1432 /*!
1433  * \brief perform it's intention action: compute mesh
1434  */
1435 //================================================================================
1436
1437 void SMESHGUI_ComputeOp::startOperation()
1438 {
1439   SMESHGUI_BaseComputeOp::startOperation();
1440   if (myMesh->_is_nil())
1441     return;
1442   computeMesh();
1443 }
1444
1445 //================================================================================
1446 /*!
1447  * \brief check the same operations on the same mesh
1448  */
1449 //================================================================================
1450
1451 bool SMESHGUI_BaseComputeOp::isValid(  SUIT_Operation* theOp  ) const
1452 {
1453   SMESHGUI_BaseComputeOp* baseOp = dynamic_cast<SMESHGUI_BaseComputeOp*>( theOp );
1454   bool ret = true;
1455   if ( !myMesh->_is_nil() && baseOp ) {
1456     SMESH::SMESH_Mesh_var aMesh = baseOp->getMesh();
1457     if ( !aMesh->_is_nil() && aMesh->GetId() == myMesh->GetId() ) ret = false;
1458   }
1459   return ret;
1460 }
1461
1462 //================================================================================
1463 /*!
1464  * \brief Gets dialog of this operation
1465  * \retval LightApp_Dialog* - pointer to dialog of this operation
1466  */
1467 //================================================================================
1468
1469 LightApp_Dialog* SMESHGUI_ComputeOp::dlg() const
1470 {
1471   return computeDlg();
1472 }
1473
1474 //================================================================================
1475 /*!
1476  * \brief Constructor
1477 */
1478 //================================================================================
1479
1480 SMESHGUI_PrecomputeOp::SMESHGUI_PrecomputeOp()
1481  : SMESHGUI_BaseComputeOp(),
1482  myDlg( 0 ),
1483  myOrderMgr( 0 ),
1484  myActiveDlg( 0 ),
1485  myPreviewDisplayer( 0 )
1486 {
1487   myHelpFileName = "constructing_meshes_page.html#preview_mesh_anchor";
1488 }
1489
1490 //================================================================================
1491 /*!
1492  * \brief Destructor
1493  */
1494 //================================================================================
1495
1496 SMESHGUI_PrecomputeOp::~SMESHGUI_PrecomputeOp()
1497 {
1498   delete myDlg;
1499   myDlg = 0;
1500   delete myOrderMgr;
1501   myOrderMgr = 0;
1502   myActiveDlg = 0;
1503   if ( myPreviewDisplayer )
1504     delete myPreviewDisplayer;
1505   myPreviewDisplayer = 0;
1506 }
1507
1508 //================================================================================
1509 /*!
1510  * \brief Gets current dialog of this operation
1511  * \retval LightApp_Dialog* - pointer to dialog of this operation
1512  */
1513 //================================================================================
1514
1515 LightApp_Dialog* SMESHGUI_PrecomputeOp::dlg() const
1516 {
1517   return myActiveDlg;
1518 }
1519
1520 //================================================================================
1521 /*!
1522  * \brief perform it's intention action: prepare data
1523  */
1524 //================================================================================
1525
1526 void SMESHGUI_PrecomputeOp::startOperation()
1527 {
1528   if ( !myDlg )
1529   {
1530     myDlg = new SMESHGUI_PrecomputeDlg( desktop() );
1531     
1532     // connect signals
1533     connect( myDlg, SIGNAL( preview() ), this, SLOT( onPreview() ) );
1534     connect( myDlg, SIGNAL( dlgOk() ), this, SLOT( onCompute() ) );
1535     connect( myDlg, SIGNAL( dlgApply() ), this, SLOT( onCompute() ) );
1536   }
1537   myActiveDlg = myDlg;
1538
1539   // connect signal to compute dialog. which will be shown after Compute mesh operation
1540   SMESHGUI_ComputeDlg* cmpDlg = computeDlg();
1541   if ( cmpDlg )
1542   {
1543     // disconnect signals
1544     disconnect( cmpDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1545     disconnect( cmpDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1546     disconnect( cmpDlg, SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
1547     disconnect( cmpDlg, SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
1548     disconnect( cmpDlg, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
1549
1550     // connect signals
1551     if( cmpDlg->testButtonFlags( QtxDialog::OK ) )
1552       connect( cmpDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1553     if( cmpDlg->testButtonFlags( QtxDialog::Apply ) )
1554       connect( cmpDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1555     if( cmpDlg->testButtonFlags( QtxDialog::Help ) )
1556       connect( cmpDlg, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
1557     if( cmpDlg->testButtonFlags( QtxDialog::Cancel ) )
1558       connect( cmpDlg, SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
1559     if( cmpDlg->testButtonFlags( QtxDialog::Close ) )
1560       connect( cmpDlg, SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
1561   }
1562
1563   SMESHGUI_BaseComputeOp::startOperation();
1564   if (myMesh->_is_nil())
1565     return;
1566
1567   if (myDlg->getPreviewMode() == -1)
1568   {
1569     // nothing to preview
1570     SUIT_MessageBox::warning(desktop(),
1571                              tr("SMESH_WRN_WARNING"),
1572                              tr("SMESH_WRN_NOTHING_PREVIEW"));
1573     onCancel();
1574     return;
1575   }
1576
1577   // disconnect slot from preview dialog to have Apply from results of compute operation only 
1578   disconnect( myDlg, SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
1579   disconnect( myDlg, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
1580
1581   myDlg->show();
1582 }
1583
1584 //================================================================================
1585 /*!
1586  * \brief Stops operation
1587  */
1588 //================================================================================
1589
1590 void SMESHGUI_PrecomputeOp::stopOperation()
1591 {
1592   if ( myPreviewDisplayer )
1593   {
1594     myPreviewDisplayer->SetVisibility( false );
1595     delete myPreviewDisplayer;
1596     myPreviewDisplayer = 0;
1597   }
1598   myMapShapeId.clear();
1599   SMESHGUI_BaseComputeOp::stopOperation();
1600 }
1601
1602 //================================================================================
1603 /*!
1604  * \brief reinitialize dialog after operaiton become active again
1605  */
1606 //================================================================================
1607
1608 void SMESHGUI_PrecomputeOp::resumeOperation()
1609 {
1610   if ( myActiveDlg == myDlg )
1611     initDialog();
1612   SMESHGUI_BaseComputeOp::resumeOperation();
1613 }
1614
1615 //================================================================================
1616 /*!
1617  * \brief perform it's intention action: reinitialise dialog
1618  */
1619 //================================================================================
1620
1621 void SMESHGUI_PrecomputeOp::initDialog()
1622 {
1623   QList<int> modes;
1624
1625   QMap<int, int> modeMap;
1626   _PTR(SObject)  pMesh = studyDS()->FindObjectID( myIObject->getEntry() );
1627   getAssignedAlgos( pMesh, modeMap );
1628   if ( modeMap.contains( SMESH::DIM_3D ) )
1629   {
1630     if ( modeMap.contains( SMESH::DIM_2D ) )
1631       modes.append( SMESH::DIM_2D );
1632     if ( modeMap.contains( SMESH::DIM_1D ) )
1633       modes.append( SMESH::DIM_1D );
1634   }
1635   else if ( modeMap.contains( SMESH::DIM_2D ) )
1636   {
1637     if ( modeMap.contains( SMESH::DIM_1D ) )
1638       modes.append( SMESH::DIM_1D );
1639   }
1640
1641   myOrderMgr = new SMESHGUI_MeshOrderMgr( myDlg->getMeshOrderBox() );
1642   myOrderMgr->SetMesh( myMesh );
1643   bool isOrder = myOrderMgr->GetMeshOrder(myPrevOrder);
1644   myDlg->getMeshOrderBox()->setShown(isOrder);
1645   if ( !isOrder ) {
1646     delete myOrderMgr;
1647     myOrderMgr = 0;
1648   }
1649
1650   myDlg->setPreviewModes( modes );
1651 }
1652
1653 //================================================================================
1654 /*!
1655  * \brief detect asigned mesh algorithms
1656  */
1657 //================================================================================
1658
1659 void SMESHGUI_PrecomputeOp::getAssignedAlgos(_PTR(SObject) theMesh,
1660                                              QMap<int,int>& theModeMap)
1661 {
1662   _PTR(SObject)          aHypRoot;
1663   _PTR(GenericAttribute) anAttr;
1664   int aPart = SMESH::Tag_RefOnAppliedAlgorithms;
1665   if ( theMesh && theMesh->FindSubObject( aPart, aHypRoot ) )
1666   {
1667     _PTR(ChildIterator) anIter =
1668       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
1669     for ( ; anIter->More(); anIter->Next() )
1670     {
1671       _PTR(SObject) anObj = anIter->Value();
1672       _PTR(SObject) aRefObj;
1673       if ( anObj->ReferencedObject( aRefObj ) )
1674         anObj = aRefObj;
1675       else
1676         continue;
1677       
1678       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
1679       {
1680         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
1681         if ( CORBA::is_nil( aVar ) )
1682           continue;
1683         
1684         for( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1685         {
1686           SMESH::SMESH_Algo_var algo;
1687           switch(dim) {
1688           case SMESH::DIM_1D: algo = SMESH::SMESH_1D_Algo::_narrow( aVar ); break;
1689           case SMESH::DIM_2D: algo = SMESH::SMESH_2D_Algo::_narrow( aVar ); break;
1690           case SMESH::DIM_3D: algo = SMESH::SMESH_3D_Algo::_narrow( aVar ); break;
1691           default: break;
1692           }
1693           if ( !algo->_is_nil() )
1694             theModeMap[ dim ] = 0;
1695         }
1696       }
1697     }
1698   }
1699 }
1700
1701 //================================================================================
1702 /*!
1703  * \brief perform it's intention action: compute mesh
1704  */
1705 //================================================================================
1706
1707 void SMESHGUI_PrecomputeOp::onCompute()
1708 {
1709   myDlg->hide();
1710   if (myOrderMgr && myOrderMgr->IsOrderChanged())
1711     myOrderMgr->SetMeshOrder();
1712   myMapShapeId.clear();
1713   myActiveDlg = computeDlg();
1714   computeMesh();
1715 }
1716
1717 //================================================================================
1718 /*!
1719  * \brief perform it's intention action: compute mesh
1720  */
1721 //================================================================================
1722
1723 void SMESHGUI_PrecomputeOp::onCancel()
1724 {
1725   QObject* curDlg = sender();
1726   if ( curDlg == computeDlg() && myActiveDlg == myDlg )
1727   {
1728     // return from error messages
1729     myDlg->show();
1730     return;
1731   }
1732
1733   bool isRestoreOrder = false;
1734   if ( myActiveDlg == myDlg  && !myMesh->_is_nil() && myMapShapeId.count() )
1735   {
1736     // ask to remove already computed mesh elements
1737     if ( SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
1738                                     tr( "CLEAR_SUBMESH_QUESTION" ),
1739                                     tr( "SMESH_BUT_DELETE" ), tr( "SMESH_BUT_NO" ), 0, 1 ) == 0 )
1740     {
1741       // remove all submeshes for collected shapes
1742       QMap<int,int>::const_iterator it = myMapShapeId.constBegin();
1743       for ( ; it != myMapShapeId.constEnd(); ++it )
1744         myMesh->ClearSubMesh( *it );
1745       isRestoreOrder = true;
1746     }
1747   }
1748
1749   // return previous mesh order
1750   if (myOrderMgr && myOrderMgr->IsOrderChanged()) {
1751     if (!isRestoreOrder)
1752       isRestoreOrder = 
1753         (SUIT_MessageBox::question( desktop(), tr( "SMESH_WARNING" ),
1754                                     tr( "SMESH_REJECT_MESH_ORDER" ),
1755                                     tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 ) == 0);
1756     if (isRestoreOrder)
1757       myOrderMgr->SetMeshOrder(myPrevOrder);
1758   }
1759
1760   delete myOrderMgr;
1761   myOrderMgr = 0;
1762
1763   myMapShapeId.clear();
1764   SMESHGUI_BaseComputeOp::onCancel();
1765 }
1766
1767 //================================================================================
1768 /*!
1769  * \brief perform it's intention action: preview mesh
1770  */
1771 //================================================================================
1772
1773 void SMESHGUI_PrecomputeOp::onPreview()
1774 {
1775   if ( !myDlg || myMesh->_is_nil() || myMainShape->_is_nil() )
1776     return;
1777
1778   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
1779   if ( !aMeshSObj )
1780     return;
1781
1782   // set modified submesh priority if any
1783   if (myOrderMgr && myOrderMgr->IsOrderChanged())
1784     myOrderMgr->SetMeshOrder();
1785
1786   // Compute preview of mesh, 
1787   // i.e. compute mesh till indicated dimension
1788   int dim = myDlg->getPreviewMode();
1789   
1790   SMESH::MemoryReserve aMemoryReserve;
1791   
1792   SMESH::compute_error_array_var aCompErrors;
1793   QString                        aHypErrors;
1794
1795   bool computeFailed = true, memoryLack = false;
1796
1797   SMESHGUI_ComputeDlg* aCompDlg = computeDlg();
1798     aCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
1799
1800   SMESHGUI* gui = getSMESHGUI();
1801   SMESH::SMESH_Gen_var gen = gui->GetSMESHGen();
1802   SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
1803   if ( errors->length() > 0 ) {
1804     aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
1805   }
1806
1807   SUIT_OverrideCursor aWaitCursor;
1808
1809   SVTK_ViewWindow*    view = SMESH::GetViewWindow( gui );
1810   if ( myPreviewDisplayer ) delete myPreviewDisplayer;
1811   myPreviewDisplayer = new SMESHGUI_MeshEditPreview( view );
1812   
1813   SMESH::long_array_var aShapesId = new SMESH::long_array();
1814   try {
1815 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1816     OCC_CATCH_SIGNALS;
1817 #endif
1818       
1819     SMESH::MeshPreviewStruct_var previewData =
1820       gen->Precompute(myMesh, myMainShape, (SMESH::Dimension)dim, aShapesId);
1821
1822     SMESH::MeshPreviewStruct* previewRes = previewData._retn();
1823     if ( previewRes && previewRes->nodesXYZ.length() > 0 )
1824     {
1825       computeFailed = false;
1826       myPreviewDisplayer->SetData( previewRes );
1827       // append shape indeces with computed mesh entities
1828       for ( int i = 0, n = aShapesId->length(); i < n; i++ )
1829         myMapShapeId[ aShapesId[ i ] ] = 0;
1830     }
1831     else
1832       myPreviewDisplayer->SetVisibility(false);
1833   }
1834   catch(const SALOME::SALOME_Exception & S_ex){
1835     memoryLack = true;
1836     myPreviewDisplayer->SetVisibility(false);
1837   }
1838
1839   try {
1840 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1841     OCC_CATCH_SIGNALS;
1842 #endif
1843     aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
1844     // check if there are memory problems
1845     for ( int i = 0; (i < aCompErrors->length()) && !memoryLack; ++i )
1846       memoryLack = ( aCompErrors[ i ].code == SMESH::COMPERR_MEMORY_PB );
1847   }
1848   catch(const SALOME::SALOME_Exception & S_ex){
1849     memoryLack = true;
1850   }
1851
1852   if ( memoryLack )
1853     aMemoryReserve.release();
1854
1855   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
1856   bool noHypoError = ( aHypErrors.isEmpty() );
1857
1858   SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( gui );
1859   int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
1860
1861   bool isShowError = true;
1862   switch( aNotifyMode ) {
1863   case 0: // show the mesh computation result dialog NEVER
1864     isShowError = false;
1865     break;
1866   case 1: // show the mesh computation result dialog if there are some errors
1867   default: // show the result dialog after each mesh computation
1868     if ( !computeFailed && !memoryLack && noCompError && noHypoError )
1869       isShowError = false;
1870     break;
1871   }
1872
1873   aWaitCursor.suspend();
1874   // SHOW ERRORS
1875   if ( isShowError )
1876   {
1877     myDlg->hide();
1878     aCompDlg->setWindowTitle(tr( computeFailed ? "SMESH_WRN_COMPUTE_FAILED" : "SMESH_COMPUTE_SUCCEED"));
1879     showComputeResult( memoryLack, noCompError, aCompErrors, noHypoError, aHypErrors );
1880   }
1881 }
1882
1883
1884 //================================================================================
1885 /*!
1886  * \brief Constructor
1887 */
1888 //================================================================================
1889
1890 SMESHGUI_PrecomputeDlg::SMESHGUI_PrecomputeDlg( QWidget* parent )
1891  : SMESHGUI_Dialog( parent, false, false, OK | Cancel | Help ),
1892    myOrderBox(0)
1893 {
1894   setWindowTitle( tr( "CAPTION" ) );
1895
1896   setButtonText( OK, tr( "COMPUTE" ) );
1897   QFrame* main = mainFrame();
1898
1899   QVBoxLayout* layout = new QVBoxLayout( main );
1900
1901   myOrderBox = new SMESHGUI_MeshOrderBox( main );
1902   layout->addWidget(myOrderBox);
1903
1904   QFrame* frame = new QFrame( main );
1905   layout->setMargin(0); layout->setSpacing(0);
1906   layout->addWidget( frame );
1907
1908   QHBoxLayout* frameLay = new QHBoxLayout( frame );
1909   frameLay->setMargin(0); frameLay->setSpacing(SPACING);
1910   
1911   myPreviewMode = new QtxComboBox( frame );
1912   frameLay->addWidget( myPreviewMode );
1913
1914   myPreviewBtn = new QPushButton( tr( "PREVIEW" ), frame );
1915   frameLay->addWidget( myPreviewBtn );
1916
1917   connect( myPreviewBtn, SIGNAL( clicked( bool ) ), this, SIGNAL( preview() ) );
1918 }
1919
1920 //================================================================================
1921 /*!
1922  * \brief Destructor
1923 */
1924 //================================================================================
1925
1926 SMESHGUI_PrecomputeDlg::~SMESHGUI_PrecomputeDlg()
1927 {
1928 }
1929
1930 //================================================================================
1931 /*!
1932  * \brief Sets available preview modes
1933 */
1934 //================================================================================
1935
1936 void SMESHGUI_PrecomputeDlg::setPreviewModes( const QList<int>& theModes )
1937 {
1938   myPreviewMode->clear();
1939   QList<int>::const_iterator it = theModes.constBegin();
1940   for ( int i = 0; it != theModes.constEnd(); ++it, i++ )
1941   {
1942     QString mode = QString( "PREVIEW_%1" ).arg( *it );
1943     myPreviewMode->addItem( tr( mode.toLatin1().data() ) );
1944     myPreviewMode->setId( i, *it );
1945   }
1946   myPreviewBtn->setEnabled( !theModes.isEmpty() );
1947 }
1948
1949 //================================================================================
1950 /*!
1951  * \brief Returns current preview mesh mode
1952 */
1953 //================================================================================
1954
1955 int SMESHGUI_PrecomputeDlg::getPreviewMode() const
1956 {
1957   return myPreviewMode->currentId();
1958 }
1959
1960 //================================================================================
1961 /*!
1962  * \brief Returns current preview mesh mode
1963 */
1964 //================================================================================
1965
1966 SMESHGUI_MeshOrderBox* SMESHGUI_PrecomputeDlg::getMeshOrderBox() const
1967 {
1968   return myOrderBox;
1969 }
1970
1971
1972 //================================================================================
1973 /*!
1974  * \brief Constructor
1975 */
1976 //================================================================================
1977
1978 SMESHGUI_EvaluateOp::SMESHGUI_EvaluateOp()
1979  : SMESHGUI_BaseComputeOp()
1980 {
1981 }
1982
1983
1984 //================================================================================
1985 /*!
1986  * \brief Desctructor
1987 */
1988 //================================================================================
1989
1990 SMESHGUI_EvaluateOp::~SMESHGUI_EvaluateOp()
1991 {
1992 }
1993
1994 //================================================================================
1995 /*!
1996  * \brief perform it's intention action: compute mesh
1997  */
1998 //================================================================================
1999
2000 void SMESHGUI_EvaluateOp::startOperation()
2001 {
2002   SMESHGUI_BaseComputeOp::evaluateDlg();
2003   SMESHGUI_BaseComputeOp::startOperation();
2004   if (myMesh->_is_nil())
2005     return;
2006   evaluateMesh();
2007 }
2008
2009 //================================================================================
2010 /*!
2011  * \brief Gets dialog of this operation
2012  * \retval LightApp_Dialog* - pointer to dialog of this operation
2013  */
2014 //================================================================================
2015
2016 LightApp_Dialog* SMESHGUI_EvaluateOp::dlg() const
2017 {
2018   return evaluateDlg();
2019 }
2020
2021 //================================================================================
2022 /*!
2023  * \brief evaluateMesh()
2024 */
2025 //================================================================================
2026
2027 void SMESHGUI_BaseComputeOp::evaluateMesh()
2028 {
2029   // EVALUATE MESH
2030
2031   SMESH::MemoryReserve aMemoryReserve;
2032
2033   SMESH::compute_error_array_var aCompErrors;
2034   QString                        aHypErrors;
2035
2036   bool evaluateFailed = true, memoryLack = false;
2037   SMESH::long_array_var aRes;
2038
2039   _PTR(SObject) aMeshSObj = SMESH::FindSObject(myMesh);
2040   if ( !aMeshSObj ) //  IPAL21340
2041     return;
2042
2043   bool hasShape = myMesh->HasShapeToMesh();
2044   bool shapeOK = myMainShape->_is_nil() ? !hasShape : hasShape;
2045   if ( shapeOK )
2046   {
2047     myCompDlg->myMeshName->setText( aMeshSObj->GetName().c_str() );
2048     SMESH::SMESH_Gen_var gen = getSMESHGUI()->GetSMESHGen();
2049     SMESH::algo_error_array_var errors = gen->GetAlgoState(myMesh,myMainShape);
2050     if ( errors->length() > 0 ) {
2051       aHypErrors = SMESH::GetMessageOnAlgoStateErrors( errors.in() );
2052     }
2053     SUIT_OverrideCursor aWaitCursor;
2054     try {
2055 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2056       OCC_CATCH_SIGNALS;
2057 #endif
2058       aRes = gen->Evaluate(myMesh, myMainShape);
2059     }
2060     catch(const SALOME::SALOME_Exception & S_ex){
2061       memoryLack = true;
2062     }
2063
2064     try {
2065 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2066       OCC_CATCH_SIGNALS;
2067 #endif
2068       aCompErrors = gen->GetComputeErrors( myMesh, myMainShape );
2069     }
2070     catch(const SALOME::SALOME_Exception & S_ex){
2071       memoryLack = true;
2072     }
2073   }
2074
2075   if ( memoryLack )
2076     aMemoryReserve.release();
2077
2078   evaluateFailed =  ( aCompErrors->length() > 0 );
2079   myCompDlg->setWindowTitle(tr( evaluateFailed ? "SMESH_WRN_EVALUATE_FAILED" : "SMESH_EVALUATE_SUCCEED"));
2080
2081   // SHOW ERRORS
2082   
2083   bool noCompError = ( !aCompErrors.operator->() || aCompErrors->length() == 0 );
2084   bool noHypoError = ( aHypErrors.isEmpty() );
2085
2086   //SUIT_ResourceMgr* resMgr = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
2087   //int aNotifyMode = resMgr->integerValue( "SMESH", "show_result_notification" );
2088
2089   bool isShowResultDlg = true;
2090   //if( noHypoError )
2091   //switch( aNotifyMode ) {
2092   //case 0: // show the mesh computation result dialog NEVER
2093   //isShowResultDlg = false;
2094   //commit();
2095   //break;
2096   //case 1: // show the mesh computation result dialog if there are some errors
2097   //if ( memoryLack || !noHypoError )
2098   //  isShowResultDlg = true;
2099   //else
2100   //{
2101   //  isShowResultDlg = false;
2102   //  commit();
2103   //}
2104   //break;
2105   //default: // show the result dialog after each mesh computation
2106   //isShowResultDlg = true;
2107   //}
2108
2109   // SHOW RESULTS
2110   if ( isShowResultDlg )
2111     showEvaluateResult( aRes, memoryLack, noCompError, aCompErrors,
2112                         noHypoError, aHypErrors);
2113 }
2114
2115
2116 void SMESHGUI_BaseComputeOp::showEvaluateResult(const SMESH::long_array& theRes,
2117                                                 const bool theMemoryLack,
2118                                                 const bool theNoCompError,
2119                                                 SMESH::compute_error_array_var& theCompErrors,
2120                                                 const bool theNoHypoError,
2121                                                 const QString& theHypErrors)
2122 {
2123   bool hasShape = myMesh->HasShapeToMesh();
2124   SMESHGUI_ComputeDlg* aCompDlg = evaluateDlg();
2125   aCompDlg->myMemoryLackGroup->hide();
2126
2127   if ( theMemoryLack )
2128   {
2129     aCompDlg->myMemoryLackGroup->show();
2130     aCompDlg->myFullInfo->hide();
2131     aCompDlg->myBriefInfo->hide();
2132     aCompDlg->myHypErrorGroup->hide();
2133     aCompDlg->myCompErrorGroup->hide();
2134   }
2135   else if ( theNoCompError && theNoHypoError )
2136   {
2137     aCompDlg->myFullInfo->SetMeshInfo( theRes );
2138     aCompDlg->myFullInfo->show();
2139     aCompDlg->myBriefInfo->hide();
2140     aCompDlg->myHypErrorGroup->hide();
2141     aCompDlg->myCompErrorGroup->hide();
2142   }
2143   else
2144   {
2145     QTableWidget* tbl = aCompDlg->myTable;
2146     aCompDlg->myBriefInfo->SetMeshInfo( theRes );
2147     aCompDlg->myBriefInfo->show();
2148     aCompDlg->myFullInfo->hide();
2149
2150     if ( theNoHypoError ) {
2151       aCompDlg->myHypErrorGroup->hide();
2152     }
2153     else {
2154       aCompDlg->myHypErrorGroup->show();
2155       aCompDlg->myHypErrorLabel->setText( theHypErrors );
2156     }
2157
2158     if ( theNoCompError ) {
2159       aCompDlg->myCompErrorGroup->hide();
2160     }
2161     else {
2162       aCompDlg->myCompErrorGroup->show();
2163
2164       aCompDlg->myPublishBtn->hide();
2165       aCompDlg->myShowBtn->hide();
2166
2167       // fill table of errors
2168       tbl->setRowCount( theCompErrors->length() );
2169       if ( !hasShape ) tbl->hideColumn( COL_SHAPE );
2170       else             tbl->showColumn( COL_SHAPE );
2171       tbl->setColumnWidth( COL_ERROR, 200 );
2172
2173       bool hasBadMesh = false;
2174       for ( int row = 0; row < theCompErrors->length(); ++row )
2175       {
2176         SMESH::ComputeError & err = theCompErrors[ row ];
2177
2178         QString text = err.algoName.in();
2179         if ( !tbl->item( row, COL_ALGO ) ) tbl->setItem( row, COL_ALGO, new QTableWidgetItem( text ) );
2180         else tbl->item( row, COL_ALGO )->setText( text );
2181
2182         text = SMESH::errorText( err.code, err.comment.in() );
2183         if ( !tbl->item( row, COL_ERROR ) ) tbl->setItem( row, COL_ERROR, new QTableWidgetItem( text ) );
2184         else tbl->item( row, COL_ERROR )->setText( text );
2185
2186         text = QString("%1").arg( err.subShapeID );
2187         if ( !tbl->item( row, COL_SHAPEID ) ) tbl->setItem( row, COL_SHAPEID, new QTableWidgetItem( text ) );
2188         else tbl->item( row, COL_SHAPEID )->setText( text );
2189
2190         text = hasShape ? SMESH::shapeText( err.subShapeID, myMainShape ) : QString("");
2191         if ( !tbl->item( row, COL_SHAPE ) ) tbl->setItem( row, COL_SHAPE, new QTableWidgetItem( text ) );
2192         else tbl->item( row, COL_SHAPE )->setText( text );
2193
2194         text = ( !hasShape || SMESH::getSubShapeSO( err.subShapeID, myMainShape )) ? "PUBLISHED" : "";
2195         if ( !tbl->item( row, COL_PUBLISHED ) ) tbl->setItem( row, COL_PUBLISHED, new QTableWidgetItem( text ) );
2196         else tbl->item( row, COL_PUBLISHED )->setText( text ); // if text=="", "PUBLISH" button enabled
2197
2198         text = err.hasBadMesh ? "hasBadMesh" : "";
2199         if ( !tbl->item( row, COL_BAD_MESH ) ) tbl->setItem( row, COL_BAD_MESH, new QTableWidgetItem( text ) );
2200         else tbl->item( row, COL_BAD_MESH )->setText( text );
2201         if ( err.hasBadMesh ) hasBadMesh = true;
2202
2203         //tbl->item( row, COL_ERROR )->setWordWrap( true ); // VSR: TODO ???
2204         tbl->resizeRowToContents( row );
2205       }
2206       tbl->resizeColumnToContents( COL_ALGO );
2207       tbl->resizeColumnToContents( COL_SHAPE );
2208       tbl->setWordWrap( true );
2209
2210       if ( hasBadMesh )
2211         aCompDlg->myBadMeshBtn->show();
2212       else
2213         aCompDlg->myBadMeshBtn->hide();
2214
2215       tbl->setCurrentCell(0,0);
2216       currentCellChanged(); // to update buttons
2217     }
2218   }
2219   // show dialog and wait, becase Compute can be invoked from Preview operation
2220   //aCompDlg->exec(); // this way it becomes modal - impossible to rotate model in the Viewer
2221   aCompDlg->show();
2222 }
2223
2224
2225 //================================================================================
2226 /*!
2227  * \brief Gets dialog of evaluate operation
2228  * \retval SMESHGUI_ComputeDlg* - pointer to dialog of this operation
2229  */
2230 //================================================================================
2231
2232 SMESHGUI_ComputeDlg* SMESHGUI_BaseComputeOp::evaluateDlg() const
2233 {
2234   if ( !myCompDlg )
2235   {
2236     SMESHGUI_BaseComputeOp* me = (SMESHGUI_BaseComputeOp*)this;
2237     me->myCompDlg = new SMESHGUI_ComputeDlg( desktop(), true );
2238     // connect signals and slots
2239     connect(myCompDlg->myShowBtn,    SIGNAL (clicked()), SLOT(onPreviewShape()));
2240     connect(myCompDlg->myPublishBtn, SIGNAL (clicked()), SLOT(onPublishShape()));
2241     connect(myCompDlg->myBadMeshBtn, SIGNAL (clicked()), SLOT(onShowBadMesh()));
2242     QTableWidget* aTable = me->table();
2243     connect(aTable, SIGNAL(itemSelectionChanged()), SLOT(currentCellChanged()));
2244     connect(aTable, SIGNAL(currentCellChanged(int,int,int,int)), SLOT(currentCellChanged()));
2245   }
2246   return myCompDlg;
2247 }
2248