Salome HOME
e3370848a182f16afa29ce98c92e25908d36d5b2
[modules/geom.git] / src / RepairGUI / RepairGUI_RemoveHolesDlg.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   : RepairGUI_RemoveHolesDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "RepairGUI_RemoveHolesDlg.h"
28
29 #include "DlgRef.h"
30 #include "GeometryGUI.h"
31 #include "GEOMBase.h"
32 #include "RepairGUI.h"
33
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_MessageBox.h>
37 #include <SalomeApp_Application.h>
38 #include <LightApp_SelectionMgr.h>
39
40 // OCCT Includes
41 #include <TopAbs.hxx>
42 #include <TColStd_MapOfInteger.hxx>
43 #include <TColStd_IndexedMapOfInteger.hxx>
44
45 #include <GEOMImpl_Types.hxx>
46
47 //=================================================================================
48 // class    : RepairGUI_RemoveHolesDlg()
49 // purpose  : Constructs a RepairGUI_RemoveHolesDlg  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 RepairGUI_RemoveHolesDlg::RepairGUI_RemoveHolesDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
55                                                     bool modal)
56   : GEOMBase_Skeleton(theGeometryGUI, parent, modal)
57 {
58   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SUPPRESS_HOLES")));
59   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
60
61   setWindowTitle(tr("GEOM_REMOVE_HOLES_TITLE"));
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle(tr("GEOM_REMOVE_HOLES_TITLE"));
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   GroupPoints = new DlgRef_1Sel1Check1Sel(centralWidget());
72   GroupPoints->GroupBox1->setTitle(tr("GEOM_HOLES"));
73   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
74   GroupPoints->PushButton1->setIcon(image1);
75   GroupPoints->LineEdit1->setReadOnly(true);
76   GroupPoints->CheckButton1->setText(tr("GEOM_REMOVE_ALL_HOLES"));
77   GroupPoints->TextLabel2->setText(tr("GEOM_WIRES_TO_REMOVE"));
78   GroupPoints->PushButton2->setIcon(image1);
79   GroupPoints->LineEdit2->setReadOnly(true);
80
81   myFreeBoundBtn = new QPushButton(tr("GEOM_DETECT") + QString(" [%1]").arg(tr("GEOM_FREE_BOUNDARIES")),
82                                     GroupPoints->Box);
83   QVBoxLayout* l = new QVBoxLayout(GroupPoints->Box);
84   l->setMargin(0); l->setSpacing(0);
85   l->addWidget(myFreeBoundBtn);
86
87   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
88   layout->setMargin(0); layout->setSpacing(6);
89   layout->addWidget(GroupPoints);
90   /***************************************************************/
91
92   setHelpFileName("suppress_holes_operation_page.html");
93
94   Init();
95 }
96
97 //=================================================================================
98 // function : ~RepairGUI_RemoveHolesDlg()
99 // purpose  : Destroys the object and frees any allocated resources
100 //=================================================================================
101 RepairGUI_RemoveHolesDlg::~RepairGUI_RemoveHolesDlg()
102 {
103 }
104
105 //=================================================================================
106 // function : Init()
107 // purpose  :
108 //=================================================================================
109 void RepairGUI_RemoveHolesDlg::Init()
110 {
111   // init variables
112   GroupPoints->LineEdit1->clear();
113   GroupPoints->LineEdit2->clear();
114   myObject = GEOM::GEOM_Object::_nil();
115   myWiresInd = new GEOM::short_array();
116   myWiresInd->length(0);
117
118   myClosed = -1;
119   myOpen = -1;
120
121   // signals and slots connections
122   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
123   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
124
125   connect(GroupPoints->PushButton1, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
126   connect(GroupPoints->PushButton2, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
127
128   connect(GroupPoints->LineEdit1,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
129   connect(GroupPoints->LineEdit2,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
130
131   connect(GroupPoints->CheckButton1, SIGNAL(clicked()), this, SLOT(onRemoveAllClicked()));
132
133   connect(myFreeBoundBtn, SIGNAL(clicked()), this, SLOT(onDetect()));
134
135   initName(tr("REMOVE_HOLES_NEW_OBJ_NAME"));
136
137   GroupPoints->PushButton1->click();
138   SelectionIntoArgument();
139   resize(100,100);
140 }
141
142 //=================================================================================
143 // function : ClickOnOk()
144 // purpose  : Same than click on apply but close this dialog.
145 //=================================================================================
146 void RepairGUI_RemoveHolesDlg::ClickOnOk()
147 {
148   setIsApplyAndClose( true );
149   if (ClickOnApply())
150     ClickOnCancel();
151 }
152
153 //=================================================================================
154 // function : ClickOnApply()
155 // purpose  :
156 //=================================================================================
157 bool RepairGUI_RemoveHolesDlg::ClickOnApply()
158 {
159   if (!onAccept())
160     return false;
161
162   initName();
163   // activate first line edit
164   GroupPoints->PushButton1->click();
165   return true;
166 }
167
168 //=================================================================================
169 // function : SelectionIntoArgument()
170 // purpose  : Called when selection is changed or on dialog initialization or activation
171 //=================================================================================
172 void RepairGUI_RemoveHolesDlg::SelectionIntoArgument()
173 {
174   erasePreview();
175   myEditCurrentArgument->setText("");
176   // the second argument depends on the first one
177   GroupPoints->LineEdit2->setText("");
178   myWiresInd->length(0);
179
180   if (myEditCurrentArgument == GroupPoints->LineEdit1)
181     myObject = GEOM::GEOM_Object::_nil();
182
183   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
184   SALOME_ListIO aSelList;
185   aSelMgr->selectedObjects(aSelList);
186
187   if (aSelList.Extent() == 1) {
188     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
189
190     if (myEditCurrentArgument == GroupPoints->LineEdit1) { // face selection
191       myObject = GEOMBase::ConvertIOinGEOMObject( anIO );
192       if ( GEOMBase::IsShape(myObject) ) {
193         myEditCurrentArgument->setText(GEOMBase::GetName(myObject));
194
195         // clear selection
196         disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
197         myGeomGUI->getApp()->selectionMgr()->clearSelected();
198         connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
199                 this, SLOT(SelectionIntoArgument()));
200
201         if (!GroupPoints->CheckButton1->isChecked())
202           GroupPoints->PushButton2->click();
203       }
204       else
205         myObject = GEOM::GEOM_Object::_nil();
206     }
207     else if (myEditCurrentArgument == GroupPoints->LineEdit2 &&
208              !GroupPoints->CheckButton1->isChecked()) {
209       TColStd_IndexedMapOfInteger aMap;
210       aSelMgr->GetIndexes(anIO, aMap);
211       const int n = aMap.Extent();
212       myWiresInd->length(n);
213       for (int i = 1; i <= n; i++)
214         myWiresInd[i-1] = aMap(i);
215       if (n)
216         myEditCurrentArgument->setText(QString::number(n) + "_" + tr("GEOM_WIRE") + tr("_S_"));
217     }
218   }
219 }
220
221 //=================================================================================
222 // function : SetEditCurrentArgument()
223 // purpose  :
224 //=================================================================================
225 void RepairGUI_RemoveHolesDlg::SetEditCurrentArgument()
226 {
227   QPushButton* send = (QPushButton*)sender();
228
229   bool isEffective = false;
230
231   if (send == GroupPoints->PushButton1) {
232     isEffective = true;
233     myEditCurrentArgument = GroupPoints->LineEdit1;
234
235     GroupPoints->PushButton2->setDown(false);
236     GroupPoints->LineEdit2->setEnabled(false);
237   }
238   else if (send == GroupPoints->PushButton2 && !myObject->_is_nil()) {
239     isEffective = true;
240     myEditCurrentArgument = GroupPoints->LineEdit2;
241
242     GroupPoints->PushButton1->setDown(false);
243     GroupPoints->LineEdit1->setEnabled(false);
244   }
245
246   if (isEffective) {
247     initSelection();
248
249     // enable line edit
250     myEditCurrentArgument->setEnabled(true);
251     myEditCurrentArgument->setFocus();
252     // after setFocus(), because it will be setDown(false) when loses focus
253     send->setDown(true);
254   }
255 }
256
257 //=================================================================================
258 // function : LineEditReturnPressed()
259 // purpose  :
260 //=================================================================================
261 void RepairGUI_RemoveHolesDlg::LineEditReturnPressed()
262 {
263   const QObject* send = sender();
264   if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2) {
265     myEditCurrentArgument = (QLineEdit*)send;
266     GEOMBase_Skeleton::LineEditReturnPressed();
267   }
268 }
269
270 //=================================================================================
271 // function : ActivateThisDialog()
272 // purpose  :
273 //=================================================================================
274 void RepairGUI_RemoveHolesDlg::ActivateThisDialog()
275 {
276   GEOMBase_Skeleton::ActivateThisDialog();
277   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
278            this, SLOT( SelectionIntoArgument() ) );
279
280   myEditCurrentArgument = GroupPoints->LineEdit1;
281   myEditCurrentArgument->setText( "" );
282   GroupPoints->LineEdit2->setText( "" );
283   myObject = GEOM::GEOM_Object::_nil();
284   myWiresInd->length( 0 );
285
286   myClosed = -1;
287   myOpen = -1;
288
289   initSelection();
290 }
291
292 //=================================================================================
293 // function : enterEvent()
294 // purpose  : Mouse enter onto the dialog to activate it
295 //=================================================================================
296 void RepairGUI_RemoveHolesDlg::enterEvent (QEvent*)
297 {
298   if (!mainFrame()->GroupConstructors->isEnabled())
299     ActivateThisDialog();
300 }
301
302 //=================================================================================
303 // function : createOperation
304 // purpose  :
305 //=================================================================================
306 GEOM::GEOM_IOperations_ptr RepairGUI_RemoveHolesDlg::createOperation()
307 {
308   return getGeomEngine()->GetIHealingOperations();
309 }
310
311 //=================================================================================
312 // function : isValid
313 // purpose  :
314 //=================================================================================
315 bool RepairGUI_RemoveHolesDlg::isValid (QString&)
316 {
317   myClosed = -1;
318   return !myObject->_is_nil() && (IsPreview() || GroupPoints->CheckButton1->isChecked() || myWiresInd->length());
319 }
320
321 //=================================================================================
322 // function : execute
323 // purpose  :
324 //=================================================================================
325 bool RepairGUI_RemoveHolesDlg::execute (ObjectList& objects)
326 {
327   bool aResult = false;
328
329   if (IsPreview()) {
330     // called from onDetect(): detect free boundary edges,
331     // highlight them (add to objects), display message dialog
332     GEOM::ListOfGO_var aClosed, anOpen;
333
334     GEOM::ListOfGO_var objList = new GEOM::ListOfGO;
335     objList->length(1);
336     objList[0] = myObject;
337     GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow(getOperation());
338     aResult = anOper->GetFreeBoundary(objList, aClosed, anOpen);
339
340     if (aResult) {
341       myClosed = aClosed->length();
342       myOpen = anOpen->length();
343       int i;
344       for (i = 0; i < myClosed; i++)
345         objects.push_back(aClosed[i]._retn());
346       for (i = 0; i < myOpen; i++)
347         objects.push_back(anOpen[i]._retn());
348     }
349     else
350       myClosed = -1;
351   }
352   else {
353     GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow(getOperation());
354     GEOM::GEOM_Object_var anObj = anOper->FillHoles(myObject, myWiresInd);
355     aResult = !anObj->_is_nil();
356     if (aResult)
357     {
358       if ( !IsPreview() )
359         RepairGUI::ShowStatistics( anOper, this );
360       objects.push_back(anObj._retn());
361     }
362   }
363
364   return aResult;
365 }
366
367 //=================================================================================
368 // function : onRemoveAllClicked
369 // purpose  :
370 //=================================================================================
371 void RepairGUI_RemoveHolesDlg::onRemoveAllClicked()
372 {
373   bool b = GroupPoints->CheckButton1->isChecked();
374   GroupPoints->TextLabel2->setEnabled(!b);
375   GroupPoints->PushButton2->setEnabled(!b);
376   GroupPoints->LineEdit2->setEnabled(!b);
377   if (b) {
378     GroupPoints->LineEdit2->setText("");
379     myWiresInd->length(0);
380   }
381 }
382
383 //=================================================================================
384 // function : initSelection
385 // purpose  :
386 //=================================================================================
387 void RepairGUI_RemoveHolesDlg::initSelection()
388 {
389   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
390
391   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
392     TColStd_MapOfInteger aTypes;
393     aTypes.Add(GEOM_COMPOUND);
394     aTypes.Add(GEOM_SOLID);
395     aTypes.Add(GEOM_SHELL);
396     aTypes.Add(GEOM_FACE);
397
398     globalSelection(aTypes);
399   }
400   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
401     localSelection(myObject, TopAbs_EDGE);
402     localSelection(myObject, TopAbs_WIRE);
403   }
404
405   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
406           this, SLOT(SelectionIntoArgument()));
407 }
408
409 //=================================================================================
410 // function : onDetect
411 // purpose  :
412 //=================================================================================
413 void RepairGUI_RemoveHolesDlg::onDetect()
414 {
415   displayPreview(true, false, true, true, 3);
416
417   // field myClosed,myOpen is initialized in execute() method, called by displayPreview().
418   QString msg;
419   if (myClosed != -1)
420     msg = tr("GEOM_FREE_BOUNDS_MSG").arg(myClosed + myOpen).arg(myClosed).arg(myOpen);
421   else
422     msg = tr("GEOM_FREE_BOUNDS_ERROR");
423   SUIT_MessageBox::information(this, tr("GEOM_FREE_BOUNDS_TLT"), msg);
424 }
425
426 //=================================================================================
427 // function : getSourceObjects
428 // purpose  : virtual method to get source objects
429 //=================================================================================
430 QList<GEOM::GeomObjPtr> RepairGUI_RemoveHolesDlg::getSourceObjects()
431 {
432   QList<GEOM::GeomObjPtr> res;
433   GEOM::GeomObjPtr aGeomObjPtr(myObject);
434   res << aGeomObjPtr;
435   return res;
436 }