Salome HOME
[bos #40619][CEA] Add Fuzzy parameter to partition and boolean operators
[modules/geom.git] / src / BooleanGUI / BooleanGUI_Dialog.cxx
index 85e1f19902c4a0b7eb08b2f5041040dd25bd91ad..4a007557df4c1fcbbaa94380a66e8bb3596ff643 100644 (file)
@@ -1,24 +1,25 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 // GEOM GEOMGUI : GUI for Geometry component
 // File   : BooleanGUI_Dialog.cxx
 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
 #include <SUIT_ResourceMgr.h>
 #include <SalomeApp_Application.h>
 #include <LightApp_SelectionMgr.h>
+#include <SALOME_ListIO.hxx>
+
+// VSR 22/08/2012: issue 0021787: remove "Preview" button from BOP and Partition operations
+// Comment next line to enable preview in BOP dialog box
+#define NO_PREVIEW
+
+#define DEFAULT_FUZZY_VALUE   1e-5
+
 
 //=================================================================================
 // class    : BooleanGUI_Dialog()
@@ -72,7 +81,7 @@ BooleanGUI_Dialog::BooleanGUI_Dialog (const int theOperation, GeometryGUI* theGe
     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SECTION")));
     aTitle = tr("GEOM_SECTION");
     aCaption = tr("GEOM_SECTION_TITLE");
-    setHelpFileName("section_opeartion_page.html");
+    setHelpFileName("section_operation_page.html");
     break;
   }
   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
@@ -87,28 +96,67 @@ BooleanGUI_Dialog::BooleanGUI_Dialog (const int theOperation, GeometryGUI* theGe
   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
   mainFrame()->RadioButton3->close();
 
-  myGroup = new DlgRef_2Sel(centralWidget());
+  myGroup = new DlgRef_2Sel2Spin3Check(centralWidget());
 
   myGroup->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
-  if (myOperation != BooleanGUI::CUT) {
+  if (myOperation == BooleanGUI::CUT) {
+    myGroup->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
+    myGroup->TextLabel2->setText(tr("GEOM_TOOL_OBJECTS"));
+  }
+  else if (myOperation == BooleanGUI::SECTION) {
     myGroup->TextLabel1->setText(tr("GEOM_OBJECT_I").arg(1));
     myGroup->TextLabel2->setText(tr("GEOM_OBJECT_I").arg(2));
-  }
-  else {
-    myGroup->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
-    myGroup->TextLabel2->setText(tr("GEOM_TOOL_OBJECT"));
+  } else { // Fuse or Common
+    myGroup->TextLabel1->setText(tr( "GEOM_SELECTED_OBJECTS" ));
+    myGroup->TextLabel2->hide();
+    myGroup->PushButton2->hide();
+    myGroup->LineEdit2->hide();
+
+    if (myOperation == BooleanGUI::FUSE) {
+      myGroup->CheckBox2->setText(tr("GEOM_BOOL_REMOVE_EXTRA_EDGES"));
+    }
   }
 
   myGroup->PushButton1->setIcon(image1);
-  myGroup->PushButton2->setIcon(image1);
   myGroup->LineEdit1->setReadOnly(true);
-  myGroup->LineEdit2->setReadOnly(true);
+
+  if (myOperation != BooleanGUI::FUSE) {
+    myGroup->CheckBox2->hide();
+
+    if (myOperation != BooleanGUI::COMMON) {
+      myGroup->PushButton2->setIcon(image1);
+      myGroup->LineEdit2->setReadOnly(true);
+    }
+  }
+
+  myGroup->TextLabel3->hide();
+  myGroup->TextLabel4->hide();
+  myGroup->SpinBox_DX->hide();
+  myGroup->SpinBox_DY->hide();
+  myGroup->CheckBox1->setText(tr("GEOM_CHECK_SELF_INTERSECTIONS"));
+
+  // Add all "fuzzy parameter" related widgets
+  myGroup->CheckBox3->setText(tr("GEOM_USE_FUZZY_PARAMETER"));
+
+  myFuzzyLbl = new QLabel(myGroup->GroupBox1);
+  myFuzzyLbl->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+  myFuzzyLbl->setText(tr("GEOM_FUZZY_PARAMETER"));
+  myGroup->gridLayout1->addWidget(myFuzzyLbl, 7, 0, 1, 2);
+
+  myFuzzyEdt = new SalomeApp_DoubleSpinBox(myGroup->GroupBox1);
+  initSpinBox( myFuzzyEdt, 1.e-7, 10.0, DEFAULT_FUZZY_VALUE, "length_tol_precision" );
+  myFuzzyEdt->setValue(DEFAULT_FUZZY_VALUE);
+  myGroup->gridLayout1->addWidget(myFuzzyEdt, 7, 2);
 
   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
   layout->setMargin(0); layout->setSpacing(6);
   layout->addWidget(myGroup);
   /***************************************************************/
 
+#ifdef NO_PREVIEW
+  mainFrame()->CheckBoxPreview->setChecked( false );
+  mainFrame()->CheckBoxPreview->hide();
+#endif
   // Initialisation
   Init();
 }
@@ -134,20 +182,36 @@ void BooleanGUI_Dialog::Init()
 
   myGroup->LineEdit1->setText("");
   myGroup->LineEdit2->setText("");
-  myObject1 = myObject2 = GEOM::GEOM_Object::_nil();
+  myGroup->CheckBox1->setChecked(true);
+
+  if (myOperation == BooleanGUI::FUSE) {
+    myGroup->CheckBox2->setChecked(true);
+  }
+
+  // Do not use Fuzzy parameter by default
+  myGroup->CheckBox3->setChecked(false);
+  myFuzzyLbl->setEnabled(false);
+  myFuzzyEdt->setEnabled(false);
+
+  myObject1.nullify();
+  reset();
  
   // signals and slots connections
   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
 
-  connect(myGroup->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
-  connect(myGroup->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
-
   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
-  connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
+
+  if (!myGroup->PushButton2->isHidden()) {
+    connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
+  }
+
+  if (!myGroup->CheckBox3->isHidden()) {
+    connect(myGroup->CheckBox3, SIGNAL(stateChanged(int)), this, SLOT(UseFuzzyChanged(int)));
+  }
 
   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
-           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
+          SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()), Qt::UniqueConnection);
 
   initName(mainFrame()->GroupConstructors->title());
 
@@ -158,9 +222,10 @@ void BooleanGUI_Dialog::Init()
   mainFrame()->RadioButton1->setFocus();
 
   globalSelection(GEOM_ALLSHAPES);
-
+  //localSelection(TopAbs_SHAPE); // VSR 24/09/2015: deactivate local selection in BOP (CoTech decision)
+  
   myGroup->PushButton1->click();
-  SelectionIntoArgument();
+  resize(100,100);
 }
 
 //=================================================================================
@@ -169,6 +234,7 @@ void BooleanGUI_Dialog::Init()
 //=================================================================================
 void BooleanGUI_Dialog::ClickOnOk()
 {
+  setIsApplyAndClose( true );
   if (ClickOnApply())
     ClickOnCancel();
 }
@@ -189,47 +255,102 @@ bool BooleanGUI_Dialog::ClickOnApply()
 }
 
 //=================================================================================
