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