Salome HOME
Build with native prerequsites
[modules/geom.git] / src / RepairGUI / RepairGUI_DivideEdgeDlg.cxx
1 // Copyright (C) 2007-2020  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_DivideEdgeDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "RepairGUI_DivideEdgeDlg.h"
28
29 #include "DlgRef.h"
30 #include "GeometryGUI.h"
31 #include "GEOMBase.h"
32 #include "RepairGUI.h"
33
34 #include <SalomeApp_DoubleSpinBox.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <SUIT_Session.h>
38 #include <SUIT_ResourceMgr.h>
39
40 #include <GEOMImpl_Types.hxx>
41
42 #include <BRepBuilderAPI_MakeVertex.hxx>
43 #include <BRep_Builder.hxx>
44 #include <BRep_Tool.hxx>
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <Geom_Curve.hxx>
47 #include <TColStd_IndexedMapOfInteger.hxx>
48 #include <TopAbs.hxx>
49 #include <TopExp.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_MapOfShape.hxx>
53 #include <TopoDS.hxx>
54 #include <TopoDS_Compound.hxx>
55 #include <TopoDS_Edge.hxx>
56 #include <gp_Pnt.hxx>
57
58 #include <QButtonGroup>
59
60 enum { BY_PARAM, BY_POINT_PROJ };
61
62 //=================================================================================
63 // class    : RepairGUI_DivideEdgeDlg()
64 // purpose  : Constructs a RepairGUI_DivideEdgeDlg  which is a child of 'parent', with the
65 //            name 'name' and widget flags set to 'f'.
66 //            The dialog will by default be modeless, unless you set 'modal' to
67 //            TRUE to construct a modal dialog.
68 //=================================================================================
69 RepairGUI_DivideEdgeDlg::RepairGUI_DivideEdgeDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
70                                                   bool modal )
71   : GEOMBase_Skeleton( theGeometryGUI, parent, modal )
72 {
73   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_DIVIDE_EDGE" ) ) );
74   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_DIVIDE_EDGE_BY_PNT" ) ) );
75   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
76
77   setWindowTitle( tr( "GEOM_DIVIDE_EDGE_TITLE" ) );
78
79   /***************************************************************/
80   mainFrame()->GroupConstructors->setTitle(tr("GEOM_DIVIDE_EDGE_TITLE"));
81   mainFrame()->RadioButton1->setIcon( image0 );
82   mainFrame()->RadioButton2->setIcon( image2 );
83   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
84   mainFrame()->RadioButton3->close();
85
86   GroupPoints = new DlgRef_2SelExt( centralWidget() );
87   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ADD_POINT" ) );
88   GroupPoints->TextLabel1->setText( tr( "GEOM_EDGE" ) );
89   GroupPoints->PushButton1->setIcon( image1 );
90   GroupPoints->LineEdit1->setReadOnly( true );
91   GroupPoints->TextLabel2->setText( tr( "DEVIDE_EDGE_BY_PROJ_POINT" ) );
92   GroupPoints->PushButton2->setIcon( image1 );
93   GroupPoints->LineEdit2->setReadOnly( true );
94
95   QRadioButton* rb1 = new QRadioButton( tr( "GEOM_BY_PARAMETER" ), GroupPoints->Box );
96   QRadioButton* rb2 = new QRadioButton( tr( "GEOM_BY_LENGTH" ),    GroupPoints->Box );
97
98   myIsParameterGr = new QButtonGroup( GroupPoints->Box );
99   myIsParameterGr->addButton( rb1, 0 );
100   myIsParameterGr->addButton( rb2, 1 );
101   rb1->setChecked( true );
102
103   myValEdt = new SalomeApp_DoubleSpinBox( GroupPoints->Box );
104   initSpinBox( myValEdt, 0., 1., 0.1, "parametric_precision" );
105   myValEdt->setValue( 0.5 );
106   myValLbl = new QLabel( tr( "GEOM_VALUE" ), GroupPoints->Box );
107
108   QGridLayout* l = new QGridLayout( GroupPoints->Box );
109   l->setMargin( 0 ); l->setSpacing( 6 );
110   l->addWidget( rb1, 0, 0, 1, 2 );
111   l->addWidget( rb2, 1, 0, 1, 2 );
112   l->addWidget( myValLbl, 2, 0 );
113   l->addWidget( myValEdt, 2, 1 );
114
115   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
116   layout->setMargin( 0 ); layout->setSpacing( 6 );
117   layout->addWidget( GroupPoints );
118
119   /***************************************************************/
120
121   setHelpFileName( "add_point_on_edge_operation_page.html" );
122
123   Init();
124 }
125
126 //=======================================================================
127 //function : ConstructorsClicked
128 //purpose  : hide/show widgets depending on a selected constructor
129 //=======================================================================
130 void RepairGUI_DivideEdgeDlg::ConstructorsClicked( int constructorId )
131 {
132   myIsParameterGr->button( 0 )->setVisible( constructorId == BY_PARAM );
133   myIsParameterGr->button( 1 )->setVisible( constructorId == BY_PARAM );
134   myValEdt                    ->setVisible( constructorId == BY_PARAM );
135   myValLbl                    ->setVisible( constructorId == BY_PARAM );
136   GroupPoints->TextLabel2     ->setVisible( constructorId == BY_POINT_PROJ );
137   GroupPoints->PushButton2    ->setVisible( constructorId == BY_POINT_PROJ );
138   GroupPoints->LineEdit2      ->setVisible( constructorId == BY_POINT_PROJ );
139
140   initSelection();
141
142   if ( constructorId == BY_PARAM &&
143        !GroupPoints->PushButton1->isDown() )
144     GroupPoints->PushButton1->click();
145
146   displayPreview();
147 }
148
149 //=================================================================================
150 // function : ~RepairGUI_DivideEdgeDlg()
151 // purpose  : Destroys the object and frees any allocated resources
152 //=================================================================================
153 RepairGUI_DivideEdgeDlg::~RepairGUI_DivideEdgeDlg()
154 {
155 }
156
157 //=================================================================================
158 // function : Init()
159 // purpose  :
160 //=================================================================================
161 void RepairGUI_DivideEdgeDlg::Init()
162 {
163   /* init variables */
164   myEditCurrentArgument = GroupPoints->LineEdit1;
165
166   myObject = GEOM::GEOM_Object::_nil();
167   myPoints.clear();
168   myIndex  = -1;
169   myProjectionOK = false;
170
171   //myGeomGUI->SetState( 0 );
172
173   /* signals and slots connections */
174   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
175   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
176
177   connect(this,           SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
178
179   connect( myValEdt,   SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
180
181   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
182   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
183   connect( GroupPoints->PushButton2, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
184   connect( GroupPoints->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
185
186   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
187            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
188
189   initName( tr( "DEVIDE_EDGE_NEW_OBJECT_NAME" ) );
190   resize(100,100);
191   ConstructorsClicked( BY_PARAM );
192   SelectionIntoArgument();
193 }
194
195 //=================================================================================
196 // function : ValueChangedInSpinBox()
197 // purpose  : On change value in spin box
198 //=================================================================================
199 void RepairGUI_DivideEdgeDlg::ValueChangedInSpinBox()
200 {
201   displayPreview();
202 }
203
204 //=================================================================================
205 // function : displayPreview()
206 // purpose  : On display Preview
207 //=================================================================================
208 void RepairGUI_DivideEdgeDlg::displayPreview()
209 {
210   myProjectionOK = false;
211   erasePreview();
212   if ( myObject->_is_nil() )
213     return;
214
215   TopoDS_Shape aShape;
216   gp_Pnt aPnt;
217   GEOMBase::GetShape( myObject, aShape, TopAbs_SHAPE );
218
219   if ( myIndex != -1 ) {
220     TopTools_IndexedMapOfShape aShapes;
221     TopExp::MapShapes(aShape, aShapes);
222     aShape = aShapes.FindKey(myIndex);
223   }
224
225   if (aShape.ShapeType() == TopAbs_EDGE) {
226     Standard_Real aFP, aLP, aP;
227     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFP, aLP);
228     if ( aCurve.IsNull() ) return;
229
230     if ( getConstructorId() == BY_PARAM )
231     {
232       aP = aFP + (aLP - aFP) * myValEdt->value();
233       aPnt = aCurve->Value(aP);
234       BRepBuilderAPI_MakeVertex mkVertex (aPnt);
235       aShape = mkVertex.Shape();
236     }
237     else if ( getConstructorId() == BY_POINT_PROJ && !myPoints.empty() )
238     {
239       BRep_Builder builder;
240       TopoDS_Compound compoundOfVV;
241       builder.MakeCompound(compoundOfVV);
242       aShape = compoundOfVV;
243
244       GeomAPI_ProjectPointOnCurve aProjector;
245       aProjector.Init( aCurve, aFP, aLP );
246
247       TopTools_MapOfShape vMap;
248       for ( int i = 0; i < myPoints.count(); ++i )
249       {
250         TopoDS_Shape aPoint;
251         GEOMBase::GetShape( myPoints[i].get(), aPoint, TopAbs_VERTEX );
252         if ( !vMap.Add( aPoint )) continue;
253
254         gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( aPoint ));
255         aProjector.Perform( p );
256         if ( aProjector.NbPoints() > 0 )
257         {
258           double u = aProjector.LowerDistanceParameter();
259           if ( Min( u - aFP, aLP - u ) > Precision::Confusion() )
260           {
261             builder.Add( compoundOfVV, BRepBuilderAPI_MakeVertex( aProjector.NearestPoint()));
262             myProjectionOK = true;
263           }
264         }
265       }
266     }
267     else
268     {
269       return;
270     }
271     // Build prs
272     SALOME_Prs* aPrs = getDisplayer()->BuildPrs( aShape );
273     if ( aPrs != 0 && !aPrs->IsNull() )
274       GEOMBase_Helper::displayPreview( aPrs, false, true );
275   }
276 }
277
278 //=================================================================================
279 // function : ClickOnOk()
280 // purpose  : Same than click on apply but close this dialog.
281 //=================================================================================
282 void RepairGUI_DivideEdgeDlg::ClickOnOk()
283 {
284   setIsApplyAndClose( true );
285   if ( ClickOnApply() )
286     ClickOnCancel();
287 }
288
289
290 //=================================================================================
291 // function : ClickOnApply()
292 // purpose  :
293 //=================================================================================
294 bool RepairGUI_DivideEdgeDlg::ClickOnApply()
295 {
296   if ( !onAccept() )
297     return false;
298
299   initName();
300
301   GroupPoints->LineEdit1->setText( "" );
302   GroupPoints->LineEdit2->setText( "" );
303   myObject = GEOM::GEOM_Object::_nil();
304   myPoints.clear();
305   myIndex = -1;
306   myProjectionOK = false;
307
308   GroupPoints->PushButton1->click();
309
310   return true;
311 }
312
313
314 //=================================================================================
315 // function : SelectionIntoArgument()
316 // purpose  : Called when selection as changed or other case
317 //          : used only by SelectButtonC1A1 (LineEditC1A1)
318 //=================================================================================
319 void RepairGUI_DivideEdgeDlg::SelectionIntoArgument()
320 {
321   myEditCurrentArgument->setText( "" );
322
323   const bool toSelectObject = ( myEditCurrentArgument == GroupPoints->LineEdit1 );
324   if ( toSelectObject )
325   {
326     myObject = GEOM::GEOM_Object::_nil();
327     myIndex = -1;
328     myProjectionOK = false;
329   }
330   else //if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
331   {
332     myPoints.clear();
333     myProjectionOK = false;
334   }
335
336   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
337   SALOME_ListIO aSelList;
338   aSelMgr->selectedObjects(aSelList);
339
340   if ( toSelectObject && aSelList.Extent() == 1 ) {
341     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
342     GEOM::GEOM_Object_var aSelectedObj = GEOMBase::ConvertIOinGEOMObject( anIO );
343     if ( !CORBA::is_nil( aSelectedObj ) )
344     {
345       TopoDS_Shape aShape;
346       QString aName = GEOMBase::GetName( aSelectedObj );
347       if ( GEOMBase::GetShape( aSelectedObj, aShape, TopAbs_SHAPE ) )
348       {
349         const int aType = aShape.ShapeType();
350         if ( aType <= TopAbs_EDGE ) // edge, wire, face, shell, solid, compound
351         {
352           GEOM::short_array anIndexes;
353
354           TColStd_IndexedMapOfInteger aMap;
355           SalomeApp_Application* anApp =
356             (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
357           anApp->selectionMgr()->GetIndexes( anIO, aMap );
358
359           if ( !aMap.IsEmpty() ) // sub-shape selection
360           {
361             myIndex = aMap( 1 );
362             myObject = aSelectedObj;
363             myEditCurrentArgument->setText( aName += QString( ":edge_%1" ).arg( myIndex ) );
364           }
365           else if ( aType == TopAbs_EDGE ) // single shape selection
366           {
367             myIndex = -1;
368             myObject = aSelectedObj;
369             myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
370           }
371           else // face, shell, solid or compound was selected, and NOT its sub-shape.
372           {
373             myIndex = -1;
374             myObject = GEOM::GEOM_Object::_nil();
375           }
376         }
377       }
378     }
379   }
380   if ( !toSelectObject )
381   {
382     myPoints = getSelected( TopAbs_VERTEX, -1, /*strict=*/true );
383     if ( myPoints.empty() )
384       myEditCurrentArgument->setText( "" );
385     else if ( myPoints.count() == 1 )
386       myEditCurrentArgument->setText( GEOMBase::GetName( myPoints[0].get() ));
387     else
388       myEditCurrentArgument->setText( QObject::tr( "%1_vertices" ).arg( myPoints.count() ));
389   }
390
391   displayPreview();
392 }
393
394 //=================================================================================
395 // function : SetEditCurrentArgument()
396 // purpose  :
397 //=================================================================================
398 void RepairGUI_DivideEdgeDlg::SetEditCurrentArgument()
399 {
400   QPushButton* send = (QPushButton*)sender();
401
402   if ( send == GroupPoints->PushButton1 ) {
403     GroupPoints->LineEdit1->setFocus();
404     myEditCurrentArgument = GroupPoints->LineEdit1;
405
406     GroupPoints->PushButton2->setDown(false);
407     GroupPoints->LineEdit2->setEnabled(false);
408   }
409   if ( send == GroupPoints->PushButton2 ) {
410     GroupPoints->LineEdit2->setFocus();
411     myEditCurrentArgument = GroupPoints->LineEdit2;
412
413     GroupPoints->PushButton1->setDown(false);
414     GroupPoints->LineEdit1->setEnabled(false);
415   }
416   // enable line edit
417   myEditCurrentArgument->setEnabled(true);
418   myEditCurrentArgument->setFocus();
419   // after setFocus(), because it will be setDown(false) when loses focus
420   send->setDown(true);
421
422   initSelection();
423   SelectionIntoArgument();
424 }
425
426
427 //=================================================================================
428 // function : LineEditReturnPressed()
429 // purpose  :
430 //=================================================================================
431 void RepairGUI_DivideEdgeDlg::LineEditReturnPressed()
432 {
433   if ( sender() == GroupPoints->LineEdit1 ) {
434     myEditCurrentArgument = GroupPoints->LineEdit1;
435     GEOMBase_Skeleton::LineEditReturnPressed();
436   }
437   if ( sender() == GroupPoints->LineEdit2 &&
438        !GroupPoints->LineEdit2->isHidden() ) {
439     myEditCurrentArgument = GroupPoints->LineEdit2;
440     GEOMBase_Skeleton::LineEditReturnPressed();
441   }
442 }
443
444
445 //=================================================================================
446 // function : ActivateThisDialog()
447 // purpose  :
448 //=================================================================================
449 void RepairGUI_DivideEdgeDlg::ActivateThisDialog()
450 {
451   GEOMBase_Skeleton::ActivateThisDialog();
452   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
453            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
454
455 //  myObject = GEOM::GEOM_Object::_nil();
456   myIndex = -1;
457
458   //myGeomGUI->SetState( 0 );
459   ConstructorsClicked(getConstructorId());
460 }
461
462 //=================================================================================
463 // function : enterEvent()
464 // purpose  : Mouse enter onto the dialog to activate it
465 //=================================================================================
466 void RepairGUI_DivideEdgeDlg::enterEvent( QEvent* )
467 {
468   if ( !mainFrame()->GroupConstructors->isEnabled() )
469     ActivateThisDialog();
470 }
471
472
473 //=================================================================================
474 // function : createOperation
475 // purpose  :
476 //=================================================================================
477 GEOM::GEOM_IOperations_ptr RepairGUI_DivideEdgeDlg::createOperation()
478 {
479   return getGeomEngine()->GetIHealingOperations();
480 }
481
482 //=================================================================================
483 // function : isValid
484 // purpose  :
485 //=================================================================================
486 bool RepairGUI_DivideEdgeDlg::isValid( QString& msg )
487 {
488   bool ok = false;
489   if ( getConstructorId() == BY_PARAM )
490   {
491     ok = myValEdt->isValid( msg, !IsPreview() );
492   }
493   else if ( getConstructorId() == BY_POINT_PROJ )
494   {
495     if (( ok = myPoints.count() ) && !( ok = myProjectionOK ))
496       msg = tr("DEVIDE_EDGE_BAD_PROJ_MSG");
497   }
498   return !myObject->_is_nil() && ok;
499 }
500
501 //=================================================================================
502 // function : execute
503 // purpose  :
504 //=================================================================================
505 bool RepairGUI_DivideEdgeDlg::execute( ObjectList& objects )
506 {
507   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow( getOperation() );
508   GEOM::GEOM_Object_var anObj;
509   if ( getConstructorId() == BY_PARAM )
510   {
511     anObj = anOper->DivideEdge( myObject, myIndex, myValEdt->value(), getIsByParameter() );
512   }
513   else
514   {
515     GEOM::ListOfGO_var objList = new GEOM::ListOfGO;
516     objList->length( myPoints.count() );
517     for ( int i = 0; i < myPoints.count(); ++i )
518       objList[i] = myPoints[i].copy();
519     anObj = anOper->DivideEdgeByPoint( myObject, myIndex, objList );
520   }
521   bool aResult = !anObj->_is_nil();
522   if ( aResult )
523   {
524     if ( !IsPreview() && ( getConstructorId() == BY_PARAM ))
525     {
526       QStringList aParameters;
527       aParameters << "";
528       aParameters << myValEdt->text();
529       aParameters << "";
530       anObj->SetParameters(aParameters.join(":").toUtf8().constData());
531     }
532     if ( !IsPreview() )
533       RepairGUI::ShowStatistics( anOper, this );
534     objects.push_back( anObj._retn() );
535   }
536
537   return aResult;
538 }
539
540 //=================================================================================
541 // function : getIsByParameter
542 // purpose  :
543 //=================================================================================
544 bool RepairGUI_DivideEdgeDlg::getIsByParameter() const
545 {
546   return myIsParameterGr->button( 0 )->isChecked();
547 }
548
549 //=================================================================================
550 // function : initSelection
551 // purpose  :
552 //=================================================================================
553 void RepairGUI_DivideEdgeDlg::initSelection()
554 {
555   // close local contexts
556   globalSelection();
557   // load local selection on ALL objects
558   localSelection( myEditCurrentArgument == GroupPoints->LineEdit2 ? TopAbs_VERTEX : TopAbs_EDGE );
559 }
560
561 //=================================================================================
562 // function : addSubshapeToStudy
563 // purpose  : virtual method to add new SubObjects if local selection
564 //=================================================================================
565 void RepairGUI_DivideEdgeDlg::addSubshapesToStudy()
566 {
567   for ( int i = 0; i < myPoints.count(); ++i )
568     GEOMBase::PublishSubObject( myPoints[i].get() );
569 }
570
571 //=================================================================================
572 // function : getSourceObjects
573 // purpose  : virtual method to get source objects
574 //=================================================================================
575 QList<GEOM::GeomObjPtr> RepairGUI_DivideEdgeDlg::getSourceObjects()
576 {
577   QList<GEOM::GeomObjPtr> res(myPoints);
578   GEOM::GeomObjPtr aGeomObjPtr(myObject);
579   res << aGeomObjPtr;
580   return res;
581 }