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