Salome HOME
6b54ccb3ddd68036f4d3aa31400134f866d9ab5e
[modules/geom.git] / src / RepairGUI / RepairGUI_SuppressFacesDlg.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 //  File   : RepairGUI_SuppressFacesDlg.cxx
23 //  Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24
25 #include "RepairGUI_SuppressFacesDlg.h"
26
27 #include "DlgRef.h"
28 #include "GeometryGUI.h"
29 #include "GEOMBase.h"
30 #include "RepairGUI.h"
31
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <SalomeApp_Study.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <SALOME_ListIO.hxx>
38
39 // OCCT Includes
40 #include <TopAbs.hxx>
41 #include <TopExp.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44 #include <TColStd_MapOfInteger.hxx>
45 #include <TColStd_IndexedMapOfInteger.hxx>
46
47 #include <GEOMImpl_Types.hxx>
48
49 //=================================================================================
50 // class    : RepairGUI_SuppressFacesDlg()
51 // purpose  : Constructs a RepairGUI_SuppressFacesDlg  which is a child of 'parent', with the
52 //            name 'name' and widget flags set to 'f'.
53 //            The dialog will by default be modeless, unless you set 'modal' to
54 //            TRUE to construct a modal dialog.
55 //=================================================================================
56 RepairGUI_SuppressFacesDlg::RepairGUI_SuppressFacesDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
57                                                         bool modal)
58   : GEOMBase_Skeleton(theGeometryGUI, parent, modal)
59 {
60   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SUPRESS_FACE")));
61   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
62
63   setWindowTitle(tr("GEOM_SUPRESSFACE_TITLE"));
64
65   /***************************************************************/
66   mainFrame()->GroupConstructors->setTitle(tr("GEOM_SUPRESSFACE"));
67   mainFrame()->RadioButton1->setIcon(image0);
68   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
69   mainFrame()->RadioButton2->close();
70   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
71   mainFrame()->RadioButton3->close();
72
73   GroupArgs = new DlgRef_2Sel(centralWidget());
74   GroupArgs->GroupBox1->setTitle(tr("Faces to remove"));
75   GroupArgs->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
76   GroupArgs->PushButton1->setIcon(image1);
77   GroupArgs->LineEdit1->setReadOnly(true);
78
79   GroupArgs->TextLabel2->setText(tr("Faces to remove"));
80   GroupArgs->PushButton2->setIcon(image1);
81   GroupArgs->LineEdit2->setReadOnly(true);
82
83   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
84   layout->setMargin(0); layout->setSpacing(6);
85   layout->addWidget(GroupArgs);
86   /***************************************************************/
87
88   setHelpFileName("suppress_faces_operation_page.html");
89
90   Init();
91 }
92
93 //=================================================================================
94 // function : ~RepairGUI_SuppressFacesDlg()
95 // purpose  : Destroys the object and frees any allocated resources
96 //=================================================================================
97 RepairGUI_SuppressFacesDlg::~RepairGUI_SuppressFacesDlg()
98 {
99 }
100
101 //=================================================================================
102 // function : Init()
103 // purpose  :
104 //=================================================================================
105 void RepairGUI_SuppressFacesDlg::Init()
106 {
107   // init variables
108   GroupArgs->LineEdit1->clear();
109   GroupArgs->LineEdit2->clear();
110   myObject = GEOM::GEOM_Object::_nil();
111   myFacesInd = new GEOM::short_array();
112   myFacesInd->length(0);
113
114   mainFrame()->GroupBoxPublish->show();
115   //Hide preview checkbox
116   mainFrame()->CheckBoxPreview->hide();
117
118   // signals and slots connections
119   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
120   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
121
122   connect(GroupArgs->PushButton1, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
123   connect(GroupArgs->PushButton2, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
124
125   connect(GroupArgs->LineEdit1,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
126   connect(GroupArgs->LineEdit2,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
127
128   initName(tr("SUPRESS_FACE_NEW_OBJ_NAME"));
129
130   GroupArgs->PushButton1->click();
131   SelectionIntoArgument();
132   resize(100,100);
133 }
134
135 //=================================================================================
136 // function : ClickOnOk()
137 // purpose  : Same than click on apply but close this dialog.
138 //=================================================================================
139 void RepairGUI_SuppressFacesDlg::ClickOnOk()
140 {
141   setIsApplyAndClose( true );
142   if (ClickOnApply())
143     ClickOnCancel();
144 }
145
146 //=================================================================================
147 // function : ClickOnApply()
148 // purpose  :
149 //=================================================================================
150 bool RepairGUI_SuppressFacesDlg::ClickOnApply()
151 {
152   if (!onAccept())
153     return false;
154
155   initName();
156   // activate first line edit
157   GroupArgs->PushButton1->click();
158   return true;
159 }
160
161 //=================================================================================
162 // function : SelectionIntoArgument()
163 // purpose  : Called when selection is changed or on dialog initialization or activation
164 //=================================================================================
165 void RepairGUI_SuppressFacesDlg::SelectionIntoArgument()
166 {
167   myEditCurrentArgument->setText("");
168   // the second argument depends on the first one
169   GroupArgs->LineEdit2->setText("");
170   myFacesInd->length(0);
171
172   if (myEditCurrentArgument == GroupArgs->LineEdit1)
173     myObject = GEOM::GEOM_Object::_nil();
174
175   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
176   SALOME_ListIO aSelList;
177   aSelMgr->selectedObjects(aSelList);
178
179   if (myEditCurrentArgument == GroupArgs->LineEdit1) {
180     if (aSelList.Extent() == 1) {
181       Handle(SALOME_InteractiveObject) anIO = aSelList.First();
182
183       myObject = GEOMBase::ConvertIOinGEOMObject( anIO );
184       if ( GEOMBase::IsShape(myObject) ) {
185         myEditCurrentArgument->setText(GEOMBase::GetName(myObject));
186         TopoDS_Shape aShape;
187         if (GEOMBase::GetShape(myObject, aShape, TopAbs_FACE))
188           GroupArgs->LineEdit2->setText(myEditCurrentArgument->text());
189
190         // clear selection
191         disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
192         myGeomGUI->getApp()->selectionMgr()->clearSelected();
193         connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
194                 this, SLOT(SelectionIntoArgument()));
195
196         GroupArgs->PushButton2->click();
197       }
198       else
199         myObject = GEOM::GEOM_Object::_nil();
200     }
201   }
202   else if (myEditCurrentArgument == GroupArgs->LineEdit2) {
203     TColStd_IndexedMapOfInteger aMap;
204
205     if (aSelList.Extent() == 1) {
206       Handle(SALOME_InteractiveObject) anIO = aSelList.First();
207       aSelMgr->GetIndexes(anIO, aMap);
208     }
209
210     if (aMap.IsEmpty() && aSelList.Extent() > 0) { // try to detect selected published sub-shape
211       TColStd_MapOfInteger anIds;
212
213       SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(myGeomGUI->getApp()->activeStudy());
214       if (!appStudy) return;
215       _PTR(Study) aStudy = appStudy->studyDS();
216
217       TopTools_IndexedMapOfShape aMainMap;
218       TopoDS_Shape aMainShape = GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), myObject);
219       TopExp::MapShapes(aMainShape, aMainMap);
220
221       SALOME_ListIO aSelListToShow;
222       SALOME_ListIteratorOfListIO anIter (aSelList);
223       for (int i = 0; anIter.More(); anIter.Next(), i++) {
224         Handle(SALOME_InteractiveObject) anIO = anIter.Value();
225         QString anEntry = anIO->getEntry();
226
227         _PTR(SObject) aSObj (aStudy->FindObjectID(anEntry.toUtf8().constData()));
228         if ( ! GeometryGUI::IsInGeomComponent( aSObj )) continue;
229         GEOM::GEOM_Object_var aGeomObj =
230           GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
231         TopoDS_Shape aShape;
232         if (GEOMBase::GetShape(aGeomObj, aShape)) {
233           if (aGeomObj->GetType() == GEOM_GROUP || aShape.ShapeType() == TopAbs_FACE) {
234             TopExp_Explorer anExp (aShape, TopAbs_FACE);
235             bool isInside = false;
236             for (; anExp.More(); anExp.Next()) {
237               TopoDS_Shape aSubShape = anExp.Current();
238               int anIndex = aMainMap.FindIndex(aSubShape);
239               if (anIndex > 0) {
240                 aMap.Add(anIndex);
241                 anIds.Add(anIndex);
242                 isInside = true;
243                 aSelListToShow.Append(anIO);
244               }
245             }
246             if (!isInside)
247             {
248               disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
249               aSelMgr->AddOrRemoveIndex(anIO, anIds, false);
250               connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
251                       this, SLOT(SelectionIntoArgument()));
252             }
253           }
254         }
255       }
256
257       if (!aMap.IsEmpty()) {
258         // highlight local faces, correspondent to OB selection
259         disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
260
261         aSelMgr->clearSelected();
262
263         QString objIOR = GEOMBase::GetIORFromObject(myObject);
264         Handle(GEOM_AISShape) aSh = GEOMBase::ConvertIORinGEOMAISShape( objIOR, true );
265         if ( aSh.IsNull() )
266           return;
267
268         aSelMgr->AddOrRemoveIndex(aSh->getIO(), anIds, false);
269
270         SALOME_ListIteratorOfListIO anIter(aSelListToShow);
271         for (; anIter.More(); anIter.Next()) {
272           aSelMgr->AddOrRemoveIndex(anIter.Value(), anIds, false);
273         }
274
275         connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
276                 this, SLOT(SelectionIntoArgument()));
277       }
278     }
279
280     const int n = aMap.Extent();
281     myFacesInd->length(n);
282     for (int i = 1; i <= n; i++)
283       myFacesInd[i-1] = aMap(i);
284     if (n)
285       myEditCurrentArgument->setText(QString::number(n) + "_" + tr("GEOM_FACE") + tr("_S_"));
286   }
287 }
288
289 //=================================================================================
290 // function : SetEditCurrentArgument()
291 // purpose  :
292 //=================================================================================
293 void RepairGUI_SuppressFacesDlg::SetEditCurrentArgument()
294 {
295   QPushButton* send = (QPushButton*)sender();
296
297   bool isEffective = false;
298
299   if (send == GroupArgs->PushButton1) {
300     isEffective = true;
301     myEditCurrentArgument = GroupArgs->LineEdit1;
302
303     GroupArgs->PushButton2->setDown(false);
304     GroupArgs->LineEdit2->setEnabled(false);
305   }
306   else if (send == GroupArgs->PushButton2 && !myObject->_is_nil()) {
307     isEffective = true;
308     myEditCurrentArgument = GroupArgs->LineEdit2;
309
310     GroupArgs->PushButton1->setDown(false);
311     GroupArgs->LineEdit1->setEnabled(false);
312   }
313
314   if (isEffective) {
315     initSelection();
316
317     // enable line edit
318     myEditCurrentArgument->setEnabled(true);
319     myEditCurrentArgument->setFocus();
320     // after setFocus(), because it will be setDown(false) when loses focus
321     send->setDown(true);
322   }
323 }
324
325 //=================================================================================
326 // function : LineEditReturnPressed()
327 // purpose  :
328 //=================================================================================
329 void RepairGUI_SuppressFacesDlg::LineEditReturnPressed()
330 {
331   const QObject* send = sender();
332   if (send == GroupArgs->LineEdit1 || send == GroupArgs->LineEdit2) {
333     myEditCurrentArgument = (QLineEdit*)send;
334     GEOMBase_Skeleton::LineEditReturnPressed();
335   }
336 }
337
338 //=================================================================================
339 // function : ActivateThisDialog()
340 // purpose  :
341 //=================================================================================
342 void RepairGUI_SuppressFacesDlg::ActivateThisDialog()
343 {
344   GEOMBase_Skeleton::ActivateThisDialog();
345   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
346            this, SLOT( SelectionIntoArgument() ) );
347
348   myEditCurrentArgument = GroupArgs->LineEdit1;
349   myEditCurrentArgument->setText( "" );
350   GroupArgs->LineEdit2->setText( "" );
351   myObject = GEOM::GEOM_Object::_nil();
352   myFacesInd->length( 0 );
353
354   initSelection();
355 }
356
357 //=================================================================================
358 // function : enterEvent()
359 // purpose  : Mouse enter onto the dialog to activate it
360 //=================================================================================
361 void RepairGUI_SuppressFacesDlg::enterEvent (QEvent*)
362 {
363   if (!mainFrame()->GroupConstructors->isEnabled())
364     ActivateThisDialog();
365 }
366
367 //=================================================================================
368 // function : createOperation
369 // purpose  :
370 //=================================================================================
371 GEOM::GEOM_IOperations_ptr RepairGUI_SuppressFacesDlg::createOperation()
372 {
373   return getGeomEngine()->GetIHealingOperations();
374 }
375
376 //=================================================================================
377 // function : isValid
378 // purpose  :
379 //=================================================================================
380 bool RepairGUI_SuppressFacesDlg::isValid (QString&)
381 {
382   TopoDS_Shape aTmpShape;
383   return !myObject->_is_nil() && (myFacesInd->length() ||
384                                   GEOMBase::GetShape(myObject, aTmpShape, TopAbs_WIRE));
385 }
386
387 //=================================================================================
388 // function : execute
389 // purpose  :
390 //=================================================================================
391 bool RepairGUI_SuppressFacesDlg::execute (ObjectList& objects)
392 {
393   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow(getOperation());
394   GEOM::GEOM_Object_var anObj = anOper->SuppressFaces(myObject, myFacesInd);
395
396   bool aResult = !anObj->_is_nil();
397   if (aResult)
398   {
399     if ( !IsPreview() )
400       RepairGUI::ShowStatistics( anOper, this );
401     objects.push_back(anObj._retn());
402   }
403   return aResult;
404 }
405
406 //=================================================================================
407 // function : initSelection
408 // purpose  :
409 //=================================================================================
410 void RepairGUI_SuppressFacesDlg::initSelection()
411 {
412   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
413
414   if (myEditCurrentArgument == GroupArgs->LineEdit1) {
415     TColStd_MapOfInteger aTypes;
416     aTypes.Add(GEOM_COMPOUND);
417     aTypes.Add(GEOM_SOLID);
418     aTypes.Add(GEOM_SHELL);
419     aTypes.Add(GEOM_FACE);
420
421     globalSelection(aTypes);
422   }
423   else if (myEditCurrentArgument == GroupArgs->LineEdit2) {
424     localSelection(myObject, TopAbs_FACE);
425   }
426
427   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
428           this, SLOT(SelectionIntoArgument()));
429 }
430
431 //=================================================================================
432 // function : restoreSubShapes
433 // purpose  :
434 //=================================================================================
435 void RepairGUI_SuppressFacesDlg::restoreSubShapes (SALOMEDS::SObject_ptr theSObject)
436 {
437   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
438     // empty list of arguments means that all arguments should be restored
439     getGeomEngine()->RestoreSubShapesSO(theSObject, GEOM::ListOfGO(),
440                                         /*theFindMethod=*/GEOM::FSM_GetInPlace,
441                                         /*theInheritFirstArg=*/true,
442                                         mainFrame()->CheckBoxAddPrefix->isChecked());
443   }
444 }
445
446 //=================================================================================
447 // function : getSourceObjects
448 // purpose  : virtual method to get source objects
449 //=================================================================================
450 QList<GEOM::GeomObjPtr> RepairGUI_SuppressFacesDlg::getSourceObjects()
451 {
452   QList<GEOM::GeomObjPtr> res;
453   GEOM::GeomObjPtr aGeomObjPtr(myObject);
454   res << aGeomObjPtr;
455   return res;
456 }