1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : RepairGUI_DivideEdgeDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
27 #include "RepairGUI_DivideEdgeDlg.h"
30 #include <GeometryGUI.h>
32 #include <SalomeApp_DoubleSpinBox.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_ResourceMgr.h>
39 #include <GEOMImpl_Types.hxx>
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>
49 #include <TopExp_Explorer.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TopTools_MapOfShape.hxx>
53 #include <TopoDS_Compound.hxx>
54 #include <TopoDS_Edge.hxx>
57 enum { BY_PARAM, BY_POINT_PROJ };
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,
68 : GEOMBase_Skeleton( theGeometryGUI, parent, modal )
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" ) ) );
74 setWindowTitle( tr( "GEOM_DIVIDE_EDGE_TITLE" ) );
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();
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 );
92 QRadioButton* rb1 = new QRadioButton( tr( "GEOM_BY_PARAMETER" ), GroupPoints->Box );
93 QRadioButton* rb2 = new QRadioButton( tr( "GEOM_BY_LENGTH" ), GroupPoints->Box );
95 myIsParameterGr = new QButtonGroup( GroupPoints->Box );
96 myIsParameterGr->addButton( rb1, 0 );
97 myIsParameterGr->addButton( rb2, 1 );
98 rb1->setChecked( true );
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 );
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 );
112 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
113 layout->setMargin( 0 ); layout->setSpacing( 6 );
114 layout->addWidget( GroupPoints );
116 /***************************************************************/
118 setHelpFileName( "add_point_on_edge_operation_page.html" );
123 //=======================================================================
124 //function : ConstructorsClicked
125 //purpose : hide/show widgets depending on a selected constructor
126 //=======================================================================
127 void RepairGUI_DivideEdgeDlg::ConstructorsClicked( int constructorId )
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 );
139 if ( constructorId == BY_PARAM &&
140 !GroupPoints->PushButton1->isDown() )
141 GroupPoints->PushButton1->click();
146 //=================================================================================
147 // function : ~RepairGUI_DivideEdgeDlg()
148 // purpose : Destroys the object and frees any allocated resources
149 //=================================================================================
150 RepairGUI_DivideEdgeDlg::~RepairGUI_DivideEdgeDlg()
154 //=================================================================================
157 //=================================================================================
158 void RepairGUI_DivideEdgeDlg::Init()
161 myEditCurrentArgument = GroupPoints->LineEdit1;
163 myObject = GEOM::GEOM_Object::_nil();
166 myProjectionOK = false;
168 //myGeomGUI->SetState( 0 );
170 /* signals and slots connections */
171 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
172 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
174 connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
176 connect( myValEdt, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
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() ) );
183 connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
184 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
186 initName( tr( "DEVIDE_EDGE_NEW_OBJECT_NAME" ) );
188 ConstructorsClicked( BY_PARAM );
189 SelectionIntoArgument();
192 //=================================================================================
193 // function : ValueChangedInSpinBox()
194 // purpose : On change value in spin box
195 //=================================================================================
196 void RepairGUI_DivideEdgeDlg::ValueChangedInSpinBox()
201 //=================================================================================
202 // function : displayPreview()
203 // purpose : On display Preview
204 //=================================================================================
205 void RepairGUI_DivideEdgeDlg::displayPreview()
207 myProjectionOK = false;
209 if ( myObject->_is_nil() )
214 GEOMBase::GetShape( myObject, aShape, TopAbs_SHAPE );
216 if ( myIndex != -1 ) {
217 TopTools_IndexedMapOfShape aShapes;
218 TopExp::MapShapes(aShape, aShapes);
219 aShape = aShapes.FindKey(myIndex);
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;
227 if ( getConstructorId() == BY_PARAM )
229 aP = aFP + (aLP - aFP) * myValEdt->value();
230 aPnt = aCurve->Value(aP);
231 BRepBuilderAPI_MakeVertex mkVertex (aPnt);
232 aShape = mkVertex.Shape();
234 else if ( getConstructorId() == BY_POINT_PROJ && myPoint )
236 TopoDS_Shape aPoints;
237 GEOMBase::GetShape( myPoint.get(), aPoints, TopAbs_SHAPE );
239 BRep_Builder builder;
240 TopoDS_Compound compoundOfVV;
241 builder.MakeCompound(compoundOfVV);
242 aShape = compoundOfVV;
244 GeomAPI_ProjectPointOnCurve aProjector;
245 aProjector.Init( aCurve, aFP, aLP );
247 TopTools_MapOfShape vMap;
248 TopExp_Explorer vertex( aPoints, TopAbs_VERTEX );
249 for ( ; vertex.More(); vertex.Next() )
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 )
256 double u = aProjector.LowerDistanceParameter();
257 if ( Min( u - aFP, aLP - u ) > Precision::Confusion() )
259 builder.Add( compoundOfVV, BRepBuilderAPI_MakeVertex( aProjector.NearestPoint()));
260 myProjectionOK = true;
270 SALOME_Prs* aPrs = getDisplayer()->BuildPrs( aShape );
271 if ( aPrs != 0 && !aPrs->IsNull() )
272 GEOMBase_Helper::displayPreview( aPrs, false, true );
276 //=================================================================================
277 // function : ClickOnOk()
278 // purpose : Same than click on apply but close this dialog.
279 //=================================================================================
280 void RepairGUI_DivideEdgeDlg::ClickOnOk()
282 setIsApplyAndClose( true );
283 if ( ClickOnApply() )
288 //=================================================================================
289 // function : ClickOnApply()
291 //=================================================================================
292 bool RepairGUI_DivideEdgeDlg::ClickOnApply()
299 myEditCurrentArgument->setText( "" );
300 myObject = GEOM::GEOM_Object::_nil();
303 myProjectionOK = false;
305 ConstructorsClicked(getConstructorId());
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()
318 myEditCurrentArgument->setText( "" );
320 const bool toSelectObject = ( myEditCurrentArgument == GroupPoints->LineEdit1 );
321 if ( toSelectObject )
323 myObject = GEOM::GEOM_Object::_nil();
326 else //if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
329 myProjectionOK = false;
332 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
333 SALOME_ListIO aSelList;
334 aSelMgr->selectedObjects(aSelList);
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 ) )
342 QString aName = GEOMBase::GetName( aSelectedObj );
343 if ( GEOMBase::GetShape( aSelectedObj, aShape, TopAbs_SHAPE ) )
345 const int aType = aShape.ShapeType();
346 if ( aType <= TopAbs_EDGE || !toSelectObject ) // edge, wire, face, shell, solid, compound
348 GEOM::short_array anIndexes;
350 TColStd_IndexedMapOfInteger aMap;
351 SalomeApp_Application* anApp =
352 (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
353 anApp->selectionMgr()->GetIndexes( anIO, aMap );
355 if ( !aMap.IsEmpty() ) // sub-shape selection
357 if ( toSelectObject ) {
359 myObject = aSelectedObj;
360 myEditCurrentArgument->setText( aName += QString( ":edge_%1" ).arg( myIndex ) );
362 else if (( myPoint = getSelected( TopAbs_VERTEX )))
364 myEditCurrentArgument->setText( aName += QString( ":vertex_%1" ).arg( aMap( 1 )));
367 else if ( aType == TopAbs_EDGE && toSelectObject ) // single shape selection
370 myObject = aSelectedObj;
371 myEditCurrentArgument->setText( aName );
373 else if ( aType == TopAbs_VERTEX && !toSelectObject ) // single shape selection
375 myPoint = aSelectedObj;
376 myEditCurrentArgument->setText( aName );
378 else // face, shell, solid or compound was selected, and NOT its sub-shape.
389 //=================================================================================
390 // function : SetEditCurrentArgument()
392 //=================================================================================
393 void RepairGUI_DivideEdgeDlg::SetEditCurrentArgument()
395 QPushButton* send = (QPushButton*)sender();
397 if ( send == GroupPoints->PushButton1 ) {
398 GroupPoints->LineEdit1->setFocus();
399 myEditCurrentArgument = GroupPoints->LineEdit1;
401 GroupPoints->PushButton2->setDown(false);
402 GroupPoints->LineEdit2->setEnabled(false);
404 if ( send == GroupPoints->PushButton2 ) {
405 GroupPoints->LineEdit2->setFocus();
406 myEditCurrentArgument = GroupPoints->LineEdit2;
408 GroupPoints->PushButton1->setDown(false);
409 GroupPoints->LineEdit1->setEnabled(false);
412 myEditCurrentArgument->setEnabled(true);
413 myEditCurrentArgument->setFocus();
414 // after setFocus(), because it will be setDown(false) when loses focus
418 SelectionIntoArgument();
422 //=================================================================================
423 // function : LineEditReturnPressed()
425 //=================================================================================
426 void RepairGUI_DivideEdgeDlg::LineEditReturnPressed()
428 if ( sender() == GroupPoints->LineEdit1 ) {
429 myEditCurrentArgument = GroupPoints->LineEdit1;
430 GEOMBase_Skeleton::LineEditReturnPressed();
432 if ( sender() == GroupPoints->LineEdit2 &&
433 !GroupPoints->LineEdit2->isHidden() ) {
434 myEditCurrentArgument = GroupPoints->LineEdit2;
435 GEOMBase_Skeleton::LineEditReturnPressed();
440 //=================================================================================
441 // function : ActivateThisDialog()
443 //=================================================================================
444 void RepairGUI_DivideEdgeDlg::ActivateThisDialog()
446 GEOMBase_Skeleton::ActivateThisDialog();
447 connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
448 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
450 // myObject = GEOM::GEOM_Object::_nil();
453 //myGeomGUI->SetState( 0 );
454 ConstructorsClicked(getConstructorId());
457 //=================================================================================
458 // function : enterEvent()
459 // purpose : Mouse enter onto the dialog to activate it
460 //=================================================================================
461 void RepairGUI_DivideEdgeDlg::enterEvent( QEvent* )
463 if ( !mainFrame()->GroupConstructors->isEnabled() )
464 ActivateThisDialog();
468 //=================================================================================
469 // function : createOperation
471 //=================================================================================
472 GEOM::GEOM_IOperations_ptr RepairGUI_DivideEdgeDlg::createOperation()
474 return getGeomEngine()->GetIHealingOperations( getStudyId() );
477 //=================================================================================
478 // function : isValid
480 //=================================================================================
481 bool RepairGUI_DivideEdgeDlg::isValid( QString& msg )
484 if ( getConstructorId() == BY_PARAM )
486 ok = myValEdt->isValid( msg, !IsPreview() );
488 else if ( getConstructorId() == BY_POINT_PROJ )
490 if (( ok = myPoint ) && !( ok = myProjectionOK ))
491 msg = tr("DEVIDE_EDGE_BAD_PROJ_MSG");
493 return !myObject->_is_nil() && ok;
496 //=================================================================================
497 // function : execute
499 //=================================================================================
500 bool RepairGUI_DivideEdgeDlg::execute( ObjectList& objects )
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() );
507 anObj = anOper->DivideEdgeByPoint( myObject, myIndex, myPoint.get() );
509 bool aResult = !anObj->_is_nil();
512 if ( !IsPreview() && ( getConstructorId() == BY_PARAM ))
514 QStringList aParameters;
516 aParameters << myValEdt->text();
518 anObj->SetParameters(aParameters.join(":").toLatin1().constData());
520 objects.push_back( anObj._retn() );
526 //=================================================================================
527 // function : getIsByParameter
529 //=================================================================================
530 bool RepairGUI_DivideEdgeDlg::getIsByParameter() const
532 return myIsParameterGr->button( 0 )->isChecked();
535 //=================================================================================
536 // function : initSelection
538 //=================================================================================
539 void RepairGUI_DivideEdgeDlg::initSelection()
541 TopAbs_ShapeEnum type = TopAbs_EDGE;
542 if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
543 type = TopAbs_VERTEX;
545 globalSelection(); // close local contexts, if any
546 localSelection( GEOM::GEOM_Object::_nil(), type ); // load local selection on ALL objects
549 //=================================================================================
550 // function : addSubshapeToStudy
551 // purpose : virtual method to add new SubObjects if local selection
552 //=================================================================================
553 void RepairGUI_DivideEdgeDlg::addSubshapesToStudy()
555 GEOMBase::PublishSubObject( myPoint.get() );