Salome HOME
0020598: EDF 1191 GEOM : Creation of hexa block from two faces
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CheckCompoundOfBlocksDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : MeasureGUI_CheckCompoundOfBlocksDlg.cxx
24 // Author : Vladimir KLYACHIN, Open CASCADE S.A.S. (vladimir.klyachin@opencascade.com)
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   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
169   SALOME_ListIO aSelList;
170   aSelMgr->selectedObjects(aSelList);
171
172   if (aSelList.Extent() != 1) {
173     myGrp->LineEdit1->setText( "" );
174     processObject();
175     return;
176   }
177
178   Standard_Boolean testResult = Standard_False;
179   GEOM::GEOM_Object_var aSelectedObject =
180     GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
181
182   if ( !testResult || aSelectedObject->_is_nil() ) {
183     myGrp->LineEdit1->setText( "" );
184     processObject();
185     return;
186   }
187
188   myObj = aSelectedObject;
189   myGrp->LineEdit1->setText( GEOMBase::GetName( myObj ) );
190   processObject();
191   displayPreview();
192 }
193
194 //=================================================================================
195 // function : SetEditCurrentArgument
196 // purpose  :
197 //=================================================================================
198 void MeasureGUI_CheckCompoundOfBlocksDlg::SetEditCurrentArgument()
199 {
200   myGrp->LineEdit1->setFocus();
201   myEditCurrentArgument = myGrp->LineEdit1;
202   SelectionIntoArgument();
203 }
204
205 //=================================================================================
206 // function : LineEditReturnPressed()
207 // purpose  :
208 //=================================================================================
209 void MeasureGUI_CheckCompoundOfBlocksDlg::LineEditReturnPressed()
210 {
211   QLineEdit* send = (QLineEdit*)sender();
212   if ( send == myGrp->LineEdit1 ) {
213     myEditCurrentArgument = myGrp->LineEdit1;
214     GEOMBase_Skeleton::LineEditReturnPressed();
215   }
216 }
217
218 //=================================================================================
219 // function : ActivateThisDialog()
220 // purpose  :
221 //=================================================================================
222 void MeasureGUI_CheckCompoundOfBlocksDlg::ActivateThisDialog()
223 {
224   GEOMBase_Skeleton::ActivateThisDialog();
225
226   LightApp_SelectionMgr* aSel = myGeomGUI->getApp()->selectionMgr();
227   if ( aSel )
228     connect( aSel, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
229
230   activateSelection();
231   displayPreview();
232 }
233
234 //=================================================================================
235 // function : getBCErrors
236 // purpose  :
237 //=================================================================================
238 bool MeasureGUI_CheckCompoundOfBlocksDlg::getBCErrors( bool& theIsCompoundOfBlocks,
239                                                        GEOM::GEOM_IBlocksOperations::BCErrors& theErrors)
240 {
241   if ( myObj->_is_nil() )
242     return false;
243   else {
244     GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() );
245     try {
246       GEOM::GEOM_IBlocksOperations::BCErrors_var aErrs;
247       theIsCompoundOfBlocks = anOper->CheckCompoundOfBlocks( myObj, aErrs );
248       if (anOper->IsDone() && aErrs->length() > 0)
249       //if (anOper->IsDone() && !aErrs._is_nil())
250         theErrors = aErrs;
251     }
252     catch ( const SALOME::SALOME_Exception& e ) {
253       SalomeApp_Tools::QtCatchCorbaException( e );
254       return false;
255     }
256
257     return anOper->IsDone();
258   }
259 }
260
261 //=================================================================================
262 // function : processObject
263 // purpose  :
264 //=================================================================================
265 void MeasureGUI_CheckCompoundOfBlocksDlg::processObject()
266 {
267   QString aMsg ( "" );
268   bool isCompoundOfBlocks;
269   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
270   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
271     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_FAILED" );
272     myGrp->TextView1->setText( aMsg );
273     myGrp->ListBox1->clear();
274     myGrp->ListBox2->clear();
275     erasePreview();
276     return;
277   }
278
279   if ( isCompoundOfBlocks ) {
280     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_NO_ERRORS" );
281     buttonOk()->setEnabled( false );
282     buttonApply()->setEnabled( false );
283   }
284   else {
285     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_ERRORS" );
286     buttonOk()->setEnabled( true );
287     buttonApply()->setEnabled( true );
288   }
289   myGrp->TextView1->setText( aMsg );
290
291   QStringList aErrList;
292   QString aErrStr( "" );
293   QString aConSfx( " # " );
294   QString aGluedSfx( " # " );
295   int aConNum = 1;
296   int aGluedNum = 1;
297   for ( int i = 0, n = aErrs.length(); i < n; i++ ) {
298     aErrStr = "";
299     switch ( aErrs[i].error ) {
300       case GEOM::GEOM_IBlocksOperations::NOT_BLOCK :
301         aErrStr = "Not a Block";
302         break;
303       case GEOM::GEOM_IBlocksOperations::EXTRA_EDGE :
304         aErrStr = "Extra Edge";
305         break;
306       case GEOM::GEOM_IBlocksOperations::INVALID_CONNECTION :
307         aErrStr = "Invalid Connection";
308         aErrStr += aConSfx;
309         aErrStr += QString::number( aConNum );
310         aConNum++;
311         break;
312       case GEOM::GEOM_IBlocksOperations::NOT_CONNECTED :
313         aErrStr = "Not Connected";
314         break;
315       case GEOM::GEOM_IBlocksOperations::NOT_GLUED :
316         aErrStr = "Not Glued";
317         aErrStr += aGluedSfx;
318         aErrStr += QString::number( aGluedNum );
319         aGluedNum++;
320         break;
321       default :
322         aErrStr = "";
323         break;
324     }
325     if ( !aErrStr.isEmpty() )
326       aErrList.append( aErrStr );
327   }
328
329   myGrp->ListBox1->clear();
330   myGrp->ListBox2->clear();
331   myGrp->ListBox1->addItems( aErrList );
332 }
333
334 //=================================================================================
335 // function : createOperation
336 // purpose  :
337 //=================================================================================
338 GEOM::GEOM_IOperations_ptr MeasureGUI_CheckCompoundOfBlocksDlg::createOperation()
339 {
340   return getGeomEngine()->GetIBlocksOperations( getStudyId() );
341 }
342
343 //=================================================================================
344 // function : onErrorsListSelectionChanged
345 // purpose  :
346 //=================================================================================
347 void MeasureGUI_CheckCompoundOfBlocksDlg::onErrorsListSelectionChanged()
348 {
349   erasePreview();
350   int aCurItem = myGrp->ListBox1->currentRow();
351   if ( aCurItem < 0 )
352     return;
353   bool isCompoundOfBlocks;
354   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
355   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
356     myGrp->TextView1->setText( "" );
357     myGrp->ListBox1->clear();
358     myGrp->ListBox2->clear();
359     return;
360   }
361
362   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aCurItem];
363   GEOM::ListOfLong aObjLst = aErr.incriminated;
364   TopoDS_Shape aSelShape;
365   TopoDS_Shape aSubShape;
366   TopTools_IndexedMapOfShape anIndices;
367   QStringList aSubShapeList;
368   QString aSubShapeName( "" );
369   Standard_CString aTypeString;
370   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
371     TopExp::MapShapes( aSelShape, anIndices);
372     for ( int i = 0, n = aObjLst.length(); i < n; i++ ) {
373       aSubShapeName = "";
374       aSubShape = anIndices.FindKey(aObjLst[i]);
375       if ( GEOMBase::GetShapeTypeString( aSubShape, aTypeString ) )
376         aSubShapeName = QString( aTypeString ) + QString( "_" ) + QString::number( aObjLst[i] );
377       if ( !aSubShapeName.isEmpty() )
378       aSubShapeList.append( aSubShapeName );
379     }
380   }
381   myGrp->ListBox2->clear();
382   myGrp->ListBox2->addItems( aSubShapeList );
383 }
384
385 //=================================================================================
386 // function : onSubShapesListSelectionChanged
387 // purpose  :
388 //=================================================================================
389 void MeasureGUI_CheckCompoundOfBlocksDlg::onSubShapesListSelectionChanged()
390 {
391   erasePreview();
392   int aErrCurItem = myGrp->ListBox1->currentRow();
393   if ( aErrCurItem < 0 )
394     return;
395   QList<int> aIds;
396   for ( int i = 0, n = myGrp->ListBox2->count(); i < n; i++ ) {
397     if ( myGrp->ListBox2->item( i )->isSelected() )
398       aIds.append( i );
399   }
400   if ( aIds.count() < 1 )
401     return;
402   bool isCompoundOfBlocks;
403   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
404   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
405     myGrp->TextView1->setText( "" );
406     myGrp->ListBox1->clear();
407     myGrp->ListBox2->clear();
408     return;
409   }
410
411   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aErrCurItem];
412   GEOM::ListOfLong aObjLst = aErr.incriminated;
413   TopoDS_Shape aSelShape;
414   TopoDS_Shape aSubShape;
415   TopTools_IndexedMapOfShape anIndices;
416   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
417     QString aMess;
418     if ( !isValid( aMess ) ) {
419       return;
420     }
421     SALOME_Prs* aPrs = 0;
422     TopExp::MapShapes( aSelShape, anIndices);
423     QList<int>::iterator it;
424     for ( it = aIds.begin(); it != aIds.end(); ++it ) {
425       aSubShape = anIndices.FindKey(aObjLst[(*it)]);
426       try {
427         getDisplayer()->SetColor( Quantity_NOC_RED );
428         getDisplayer()->SetWidth( 3 );
429         getDisplayer()->SetToActivate( false );
430         aPrs = !aSubShape.IsNull() ? getDisplayer()->BuildPrs( aSubShape ) : 0;
431         if ( aPrs )
432           displayPreview( aPrs, true );
433       }
434       catch ( const SALOME::SALOME_Exception& e ) {
435         SalomeApp_Tools::QtCatchCorbaException( e );
436       }
437     }
438   }
439 }
440
441 //=================================================================================
442 // function : activateSelection
443 // purpose  : activate selection of faces, shells, and solids
444 //=================================================================================
445 void MeasureGUI_CheckCompoundOfBlocksDlg::activateSelection()
446 {
447   TColStd_MapOfInteger aMap;
448   aMap.Add( GEOM_SOLID );
449   aMap.Add( GEOM_COMPOUND );
450   globalSelection( aMap );
451 }
452
453 //=================================================================================
454 // function : enterEvent()
455 // purpose  :
456 //=================================================================================
457 void MeasureGUI_CheckCompoundOfBlocksDlg::enterEvent( QEvent* )
458 {
459   if ( !mainFrame()->GroupConstructors->isEnabled() )
460     ActivateThisDialog();
461 }
462
463 //=================================================================================
464 // function : isValid
465 // purpose  :
466 //=================================================================================
467 bool MeasureGUI_CheckCompoundOfBlocksDlg::isValid( QString& )
468 {
469   return !myObj->_is_nil();
470 }
471
472 //=================================================================================
473 // function : execute
474 // purpose  :
475 //=================================================================================
476 bool MeasureGUI_CheckCompoundOfBlocksDlg::execute( ObjectList& objects )
477 {
478   GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() );
479   GEOM::GEOM_Object_var anObj = anOper->CheckAndImprove( myObj );
480
481   if ( !anObj->_is_nil() )
482     objects.push_back( anObj._retn() );
483
484   return true;
485 }