Salome HOME
cc1110d91dbea7f20174f943e3316eee2123c943
[modules/geom.git] / src / EntityGUI / EntityGUI_3DSketcherDlg.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // GEOM GEOMGUI : GUI for Geometry component
21 // File   : EntityGUI_3DSketcherDlg.cxx
22 // Author : DMV, OCN
23 //
24 #include <cmath>
25 #include <string>
26 #include <boost/lexical_cast.hpp>
27
28 #include "EntityGUI_3DSketcherDlg.h"
29 #include "EntityGUI_Widgets.h"
30 #include <SalomeApp_DoubleSpinBox.h>
31
32 #include <GEOMBase.h>
33 #include <GeometryGUI.h>
34 #include <Precision.hxx> 
35
36 #include <DlgRef.h>
37 #include <SUIT_Session.h>
38 #include <SUIT_Desktop.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_ResourceMgr.h>
41 #include <SUIT_ViewWindow.h>
42 #include <SUIT_ViewManager.h>
43 #include <SOCC_Prs.h>
44 #include <SOCC_ViewModel.h>
45 #include <SalomeApp_Application.h>
46 #include <LightApp_Application.h>
47 #include <LightApp_SelectionMgr.h>
48
49 #include <SalomeApp_Tools.h>
50
51 //OCCT includes
52 //#include <BRep_Tool.hxx>
53 //#include <TopExp.hxx>
54 //#include <TopExp_Explorer.hxx>
55 //#include <TopoDS_Vertex.hxx>
56 #include <TopoDS.hxx>
57 #include <TColStd_IndexedMapOfInteger.hxx>
58 //#include <BRepBuilderAPI_Transform.hxx>
59 //#include <BRepBuilderAPI_MakeWire.hxx>
60 #include <BRepBuilderAPI_MakeVertex.hxx>
61 #include <BRepBuilderAPI_MakePolygon.hxx>
62 #include <BRepBuilderAPI_MakeEdge.hxx>
63
64 #include <AIS_Trihedron.hxx>
65 #include <AIS_AngleDimension.hxx>
66 #include <AIS_LengthDimension.hxx>
67 #include <AIS_Drawer.hxx>
68 #include <Geom_Axis2Placement.hxx>
69 #include <Geom_Plane.hxx>
70 #include <SelectMgr_Selection.hxx>
71 #include <gce_MakePln.hxx>
72 #include <Prs3d_AngleAspect.hxx>
73 #include <Prs3d_LineAspect.hxx>
74 #include <Prs3d_LengthAspect.hxx>
75
76 // This include must be *AFTER* SOCC_ViewModel.h because
77 // of the constant ROTATE which is a #define in
78 // GEOMImpl_Types.hxx and an enum in SOCC_ViewModel.h
79 #include <GEOMImpl_Types.hxx>
80
81 enum
82 {
83   NONE,
84   TYPE_LENGTH,
85   TYPE_ANGLE,
86   TYPE_TWO_ANGLES
87 };
88
89 class Locker
90 {
91 public:
92   Locker( bool& l ) : myLock( l ) { myLock = true;  }
93   ~Locker()                       { myLock = false; }
94 private:
95   bool& myLock;
96 };
97
98 //=================================================================================
99 // class    : EntityGUI_3DSketcherDlg()
100 // purpose  : Constructs a EntityGUI_3DSketcherDlg which is a child of 'parent', with the
101 //            name 'name' and widget flags set to 'f'.
102 //            The dialog will by default be modeless, unless you set 'modal' to
103 //            TRUE to construct a modal dialog.
104 //=================================================================================
105 EntityGUI_3DSketcherDlg::EntityGUI_3DSketcherDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
106                                                   bool modal, Qt::WindowFlags fl,
107                                                   const double lineWidth )
108   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl ),
109     myMode( -1 ),
110     myOK( false ),
111     myLineWidth( lineWidth ),
112     myGeometryGUI( theGeometryGUI ),
113     myLengthIORedoList()
114 {
115   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
116   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_UNDO" ) ) );
117   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_REDO" ) ) );
118   QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICO_3DSKETCH" ) ) );
119
120   setWindowTitle( tr( "GEOM_3DSKETCHER_TITLE" ) );
121
122   /***************************************************************/
123
124   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_3DSKETCHER" ) );
125   mainFrame()->RadioButton1->setIcon( image3 );;
126   mainFrame()->RadioButton2->close();
127   mainFrame()->RadioButton3->close();
128
129   GroupType = new DlgRef_3Radio(centralWidget());
130   GroupType->GroupBox1->setTitle(tr("GEOM_COORDINATES_TYPE"));
131   GroupType->RadioButton1->setText(tr("GEOM_SKETCHER_ABS"));
132   GroupType->RadioButton2->setText(tr("GEOM_SKETCHER_REL"));
133   GroupType->RadioButton3->setText(tr("Angles")); //TODO translation
134 //   GroupType->RadioButton3->close();
135   myTypeGroup = new QButtonGroup( this );
136   myTypeGroup->addButton( GroupType->RadioButton1, 0 );
137   myTypeGroup->addButton( GroupType->RadioButton2, 1 );
138   myTypeGroup->addButton( GroupType->RadioButton3, 2 );
139
140   Group3Spin = new EntityGUI_3Spin( centralWidget() );
141   Group3Spin->GroupBox1->setTitle( tr( "GEOM_SKETCHER_VALUES" ) );
142   Group3Spin->buttonApply->setText( tr( "GEOM_SKETCHER_APPLY" ) );
143   Group3Spin->buttonUndo->setIcon( image1 );
144   Group3Spin->buttonRedo->setIcon( image2 );
145   Group3Spin->TextLabel1->setText( tr( "GEOM_SKETCHER_X2" ) );
146   Group3Spin->TextLabel2->setText( tr( "GEOM_SKETCHER_Y2" ) );
147   Group3Spin->TextLabel3->setText( tr( "GEOM_SKETCHER_Z2" ) );
148   
149   GroupAngles = new EntityGUI_Angles( centralWidget() );
150   GroupAngles->buttonApply->setText( tr( "GEOM_SKETCHER_APPLY" ) );
151   GroupAngles->buttonUndo->setIcon( image1 );
152   GroupAngles->buttonRedo->setIcon( image2 );
153   GroupAngles->checkBox->setText( tr( "Angle 2" ) ); //TODO translation
154   
155   GroupControls = new EntityGUI_Controls( centralWidget() );
156   GroupControls->GroupBox1->setTitle(tr("GEOM_CONTROLS"));
157   GroupControls->CheckBox1->setText( tr( "Show length dimensions" ) ); //TODO translation
158   GroupControls->CheckBox2->setText( tr( "Show angle dimensions" ) );  //TODO translation
159
160   buttonOk()->setText( tr( "GEOM_BUT_END_SKETCH" ) );
161   buttonApply()->setText( tr( "GEOM_BUT_CLOSE_SKETCH" ) );
162   
163   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
164   layout->setMargin( 0 ); layout->setSpacing( 6 );
165   layout->addWidget( GroupType );
166   layout->addWidget( Group3Spin );
167   layout->addWidget( GroupAngles );
168   layout->addWidget( GroupControls );
169
170   setHelpFileName( "create_3dsketcher_page.html" );
171
172   resize(100,100);
173   Init();
174 }  
175
176
177
178 //=================================================================================
179 // function : ~EntityGUI_3DSketcherDlg()
180 // purpose  : Destroys the object and frees any allocated resources
181 //=================================================================================
182 EntityGUI_3DSketcherDlg::~EntityGUI_3DSketcherDlg()
183 {
184   myGeomGUI->SetActiveDialogBox( 0 );
185 }
186
187 //=================================================================================
188 // function : Init()
189 // purpose  :
190 //=================================================================================
191 void EntityGUI_3DSketcherDlg::Init()
192 {
193   myOK = false;
194   myOrientation = 1;
195   myPrsType = NONE;
196   
197   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
198   myAnglePrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
199   myLengthPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
200   
201   //TEST
202   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
203 //   globalSelection(GEOM_PREVIEW);
204
205   /* Get setting of step value from file configuration */
206   double step = SUIT_Session::session()->resourceMgr()->doubleValue( "Geometry", "SettingsGeomStep", 100.0 );
207
208   /* min, max, step and decimals for spin boxes */
209   initSpinBox( Group3Spin->SpinBox_DX, COORD_MIN, COORD_MAX, step, "length_precision" );
210   initSpinBox( Group3Spin->SpinBox_DY, COORD_MIN, COORD_MAX, step, "length_precision" );
211   initSpinBox( Group3Spin->SpinBox_DZ, COORD_MIN, COORD_MAX, step, "length_precision" );
212   
213   initSpinBox( GroupAngles->SpinBox_DA , -180.0, 180.0, step, "angular_precision" );
214   initSpinBox( GroupAngles->SpinBox_DA2,  -90.0,  90.0, step, "angular_precision" );
215   initSpinBox( GroupAngles->SpinBox_DL , COORD_MIN, COORD_MAX, step, "length_precision" );
216
217   Group3Spin->SpinBox_DX->setValue(0.0);
218   Group3Spin->SpinBox_DY->setValue(0.0);
219   Group3Spin->SpinBox_DZ->setValue(0.0);
220   
221   GroupAngles->SpinBox_DA->setValue(0.0);
222   GroupAngles->SpinBox_DA2->setValue(0.0);
223   GroupAngles->SpinBox_DL->setValue(0.0);
224   
225   GroupAngles->radioButton_1->setChecked(true);
226   GroupAngles->checkBox->setChecked(false);
227   GroupAngles->SpinBox_DA2->setEnabled(false); 
228   
229   GroupControls->CheckBox1->setChecked( true );
230   GroupControls->CheckBox2->setChecked( true );
231   isLengthVisible = true;
232   isAngleVisible = true;
233    
234   GroupAngles->hide();
235
236   /* signals and slots connections */
237   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
238   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
239
240   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
241
242   connect( Group3Spin->buttonApply,  SIGNAL( clicked() ), this, SLOT( ClickOnAddPoint() ) );
243   connect( Group3Spin->buttonUndo,   SIGNAL( clicked() ), this, SLOT( ClickOnUndo() ) );
244   connect( Group3Spin->buttonRedo,   SIGNAL( clicked() ), this, SLOT( ClickOnRedo() ) ) ;
245   
246   connect( GroupAngles->buttonApply,  SIGNAL( clicked() ), this, SLOT( ClickOnAddPoint() ) );
247   connect( GroupAngles->buttonUndo,   SIGNAL( clicked() ), this, SLOT( ClickOnUndo() ) );
248   connect( GroupAngles->buttonRedo,   SIGNAL( clicked() ), this, SLOT( ClickOnRedo() ) ) ;
249
250   connect( myTypeGroup, SIGNAL( buttonClicked( int ) ),  this, SLOT( TypeClicked( int ) ) );
251
252   connect( Group3Spin->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
253   connect( Group3Spin->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
254   connect( Group3Spin->SpinBox_DZ, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
255   
256   connect( GroupAngles->SpinBox_DA,  SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
257   connect( GroupAngles->SpinBox_DA2, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
258   connect( GroupAngles->SpinBox_DL,  SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
259   
260   connect( GroupAngles->radioButton_1,   SIGNAL( clicked ( bool ) ), this, SLOT( ButtonClicked(bool) ) ) ;
261   connect( GroupAngles->radioButton_2,   SIGNAL( clicked ( bool ) ), this, SLOT( ButtonClicked(bool) ) ) ;
262   connect( GroupAngles->radioButton_3,   SIGNAL( clicked ( bool ) ), this, SLOT( ButtonClicked(bool) ) ) ;
263   
264   connect( GroupAngles->checkBox,        SIGNAL( clicked ( bool ) ), this, SLOT( BoxChecked (bool) ) ) ;
265   connect( GroupControls->CheckBox1,     SIGNAL( clicked ( bool ) ), this, SLOT( BoxChecked (bool) ) ) ;
266   connect( GroupControls->CheckBox2,     SIGNAL( clicked ( bool ) ), this, SLOT( BoxChecked (bool) ) ) ;
267
268   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
269
270   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
271   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
272
273   initName( tr( "GEOM_3DSKETCHER" ) );
274
275   UpdateButtonsState();
276   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
277 }
278
279 //=================================================================================
280 // function : TypeClicked()
281 // purpose  : Radio button management
282 //=================================================================================
283 void EntityGUI_3DSketcherDlg::TypeClicked( int mode )
284 {
285   if ( mode == myMode ) return;
286
287   GroupAngles->hide();
288   Group3Spin->show();
289   
290   bool blocked = Group3Spin->SpinBox_DX->signalsBlocked();
291   Group3Spin->SpinBox_DX->blockSignals(true);
292   Group3Spin->SpinBox_DY->blockSignals(true);
293   Group3Spin->SpinBox_DZ->blockSignals(true);
294   // Get setting of step value from file configuration
295   XYZ xyz = getLastPoint();
296   bool okx, oky, okz;
297   Group3Spin->SpinBox_DX->text().toDouble( &okx );
298   Group3Spin->SpinBox_DY->text().toDouble( &oky );
299   Group3Spin->SpinBox_DZ->text().toDouble( &okz );
300   if ( mode == 0 ) {  // XY
301     Group3Spin->TextLabel1->setText( tr( "GEOM_SKETCHER_X2" ) );
302     Group3Spin->TextLabel2->setText( tr( "GEOM_SKETCHER_Y2" ) );
303     Group3Spin->TextLabel3->setText( tr( "GEOM_SKETCHER_Z2" ) );
304     if (myMode == 1)
305     {
306       if ( okx ) Group3Spin->SpinBox_DX->setValue( xyz.x + Group3Spin->SpinBox_DX->value() );
307       if ( oky ) Group3Spin->SpinBox_DY->setValue( xyz.y + Group3Spin->SpinBox_DY->value() );
308       if ( okz ) Group3Spin->SpinBox_DZ->setValue( xyz.z + Group3Spin->SpinBox_DZ->value() );
309     }
310     Group3Spin->buttonApply->setFocus();
311   }
312   else if ( mode == 1) { // DXDY
313     Group3Spin->TextLabel1->setText( tr( "GEOM_SKETCHER_DX2" ) );
314     Group3Spin->TextLabel2->setText( tr( "GEOM_SKETCHER_DY2" ) );
315     Group3Spin->TextLabel3->setText( tr( "GEOM_SKETCHER_DZ2" ) );
316     if (myMode == 0)
317     {
318       if ( okx ) Group3Spin->SpinBox_DX->setValue( Group3Spin->SpinBox_DX->value() - xyz.x );
319       if ( oky ) Group3Spin->SpinBox_DY->setValue( Group3Spin->SpinBox_DY->value() - xyz.y );
320       if ( okz ) Group3Spin->SpinBox_DZ->setValue( Group3Spin->SpinBox_DZ->value() - xyz.z );
321     }
322     Group3Spin->buttonApply->setFocus();
323   }
324   else if (mode == 2){ // Angles
325     Group3Spin->hide();
326     GroupAngles->show();
327     GroupAngles->buttonApply->setFocus();
328   }
329   
330   Group3Spin->SpinBox_DX->blockSignals(blocked);
331   Group3Spin->SpinBox_DY->blockSignals(blocked);
332   Group3Spin->SpinBox_DZ->blockSignals(blocked);
333
334   myMode = mode;
335   
336   updateGeometry();
337   resize(minimumSizeHint());
338 }
339
340 //=================================================================================
341 // function : ClickOnAddPoint()
342 // purpose  : called when the point coordinates is Applyed
343 //=================================================================================
344 void EntityGUI_3DSketcherDlg::ClickOnAddPoint()
345 {
346   QString msg;
347   if ( !isValid( msg ) ) {
348     showError( msg );
349     return;
350   }
351
352   // Erase dimensions presentations
353   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
354   ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myAnglePrs, true);
355   ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myLengthPrs, true);
356   
357   // Store last angle dimensions if any
358   if(GroupType->RadioButton3->isChecked() && GroupAngles->SpinBox_DL->value()>Precision::Confusion())
359   {
360     double anAngle2 = 0.0;
361     if (GroupAngles->checkBox->isChecked())
362       anAngle2 = GroupAngles->SpinBox_DA2->value();
363     
364     displayAngle(GroupAngles->SpinBox_DA->value(), anAngle2, GroupAngles->SpinBox_DL->value(), myOrientation, true);  
365   }
366   
367   // Store last length dimensions if any
368   if (GroupType->RadioButton1->isChecked() ||
369       GroupType->RadioButton2->isChecked())
370   {
371     displayLength(-1, true);
372   }
373   
374   // Display modified presentation
375   if (isLengthVisible)
376     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myLengthPrs);
377   if (isAngleVisible)
378     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myAnglePrs);
379   
380   myPointsList.append( getCurrentPoint() );
381   myPrsTypeList.push_back( myPrsType );
382   
383   myRedoList.clear();
384   myPrsTypeRedoList.clear();
385   myLengthIORedoList.Clear();
386   myAngleIORedoList.Clear();
387   
388   if ( myMode == 1 ) {
389     Group3Spin->SpinBox_DX->setValue( 0.0 );
390     Group3Spin->SpinBox_DY->setValue( 0.0 );
391     Group3Spin->SpinBox_DZ->setValue( 0.0 );
392   }
393   UpdateButtonsState();
394   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
395   
396 }
397
398 //=================================================================================
399 // function : UpdateButtonsState()
400 // purpose  : 
401 //=================================================================================
402 void EntityGUI_3DSketcherDlg::UpdateButtonsState()
403 {
404   if ( myPointsList.count() == 0 ) GroupType->RadioButton1->click();
405   GroupType->RadioButton2->setEnabled( myPointsList.count() > 0 );
406   GroupType->RadioButton3->setEnabled( myPointsList.count() > 0 );
407   Group3Spin->buttonUndo->setEnabled( myPointsList.count() > 0 );
408   Group3Spin->buttonRedo->setEnabled( myRedoList.count() > 0 );
409   GroupAngles->buttonUndo->setEnabled( myPointsList.count() > 0 );
410   GroupAngles->buttonRedo->setEnabled( myRedoList.count() > 0 );
411 }
412
413 //=================================================================================
414 // function : ClickOnUndo()
415 // purpose  :
416 //=================================================================================
417 void EntityGUI_3DSketcherDlg::ClickOnUndo()
418 {
419   if (myPointsList.count() > 0) {
420     myRedoList.append( myPointsList.takeLast() );
421     UpdateButtonsState();
422     GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
423     
424     // Erase dimensions presentations
425     SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
426     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myLengthPrs, true); 
427     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myAnglePrs, true);
428     
429     AIS_ListOfInteractive anIOList;
430     
431     if (myPrsTypeList.back() != NONE)
432     {
433       // Remove last prepended IO
434       myLengthPrs->GetObjects(anIOList);
435       myLengthIORedoList.Prepend(anIOList.First()); // due to AIS_ListOfInteractive structure 
436                                                     // we have to prepend objects and take first then
437       myLengthPrs->RemoveFirst();
438     }   
439     if ( myPrsTypeList.back() == TYPE_ANGLE ||
440          myPrsTypeList.back() == TYPE_TWO_ANGLES )
441     {
442       myAnglePrs->GetObjects(anIOList);
443       myAngleIORedoList.Prepend(anIOList.First());
444       myAnglePrs->RemoveFirst();
445       if ( myPrsTypeList.back() == TYPE_TWO_ANGLES )
446       {
447         // Remove a second IO
448         myAnglePrs->GetObjects(anIOList);
449         myAngleIORedoList.Prepend(anIOList.First());
450         myAnglePrs->RemoveFirst();
451       }
452     }
453        
454     // Erase last action type and store it in redo list
455     myPrsTypeRedoList.push_back(myPrsTypeList.back());
456     myPrsTypeList.pop_back();
457     
458     // Display modified presentation
459     if (isLengthVisible)
460       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myLengthPrs);
461     if (isAngleVisible)
462       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myAnglePrs);
463     
464     ValueChangedInSpinBox(0.0); // To update preview
465   }
466 }
467
468 //=================================================================================
469 // function : ClickOnRedo()
470 // purpose  :
471 //=================================================================================
472 void EntityGUI_3DSketcherDlg::ClickOnRedo()
473 {
474   if ( myRedoList.count() > 0) {
475     myPointsList.append( myRedoList.takeLast() );
476     UpdateButtonsState();
477     GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
478       
479     // Erase dimensions presentations
480     SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
481     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myLengthPrs, true);
482     
483     if ( myPrsTypeRedoList.back() != NONE )
484     {
485       myLengthPrs->PrependObject(myLengthIORedoList.First());
486       myLengthIORedoList.RemoveFirst();
487     }    
488     if ( myPrsTypeRedoList.back() == TYPE_ANGLE || 
489          myPrsTypeRedoList.back() == TYPE_TWO_ANGLES )
490     {
491       myAnglePrs->PrependObject(myAngleIORedoList.First());
492       myAngleIORedoList.RemoveFirst();
493       if ( myPrsTypeRedoList.back() == TYPE_TWO_ANGLES )
494       {
495         // Add a second IO from the Redo list
496         myAnglePrs->PrependObject(myAngleIORedoList.First());
497         myAngleIORedoList.RemoveFirst();
498       }
499     }
500        
501     // Record last prs type
502     myPrsTypeList.push_back(myPrsTypeRedoList.back());
503     myPrsTypeRedoList.pop_back();
504     
505     // Display modified presentation
506     if (isLengthVisible)
507       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myLengthPrs);
508     if (isAngleVisible)
509       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myAnglePrs);
510     
511     ValueChangedInSpinBox(0.0); // To update preview
512   }
513 }
514
515 //=================================================================================
516 // function : SelectionIntoArgument()
517 // purpose  : Called when selection as changed
518 //=================================================================================
519 void EntityGUI_3DSketcherDlg::SelectionIntoArgument()
520 {
521   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
522   SALOME_ListIO aSelList;
523   aSelMgr->selectedObjects(aSelList);
524
525   int nbSel = aSelList.Extent();
526   if ( nbSel == 1 ) {
527     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
528     if ( !CORBA::is_nil(aSelectedObject) ) {
529       TopoDS_Shape aShape;
530       if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE)) { // Explore the shape if its a local selection
531         TColStd_IndexedMapOfInteger aMap;
532         aSelMgr->GetIndexes(aSelList.First(), aMap);
533         if (aMap.Extent() == 1)
534           {
535             int anIndex = aMap(1);
536             GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
537             aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
538           }
539       }
540       bool isOk = true;
541       if ( aShape.ShapeType() != TopAbs_VERTEX )
542         isOk = GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_VERTEX);
543       if (isOk) {
544         gp_Pnt aPnt;
545         if ( GEOMBase::VertexToPoint( aShape, aPnt ) ) {
546           // set coordinates to the Spin Boxes
547           double aX, aY, aZ;
548           aX = aPnt.X();
549           aY = aPnt.Y();
550           aZ = aPnt.Z();
551           bool blocked = Group3Spin->SpinBox_DX->signalsBlocked();
552           Group3Spin->SpinBox_DX->blockSignals(true);
553           Group3Spin->SpinBox_DY->blockSignals(true);
554           Group3Spin->SpinBox_DZ->blockSignals(true);
555           if ( GroupType->RadioButton1->isChecked() ) {
556             Group3Spin->SpinBox_DX->setValue( aX );
557             Group3Spin->SpinBox_DY->setValue( aY );
558             Group3Spin->SpinBox_DZ->setValue( aZ );
559           } else if ( GroupType->RadioButton2->isChecked() ) {
560             XYZ xyz = getLastPoint();
561             Group3Spin->SpinBox_DX->setValue( aX - xyz.x );
562             Group3Spin->SpinBox_DY->setValue( aY - xyz.y );
563             Group3Spin->SpinBox_DZ->setValue( aZ - xyz.z );
564           }
565           Group3Spin->SpinBox_DX->blockSignals(blocked);
566           Group3Spin->SpinBox_DY->blockSignals(blocked);
567           Group3Spin->SpinBox_DZ->blockSignals(blocked);
568         }
569       }
570     }
571   }
572   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
573 }
574
575 //=================================================================================
576 // function : DeactivateActiveDialog()
577 // purpose  :
578 //=================================================================================
579 void EntityGUI_3DSketcherDlg::DeactivateActiveDialog()
580 {
581   setEnabled( false );
582   globalSelection();
583   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
584   myGeomGUI->SetActiveDialogBox( 0 );
585 }
586
587
588 //=================================================================================
589 // function : ActivateThisDialog()
590 // purpose  :
591 //=================================================================================
592 void EntityGUI_3DSketcherDlg::ActivateThisDialog()
593 {
594   myGeomGUI->EmitSignalDeactivateDialog();
595   setEnabled( true );
596   myGeomGUI->SetActiveDialogBox( this );
597
598   connect( myGeomGUI->getApp()->selectionMgr(),
599           SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
600
601   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
602   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
603 }
604
605
606 //=================================================================================
607 // function : ValueChangedInSpinBox()
608 // purpose  :
609 //=================================================================================
610 void EntityGUI_3DSketcherDlg::ValueChangedInSpinBox( double newValue )
611 {
612   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
613   
614   if(GroupType->RadioButton3->isChecked() && GroupAngles->SpinBox_DL->value()>Precision::Confusion())
615   {
616     double anAngle2 = 0.0;
617     if (GroupAngles->checkBox->isChecked())
618       anAngle2 = GroupAngles->SpinBox_DA2->value();
619     
620     displayAngle(GroupAngles->SpinBox_DA->value(), anAngle2, GroupAngles->SpinBox_DL->value(), myOrientation);
621     displayLength(GroupAngles->SpinBox_DL->value());  
622   }
623   if( GroupType->RadioButton1->isChecked() ||
624       GroupType->RadioButton2->isChecked() )
625   {
626     displayLength();
627   }  
628 }
629
630
631 //=================================================================================
632 // function : BoxChecked()
633 // purpose  : ChecBoxes management
634 //=================================================================================
635 void EntityGUI_3DSketcherDlg::BoxChecked( bool checked )
636 {
637   QCheckBox* send = (QCheckBox*) sender();
638   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
639   
640   if (send == GroupAngles->checkBox)
641   {
642     GroupAngles->SpinBox_DA2->setEnabled(checked); 
643     GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
644   
645     if(GroupAngles->SpinBox_DL->value()>Precision::Confusion())
646     {
647       double anAngle2 = 0.0;
648       if (checked)
649         anAngle2 = GroupAngles->SpinBox_DA2->value();
650       
651       displayAngle(GroupAngles->SpinBox_DA->value(), anAngle2, GroupAngles->SpinBox_DL->value(), myOrientation);
652       displayLength(GroupAngles->SpinBox_DL->value()); 
653     }
654   }
655   else if (send == GroupControls->CheckBox1)
656   {
657     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myLengthPrs, true);
658     if(checked){
659       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myLengthPrs);
660       isLengthVisible=true;
661     }
662     else
663       isLengthVisible=false;
664   }
665   else if (send == GroupControls->CheckBox2)
666   {
667     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myAnglePrs, true); 
668     if(checked)
669     {
670       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myAnglePrs);
671       isAngleVisible=true;
672     }
673     else
674       isAngleVisible=false;
675   }
676   updateViewer();
677   ValueChangedInSpinBox(0.0);
678 }
679
680 //=================================================================================
681 // function : ButtonClicked()
682 // purpose  :
683 //=================================================================================
684 void EntityGUI_3DSketcherDlg::ButtonClicked( bool checked )
685 {
686   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
687   if (GroupAngles->radioButton_1->isChecked())
688     myOrientation = 1;
689   else if (GroupAngles->radioButton_2->isChecked())
690     myOrientation = 2;
691   else
692     myOrientation = 3;
693   
694   if(GroupAngles->SpinBox_DL->value()>Precision::Confusion())
695   {
696     double anAngle2 = 0.0;
697     if (GroupAngles->checkBox->isChecked())
698       anAngle2 = GroupAngles->SpinBox_DA2->value();
699     
700     displayAngle(GroupAngles->SpinBox_DA->value(), anAngle2, GroupAngles->SpinBox_DL->value(), myOrientation);
701     displayLength(GroupAngles->SpinBox_DL->value());  
702   }
703 }
704
705 //=================================================================================
706 // function : enterEvent()
707 // purpose  :
708 //=================================================================================
709 void EntityGUI_3DSketcherDlg::enterEvent( QEvent* )
710 {
711   if ( !mainFrame()->GroupConstructors->isEnabled() )
712     ActivateThisDialog();
713 }
714
715 //=================================================================================
716 // function : createOperation
717 // purpose  :
718 //=================================================================================
719 GEOM::GEOM_IOperations_ptr EntityGUI_3DSketcherDlg::createOperation()
720 {
721   return getGeomEngine()->GetICurvesOperations( getStudyId() );
722 }
723
724 //=================================================================================
725 // function : isValid
726 // purpose  :
727 //=================================================================================
728 bool EntityGUI_3DSketcherDlg::isValid( QString& msg )
729 {
730   bool ok = true;
731   ok = Group3Spin->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
732   ok = Group3Spin->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
733   ok = Group3Spin->SpinBox_DZ->isValid( msg, !IsPreview() ) && ok;
734   return ok;
735 }
736
737 //=================================================================================
738 // function : execute
739 // purpose  :
740 //=================================================================================
741 bool EntityGUI_3DSketcherDlg::execute( ObjectList& objects )
742 {
743   GEOM::ListOfDouble_var aCoordsArray = new GEOM::ListOfDouble;
744   if (!myOK || myPointsList.size() == 0)
745     aCoordsArray->length((myPointsList.size()+1)*3);
746   else
747     aCoordsArray->length(myPointsList.size()*3);
748
749   QStringList aParameters;
750
751   int i = 0;
752   QList<XYZ>::const_iterator it;
753   for(it = myPointsList.begin(); it != myPointsList.end(); ++it ) {
754     aCoordsArray[i++] = (*it).x;
755     aCoordsArray[i++] = (*it).y;
756     aCoordsArray[i++] = (*it).z;
757     aParameters << (*it).xt;
758     aParameters << (*it).yt;
759     aParameters << (*it).zt;
760   }
761
762   if (!myOK || myPointsList.size() == 0) {
763     XYZ xyz = getCurrentPoint();
764     aCoordsArray[i++] = xyz.x;
765     aCoordsArray[i++] = xyz.y;
766     aCoordsArray[i++] = xyz.z;
767     aParameters << xyz.xt;
768     aParameters << xyz.yt;
769     aParameters << xyz.zt;
770   } 
771
772   GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow(getOperation());
773   GEOM::GEOM_Object_var anObj = anOper->Make3DSketcher( aCoordsArray );
774
775   if ( !anObj->_is_nil() ) {
776     if ( !IsPreview() ) anObj->SetParameters(aParameters.join(":").toLatin1().constData());
777     objects.push_back( anObj._retn() );
778   }
779
780   return true;
781 }
782
783 //=================================================================================
784 // function : SetDoubleSpinBoxStep()
785 // purpose  : Double spin box management
786 //=================================================================================
787 void EntityGUI_3DSketcherDlg::SetDoubleSpinBoxStep( double step )
788 {
789   Group3Spin->SpinBox_DX->setSingleStep(step);
790   Group3Spin->SpinBox_DY->setSingleStep(step);
791   Group3Spin->SpinBox_DZ->setSingleStep(step);
792 }
793
794 //=================================================================================
795 // function : ClickOnOk()
796 // purpose  :
797 //=================================================================================
798 void EntityGUI_3DSketcherDlg::ClickOnOk()
799 {
800   Locker lock( myOK );
801
802   if ( !onAccept() )
803     return;
804
805   ClickOnCancel();
806 }
807
808 //=================================================================================
809 // function : ClickOnApply()
810 // purpose  :
811 //=================================================================================
812 bool EntityGUI_3DSketcherDlg::ClickOnApply()
813 {
814   QString msg;
815   if ( !isValid( msg ) ) {
816     showError( msg );
817     return false;
818   }
819
820   if (myPointsList.count() > 0)
821     myPointsList.append(myPointsList[0]);
822
823   Locker lock( myOK );
824
825   if ( !onAccept() )
826     return false;
827   
828   ClickOnCancel();
829   return true;
830 }
831
832 //=================================================================================
833 // function : getLastPoint()
834 // purpose  : return last points from list
835 //=================================================================================
836 EntityGUI_3DSketcherDlg::XYZ EntityGUI_3DSketcherDlg::getLastPoint() const
837 {
838   return myPointsList.count() > 0 ? myPointsList.last() : XYZ();
839 }
840
841 //=================================================================================
842 // function : getPenultimatePoint()
843 // purpose  : return penultimate point from list
844 //=================================================================================
845 EntityGUI_3DSketcherDlg::XYZ EntityGUI_3DSketcherDlg::getPenultimatePoint() const
846 {
847   double size = myPointsList.count();
848   return size > 1 ? myPointsList.at(size - 2) : XYZ();
849 }
850
851 //=================================================================================
852 // function : getCurrentPoint()
853 // purpose  : returns current points
854 //=================================================================================
855 EntityGUI_3DSketcherDlg::XYZ EntityGUI_3DSketcherDlg::getCurrentPoint() const
856 {
857   XYZ xyz; 
858   // Temporary way of doing this. To be changed with ordered improvement of the sketcher
859   if ( myMode == 2 )
860   {
861     double anAngle  = GroupAngles->SpinBox_DA->value() * M_PI/180.0;
862     double anAngle2 = 0.0;
863     double aLength  = GroupAngles->SpinBox_DL->value();
864     
865     if (GroupAngles->checkBox->isChecked())
866       anAngle2 = GroupAngles->SpinBox_DA2->value() * M_PI/180.0;
867     
868     double aProjectedLength = aLength * cos(anAngle2);
869     
870     xyz = getLastPoint();
871     if (GroupAngles->radioButton_1->isChecked())
872     {
873       xyz.x += aProjectedLength * cos(anAngle);
874       xyz.y += aProjectedLength * sin(anAngle);
875       xyz.z += aLength * sin(anAngle2);
876     }
877     else if (GroupAngles->radioButton_2->isChecked())
878     {
879       xyz.y += aProjectedLength * cos(anAngle);
880       xyz.z += aProjectedLength * sin(anAngle);
881       xyz.x += aLength * sin(anAngle2);
882     }
883     else
884     {
885       xyz.z += aProjectedLength * sin(anAngle);
886       xyz.x += aProjectedLength * cos(anAngle);
887       xyz.y += aLength * sin(anAngle2);
888     }
889   }
890   else
891   {
892     if ( myMode == 0 ) {
893       xyz.x = Group3Spin->SpinBox_DX->value();
894       xyz.y = Group3Spin->SpinBox_DY->value();
895       xyz.z = Group3Spin->SpinBox_DZ->value();
896     } 
897     else{
898       xyz = getLastPoint();
899       xyz.x += Group3Spin->SpinBox_DX->value();
900       xyz.y += Group3Spin->SpinBox_DY->value();
901       xyz.z += Group3Spin->SpinBox_DZ->value();
902     }
903     xyz.xt = Group3Spin->SpinBox_DX->text();
904     xyz.yt = Group3Spin->SpinBox_DY->text();
905     xyz.zt = Group3Spin->SpinBox_DZ->text();
906   }
907   return xyz;
908 }
909
910 //================================================================
911 // Function : displayPreview
912 // Purpose  : Method for displaying preview of resulting shape
913 //            Redefined from GEOMBase_Helper.
914 //================================================================
915 void EntityGUI_3DSketcherDlg::displayPreview( GEOM::GEOM_Object_ptr object,
916                                               const bool            append,
917                                               const bool            activate,
918                                               const bool            update,
919                                               const double          lineWidth,
920                                               const int             displayMode,
921                                               const int             color )
922
923   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr(); 
924   
925   QColor aColor = resMgr->colorValue("Geometry","line_color",QColor(255,0,0));
926   Quantity_NameOfColor line_color = SalomeApp_Tools::color( aColor ).Name();
927   
928   // set width of displayed shape
929   int lw = lineWidth;
930   if(lw == -1) { 
931     lw = resMgr->integerValue("Geometry", "preview_edge_width", -1);
932   }
933   getDisplayer()->SetWidth( lw );
934
935   // Disable activation of selection
936   getDisplayer()->SetToActivate( activate );
937
938   // Make a reference to GEOM_Object
939   CORBA::String_var objStr = myGeometryGUI->getApp()->orb()->object_to_string( object );
940   getDisplayer()->SetName( objStr.in() );
941
942   // Create wire from applied object
943   TopoDS_Shape anApplyedWire, aLastSegment;
944   if ( !createShapes( object, anApplyedWire, aLastSegment ) )
945     return;
946
947   // Set color for preview shape
948   getDisplayer()->SetColor( line_color );
949   
950   // Build prs
951   SALOME_Prs* aPrs = getDisplayer()->BuildPrs( anApplyedWire );
952   if ( aPrs != 0 && !aPrs->IsNull() )
953     GEOMBase_Helper::displayPreview( aPrs, append, update );
954
955   getDisplayer()->SetColor( Quantity_NOC_VIOLET );
956   aPrs = getDisplayer()->BuildPrs( aLastSegment );
957     
958   if ( aPrs != 0 && !aPrs->IsNull() )
959     GEOMBase_Helper::displayPreview( aPrs, append, update );
960   
961   getDisplayer()->SetColor( line_color );
962
963   // Display trihedron
964 //   if(GroupType->RadioButton3->isChecked())
965 //     displayTrihedron(3);
966 //   else
967   displayTrihedron(2);
968   
969   getDisplayer()->UnsetName();
970
971   // Enable activation of displayed objects
972   getDisplayer()->SetToActivate( true );
973 }
974
975 //================================================================
976 // Function : displayTrihedron()
977 // Purpose  : Method for displaying trihedron
978 //================================================================
979 void EntityGUI_3DSketcherDlg::displayTrihedron(int selMode)
980 {
981   // Add trihedron to preview
982   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
983   
984   gp_Pnt P(getLastPoint().x,getLastPoint().y,getLastPoint().z);
985   Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(P,gp::DZ(),gp::DX());
986   Handle(AIS_Trihedron) anIO = new AIS_Trihedron(anAxis);
987   anIO->SetSelectionMode(selMode);
988   
989   SOCC_Prs* aSPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
990   
991   if (aSPrs)
992   {
993     aSPrs->PrependObject(anIO);
994     GEOMBase_Helper::displayPreview( aSPrs, true, true );
995 //     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->LocalSelection(aSPrs, selMode);
996   }
997 }
998
999 //================================================================
1000 // Function : displayAngle()
1001 // Purpose  : Method for displaying angle dimensions
1002 //================================================================
1003 void EntityGUI_3DSketcherDlg::displayAngle(double theAngle1, double theAngle2, double theLength, int theOrientation, bool store)
1004 {
1005   if(Abs(theAngle2 - 90.0) < Precision::Angular())
1006     return;
1007   
1008   // Display length dimensions
1009   displayLength(theLength, store);
1010
1011   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
1012   
1013   XYZ Last    = getLastPoint();
1014   XYZ Current = getCurrentPoint();
1015     
1016   gp_Pnt Last_Pnt(Last.x,Last.y,Last.z);
1017   gp_Pnt Current_Pnt(Current.x,Current.y,Current.z);
1018   gp_Pnt P1, P2; //, P3;
1019   
1020   bool twoAngles = GroupAngles->checkBox->isChecked();
1021   
1022   switch(theOrientation)
1023   {
1024     case 1: //OXY
1025     {   
1026       P1 = gp_Pnt(Last.x + theLength,Last.y,Last.z);    // X direction
1027       P2 = gp_Pnt(Last.x + theLength * cos(theAngle1 * M_PI / 180.),
1028                   Last.y + theLength * sin(theAngle1 * M_PI / 180.),
1029                   Last.z); 
1030 //       P3 = gp_Pnt(Last.x,Last.y,theLength);     // Z direction (normal to the plane)
1031       break;
1032     }
1033     case 2: //OYZ
1034     {
1035       P1 = gp_Pnt(Last.x, Last.y + theLength,Last.z);     // Y direction
1036       P2 = gp_Pnt(Last.x, 
1037                   Last.y + theLength * cos(theAngle1 * M_PI / 180.),
1038                   Last.z + theLength * sin(theAngle1 * M_PI / 180.)); 
1039 //       P2 = gp_Pnt(Last.x,Current.y,Current.z);  // Projection in OYZ plane
1040 //       P3 = gp_Pnt(theLength,Last.y,Last.z);     // X direction
1041       break;
1042     }
1043     case 3: //OXZ
1044     {
1045       P1 = gp_Pnt( Last.x + theLength,Last.y,Last.z);     // X direction
1046       P2 = gp_Pnt( Last.x + theLength * cos(theAngle1 * M_PI / 180.) ,
1047                    Last.y,
1048                    Last.z + theLength * sin(theAngle1 * M_PI / 180.)); 
1049 //       P2 = gp_Pnt(Current.x,Last.y,Current.z);  // Projection in OXZ plane
1050 //       P3 = gp_Pnt(Last.x,theLength,Last.z);     // Y direction
1051       break;
1052     }
1053   }
1054   
1055   TopoDS_Vertex V1    = BRepBuilderAPI_MakeVertex(P1);
1056   TopoDS_Vertex V2    = BRepBuilderAPI_MakeVertex(P2);
1057   TopoDS_Vertex LastV = BRepBuilderAPI_MakeVertex(Last_Pnt);
1058   TopoDS_Vertex CurV  = BRepBuilderAPI_MakeVertex(Current_Pnt);
1059   TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge(LastV, V1);
1060   TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(LastV, V2);
1061   TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge(LastV, CurV);
1062   
1063   gce_MakePln gce_MP(Last_Pnt, P1, P2);
1064         Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
1065       
1066   // Covert angles to string
1067   std::string Angle1_str = doubleToString(theAngle1);
1068   std::string Angle2_str = doubleToString(theAngle2);
1069   
1070   Handle(AIS_AngleDimension) anAngleIO  = new AIS_AngleDimension(anEdge1, anEdge2, aPlane, theAngle1 * M_PI / 180.,
1071            TCollection_ExtendedString(Angle1_str.c_str()));
1072   anAngleIO->SetArrowSize( (theAngle1 * M_PI / 180) * (theLength/20) );
1073   
1074   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1075   int w = resMgr->integerValue("Geometry", "measures_line_width", 1);
1076   
1077   Handle(Prs3d_AngleAspect) asp = new Prs3d_AngleAspect();
1078   asp->LineAspect()->SetWidth(w);
1079   anAngleIO->Attributes()->SetAngleAspect(asp);
1080   //TEST
1081   //anAngleIO->SetSelectionMode(1);
1082   
1083   SOCC_Prs* aSPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
1084   
1085   if (store)
1086   {
1087     myAnglePrs->PrependObject(anAngleIO);
1088     myPrsType = TYPE_ANGLE;  // Overwrite type with ANGLE
1089   }
1090   else if(aSPrs)
1091   {
1092     aSPrs->AddObject(anAngleIO);
1093   }
1094   
1095   if (twoAngles)
1096   {
1097     gce_MakePln gce_MP2(Last_Pnt, P2, Current_Pnt);
1098     Handle(Geom_Plane) aPlane2 = new Geom_Plane(gce_MP2.Value());
1099           
1100     Handle(AIS_AngleDimension) anAngle2IO = new AIS_AngleDimension(anEdge2, anEdge3, aPlane2, theAngle2 * M_PI / 180.,
1101             TCollection_ExtendedString(Angle2_str.c_str()));
1102     anAngle2IO->SetArrowSize( (theAngle2 * M_PI / 180) * (theLength/20) );
1103     
1104     anAngle2IO->Attributes()->SetAngleAspect(asp);
1105     
1106     if (store)
1107     {
1108       myAnglePrs->PrependObject(anAngle2IO);
1109       myPrsType = TYPE_TWO_ANGLES;   // Overwrite type with TWO_ANGLES
1110     }
1111     else if (aSPrs)
1112     {
1113       aSPrs->AddObject(anAngle2IO);   
1114     }
1115   }
1116   
1117   if(!store && isAngleVisible)
1118   {       
1119     GEOMBase_Helper::displayPreview( aSPrs, true, true ); 
1120   }
1121 }
1122
1123 //================================================================
1124 // Function : displayLength()
1125 // Purpose  : Method for displaying length dimensions
1126 //================================================================
1127 void EntityGUI_3DSketcherDlg::displayLength(double theLength, bool store)
1128 {
1129   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
1130   
1131   XYZ Last    = getLastPoint();
1132   XYZ Current = getCurrentPoint();
1133   XYZ Penultimate = getPenultimatePoint();
1134   
1135   double aLength = 0.0;
1136   
1137   if( theLength < 0)
1138   {
1139     aLength = sqrt((Last.x - Current.x)*(Last.x - Current.x) +
1140                    (Last.y - Current.y)*(Last.y - Current.y) +  
1141                    (Last.z - Current.z)*(Last.z - Current.z));
1142   }
1143   else
1144     aLength = theLength;
1145   
1146   if (aLength<Precision::Confusion())
1147     return;
1148   
1149   gp_Pnt P1 = gp_Pnt(Last.x,Last.y,Last.z);
1150   gp_Pnt P2 = gp_Pnt(Current.x,Current.y,Current.z);
1151   gp_Pnt P3 = gp_Pnt(Penultimate.x,Penultimate.y,Penultimate.z);
1152   
1153   gp_Vec Vec1(P1,P2);
1154   gp_Vec Vec2(P1,P3);
1155   
1156   TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex(P1);
1157   TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex(P2);
1158   
1159   // Convert length to string
1160   std::string aLength_str = doubleToString(aLength);
1161   
1162   // Plane for the presentation
1163   if ( Abs(Vec1.CrossMagnitude(Vec2)) < Precision::Confusion() ) // Check colinearity
1164   {
1165     Vec2 = gp_Vec(gp::DX());
1166     if ( Abs(Vec1.CrossMagnitude(Vec2)) < Precision::Confusion())
1167     {
1168       Vec2 = gp_Vec(gp::DY());
1169     }
1170   }
1171   
1172   // Normal to the representation plane
1173   gp_Dir Normal(Vec2.Crossed(Vec1));
1174   
1175 //   // Check orientation of plane in order to avoid it to change during an Angle preview
1176 //   MESSAGE("repere0  abs = "<< Abs(Normal.Dot(gp::DZ()) ))
1177 //   if ( Abs(Normal.Dot(gp::DZ())) > Precision::Confusion() )
1178 //   {
1179 //     MESSAGE("repere1")
1180 //     if ( Normal.Dot(gp::DZ()) < 0.0 ){
1181 //       MESSAGE("REVERSE on OZ basis")
1182 //       Normal.Reverse();
1183 //     }
1184 //   }
1185 //   else if ( Abs(Normal.Dot(gp::DX())) > Precision::Confusion() )
1186 //   {
1187 //     MESSAGE("repere2")
1188 //     if ( Normal.Dot(gp::DX()) < 0.0 ){
1189 //       MESSAGE("REVERSE on OX basis")
1190 //       Normal.Reverse();
1191 //     }
1192 //   }
1193 //   else if ( Abs(Normal.Dot(gp::DY())) > Precision::Confusion() )
1194 //   {
1195 //     MESSAGE("repere3")
1196 //     if ( Normal.Dot(gp::DY()) < 0.0 ){
1197 //       MESSAGE("REVERSE on OY basis")
1198 //       Normal.Reverse();
1199 //     }
1200 //   }
1201   
1202   gce_MakePln gce_MP(P1, Normal);
1203   Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
1204   
1205   Handle(AIS_LengthDimension) anIO = new AIS_LengthDimension(aVert1, 
1206                                                              aVert2, 
1207                                                              aPlane, 
1208                                                              aLength, 
1209                                                              TCollection_ExtendedString(aLength_str.c_str()));
1210   anIO->SetArrowSize(aLength/20);
1211       
1212   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1213   int w = resMgr->integerValue("Geometry", "measures_line_width", 1);
1214   Handle(Prs3d_LengthAspect) asp = new Prs3d_LengthAspect();
1215   asp->LineAspect()->SetWidth(w);
1216   anIO->Attributes()->SetLengthAspect(asp);
1217    
1218   if (store)
1219   {
1220     myLengthPrs->PrependObject(anIO);
1221     myPrsType = TYPE_LENGTH;
1222   }
1223   else if( isLengthVisible )
1224   {
1225     SOCC_Prs* aSPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
1226     if (aSPrs)
1227     {
1228       aSPrs->PrependObject(anIO);
1229       GEOMBase_Helper::displayPreview( aSPrs, true, true );
1230     }
1231   }
1232 }
1233
1234 //================================================================
1235 // Function : createShapes
1236 // Purpose  : Create applyed wire, and last segment from entry object
1237 //================================================================
1238 bool EntityGUI_3DSketcherDlg::createShapes( GEOM::GEOM_Object_ptr /*theObject*/,
1239                                             TopoDS_Shape&         theApplyedWire,
1240                                             TopoDS_Shape&         theLastSegment )
1241 {
1242   QList<gp_Pnt> points;
1243   foreach( XYZ xyz, myPointsList) {
1244     gp_Pnt p(xyz.x, xyz.y, xyz.z);
1245     if ( points.isEmpty() || points.last().Distance(p) > gp::Resolution())
1246       points << p;
1247   } 
1248   
1249   if ( points.count() == 1 ) {
1250     // only one point is created
1251     BRepBuilderAPI_MakeVertex mkVertex (points.last());
1252     theApplyedWire = mkVertex.Shape();
1253   }
1254   else if ( points.count() > 1 ) {
1255     // wire is created
1256     BRepBuilderAPI_MakePolygon mkWire;
1257     foreach( gp_Pnt p, points )
1258       mkWire.Add(p);
1259     theApplyedWire = mkWire.Shape();
1260   }
1261
1262   XYZ curxyz = getCurrentPoint();
1263   gp_Pnt curpnt(curxyz.x, curxyz.y, curxyz.z);
1264   
1265   if ( points.isEmpty() || points.last().Distance(curpnt) <= gp::Resolution() ) {
1266     BRepBuilderAPI_MakeVertex mkVertex (curpnt);
1267     theLastSegment = mkVertex.Shape();
1268   }
1269   else {
1270     BRepBuilderAPI_MakeEdge mkEdge(points.last(), curpnt);
1271     theLastSegment = mkEdge.Shape();
1272   }
1273
1274   /* VSR: old algorithm does not work properly, see bug 0020899
1275   TopoDS_Shape aShape;
1276   if ( !GEOMBase::GetShape( theObject, aShape ) )
1277     return false;
1278
1279   if( aShape.ShapeType() != TopAbs_WIRE && aShape.ShapeType() != TopAbs_VERTEX )
1280     return false;
1281
1282   theApplyedWire = aShape;
1283   if ( myOK  )
1284      return true;
1285
1286   BRepBuilderAPI_MakeWire aBuilder;
1287   TopExp_Explorer edgeExp( aShape, TopAbs_EDGE );
1288   while ( 1 ) {
1289     TopoDS_Shape anEdge = edgeExp.Current();
1290     edgeExp.Next();
1291     if ( edgeExp.More() ) // i.e. non-last edge
1292       aBuilder.Add( TopoDS::Edge( anEdge ) );
1293     else {
1294       theLastSegment = anEdge;
1295       break;
1296     }
1297   }
1298
1299   if ( aBuilder.IsDone() ) {
1300     theApplyedWire = aBuilder.Shape();
1301   }
1302   else if ( !theLastSegment.IsNull() ) {
1303     TopExp_Explorer vertexExp( theLastSegment, TopAbs_VERTEX );
1304     theApplyedWire = vertexExp.Current();
1305     }
1306   */
1307
1308   return true;
1309 }
1310
1311 //================================================================
1312 // Function : doubleToString
1313 // Purpose  : converts double to string
1314 //================================================================
1315 std::string EntityGUI_3DSketcherDlg::doubleToString(double num)
1316 {
1317   // truncate num
1318   int digNum = 5;
1319   char format = 'g'; // truncated to a number of significant digits
1320   
1321   return QString::number( num, format, digNum).toStdString();
1322 }