-// function : SelectionIntoArgument()
-// purpose  : Called when selection is changed or on dialog initialization or activation
+// function : reset()
+// purpose  : 
 //=================================================================================
-void BooleanGUI_Dialog::SelectionIntoArgument()
+void BooleanGUI_Dialog::reset()
 {
-  myEditCurrentArgument->setText("");
-
-  LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
-  SALOME_ListIO aSelList;
-  aSelMgr->selectedObjects(aSelList);
+  myObjects.clear();
+}
 
-  if (aSelList.Extent() != 1) {
-    if      (myEditCurrentArgument == myGroup->LineEdit1) myObject1 = GEOM::GEOM_Object::_nil();
-    else if (myEditCurrentArgument == myGroup->LineEdit2) myObject2 = GEOM::GEOM_Object::_nil();
-    return;
-  }
+//=================================================================================
+// function : singleSelection
+// purpose  : Performs single selection. Called from SelectionIntoArgument()
+//=================================================================================
+void BooleanGUI_Dialog::singleSelection()
+{
+  myEditCurrentArgument->setText("");
 
-  // nbSel == 1
-  Standard_Boolean aRes = Standard_False;
-  GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aRes);
-  if (!CORBA::is_nil(aSelectedObject) && aRes && GEOMBase::IsShape(aSelectedObject))
-  {
-    myEditCurrentArgument->setText(GEOMBase::GetName(aSelectedObject));
+  GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_SHAPE );
+  TopoDS_Shape aShape;
+  if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
+    QString aName = GEOMBase::GetName( aSelectedObject.get() );
+    myEditCurrentArgument->setText( aName );
 
     // clear selection
     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
     myGeomGUI->getApp()->selectionMgr()->clearSelected();
     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
-            this, SLOT(SelectionIntoArgument()));
+            this, SLOT(SelectionIntoArgument()), Qt::UniqueConnection);
 
     if (myEditCurrentArgument == myGroup->LineEdit1) {
       myObject1 = aSelectedObject;
-      if (myObject2->_is_nil())
+      if (!myGroup->PushButton2->isHidden() && !myObjects.count())
         myGroup->PushButton2->click();
     }
     else if (myEditCurrentArgument == myGroup->LineEdit2) {
-      myObject2 = aSelectedObject;
-      if (myObject1->_is_nil())
+      myObjects.clear();
+      myObjects << aSelectedObject;
+      if (!myObject1)
         myGroup->PushButton1->click();
     }
   }
