Salome HOME
Bug IPAL19097 NPAL16768 is not implemented for "Normal to a face"
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CheckCompoundOfBlocksDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry 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 // File   : MeasureGUI_CheckCompoundOfBlocksDlg.cxx
23 // Author : Vladimir KLYACHIN, Open CASCADE S.A.S. (vladimir.klyachin@opencascade.com)
24 //
25
26 #include "MeasureGUI_CheckCompoundOfBlocksDlg.h"
27 #include "MeasureGUI_Widgets.h"
28
29 #include <SUIT_Session.h>
30 #include <SUIT_ResourceMgr.h>
31 #include <LightApp_SelectionMgr.h>
32 #include <SalomeApp_Tools.h>
33 #include <SalomeApp_Application.h>
34
35 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <TopExp.hxx>
37 #include <TColStd_MapOfInteger.hxx>
38
39 #include <DlgRef.h>
40 #include <GeometryGUI.h>
41 #include <GEOMBase.h>
42 #include <GEOMImpl_Types.hxx>
43
44 #define TEXTEDIT_FONT_FAMILY "Courier"
45 #define TEXTEDIT_FONT_SIZE 11
46
47 //=================================================================================
48 // class    : MeasureGUI_CheckCompoundOfBlocksDlg()
49 // purpose  : Constructs a MeasureGUI_CheckCompoundOfBlocksDlg which is a child of 'parent', with the
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            true to construct a modal dialog.
53 //=================================================================================
54 MeasureGUI_CheckCompoundOfBlocksDlg::MeasureGUI_CheckCompoundOfBlocksDlg( GeometryGUI* GUI, QWidget* parent )
55   : GEOMBase_Skeleton( GUI, parent, false )
56 {
57   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
58   QPixmap image0( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_CHECK_COMPOUND_OF_BLOCKS" ) ) );
59   QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
60
61   setWindowTitle( tr( "GEOM_CHECK_BLOCKS_COMPOUND" ) );
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_CHECK_BLOCKS_COMPOUND" ) );
65   mainFrame()->RadioButton1->setIcon( image0 );
66   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
67   mainFrame()->RadioButton2->close();
68   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
69   mainFrame()->RadioButton3->close();
70
71   myGrp = new MeasureGUI_1Sel1TextView2ListBox( centralWidget() );
72   myGrp->GroupBox1->setTitle( tr( "GEOM_CHECK_INFOS" ) );
73   myGrp->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
74
75   myGrp->TextView1->setReadOnly( true );
76   QFont aFont( TEXTEDIT_FONT_FAMILY, TEXTEDIT_FONT_SIZE );
77   aFont.setStyleHint( QFont::TypeWriter, QFont::PreferAntialias );
78   myGrp->TextView1->setFont( aFont );
79
80   myGrp->PushButton1->setIcon( image1 );
81   myGrp->LineEdit1->setReadOnly( true );
82
83   myGrp->TextLabel2->setText( tr( "GEOM_CHECK_BLOCKS_COMPOUND_ERRORS" ) );
84   myGrp->TextLabel3->setText( tr( "GEOM_CHECK_BLOCKS_COMPOUND_SUBSHAPES" ) );
85
86   myGrp->ListBox2->setSelectionMode( QAbstractItemView::ExtendedSelection );
87
88   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
89   layout->setMargin( 0 ); layout->setSpacing( 6 );
90   layout->addWidget( myGrp );
91
92   connect( myGrp->ListBox1, SIGNAL( itemSelectionChanged() ), SLOT( onErrorsListSelectionChanged() ) );
93   connect( myGrp->ListBox2, SIGNAL( itemSelectionChanged() ), SLOT( onSubShapesListSelectionChanged() ) );
94
95   /***************************************************************/
96
97   myHelpFileName = "using_measurement_tools_page.html#check_compound_anchor";
98
99   /* Initialisation */
100   Init();
101 }
102
103 //=================================================================================
104 // function : ~MeasureGUI_CheckCompoundOfBlocksDlg()
105 // purpose  : Destroys the object and frees any allocated resources
106 //=================================================================================
107 MeasureGUI_CheckCompoundOfBlocksDlg::~MeasureGUI_CheckCompoundOfBlocksDlg()
108 {
109 }
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void MeasureGUI_CheckCompoundOfBlocksDlg::Init()
116 {
117   myEditCurrentArgument = myGrp->LineEdit1;
118
119   // signals and slots connections
120   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
121   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
122
123   connect( myGrp->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
124   connect( myGrp->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
125
126   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ), 
127            this, SLOT( SelectionIntoArgument() ) );
128
129   initName( tr( "GEOM_BLOCKS_COMPOUND") );
130   buttonOk()->setEnabled( false );
131   buttonApply()->setEnabled( false );
132   activateSelection();
133   SelectionIntoArgument();
134 }
135
136 //=================================================================================
137 // function : ClickOnOk()
138 // purpose  :
139 //=================================================================================
140 void MeasureGUI_CheckCompoundOfBlocksDlg::ClickOnOk()
141 {
142   if ( ClickOnApply() )
143     ClickOnCancel();
144 }
145
146 //=================================================================================
147 // function : ClickOnApply()
148 // purpose  :
149 //=================================================================================
150 bool MeasureGUI_CheckCompoundOfBlocksDlg::ClickOnApply()
151 {
152   if ( !onAccept() )
153     return false;
154
155   initName();
156   return true;
157 }
158
159 //=================================================================================
160 // function : SelectionIntoArgument
161 // purpose  :
162 //=================================================================================
163 void MeasureGUI_CheckCompoundOfBlocksDlg::SelectionIntoArgument()
164 {
165   erasePreview();
166   myObj = GEOM::GEOM_Object::_nil();
167
168   if ( IObjectCount() != 1 ) {
169     myGrp->LineEdit1->setText( "" );
170     processObject();
171     return;
172   }
173
174   Standard_Boolean testResult = Standard_False;
175   GEOM::GEOM_Object_var aSelectedObject =
176     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
177
178   if ( !testResult || aSelectedObject->_is_nil() ) {
179     myGrp->LineEdit1->setText( "" );
180     processObject();
181     return;
182   }
183
184   myObj = aSelectedObject;
185   myGrp->LineEdit1->setText( GEOMBase::GetName( myObj ) );
186   processObject();
187   displayPreview();
188 }
189
190 //=================================================================================
191 // function : SetEditCurrentArgument
192 // purpose  :
193 //=================================================================================
194 void MeasureGUI_CheckCompoundOfBlocksDlg::SetEditCurrentArgument()
195 {
196   myGrp->LineEdit1->setFocus();
197   myEditCurrentArgument = myGrp->LineEdit1;
198   SelectionIntoArgument();
199 }
200
201 //=================================================================================
202 // function : LineEditReturnPressed()
203 // purpose  :
204 //=================================================================================
205 void MeasureGUI_CheckCompoundOfBlocksDlg::LineEditReturnPressed()
206 {
207   QLineEdit* send = (QLineEdit*)sender();
208   if ( send == myGrp->LineEdit1 ) {
209     myEditCurrentArgument = myGrp->LineEdit1;
210     GEOMBase_Skeleton::LineEditReturnPressed();
211   }
212 }
213
214 //=================================================================================
215 // function : ActivateThisDialog()
216 // purpose  :
217 //=================================================================================
218 void MeasureGUI_CheckCompoundOfBlocksDlg::ActivateThisDialog()
219 {
220   GEOMBase_Skeleton::ActivateThisDialog();
221
222   LightApp_SelectionMgr* aSel = myGeomGUI->getApp()->selectionMgr();
223   if ( aSel )
224     connect( aSel, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
225
226   activateSelection();
227   displayPreview();
228 }
229
230 //=================================================================================
231 // function : getBCErrors
232 // purpose  :
233 //=================================================================================
234 bool MeasureGUI_CheckCompoundOfBlocksDlg::getBCErrors( bool& theIsCompoundOfBlocks,
235                                                        GEOM::GEOM_IBlocksOperations::BCErrors& theErrors)
236 {
237   if ( myObj->_is_nil() )
238     return false;
239   else {
240     try {
241       GEOM::GEOM_IBlocksOperations::BCErrors_var aErrs;
242       theIsCompoundOfBlocks =
243         GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->CheckCompoundOfBlocks( myObj, aErrs );
244       theErrors = aErrs;
245     }
246     catch ( const SALOME::SALOME_Exception& e ) {
247       SalomeApp_Tools::QtCatchCorbaException( e );
248       return false;
249     }
250
251     return getOperation()->IsDone();
252   }
253 }
254
255 //=================================================================================
256 // function : processObject
257 // purpose  :
258 //=================================================================================
259 void MeasureGUI_CheckCompoundOfBlocksDlg::processObject()
260 {
261   QString aMsg ( "" );
262   bool isCompoundOfBlocks;
263   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
264   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
265     myGrp->TextView1->setText( aMsg );
266     myGrp->ListBox1->clear();
267     myGrp->ListBox2->clear();
268     erasePreview();
269     return;
270   }
271
272   if ( isCompoundOfBlocks ) {
273     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_NO_ERRORS" );
274     buttonOk()->setEnabled( false );
275     buttonApply()->setEnabled( false );
276   } 
277   else {
278     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_ERRORS" );
279     buttonOk()->setEnabled( true );
280     buttonApply()->setEnabled( true );
281   }
282   myGrp->TextView1->setText( aMsg );
283
284   QStringList aErrList;
285   QString aErrStr( "" );
286   QString aConSfx( " # " );
287   QString aGluedSfx( " # " );
288   int aConNum = 1;
289   int aGluedNum = 1;
290   for ( int i = 0, n = aErrs.length(); i < n; i++ ) {
291     aErrStr = "";
292     switch ( aErrs[i].error ) {
293       case GEOM::GEOM_IBlocksOperations::NOT_BLOCK :
294         aErrStr = "Not a Block";
295         break;
296       case GEOM::GEOM_IBlocksOperations::EXTRA_EDGE :
297         aErrStr = "Extra Edge";
298         break;
299       case GEOM::GEOM_IBlocksOperations::INVALID_CONNECTION :
300         aErrStr = "Invalid Connection";
301         aErrStr += aConSfx;
302         aErrStr += QString::number( aConNum );
303         aConNum++;
304         break;
305       case GEOM::GEOM_IBlocksOperations::NOT_CONNECTED :
306         aErrStr = "Not Connected";
307         break;
308       case GEOM::GEOM_IBlocksOperations::NOT_GLUED :
309         aErrStr = "Not Glued";
310         aErrStr += aGluedSfx;
311         aErrStr += QString::number( aGluedNum );
312         aGluedNum++;
313         break;
314       default :
315         aErrStr = "";
316         break;
317     }
318     if ( !aErrStr.isEmpty() )
319       aErrList.append( aErrStr );
320   }
321
322   myGrp->ListBox1->clear();
323   myGrp->ListBox2->clear();
324   myGrp->ListBox1->addItems( aErrList );
325 }
326
327 //=================================================================================
328 // function : createOperation
329 // purpose  :
330 //=================================================================================
331 GEOM::GEOM_IOperations_ptr MeasureGUI_CheckCompoundOfBlocksDlg::createOperation()
332 {
333   return getGeomEngine()->GetIBlocksOperations( getStudyId() );
334 }
335
336 //=================================================================================
337 // function : onErrorsListSelectionChanged
338 // purpose  :
339 //=================================================================================
340 void MeasureGUI_CheckCompoundOfBlocksDlg::onErrorsListSelectionChanged()
341 {
342   erasePreview();
343   int aCurItem = myGrp->ListBox1->currentRow();
344   if ( aCurItem < 0 )
345     return;
346   bool isCompoundOfBlocks;
347   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
348   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
349     myGrp->TextView1->setText( "" );
350     myGrp->ListBox1->clear();
351     myGrp->ListBox2->clear();
352     return;
353   }
354   
355   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aCurItem];
356   GEOM::ListOfLong aObjLst = aErr.incriminated;
357   TopoDS_Shape aSelShape;
358   TopoDS_Shape aSubShape; 
359   TopTools_IndexedMapOfShape anIndices;
360   QStringList aSubShapeList;
361   QString aSubShapeName( "" );
362   Standard_CString aTypeString;
363   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
364     TopExp::MapShapes( aSelShape, anIndices);
365     for ( int i = 0, n = aObjLst.length(); i < n; i++ ) {
366       aSubShapeName = "";
367       aSubShape = anIndices.FindKey(aObjLst[i]);
368       if ( GEOMBase::GetShapeTypeString( aSubShape, aTypeString ) )     
369         aSubShapeName = QString( aTypeString ) + QString( "_" ) + QString::number( aObjLst[i] );        
370       if ( !aSubShapeName.isEmpty() )
371       aSubShapeList.append( aSubShapeName );
372     }
373   }
374   myGrp->ListBox2->clear();
375   myGrp->ListBox2->addItems( aSubShapeList );
376 }
377
378 //=================================================================================
379 // function : onSubShapesListSelectionChanged
380 // purpose  :
381 //=================================================================================
382 void MeasureGUI_CheckCompoundOfBlocksDlg::onSubShapesListSelectionChanged()
383 {
384   erasePreview();
385   int aErrCurItem = myGrp->ListBox1->currentRow();
386   if ( aErrCurItem < 0 )
387     return;
388   QList<int> aIds;
389   for ( int i = 0, n = myGrp->ListBox2->count(); i < n; i++ ) {
390     if ( myGrp->ListBox2->item( i )->isSelected() ) 
391       aIds.append( i );
392   }
393   if ( aIds.count() < 1 )
394     return;
395   bool isCompoundOfBlocks;
396   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
397   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
398     myGrp->TextView1->setText( "" );
399     myGrp->ListBox1->clear();
400     myGrp->ListBox2->clear();
401     return;
402   }
403   
404   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aErrCurItem];
405   GEOM::ListOfLong aObjLst = aErr.incriminated;
406   TopoDS_Shape aSelShape;
407   TopoDS_Shape aSubShape; 
408   TopTools_IndexedMapOfShape anIndices;
409   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
410     QString aMess;
411     if ( !isValid( aMess ) ) {
412       return;
413     }
414     SALOME_Prs* aPrs = 0;
415     TopExp::MapShapes( aSelShape, anIndices);
416     QList<int>::iterator it;
417     for ( it = aIds.begin(); it != aIds.end(); ++it ) {
418       aSubShape = anIndices.FindKey(aObjLst[(*it)]);
419       try {
420         getDisplayer()->SetColor( Quantity_NOC_RED );
421         getDisplayer()->SetWidth( 3 );
422         getDisplayer()->SetToActivate( false );
423         aPrs = !aSubShape.IsNull() ? getDisplayer()->BuildPrs( aSubShape ) : 0;
424         if ( aPrs )
425           displayPreview( aPrs, true );
426       }
427       catch ( const SALOME::SALOME_Exception& e ) {
428         SalomeApp_Tools::QtCatchCorbaException( e );
429       }
430     }
431   }
432 }
433
434 //=================================================================================
435 // function : activateSelection
436 // purpose  : activate selection of faces, shells, and solids
437 //=================================================================================
438 void MeasureGUI_CheckCompoundOfBlocksDlg::activateSelection()
439 {
440   TColStd_MapOfInteger aMap;
441   aMap.Add( GEOM_SOLID );
442   aMap.Add( GEOM_COMPOUND );
443   globalSelection( aMap );
444 }
445
446 //=================================================================================
447 // function : enterEvent()
448 // purpose  :
449 //=================================================================================
450 void MeasureGUI_CheckCompoundOfBlocksDlg::enterEvent( QEvent* )
451 {
452   if ( !mainFrame()->GroupConstructors->isEnabled() )
453     ActivateThisDialog();
454 }
455
456 //=================================================================================
457 // function : isValid
458 // purpose  :
459 //=================================================================================
460 bool MeasureGUI_CheckCompoundOfBlocksDlg::isValid( QString& )
461 {
462   return !myObj->_is_nil();
463 }
464
465 //=================================================================================
466 // function : execute
467 // purpose  :
468 //=================================================================================
469 bool MeasureGUI_CheckCompoundOfBlocksDlg::execute( ObjectList& objects )
470 {
471   GEOM::GEOM_Object_var anObj;
472
473   anObj = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->CheckAndImprove( myObj );
474
475   if ( !anObj->_is_nil() )
476     objects.push_back( anObj._retn() );
477
478   return true;
479 }