Salome HOME
3523c579caf3bcd09c4001829b1dc8a7a54d0e8d
[modules/geom.git] / src / RepairGUI / RepairGUI_SewingDlg.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_SewingDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "RepairGUI_SewingDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32 #include <SalomeApp_DoubleSpinBox.h>
33
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_ResourceMgr.h>
39
40 #include <GEOMImpl_Types.hxx>
41
42 #include <TopAbs.hxx>
43 #include <TColStd_MapOfInteger.hxx>
44
45 #define DEFAULT_TOLERANCE_VALUE 1e-07
46
47 //=================================================================================
48 // class    : RepairGUI_SewingDlg()
49 // purpose  : Constructs a RepairGUI_SewingDlg  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_SewingDlg::RepairGUI_SewingDlg( 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_SEWING" ) ) );
59   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
60
61   setWindowTitle( tr( "GEOM_SEWING_TITLE" ) );
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_SEWING_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_1SelExt( centralWidget() );
72   GroupPoints->GroupBox1->setTitle( tr( "GEOM_SEWING" ) );
73   GroupPoints->TextLabel1->setText( tr( "GEOM_SELECTED_SHAPES" ) );
74   GroupPoints->PushButton1->setIcon( image1 );
75   GroupPoints->LineEdit1->setReadOnly( true );
76
77   QGridLayout* aLay = new QGridLayout( GroupPoints->Box );
78   aLay->setMargin( 0 ); aLay->setSpacing( 6 );
79   myAllowNonManifoldChk = new QCheckBox (tr("GEOM_ALLOW_NON_MANIFOLD"), GroupPoints->Box);
80   myTolEdt = new SalomeApp_DoubleSpinBox( GroupPoints->Box );
81   initSpinBox( myTolEdt, 0.0, 100.0, DEFAULT_TOLERANCE_VALUE, "len_tol_precision" );
82   myTolEdt->setValue( DEFAULT_TOLERANCE_VALUE );
83   QLabel* aLbl1 = new QLabel( tr( "GEOM_TOLERANCE" ), GroupPoints->Box );
84   myFreeBoundBtn = new QPushButton( tr( "GEOM_DETECT" ) + QString( " [%1]" ).arg( tr( "GEOM_FREE_BOUNDARIES" ) ), 
85                                     GroupPoints->Box );
86   aLay->addWidget( myAllowNonManifoldChk, 0, 0 );
87   aLay->addWidget( aLbl1,          1, 0 );
88   aLay->addWidget( myTolEdt,       1, 1 );
89   aLay->addWidget( myFreeBoundBtn, 2, 0, 1, 2 );
90
91   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
92   layout->setMargin( 0 ); layout->setSpacing( 6 );
93   layout->addWidget( GroupPoints );
94   /***************************************************************/
95
96   setHelpFileName( "sewing_operation_page.html" );
97
98   Init();
99 }
100
101
102 //=================================================================================
103 // function : ~RepairGUI_SewingDlg()
104 // purpose  : Destroys the object and frees any allocated resources
105 //=================================================================================
106 RepairGUI_SewingDlg::~RepairGUI_SewingDlg()
107 {
108 }
109
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void RepairGUI_SewingDlg::Init()
116 {
117   /* init variables */
118   myEditCurrentArgument = GroupPoints->LineEdit1;
119
120   myObjects.clear();
121
122   //myGeomGUI->SetState( 0 );
123   initSelection();
124
125   myClosed = -1;
126   myOpen = -1;
127         
128   /* signals and slots connections */
129   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
130   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
131
132   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
133   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
134
135   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
136            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
137
138   connect( myFreeBoundBtn, SIGNAL( clicked() ), this, SLOT( onDetect() ) );
139
140   initName( tr( "SEWING_NEW_OBJ_NAME" ) );
141   resize(100,100);
142   SelectionIntoArgument();
143 }
144
145
146 //=================================================================================
147 // function : ClickOnOk()
148 // purpose  : Same than click on apply but close this dialog.
149 //=================================================================================
150 void RepairGUI_SewingDlg::ClickOnOk()
151 {
152   setIsApplyAndClose( true );
153   if ( ClickOnApply() )
154     ClickOnCancel();
155 }
156
157 //=================================================================================
158 // function : ClickOnApply()
159 // purpose  :
160 //=================================================================================
161 bool RepairGUI_SewingDlg::ClickOnApply()
162 {
163   if ( !onAccept() )
164         return false;
165
166   initName();
167
168   GroupPoints->LineEdit1->setText( "" );
169   myObjects.clear();
170
171   initSelection();
172
173   return true;
174 }
175
176
177 //=================================================================================
178 // function : SelectionIntoArgument()
179 // purpose  : Called when selection
180 //=================================================================================
181 void RepairGUI_SewingDlg::SelectionIntoArgument()
182 {
183   erasePreview();
184   myEditCurrentArgument->setText( "" );
185   myObjects.clear();
186
187   myObjects = getSelected( TopAbs_SHAPE, -1 );
188
189   if ( !myObjects.isEmpty() ) {
190     QString aName = myObjects.count() > 1 ? QString( "%1_objects").arg( myObjects.count() ) : GEOMBase::GetName( myObjects[0].get() );
191     myEditCurrentArgument->setText( aName );
192   }
193 }
194
195 //=================================================================================
196 // function : SetEditCurrentArgument()
197 // purpose  :
198 //=================================================================================
199 void RepairGUI_SewingDlg::SetEditCurrentArgument()
200 {
201   const QObject* send = sender();
202   if ( send == GroupPoints->PushButton1 ) {
203     myEditCurrentArgument->setFocus();
204     SelectionIntoArgument();
205   }
206 }
207
208
209 //=================================================================================
210 // function : LineEditReturnPressed()
211 // purpose  :
212 //=================================================================================
213 void RepairGUI_SewingDlg::LineEditReturnPressed()
214 {
215   const QObject* send = sender();
216   if( send == GroupPoints->LineEdit1 ) {
217     myEditCurrentArgument = GroupPoints->LineEdit1;
218     GEOMBase_Skeleton::LineEditReturnPressed();
219   }
220 }
221
222
223 //=================================================================================
224 // function : ActivateThisDialog()
225 // purpose  :
226 //=================================================================================
227 void RepairGUI_SewingDlg::ActivateThisDialog()
228 {
229   GEOMBase_Skeleton::ActivateThisDialog();
230   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
231            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
232
233   GroupPoints->LineEdit1->setText( "" );
234   myObjects.clear();
235
236   myClosed = -1;
237   myOpen = -1;
238
239   //myGeomGUI->SetState( 0 );
240   initSelection();
241 }
242
243
244 //=================================================================================
245 // function : enterEvent()
246 // purpose  : Mouse enter onto the dialog to activate it
247 //=================================================================================
248 void RepairGUI_SewingDlg::enterEvent( QEvent* )
249 {
250   if ( !mainFrame()->GroupConstructors->isEnabled() )
251     ActivateThisDialog();
252 }
253
254
255 //=================================================================================
256 // function : createOperation
257 // purpose  :
258 //=================================================================================
259 GEOM::GEOM_IOperations_ptr RepairGUI_SewingDlg::createOperation()
260 {
261   return getGeomEngine()->GetIHealingOperations();
262 }
263
264 //=================================================================================
265 // function : isValid
266 // purpose  :
267 //=================================================================================
268 bool RepairGUI_SewingDlg::isValid( QString& msg )
269 {
270   myClosed = -1;
271   bool ok = myTolEdt->isValid( msg, !IsPreview() );
272   return !myObjects.isEmpty() && ( IsPreview() || myTolEdt->value() > 0. ) && ok;
273 }
274
275 //=================================================================================
276 // function : execute
277 // purpose  :
278 //=================================================================================
279 bool RepairGUI_SewingDlg::execute( ObjectList& objects )
280 {
281   bool aResult = false;
282   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow( getOperation() );
283
284   GEOM::ListOfGO_var objList = new GEOM::ListOfGO;
285   objList->length( myObjects.count() );
286   for ( int i = 0; i < myObjects.count(); ++i )
287     objList[i] = myObjects[i].copy();
288
289   if ( IsPreview() ) { // called from onDetect(): detect free boundary edges, highlight them (add to objects), display message dialog
290     GEOM::ListOfGO_var aClosed, anOpen;
291
292     aResult = anOper->GetFreeBoundary( objList, aClosed, anOpen );
293
294     if ( aResult ) {
295       myClosed = aClosed->length();
296       myOpen = anOpen->length();
297       int i;
298       for ( i = 0; i < myClosed; i++ )
299         objects.push_back( aClosed[i]._retn() );
300       for ( i = 0; i < myOpen; i++ )
301         objects.push_back( anOpen[i]._retn() );
302     }
303     else
304       myClosed = -1;
305   }
306   else {
307     GEOM::GEOM_Object_var anObj;
308
309     if (myAllowNonManifoldChk->isChecked()) {
310       anObj = anOper->SewAllowNonManifold( objList, myTolEdt->value() );
311     } else {
312       anObj = anOper->Sew( objList, myTolEdt->value() );
313     }
314
315     aResult = !anObj->_is_nil();
316     if ( aResult )
317     {
318       if ( !IsPreview() )
319       {
320         QStringList aParameters;
321         aParameters << myTolEdt->text();
322         anObj->SetParameters(aParameters.join(":").toUtf8().constData());
323       }
324       objects.push_back( anObj._retn() );
325     }
326   }
327
328   return aResult;
329 }
330
331 //=================================================================================
332 // function : initSelection
333 // purpose  :
334 //=================================================================================
335 void RepairGUI_SewingDlg::initSelection()
336 {
337   TColStd_MapOfInteger aTypes;
338   aTypes.Add( GEOM_FACE );
339   aTypes.Add( GEOM_SHELL );
340   aTypes.Add( GEOM_SOLID );
341   aTypes.Add( GEOM_COMPOUND );
342   globalSelection( aTypes );
343 }
344
345 //=================================================================================
346 // function : onDetect
347 // purpose  :
348 //=================================================================================
349 void RepairGUI_SewingDlg::onDetect()
350 {
351   displayPreview( true, false, true, true, 3 );
352
353   // field myClosed,myOpen is initialized in execute() method, called by displayPreview().
354   QString msg;
355   if ( myClosed != -1 )
356     msg = tr( "GEOM_FREE_BOUNDS_MSG" ).arg( myClosed + myOpen ).arg( myClosed ).arg( myOpen );
357   else
358     msg = tr( "GEOM_FREE_BOUNDS_ERROR" );
359   SUIT_MessageBox::information( this, tr( "GEOM_FREE_BOUNDS_TLT" ), msg );
360 }
361
362 //=================================================================================
363 // function : getSourceObjects
364 // purpose  : virtual method to get source objects
365 //=================================================================================
366 QList<GEOM::GeomObjPtr> RepairGUI_SewingDlg::getSourceObjects()
367 {
368   return myObjects;
369 }