1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2003 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.
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
22 // File : EntityGUI_3DSketcherDlg.cxx
26 #include "EntityGUI_3DSketcherDlg.h"
27 #include "EntityGUI_Widgets.h"
28 #include <QtxDoubleSpinBox.h>
31 #include <GeometryGUI.h>
32 #include <GEOMImpl_Types.hxx>
33 #include <Precision.hxx>
36 #include <SUIT_Desktop.h>
37 #include <SUIT_Session.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_ResourceMgr.h>
40 #include <SalomeApp_Application.h>
41 #include <LightApp_Application.h>
42 #include <LightApp_SelectionMgr.h>
44 #include <BRep_Tool.hxx>
46 #include <TopExp_Explorer.hxx>
47 #include <TopoDS_Vertex.hxx>
49 #include <TColStd_IndexedMapOfInteger.hxx>
50 #include <BRepBuilderAPI_Transform.hxx>
51 #include <BRepBuilderAPI_MakeWire.hxx>
56 Locker( bool& l ) : myLock( l ) { myLock = true; }
57 ~Locker() { myLock = false; }
62 //=================================================================================
63 // class : EntityGUI_3DSketcherDlg()
64 // purpose : Constructs a EntityGUI_3DSketcherDlg 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 EntityGUI_3DSketcherDlg::EntityGUI_3DSketcherDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
70 bool modal, Qt::WindowFlags fl,
71 const double lineWidth )
72 : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl ),
73 myGeometryGUI( theGeometryGUI ),
74 myLineWidth( lineWidth )
76 QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
77 QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_UNDO" ) ) );
78 QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_REDO" ) ) );
79 QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICO_3DSKETCH" ) ) );
81 setWindowTitle( tr( "GEOM_3DSKETCHER_TITLE" ) );
83 /***************************************************************/
85 mainFrame()->GroupConstructors->setTitle( tr( "GEOM_3DSKETCHER" ) );
86 mainFrame()->RadioButton1->setIcon( image3 );;
87 mainFrame()->RadioButton2->close();
88 mainFrame()->RadioButton3->close();
90 GroupType = new DlgRef_3Radio(centralWidget());
91 GroupType->GroupBox1->setTitle(tr("GEOM_COORDINATES_TYPE"));
92 GroupType->RadioButton1->setText(tr("GEOM_SKETCHER_ABS"));
93 GroupType->RadioButton2->setText(tr("GEOM_SKETCHER_REL"));
94 GroupType->RadioButton3->close();
96 Group3Spin = new EntityGUI_3Spin( centralWidget() );
97 Group3Spin->GroupBox1->setTitle( tr( "GEOM_SKETCHER_VALUES" ) );
98 Group3Spin->buttonApply->setText( tr( "GEOM_SKETCHER_APPLY" ) );
99 Group3Spin->buttonUndo->setIcon( image1 );
100 Group3Spin->buttonRedo->setIcon( image2 );
101 Group3Spin->TextLabel1->setText( tr( "GEOM_SKETCHER_X2" ) );
102 Group3Spin->TextLabel2->setText( tr( "GEOM_SKETCHER_Y2" ) );
103 Group3Spin->TextLabel3->setText( tr( "GEOM_SKETCHER_Z2" ) );
105 buttonOk()->setText( tr( "GEOM_BUT_END_SKETCH" ) );
106 buttonApply()->setText( tr( "GEOM_BUT_CLOSE_SKETCH" ) );
108 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
109 layout->setMargin( 0 ); layout->setSpacing( 6 );
110 layout->addWidget( GroupType );
111 layout->addWidget( Group3Spin );
113 setHelpFileName( "create_3dsketcher_page.html" );
121 //=================================================================================
122 // function : ~EntityGUI_3DSketcherDlg()
123 // purpose : Destroys the object and frees any allocated resources
124 //=================================================================================
125 EntityGUI_3DSketcherDlg::~EntityGUI_3DSketcherDlg()
127 myGeomGUI->SetActiveDialogBox( 0 );
130 //=================================================================================
133 //=================================================================================
134 void EntityGUI_3DSketcherDlg::Init()
138 localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
140 /* Get setting of step value from file configuration */
141 double step = SUIT_Session::session()->resourceMgr()->doubleValue( "Geometry", "SettingsGeomStep", 100.0 );
143 /* min, max, step and decimals for spin boxes */
144 initSpinBox( Group3Spin->SpinBox_DX, COORD_MIN, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
145 initSpinBox( Group3Spin->SpinBox_DY, COORD_MIN, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
146 initSpinBox( Group3Spin->SpinBox_DZ, COORD_MIN, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
148 /* signals and slots connections */
149 connect( this, SIGNAL( constructorsClicked( int ) ), this, SLOT( PointClicked( int ) ) );
151 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
152 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
154 connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
156 connect( Group3Spin->buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnAddPoint() ) );
157 connect( Group3Spin->buttonUndo, SIGNAL( clicked() ), this, SLOT( ClickOnUndo() ) );
158 connect( Group3Spin->buttonRedo, SIGNAL( clicked() ), this, SLOT( ClickOnRedo() ) ) ;
160 connect( GroupType->RadioButton1, SIGNAL( clicked() ), this, SLOT( TypeClicked() ) );
161 connect( GroupType->RadioButton2, SIGNAL( clicked() ), this, SLOT( TypeClicked() ) );
163 connect( Group3Spin->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
164 connect( Group3Spin->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
165 connect( Group3Spin->SpinBox_DZ, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
167 connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
169 connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
170 connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) );
172 initName( tr( "GEOM_3DSKETCHER" ) );
174 UpdateButtonsState();
175 GEOMBase_Helper::displayPreview( false, true, true, myLineWidth );
178 //=================================================================================
179 // function : TypeClicked()
180 // purpose : Radio button management
181 //=================================================================================
182 void EntityGUI_3DSketcherDlg::TypeClicked()
184 bool blocked = Group3Spin->SpinBox_DX->signalsBlocked();
185 Group3Spin->SpinBox_DX->blockSignals(true);
186 Group3Spin->SpinBox_DY->blockSignals(true);
187 Group3Spin->SpinBox_DZ->blockSignals(true);
188 // Get setting of step value from file configuration
190 GetLastPoints(x, y, z);
191 if ( GroupType->RadioButton1->isChecked() ) { // XY
192 Group3Spin->TextLabel1->setText( tr( "GEOM_SKETCHER_X2" ) );
193 Group3Spin->TextLabel2->setText( tr( "GEOM_SKETCHER_Y2" ) );
194 Group3Spin->TextLabel3->setText( tr( "GEOM_SKETCHER_Z2" ) );
195 Group3Spin->SpinBox_DX->setValue( x + Group3Spin->SpinBox_DX->value() );
196 Group3Spin->SpinBox_DY->setValue( y + Group3Spin->SpinBox_DY->value() );
197 Group3Spin->SpinBox_DZ->setValue( z + Group3Spin->SpinBox_DZ->value() );
198 Group3Spin->buttonApply->setFocus();
199 } else if ( GroupType->RadioButton2->isChecked() ) { // DXDY
200 Group3Spin->TextLabel1->setText( tr( "GEOM_SKETCHER_DX2" ) );
201 Group3Spin->TextLabel2->setText( tr( "GEOM_SKETCHER_DY2" ) );
202 Group3Spin->TextLabel3->setText( tr( "GEOM_SKETCHER_DZ2" ) );
203 Group3Spin->SpinBox_DX->setValue( Group3Spin->SpinBox_DX->value() - x );
204 Group3Spin->SpinBox_DY->setValue( Group3Spin->SpinBox_DY->value() - y );
205 Group3Spin->SpinBox_DZ->setValue( Group3Spin->SpinBox_DZ->value() - z );
206 Group3Spin->buttonApply->setFocus();
208 Group3Spin->SpinBox_DX->blockSignals(blocked);
209 Group3Spin->SpinBox_DY->blockSignals(blocked);
210 Group3Spin->SpinBox_DZ->blockSignals(blocked);
213 //=================================================================================
214 // function : ClickOnAddPoint()
215 // purpose : called when the point coordinates is Applyed
216 //=================================================================================
217 void EntityGUI_3DSketcherDlg::ClickOnAddPoint()
222 GetCurrentPoints(x, y, z);
223 myPointsList.append(x);
224 myPointsList.append(y);
225 myPointsList.append(z);
229 if ( GroupType->RadioButton2->isChecked() ) {
230 Group3Spin->SpinBox_DX->setValue( 0.0 );
231 Group3Spin->SpinBox_DY->setValue( 0.0 );
232 Group3Spin->SpinBox_DZ->setValue( 0.0 );
234 GEOMBase_Helper::displayPreview( false, true, true, myLineWidth );
235 UpdateButtonsState();
238 //=================================================================================
239 // function : UpdateButtonsState()
241 //=================================================================================
242 void EntityGUI_3DSketcherDlg::UpdateButtonsState()
244 if ( !myPointsList.count() )
245 GroupType->RadioButton1->setChecked( true );
246 GroupType->RadioButton2->setEnabled( myPointsList.count() > 2 );
247 Group3Spin->buttonUndo->setEnabled( myPointsList.count() > 2 );
248 Group3Spin->buttonRedo->setEnabled( myRedoList.count() > 2 );
251 //=================================================================================
252 // function : ClickOnUndo()
254 //=================================================================================
255 void EntityGUI_3DSketcherDlg::ClickOnUndo()
257 if (myPointsList.count() > 2) {
259 GetLastPoints(x, y, z);
260 myRedoList.append(x);
261 myRedoList.append(y);
262 myRedoList.append(z);
263 myPointsList.removeLast();
264 myPointsList.removeLast();
265 myPointsList.removeLast();
267 UpdateButtonsState();
268 GEOMBase_Helper::displayPreview( false, true, true, myLineWidth );
272 //=================================================================================
273 // function : ClickOnRedo()
275 //=================================================================================
276 void EntityGUI_3DSketcherDlg::ClickOnRedo()
278 int count = myRedoList.count();
280 myPointsList.append( myRedoList[count-3] );
281 myPointsList.append( myRedoList[count-2] );
282 myPointsList.append( myRedoList[count-1] );
283 myRedoList.removeLast();
284 myRedoList.removeLast();
285 myRedoList.removeLast();
287 UpdateButtonsState();
288 GEOMBase_Helper::displayPreview( false, true, true, myLineWidth );
292 //=================================================================================
293 // function : SelectionIntoArgument()
294 // purpose : Called when selection as changed
295 //=================================================================================
296 void EntityGUI_3DSketcherDlg::SelectionIntoArgument()
298 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
299 SALOME_ListIO aSelList;
300 aSelMgr->selectedObjects(aSelList);
302 int nbSel = aSelList.Extent();
304 Standard_Boolean aRes = Standard_False;
305 GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aRes);
306 if (!CORBA::is_nil(aSelectedObject) && aRes) {
308 if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE)) { // Explore the shape if its a local selection
309 TColStd_IndexedMapOfInteger aMap;
310 aSelMgr->GetIndexes(aSelList.First(), aMap);
311 if (aMap.Extent() == 1)
313 int anIndex = aMap(1);
314 GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
315 aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
319 if ( aShape.ShapeType() != TopAbs_VERTEX )
320 isOk = GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_VERTEX);
323 if ( GEOMBase::VertexToPoint( aShape, aPnt ) ) {
324 // set coordinates to the Spin Boxes
329 bool blocked = Group3Spin->SpinBox_DX->signalsBlocked();
330 Group3Spin->SpinBox_DX->blockSignals(true);
331 Group3Spin->SpinBox_DY->blockSignals(true);
332 Group3Spin->SpinBox_DZ->blockSignals(true);
333 if ( GroupType->RadioButton1->isChecked() ) {
334 Group3Spin->SpinBox_DX->setValue( aX );
335 Group3Spin->SpinBox_DY->setValue( aY );
336 Group3Spin->SpinBox_DZ->setValue( aZ );
337 } else if ( GroupType->RadioButton2->isChecked() ) {
339 GetLastPoints(x, y, z);
340 Group3Spin->SpinBox_DX->setValue( aX - x );
341 Group3Spin->SpinBox_DY->setValue( aY - y );
342 Group3Spin->SpinBox_DZ->setValue( aZ - z );
344 Group3Spin->SpinBox_DX->blockSignals(blocked);
345 Group3Spin->SpinBox_DY->blockSignals(blocked);
346 Group3Spin->SpinBox_DZ->blockSignals(blocked);
351 GEOMBase_Helper::displayPreview( false, true, true, myLineWidth );
354 //=================================================================================
355 // function : DeactivateActiveDialog()
357 //=================================================================================
358 void EntityGUI_3DSketcherDlg::DeactivateActiveDialog()
362 disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
363 myGeomGUI->SetActiveDialogBox( 0 );
367 //=================================================================================
368 // function : ActivateThisDialog()
370 //=================================================================================
371 void EntityGUI_3DSketcherDlg::ActivateThisDialog()
373 myGeomGUI->EmitSignalDeactivateDialog();
375 myGeomGUI->SetActiveDialogBox( this );
377 connect( myGeomGUI->getApp()->selectionMgr(),
378 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
380 localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
381 GEOMBase_Helper::displayPreview( false, true, true, myLineWidth );
385 //=================================================================================
386 // function : ValueChangedInSpinBox()
388 //=================================================================================
389 void EntityGUI_3DSketcherDlg::ValueChangedInSpinBox( double newValue )
391 GEOMBase_Helper::displayPreview( false, true, true, myLineWidth );
394 //=================================================================================
395 // function : enterEvent()
397 //=================================================================================
398 void EntityGUI_3DSketcherDlg::enterEvent( QEvent* )
400 if ( !mainFrame()->GroupConstructors->isEnabled() )
401 ActivateThisDialog();
404 //=================================================================================
405 // function : createOperation
407 //=================================================================================
408 GEOM::GEOM_IOperations_ptr EntityGUI_3DSketcherDlg::createOperation()
410 return getGeomEngine()->GetICurvesOperations( getStudyId() );
413 //=================================================================================
414 // function : isValid
416 //=================================================================================
417 bool EntityGUI_3DSketcherDlg::isValid( QString& msg )
422 //=================================================================================
423 // function : execute
425 //=================================================================================
426 bool EntityGUI_3DSketcherDlg::execute( ObjectList& objects )
428 GEOM::ListOfDouble_var aCoordsArray = new GEOM::ListOfDouble;
429 if (!myOK || myPointsList.size() == 0)
430 aCoordsArray->length(myPointsList.size()+3);
432 aCoordsArray->length(myPointsList.size());
435 QList<double>::const_iterator it;
436 for(it = myPointsList.begin(); it != myPointsList.end(); ++it ) {
437 aCoordsArray[i] = *it;
441 if (!myOK || myPointsList.size() == 0) {
443 GetCurrentPoints(x, y, z);
445 aCoordsArray[i+1] = y;
446 aCoordsArray[i+2] = z;
449 GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow(getOperation());
450 GEOM::GEOM_Object_var anObj = anOper->Make3DSketcher( aCoordsArray );
452 if ( !anObj->_is_nil() )
453 objects.push_back( anObj._retn() );
458 //=================================================================================
459 // function : initSpinBox()
461 //=================================================================================
463 void EntityGUI_3DSketcherDlg::initSpinBox( QDoubleSpinBox* spinBox,
464 double min, double max,
465 double step, int decimals )
467 spinBox->setDecimals( decimals );
468 spinBox->setRange( min, max );
469 spinBox->setSingleStep( step );
472 //=================================================================================
473 // function : SetDoubleSpinBoxStep()
474 // purpose : Double spin box management
475 //=================================================================================
476 void EntityGUI_3DSketcherDlg::SetDoubleSpinBoxStep( double step )
478 Group3Spin->SpinBox_DX->setSingleStep(step);
479 Group3Spin->SpinBox_DY->setSingleStep(step);
480 Group3Spin->SpinBox_DZ->setSingleStep(step);
483 //=================================================================================
484 // function : ClickOnOk()
486 //=================================================================================
487 void EntityGUI_3DSketcherDlg::ClickOnOk()
497 //=================================================================================
498 // function : ClickOnApply()
500 //=================================================================================
501 bool EntityGUI_3DSketcherDlg::ClickOnApply()
503 if (myPointsList.count() > 3) {
504 myPointsList.append(myPointsList[0]);
505 myPointsList.append(myPointsList[1]);
506 myPointsList.append(myPointsList[2]);
518 //=================================================================================
519 // function : GetLastPoints()
520 // purpose : return last points from list
521 //=================================================================================
522 void EntityGUI_3DSketcherDlg::GetLastPoints(double& x, double& y, double& z)
524 int count = myPointsList.count();
525 x = count > 2 ? myPointsList[count-3] : 0.0;
526 y = count > 2 ? myPointsList[count-2] : 0.0;
527 z = count > 2 ? myPointsList[count-1] : 0.0;
530 //=================================================================================
531 // function : GetCurrentPoints()
532 // purpose : returns current points
533 //=================================================================================
534 void EntityGUI_3DSketcherDlg::GetCurrentPoints(double& x, double& y, double& z)
536 if ( GroupType->RadioButton1->isChecked() ) {
537 x = Group3Spin->SpinBox_DX->value();
538 y = Group3Spin->SpinBox_DY->value();
539 z = Group3Spin->SpinBox_DZ->value();
540 } else { // if (GroupType->RadioButton2->isChecked())
541 GetLastPoints(x, y, z);
542 x += Group3Spin->SpinBox_DX->value();
543 y += Group3Spin->SpinBox_DY->value();
544 z += Group3Spin->SpinBox_DZ->value();
548 //================================================================
549 // Function : displayPreview
550 // Purpose : Method for displaying preview of resulting shape
551 // Redefined from GEOMBase_Helper.
552 //================================================================
553 void EntityGUI_3DSketcherDlg::displayPreview( GEOM::GEOM_Object_ptr object,
557 const double lineWidth,
558 const int displayMode,
561 // Set color for preview shape
562 getDisplayer()->SetColor( Quantity_NOC_RED );
564 // set width of displayed shape
565 getDisplayer()->SetWidth( (lineWidth == -1)?myLineWidth:lineWidth );
567 // Disable activation of selection
568 getDisplayer()->SetToActivate( activate );
570 // Make a reference to GEOM_Object
571 CORBA::String_var objStr = myGeometryGUI->getApp()->orb()->object_to_string( object );
572 getDisplayer()->SetName( objStr.in() );
574 // Create wire from applayed object
575 TopoDS_Shape anApplyedWire, aLastSegment;
576 if ( !createShapes( object, anApplyedWire, aLastSegment ) )
580 SALOME_Prs* aPrs = getDisplayer()->BuildPrs( anApplyedWire );
581 if ( aPrs != 0 && !aPrs->IsNull() )
582 GEOMBase_Helper::displayPreview( aPrs, append, update );
584 getDisplayer()->SetColor( Quantity_NOC_VIOLET );
585 aPrs = getDisplayer()->BuildPrs( aLastSegment );
586 if ( aPrs != 0 && !aPrs->IsNull() )
587 GEOMBase_Helper::displayPreview( aPrs, append, update );
589 getDisplayer()->UnsetName();
591 // Enable activation of displayed objects
592 getDisplayer()->SetToActivate( true );
595 //================================================================
596 // Function : createShapes
597 // Purpose : Create applyed wire, and last segment from entry object
598 //================================================================
599 bool EntityGUI_3DSketcherDlg::createShapes( GEOM::GEOM_Object_ptr theObject,
600 TopoDS_Shape& theApplyedWire,
601 TopoDS_Shape& theLastSegment )
604 if ( !GEOMBase::GetShape( theObject, aShape ) ||
605 aShape.ShapeType() != TopAbs_WIRE && aShape.ShapeType() != TopAbs_VERTEX )
609 theApplyedWire = aShape;
613 BRepBuilderAPI_MakeWire aBuilder;
614 TopExp_Explorer anExp( aShape, TopAbs_EDGE );
616 TopoDS_Shape anEdge = anExp.Current();
618 if ( anExp.More() ) // i.e. non-last edge
619 aBuilder.Add( TopoDS::Edge( anEdge ) );
621 theLastSegment = anEdge;
626 if ( aBuilder.IsDone() )
627 theApplyedWire = aBuilder.Shape();