1 // Copyright (C) 2007-2023 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 #include <QButtonGroup>
60 enum { BY_PARAM, BY_POINT_PROJ };
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,
71 : GEOMBase_Skeleton( theGeometryGUI, parent, modal )
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" ) ) );
77 setWindowTitle( tr( "GEOM_DIVIDE_EDGE_TITLE" ) );
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();
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 );
95 QRadioButton* rb1 = new QRadioButton( tr( "GEOM_BY_PARAMETER" ), GroupPoints->Box );
96 QRadioButton* rb2 = new QRadioButton( tr( "GEOM_BY_LENGTH" ), GroupPoints->Box );
98 myIsParameterGr = new QButtonGroup( GroupPoints->Box );
99 myIsParameterGr->addButton( rb1, 0 );
100 myIsParameterGr->addButton( rb2, 1 );
101 rb1->setChecked( true );
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 );
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 );
115 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
116 layout->setMargin( 0 ); layout->setSpacing( 6 );
117 layout->addWidget( GroupPoints );
119 /***************************************************************/
121 setHelpFileName( "add_point_on_edge_operation_page.html" );
126 //=======================================================================
127 //function : ConstructorsClicked
128 //purpose : hide/show widgets depending on a selected constructor
129 //=======================================================================
130 void RepairGUI_DivideEdgeDlg::ConstructorsClicked( int constructorId )
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 );
142 if ( constructorId == BY_PARAM &&
143 !GroupPoints->PushButton1->isDown() )
144 GroupPoints->PushButton1->click();
149 //=================================================================================
150 // function : ~RepairGUI_DivideEdgeDlg()
151 // purpose : Destroys the object and frees any allocated resources
152 //=================================================================================
153 RepairGUI_DivideEdgeDlg::~RepairGUI_DivideEdgeDlg()
157 //=================================================================================
160 //=================================================================================
161 void RepairGUI_DivideEdgeDlg::Init()
164 myEditCurrentArgument = GroupPoints->LineEdit1;
166 myObject = GEOM::GEOM_Object::_nil();
169 myProjectionOK = false;
171 //myGeomGUI->SetState( 0 );
173 /* signals and slots connections */
174 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
175 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
177 connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
179 connect( myValEdt, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
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() ) );
186 connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
187 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
189 initName( tr( "DEVIDE_EDGE_NEW_OBJECT_NAME" ) );
191 ConstructorsClicked( BY_PARAM );
192 SelectionIntoArgument();
195 //=================================================================================
196 // function : ValueChangedInSpinBox()
197 // purpose : On change value in spin box
198 //=================================================================================
199 void RepairGUI_DivideEdgeDlg::ValueChangedInSpinBox()
204 //=================================================================================
205 // function : displayPreview()
206 // purpose : On display Preview
207 //=================================================================================
208 void RepairGUI_DivideEdgeDlg::displayPreview()
210 myProjectionOK = false;
212 if ( myObject->_is_nil() )
217 GEOMBase::GetShape( myObject, aShape, TopAbs_SHAPE );
219 if ( myIndex != -1 ) {
220 TopTools_IndexedMapOfShape aShapes;
221 TopExp::MapShapes(aShape, aShapes);
222 aShape = aShapes.FindKey(myIndex);
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;
230 if ( getConstructorId() == BY_PARAM )
232 aP = aFP + (aLP - aFP) * myValEdt->value();
233 aPnt = aCurve->Value(aP);
234 BRepBuilderAPI_MakeVertex mkVertex (aPnt);
235 aShape = mkVertex.Shape();
237 else if ( getConstructorId() == BY_POINT_PROJ && !myPoints.empty() )
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 for ( int i = 0; i < myPoints.count(); ++i )
251 GEOMBase::GetShape( myPoints[i].get(), aPoint, TopAbs_VERTEX );
252 if ( !vMap.Add( aPoint )) continue;
254 gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( aPoint ));
255 aProjector.Perform( p );
256 if ( aProjector.NbPoints() > 0 )
258 double u = aProjector.LowerDistanceParameter();
259 if ( Min( u - aFP, aLP - u ) > Precision::Confusion() )
261 builder.Add( compoundOfVV, BRepBuilderAPI_MakeVertex( aProjector.NearestPoint()));
262 myProjectionOK = true;
272 SALOME_Prs* aPrs = getDisplayer()->BuildPrs( aShape );
273 if ( aPrs != 0 && !aPrs->IsNull() )
274 GEOMBase_Helper::displayPreview( aPrs, false, true );
278 //=================================================================================
279 // function : ClickOnOk()
280 // purpose : Same than click on apply but close this dialog.
281 //=================================================================================
282 void RepairGUI_DivideEdgeDlg::ClickOnOk()
284 setIsApplyAndClose( true );
285 if ( ClickOnApply() )
290 //=================================================================================
291 // function : ClickOnApply()
293 //=================================================================================
294 bool RepairGUI_DivideEdgeDlg::ClickOnApply()
301 GroupPoints->LineEdit1->setText( "" );
302 GroupPoints->LineEdit2->setText( "" );
303 myObject = GEOM::GEOM_Object::_nil();
306 myProjectionOK = false;
308 GroupPoints->PushButton1->click();
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()
321 myEditCurrentArgument->setText( "" );
323 const bool toSelectObject = ( myEditCurrentArgument == GroupPoints->LineEdit1 );
324 if ( toSelectObject )
326 myObject = GEOM::GEOM_Object::_nil();
328 myProjectionOK = false;
330 else //if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
333 myProjectionOK = false;
336 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
337 SALOME_ListIO aSelList;
338 aSelMgr->selectedObjects(aSelList);
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 ) )
346 QString aName = GEOMBase::GetName( aSelectedObj );
347 if ( GEOMBase::GetShape( aSelectedObj, aShape, TopAbs_SHAPE ) )
349 const int aType = aShape.ShapeType();
350 if ( aType <= TopAbs_EDGE ) // edge, wire, face, shell, solid, compound
352 GEOM::short_array anIndexes;
354 TColStd_IndexedMapOfInteger aMap;
355 SalomeApp_Application* anApp =
356 (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
357 anApp->selectionMgr()->GetIndexes( anIO, aMap );
359 if ( !aMap.IsEmpty() ) // sub-shape selection
362 myObject = aSelectedObj;
363 myEditCurrentArgument->setText( aName += QString( ":edge_%1" ).arg( myIndex ) );
365 else if ( aType == TopAbs_EDGE ) // single shape selection
368 myObject = aSelectedObj;
369 myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
371 else // face, shell, solid or compound was selected, and NOT its sub-shape.
374 myObject = GEOM::GEOM_Object::_nil();
380 if ( !toSelectObject )
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() ));
388 myEditCurrentArgument->setText( QObject::tr( "%1_vertices" ).arg( myPoints.count() ));
394 //=================================================================================
395 // function : SetEditCurrentArgument()
397 //=================================================================================
398 void RepairGUI_DivideEdgeDlg::SetEditCurrentArgument()
400 QPushButton* send = (QPushButton*)sender();
402 if ( send == GroupPoints->PushButton1 ) {
403 GroupPoints->LineEdit1->setFocus();
404 myEditCurrentArgument = GroupPoints->LineEdit1;
406 GroupPoints->PushButton2->setDown(false);
407 GroupPoints->LineEdit2->setEnabled(false);
409 if ( send == GroupPoints->PushButton2 ) {
410 GroupPoints->LineEdit2->setFocus();
411 myEditCurrentArgument = GroupPoints->LineEdit2;
413 GroupPoints->PushButton1->setDown(false);
414 GroupPoints->LineEdit1->setEnabled(false);
417 myEditCurrentArgument->setEnabled(true);
418 myEditCurrentArgument->setFocus();
419 // after setFocus(), because it will be setDown(false) when loses focus
423 SelectionIntoArgument();
427 //=================================================================================
428 // function : LineEditReturnPressed()
430 //=================================================================================
431 void RepairGUI_DivideEdgeDlg::LineEditReturnPressed()
433 if ( sender() == GroupPoints->LineEdit1 ) {
434 myEditCurrentArgument = GroupPoints->LineEdit1;
435 GEOMBase_Skeleton::LineEditReturnPressed();
437 if ( sender() == GroupPoints->LineEdit2 &&
438 !GroupPoints->LineEdit2->isHidden() ) {
439 myEditCurrentArgument = GroupPoints->LineEdit2;
440 GEOMBase_Skeleton::LineEditReturnPressed();
445 //=================================================================================
446 // function : ActivateThisDialog()
448 //=================================================================================
449 void RepairGUI_DivideEdgeDlg::ActivateThisDialog()
451 GEOMBase_Skeleton::ActivateThisDialog();
452 connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
453 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
455 // myObject = GEOM::GEOM_Object::_nil();
458 //myGeomGUI->SetState( 0 );
459 ConstructorsClicked(getConstructorId());
462 //=================================================================================
463 // function : enterEvent()
464 // purpose : Mouse enter onto the dialog to activate it
465 //=================================================================================
466 void RepairGUI_DivideEdgeDlg::enterEvent( QEvent* )
468 if ( !mainFrame()->GroupConstructors->isEnabled() )
469 ActivateThisDialog();
473 //=================================================================================
474 // function : createOperation
476 //=================================================================================
477 GEOM::GEOM_IOperations_ptr RepairGUI_DivideEdgeDlg::createOperation()
479 return getGeomEngine()->GetIHealingOperations();
482 //=================================================================================
483 // function : isValid
485 //=================================================================================
486 bool RepairGUI_DivideEdgeDlg::isValid( QString& msg )
489 if ( getConstructorId() == BY_PARAM )
491 ok = myValEdt->isValid( msg, !IsPreview() );
493 else if ( getConstructorId() == BY_POINT_PROJ )
495 if (( ok = myPoints.count() ) && !( ok = myProjectionOK ))
496 msg = tr("DEVIDE_EDGE_BAD_PROJ_MSG");
498 return !myObject->_is_nil() && ok;
501 //=================================================================================
502 // function : execute
504 //=================================================================================
505 bool RepairGUI_DivideEdgeDlg::execute( ObjectList& objects )
507 GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow( getOperation() );
508 GEOM::GEOM_Object_var anObj;
509 if ( getConstructorId() == BY_PARAM )
511 anObj = anOper->DivideEdge( myObject, myIndex, myValEdt->value(), getIsByParameter() );
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 );
521 bool aResult = !anObj->_is_nil();
524 if ( !IsPreview() && ( getConstructorId() == BY_PARAM ))
526 QStringList aParameters;
528 aParameters << myValEdt->text();
530 anObj->SetParameters(aParameters.join(":").toUtf8().constData());
533 RepairGUI::ShowStatistics( anOper, this );
534 objects.push_back( anObj._retn() );
540 //=================================================================================
541 // function : getIsByParameter
543 //=================================================================================
544 bool RepairGUI_DivideEdgeDlg::getIsByParameter() const
546 return myIsParameterGr->button( 0 )->isChecked();
549 //=================================================================================
550 // function : initSelection
552 //=================================================================================
553 void RepairGUI_DivideEdgeDlg::initSelection()
555 // close local contexts
557 // load local selection on ALL objects
558 localSelection( myEditCurrentArgument == GroupPoints->LineEdit2 ? TopAbs_VERTEX : TopAbs_EDGE );
561 //=================================================================================
562 // function : addSubshapeToStudy
563 // purpose : virtual method to add new SubObjects if local selection
564 //=================================================================================
565 void RepairGUI_DivideEdgeDlg::addSubshapesToStudy()
567 for ( int i = 0; i < myPoints.count(); ++i )
568 GEOMBase::PublishSubObject( myPoints[i].get() );
571 //=================================================================================
572 // function : getSourceObjects
573 // purpose : virtual method to get source objects
574 //=================================================================================
575 QList<GEOM::GeomObjPtr> RepairGUI_DivideEdgeDlg::getSourceObjects()
577 QList<GEOM::GeomObjPtr> res(myPoints);
578 GEOM::GeomObjPtr aGeomObjPtr(myObject);