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