Salome HOME
[bos #40619][CEA] Add Fuzzy parameter to partition and boolean operators
[modules/geom.git] / src / BooleanGUI / BooleanGUI_Dialog.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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   : BooleanGUI_Dialog.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "BooleanGUI.h"
28 #include "BooleanGUI_Dialog.h"
29
30 #include <DlgRef.h>
31 #include <GeometryGUI.h>
32 #include <GEOMBase.h>
33
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SalomeApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <SALOME_ListIO.hxx>
39
40 // VSR 22/08/2012: issue 0021787: remove "Preview" button from BOP and Partition operations
41 // Comment next line to enable preview in BOP dialog box
42 #define NO_PREVIEW
43
44 #define DEFAULT_FUZZY_VALUE   1e-5
45
46
47 //=================================================================================
48 // class    : BooleanGUI_Dialog()
49 // purpose  : Constructs a BooleanGUI_Dialog 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 BooleanGUI_Dialog::BooleanGUI_Dialog (const int theOperation, GeometryGUI* theGeometryGUI,
55                                       QWidget* parent, bool modal, Qt::WindowFlags fl)
56   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
57     myOperation(theOperation)
58 {
59   QPixmap image0;
60   QString aTitle, aCaption;
61   switch (myOperation) {
62   case BooleanGUI::COMMON:
63     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_COMMON")));
64     aTitle = tr("GEOM_COMMON");
65     aCaption = tr("GEOM_COMMON_TITLE");
66     setHelpFileName("common_operation_page.html");
67     break;
68   case BooleanGUI::CUT:
69     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_CUT")));
70     aTitle = tr("GEOM_CUT");
71     aCaption = tr("GEOM_CUT_TITLE");
72     setHelpFileName("cut_operation_page.html");
73     break;
74   case BooleanGUI::FUSE:
75     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FUSE")));
76     aTitle = tr("GEOM_FUSE");
77     aCaption = tr("GEOM_FUSE_TITLE");
78     setHelpFileName("fuse_operation_page.html");
79     break;
80   case BooleanGUI::SECTION:
81     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SECTION")));
82     aTitle = tr("GEOM_SECTION");
83     aCaption = tr("GEOM_SECTION_TITLE");
84     setHelpFileName("section_operation_page.html");
85     break;
86   }
87   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
88
89   setWindowTitle(aCaption);
90
91   /***************************************************************/
92   mainFrame()->GroupConstructors->setTitle(aTitle);
93   mainFrame()->RadioButton1->setIcon(image0);
94   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
95   mainFrame()->RadioButton2->close();
96   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
97   mainFrame()->RadioButton3->close();
98
99   myGroup = new DlgRef_2Sel2Spin3Check(centralWidget());
100
101   myGroup->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
102   if (myOperation == BooleanGUI::CUT) {
103     myGroup->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
104     myGroup->TextLabel2->setText(tr("GEOM_TOOL_OBJECTS"));
105   }
106   else if (myOperation == BooleanGUI::SECTION) {
107     myGroup->TextLabel1->setText(tr("GEOM_OBJECT_I").arg(1));
108     myGroup->TextLabel2->setText(tr("GEOM_OBJECT_I").arg(2));
109   } else { // Fuse or Common
110     myGroup->TextLabel1->setText(tr( "GEOM_SELECTED_OBJECTS" ));
111     myGroup->TextLabel2->hide();
112     myGroup->PushButton2->hide();
113     myGroup->LineEdit2->hide();
114
115     if (myOperation == BooleanGUI::FUSE) {
116       myGroup->CheckBox2->setText(tr("GEOM_BOOL_REMOVE_EXTRA_EDGES"));
117     }
118   }
119
120   myGroup->PushButton1->setIcon(image1);
121   myGroup->LineEdit1->setReadOnly(true);
122
123   if (myOperation != BooleanGUI::FUSE) {
124     myGroup->CheckBox2->hide();
125
126     if (myOperation != BooleanGUI::COMMON) {
127       myGroup->PushButton2->setIcon(image1);
128       myGroup->LineEdit2->setReadOnly(true);
129     }
130   }
131
132   myGroup->TextLabel3->hide();
133   myGroup->TextLabel4->hide();
134   myGroup->SpinBox_DX->hide();
135   myGroup->SpinBox_DY->hide();
136   myGroup->CheckBox1->setText(tr("GEOM_CHECK_SELF_INTERSECTIONS"));
137
138   // Add all "fuzzy parameter" related widgets
139   myGroup->CheckBox3->setText(tr("GEOM_USE_FUZZY_PARAMETER"));
140
141   myFuzzyLbl = new QLabel(myGroup->GroupBox1);
142   myFuzzyLbl->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
143   myFuzzyLbl->setText(tr("GEOM_FUZZY_PARAMETER"));
144   myGroup->gridLayout1->addWidget(myFuzzyLbl, 7, 0, 1, 2);
145
146   myFuzzyEdt = new SalomeApp_DoubleSpinBox(myGroup->GroupBox1);
147   initSpinBox( myFuzzyEdt, 1.e-7, 10.0, DEFAULT_FUZZY_VALUE, "length_tol_precision" );
148   myFuzzyEdt->setValue(DEFAULT_FUZZY_VALUE);
149   myGroup->gridLayout1->addWidget(myFuzzyEdt, 7, 2);
150
151   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
152   layout->setMargin(0); layout->setSpacing(6);
153   layout->addWidget(myGroup);
154   /***************************************************************/
155
156 #ifdef NO_PREVIEW
157   mainFrame()->CheckBoxPreview->setChecked( false );
158   mainFrame()->CheckBoxPreview->hide();
159 #endif
160   // Initialisation
161   Init();
162 }
163
164 //=================================================================================
165 // function : ~BooleanGUI_Dialog()
166 // purpose  : Destroys the object and frees any allocated resources
167 //=================================================================================
168 BooleanGUI_Dialog::~BooleanGUI_Dialog()
169 {
170 }
171
172 //=================================================================================
173 // function : Init()
174 // purpose  :
175 //=================================================================================
176 void BooleanGUI_Dialog::Init()
177 {
178   mainFrame()->GroupBoxPublish->show();
179
180   // init variables
181   myEditCurrentArgument = myGroup->LineEdit1;
182
183   myGroup->LineEdit1->setText("");
184   myGroup->LineEdit2->setText("");
185   myGroup->CheckBox1->setChecked(true);
186
187   if (myOperation == BooleanGUI::FUSE) {
188     myGroup->CheckBox2->setChecked(true);
189   }
190
191   // Do not use Fuzzy parameter by default
192   myGroup->CheckBox3->setChecked(false);
193   myFuzzyLbl->setEnabled(false);
194   myFuzzyEdt->setEnabled(false);
195
196   myObject1.nullify();
197   reset();
198  
199   // signals and slots connections
200   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
201   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
202
203   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
204
205   if (!myGroup->PushButton2->isHidden()) {
206     connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
207   }
208
209   if (!myGroup->CheckBox3->isHidden()) {
210     connect(myGroup->CheckBox3, SIGNAL(stateChanged(int)), this, SLOT(UseFuzzyChanged(int)));
211   }
212
213   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
214           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()), Qt::UniqueConnection);
215
216   initName(mainFrame()->GroupConstructors->title());
217
218   setTabOrder(mainFrame()->GroupConstructors, mainFrame()->GroupBoxName);
219   setTabOrder(mainFrame()->GroupBoxName, mainFrame()->GroupMedium);
220   setTabOrder(mainFrame()->GroupMedium, mainFrame()->GroupButtons);
221
222   mainFrame()->RadioButton1->setFocus();
223
224   globalSelection(GEOM_ALLSHAPES);
225   //localSelection(TopAbs_SHAPE); // VSR 24/09/2015: deactivate local selection in BOP (CoTech decision)
226   
227   myGroup->PushButton1->click();
228   resize(100,100);
229 }
230
231 //=================================================================================
232 // function : ClickOnOk()
233 // purpose  :
234 //=================================================================================
235 void BooleanGUI_Dialog::ClickOnOk()
236 {
237   setIsApplyAndClose( true );
238   if (ClickOnApply())
239     ClickOnCancel();
240 }
241
242 //=================================================================================
243 // function : ClickOnApply()
244 // purpose  :
245 //=================================================================================
246 bool BooleanGUI_Dialog::ClickOnApply()
247 {
248   if (!onAccept())
249     return false;
250
251   initName();
252   // activate selection and connect selection manager
253   myGroup->PushButton1->click();
254   return true;
255 }
256
257 //=================================================================================
258 // function : reset()
259 // purpose  : 
260 //=================================================================================
261 void BooleanGUI_Dialog::reset()
262 {
263   myObjects.clear();
264 }
265
266 //=================================================================================
267 // function : singleSelection
268 // purpose  : Performs single selection. Called from SelectionIntoArgument()
269 //=================================================================================
270 void BooleanGUI_Dialog::singleSelection()
271 {
272   myEditCurrentArgument->setText("");
273
274   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_SHAPE );
275   TopoDS_Shape aShape;
276   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
277     QString aName = GEOMBase::GetName( aSelectedObject.get() );
278     myEditCurrentArgument->setText( aName );
279
280     // clear selection
281     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
282     myGeomGUI->getApp()->selectionMgr()->clearSelected();
283     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
284             this, SLOT(SelectionIntoArgument()), Qt::UniqueConnection);
285
286     if (myEditCurrentArgument == myGroup->LineEdit1) {
287       myObject1 = aSelectedObject;
288       if (!myGroup->PushButton2->isHidden() && !myObjects.count())
289         myGroup->PushButton2->click();
290     }
291     else if (myEditCurrentArgument == myGroup->LineEdit2) {
292       myObjects.clear();
293       myObjects << aSelectedObject;
294       if (!myObject1)
295         myGroup->PushButton1->click();
296     }
297   }
298   else {
299     if      (myEditCurrentArgument == myGroup->LineEdit1) myObject1.nullify();
300     else if (myEditCurrentArgument == myGroup->LineEdit2) reset();
301   }
302 }
303
304 //=================================================================================
305 // function : multipleSelection
306 // purpose  : Performs multiple selection. Called from SelectionIntoArgument()
307 //=================================================================================
308 void BooleanGUI_Dialog::multipleSelection()
309 {
310   myEditCurrentArgument->setText( "" );
311   reset();
312         
313   myObjects = getSelected( TopAbs_SHAPE, -1 );
314
315   int i = myObjects.count();
316   if ( i == 1 ) {
317     myEditCurrentArgument->setText( GEOMBase::GetName( myObjects.first().get() ) );
318   } else if ( i > 0 ) {
319     myEditCurrentArgument->setText( QString::number( i ) + "_" + tr( "GEOM_OBJECTS" ) );
320   }
321 }
322
323 //=================================================================================
324 // function : SelectionIntoArgument()
325 // purpose  : Called when selection is changed or on dialog initialization or activation
326 //=================================================================================
327 void BooleanGUI_Dialog::SelectionIntoArgument()
328 {
329   myEditCurrentArgument->setText("");
330   if ( myOperation == BooleanGUI::SECTION ||
331       (myOperation == BooleanGUI::CUT &&
332        myEditCurrentArgument == myGroup->LineEdit1)) {
333     singleSelection();
334   } else {
335     multipleSelection();
336   }
337
338   processPreview();
339 }
340
341 //=================================================================================
342 // function : UseFuzzyChanged()
343 // purpose  : This slot is called whenever the status of CheckButton3 has changed
344 //=================================================================================
345 void BooleanGUI_Dialog::UseFuzzyChanged(int state)
346 {
347   QCheckBox* send = (QCheckBox*)sender();
348
349   if (send == myGroup->CheckBox3) {
350     bool isChecked = (state != Qt::Unchecked);
351     myFuzzyLbl->setEnabled(isChecked);
352     myFuzzyEdt->setEnabled(isChecked);
353   }
354 }
355
356 //=================================================================================
357 // function : SetEditCurrentArgument()
358 // purpose  :
359 //=================================================================================
360 void BooleanGUI_Dialog::SetEditCurrentArgument()
361 {
362   QPushButton* send = (QPushButton*)sender();
363
364   if (send == myGroup->PushButton1) {
365     myEditCurrentArgument = myGroup->LineEdit1;
366
367     if (!myGroup->PushButton2->isHidden()) {
368       myGroup->PushButton2->setDown(false);
369       myGroup->LineEdit2->setEnabled(false);
370     }
371   }
372   else if (send == myGroup->PushButton2) {
373     myEditCurrentArgument = myGroup->LineEdit2;
374
375     myGroup->PushButton1->setDown(false);
376     myGroup->LineEdit1->setEnabled(false);
377   }
378
379   globalSelection(GEOM_ALLSHAPES);
380   //localSelection(TopAbs_SHAPE); // VSR 24/09/2015: deactivate local selection in BOP (CoTech decision)
381
382   // enable line edit
383   myEditCurrentArgument->setEnabled(true);
384   myEditCurrentArgument->setFocus();
385   // after setFocus(), because it will be setDown(false) when loses focus
386   send->setDown(true);
387
388   SelectionIntoArgument();
389 }
390
391 //=================================================================================
392 // function : ActivateThisDialog()
393 // purpose  :
394 //=================================================================================
395 void BooleanGUI_Dialog::ActivateThisDialog()
396 {
397   GEOMBase_Skeleton::ActivateThisDialog();
398
399   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
400            this, SLOT( SelectionIntoArgument() ), Qt::UniqueConnection );
401   processPreview();
402 }
403
404 //=================================================================================
405 // function : enterEvent()
406 // purpose  : when mouse enter onto the QWidget
407 //=================================================================================
408 void BooleanGUI_Dialog::enterEvent (QEvent*)
409 {
410   if (!mainFrame()->GroupConstructors->isEnabled())
411     ActivateThisDialog();
412 }
413
414 //=================================================================================
415 // function : createOperation
416 // purpose  :
417 //=================================================================================
418 GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
419 {
420   return getGeomEngine()->GetIBooleanOperations();
421 }
422
423 //=================================================================================
424 // function : isValid
425 // purpose  :
426 //=================================================================================
427 bool BooleanGUI_Dialog::isValid (QString&)
428 {
429   bool isOK = false;
430
431   switch (myOperation) {
432     case BooleanGUI::FUSE:
433     case BooleanGUI::COMMON:
434       isOK = myObjects.count() > 1;
435     break;
436   case BooleanGUI::CUT:
437     isOK = myObject1 && myObjects.count();
438     break;
439   case BooleanGUI::SECTION:
440     isOK = myObject1 && (myObjects.count() == 1);
441     break;
442   default:
443     break;
444   }
445
446   return isOK;
447 }
448
449 //=================================================================================
450 // function : execute
451 // purpose  :
452 //=================================================================================
453 bool BooleanGUI_Dialog::execute (ObjectList& objects)
454 {
455   GEOM::GEOM_Object_var anObj;
456
457   GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
458   const bool isCheckSelfInte = myGroup->CheckBox1->isChecked();
459   const bool useFuzzyParam = myGroup->CheckBox3->isChecked();
460   const double fuzzyValue = (useFuzzyParam ? myFuzzyEdt->value() : -1);
461
462   GEOM::ListOfGO_var anObjects = new GEOM::ListOfGO();
463   anObjects->length( myObjects.count() );
464   for ( int i = 0; i < myObjects.count(); i++ )
465     anObjects[i] = myObjects[i].copy();
466
467   switch (myOperation) {
468     case BooleanGUI::FUSE:
469       {
470         const bool isRmExtraEdges = myGroup->CheckBox2->isChecked();
471         anObj = anOper->MakeFuseListWithFuzzy(anObjects, isCheckSelfInte, isRmExtraEdges, fuzzyValue);
472       }
473       break;
474     case BooleanGUI::COMMON:
475       anObj = anOper->MakeCommonListWithFuzzy(anObjects, isCheckSelfInte, fuzzyValue);
476       break;
477     case BooleanGUI::CUT:
478       anObj = anOper->MakeCutListWithFuzzy(myObject1.get(), anObjects, isCheckSelfInte, fuzzyValue);
479       break;
480     case BooleanGUI::SECTION:
481       anObj = anOper->MakeBooleanWithFuzzy(myObject1.get(), anObjects[0], myOperation, isCheckSelfInte, fuzzyValue);
482       break;
483     default:
484       break;
485   }
486
487   if (!anObj->_is_nil())
488     objects.push_back(anObj._retn());
489
490   return true;
491 }
492
493 //=================================================================================
494 // function : restoreSubShapes
495 // purpose  :
496 //=================================================================================
497 void BooleanGUI_Dialog::restoreSubShapes (SALOMEDS::SObject_ptr theSObject)
498 {
499   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
500     // empty list of arguments means that all arguments should be restored
501     getGeomEngine()->RestoreSubShapesSO( theSObject, GEOM::ListOfGO(),
502                                          /*theFindMethod=*/GEOM::FSM_GetInPlace, // ? GEOM::FSM_GetSame
503                                          /*theInheritFirstArg=*/myOperation == BooleanGUI::CUT,
504                                          mainFrame()->CheckBoxAddPrefix->isChecked()); // ? false
505   }
506 }
507
508 //=================================================================================
509 // function : addSubshapesToStudy
510 // purpose  : virtual method to add new SubObjects if local selection
511 //=================================================================================
512 void BooleanGUI_Dialog::addSubshapesToStudy()
513 {
514   GEOMBase::PublishSubObject( myObject1.get() );
515   for ( int i = 0; i < myObjects.count(); i++ )
516     GEOMBase::PublishSubObject( myObjects[i].get() );
517 }
518
519 //=================================================================================
520 // function : getSourceObjects
521 // purpose  : virtual method to get source objects
522 //=================================================================================
523 QList<GEOM::GeomObjPtr> BooleanGUI_Dialog::getSourceObjects()
524 {
525   QList<GEOM::GeomObjPtr> res(myObjects);
526   res << myObject1;
527   return res;
528 }