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