+  else {
+    if      (myEditCurrentArgument == myGroup->LineEdit1) myObject1.nullify();
+    else if (myEditCurrentArgument == myGroup->LineEdit2) reset();
+  }
+}
+
+//=================================================================================
+// function : multipleSelection
+// purpose  : Performs multiple selection. Called from SelectionIntoArgument()
+//=================================================================================
+void BooleanGUI_Dialog::multipleSelection()
+{
+  myEditCurrentArgument->setText( "" );
+  reset();
+        
+  myObjects = getSelected( TopAbs_SHAPE, -1 );
+
+  int i = myObjects.count();
+  if ( i == 1 ) {
+    myEditCurrentArgument->setText( GEOMBase::GetName( myObjects.first().get() ) );
+  } else if ( i > 0 ) {
+    myEditCurrentArgument->setText( QString::number( i ) + "_" + tr( "GEOM_OBJECTS" ) );
+  }
+}
+
+//=================================================================================
+// function : SelectionIntoArgument()
+// purpose  : Called when selection is changed or on dialog initialization or activation
+//=================================================================================
+void BooleanGUI_Dialog::SelectionIntoArgument()
+{
+  myEditCurrentArgument->setText("");
+  if ( myOperation == BooleanGUI::SECTION ||
+      (myOperation == BooleanGUI::CUT &&
+       myEditCurrentArgument == myGroup->LineEdit1)) {
+    singleSelection();
+  } else {
+    multipleSelection();
+  }
+
+  processPreview();
+}
+
+//=================================================================================
+// function : UseFuzzyChanged()
+// purpose  : This slot is called whenever the status of CheckButton3 has changed
+//=================================================================================
+void BooleanGUI_Dialog::UseFuzzyChanged(int state)
+{
+  QCheckBox* send = (QCheckBox*)sender();
+
+  if (send == myGroup->CheckBox3) {
+    bool isChecked = (state != Qt::Unchecked);
+    myFuzzyLbl->setEnabled(isChecked);
+    myFuzzyEdt->setEnabled(isChecked);
+  }
 }
 
 //=================================================================================
@@ -243,8 +364,10 @@ void BooleanGUI_Dialog::SetEditCurrentArgument()
   if (send == myGroup->PushButton1) {
     myEditCurrentArgument = myGroup->LineEdit1;
 
-    myGroup->PushButton2->setDown(false);
-    myGroup->LineEdit2->setEnabled(false);
+    if (!myGroup->PushButton2->isHidden()) {
+      myGroup->PushButton2->setDown(false);
+      myGroup->LineEdit2->setEnabled(false);
+    }
   }
   else if (send == myGroup->PushButton2) {
     myEditCurrentArgument = myGroup->LineEdit2;
@@ -253,24 +376,16 @@ void BooleanGUI_Dialog::SetEditCurrentArgument()
     myGroup->LineEdit1->setEnabled(false);
   }
 
+  globalSelection(GEOM_ALLSHAPES);
+  //localSelection(TopAbs_SHAPE); // VSR 24/09/2015: deactivate local selection in BOP (CoTech decision)
+
   // enable line edit
   myEditCurrentArgument->setEnabled(true);
   myEditCurrentArgument->setFocus();
   // after setFocus(), because it will be setDown(false) when loses focus
   send->setDown(true);
-}
 
-//=================================================================================
-// function : LineEditReturnPressed()
-// purpose  :
-//=================================================================================
-void BooleanGUI_Dialog::LineEditReturnPressed()
-{
-  QLineEdit* send = (QLineEdit*)sender();
-  if (send == myGroup->LineEdit1 || send == myGroup->LineEdit2) {
-    myEditCurrentArgument = send;
-    GEOMBase_Skeleton::LineEditReturnPressed();
-  }
+  SelectionIntoArgument();
 }
 
 //=================================================================================
@@ -282,8 +397,8 @@ void BooleanGUI_Dialog::ActivateThisDialog()
   GEOMBase_Skeleton::ActivateThisDialog();
 
   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
-          this, SLOT( SelectionIntoArgument() ) );
-  displayPreview();
+           this, SLOT( SelectionIntoArgument() ), Qt::UniqueConnection );
+  processPreview();
 }
 
 //=================================================================================
@@ -302,7 +417,7 @@ void BooleanGUI_Dialog::enterEvent (QEvent*)
 //=================================================================================
 GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
 {
-  return getGeomEngine()->GetIBooleanOperations(getStudyId());
+  return getGeomEngine()->GetIBooleanOperations();
 }
 
 //=================================================================================
