Salome HOME
NPAL16631: EDF281: Suppress automatic update if mesh computation failed because of...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_ComputeDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : SMESHGUI_ComputeDlg.cxx
25 //  Author : Edward AGAPOV
26 //  Module : SMESH
27
28 #include "SMESHGUI_ComputeDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_GEOMGenUtils.h"
32 #include "SMESHGUI_MeshUtils.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESHGUI_HypothesesUtils.h"
36
37 #include "SMDS_SetIterator.hxx"
38
39 #include "GEOMBase.h"
40 #include "GEOM_Actor.h"
41
42 #include "LightApp_SelectionMgr.h"
43 #include "LightApp_UpdateFlags.h"
44 #include "SALOMEDSClient_SObject.hxx"
45 #include "SALOME_ListIO.hxx"
46 #include "SVTK_ViewWindow.h"
47 #include "SVTK_ViewModel.h"
48 #include "SalomeApp_Tools.h"
49 #include "SalomeApp_Application.h"
50 #include "SUIT_ResourceMgr.h"
51 #include "SUIT_OverrideCursor.h"
52 #include "SUIT_MessageBox.h"
53 #include "SUIT_Desktop.h"
54 #include "SUIT_Study.h"
55 #include "OB_Browser.h"
56
57 // OCCT Includes
58 #include <BRep_Tool.hxx>
59 #include <TopExp.hxx>
60 #include <TopExp_Explorer.hxx>
61 #include <TopTools_IndexedMapOfShape.hxx>
62 #include <TopoDS.hxx>
63
64 #include <TopLoc_Location.hxx>
65 #include <Poly_Triangulation.hxx>
66 #include <Bnd_Box.hxx>
67 #include <BRepBndLib.hxx>
68 #include <BRepMesh_IncrementalMesh.hxx>
69
70 // QT Includes
71 #include <qframe.h>
72 #include <qlayout.h>
73 #include <qpushbutton.h>
74 #include <qlabel.h>
75 #include <qbuttongroup.h>
76 #include <qradiobutton.h>
77 #include <qtable.h>
78 #include <qhbox.h>
79 #include <qhgroupbox.h>
80 #include <qvgroupbox.h>
81
82 #include <vtkProperty.h>
83
84 // IDL Headers
85 #include <SALOMEconfig.h>
86 #include CORBA_SERVER_HEADER(SMESH_Gen)
87 #include CORBA_SERVER_HEADER(SMESH_Mesh)
88
89 #include <vector>
90 #include <set>
91
92
93 #define SPACING 5
94 #define MARGIN  10
95
96 #define COLONIZE(str)   (QString(str).contains(":") > 0 ? QString(str) : QString(str) + " :" )
97
98 #define _SEPARATOR(father) \
99 {\
100   /*new QLabel(father); new QLabel(father); new QLabel(father)*/;\
101   (new QFrame(father))->setFrameStyle(QFrame::HLine | QFrame::Sunken);\
102   (new QFrame(father))->setFrameStyle(QFrame::HLine | QFrame::Sunken);\
103   (new QFrame(father))->setFrameStyle(QFrame::HLine | QFrame::Sunken);\
104   (new QFrame(father))->setFrameStyle(QFrame::HLine | QFrame::Sunken);\
105 }
106
107 enum TCol { COL_ALGO = 0, COL_SHAPE, COL_ERROR, COL_SHAPEID, COL_PUBLISHED, NB_COLUMNS };
108
109 using namespace SMESH;
110
111 namespace SMESH {
112   
113   //=============================================================================
114   /*!
115    * \brief Allocate some memory at construction and release it at destruction.
116    * Is used to be able to continue working after mesh generation or visualization
117    * break due to lack of memory
118    */
119   //=============================================================================
120
121   struct MemoryReserve
122   {
123     char* myBuf;
124     MemoryReserve(): myBuf( new char[1024*1024*1] ){} // 1M
125     ~MemoryReserve() { delete [] myBuf; }
126   };
127
128   // =========================================================================================
129   /*!
130    * \brief Class showing shapes without publishing
131    */
132   // =========================================================================================
133
134   class TShapeDisplayer
135   {
136   public:
137     // -----------------------------------------------------------------------
138     TShapeDisplayer(): myViewWindow(0)
139     {
140       myProperty = vtkProperty::New();
141       myProperty->SetRepresentationToWireframe();
142       myProperty->SetColor( 250, 0, 250 );
143       myProperty->SetAmbientColor( 250, 0, 250 );
144       myProperty->SetDiffuseColor( 250, 0, 250 );
145       //myProperty->SetSpecularColor( 250, 0, 250 );
146       myProperty->SetLineWidth( 5 );
147     }
148     // -----------------------------------------------------------------------
149     ~TShapeDisplayer()
150     {
151       DeleteActors();
152       myProperty->Delete();
153     }
154     // -----------------------------------------------------------------------
155     void DeleteActors()
156     {
157       if ( hasViewWindow() ) {
158         TActorIterator actorIt = actorIterator();
159         while ( actorIt.more() )
160           if (VTKViewer_Actor* anActor = actorIt.next()) {
161             myViewWindow->RemoveActor( anActor );
162             //anActor->Delete();
163           }
164       }
165       myIndexToShape.Clear();
166       myActors.clear();
167       myShownActors.clear();
168       myBuiltSubs.clear();
169     }
170     // -----------------------------------------------------------------------
171     void SetVisibility (bool theVisibility)
172     {
173       TActorIterator actorIt = shownIterator();
174       while ( actorIt.more() )
175         if (VTKViewer_Actor* anActor = actorIt.next())
176           anActor->SetVisibility(theVisibility);
177       SMESH::RepaintCurrentView();
178     }
179     // -----------------------------------------------------------------------
180     bool HasReadyActorsFor (int subShapeID, GEOM::GEOM_Object_var aMainShape )
181     {
182       string mainEntry;
183       if ( !aMainShape->_is_nil() )
184         mainEntry = aMainShape->GetStudyEntry();
185       return ( myMainEntry == mainEntry &&
186                myBuiltSubs.find( subShapeID ) != myBuiltSubs.end() );
187     }
188     // -----------------------------------------------------------------------
189     void Show( int subShapeID, GEOM::GEOM_Object_var aMainShape, bool only = false)
190     {
191       SVTK_ViewWindow* aViewWindow  = SMESH::GetViewWindow( SMESHGUI::GetSMESHGUI() );
192       string mainEntry;
193       if ( !aMainShape->_is_nil() )
194         mainEntry = aMainShape->GetStudyEntry();
195       if ( myMainEntry != mainEntry || aViewWindow != myViewWindow ) { // remove actors
196         DeleteActors();
197         TopoDS_Shape aShape;
198         if ( !aMainShape->_is_nil() && GEOMBase::GetShape(aMainShape, aShape)) {
199           checkTriangulation( aShape );
200           TopExp::MapShapes(aShape, myIndexToShape);
201           myActors.resize( myIndexToShape.Extent(), 0 );
202           myShownActors.reserve( myIndexToShape.Extent() );
203         }
204         myMainEntry  = mainEntry;
205         myViewWindow = aViewWindow;
206       }
207       if ( only ) { // hide shown actors
208         TActorIterator actorIt = shownIterator();
209         while ( actorIt.more() )
210           if (VTKViewer_Actor* anActor = actorIt.next())
211             anActor->SetVisibility(false);
212         myShownActors.clear();
213       }
214       // find actors to show
215       TopoDS_Shape aShape = myIndexToShape( subShapeID );
216       if ( !aShape.IsNull() ) {
217         TopAbs_ShapeEnum type( aShape.ShapeType() >= TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE );
218         for ( TopExp_Explorer exp( aShape, type ); exp.More(); exp.Next() ) {
219           //checkTriangulation( exp.Current() );
220           if ( GEOM_Actor* anActor = getActor( exp.Current() ))
221             myShownActors.push_back( anActor );
222         }
223         if ( type == TopAbs_FACE ) {
224           for ( TopExp_Explorer exp( aShape, TopAbs_EDGE ); exp.More(); exp.Next() ) {
225             const TopoDS_Edge & edge = TopoDS::Edge( exp.Current() );
226             if ( !BRep_Tool::Degenerated( edge ))
227               if ( GEOM_Actor* anActor = getActor( exp.Current() ))
228                 myShownActors.push_back( anActor );
229           }
230         }
231       }
232       myBuiltSubs.insert( subShapeID );
233       SetVisibility(true);
234     }
235     // -----------------------------------------------------------------------
236
237   private:
238
239     typedef std::vector<GEOM_Actor*> TActorVec;
240     TActorVec                  myActors;
241     TActorVec                  myShownActors;
242     TopTools_IndexedMapOfShape myIndexToShape;
243     string                     myMainEntry;
244     SVTK_ViewWindow*           myViewWindow;
245     vtkProperty*               myProperty;
246     std::set<int>              myBuiltSubs;
247
248     // -----------------------------------------------------------------------
249     typedef SMDS_SetIterator< GEOM_Actor*, TActorVec::const_iterator> TActorIterator;
250     TActorIterator actorIterator() {
251       return TActorIterator( myActors.begin(), myActors.end() );
252     }
253     TActorIterator shownIterator() {
254       return TActorIterator( myShownActors.begin(), myShownActors.end() );
255     }
256     // -----------------------------------------------------------------------
257     GEOM_Actor* getActor(const TopoDS_Shape& shape)
258     {
259       int index = myIndexToShape.FindIndex( shape ) - 1;
260       if ( index < 0 || index >= myActors.size() )
261         return 0;
262       GEOM_Actor* & actor = myActors[ index ];
263       if ( !actor ) {
264         actor = GEOM_Actor::New();
265         if ( actor ) {
266           actor->setInputShape(shape,0,0);
267           //actor->SetProperty(myProperty);
268           actor->SetShadingProperty(myProperty);
269           actor->SetWireframeProperty(myProperty);
270           actor->SetPreviewProperty(myProperty);
271           actor->PickableOff();
272           //         if ( shape.ShapeType() == TopAbs_EDGE )
273           //           actor->SubShapeOn();
274           myViewWindow->AddActor( actor );
275         }
276       }
277       return actor;
278     }
279     // -----------------------------------------------------------------------
280     void checkTriangulation(const TopoDS_Shape& shape)
281     {
282       TopLoc_Location aLoc;
283       Standard_Boolean alreadymesh = Standard_True;
284       TopExp_Explorer ex(shape, TopAbs_FACE);
285       if ( ex.More() )
286         for (; ex.More(); ex.Next()) {
287           const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
288           Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
289           if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
290         }
291       else
292         for (ex.Init(shape, TopAbs_EDGE); ex.More(); ex.Next()) {
293           const TopoDS_Edge& edge = TopoDS::Edge(ex.Current());
294           Handle(Poly_Polygon3D) aPoly = BRep_Tool::Polygon3D(edge, aLoc);
295           if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
296         }
297       if (alreadymesh) return;
298       // Compute default deflection
299       Bnd_Box B;
300       BRepBndLib::Add(shape, B);
301       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
302       B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
303       double deflection = Max( aXmax-aXmin , Max ( aYmax-aYmin , aZmax-aZmin)) * 0.01 *4;
304       BRepMesh_IncrementalMesh MESH(shape,deflection);
305     }
306     // -----------------------------------------------------------------------
307     bool hasViewWindow() const
308     {
309       if ( !myViewWindow ) return false;
310
311       if ( SalomeApp_Application* anApp = SMESHGUI::GetSMESHGUI()->getApp() )
312         return FindVtkViewWindow( anApp->getViewManager(SVTK_Viewer::Type(), false ),
313                                   myViewWindow );
314       return false;
315     }
316   };
317
318   // =========================================================================================
319   /*!
320    * \brief Return text describing an error
321    */
322 #define CASE2TEXT(enum) case SMESH::enum: text = QObject::tr( #enum ); break;
323   QString errorText(int errCode, const char* comment)
324   {
325     QString text;
326     switch ( errCode ) {
327       CASE2TEXT( COMPERR_OK            );
328       CASE2TEXT( COMPERR_BAD_INPUT_MESH);
329       CASE2TEXT( COMPERR_STD_EXCEPTION );
330       CASE2TEXT( COMPERR_OCC_EXCEPTION );
331       CASE2TEXT( COMPERR_SLM_EXCEPTION );
332       CASE2TEXT( COMPERR_EXCEPTION     );
333       CASE2TEXT( COMPERR_MEMORY_PB     );
334       CASE2TEXT( COMPERR_BAD_SHAPE     );
335     case SMESH::COMPERR_ALGO_FAILED:
336       if ( strlen(comment) == 0 )
337         text = QObject::tr("COMPERR_ALGO_FAILED");
338       break;
339     default:
340       text = QString("#%1").arg( -errCode );
341     }
342     if ( text.length() > 0 ) text += ". ";
343     return text + comment;
344   }
345   // -----------------------------------------------------------------------
346   /*!
347    * \brief Return SO of a subshape
348    */
349   _PTR(SObject) getSubShapeSO( int subShapeID, GEOM::GEOM_Object_var aMainShape)
350   {
351     _PTR(SObject) so = SMESH::FindSObject(aMainShape);
352     if ( subShapeID == 1 || !so )
353       return so;
354     _PTR(ChildIterator) it;
355     if (_PTR(Study) study = SMESH::GetActiveStudyDocument())
356       it =  study->NewChildIterator(so);
357     _PTR(SObject) subSO;
358     if ( it ) {
359       for ( it->InitEx(true); !subSO && it->More(); it->Next() ) {
360         GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( it->Value() );
361         if ( !geom->_is_nil() ) {
362           GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
363           if ( list->length() == 1 && list[0] == subShapeID )
364             subSO = it->Value();
365         }
366       }
367     }
368     return subSO;
369   }
370   // -----------------------------------------------------------------------
371   /*!
372    * \brief Return subshape by ID
373    */
374   GEOM::GEOM_Object_ptr getSubShape( int subShapeID, GEOM::GEOM_Object_var aMainShape)
375   {
376     GEOM::GEOM_Object_var aSubShape;
377     if ( subShapeID == 1 )
378       aSubShape = aMainShape;
379     else if ( _PTR(SObject) so = getSubShapeSO( subShapeID, aMainShape ))
380       aSubShape = SMESH::SObjectToInterface<GEOM::GEOM_Object>( so );
381     else
382       aSubShape = SMESH::GetSubShape( aMainShape, subShapeID );
383     return aSubShape._retn();
384   }
385   // -----------------------------------------------------------------------
386   /*!
387    * \brief Return shape type name
388    */
389 #define CASE2NAME(enum) case GEOM::enum: name = QObject::tr( "GEOM_" #enum ); break;
390   QString shapeTypeName(GEOM::GEOM_Object_var aShape, const char* dflt = "" )
391   {
392     QString name = dflt;
393     if ( !aShape->_is_nil() ) {
394       switch ( aShape->GetShapeType() ) {
395       CASE2NAME( VERTEX    );
396       CASE2NAME( EDGE      );
397       CASE2NAME( WIRE      );
398       CASE2NAME( FACE      );
399       CASE2NAME( SHELL     );
400       CASE2NAME( SOLID     );
401       CASE2NAME( COMPSOLID );
402       CASE2NAME( COMPOUND  );
403       default:;
404       }
405     }
406     return name;
407   }
408   // -----------------------------------------------------------------------
409   /*!
410    * \brief Return text describing a subshape
411    */
412   QString shapeText(int subShapeID, GEOM::GEOM_Object_var aMainShape )
413   {
414     QString text;
415     if ( _PTR(SObject) aSO = getSubShapeSO( subShapeID, aMainShape ))
416       text = aSO->GetName();
417     else {
418       text = QString("#%1").arg( subShapeID );
419       QString typeName = shapeTypeName( getSubShape( subShapeID, aMainShape ));
420       if ( typeName.length() )
421         text += QString(" (%1)").arg(typeName);
422     }
423     return text;
424   }
425   // -----------------------------------------------------------------------
426   /*!
427    * \brief Return text describing a subshape
428    */
429   bool getSelectedRows(QTable* table, list< int > & rows)
430   {
431     rows.clear();
432     int nbSel = table->numSelections();
433     for ( int i = 0; i < nbSel; ++i )
434     {
435       QTableSelection selected = table->selection(i);
436       if ( !selected.isActive() ) continue;
437       for ( int row = selected.topRow(); row <= selected.bottomRow(); ++row )
438         rows.push_back( row );
439     }
440     return !rows.empty();
441   }
442
443 } // namespace SMESH
444
445
446 // =========================================================================================
447 /*!
448  * \brief Box showing mesh info
449  */
450 // =========================================================================================
451
452 SMESHGUI_MeshInfosBox::SMESHGUI_MeshInfosBox(const bool full, QWidget* theParent)
453   :QGroupBox( 4, Qt::Horizontal, tr("SMESH_MESHINFO_TITLE"), theParent ), myFull( full )
454 {
455   // title
456   QLabel* lab1 = new QLabel(this);
457   QLabel* lab2 = new QLabel(tr("SMESH_MESHINFO_ORDER0"), this );
458   QLabel* lab3 = new QLabel(tr("SMESH_MESHINFO_ORDER1"), this );
459   QLabel* lab4 = new QLabel(tr("SMESH_MESHINFO_ORDER2"), this );
460
461   QFont italic = lab1->font(); italic.setItalic(true);
462   QFont bold   = lab1->font(); bold.setBold(true);
463
464   lab1->setMinimumWidth(100); lab1->setFont( italic );
465   lab2->setMinimumWidth(100); lab2->setFont( italic );
466   lab3->setMinimumWidth(100); lab3->setFont( italic );
467   lab4->setMinimumWidth(100); lab4->setFont( italic );
468
469   if ( myFull )
470   {
471     // nodes
472     (new QLabel(COLONIZE(tr("SMESH_MESHINFO_NODES")), this ))->setFont( bold );
473     myNbNode = new QLabel( this );
474     new QLabel(this);
475     new QLabel(this);
476
477     _SEPARATOR(this);
478
479     // edges
480     (new QLabel(COLONIZE(tr("SMESH_MESHINFO_EDGES")), this ))->setFont( bold );
481     myNbEdge = new QLabel( this );
482     myNbLinEdge = new QLabel( this );
483     myNbQuadEdge = new QLabel( this );
484
485     _SEPARATOR(this);
486
487     // faces
488     (new QLabel(COLONIZE(tr("SMESH_MESHINFO_FACES")), this))->setFont( bold );
489     myNbFace     = new QLabel( this );
490     myNbLinFace  = new QLabel( this );
491     myNbQuadFace = new QLabel( this );
492     // triangles
493     new QLabel(COLONIZE(tr("SMESH_MESHINFO_TRIANGLES")), this );
494     myNbTrai     = new QLabel( this );
495     myNbLinTrai  = new QLabel( this );
496     myNbQuadTrai = new QLabel( this );
497     // quadrangles
498     new QLabel(COLONIZE(tr("SMESH_MESHINFO_QUADRANGLES")), this );
499     myNbQuad     = new QLabel( this );
500     myNbLinQuad  = new QLabel( this );
501     myNbQuadQuad = new QLabel( this );
502     // poligones
503     new QLabel(COLONIZE(tr("SMESH_MESHINFO_POLYGONES")), this );
504     myNbPolyg    = new QLabel( this );
505     new QLabel("",this );
506     new QLabel("", this );
507
508     _SEPARATOR(this);
509
510     // volumes
511     (new QLabel(COLONIZE(tr("SMESH_MESHINFO_VOLUMES")), this))->setFont( bold );
512     myNbVolum     = new QLabel( this );
513     myNbLinVolum  = new QLabel( this );
514     myNbQuadVolum = new QLabel( this );
515     // tetras
516     new QLabel(COLONIZE(tr("SMESH_MESHINFO_TETRAS")), this );
517     myNbTetra     = new QLabel( this );
518     myNbLinTetra  = new QLabel( this );
519     myNbQuadTetra = new QLabel( this );
520     // hexas
521     new QLabel(COLONIZE(tr("SMESH_MESHINFO_HEXAS")), this );
522     myNbHexa      = new QLabel( this );
523     myNbLinHexa   = new QLabel( this );
524     myNbQuadHexa  = new QLabel( this );
525     // pyras
526     new QLabel(COLONIZE(tr("SMESH_MESHINFO_PYRAS")), this );
527     myNbPyra      = new QLabel( this );
528     myNbLinPyra   = new QLabel( this );
529     myNbQuadPyra  = new QLabel( this );
530     // prisms
531     new QLabel(COLONIZE(tr("SMESH_MESHINFO_PRISMS")), this );
532     myNbPrism     = new QLabel( this );
533     myNbLinPrism  = new QLabel( this );
534     myNbQuadPrism = new QLabel( this );
535     // polyedres
536     new QLabel(COLONIZE(tr("SMESH_MESHINFO_POLYEDRES")), this );
537     myNbPolyh     = new QLabel( this );
538     new QLabel("", this );
539     new QLabel("", this );
540   }
541   else
542   {
543     // nodes
544     new QLabel(COLONIZE(tr("SMESH_MESHINFO_NODES")), this );
545     myNbNode      = new QLabel( this );
546     new QLabel(this);
547     new QLabel(this);
548
549     // edges
550     new QLabel(COLONIZE(tr("SMESH_MESHINFO_EDGES")), this );
551     myNbEdge      = new QLabel( this );
552     myNbLinEdge   = new QLabel( this );
553     myNbQuadEdge  = new QLabel( this );
554
555     // faces
556     new QLabel(COLONIZE(tr("SMESH_MESHINFO_FACES")), this);
557     myNbFace      = new QLabel( this );
558     myNbLinFace   = new QLabel( this );
559     myNbQuadFace  = new QLabel( this );
560
561     // volumes
562     new QLabel(COLONIZE(tr("SMESH_MESHINFO_VOLUMES")), this);
563     myNbVolum     = new QLabel( this );
564     myNbLinVolum  = new QLabel( this );
565     myNbQuadVolum = new QLabel( this );
566   }
567 }
568
569 // =========================================================================================
570 /*!
571  * \brief Set mesh info
572  */
573 // =========================================================================================
574
575 void SMESHGUI_MeshInfosBox::SetInfoByMesh(SMESH::SMESH_Mesh_var mesh)
576 {
577   const SMESH::ElementOrder lin = SMESH::ORDER_LINEAR;
578   int nbTot, nbLin;
579
580   // nodes
581   myNbNode     ->setText( QString("%1").arg( mesh->NbNodes() ));
582
583   // edges
584   nbTot = mesh->NbEdges(), nbLin = mesh->NbEdgesOfOrder(lin);
585   myNbEdge     ->setText( QString("%1").arg( nbTot ));
586   myNbLinEdge  ->setText( QString("%1").arg( nbLin ));
587   myNbQuadEdge ->setText( QString("%1").arg( nbTot - nbLin ));
588
589   // faces
590   nbTot = mesh->NbFaces(), nbLin = mesh->NbFacesOfOrder(lin);
591   myNbFace     ->setText( QString("%1").arg( nbTot ));
592   myNbLinFace  ->setText( QString("%1").arg( nbLin )); 
593   myNbQuadFace ->setText( QString("%1").arg( nbTot - nbLin ));
594
595   // volumes
596   nbTot = mesh->NbVolumes(), nbLin = mesh->NbVolumesOfOrder(lin);
597   myNbVolum    ->setText( QString("%1").arg( nbTot ));
598   myNbLinVolum ->setText( QString("%1").arg( nbLin ));
599   myNbQuadVolum->setText( QString("%1").arg( nbTot - nbLin ));
600
601   if ( myFull )
602   {
603     // triangles
604     nbTot = mesh->NbTriangles(), nbLin = mesh->NbTrianglesOfOrder(lin);
605     myNbTrai     ->setText( QString("%1").arg( nbTot ));
606     myNbLinTrai  ->setText( QString("%1").arg( nbLin ));
607     myNbQuadTrai ->setText( QString("%1").arg( nbTot - nbLin ));
608     // quadrangles
609     nbTot = mesh->NbQuadrangles(), nbLin = mesh->NbQuadranglesOfOrder(lin);
610     myNbQuad     ->setText( QString("%1").arg( nbTot ));
611     myNbLinQuad  ->setText( QString("%1").arg( nbLin ));
612     myNbQuadQuad ->setText( QString("%1").arg( nbTot - nbLin ));
613     // poligones
614     myNbPolyg    ->setText( QString("%1").arg( mesh->NbPolygons() ));
615
616     // tetras
617     nbTot = mesh->NbTetras(), nbLin = mesh->NbTetrasOfOrder(lin);
618     myNbTetra    ->setText( QString("%1").arg( nbTot ));
619     myNbLinTetra ->setText( QString("%1").arg( nbLin ));
620     myNbQuadTetra->setText( QString("%1").arg( nbTot - nbLin ));
621     // hexas
622     nbTot = mesh->NbHexas(), nbLin = mesh->NbHexasOfOrder(lin);
623     myNbHexa     ->setText( QString("%1").arg( nbTot ));
624     myNbLinHexa  ->setText( QString("%1").arg( nbLin ));
625     myNbQuadHexa ->setText( QString("%1").arg( nbTot - nbLin ));
626     // pyras
627     nbTot = mesh->NbPyramids(), nbLin = mesh->NbPyramidsOfOrder(lin);
628     myNbPyra     ->setText( QString("%1").arg( nbTot ));
629     myNbLinPyra  ->setText( QString("%1").arg( nbLin ));
630     myNbQuadPyra ->setText( QString("%1").arg( nbTot - nbLin ));
631     // prisms
632     nbTot = mesh->NbPrisms(), nbLin = mesh->NbPrismsOfOrder(lin);
633     myNbPrism    ->setText( QString("%1").arg( nbTot ));
634     myNbLinPrism ->setText( QString("%1").arg( nbLin ));
635     myNbQuadPrism->setText( QString("%1").arg( nbTot - nbLin ));
636     // polyedres
637     myNbPolyh    ->setText( QString("%1").arg( mesh->NbPolyhedrons() ));
638   }
639 }
640
641 // =========================================================================================
642 /*!
643  * \brief Dialog to compute a mesh and show computation errors
644  */
645 //=======================================================================
646
647 SMESHGUI_ComputeDlg::SMESHGUI_ComputeDlg(): SMESHGUI_Dialog( 0, false, true, OK/* | Help*/ )
648 {
649   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame(), 0, SPACING);
650
651   QFrame* aMainFrame = createMainFrame  (mainFrame());
652
653   aDlgLay->addWidget(aMainFrame);
654
655   aDlgLay->setStretchFactor(aMainFrame, 1);
656 }
657
658 //=======================================================================
659 // function : createMainFrame()
660 // purpose  : Create frame containing dialog's fields
661 //=======================================================================
662
663 #define CASE2HEADER(enum) case enum: header = QObject::tr( #enum "_HEADER" ); break;
664
665 QFrame* SMESHGUI_ComputeDlg::createMainFrame (QWidget* theParent)
666 {
667   QFrame* aFrame = new QFrame(theParent);
668
669   SUIT_ResourceMgr* rm = resourceMgr();
670   QPixmap iconCompute (rm->loadPixmap("SMESH", tr("ICON_COMPUTE")));
671
672   // constructor
673
674   QButtonGroup* aPixGrp = new QButtonGroup(1, Qt::Vertical, tr("CONSTRUCTOR"), aFrame);
675   aPixGrp->setExclusive(TRUE);
676   QRadioButton* aRBut = new QRadioButton(aPixGrp);
677   aRBut->setPixmap(iconCompute);
678   aRBut->setChecked(TRUE);
679
680   // Mesh name
681
682   QHGroupBox* nameBox = new QHGroupBox(tr("SMESH_MESHINFO_NAME"), aFrame );
683   myMeshName = new QLabel(nameBox);
684
685   // Mesh Info
686
687   myBriefInfo = new SMESHGUI_MeshInfosBox(false, aFrame);
688   myFullInfo  = new SMESHGUI_MeshInfosBox(true,  aFrame);
689
690   // errors
691
692   myErrorGroup = new QGroupBox(tr("ERRORS"), aFrame, "errorGrBox");
693   myTable      = new QTable( 1, NB_COLUMNS, myErrorGroup, "myTable");
694   myShowBtn    = new QPushButton(tr("SHOW_SHAPE"), myErrorGroup, "myShowBtn");
695   myPublishBtn = new QPushButton(tr("PUBLISH_SHAPE"), myErrorGroup, "myPublishBtn");
696
697   myTable->setReadOnly( TRUE );
698   myTable->hideColumn( COL_PUBLISHED );
699   myTable->hideColumn( COL_SHAPEID );
700   myTable->setColumnStretchable( COL_ERROR, 1 );
701   for ( int col = 0; col < NB_COLUMNS; ++col ) {
702     QString header;
703     switch ( col ) {
704     CASE2HEADER( COL_ALGO     );
705     CASE2HEADER( COL_SHAPE    );
706     CASE2HEADER( COL_ERROR    );
707     CASE2HEADER( COL_SHAPEID  );
708     CASE2HEADER( COL_PUBLISHED);
709     }
710     myTable->horizontalHeader()->setLabel( col, header );
711   }
712
713   myErrorGroup->setColumnLayout(0, Qt::Vertical);
714   myErrorGroup->layout()->setSpacing(0);
715   myErrorGroup->layout()->setMargin(0);
716   QGridLayout* grpLayout = new QGridLayout(myErrorGroup->layout());
717   grpLayout->setAlignment(Qt::AlignTop);
718   grpLayout->setSpacing(SPACING);
719   grpLayout->setMargin(MARGIN);
720   grpLayout->addMultiCellWidget( myTable,   0, 2, 0, 0 );
721   grpLayout->addWidget         ( myShowBtn,    0, 1 );
722   grpLayout->addWidget         ( myPublishBtn, 1, 1 );
723   grpLayout->setRowStretch( 2, 1 );
724
725   // Memory Lack Label
726
727   myMemoryLackGroup = new QVGroupBox(tr("ERRORS"), aFrame, "memlackGrBox");
728   QLabel* memLackLabel = new QLabel(tr("MEMORY_LACK"), myMemoryLackGroup);
729   QFont bold = memLackLabel->font(); bold.setBold(true);
730   memLackLabel->setFont( bold );
731   memLackLabel->setMinimumWidth(300);
732
733   // add all widgets to aFrame
734   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
735   aLay->addWidget( aPixGrp );
736   aLay->addWidget( nameBox );
737   aLay->addWidget( myBriefInfo );
738   aLay->addWidget( myFullInfo );
739   aLay->addWidget( myErrorGroup );
740   aLay->addWidget( myMemoryLackGroup );
741   aLay->setStretchFactor( myErrorGroup, 1 );
742
743   return aFrame;
744 }
745
746 //================================================================================
747 /*!
748  * \brief Constructor
749 */
750 //================================================================================
751
752 SMESHGUI_ComputeOp::SMESHGUI_ComputeOp()
753 {
754   myDlg = new SMESHGUI_ComputeDlg;
755   myTShapeDisplayer = new TShapeDisplayer();
756   myHelpFileName = "/files/about_meshes.htm";
757
758   // connect signals and slots
759   connect(myDlg->myShowBtn,    SIGNAL (clicked()), SLOT(onPreviewShape()));
760   connect(myDlg->myPublishBtn, SIGNAL (clicked()), SLOT(onPublishShape()));
761   connect(table(),SIGNAL(selectionChanged()), SLOT(currentCellChanged()));
762 }
763
764 //=======================================================================
765 // function : startOperation()
766 // purpose  : Init dialog fields, connect signals and slots, show dialog
767 //=======================================================================
768
769 void SMESHGUI_ComputeOp::startOperation()
770 {
771   SMESHGUI_Operation::startOperation();
772
773   SMESH::SMESH_Mesh_var          aMesh;
774   SMESH::compute_error_array_var anErrors;
775
776   myMainShape = GEOM::GEOM_Object::_nil();
777
778   // COMPUTE MESH
779
780   bool computeFailed = true, memoryLack = false;
781   int nbNodes = 0, nbEdges = 0, nbFaces = 0, nbVolums = 0;
782
783   LightApp_SelectionMgr *Sel = selectionMgr();
784   SALOME_ListIO selected; Sel->selectedObjects( selected );
785
786   int nbSel = selected.Extent();
787   if (nbSel != 1) {
788     SUIT_MessageBox::warn1(desktop(),
789                            tr("SMESH_WRN_WARNING"),
790                            tr("SMESH_WRN_NO_AVAILABLE_DATA"),
791                            tr("SMESH_BUT_OK"));
792     onCancel();
793     return;
794   }
795
796   Handle(SALOME_InteractiveObject) IObject = selected.First();
797   aMesh = SMESH::GetMeshByIO(IObject);
798   if (!aMesh->_is_nil())
799   {
800     MemoryReserve aMemoryReserve;
801     _PTR(SObject) aMeshSObj = SMESH::FindSObject(aMesh);
802     myMainShape = aMesh->GetShapeToMesh();
803     if ( !myMainShape->_is_nil() && aMeshSObj )
804     {
805       myDlg->myMeshName->setText( aMeshSObj->GetName() );
806       SMESH::SMESH_Gen_var gen = getSMESHGUI()->GetSMESHGen();
807       SMESH::algo_error_array_var errors = gen->GetAlgoState(aMesh,myMainShape);
808       if ( errors->length() > 0 ) {
809         SUIT_MessageBox::warn1(desktop(), tr("SMESH_WRN_WARNING"),
810                                SMESH::GetMessageOnAlgoStateErrors( errors.in() ),
811                                tr("SMESH_BUT_OK"));
812         onCancel();
813         return;
814       }
815       SUIT_OverrideCursor aWaitCursor;
816       try {
817         if (gen->Compute(aMesh, myMainShape))
818           computeFailed = false;
819       }
820       catch(const SALOME::SALOME_Exception & S_ex){
821         memoryLack = true;
822         //SalomeApp_Tools::QtCatchCorbaException(S_ex);
823       }
824       try {
825         anErrors = gen->GetComputeErrors( aMesh, myMainShape );
826         //           if ( anErrors->length() == 0 ) {
827         //             SUIT_MessageBox::warn1(desktop(),
828         //                                    tr("SMESH_WRN_WARNING"),
829         //                                    tr("SMESH_WRN_COMPUTE_FAILED"),
830         //                                    tr("SMESH_BUT_OK"));
831         //             onCancel();
832         //             return;
833         //           }
834         // check if there are memory problems
835         for ( int i = 0; i < anErrors->length() && !memoryLack; ++i )
836           memoryLack = ( anErrors[ i ].code == SMESH::COMPERR_MEMORY_PB );
837       }
838       catch(const SALOME::SALOME_Exception & S_ex){
839         memoryLack = true;
840       }
841
842       // NPAL16631: if ( !memoryLack )
843       {
844         SMESH::ModifiedMesh(aMeshSObj, !computeFailed, aMesh->NbNodes() == 0);
845         update( UF_ObjBrowser | UF_Model );
846
847         // SHOW MESH
848         // NPAL16631: if ( getSMESHGUI()->automaticUpdate() ) {
849         if ( !memoryLack && getSMESHGUI()->automaticUpdate() ) // NPAL16631
850         {
851           try {
852             SVTK_ViewWindow* aVTKView = SMESH::GetViewWindow(getSMESHGUI(), true);
853             if (aVTKView) {
854               int anId = study()->id();
855               TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId, IObject->getEntry());
856               if (aVisualObj) {
857                 aVisualObj->Update();
858                 SMESH_Actor* anActor = SMESH::FindActorByEntry(IObject->getEntry());
859                 if (!anActor) {
860                   anActor = SMESH::CreateActor(studyDS(), IObject->getEntry());
861                   if (anActor) {
862                     SMESH::DisplayActor(aVTKView, anActor); //apo
863                     SMESH::FitAll();
864                   }
865                 }
866                 SMESH::RepaintCurrentView();
867                 Sel->setSelectedObjects( selected );
868               }
869             }
870           }
871           catch (...) {
872             memoryLack = true;
873           }
874         }
875       }
876     }
877   }
878   else {
879     SUIT_MessageBox::warn1(desktop(),
880                            tr("SMESH_WRN_WARNING"),
881                            tr("SMESH_WRN_NO_AVAILABLE_DATA"),
882                            tr("SMESH_BUT_OK"));
883     onCancel();
884     return;
885   }
886   myDlg->setCaption(tr( computeFailed ? "SMESH_WRN_COMPUTE_FAILED" : "SMESH_COMPUTE_SUCCEED"));
887   myDlg->myMemoryLackGroup->hide();
888
889   // SHOW ERRORS
890
891   bool noError = ( !anErrors.operator->() || anErrors->length() == 0 );
892
893   if ( memoryLack )
894   {
895     myDlg->myMemoryLackGroup->show();
896     myDlg->myFullInfo->hide();
897     myDlg->myBriefInfo->hide();
898     myDlg->myErrorGroup->hide();
899   }
900   else if ( noError )
901   {
902     myDlg->myFullInfo->SetInfoByMesh( aMesh );
903     myDlg->myFullInfo->show();
904     myDlg->myBriefInfo->hide();
905     myDlg->myErrorGroup->hide();
906   }
907   else
908   {
909     QTable* tbl = myDlg->myTable;
910     myDlg->myBriefInfo->SetInfoByMesh( aMesh );
911     myDlg->myBriefInfo->show();
912     myDlg->myFullInfo->hide();
913     myDlg->myErrorGroup->show();
914
915     // fill table of errors
916     tbl->setNumRows( anErrors->length() );
917     bool hasShape = aMesh->HasShapeToMesh();
918     if ( !hasShape ) tbl->hideColumn( COL_SHAPE );
919     else             tbl->showColumn( COL_SHAPE );
920     tbl->setColumnWidth( COL_ERROR, 200 );
921
922     for ( int row = 0; row < anErrors->length(); ++row )
923     {
924       SMESH::ComputeError & err = anErrors[ row ];
925       tbl->setText( row, COL_ALGO,    err.algoName.in() );
926       tbl->setText( row, COL_ERROR,   errorText( err.code, err.comment.in() ));
927       tbl->setText( row, COL_SHAPEID, QString("%1").arg( err.subShapeID ));
928
929       QString text = hasShape ? shapeText( err.subShapeID, myMainShape ) : QString("");
930       tbl->setText( row, COL_SHAPE,   text );
931
932       text = ( !hasShape || getSubShapeSO( err.subShapeID, myMainShape )) ? "PUBLISHED" : "";
933       tbl->setText( row, COL_PUBLISHED, text ); // if text=="", "PUBLISH" button enabled
934
935       tbl->item( row, COL_ERROR )->setWordWrap( TRUE );
936       tbl->adjustRow( row );
937     }
938     tbl->adjustColumn( COL_ALGO );
939     tbl->adjustColumn( COL_SHAPE );
940
941     tbl->setCurrentCell(0,0);
942     currentCellChanged(); // to update buttons
943   }
944   myDlg->show();
945 }
946
947 //================================================================================
948 /*!
949  * \brief Stops operation
950  */
951 //================================================================================
952
953 void SMESHGUI_ComputeOp::stopOperation()
954 {
955   SMESHGUI_Operation::stopOperation();
956   myTShapeDisplayer->SetVisibility( false );
957 }
958
959 //================================================================================
960 /*!
961  * \brief publish selected subshape
962  */
963 //================================================================================
964
965 void SMESHGUI_ComputeOp::onPublishShape()
966 {
967   GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
968   SALOMEDS::Study_var study = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
969
970   list< int > rows;
971   list< int >::iterator row;
972   getSelectedRows( table(), rows );
973   for ( row = rows.begin(); row != rows.end(); ++row )
974   {
975     int curSub = table()->text(*row, COL_SHAPEID).toInt();
976     GEOM::GEOM_Object_var shape = getSubShape( curSub, myMainShape );
977     if ( !shape->_is_nil() && ! getSubShapeSO( curSub, myMainShape ))
978     {
979       if ( !getSubShapeSO( 1, myMainShape )) // the main shape not published
980       {
981         QString name = GEOMBase::GetDefaultName( shapeTypeName( myMainShape, "MAIN_SHAPE" ));
982         SALOMEDS::SObject_var so =
983           geomGen->AddInStudy( study, myMainShape, name, GEOM::GEOM_Object::_nil());
984         // look for myMainShape in the table
985         for ( int r = 0, nr = table()->numRows(); r < nr; ++r ) {
986           if ( table()->text(r, COL_SHAPEID) == "1" ) {
987             if ( so->_is_nil() ) {
988               table()->setText( r, COL_SHAPE, so->GetName() );
989               table()->setText( r, COL_PUBLISHED, so->GetID() );
990             }
991             break;
992           }
993         }
994         if ( curSub == 1 ) continue;
995       }
996       QString name = GEOMBase::GetDefaultName( shapeTypeName( shape, "ERROR_SHAPE" ));
997       SALOMEDS::SObject_var so = geomGen->AddInStudy( study, shape, name, myMainShape);
998       if ( !so->_is_nil() ) {
999         table()->setText( *row, COL_SHAPE, so->GetName() );
1000         table()->setText( *row, COL_PUBLISHED, so->GetID() );
1001       }
1002     }
1003   }
1004   getSMESHGUI()->getApp()->updateObjectBrowser();
1005   currentCellChanged(); // to update buttons
1006 }
1007
1008 //================================================================================
1009 /*!
1010  * \brief SLOT called when a selected cell in table() changed
1011  */
1012 //================================================================================
1013
1014 void SMESHGUI_ComputeOp::currentCellChanged()
1015 {
1016   myTShapeDisplayer->SetVisibility( false );
1017
1018   bool publishEnable = 0, showEnable = 0, showOnly = 1;
1019   list< int > rows;
1020   list< int >::iterator row;
1021   getSelectedRows( table(), rows );
1022   for ( row = rows.begin(); row != rows.end(); ++row )
1023   {
1024     bool hasData     = ( !table()->text(*row, COL_SHAPE).isEmpty() );
1025     bool isPublished = ( !table()->text(*row, COL_PUBLISHED).isEmpty() );
1026     if ( hasData && !isPublished )
1027       publishEnable = true;
1028
1029     int curSub = table()->text(*row, COL_SHAPEID).toInt();
1030     bool prsReady = myTShapeDisplayer->HasReadyActorsFor( curSub, myMainShape );
1031     if ( prsReady ) {
1032       myTShapeDisplayer->Show( curSub, myMainShape, showOnly );
1033       showOnly = false;
1034     }
1035     else {
1036       showEnable = true;
1037     }
1038   }
1039   myDlg->myPublishBtn->setEnabled( publishEnable );
1040   myDlg->myShowBtn->setEnabled( showEnable );
1041 }
1042
1043 //================================================================================
1044 /*!
1045  * \brief update preview
1046  */
1047 //================================================================================
1048
1049 void SMESHGUI_ComputeOp::onPreviewShape()
1050 {
1051   if ( myTShapeDisplayer )
1052   {
1053     SUIT_OverrideCursor aWaitCursor;
1054     list< int > rows;
1055     list< int >::iterator row;
1056     getSelectedRows( table(), rows );
1057
1058     bool showOnly = true;
1059     for ( row = rows.begin(); row != rows.end(); ++row )
1060     {
1061       int curSub = table()->text(*row, COL_SHAPEID).toInt();
1062       if ( curSub > 0 ) {
1063         myTShapeDisplayer->Show( curSub, myMainShape, showOnly );
1064         showOnly = false;
1065       }
1066     }
1067     currentCellChanged(); // to update buttons
1068   }
1069 }
1070
1071 //================================================================================
1072 /*!
1073  * \brief Destructor
1074  */
1075 //================================================================================
1076
1077 SMESHGUI_ComputeOp::~SMESHGUI_ComputeOp()
1078 {
1079   if ( myTShapeDisplayer ) delete myTShapeDisplayer;
1080 }
1081
1082 //================================================================================
1083 /*!
1084  * \brief Gets dialog of this operation
1085  * \retval LightApp_Dialog* - pointer to dialog of this operation
1086  */
1087 //================================================================================
1088
1089 LightApp_Dialog* SMESHGUI_ComputeOp::dlg() const
1090 {
1091   return myDlg;
1092 }
1093
1094 //================================================================================
1095 /*!
1096  * \brief perform it's intention action: compute mesh
1097  */
1098 //================================================================================
1099
1100 bool SMESHGUI_ComputeOp::onApply()
1101 {
1102   return true;
1103 }
1104
1105 //================================================================================
1106 /*!
1107  * \brief Return a table
1108  */
1109 //================================================================================
1110
1111 QTable* SMESHGUI_ComputeOp::table()
1112 {
1113   return myDlg->myTable;
1114 }