1 // Copyright (C) 2007-2016 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 "RepairGUI.h"
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>
40 #include <GEOMImpl_Types.hxx>
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>
50 #include <TopExp_Explorer.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_MapOfShape.hxx>
54 #include <TopoDS_Compound.hxx>
55 #include <TopoDS_Edge.hxx>
58 enum { BY_PARAM, BY_POINT_PROJ };
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,
69 : GEOMBase_Skeleton( theGeometryGUI, parent, modal )
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" ) ) );
75 setWindowTitle( tr( "GEOM_DIVIDE_EDGE_TITLE" ) );
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();
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 );
93 QRadioButton* rb1 = new QRadioButton( tr( "GEOM_BY_PARAMETER" ), GroupPoints->Box );
94 QRadioButton* rb2 = new QRadioButton( tr( "GEOM_BY_LENGTH" ), GroupPoints->Box );
96 myIsParameterGr = new QButtonGroup( GroupPoints->Box );
97 myIsParameterGr->addButton( rb1, 0 );
98 myIsParameterGr->addButton( rb2, 1 );
99 rb1->setChecked( true );
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 );
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 );
113 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
114 layout->setMargin( 0 ); layout->setSpacing( 6 );
115 layout->addWidget( GroupPoints );
117 /***************************************************************/
119 setHelpFileName( "add_point_on_edge_operation_page.html" );
124 //=======================================================================
125 //function : ConstructorsClicked
126 //purpose : hide/show widgets depending on a selected constructor
127 //=======================================================================
128 void RepairGUI_DivideEdgeDlg::ConstructorsClicked( int constructorId )
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 );
140 if ( constructorId == BY_PARAM &&
141 !GroupPoints->PushButton1->isDown() )
142 GroupPoints->PushButton1->click();
147 //=================================================================================
148 // function : ~RepairGUI_DivideEdgeDlg()
149 // purpose : Destroys the object and frees any allocated resources
150 //=================================================================================
151 RepairGUI_DivideEdgeDlg::~RepairGUI_DivideEdgeDlg()
155 //=================================================================================
158 //=================================================================================
159 void RepairGUI_DivideEdgeDlg::Init()
162 myEditCurrentArgument = GroupPoints->LineEdit1;
164 myObject = GEOM::GEOM_Object::_nil();
167 myProjectionOK = false;
169 //myGeomGUI->SetState( 0 );
171 /* signals and slots connections */
172 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
173 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
175 connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
177 connect( myValEdt, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
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() ) );
184 connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
185 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
187 initName( tr( "DEVIDE_EDGE_NEW_OBJECT_NAME" ) );
189 ConstructorsClicked( BY_PARAM );
190 SelectionIntoArgument();
193 //=================================================================================
194 // function : ValueChangedInSpinBox()
195 // purpose : On change value in spin box
196 //=================================================================================
197 void RepairGUI_DivideEdgeDlg::ValueChangedInSpinBox()
202 //=================================================================================
203 // function : displayPreview()
204 // purpose : On display Preview
205 //=================================================================================
206 void RepairGUI_DivideEdgeDlg::displayPreview()
208 myProjectionOK = false;
210 if ( myObject->_is_nil() )
215 GEOMBase::GetShape( myObject, aShape, TopAbs_SHAPE );
217 if ( myIndex != -1 ) {
218 TopTools_IndexedMapOfShape aShapes;
219 TopExp::MapShapes(aShape, aShapes);
220 aShape = aShapes.FindKey(myIndex);
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;
228 if ( getConstructorId() == BY_PARAM )
230 aP = aFP + (aLP - aFP) * myValEdt->value();
231 aPnt = aCurve->Value(aP);
232 BRepBuilderAPI_MakeVertex mkVertex (aPnt);
233 aShape = mkVertex.Shape();
235 else if ( getConstructorId() == BY_POINT_PROJ && !myPoints.empty() )
237 BRep_Builder builder;
238 TopoDS_Compound compoundOfVV;
239 builder.MakeCompound(compoundOfVV);
240 aShape = compoundOfVV;
242 GeomAPI_ProjectPointOnCurve aProjector;
243 aProjector.Init( aCurve, aFP, aLP );
245 TopTools_MapOfShape vMap;
246 for ( int i = 0; i < myPoints.count(); ++i )
249 GEOMBase::GetShape( myPoints[i].get(), aPoint, TopAbs_VERTEX );
250 if ( !vMap.Add( aPoint )) continue;
252 gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( aPoint ));
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 GroupPoints->LineEdit1->setText( "" );
300 GroupPoints->LineEdit2->setText( "" );
301 myObject = GEOM::GEOM_Object::_nil();
304 myProjectionOK = false;
306 GroupPoints->PushButton1->click();
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()
319 myEditCurrentArgument->setText( "" );
321 const bool toSelectObject = ( myEditCurrentArgument == GroupPoints->LineEdit1 );
322 if ( toSelectObject )
324 myObject = GEOM::GEOM_Object::_nil();
326 myProjectionOK = false;
328 else //if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
331 myProjectionOK = false;
334 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
335 SALOME_ListIO aSelList;
336 aSelMgr->selectedObjects(aSelList);
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 ) )
344 QString aName = GEOMBase::GetName( aSelectedObj );
345 if ( GEOMBase::GetShape( aSelectedObj, aShape, TopAbs_SHAPE ) )
347 const int aType = aShape.ShapeType();
348 if ( aType <= TopAbs_EDGE ) // edge, wire, face, shell, solid, compound
350 GEOM::short_array anIndexes;
352 TColStd_IndexedMapOfInteger aMap;
353 SalomeApp_Application* anApp =
354 (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
355 anApp->selectionMgr()->GetIndexes( anIO, aMap );
357 if ( !aMap.IsEmpty() ) // sub-shape selection
360 myObject = aSelectedObj;
361 myEditCurrentArgument->setText( aName += QString( ":edge_%1" ).arg( myIndex ) );
363 else if ( aType == TopAbs_EDGE ) // single shape selection
366 myObject = aSelectedObj;
367 myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
369 else // face, shell, solid or compound was selected, and NOT its sub-shape.
372 myObject = GEOM::GEOM_Object::_nil();
378 if ( !toSelectObject )
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() ));
386 myEditCurrentArgument->setText( QObject::tr( "%1_vertices" ).arg( myPoints.count() ));
392 //=================================================================================
393 // function : SetEditCurrentArgument()
395 //=================================================================================
396 void RepairGUI_DivideEdgeDlg::SetEditCurrentArgument()
398 QPushButton* send = (QPushButton*)sender();
400 if ( send == GroupPoints->PushButton1 ) {
401 GroupPoints->LineEdit1->setFocus();
402 myEditCurrentArgument = GroupPoints->LineEdit1;
404 GroupPoints->PushButton2->setDown(false);
405 GroupPoints->LineEdit2->setEnabled(false);
407 if ( send == GroupPoints->PushButton2 ) {
408 GroupPoints->LineEdit2->setFocus();
409 myEditCurrentArgument = GroupPoints->LineEdit2;
411 GroupPoints->PushButton1->setDown(false);
412 GroupPoints->LineEdit1->setEnabled(false);
415 myEditCurrentArgument->setEnabled(true);
416 myEditCurrentArgument->setFocus();
417 // after setFocus(), because it will be setDown(false) when loses focus
421 SelectionIntoArgument();
425 //=================================================================================
426 // function : LineEditReturnPressed()
428 //=================================================================================
429 void RepairGUI_DivideEdgeDlg::LineEditReturnPressed()
431 if ( sender() == GroupPoints->LineEdit1 ) {
432 myEditCurrentArgument = GroupPoints->LineEdit1;
433 GEOMBase_Skeleton::LineEditReturnPressed();
435 if ( sender() == GroupPoints->LineEdit2 &&
436 !GroupPoints->LineEdit2->isHidden() ) {
437 myEditCurrentArgument = GroupPoints->LineEdit2;
438 GEOMBase_Skeleton::LineEditReturnPressed();
443 //=================================================================================
444 // function : ActivateThisDialog()
446 //=================================================================================
447 void RepairGUI_DivideEdgeDlg::ActivateThisDialog()
449 GEOMBase_Skeleton::ActivateThisDialog();
450 connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
451 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
453 // myObject = GEOM::GEOM_Object::_nil();
456 //myGeomGUI->SetState( 0 );
457 ConstructorsClicked(getConstructorId());
460 //=================================================================================
461 // function : enterEvent()
462 // purpose : Mouse enter onto the dialog to activate it
463 //=================================================================================
464 void RepairGUI_DivideEdgeDlg::enterEvent( QEvent* )
466 if ( !mainFrame()->GroupConstructors->isEnabled() )
467 ActivateThisDialog();
471 //=================================================================================
472 // function : createOperation
474 //=================================================================================
475 GEOM::GEOM_IOperations_ptr RepairGUI_DivideEdgeDlg::createOperation()
477 return getGeomEngine()->GetIHealingOperations( getStudyId() );
480 //=================================================================================
481 // function : isValid
483 //=================================================================================
484 bool RepairGUI_DivideEdgeDlg::isValid( QString& msg )
487 if ( getConstructorId() == BY_PARAM )
489 ok = myValEdt->isValid( msg, !IsPreview() );
491 else if ( getConstructorId() == BY_POINT_PROJ )
493 if (( ok = myPoints.count() ) && !( ok = myProjectionOK ))
494 msg = tr("DEVIDE_EDGE_BAD_PROJ_MSG");
496 return !myObject->_is_nil() && ok;
499 //=================================================================================
500 // function : execute
502 //=================================================================================
503 bool RepairGUI_DivideEdgeDlg::execute( ObjectList& objects )
505 GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow( getOperation() );
506 GEOM::GEOM_Object_var anObj;
507 if ( getConstructorId() == BY_PARAM )
509 anObj = anOper->DivideEdge( myObject, myIndex, myValEdt->value(), getIsByParameter() );
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 );
519 bool aResult = !anObj->_is_nil();
522 if ( !IsPreview() && ( getConstructorId() == BY_PARAM ))
524 QStringList aParameters;
526 aParameters << myValEdt->text();
528 anObj->SetParameters(aParameters.join(":").toLatin1().constData());
531 RepairGUI::ShowStatistics( anOper, this );
532 objects.push_back( anObj._retn() );
538 //=================================================================================
539 // function : getIsByParameter
541 //=================================================================================
542 bool RepairGUI_DivideEdgeDlg::getIsByParameter() const
544 return myIsParameterGr->button( 0 )->isChecked();
547 //=================================================================================
548 // function : initSelection
550 //=================================================================================
551 void RepairGUI_DivideEdgeDlg::initSelection()
553 // close local contexts
555 // load local selection on ALL objects
556 localSelection( myEditCurrentArgument == GroupPoints->LineEdit2 ? TopAbs_VERTEX : TopAbs_EDGE );
559 //=================================================================================
560 // function : addSubshapeToStudy
561 // purpose : virtual method to add new SubObjects if local selection
562 //=================================================================================
563 void RepairGUI_DivideEdgeDlg::addSubshapesToStudy()
565 for ( int i = 0; i < myPoints.count(); ++i )
566 GEOMBase::PublishSubObject( myPoints[i].get() );
569 //=================================================================================
570 // function : getSourceObjects
571 // purpose : virtual method to get source objects
572 //=================================================================================
573 QList<GEOM::GeomObjPtr> RepairGUI_DivideEdgeDlg::getSourceObjects()
575 QList<GEOM::GeomObjPtr> res(myPoints);
576 GEOM::GeomObjPtr aGeomObjPtr(myObject);