Salome HOME
updated copyright message
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CheckCompoundOfBlocksDlg.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : MeasureGUI_CheckCompoundOfBlocksDlg.cxx
25 // Author : Vladimir KLYACHIN, Open CASCADE S.A.S. (vladimir.klyachin@opencascade.com)
26 //
27 #include "MeasureGUI.h"
28 #include "MeasureGUI_CheckCompoundOfBlocksDlg.h"
29
30 #include <SUIT_Session.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <LightApp_SelectionMgr.h>
33 #include <SalomeApp_Tools.h>
34 #include <SalomeApp_Application.h>
35
36 #include <TopTools_IndexedMapOfShape.hxx>
37 #include <TopExp.hxx>
38 #include <TColStd_MapOfInteger.hxx>
39
40 #include <DlgRef.h>
41 #include <GeometryGUI.h>
42 #include <GEOMBase.h>
43 #include <GEOMImpl_Types.hxx>
44
45 #include <QListWidget>
46
47 #define TEXTEDIT_FONT_FAMILY "Courier"
48 #define TEXTEDIT_FONT_SIZE 11
49
50 //=================================================================================
51 // class    : MeasureGUI_CheckCompoundOfBlocksDlg()
52 // purpose  : Constructs a MeasureGUI_CheckCompoundOfBlocksDlg which is a child of 'parent', with the
53 //            name 'name' and widget flags set to 'f'.
54 //            The dialog will by default be modeless, unless you set 'modal' to
55 //            true to construct a modal dialog.
56 //=================================================================================
57 MeasureGUI_CheckCompoundOfBlocksDlg::MeasureGUI_CheckCompoundOfBlocksDlg( GeometryGUI* GUI, QWidget* parent )
58   : GEOMBase_Skeleton(GUI, parent, false),
59     myObjectName     (0),
60     mySelButton      (0),
61     myUseC1Check     (0),
62     myTolLbl         (0),
63     mySpinTol        (0),
64     myTextView       (0),
65     myListBox1       (0),
66     myListBox2       (0)
67 {
68   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
69   QPixmap image0( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_CHECK_COMPOUND_OF_BLOCKS" ) ) );
70   QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
71
72   setWindowTitle( tr( "GEOM_CHECK_BLOCKS_COMPOUND" ) );
73
74   /***************************************************************/
75   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_CHECK_BLOCKS_COMPOUND" ) );
76   mainFrame()->RadioButton1->setIcon( image0 );
77   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
78   mainFrame()->RadioButton2->close();
79   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
80   mainFrame()->RadioButton3->close();
81
82   QGroupBox   *aGrpParams    =
83     new QGroupBox(tr("GEOM_CHECK_INFOS"), centralWidget());
84   QGridLayout *aParamsLayout = new QGridLayout(aGrpParams);
85   QLabel      *anObjLbl      = new QLabel(tr("GEOM_OBJECT"), aGrpParams);
86   QLabel      *anErrorsLbl   =
87     new QLabel(tr("GEOM_CHECK_BLOCKS_COMPOUND_ERRORS"), aGrpParams);
88   QLabel      *aNonBlocksLbl =
89     new QLabel(tr("GEOM_CHECK_BLOCKS_COMPOUND_SUBSHAPES"), aGrpParams);
90
91   myObjectName = new QLineEdit(aGrpParams);
92   mySelButton  = new QPushButton(aGrpParams);
93   myUseC1Check = new QCheckBox(tr("GEOM_USE_C1_CRITERION"), aGrpParams);
94   myTolLbl     = new QLabel(tr("GEOM_ANGULAR_TOLERANCE"), aGrpParams);
95   mySpinTol    = new SalomeApp_DoubleSpinBox(aGrpParams);
96   myTextView   = new QTextBrowser(aGrpParams);
97   myListBox1   = new QListWidget(aGrpParams);
98   myListBox2   = new QListWidget(aGrpParams);
99
100   myObjectName->setReadOnly(true);
101   mySelButton->setIcon(image1);
102   mySelButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
103   myUseC1Check->setText(tr("GEOM_USE_C1_CRITERION"));
104   myUseC1Check->setChecked(true);
105   myTextView->setReadOnly(true);
106   myListBox2->setSelectionMode(QAbstractItemView::ExtendedSelection);
107
108   // Set text view font.
109   QFont aFont(TEXTEDIT_FONT_FAMILY, TEXTEDIT_FONT_SIZE);
110
111   aFont.setStyleHint(QFont::TypeWriter, QFont::PreferAntialias);
112   myTextView->setFont(aFont);
113
114   aParamsLayout->setMargin(9);
115   aParamsLayout->setSpacing(6);
116   aParamsLayout->addWidget(anObjLbl,      0, 0);
117   aParamsLayout->addWidget(mySelButton,   0, 1);
118   aParamsLayout->addWidget(myObjectName,  0, 2);
119   aParamsLayout->addWidget(myUseC1Check,  1, 0, 1, 3);
120   aParamsLayout->addWidget(myTolLbl,      2, 0);
121   aParamsLayout->addWidget(mySpinTol,     2, 1, 1, 2);
122   aParamsLayout->addWidget(myTextView,    3, 0, 1, 3);
123   aParamsLayout->addWidget(anErrorsLbl,   4, 0);
124   aParamsLayout->addWidget(myListBox1,    5, 0, 1, 2);
125   aParamsLayout->addWidget(aNonBlocksLbl, 4, 2);
126   aParamsLayout->addWidget(myListBox2,    5, 2);
127
128   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
129   layout->setMargin( 0 ); layout->setSpacing( 6 );
130   layout->addWidget( aGrpParams );
131
132   /***************************************************************/
133
134   myHelpFileName = "check_compound_of_blocks_page.html";
135
136   /* Initialisation */
137   Init();
138 }
139
140 //=================================================================================
141 // function : ~MeasureGUI_CheckCompoundOfBlocksDlg()
142 // purpose  : Destroys the object and frees any allocated resources
143 //=================================================================================
144 MeasureGUI_CheckCompoundOfBlocksDlg::~MeasureGUI_CheckCompoundOfBlocksDlg()
145 {
146 }
147
148 //=================================================================================
149 // function : Init()
150 // purpose  :
151 //=================================================================================
152 void MeasureGUI_CheckCompoundOfBlocksDlg::Init()
153 {
154   /* init variables */
155   double SpecificStep = 0.0001;
156   double aDefaultTol  = Precision::Angular();
157
158   initSpinBox(mySpinTol, aDefaultTol, MAX_NUMBER, SpecificStep, "ang_tol_precision");
159   mySpinTol->setValue(aDefaultTol);
160   myEditCurrentArgument = myObjectName;
161
162   // signals and slots connections
163   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
164   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
165
166   connect(myObjectName, SIGNAL(returnPressed()),        this, SLOT(LineEditReturnPressed()));
167   connect(mySelButton,  SIGNAL(clicked()),              this, SLOT(SetEditCurrentArgument()));
168   connect(myListBox1,   SIGNAL(itemSelectionChanged()), this, SLOT(onErrorsListSelectionChanged()));
169   connect(myListBox2,   SIGNAL(itemSelectionChanged()), this, SLOT(onSubShapesListSelectionChanged()));
170   connect(myUseC1Check, SIGNAL(clicked()),              this, SLOT(SetUseC1Tolerance()));
171   connect(mySpinTol,    SIGNAL(valueChanged(double)),   this, SLOT(processObject()));
172
173   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
174            this, SLOT( SelectionIntoArgument() ) );
175
176   initName( tr( "GEOM_BLOCKS_COMPOUND") );
177   buttonOk()->setEnabled( false );
178   buttonApply()->setEnabled( false );
179   activateSelection();
180   SelectionIntoArgument();
181 }
182
183 //=================================================================================
184 // function : ClickOnOk()
185 // purpose  :
186 //=================================================================================
187 void MeasureGUI_CheckCompoundOfBlocksDlg::ClickOnOk()
188 {
189   if ( ClickOnApply() )
190     ClickOnCancel();
191 }
192
193 //=================================================================================
194 // function : ClickOnApply()
195 // purpose  :
196 //=================================================================================
197 bool MeasureGUI_CheckCompoundOfBlocksDlg::ClickOnApply()
198 {
199   if ( !onAccept() )
200     return false;
201
202   initName();
203   return true;
204 }
205
206 //=================================================================================
207 // function : SelectionIntoArgument
208 // purpose  :
209 //=================================================================================
210 void MeasureGUI_CheckCompoundOfBlocksDlg::SelectionIntoArgument()
211 {
212   erasePreview();
213   myObj = GEOM::GEOM_Object::_nil();
214
215   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
216   SALOME_ListIO aSelList;
217   aSelMgr->selectedObjects(aSelList);
218
219   if (aSelList.Extent() != 1) {
220     myObjectName->setText( "" );
221     processObject();
222     return;
223   }
224
225   GEOM::GEOM_Object_var aSelectedObject =
226     GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
227
228   if ( aSelectedObject->_is_nil() ) {
229     myObjectName->setText( "" );
230     processObject();
231     return;
232   }
233
234   myObj = aSelectedObject;
235   myObjectName->setText( GEOMBase::GetName( myObj ) );
236   processObject();
237   DISPLAY_PREVIEW_MACRO;
238 }
239
240 //=================================================================================
241 // function : SetEditCurrentArgument
242 // purpose  :
243 //=================================================================================
244 void MeasureGUI_CheckCompoundOfBlocksDlg::SetEditCurrentArgument()
245 {
246   myObjectName->setFocus();
247   myEditCurrentArgument = myObjectName;
248   SelectionIntoArgument();
249 }
250
251 //=================================================================================
252 // function : SetUseC1Tolerance()
253 // purpose  :
254 //=================================================================================
255 void MeasureGUI_CheckCompoundOfBlocksDlg::SetUseC1Tolerance()
256 {
257   myTolLbl->setEnabled(myUseC1Check->isChecked());
258   mySpinTol->setEnabled(myUseC1Check->isChecked());
259   processObject();
260 }
261
262 //=================================================================================
263 // function : LineEditReturnPressed()
264 // purpose  :
265 //=================================================================================
266 void MeasureGUI_CheckCompoundOfBlocksDlg::LineEditReturnPressed()
267 {
268   QLineEdit* send = (QLineEdit*)sender();
269   if ( send == myObjectName ) {
270     myEditCurrentArgument = myObjectName;
271     GEOMBase_Skeleton::LineEditReturnPressed();
272   }
273 }
274
275 //=================================================================================
276 // function : ActivateThisDialog()
277 // purpose  :
278 //=================================================================================
279 void MeasureGUI_CheckCompoundOfBlocksDlg::ActivateThisDialog()
280 {
281   GEOMBase_Skeleton::ActivateThisDialog();
282
283   LightApp_SelectionMgr* aSel = myGeomGUI->getApp()->selectionMgr();
284   if ( aSel )
285     connect( aSel, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
286
287   activateSelection();
288   DISPLAY_PREVIEW_MACRO
289 }
290
291 //=================================================================================
292 // function : getBCErrors
293 // purpose  :
294 //=================================================================================
295 bool MeasureGUI_CheckCompoundOfBlocksDlg::getBCErrors( bool& theIsCompoundOfBlocks,
296                                                        GEOM::GEOM_IBlocksOperations::BCErrors& theErrors)
297 {
298   if ( myObj->_is_nil() )
299     return false;
300   else {
301     GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() );
302     try {
303       GEOM::GEOM_IBlocksOperations::BCErrors_var aErrs;
304       double aC1Tol = -1.;
305
306       if (myUseC1Check->isChecked()) {
307         aC1Tol = mySpinTol->value();
308       }
309
310       theIsCompoundOfBlocks = anOper->CheckCompoundOfBlocks( myObj, aC1Tol, aErrs );
311       if (anOper->IsDone() && aErrs->length() > 0)
312       //if (anOper->IsDone() && !aErrs._is_nil())
313         theErrors = aErrs;
314     }
315     catch ( const SALOME::SALOME_Exception& e ) {
316       SalomeApp_Tools::QtCatchCorbaException( e );
317       return false;
318     }
319
320     return anOper->IsDone();
321   }
322 }
323
324 //=================================================================================
325 // function : processObject
326 // purpose  :
327 //=================================================================================
328 void MeasureGUI_CheckCompoundOfBlocksDlg::processObject()
329 {
330   QString aMsg ( "" );
331   bool isCompoundOfBlocks;
332   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
333   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
334     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_FAILED" );
335     myTextView->setText( aMsg );
336     myListBox1->clear();
337     myListBox2->clear();
338     erasePreview();
339     return;
340   }
341
342   if ( isCompoundOfBlocks ) {
343     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_NO_ERRORS" );
344     buttonOk()->setEnabled( false );
345     buttonApply()->setEnabled( false );
346   }
347   else {
348     aMsg += tr( "GEOM_CHECK_BLOCKS_COMPOUND_HAS_ERRORS" );
349     buttonOk()->setEnabled( true );
350     buttonApply()->setEnabled( true );
351   }
352   myTextView->setText( aMsg );
353
354   QStringList aErrList;
355   QString aErrStr( "" );
356   QString aConSfx( " # " );
357   QString aGluedSfx( " # " );
358   int aConNum = 1;
359   int aGluedNum = 1;
360   for ( int i = 0, n = aErrs.length(); i < n; i++ ) {
361     aErrStr = "";
362     switch ( aErrs[i].error ) {
363       case GEOM::GEOM_IBlocksOperations::NOT_BLOCK :
364         aErrStr = tr("GEOM_CHECK_BLOCKS_NOT_BLOCK");
365         break;
366       case GEOM::GEOM_IBlocksOperations::EXTRA_EDGE :
367         aErrStr = tr("GEOM_CHECK_BLOCKS_EXTRA_EDGE");
368         break;
369       case GEOM::GEOM_IBlocksOperations::INVALID_CONNECTION :
370         aErrStr = tr("GEOM_CHECK_BLOCKS_INVALID_CONNECTION").arg(aConNum++);
371         break;
372       case GEOM::GEOM_IBlocksOperations::NOT_CONNECTED :
373         aErrStr = tr("GEOM_CHECK_BLOCKS_NOT_CONNECTED");
374         break;
375       case GEOM::GEOM_IBlocksOperations::NOT_GLUED :
376         aErrStr = tr("GEOM_CHECK_BLOCKS_NOT_GLUED").arg(aGluedNum++);
377         break;
378       default :
379         aErrStr = "";
380         break;
381     }
382     if ( !aErrStr.isEmpty() )
383       aErrList.append( aErrStr );
384   }
385
386   myListBox1->clear();
387   myListBox2->clear();
388   myListBox1->addItems( aErrList );
389 }
390
391 //=================================================================================
392 // function : createOperation
393 // purpose  :
394 //=================================================================================
395 GEOM::GEOM_IOperations_ptr MeasureGUI_CheckCompoundOfBlocksDlg::createOperation()
396 {
397   return getGeomEngine()->GetIBlocksOperations();
398 }
399
400 //=================================================================================
401 // function : onErrorsListSelectionChanged
402 // purpose  :
403 //=================================================================================
404 void MeasureGUI_CheckCompoundOfBlocksDlg::onErrorsListSelectionChanged()
405 {
406   erasePreview();
407   int aCurItem = myListBox1->currentRow();
408   if ( aCurItem < 0 )
409     return;
410   bool isCompoundOfBlocks;
411   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
412   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
413     myTextView->setText( "" );
414     myListBox1->clear();
415     myListBox2->clear();
416     return;
417   }
418
419   myListBox2->clear();
420
421   if (aCurItem < (int)aErrs.length()) {
422     GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aCurItem];
423     GEOM::ListOfLong aObjLst = aErr.incriminated;
424     QStringList aSubShapeList;
425     TopoDS_Shape aSelShape;
426     if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
427       TopTools_IndexedMapOfShape anIndices;
428       TopExp::MapShapes( aSelShape, anIndices );
429       for ( int i = 0, n = aObjLst.length(); i < n; i++ ) {
430         TopoDS_Shape aSubShape = anIndices.FindKey( aObjLst[i] );
431         QString aType = GEOMBase::GetShapeTypeString( aSubShape );
432         if ( !aType.isEmpty() )
433           aSubShapeList.append( QString( "%1_%2" ).arg( aType ).arg( aObjLst[i] ) );
434       }
435     }
436     myListBox2->addItems( aSubShapeList );
437   }
438 }
439
440 //=================================================================================
441 // function : onSubShapesListSelectionChanged
442 // purpose  :
443 //=================================================================================
444 void MeasureGUI_CheckCompoundOfBlocksDlg::onSubShapesListSelectionChanged()
445 {
446   erasePreview();
447   int aErrCurItem = myListBox1->currentRow();
448   if ( aErrCurItem < 0 )
449     return;
450   QList<int> aIds;
451   for ( int i = 0, n = myListBox2->count(); i < n; i++ ) {
452     if ( myListBox2->item( i )->isSelected() )
453       aIds.append( i );
454   }
455   if ( aIds.count() < 1 )
456     return;
457   bool isCompoundOfBlocks;
458   GEOM::GEOM_IBlocksOperations::BCErrors aErrs;
459   if ( !getBCErrors( isCompoundOfBlocks, aErrs ) ) {
460     myTextView->setText( "" );
461     myListBox1->clear();
462     myListBox2->clear();
463     return;
464   }
465
466   GEOM::GEOM_IBlocksOperations::BCError aErr = aErrs[aErrCurItem];
467   GEOM::ListOfLong aObjLst = aErr.incriminated;
468   TopoDS_Shape aSelShape;
469   TopoDS_Shape aSubShape;
470   TopTools_IndexedMapOfShape anIndices;
471   if ( !myObj->_is_nil() && GEOMBase::GetShape( myObj, aSelShape ) ) {
472     QString aMess;
473     if ( !isValid( aMess ) ) {
474       return;
475     }
476     SALOME_Prs* aPrs = 0;
477     TopExp::MapShapes( aSelShape, anIndices);
478     QList<int>::iterator it;
479     for ( it = aIds.begin(); it != aIds.end(); ++it ) {
480       aSubShape = anIndices.FindKey(aObjLst[(*it)]);
481       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
482       int w = resMgr->integerValue("Geometry", "preview_edge_width", 1);
483       try {
484         getDisplayer()->SetColor( Quantity_NOC_RED );
485         getDisplayer()->SetWidth( w );
486         getDisplayer()->SetToActivate( false );
487         aPrs = !aSubShape.IsNull() ? getDisplayer()->BuildPrs( aSubShape ) : 0;
488         if ( aPrs )
489           displayPreview( aPrs, true );
490       }
491       catch ( const SALOME::SALOME_Exception& e ) {
492         SalomeApp_Tools::QtCatchCorbaException( e );
493       }
494     }
495   }
496 }
497
498 //=================================================================================
499 // function : onDisplayPreview
500 // purpose  : 
501 //=================================================================================
502 void MeasureGUI_CheckCompoundOfBlocksDlg::onDisplayPreview()
503 {
504   DISPLAY_PREVIEW_MACRO;
505 }
506
507 //=================================================================================
508 // function : activateSelection
509 // purpose  : activate selection of faces, shells, and solids
510 //=================================================================================
511 void MeasureGUI_CheckCompoundOfBlocksDlg::activateSelection()
512 {
513   TColStd_MapOfInteger aMap;
514   aMap.Add( GEOM_SOLID );
515   aMap.Add( GEOM_COMPOUND );
516   globalSelection( aMap );
517 }
518
519 //=================================================================================
520 // function : enterEvent()
521 // purpose  :
522 //=================================================================================
523 void MeasureGUI_CheckCompoundOfBlocksDlg::enterEvent( QEvent* )
524 {
525   if ( !mainFrame()->GroupConstructors->isEnabled() )
526     ActivateThisDialog();
527 }
528
529 //=================================================================================
530 // function : isValid
531 // purpose  :
532 //=================================================================================
533 bool MeasureGUI_CheckCompoundOfBlocksDlg::isValid( QString &msg)
534 {
535   return !myObj->_is_nil() && mySpinTol->isValid(msg, !IsPreview());
536 }
537
538 //=================================================================================
539 // function : execute
540 // purpose  :
541 //=================================================================================
542 bool MeasureGUI_CheckCompoundOfBlocksDlg::execute( ObjectList& objects )
543 {
544   GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() );
545   GEOM::GEOM_Object_var anObj = anOper->CheckAndImprove( myObj );
546
547   if ( !anObj->_is_nil() )
548     objects.push_back( anObj._retn() );
549
550   return true;
551 }