@@ -311,13 +426,24 @@ GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
 //=================================================================================
 bool BooleanGUI_Dialog::isValid (QString&)
 {
-  //Handle(SALOME_InteractiveObject) IO = firstIObject();
-  //Standard_Boolean testResult;
-  //GEOM::GEOM_Object_var anObject = GEOMBase::ConvertIOinGEOMObject(IO, testResult);
-  //if (!testResult || anObject->_is_nil())
-  //  return false;
+  bool isOK = false;
 
-  return !CORBA::is_nil(myObject1) && !CORBA::is_nil(myObject2);
+  switch (myOperation) {
+    case BooleanGUI::FUSE:
+    case BooleanGUI::COMMON:
+      isOK = myObjects.count() > 1;
+    break;
+  case BooleanGUI::CUT:
+    isOK = myObject1 && myObjects.count();
+    break;
+  case BooleanGUI::SECTION:
+    isOK = myObject1 && (myObjects.count() == 1);
+    break;
+  default:
+    break;
+  }
+
+  return isOK;
 }
 
 //=================================================================================
@@ -328,8 +454,36 @@ bool BooleanGUI_Dialog::execute (ObjectList& objects)
 {
   GEOM::GEOM_Object_var anObj;
 
-  anObj = GEOM::GEOM_IBooleanOperations::_narrow(getOperation())->
-    MakeBoolean(myObject1, myObject2, myOperation);
+  GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
+  const bool isCheckSelfInte = myGroup->CheckBox1->isChecked();
+  const bool useFuzzyParam = myGroup->CheckBox3->isChecked();
+  const double fuzzyValue = (useFuzzyParam ? myFuzzyEdt->value() : -1);
+
+  GEOM::ListOfGO_var anObjects = new GEOM::ListOfGO();
+  anObjects->length( myObjects.count() );
+  for ( int i = 0; i < myObjects.count(); i++ )
+    anObjects[i] = myObjects[i].copy();
+
+  switch (myOperation) {
+    case BooleanGUI::FUSE:
+      {
+        const bool isRmExtraEdges = myGroup->CheckBox2->isChecked();
+        anObj = anOper->MakeFuseListWithFuzzy(anObjects, isCheckSelfInte, isRmExtraEdges, fuzzyValue);
+      }
+      break;
+    case BooleanGUI::COMMON:
+      anObj = anOper->MakeCommonListWithFuzzy(anObjects, isCheckSelfInte, fuzzyValue);
+      break;
+    case BooleanGUI::CUT:
+      anObj = anOper->MakeCutListWithFuzzy(myObject1.get(), anObjects, isCheckSelfInte, fuzzyValue);
+      break;
+    case BooleanGUI::SECTION:
+      anObj = anOper->MakeBooleanWithFuzzy(myObject1.get(), anObjects[0], myOperation, isCheckSelfInte, fuzzyValue);
+      break;
+    default:
+      break;
+  }
+
   if (!anObj->_is_nil())
     objects.push_back(anObj._retn());
 
@@ -340,13 +494,35 @@ bool BooleanGUI_Dialog::execute (ObjectList& objects)
 // function : restoreSubShapes
 // purpose  :
 //=================================================================================
-void BooleanGUI_Dialog::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
-                                          SALOMEDS::SObject_ptr theSObject)
+void BooleanGUI_Dialog::restoreSubShapes (SALOMEDS::SObject_ptr theSObject)
 {
   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
     // empty list of arguments means that all arguments should be restored
-    getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
+    getGeomEngine()->RestoreSubShapesSO( theSObject, GEOM::ListOfGO(),
                                          /*theFindMethod=*/GEOM::FSM_GetInPlace, // ? GEOM::FSM_GetSame
-                                         /*theInheritFirstArg=*/myOperation == BooleanGUI::CUT); // ? false
+                                         /*theInheritFirstArg=*/myOperation == BooleanGUI::CUT,
+                                         mainFrame()->CheckBoxAddPrefix->isChecked()); // ? false
   }
 }
+
+//=================================================================================
+// function : addSubshapesToStudy
+// purpose  : virtual method to add new SubObjects if local selection
+//=================================================================================
+void BooleanGUI_Dialog::addSubshapesToStudy()
+{
+  GEOMBase::PublishSubObject( myObject1.get() );
+  for ( int i = 0; i < myObjects.count(); i++ )
+    GEOMBase::PublishSubObject( myObjects[i].get() );
+}
+
+//=================================================================================
+// function : getSourceObjects
+// purpose  : virtual method to get source objects
+//=================================================================================
+QList<GEOM::GeomObjPtr> BooleanGUI_Dialog::getSourceObjects()
+{
+  QList<GEOM::GeomObjPtr> res(myObjects);
+  res << myObject1;
+  return res;
+}