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