Salome HOME
Copyright update 2022
[modules/geom.git] / src / EntityGUI / EntityGUI_PolylineDlg.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "EntityGUI_PolylineDlg.h"
24 #include <CurveCreator_Curve.hxx>
25 #include <CurveCreator_Displayer.hxx>
26 #include <CurveCreator_Utils.hxx>
27 #include <CurveCreator_Widget.h>
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include "utilities.h"
33
34 #include <OCCViewer_ViewManager.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <SalomeApp_Application.h>
37 #include <SUIT_ResourceMgr.h>
38 #include <SUIT_Session.h>
39 #include "utilities.h"
40
41 #include <BRep_Tool.hxx>
42 #include <Geom_Surface.hxx>
43 #include <GeomLib_IsPlanarSurface.hxx>
44 #include <Prs3d_LineAspect.hxx>
45 #include <TopoDS.hxx>
46
47 #include <QGroupBox>
48 #include <QVBoxLayout>
49
50 //#define SET_PLANE
51
52 //=================================================================================
53 // function : Constructor
54 // purpose  :
55 //=================================================================================
56 EntityGUI_PolylineDlg::EntityGUI_PolylineDlg
57         (GeometryGUI* theGeometryGUI, QWidget* parent, bool modal, Qt::WindowFlags fl)
58   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl ),
59     myCurve               (0),
60     myEditorWidget        (0),
61     myAddElementBox       (0),
62     myPlnComboBox         (0),
63     myPlnButton           (0),
64     myPlnSelButton        (0),
65     myPolylineSelButton   (0),
66     myWPlaneLineEdit      (0),
67     myPolylineEdit        (0),
68     myEditCurrentArgument (0),
69     myPreviewManager(0),
70     myPreviewZLayer(-1)
71 {
72   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
73   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
74
75   setWindowTitle(tr("POLYLINE_DLG_TITLE"));
76
77   /***************************************************************/
78   mainFrame()->GroupConstructors->setTitle(tr("POLYLINE_TITLE"));
79   mainFrame()->RadioButton1->setIcon(image0);
80   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
81   mainFrame()->RadioButton2->close();
82   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
83   mainFrame()->RadioButton3->close();
84
85   QGroupBox   *aGroupBox1 = new QGroupBox(this);
86   QGridLayout *aPlaneLayout = new QGridLayout(aGroupBox1);
87
88   aPlaneLayout->setSpacing(6);
89   aPlaneLayout->setMargin(11);
90
91   myPlnComboBox = new QComboBox(aGroupBox1);
92   aPlaneLayout->addWidget(myPlnComboBox, 0, 0, 1, 3);
93
94   myPlnButton = new QPushButton (aGroupBox1);
95   myPlnButton->setText( tr( "GEOM_SKETCHER_RESTORE" ) );
96   aPlaneLayout->addWidget(myPlnButton, 0, 3);
97
98   bool isCS = false;
99 #ifdef SET_PLANE
100   isCS = true;
101 #endif
102   myPlnComboBox->setVisible( isCS );
103   myPlnButton->setVisible( isCS );
104
105 #ifdef SET_PLANE
106   QLabel *aPlaneLbl = new QLabel(tr("GEOM_PLANE"), aGroupBox1);
107
108   myPlnSelButton = new QPushButton (aGroupBox1);
109   myPlnSelButton->setIcon(image1);
110   myWPlaneLineEdit = new QLineEdit (aGroupBox1);
111   myWPlaneLineEdit->setReadOnly(true);
112 #endif
113
114   QLabel *aPolylineLbl = new QLabel(tr("POLYLINE_IMPORT"), aGroupBox1);
115
116   myPolylineSelButton = new QPushButton (aGroupBox1);
117   myPolylineSelButton->setIcon(image1);
118   myPolylineSelButton->setCheckable(true);
119   myPolylineEdit = new QLineEdit (aGroupBox1);
120   myPolylineEdit->setReadOnly(true);
121
122 #ifdef SET_PLANE
123   aPlaneLayout->addWidget(aPlaneLbl, 1, 0);
124   aPlaneLayout->addWidget(myPlnSelButton, 1, 1);
125   aPlaneLayout->addWidget(myWPlaneLineEdit, 1, 2, 1, 2);
126 #endif
127   aPlaneLayout->addWidget(aPolylineLbl, 2, 0);
128   aPlaneLayout->addWidget(myPolylineSelButton, 2, 1);
129   aPlaneLayout->addWidget(myPolylineEdit, 2, 2, 1, 2);
130   
131   aPlaneLayout->setColumnStretch(2, 1);
132
133   myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
134   myEditorWidget = new CurveCreator_Widget (centralWidget(), myCurve);
135   myAddElementBox = new QGroupBox (tr("POLYLINE_ADD_SECTION"), centralWidget());
136
137   QBoxLayout* anAddElementLayout = new QVBoxLayout( myAddElementBox );
138
139   anAddElementLayout->setMargin( 0 );
140   anAddElementLayout->setSpacing( 6 );
141
142   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
143
144   layout->setMargin( 0 );
145   layout->setSpacing( 6 );
146   layout->addWidget( aGroupBox1 );
147   layout->addWidget( myEditorWidget );
148   layout->addWidget( myAddElementBox );
149
150   /***************************************************************/
151
152   setHelpFileName( "create_polyline_page.html" );
153
154   /* Initializations */
155   Init();
156 }
157
158 //=================================================================================
159 // function : Destructor
160 // purpose  :
161 //=================================================================================
162 EntityGUI_PolylineDlg::~EntityGUI_PolylineDlg()
163 {
164   erasePreview();
165   myEditorWidget->SetViewer2DMode(false);
166 }
167
168 //=================================================================================
169 // function : Init()
170 // purpose  :
171 //=================================================================================
172 void EntityGUI_PolylineDlg::Init()
173 {
174   initName(tr("POLYLINE_NAME"));
175
176   SalomeApp_Application *anApp        = myGeomGUI->getApp();
177   OCCViewer_ViewManager *aViewManager = dynamic_cast<OCCViewer_ViewManager*>
178     (anApp->getViewManager(OCCViewer_Viewer::Type(), true));
179   LightApp_SelectionMgr *aSelMgr = myGeomGUI->getApp()->selectionMgr();
180
181   myEditorWidget->setOCCViewer(aViewManager ? aViewManager->getOCCViewer() : 0);
182   setPreviewManager( aViewManager );
183
184   // Init the list of local coordinate system
185   gp_Pnt aPnt(0., 0., 0.);
186   gp_Dir aDirN(0., 0., 1.);
187   gp_Dir aDirX(1., 0., 0.);
188   gp_Ax3 aLCS(aPnt, aDirN, aDirX);
189
190   //add Global CS
191   myPlnComboBox->addItem(tr("GEOM_GCS"), false);
192   myWPlaneList.push_back(GEOM::GeomObjPtr());
193   myLCSList.push_back(aLCS);
194
195 #ifdef SET_PLANE
196   connect( myPlnSelButton, SIGNAL(toggled(bool)),
197            this,           SLOT(SetEditCurrentArgument(bool)) );
198 #endif
199   connect( myPolylineSelButton, SIGNAL(toggled(bool)),
200            this,                SLOT(SetEditCurrentArgument(bool)) );
201   connect( aSelMgr, SIGNAL(currentSelectionChanged()),
202            this,    SLOT(SelectionIntoArgument()) );
203   connect( myEditorWidget, SIGNAL(subOperationStarted(QWidget*, bool)),
204            this,           SLOT(processStartedSubOperation(QWidget*, bool)) );
205   connect( myEditorWidget, SIGNAL(subOperationFinished(QWidget*)),
206            this,           SLOT(processFinishedSubOperation(QWidget*)) );
207 #ifdef SET_PLANE
208   connect( myPlnComboBox,  SIGNAL(activated(int)),
209            this,           SLOT(ActivateLocalCS()) );
210   connect( myPlnButton,    SIGNAL(clicked()),
211            this,           SLOT(ActivateLocalCS()) );
212 #endif
213   connect( buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()) );
214   connect( buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()) );
215
216   myAddElementBox->hide();
217
218   // Processing of the selected object
219   myPolylineSelButton->setChecked( true );
220   SelectionIntoArgument( true );
221 }
222
223 //=================================================================================
224 // function : Clear
225 // purpose  :
226 //=================================================================================
227 void EntityGUI_PolylineDlg::Clear()
228 {
229   delete myCurve;
230
231   myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
232   myEditorWidget->setCurve(myCurve);
233 }
234
235 //=================================================================================
236 // function : GetCurveParams
237 // purpose  :
238 //=================================================================================
239 void EntityGUI_PolylineDlg::GetCurveParams(GEOM::ListOfListOfDouble &theCoords,
240                                            GEOM::string_array       &theNames,
241                                            GEOM::short_array        &theTypes,
242                                            GEOM::ListOfBool         &theCloseds)
243 {
244   const int aNbSec = myCurve->getNbSections();
245   int       i;
246   int       j;
247
248   theCoords.length(aNbSec);
249   theNames.length(aNbSec);
250   theTypes.length(aNbSec);
251   theCloseds.length(aNbSec);
252
253   for (i = 0; i < aNbSec; ++i) {
254     // Set coordinates
255     CurveCreator::Coordinates aCoords   = myCurve->getCoords(i);
256     const int                 aNbPoints = aCoords.size();
257
258     theCoords[i].length(aNbPoints);
259
260     for (j = 0; j < aNbPoints; ++j) {
261       theCoords[i][j] = aCoords[j];
262     }
263
264     // Set section type
265     const CurveCreator::SectionType aType = myCurve->getSectionType(i);
266
267     switch (aType) {
268       case CurveCreator::Spline:
269         theTypes[i] = GEOM::Interpolation;
270         break;
271       case CurveCreator::Polyline:
272       default:
273         theTypes[i] = GEOM::Polyline;
274         break;
275     }
276
277     // Set section names and closed flags.
278     theNames[i]   = CORBA::string_dup(myCurve->getSectionName(i).c_str());
279     theCloseds[i] = myCurve->isClosed(i);
280   }
281 }
282
283 //=================================================================================
284 // function : createOperation
285 // purpose  :
286 //=================================================================================
287 GEOM::GEOM_IOperations_ptr EntityGUI_PolylineDlg::createOperation()
288 {
289   return getGeomEngine()->GetICurvesOperations();
290 }
291
292 //=================================================================================
293 // function : isValid
294 // purpose  :
295 //=================================================================================
296 bool EntityGUI_PolylineDlg::isValid( QString& /*msg*/ )
297 {
298   return true;
299 }
300
301 //=================================================================================
302 // function : execute
303 // purpose  :
304 //=================================================================================
305 bool EntityGUI_PolylineDlg::execute( ObjectList& objects )
306 {
307   GEOM::GEOM_ICurvesOperations_var anOper =
308     GEOM::GEOM_ICurvesOperations::_narrow(getOperation());
309
310   // Get the polyline creation parameters.
311   GEOM::ListOfListOfDouble aCoords;
312   GEOM::string_array       aNames;
313   GEOM::short_array        aTypes;
314   GEOM::ListOfBool         aCloseds;
315
316   GetCurveParams(aCoords, aNames, aTypes, aCloseds);
317
318   // Get Working Plane.
319   int ind = myPlnComboBox->currentIndex();
320
321   if (ind != -1) {
322     bool                  isPlane = myPlnComboBox->itemData(ind).toBool();
323     GEOM::GEOM_Object_var anObj;
324
325     // Perform operation
326     if (isPlane) {
327       anObj = anOper->MakePolyline2DOnPlane
328         (aCoords, aNames, aTypes, aCloseds, myWPlaneList.at(ind).get());
329     } else {
330       gp_Ax3             anAxis = myLCSList.at(ind);
331       GEOM::ListOfDouble aPlane;
332
333       aPlane.length(9);
334       aPlane[0] = anAxis.Location().X();
335       aPlane[1] = anAxis.Location().Y();
336       aPlane[2] = anAxis.Location().Z();
337       aPlane[3] = anAxis.Direction().X();
338       aPlane[4] = anAxis.Direction().Y();
339       aPlane[5] = anAxis.Direction().Z();
340       aPlane[6] = anAxis.XDirection().X();
341       aPlane[7] = anAxis.XDirection().Y();
342       aPlane[8] = anAxis.XDirection().Z();
343
344       anObj = anOper->MakePolyline2D
345         (aCoords, aNames, aTypes, aCloseds, aPlane);
346     }
347
348     if (!anObj->_is_nil()) {
349       objects.push_back(anObj._retn());
350     }
351   }
352
353   return true;
354 }
355
356 //=================================================================================
357 // function : ClickOnOk()
358 // purpose  :
359 //=================================================================================
360 void EntityGUI_PolylineDlg::ClickOnOk()
361 {
362   setIsApplyAndClose( true );
363
364   if (ClickOnApply())
365     ClickOnCancel();
366 }
367
368 //=================================================================================
369 // function : ClickOnApply()
370 // purpose  :
371 //=================================================================================
372 bool EntityGUI_PolylineDlg::ClickOnApply()
373 {
374   if (!onAccept())
375     return false;
376
377   initName();
378
379   // Reset actions
380   myEditorWidget->reset();
381
382   return true;
383 }
384
385 //=================================================================================
386 // function : processStartedSubOperation
387 // purpose  :
388 //=================================================================================
389 void EntityGUI_PolylineDlg::processStartedSubOperation( QWidget* theWidget, bool theIsEdit )
390 {
391   myEditorWidget->setEnabled( false );
392
393   myAddElementBox->setTitle( theIsEdit ? tr( "POLYLINE_EDIT_SECTION" ) : tr( "POLYLINE_ADD_SECTION" ) );
394   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
395   anAddElementLayout->addWidget( theWidget );
396
397   theWidget->show();
398   myAddElementBox->show();
399 }
400
401
402 //=================================================================================
403 // function : processFinishedSubOperation
404 // purpose  :
405 //=================================================================================
406 void EntityGUI_PolylineDlg::processFinishedSubOperation( QWidget* theWidget )
407 {
408   myEditorWidget->setEnabled( true );
409
410   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
411   anAddElementLayout->removeWidget( theWidget );
412
413   theWidget->hide();
414   myAddElementBox->hide();
415 }
416
417 //=================================================================================
418 // function : deleteSelected
419 // purpose  : Redirect the delete action to editor widget
420 //=================================================================================
421 void EntityGUI_PolylineDlg::deleteSelected()
422 {
423   myEditorWidget->removeSelected();
424 }
425
426 //=================================================================================
427 // function : deleteEnabled
428 // purpose  : Checks whether there are some to delete
429 //=================================================================================
430 bool EntityGUI_PolylineDlg::deleteEnabled()
431 {
432   return myEditorWidget->removeEnabled();
433 }
434
435 //=================================================================================
436 // function : SelectionIntoArgument
437 // purpose  : Called when selection is changed
438 //=================================================================================
439 void EntityGUI_PolylineDlg::SelectionIntoArgument( bool isForced )
440 {
441   bool             isModified      = isForced;
442   GEOM::GeomObjPtr aSelectedObject = getSelected(TopAbs_SHAPE);
443   TopoDS_Shape     aShape;
444
445   if (aSelectedObject && GEOMBase::GetShape(aSelectedObject.get(), aShape) &&
446       !aShape.IsNull())
447   {
448     QString aName = GEOMBase::GetName(aSelectedObject.get()); 
449
450     if ( myEditCurrentArgument == myPolylineEdit && isCheckToSelect() )
451     {
452       // Import a curve
453       CurveCreator_Curve *aNewCurve = 
454         new CurveCreator_Curve(CurveCreator::Dim2d);
455       gp_Ax3              aLocalCS;
456
457       if (CurveCreator_Utils::constructCurve(aShape, aNewCurve, aLocalCS)) {
458         // Change the current curve be the new one.
459         myEditorWidget->setCurve(aNewCurve);
460         delete myCurve;
461         myCurve = aNewCurve;
462         isModified = true;
463         myPolylineEdit->setText(aName);
464 #ifdef SET_PLANE
465         AddLocalCS(aSelectedObject.get(), false, aLocalCS);
466         myWPlaneLineEdit->clear();
467         myPlnSelButton->setChecked(false);
468 #endif
469         myPolylineSelButton->setChecked(false);
470
471         disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
472         myGeomGUI->getApp()->selectionMgr()->clearSelected();
473         connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
474                 this, SLOT(SelectionIntoArgument()));
475       } else {
476         // Does nothing, just clears selection.
477         delete aNewCurve;
478       }
479     }
480 #ifdef SET_PLANE
481     else if ( myEditCurrentArgument == myWPlaneLineEdit && isCheckToSelect() )
482     {
483       // Import planar face.
484       if (aShape.ShapeType() == TopAbs_FACE) {
485         // Check if the face is planar
486         Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
487         GeomLib_IsPlanarSurface aPlanarCheck(aSurf, Precision::Confusion());
488
489         if (aPlanarCheck.IsPlanar()) {
490           myWPlaneLineEdit->setText(aName);
491           myPolylineEdit->clear();
492           AddLocalCS(aSelectedObject.get(), true, 
493                      WPlaneToLCS(aSelectedObject.get()));
494           isModified = true;
495           myPlnSelButton->setChecked(false);
496           myPolylineSelButton->setChecked(false);
497         }
498       }
499       
500       if (!isModified) {
501         myEditCurrentArgument->setText(tr("GEOM_SKETCHER_WPLANE"));
502       }
503     }
504 #endif
505   }
506   else
507   {
508     if (isForced)
509     {
510 #ifdef SET_PLANE
511       myPlnSelButton->setChecked(false);
512 #endif
513       myPolylineSelButton->setChecked(false);
514     }
515   }
516
517   if (!isModified)
518   {
519     // Does nothing, just clears selection.
520     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
521     myGeomGUI->getApp()->selectionMgr()->clearSelected();
522     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
523             this, SLOT(SelectionIntoArgument()));
524   }
525   else
526     displayPreview();
527 }
528
529 //=================================================================================
530 // function : SetEditCurrentArgument()
531 // purpose  :
532 //=================================================================================
533 void EntityGUI_PolylineDlg::SetEditCurrentArgument( bool /*isChecked*/ )
534 {
535   if (sender() == myPlnSelButton)
536   {
537 #ifdef SET_PLANE
538     myEditCurrentArgument = myWPlaneLineEdit;
539     myEditCurrentArgument->setFocus();
540     myPolylineSelButton->blockSignals(true);
541     myPolylineSelButton->setChecked(false);
542     myPolylineSelButton->blockSignals(false);
543 #endif
544   }
545   else if (sender() == myPolylineSelButton)
546   {
547     myEditCurrentArgument = myPolylineEdit;
548     myEditCurrentArgument->setFocus();
549 #ifdef SET_PLANE
550     myPlnSelButton->blockSignals(true);
551     myPlnSelButton->setChecked(false);
552     myPlnSelButton->blockSignals(false);
553 #endif
554   }
555 }
556
557 //=================================================================================
558 // function : ActivateThisDialog
559 // purpose  :
560 //=================================================================================
561 void EntityGUI_PolylineDlg::ActivateThisDialog()
562 {
563   GEOMBase_Skeleton::ActivateThisDialog();
564
565   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
566            this, SLOT(SelectionIntoArgument()));
567 }
568
569 //=================================================================================
570 // function : enterEvent()
571 // purpose  :
572 //=================================================================================
573 void EntityGUI_PolylineDlg::enterEvent (QEvent*)
574 {
575   if (!mainFrame()->GroupConstructors->isEnabled())
576     ActivateThisDialog();
577 }
578
579 //=================================================================================
580 // function : ActivateLocalCS
581 // purpose  : Activate & Fit Working plane
582 //=================================================================================
583 void EntityGUI_PolylineDlg::ActivateLocalCS()
584 {
585   const int ind = myPlnComboBox->currentIndex();
586
587   if (ind == 0) {
588     // Default plane
589 #ifdef SET_PLANE
590     myWPlaneLineEdit->clear();
591 #endif
592     myPolylineEdit->clear();
593   } else if (ind > 0) { // Skip 0 as it is default
594     // Update text on line edits.
595     QString aName   = GEOMBase::GetName(GetActiveWPlane().get());
596     bool    isPlane = myPlnComboBox->itemData(ind).toBool();
597
598     if (isPlane) {
599 #ifdef SET_PLANE
600       myWPlaneLineEdit->setText(aName);
601 #endif
602       myPolylineEdit->clear();
603     } else {
604       myPolylineEdit->setText(aName);
605 #ifdef SET_PLANE
606       myWPlaneLineEdit->clear();
607 #endif
608     }
609   }
610
611   gp_Ax3 anAxis = GetActiveLocalCS();
612
613   myGeomGUI->SetWorkingPlane(anAxis);
614   myGeomGUI->ActiveWorkingPlane();
615 }
616
617 //=================================================================================
618 // function : GetActiveLocalCS
619 // purpose  : Get Working plane
620 //=================================================================================
621 gp_Ax3 EntityGUI_PolylineDlg::GetActiveLocalCS()
622 {
623   const int ind = myPlnComboBox->currentIndex();
624
625   return ind >= 0 ? myLCSList.at(ind) : myGeomGUI->GetWorkingPlane();
626 }
627
628 //=================================================================================
629 // function : GetActiveWPlane
630 // purpose  : Get Working plane
631 //=================================================================================
632 GEOM::GeomObjPtr EntityGUI_PolylineDlg::GetActiveWPlane()
633 {
634   const int ind = myPlnComboBox->currentIndex();
635
636   return ind >= 0 ? myWPlaneList.at(ind) : GEOM::GeomObjPtr();
637 }
638
639 //=================================================================================
640 // function : AddLocalCS()
641 // purpose  : Add All Coordinates systems in study
642 //=================================================================================
643 void EntityGUI_PolylineDlg::AddLocalCS(GEOM::GeomObjPtr  theSelectedObject,
644                                        const bool        IsPlane,
645                                        const gp_Ax3      &theLCS)
646 {
647   QString aName = GEOMBase::GetName(theSelectedObject.get());
648
649   int index = myPlnComboBox->findText(aName, Qt::MatchExactly);
650
651   if (index == -1) { // If the working plane hasn't been added yet
652     myWPlaneList.push_back(theSelectedObject);
653     myLCSList.push_back(theLCS);
654     myPlnComboBox->addItem(aName, QVariant(IsPlane));
655     index = myPlnComboBox->count();
656     myPlnComboBox->setCurrentIndex(index - 1);
657   } else {
658     myPlnComboBox->setCurrentIndex(index);
659   }
660   ActivateLocalCS();
661 }
662
663 //=================================================================================
664 // function : WPlaneToLCS ( aWPlane )
665 // purpose  : 
666 //=================================================================================
667 gp_Ax3 EntityGUI_PolylineDlg::WPlaneToLCS(GEOM::GeomObjPtr theGeomObj)
668 {
669   TopoDS_Shape aShape =
670     GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), theGeomObj.get());
671   gp_Ax3 aLCS;
672
673   if (theGeomObj || aShape.IsNull()) {
674     MESSAGE("CORBA::is_nil(theGeomObj) || aShape.IsNull()")
675   }
676
677   aLCS.Transform(aShape.Location().Transformation());
678
679   if (aShape.ShapeType() == TopAbs_FACE) {
680     GEOM::GEOM_IMeasureOperations_ptr aMeasureOp =
681       myGeomGUI->GetGeomGen()->GetIMeasureOperations();
682     double Ox, Oy, Oz, Zx, Zy, Zz, Xx, Xy, Xz;
683
684     aMeasureOp->GetPosition(theGeomObj.get(), Ox, Oy, Oz, Zx, Zy, Zz, Xx, Xy, Xz);
685
686     if (aMeasureOp->IsDone()) {
687       gp_Pnt aPnt (Ox, Oy, Oz);
688       gp_Dir aDirN (Zx, Zy, Zz);
689       gp_Dir aDirX (Xx, Xy, Xz);
690       aLCS = gp_Ax3(aPnt, aDirN, aDirX);
691     }
692   }
693
694   return aLCS;
695 }
696
697 //=================================================================================
698 // function : getSourceObjects
699 // purpose  : virtual method to get source objects
700 //=================================================================================
701 QList<GEOM::GeomObjPtr> EntityGUI_PolylineDlg::getSourceObjects()
702 {
703   return myWPlaneList;
704 }
705
706 //=================================================================================
707 // function : setPreviewManager ( OCCViewer_ViewManager )
708 // purpose  : Sets view manager to control the displayed objects.
709 //=================================================================================
710 void EntityGUI_PolylineDlg::setPreviewManager( OCCViewer_ViewManager* theManager )
711 {
712   myPreviewManager = theManager;
713
714   if ( getPreviewManager() )
715   {
716     if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
717     {
718       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
719       if( !aCtx.IsNull() )
720       {
721         // Add Z layer to show temporary objects
722         int aZLayer = -1;
723         aViewer->getViewer3d()->AddZLayer( aZLayer );
724         setPreviewZLayer( aZLayer );
725       }
726     }
727   }
728 }
729
730 //=================================================================================
731 // function : getPreviewManager()
732 // purpose  : 
733 //=================================================================================
734 OCCViewer_ViewManager* EntityGUI_PolylineDlg::getPreviewManager()
735 {
736   return myPreviewManager;
737 }
738
739 //=================================================================================
740 // function : setPreviewZLayer( theZLayer )
741 // purpose  : Sets Z layer to show temporary objects.
742 //=================================================================================
743 void EntityGUI_PolylineDlg::setPreviewZLayer( int theZLayer )
744 {
745   if ( theZLayer != myPreviewZLayer )
746     myPreviewZLayer = theZLayer;
747 }
748
749 //=================================================================================
750 // function : getPreviewZLayer()
751 // purpose  : 
752 //=================================================================================
753 int EntityGUI_PolylineDlg::getPreviewZLayer() const
754 {
755   return myPreviewZLayer;
756 }
757
758 //=================================================================================
759 // function : displayPreview()
760 // purpose  : 
761 //=================================================================================
762 void EntityGUI_PolylineDlg::displayPreview()
763 {
764   if ( getPreviewManager() )
765   {
766     if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
767     {
768       // Disable changing of OCCViewer's selection to use vertex of curve selection
769       disconnect(getPreviewManager(), SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
770                  aViewer,             SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
771
772       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
773       if ( !aCtx.IsNull() )
774       {
775         CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
776         myCurve->setDisplayer( aDisplayer );
777
778         Handle(AIS_InteractiveObject) anAISObj = myCurve->getAISObject( true );
779         aDisplayer->display( anAISObj, true );
780
781         // Set color for temporary AIS_InteractiveObject
782         anAISObj->Attributes()->WireAspect()->SetColor( Quantity_NOC_VIOLET );
783         aCtx->Redisplay( anAISObj, Standard_True );
784       }
785     }
786   }
787 }
788
789 //=================================================================================
790 // function : erasePreview()
791 // purpose  : 
792 //=================================================================================
793 void EntityGUI_PolylineDlg::erasePreview()
794 {
795   CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
796   if( getPreviewManager() )
797   {
798     if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
799     {
800       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
801       if( !aCtx.IsNull() && aDisplayer )
802         aDisplayer->eraseAll( true );
803
804       // Enable changing of OCCViewer's selection
805       connect(getPreviewManager(), SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
806               aViewer,             SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
807     }
808   }
809
810   setPreviewManager( NULL );
811   if ( myCurve )
812   {
813     delete myCurve;
814     myCurve = NULL;
815   }
816 }
817
818 //=================================================================================
819 // function : isCheckToSelect()
820 // purpose  : 
821 //=================================================================================
822 bool EntityGUI_PolylineDlg::isCheckToSelect()
823 {
824 #ifdef SET_PLANE
825   return myPlnSelButton->isChecked();
826 #endif
827   return myPolylineSelButton->isChecked();
828 }