]> SALOME platform Git repositories - modules/geom.git/blob - src/EntityGUI/EntityGUI_3DSketcherDlg.cxx
Salome HOME
Merge from V6_main_20120808 08Aug12
[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   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
340 }
341
342 //=================================================================================
343 // function : ClickOnAddPoint()
344 // purpose  : called when the point coordinates is Applyed
345 //=================================================================================
346 void EntityGUI_3DSketcherDlg::ClickOnAddPoint()
347 {
348   QString msg;
349   if ( !isValid( msg ) ) {
350     showError( msg );
351     return;
352   }
353   
354   // Display and store angle dimensions interactive objects in Prs
355   if( GroupType->RadioButton3->isChecked() )
356   {
357     double anAngle2 = 0.0;
358     if (GroupAngles->checkBox->isChecked())
359       anAngle2 = GroupAngles->SpinBox_DA2->value();
360     
361     // Store length dimensions
362     displayLength(GroupAngles->SpinBox_DL->value(), /*store =*/true);
363     // Store angle dimensions
364     displayAngle(GroupAngles->SpinBox_DA->value(), anAngle2, 
365                  GroupAngles->SpinBox_DL->value(), myOrientation,  /*store =*/true);  
366   }
367   
368   // Display and store store length dimension interactive object in Prs
369   if (GroupType->RadioButton1->isChecked() ||
370       GroupType->RadioButton2->isChecked())
371   {
372     displayLength(-1, /*store=*/true);
373   }
374   
375   myPointsList.append( getCurrentPoint() );
376   myPrsTypeList.push_back( myPrsType );
377   
378   // Clean redo lists
379   myRedoList.clear();
380   myPrsTypeRedoList.clear();
381   myLengthIORedoList.Clear();
382   myAngleIORedoList.Clear();
383   
384   if ( myMode == 1 ) {
385     Group3Spin->SpinBox_DX->setValue( 0.0 );
386     Group3Spin->SpinBox_DY->setValue( 0.0 );
387     Group3Spin->SpinBox_DZ->setValue( 0.0 );
388   }
389   else if ( myMode == 2 )
390   {
391     GroupAngles->SpinBox_DA->setValue( 0.0 );
392     GroupAngles->SpinBox_DL->setValue( 0.0 );
393     GroupAngles->SpinBox_DA2->setValue( 0.0 );
394   }
395   UpdateButtonsState();
396   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
397   
398 }
399
400 //=================================================================================
401 // function : UpdateButtonsState()
402 // purpose  : 
403 //=================================================================================
404 void EntityGUI_3DSketcherDlg::UpdateButtonsState()
405 {
406   if ( myPointsList.count() == 0 ) GroupType->RadioButton1->click();
407   GroupType->RadioButton2->setEnabled( myPointsList.count() > 0 );
408   GroupType->RadioButton3->setEnabled( myPointsList.count() > 0 );
409   Group3Spin->buttonUndo->setEnabled( myPointsList.count() > 0 );
410   Group3Spin->buttonRedo->setEnabled( myRedoList.count() > 0 );
411   GroupAngles->buttonUndo->setEnabled( myPointsList.count() > 0 );
412   GroupAngles->buttonRedo->setEnabled( myRedoList.count() > 0 );
413 }
414
415 //=================================================================================
416 // function : ClickOnUndo()
417 // purpose  :
418 //=================================================================================
419 void EntityGUI_3DSketcherDlg::ClickOnUndo()
420 {
421   if (myPointsList.count() > 0) {
422     myRedoList.append( myPointsList.takeLast() );
423     UpdateButtonsState();
424     GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
425     
426     // Erase dimensions presentations
427     SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
428     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myLengthPrs, true); 
429     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myAnglePrs, true);
430     
431     if (myPrsTypeList.back() != NONE)
432     {
433       // Remove last prepended IO
434       removeLastIOFromPrs( TYPE_LENGTH );     
435     }   
436     if ( myPrsTypeList.back() == TYPE_ANGLE ||
437          myPrsTypeList.back() == TYPE_TWO_ANGLES )
438     {
439       // Remove first Angle IO from presentation
440       removeLastIOFromPrs( TYPE_ANGLE );   
441       if ( myPrsTypeList.back() == TYPE_TWO_ANGLES )
442       {
443         // Remove second  Angle IO
444         removeLastIOFromPrs( TYPE_ANGLE );   
445       }
446     }
447        
448     // Erase last action type and store it in redo list
449     myPrsTypeRedoList.push_back(myPrsTypeList.back());
450     myPrsTypeList.pop_back();
451     
452     // Display modified presentation
453     if (isLengthVisible)
454       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myLengthPrs);
455     if (isAngleVisible)
456       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myAnglePrs);
457     
458     updateViewer();
459   }
460 }
461
462 //=================================================================================
463 // function : ClickOnRedo()
464 // purpose  :
465 //=================================================================================
466 void EntityGUI_3DSketcherDlg::ClickOnRedo()
467 {
468   if ( myRedoList.count() > 0) {
469     myPointsList.append( myRedoList.takeLast() );
470     UpdateButtonsState();
471     GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
472       
473     // Erase dimensions presentations
474     SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
475     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myLengthPrs, true);
476     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myAnglePrs, true);
477     
478     if ( myPrsTypeRedoList.back() != NONE )
479       restoreLastIOToPrs( TYPE_LENGTH ); 
480     
481     if ( myPrsTypeRedoList.back() == TYPE_ANGLE || 
482          myPrsTypeRedoList.back() == TYPE_TWO_ANGLES )
483     {
484       // Add a first IO from the Redo list
485       restoreLastIOToPrs( TYPE_ANGLE ); 
486       if ( myPrsTypeRedoList.back() == TYPE_TWO_ANGLES )
487       {
488         // Add a second IO from the Redo list
489         restoreLastIOToPrs( TYPE_ANGLE ); 
490       }
491     }
492     
493     // Record last prs type
494     myPrsTypeList.push_back(myPrsTypeRedoList.back());
495     myPrsTypeRedoList.pop_back();
496     
497     // Display modified presentation
498     if (isLengthVisible)
499       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myLengthPrs);
500     if (isAngleVisible)
501       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myAnglePrs);
502     
503     updateViewer();
504   }
505 }
506
507 //=================================================================================
508 // function : removeLastIO()
509 // purpose  :
510 //=================================================================================
511 void EntityGUI_3DSketcherDlg::removeLastIOFromPrs(int type)
512 {
513   AIS_ListOfInteractive anIOList;
514   
515   if (type == TYPE_LENGTH)
516   {
517     myLengthPrs->GetObjects(anIOList);
518     myLengthIORedoList.Prepend(anIOList.First());  // Store last prepended Length IO in redo list
519     myLengthPrs->RemoveFirst();                    // Remove it from myLengthPrs
520   }   
521   if (type == TYPE_ANGLE)
522   {
523     myAnglePrs->GetObjects(anIOList);
524     myAngleIORedoList.Prepend(anIOList.First());  // Store last prepended Angle IO in redo list
525     myAnglePrs->RemoveFirst();                    // Remove it from myAnglePrs
526   }   
527 }
528
529 //=================================================================================
530 // function : restoreLastIO()
531 // purpose  :
532 //=================================================================================
533 void EntityGUI_3DSketcherDlg::restoreLastIOToPrs(int type)
534 {
535   if (type == TYPE_LENGTH)
536   {
537     myLengthPrs->PrependObject(myLengthIORedoList.First()); // Restore last removed IO
538     myLengthIORedoList.RemoveFirst();                       // Remove it from redo list
539   }   
540   if (type == TYPE_ANGLE)
541   {
542     myAnglePrs->PrependObject(myAngleIORedoList.First());  // Restore last removed IO
543     myAngleIORedoList.RemoveFirst();                       // Remove it from redo list
544   }   
545 }
546
547 //=================================================================================
548 // function : SelectionIntoArgument()
549 // purpose  : Called when selection as changed
550 //=================================================================================
551 void EntityGUI_3DSketcherDlg::SelectionIntoArgument()
552 {
553   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
554   SALOME_ListIO aSelList;
555   aSelMgr->selectedObjects(aSelList);
556
557   int nbSel = aSelList.Extent();
558   if ( nbSel == 1 ) {
559     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
560     if ( !CORBA::is_nil(aSelectedObject) ) {
561       TopoDS_Shape aShape;
562       if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE)) { // Explore the shape if its a local selection
563         TColStd_IndexedMapOfInteger aMap;
564         aSelMgr->GetIndexes(aSelList.First(), aMap);
565         if (aMap.Extent() == 1)
566           {
567             int anIndex = aMap(1);
568             GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
569             aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
570           }
571       }
572       bool isOk = true;
573       if ( aShape.ShapeType() != TopAbs_VERTEX )
574         isOk = GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_VERTEX);
575       if (isOk) {
576         gp_Pnt aPnt;
577         if ( GEOMBase::VertexToPoint( aShape, aPnt ) ) {
578           // set coordinates to the Spin Boxes
579           double aX, aY, aZ;
580           aX = aPnt.X();
581           aY = aPnt.Y();
582           aZ = aPnt.Z();
583           bool blocked = Group3Spin->SpinBox_DX->signalsBlocked();
584           Group3Spin->SpinBox_DX->blockSignals(true);
585           Group3Spin->SpinBox_DY->blockSignals(true);
586           Group3Spin->SpinBox_DZ->blockSignals(true);
587           if ( GroupType->RadioButton1->isChecked() ) {
588             Group3Spin->SpinBox_DX->setValue( aX );
589             Group3Spin->SpinBox_DY->setValue( aY );
590             Group3Spin->SpinBox_DZ->setValue( aZ );
591           } else if ( GroupType->RadioButton2->isChecked() ) {
592             XYZ xyz = getLastPoint();
593             Group3Spin->SpinBox_DX->setValue( aX - xyz.x );
594             Group3Spin->SpinBox_DY->setValue( aY - xyz.y );
595             Group3Spin->SpinBox_DZ->setValue( aZ - xyz.z );
596           }
597           Group3Spin->SpinBox_DX->blockSignals(blocked);
598           Group3Spin->SpinBox_DY->blockSignals(blocked);
599           Group3Spin->SpinBox_DZ->blockSignals(blocked);
600         }
601       }
602     }
603   }
604   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
605 }
606
607 //=================================================================================
608 // function : DeactivateActiveDialog()
609 // purpose  :
610 //=================================================================================
611 void EntityGUI_3DSketcherDlg::DeactivateActiveDialog()
612 {
613   setEnabled( false );
614   globalSelection();
615   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
616   myGeomGUI->SetActiveDialogBox( 0 );
617 }
618
619
620 //=================================================================================
621 // function : ActivateThisDialog()
622 // purpose  :
623 //=================================================================================
624 void EntityGUI_3DSketcherDlg::ActivateThisDialog()
625 {
626   myGeomGUI->EmitSignalDeactivateDialog();
627   setEnabled( true );
628   myGeomGUI->SetActiveDialogBox( this );
629
630   connect( myGeomGUI->getApp()->selectionMgr(),
631           SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
632
633   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
634   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
635 }
636
637
638 //=================================================================================
639 // function : ValueChangedInSpinBox()
640 // purpose  :
641 //=================================================================================
642 void EntityGUI_3DSketcherDlg::ValueChangedInSpinBox( double newValue )
643 {
644   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
645 }
646
647
648 //=================================================================================
649 // function : BoxChecked()
650 // purpose  : ChecBoxes management
651 //=================================================================================
652 void EntityGUI_3DSketcherDlg::BoxChecked( bool checked )
653 {
654   QCheckBox* send = (QCheckBox*) sender();
655   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
656   
657   if (send == GroupAngles->checkBox)
658     GroupAngles->SpinBox_DA2->setEnabled(checked); 
659   
660   else if (send == GroupControls->CheckBox1)
661   {
662     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myLengthPrs, true);
663     if(checked){
664       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myLengthPrs);
665       isLengthVisible=true;
666     }
667     else
668       isLengthVisible=false;
669   }
670   else if (send == GroupControls->CheckBox2)
671   {
672     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myAnglePrs, true); 
673     if(checked)
674     {
675       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myAnglePrs);
676       isAngleVisible=true;
677     }
678     else
679       isAngleVisible=false;
680   }
681
682   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
683 }
684
685 //=================================================================================
686 // function : ButtonClicked()
687 // purpose  :
688 //=================================================================================
689 void EntityGUI_3DSketcherDlg::ButtonClicked( bool checked )
690 {
691   if (GroupAngles->radioButton_1->isChecked())
692     myOrientation = 1;
693   else if (GroupAngles->radioButton_2->isChecked())
694     myOrientation = 2;
695   else
696     myOrientation = 3; 
697   
698   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
699 }
700
701 //=================================================================================
702 // function : enterEvent()
703 // purpose  :
704 //=================================================================================
705 void EntityGUI_3DSketcherDlg::enterEvent( QEvent* )
706 {
707   if ( !mainFrame()->GroupConstructors->isEnabled() )
708     ActivateThisDialog();
709 }
710
711 //=================================================================================
712 // function : createOperation
713 // purpose  :
714 //=================================================================================
715 GEOM::GEOM_IOperations_ptr EntityGUI_3DSketcherDlg::createOperation()
716 {
717   return getGeomEngine()->GetICurvesOperations( getStudyId() );
718 }
719
720 //=================================================================================
721 // function : isValid
722 // purpose  :
723 //=================================================================================
724 bool EntityGUI_3DSketcherDlg::isValid( QString& msg )
725 {
726   bool ok = true;
727   ok = Group3Spin->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
728   ok = Group3Spin->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
729   ok = Group3Spin->SpinBox_DZ->isValid( msg, !IsPreview() ) && ok;
730   return ok;
731 }
732
733 //=================================================================================
734 // function : execute
735 // purpose  :
736 //=================================================================================
737 bool EntityGUI_3DSketcherDlg::execute( ObjectList& objects )
738 {
739   GEOM::ListOfDouble_var aCoordsArray = new GEOM::ListOfDouble;
740   if (!myOK || myPointsList.size() == 0)
741     aCoordsArray->length((myPointsList.size()+1)*3);
742   else
743     aCoordsArray->length(myPointsList.size()*3);
744
745   QStringList aParameters;
746
747   int i = 0;
748   QList<XYZ>::const_iterator it;
749   for(it = myPointsList.begin(); it != myPointsList.end(); ++it ) {
750     aCoordsArray[i++] = (*it).x;
751     aCoordsArray[i++] = (*it).y;
752     aCoordsArray[i++] = (*it).z;
753     aParameters << (*it).xt;
754     aParameters << (*it).yt;
755     aParameters << (*it).zt;
756   }
757
758   if (!myOK || myPointsList.size() == 0) {
759     XYZ xyz = getCurrentPoint();
760     aCoordsArray[i++] = xyz.x;
761     aCoordsArray[i++] = xyz.y;
762     aCoordsArray[i++] = xyz.z;
763     aParameters << xyz.xt;
764     aParameters << xyz.yt;
765     aParameters << xyz.zt;
766   } 
767
768   GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow(getOperation());
769   GEOM::GEOM_Object_var anObj = anOper->Make3DSketcher( aCoordsArray );
770
771   if ( !anObj->_is_nil() ) {
772     if ( !IsPreview() ) anObj->SetParameters(aParameters.join(":").toLatin1().constData());
773     objects.push_back( anObj._retn() );
774   }
775
776   return true;
777 }
778
779 //=================================================================================
780 // function : SetDoubleSpinBoxStep()
781 // purpose  : Double spin box management
782 //=================================================================================
783 void EntityGUI_3DSketcherDlg::SetDoubleSpinBoxStep( double step )
784 {
785   Group3Spin->SpinBox_DX->setSingleStep(step);
786   Group3Spin->SpinBox_DY->setSingleStep(step);
787   Group3Spin->SpinBox_DZ->setSingleStep(step);
788   GroupAngles->SpinBox_DA->setSingleStep(step);
789   GroupAngles->SpinBox_DL->setSingleStep(step);
790   GroupAngles->SpinBox_DA2->setSingleStep(step);
791 }
792
793 //=================================================================================
794 // function : ClickOnOk()
795 // purpose  :
796 //=================================================================================
797 void EntityGUI_3DSketcherDlg::ClickOnOk()
798 {
799   Locker lock( myOK );
800
801   if ( !onAccept() )
802     return;
803
804   ClickOnCancel();
805 }
806
807 //=================================================================================
808 // function : ClickOnApply()
809 // purpose  :
810 //=================================================================================
811 bool EntityGUI_3DSketcherDlg::ClickOnApply()
812 {
813   QString msg;
814   if ( !isValid( msg ) ) {
815     showError( msg );
816     return false;
817   }
818
819   if (myPointsList.count() > 0)
820     myPointsList.append(myPointsList[0]);
821
822   Locker lock( myOK );
823
824   if ( !onAccept() )
825     return false;
826   
827   ClickOnCancel();
828   return true;
829 }
830
831 //=================================================================================
832 // function : getLastPoint()
833 // purpose  : return last points from list
834 //=================================================================================
835 EntityGUI_3DSketcherDlg::XYZ EntityGUI_3DSketcherDlg::getLastPoint() const
836 {
837   return myPointsList.count() > 0 ? myPointsList.last() : XYZ();
838 }
839
840 //=================================================================================
841 // function : getPenultimatePoint()
842 // purpose  : return penultimate point from list
843 //=================================================================================
844 EntityGUI_3DSketcherDlg::XYZ EntityGUI_3DSketcherDlg::getPenultimatePoint() const
845 {
846   double size = myPointsList.count();
847   return size > 1 ? myPointsList.at(size - 2) : XYZ();
848 }
849
850 //=================================================================================
851 // function : getCurrentPoint()
852 // purpose  : returns current points
853 //=================================================================================
854 EntityGUI_3DSketcherDlg::XYZ EntityGUI_3DSketcherDlg::getCurrentPoint() const
855 {
856   XYZ xyz; 
857   // Temporary way of doing this. To be changed with ordered improvement of the sketcher
858   if ( myMode == 2 )
859   {
860     double anAngle  = GroupAngles->SpinBox_DA->value() * M_PI/180.0;
861     double anAngle2 = 0.0;
862     double aLength  = GroupAngles->SpinBox_DL->value();
863     
864     if (GroupAngles->checkBox->isChecked())
865       anAngle2 = GroupAngles->SpinBox_DA2->value() * M_PI/180.0;
866     
867     double aProjectedLength = aLength * cos(anAngle2);
868     
869     xyz = getLastPoint();
870     if (GroupAngles->radioButton_1->isChecked())
871     {
872       xyz.x += aProjectedLength * cos(anAngle);
873       xyz.y += aProjectedLength * sin(anAngle);
874       xyz.z += aLength * sin(anAngle2);
875     }
876     else if (GroupAngles->radioButton_2->isChecked())
877     {
878       xyz.y += aProjectedLength * cos(anAngle);
879       xyz.z += aProjectedLength * sin(anAngle);
880       xyz.x += aLength * sin(anAngle2);
881     }
882     else
883     {
884       xyz.z += aProjectedLength * sin(anAngle);
885       xyz.x += aProjectedLength * cos(anAngle);
886       xyz.y += aLength * sin(anAngle2);
887     }
888   }
889   else
890   {
891     if ( myMode == 0 ) {
892       xyz.x = Group3Spin->SpinBox_DX->value();
893       xyz.y = Group3Spin->SpinBox_DY->value();
894       xyz.z = Group3Spin->SpinBox_DZ->value();
895     } 
896     else{
897       xyz = getLastPoint();
898       xyz.x += Group3Spin->SpinBox_DX->value();
899       xyz.y += Group3Spin->SpinBox_DY->value();
900       xyz.z += Group3Spin->SpinBox_DZ->value();
901     }
902     xyz.xt = Group3Spin->SpinBox_DX->text();
903     xyz.yt = Group3Spin->SpinBox_DY->text();
904     xyz.zt = Group3Spin->SpinBox_DZ->text();
905   }
906   return xyz;
907 }
908
909 //================================================================
910 // Function : displayPreview
911 // Purpose  : Method for displaying preview of resulting shape
912 //            Redefined from GEOMBase_Helper.
913 //================================================================
914 void EntityGUI_3DSketcherDlg::displayPreview( GEOM::GEOM_Object_ptr object,
915                                               const bool            append,
916                                               const bool            activate,
917                                               const bool            update,
918                                               const double          lineWidth,
919                                               const int             displayMode,
920                                               const int             color )
921
922   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr(); 
923   
924   QColor aColor = resMgr->colorValue("Geometry","line_color",QColor(255,0,0));
925   Quantity_NameOfColor line_color = SalomeApp_Tools::color( aColor ).Name();
926   
927   // set width of displayed shape
928   int lw = lineWidth;
929   if(lw == -1) { 
930     lw = resMgr->integerValue("Geometry", "preview_edge_width", -1);
931   }
932   getDisplayer()->SetWidth( lw );
933
934   // Disable activation of selection
935   getDisplayer()->SetToActivate( activate );
936
937   // Make a reference to GEOM_Object
938   CORBA::String_var objStr = myGeometryGUI->getApp()->orb()->object_to_string( object );
939   getDisplayer()->SetName( objStr.in() );
940
941   // Create wire from applied object
942   TopoDS_Shape anApplyedWire, aLastSegment;
943   if ( !createShapes( object, anApplyedWire, aLastSegment ) )
944     return;
945
946   // Set color for preview shape
947   getDisplayer()->SetColor( line_color );
948   
949   // Build prs
950   SALOME_Prs* aPrs = getDisplayer()->BuildPrs( anApplyedWire );
951   if ( aPrs != 0 && !aPrs->IsNull() )
952     GEOMBase_Helper::displayPreview( aPrs, append, update );
953
954   getDisplayer()->SetColor( Quantity_NOC_VIOLET );
955   aPrs = getDisplayer()->BuildPrs( aLastSegment );
956     
957   if ( aPrs != 0 && !aPrs->IsNull() )
958     GEOMBase_Helper::displayPreview( aPrs, append, update );
959   
960   getDisplayer()->SetColor( line_color );
961   
962   // Display local trihedron if the mode is relatives coordinates or angles
963   if (myMode == 1 || myMode == 2)
964     displayTrihedron(2);
965   
966   // Display preview of suitable dimension presentations
967   if(myMode == 2)  // ANGLES
968   {
969     double anAngle2 = 0.0;
970     if (GroupAngles->checkBox->isChecked())
971       anAngle2 = GroupAngles->SpinBox_DA2->value();
972     
973     displayAngle(GroupAngles->SpinBox_DA->value(), anAngle2, GroupAngles->SpinBox_DL->value(), myOrientation);
974     displayLength(GroupAngles->SpinBox_DL->value());  
975   }
976   if( myMode == 0 || myMode == 1 )  // COORDINATES
977     displayLength(); 
978   
979   getDisplayer()->UnsetName();
980
981   // Enable activation of displayed objects
982   getDisplayer()->SetToActivate( true );
983 }
984
985 //================================================================
986 // Function : displayTrihedron()
987 // Purpose  : Method for displaying trihedron
988 //================================================================
989 void EntityGUI_3DSketcherDlg::displayTrihedron(int selMode)
990 {
991   // Add trihedron to preview
992   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
993   
994   gp_Pnt P(getLastPoint().x,getLastPoint().y,getLastPoint().z);
995   Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(P,gp::DZ(),gp::DX());
996   Handle(AIS_Trihedron) anIO = new AIS_Trihedron(anAxis);
997   anIO->SetSelectionMode(selMode);
998   
999   SOCC_Prs* aSPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
1000   
1001   if (aSPrs)
1002   {
1003     aSPrs->PrependObject(anIO);
1004     GEOMBase_Helper::displayPreview( aSPrs, true, true );
1005   }
1006 }
1007
1008 //================================================================
1009 // Function : displayAngle()
1010 // Purpose  : Method for displaying angle dimensions
1011 //================================================================
1012 void EntityGUI_3DSketcherDlg::displayAngle(double theAngle1, double theAngle2, double theLength, int theOrientation, bool store)
1013 {
1014   if( Abs(theAngle2 - 90.0) < Precision::Angular() || 
1015       theLength < Precision::Confusion() )
1016     return;
1017
1018   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow(); 
1019   
1020   XYZ Last    = getLastPoint();
1021   XYZ Current = getCurrentPoint();
1022     
1023   gp_Pnt Last_Pnt(Last.x,Last.y,Last.z);
1024   gp_Pnt Current_Pnt(Current.x,Current.y,Current.z);
1025   gp_Pnt P1, P2;
1026   
1027   bool twoAngles = GroupAngles->checkBox->isChecked();
1028   
1029   switch(theOrientation)
1030   {
1031     case 1: //OXY
1032     {   
1033       P1 = gp_Pnt(Last.x + theLength,Last.y,Last.z);    // X direction
1034       P2 = gp_Pnt(Last.x + theLength * cos(theAngle1 * M_PI / 180.),
1035                   Last.y + theLength * sin(theAngle1 * M_PI / 180.),
1036                   Last.z); 
1037       break;
1038     }
1039     case 2: //OYZ
1040     {
1041       P1 = gp_Pnt(Last.x, Last.y + theLength,Last.z);     // Y direction
1042       P2 = gp_Pnt(Last.x, 
1043                   Last.y + theLength * cos(theAngle1 * M_PI / 180.),
1044                   Last.z + theLength * sin(theAngle1 * M_PI / 180.)); 
1045       break;
1046     }
1047     case 3: //OXZ
1048     {
1049       P1 = gp_Pnt( Last.x + theLength,Last.y,Last.z);     // X direction
1050       P2 = gp_Pnt( Last.x + theLength * cos(theAngle1 * M_PI / 180.) ,
1051                    Last.y,
1052                    Last.z + theLength * sin(theAngle1 * M_PI / 180.)); 
1053       break;
1054     }
1055   }
1056   
1057   TopoDS_Vertex V1    = BRepBuilderAPI_MakeVertex(P1);
1058   TopoDS_Vertex V2    = BRepBuilderAPI_MakeVertex(P2);
1059   TopoDS_Vertex LastV = BRepBuilderAPI_MakeVertex(Last_Pnt);
1060   TopoDS_Vertex CurV  = BRepBuilderAPI_MakeVertex(Current_Pnt);
1061   TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge(LastV, V1);
1062   TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(LastV, V2);
1063   TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge(LastV, CurV);
1064   
1065   gce_MakePln gce_MP(Last_Pnt, P1, P2);
1066         Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
1067       
1068   // Covert angles to string
1069   std::string Angle1_str = doubleToString(theAngle1);
1070   std::string Angle2_str = doubleToString(theAngle2);
1071   
1072   // Create interactive object
1073   Handle(AIS_AngleDimension) anAngleIO  = new AIS_AngleDimension(anEdge1, anEdge2, aPlane, theAngle1 * M_PI / 180.,
1074            TCollection_ExtendedString(Angle1_str.c_str()));
1075   anAngleIO->SetArrowSize( (theAngle1 * M_PI / 180) * (theLength/20) );
1076   
1077   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1078   int w = resMgr->integerValue("Geometry", "measures_line_width", 1);
1079   
1080   Handle(Prs3d_AngleAspect) asp = new Prs3d_AngleAspect();
1081   asp->LineAspect()->SetWidth(w);
1082   anAngleIO->Attributes()->SetAngleAspect(asp);
1083   
1084   SOCC_Prs* aSPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
1085   
1086   if (store)
1087   {
1088     // Erase dimensions presentations
1089     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myAnglePrs, true);
1090     
1091     myAnglePrs->PrependObject(anAngleIO);
1092     myPrsType = TYPE_ANGLE;  // Overwrite type with ANGLE
1093     
1094     // Display modified presentation
1095     if (isAngleVisible)
1096       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myAnglePrs);
1097   }
1098   else if(aSPrs)
1099   {
1100     aSPrs->AddObject(anAngleIO);
1101   }
1102   
1103   if (twoAngles)
1104   {
1105     gce_MakePln gce_MP2(Last_Pnt, P2, Current_Pnt);
1106     Handle(Geom_Plane) aPlane2 = new Geom_Plane(gce_MP2.Value());
1107           
1108     Handle(AIS_AngleDimension) anAngle2IO = new AIS_AngleDimension(anEdge2, anEdge3, aPlane2, theAngle2 * M_PI / 180.,
1109             TCollection_ExtendedString(Angle2_str.c_str()));
1110     anAngle2IO->SetArrowSize( (theAngle2 * M_PI / 180) * (theLength/20) );
1111     
1112     anAngle2IO->Attributes()->SetAngleAspect(asp);
1113     
1114     if (store)
1115     {
1116       // Erase dimensions presentations
1117       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myAnglePrs, true);
1118       
1119       myAnglePrs->PrependObject(anAngle2IO);
1120       myPrsType = TYPE_TWO_ANGLES;   // Overwrite type with TWO_ANGLES
1121       
1122       // Display modified presentation
1123       if (isAngleVisible)
1124         ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myAnglePrs);
1125     }
1126     else if (aSPrs)
1127     {
1128       aSPrs->AddObject(anAngle2IO);   
1129     }
1130   }
1131   
1132   if(!store && isAngleVisible)
1133   {       
1134     GEOMBase_Helper::displayPreview( aSPrs, true, true ); 
1135   }
1136 }
1137
1138 //================================================================
1139 // Function : displayLength()
1140 // Purpose  : Method for displaying length dimensions
1141 //================================================================
1142 void EntityGUI_3DSketcherDlg::displayLength(double theLength, bool store)
1143 {
1144   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
1145   
1146   XYZ Last    = getLastPoint();
1147   XYZ Current = getCurrentPoint();
1148   XYZ Penultimate = getPenultimatePoint();
1149   
1150   double aLength = 0.0;
1151   
1152   if( theLength < 0)  // Calculate length if not given
1153   {
1154     aLength = sqrt((Last.x - Current.x)*(Last.x - Current.x) +
1155                    (Last.y - Current.y)*(Last.y - Current.y) +  
1156                    (Last.z - Current.z)*(Last.z - Current.z));
1157   }
1158   else
1159     aLength = theLength;
1160   
1161   if (aLength<Precision::Confusion())
1162     return;
1163   
1164   gp_Pnt P1 = gp_Pnt(Last.x,Last.y,Last.z);
1165   gp_Pnt P2 = gp_Pnt(Current.x,Current.y,Current.z);
1166   gp_Pnt P3 = gp_Pnt(Penultimate.x,Penultimate.y,Penultimate.z);
1167   
1168   gp_Vec Vec1(P1,P2);
1169   gp_Vec Vec2(P1,P3);
1170   
1171   TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex(P1);
1172   TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex(P2);
1173   
1174   // Convert length to string
1175   std::string aLength_str = doubleToString(aLength);
1176   
1177   // Plane for the presentation
1178   if ( Abs(Vec1.CrossMagnitude(Vec2)) < Precision::Confusion() ) // Check colinearity
1179   {
1180     Vec2 = gp_Vec(gp::DX());
1181     if ( Abs(Vec1.CrossMagnitude(Vec2)) < Precision::Confusion())
1182     {
1183       Vec2 = gp_Vec(gp::DY());
1184     }
1185   }
1186   
1187   // Normal to the representation plane
1188   gp_Dir Normal(Vec2.Crossed(Vec1));
1189   
1190 //   // Check orientation of plane in order to avoid it to change during an Angle preview
1191 //   MESSAGE("repere0  abs = "<< Abs(Normal.Dot(gp::DZ()) ))
1192 //   if ( Abs(Normal.Dot(gp::DZ())) > Precision::Confusion() )
1193 //   {
1194 //     MESSAGE("repere1")
1195 //     if ( Normal.Dot(gp::DZ()) < 0.0 ){
1196 //       MESSAGE("REVERSE on OZ basis")
1197 //       Normal.Reverse();
1198 //     }
1199 //   }
1200 //   else if ( Abs(Normal.Dot(gp::DX())) > Precision::Confusion() )
1201 //   {
1202 //     MESSAGE("repere2")
1203 //     if ( Normal.Dot(gp::DX()) < 0.0 ){
1204 //       MESSAGE("REVERSE on OX basis")
1205 //       Normal.Reverse();
1206 //     }
1207 //   }
1208 //   else if ( Abs(Normal.Dot(gp::DY())) > Precision::Confusion() )
1209 //   {
1210 //     MESSAGE("repere3")
1211 //     if ( Normal.Dot(gp::DY()) < 0.0 ){
1212 //       MESSAGE("REVERSE on OY basis")
1213 //       Normal.Reverse();
1214 //     }
1215 //   }
1216   
1217   gce_MakePln gce_MP(P1, Normal);
1218   Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
1219   
1220   Handle(AIS_LengthDimension) anIO = new AIS_LengthDimension(aVert1, 
1221                                                              aVert2, 
1222                                                              aPlane, 
1223                                                              aLength, 
1224                                                              TCollection_ExtendedString(aLength_str.c_str()));
1225   anIO->SetArrowSize(aLength/20);
1226       
1227   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1228   int w = resMgr->integerValue("Geometry", "measures_line_width", 1);
1229   Handle(Prs3d_LengthAspect) asp = new Prs3d_LengthAspect();
1230   asp->LineAspect()->SetWidth(w);
1231   anIO->Attributes()->SetLengthAspect(asp);
1232    
1233   if (store)
1234   {
1235     // Erase length dimensions presentation
1236     ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Erase(myLengthPrs, true);
1237     
1238     myLengthPrs->PrependObject(anIO);
1239     myPrsType = TYPE_LENGTH;
1240     
1241     // Display modified presentation
1242     if (isLengthVisible)
1243       ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->Display(myLengthPrs);
1244   }
1245   else if( isLengthVisible )
1246   {
1247     SOCC_Prs* aSPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
1248     if (aSPrs)
1249     {
1250       aSPrs->PrependObject(anIO);
1251       GEOMBase_Helper::displayPreview( aSPrs, true, true );
1252     }
1253   }
1254 }
1255
1256 //================================================================
1257 // Function : createShapes
1258 // Purpose  : Create applyed wire, and last segment from entry object
1259 //================================================================
1260 bool EntityGUI_3DSketcherDlg::createShapes( GEOM::GEOM_Object_ptr /*theObject*/,
1261                                             TopoDS_Shape&         theApplyedWire,
1262                                             TopoDS_Shape&         theLastSegment )
1263 {
1264   QList<gp_Pnt> points;
1265   foreach( XYZ xyz, myPointsList) {
1266     gp_Pnt p(xyz.x, xyz.y, xyz.z);
1267     if ( points.isEmpty() || points.last().Distance(p) > gp::Resolution())
1268       points << p;
1269   } 
1270   
1271   if ( points.count() == 1 ) {
1272     // only one point is created
1273     BRepBuilderAPI_MakeVertex mkVertex (points.last());
1274     theApplyedWire = mkVertex.Shape();
1275   }
1276   else if ( points.count() > 1 ) {
1277     // wire is created
1278     BRepBuilderAPI_MakePolygon mkWire;
1279     foreach( gp_Pnt p, points )
1280       mkWire.Add(p);
1281     theApplyedWire = mkWire.Shape();
1282   }
1283
1284   XYZ curxyz = getCurrentPoint();
1285   gp_Pnt curpnt(curxyz.x, curxyz.y, curxyz.z);
1286   
1287   if ( points.isEmpty() || points.last().Distance(curpnt) <= gp::Resolution() ) {
1288     BRepBuilderAPI_MakeVertex mkVertex (curpnt);
1289     theLastSegment = mkVertex.Shape();
1290   }
1291   else {
1292     BRepBuilderAPI_MakeEdge mkEdge(points.last(), curpnt);
1293     theLastSegment = mkEdge.Shape();
1294   }
1295
1296   /* VSR: old algorithm does not work properly, see bug 0020899
1297   TopoDS_Shape aShape;
1298   if ( !GEOMBase::GetShape( theObject, aShape ) )
1299     return false;
1300
1301   if( aShape.ShapeType() != TopAbs_WIRE && aShape.ShapeType() != TopAbs_VERTEX )
1302     return false;
1303
1304   theApplyedWire = aShape;
1305   if ( myOK  )
1306      return true;
1307
1308   BRepBuilderAPI_MakeWire aBuilder;
1309   TopExp_Explorer edgeExp( aShape, TopAbs_EDGE );
1310   while ( 1 ) {
1311     TopoDS_Shape anEdge = edgeExp.Current();
1312     edgeExp.Next();
1313     if ( edgeExp.More() ) // i.e. non-last edge
1314       aBuilder.Add( TopoDS::Edge( anEdge ) );
1315     else {
1316       theLastSegment = anEdge;
1317       break;
1318     }
1319   }
1320
1321   if ( aBuilder.IsDone() ) {
1322     theApplyedWire = aBuilder.Shape();
1323   }
1324   else if ( !theLastSegment.IsNull() ) {
1325     TopExp_Explorer vertexExp( theLastSegment, TopAbs_VERTEX );
1326     theApplyedWire = vertexExp.Current();
1327     }
1328   */
1329
1330   return true;
1331 }
1332
1333 //================================================================
1334 // Function : doubleToString
1335 // Purpose  : converts double to string
1336 //================================================================
1337 std::string EntityGUI_3DSketcherDlg::doubleToString(double num)
1338 {
1339   // truncate num
1340   int digNum = 5;
1341   char format = 'g'; // truncated to a number of significant digits
1342   
1343   return QString::number( num, format, digNum).toStdString();
1344 }