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