Salome HOME
58f029612eaeba351c7f544842c5a4f3ce84c5c1
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddNodeOnFaceDlg.cxx
1 // Copyright (C) 2007-2021  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 // File   : SMESHGUI_AddNodeOnFaceDlg.cxx
24 // Author : Edward AGAPOV, Open CASCADE S.A.S.
25 //
26 #include "SMESHGUI_AddNodeOnFaceDlg.h"
27
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_Filter.h"
30 #include "SMESHGUI_IdValidator.h"
31 #include "SMESHGUI_MeshEditPreview.h"
32 #include "SMESHGUI_MeshUtils.h"
33 #include "SMESHGUI_SpinBox.h"
34 #include "SMESHGUI_VTKUtils.h"
35
36 #include <SMDS_Mesh.hxx>
37 #include <SMESH_Actor.h>
38 #include <SMESH_ActorUtils.h>
39 #include <SMESH_TypeDefs.hxx>
40 #include <SMESH_MeshAlgos.hxx>
41
42 // SALOME GUI includes
43 #include <LightApp_SelectionMgr.h>
44 #include <SALOME_ListIO.hxx>
45 #include <SUIT_Desktop.h>
46 #include <SVTK_ViewModel.h>
47 #include <SVTK_ViewWindow.h>
48 #include <SalomeApp_Tools.h>
49 #include <SalomeApp_TypeFilter.h>
50 #include <SUIT_ResourceMgr.h>
51 #include <SUIT_OverrideCursor.h>
52 #include <SUIT_MessageBox.h>
53 #include <SVTK_RenderWindowInteractor.h>
54 #include <SVTK_Renderer.h>
55 #include <SVTK_Event.h>
56
57 // Qt includes
58 #include <QApplication>
59 #include <QGroupBox>
60 #include <QGridLayout>
61 #include <QHBoxLayout>
62 #include <QVBoxLayout>
63 #include <QLineEdit>
64 #include <QPushButton>
65 #include <QLabel>
66 #include <QRadioButton>
67 #include <QCheckBox>
68 #include <QButtonGroup>
69
70 // VTK includes
71 #include <vtkProperty.h>
72 #include <vtkCellPicker.h>
73 #include <vtkInteractorStyle.h>
74 #include <vtkGenericRenderWindowInteractor.h>
75 #include <vtkInteractorObserver.h>
76
77
78 // IDL includes
79 #include <SALOMEconfig.h>
80 #include CORBA_SERVER_HEADER(SMESH_Mesh)
81 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
82
83 #define SPACING 6
84 #define MARGIN  11
85
86 #define TOLERANCE 1e-3
87
88 //=======================================================================
89 /*!
90  * \brief Dialog to publish a sub-shape of the mesh main shape
91  *        by selecting mesh elements
92  */
93 //=======================================================================
94
95 SMESHGUI_AddNodeOnFaceDlg::SMESHGUI_AddNodeOnFaceDlg()
96   : SMESHGUI_Dialog( 0, false, true )
97 {
98   setWindowTitle(tr("CAPTION"));
99
100   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
101   aDlgLay->setMargin(0);
102   aDlgLay->setSpacing(SPACING);
103   myMainFrame = createMainFrame(mainFrame());
104
105   aDlgLay->addWidget(myMainFrame);
106
107   aDlgLay->setStretchFactor(myMainFrame, 1);
108 }
109
110 //=======================================================================
111 // function : createMainFrame()
112 // purpose  : Create frame containing dialog's input fields
113 //=======================================================================
114
115 QWidget* SMESHGUI_AddNodeOnFaceDlg::createMainFrame (QWidget* theParent)
116 {
117   QWidget* aFrame = new QWidget(theParent);
118
119   SUIT_ResourceMgr* rm = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
120   QPixmap iconSelect( rm->loadPixmap("SMESH", tr("ICON_SELECT")));
121
122   // Face to split
123
124   QGroupBox* faceGrp = new QGroupBox(tr("FACE_GROUP"), aFrame);
125   faceGrp->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
126   QLabel* idLabel = new QLabel(tr("FACE_ID"), faceGrp);
127   myIdBtn = new QPushButton(faceGrp);
128   myIdBtn->setIcon(iconSelect);
129   myIdBtn->setCheckable(true);
130   myId = new QLineEdit(faceGrp);
131   myId->setValidator(new SMESHGUI_IdValidator(this, 1));
132
133   QGridLayout* faceGrpLayout = new QGridLayout(faceGrp);
134   faceGrpLayout->setSpacing(SPACING);
135   faceGrpLayout->setMargin(MARGIN);
136
137   faceGrpLayout->addWidget( idLabel, 0, 0 );
138   faceGrpLayout->addWidget( myIdBtn, 0, 1 );
139   faceGrpLayout->addWidget( myId,    0, 2 );
140
141   // Node location
142
143   QGroupBox* xyzGrp = new QGroupBox(tr("XYZ_GROUP"), aFrame);
144   xyzGrp->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
145
146   myDestBtn = new QPushButton(xyzGrp);
147   myDestBtn->setIcon(iconSelect);
148   myDestBtn->setCheckable(true);
149
150   QLabel* locationXLabel = new QLabel(tr("SMESH_X"), xyzGrp);
151   myDestinationX = new SMESHGUI_SpinBox(xyzGrp);
152   QLabel* locationYLabel = new QLabel(tr("SMESH_Y"), xyzGrp);
153   myDestinationY = new SMESHGUI_SpinBox(xyzGrp);
154   QLabel* locationZLabel = new QLabel(tr("SMESH_Z"), xyzGrp);
155   myDestinationZ = new SMESHGUI_SpinBox(xyzGrp);
156
157   myDestinationX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
158   myDestinationY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
159   myDestinationZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
160
161   myPointOnFace = new QCheckBox(tr("XYZ_NODE_ON_FACE"), xyzGrp);
162
163   QGridLayout* aDestLayout = new QGridLayout(xyzGrp);
164   aDestLayout->setMargin(MARGIN);
165   aDestLayout->setSpacing(SPACING);
166   aDestLayout->addWidget(myDestBtn, 0, 0);
167   aDestLayout->addWidget(locationXLabel, 0, 1);
168   aDestLayout->addWidget(myDestinationX, 0, 2);
169   aDestLayout->addWidget(locationYLabel, 0, 3);
170   aDestLayout->addWidget(myDestinationY, 0, 4);
171   aDestLayout->addWidget(locationZLabel, 0, 5);
172   aDestLayout->addWidget(myDestinationZ, 0, 6);
173   aDestLayout->setColumnStretch(2, 1);
174   aDestLayout->setColumnStretch(4, 1);
175   aDestLayout->setColumnStretch(6, 1);
176   aDestLayout->addWidget(myPointOnFace, 1, 0, 1, 6);
177
178   // Preview
179   myPreviewChkBox = new QCheckBox( tr("PREVIEW"), aFrame);
180   myPreviewChkBox->setChecked( true );
181
182   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
183   aLay->addWidget(faceGrp);
184   aLay->addWidget(xyzGrp);
185   aLay->addWidget(myPreviewChkBox);
186
187   connect(myDestBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
188   connect(myIdBtn,   SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
189
190   myIdBtn->setChecked(true);
191
192   return aFrame;
193 }
194
195 //================================================================================
196 /*!
197  * \brief SLOT called when any button is toggled
198   * \param bool - on or off
199  */
200 //================================================================================
201
202 void SMESHGUI_AddNodeOnFaceDlg::ButtonToggled (bool on)
203 {
204   const QObject* aSender = sender();
205   if ( on ) {
206     if ( aSender == myDestBtn ) // button to set coord by node selection
207     {
208       if ( myIdBtn->isEnabled() )
209         myIdBtn->setChecked( !on );
210     }
211     else if ( aSender == myIdBtn ) // button to select a node to move
212     {
213       myDestBtn->setChecked( !on );
214     }
215   }
216   emit selTypeChanged();
217 }
218
219 //================================================================================
220 /*!
221  * \brief Constructor
222 */
223 //================================================================================
224
225 SMESHGUI_AddNodeOnFaceOp::SMESHGUI_AddNodeOnFaceOp() : 
226   SMESHGUI_InteractiveOp()
227 {
228   mySimulation = 0;
229   mySMESHGUI = 0;
230   myDlg = new SMESHGUI_AddNodeOnFaceDlg;
231   myHelpFileName = "add_node_on_face.html";
232
233   myNoPreview = false;
234   myUpdateDestination = false;
235
236   // connect signals and slots
237   connect(myDlg->myDestinationX,  SIGNAL (valueChanged(double)), this, SLOT(onDestCoordChanged()));
238   connect(myDlg->myDestinationY,  SIGNAL (valueChanged(double)), this, SLOT(onDestCoordChanged()));
239   connect(myDlg->myDestinationZ,  SIGNAL (valueChanged(double)), this, SLOT(onDestCoordChanged()));
240   connect(myDlg->myId,            SIGNAL (textChanged(const QString&)),SLOT(redisplayPreview()));
241   connect(myDlg->myPreviewChkBox, SIGNAL (toggled(bool)),              SLOT(redisplayPreview()));
242   connect(myDlg,                  SIGNAL (selTypeChanged() ),          SLOT(onSelTypeChange()));
243   connect(myDlg->myId,            SIGNAL (textChanged(const QString&)),SLOT(onTextChange(const QString&)));
244   connect(myDlg->myPointOnFace,   SIGNAL(toggled(bool)),               SLOT(pointLocationChanged(bool)));
245
246   myFacePicker = vtkCellPicker::New();
247   myFacePicker->SetTolerance(0.005);
248   myFacePicker->PickFromListOn();
249
250 }
251
252 //================================================================================
253 /*!
254  * \brief SLOT. Called upon change of selection type
255  */
256 //================================================================================
257
258 void SMESHGUI_AddNodeOnFaceOp::onSelTypeChange()
259 {
260   if ( myDlg->myIdBtn->isChecked() )
261   {
262     setSelectionMode( FaceSelection );
263   }
264   else if ( myDlg->myDestBtn->isChecked() )
265   {
266     QString msg;
267     if (isValid(msg)) {
268       //Disconnect selectionChanged to keep selected element
269       disconnect(selectionMgr(), SIGNAL(selectionChanged()), this, SLOT(onSelectionDone()));
270       // Set selection mode to ActorSelection to avoid element's prehighlight during interactive selection
271       setSelectionMode(ActorSelection);
272       connect(selectionMgr(), SIGNAL(selectionChanged()), SLOT(onSelectionDone()));
273     }
274   }
275   else
276   {
277     setSelectionMode( ActorSelection );
278   }
279 }
280
281 //=======================================================================
282 // function : startOperation()
283 // purpose  : Init dialog fields, connect signals and slots, show dialog
284 //=======================================================================
285
286 void SMESHGUI_AddNodeOnFaceOp::startOperation()
287 {
288   myNoPreview = false;
289   myMeshActor = 0;
290
291   // init simulation with a current View
292   if ( mySimulation ) delete mySimulation;
293   mySMESHGUI = getSMESHGUI();
294   mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( mySMESHGUI ) );
295   connect(mySMESHGUI, SIGNAL (SignalActivatedViewManager()), this, SLOT(onOpenView()));
296   connect(mySMESHGUI, SIGNAL (SignalCloseView()), this, SLOT(onCloseView()));
297   vtkProperty* aProp = vtkProperty::New();
298   aProp->SetRepresentationToWireframe();
299   aProp->SetColor(250, 0, 250);
300   aProp->SetPointSize(5);
301   aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
302   mySimulation->GetActor()->SetProperty(aProp);
303   aProp->Delete();
304
305   SMESHGUI_SelectionOp::startOperation();
306   SMESHGUI_InteractiveOp::startOperation();
307   myDlg->myId->setText("");
308   myUpdateDestination = true;
309   myDlg->myDestinationX->SetValue(0);
310   myDlg->myDestinationY->SetValue(0);
311   myDlg->myDestinationZ->SetValue(0);
312   myUpdateDestination = false;
313
314   addObserver();
315
316   myDlg->show();
317
318   onSelectionDone(); // init myMeshActor
319 }
320
321 //================================================================================
322 /*!
323  * \brief Stops operation
324  */
325 //================================================================================
326
327 void SMESHGUI_AddNodeOnFaceOp::stopOperation()
328 {
329   myNoPreview = true;
330   if ( mySimulation )
331   {
332     mySimulation->SetVisibility(false);
333     delete mySimulation;
334     mySimulation = 0;
335   }
336   if ( myMeshActor ) {
337     myMeshActor = 0;
338   }
339   // SMESH::SetPointRepresentation( false );
340   // SMESH::RepaintCurrentView();
341
342   disconnect(mySMESHGUI, SIGNAL (SignalActivatedViewManager()), this, SLOT(onOpenView()));
343   disconnect(mySMESHGUI, SIGNAL (SignalCloseView()),            this, SLOT(onCloseView()));
344   SMESHGUI_SelectionOp::stopOperation();
345   removeObserver();
346 }
347
348 //================================================================================
349 /*!
350  * \brief perform it's intention action: split a face
351  */
352 //================================================================================
353
354 bool SMESHGUI_AddNodeOnFaceOp::onApply()
355 {
356   if( SMESHGUI::isStudyLocked() )
357     return false;
358
359   if ( !myMeshActor ) {
360     SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ), tr("INVALID_MESH") );
361     dlg()->show();
362     return false;
363   }
364
365   QString msg;
366   if ( !isValid( msg ) ) { // node id is invalid
367     if( !msg.isEmpty() )
368       SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ), tr("INVALID_ID") );
369     dlg()->show();
370     return false;
371   }
372
373   QStringList aParameters;
374   aParameters << myDlg->myDestinationX->text();
375   aParameters << myDlg->myDestinationY->text();
376   aParameters << myDlg->myDestinationZ->text();
377
378   try {
379     SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
380     if (aMesh->_is_nil()) {
381       SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"), tr("SMESHG_NO_MESH") );
382       return true;
383     }
384     SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
385     if (aMeshEditor->_is_nil())
386       return true;
387
388     aMesh->SetParameters( aParameters.join(":").toUtf8().constData() );
389
390     bool ok;
391     SMESH::smIdType anId = myDlg->myId->text().toLong( &ok );
392
393     aMeshEditor->AddNodeOnFace( anId,
394                                 myDlg->myDestinationX->GetValue(),
395                                 myDlg->myDestinationY->GetValue(),
396                                 myDlg->myDestinationZ->GetValue() );
397     SALOME_ListIO aList;
398     selectionMgr()->setSelectedObjects(aList,false);
399     aList.Append(myMeshActor->getIO());
400     selectionMgr()->setSelectedObjects(aList,false);
401     onSelectionDone();
402     SMESH::UpdateView();
403     SMESHGUI::Modified();
404   }
405   catch (const SALOME::SALOME_Exception& S_ex) {
406     SalomeApp_Tools::QtCatchCorbaException(S_ex);
407   }
408   catch (...) {
409   }
410
411   return true;
412 }
413
414 //================================================================================
415 /*!
416  * \brief Check selected face id validity
417  */
418 //================================================================================
419
420 bool SMESHGUI_AddNodeOnFaceOp::isValid( QString& msg )
421 {
422   bool ok = false;
423   if ( myMeshActor )
424   {
425     SMESH::smIdType id = myDlg->myId->text().toLong();
426     if ( id > 0 )
427       if (SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh())
428         if ( const SMDS_MeshElement* face = aMesh->FindElement( id ))
429         {
430           if ( face->GetType() == SMDSAbs_Face )
431             ok = true;
432           else
433             msg += tr("NOT_FACE") + "\n";
434         }
435     if ( !ok )
436       msg += tr("INVALID_ID") + "\n";
437   }
438
439   ok = myDlg->myDestinationX->isValid( msg, !myNoPreview ) && ok;
440   ok = myDlg->myDestinationY->isValid( msg, !myNoPreview ) && ok;
441   ok = myDlg->myDestinationZ->isValid( msg, !myNoPreview ) && ok;
442
443   return ok;
444 }
445
446 //================================================================================
447 /*!
448  * \brief SLOT called when selection changed
449  */
450 //================================================================================
451
452 void SMESHGUI_AddNodeOnFaceOp::onSelectionDone()
453 {
454   if ( !myDlg->isVisible() || !myDlg->isEnabled() )
455     return;
456
457   myNoPreview = true;
458   QString idStr;
459   try {
460     SALOME_ListIO aList;
461     selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
462     if (aList.Extent() != 1)
463       return;
464     Handle(SALOME_InteractiveObject) anIO = aList.First();
465     myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
466     myFacePicker->InitializePickList();
467     myFacePicker->AddPickList(myMeshActor);
468
469     QString aString;
470     int nbElems = SMESH::GetNameOfSelectedElements(selector(),anIO, aString);
471     if (nbElems == 1)
472       if ( SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh())
473         if ( const SMDS_MeshElement* face = aMesh->FindElement( aString.toLong() ))
474         {
475           idStr = aString;
476           // set coordinates to face gravity center
477           gp_XYZ faceGC( 0,0,0 );
478           for ( int i = 0; i < face->NbCornerNodes(); ++i )
479             faceGC += SMESH_NodeXYZ( face->GetNode( i ));
480           faceGC /= face->NbCornerNodes();
481           myUpdateDestination = true;
482           myDlg->myDestinationX->SetValue(faceGC.X());
483           myDlg->myDestinationY->SetValue(faceGC.Y());
484           myDlg->myDestinationZ->SetValue(faceGC.Z());
485           myUpdateDestination = false;
486         }
487   } catch (...) {
488   }
489   myDlg->myId->setText( idStr );
490
491   myNoPreview = false;
492   redisplayPreview();
493 }
494
495 //================================================================================
496 /*!
497  * \brief update preview
498  */
499 //================================================================================
500
501 void SMESHGUI_AddNodeOnFaceOp::redisplayPreview()
502 {
503   if ( myNoPreview )
504     return;
505   myNoPreview = true;
506
507   SMESH::MeshPreviewStruct_var aMeshPreviewStruct;
508
509   if ( myMeshActor && myDlg->myPreviewChkBox->isChecked() )
510   {
511     QString msg;
512     if ( isValid( msg ))
513     {
514       try {
515         SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
516         if (!aMesh->_is_nil()) {
517           SMESH::SMESH_MeshEditor_var aPreviewer = aMesh->GetMeshEditPreviewer();
518           if (!aPreviewer->_is_nil())
519           {
520             SUIT_OverrideCursor aWaitCursor;
521
522             SMESH::smIdType anId = myDlg->myId->text().toLong();
523             aPreviewer->AddNodeOnFace(anId,
524                                       myDlg->myDestinationX->GetValue(),
525                                       myDlg->myDestinationY->GetValue(),
526                                       myDlg->myDestinationZ->GetValue());
527             aMeshPreviewStruct = aPreviewer->GetPreviewData();
528           }
529         }
530       }
531       catch (...) {
532       }
533     }
534   }
535   if (!mySimulation)
536     mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( mySMESHGUI ));
537   // display data
538   if ( & aMeshPreviewStruct.in() )
539   {
540     mySimulation->SetData( aMeshPreviewStruct.in() );
541   }
542   else
543   {
544     mySimulation->SetVisibility(false);
545   }
546
547   myNoPreview = false;
548 }
549
550 //=================================================================================
551 /*!
552  * \brief SLOT called when the viewer opened
553  */
554 //=================================================================================
555
556 void SMESHGUI_AddNodeOnFaceOp::onOpenView()
557 {
558   if ( mySimulation ) {
559     mySimulation->SetVisibility(false);
560     SMESH::SetPointRepresentation(false);
561   }
562   else {
563     mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( mySMESHGUI ));
564   }
565 }
566
567 //=================================================================================
568 /*!
569  * \brief SLOT called when the viewer closed
570  */
571 //=================================================================================
572
573 void SMESHGUI_AddNodeOnFaceOp::onCloseView()
574 {
575   delete mySimulation;
576   mySimulation = 0;
577 }
578
579 //================================================================================
580 /*!
581  * \brief SLOT called when the face id is manually changed
582  */
583 //================================================================================
584
585 void SMESHGUI_AddNodeOnFaceOp::onTextChange( const QString& theText )
586 {
587   if( myMeshActor )
588   {
589     if( SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh() )
590     {
591       Handle(SALOME_InteractiveObject) anIO = myMeshActor->getIO();
592       SALOME_ListIO aList;
593       aList.Append( anIO );
594       selectionMgr()->setSelectedObjects( aList, false );
595
596       if( const SMDS_MeshElement* face = aMesh->FindElement( theText.toLong() ) )
597       {
598         SVTK_TVtkIDsMap aListInd;
599         aListInd.Add( FromSmIdType<int>( face->GetID()) );
600         selector()->AddOrRemoveIndex( anIO, aListInd, false );
601         if( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( SMESHGUI::GetSMESHGUI() ) )
602           aViewWindow->highlight( anIO, true, true );
603       }
604     }
605   }
606 }
607
608 //================================================================================
609 /*!
610  * \brief Activate selection
611  */
612 //================================================================================
613
614 void SMESHGUI_AddNodeOnFaceOp::activateSelection()
615 {
616   selectionMgr()->clearFilters();
617   SMESH::SetPointRepresentation( false );
618   onSelTypeChange();
619 }
620
621 //================================================================================
622 /*!
623  * \brief Destructor
624 */
625 //================================================================================
626
627 SMESHGUI_AddNodeOnFaceOp::~SMESHGUI_AddNodeOnFaceOp()
628 {
629   if ( myDlg )        delete myDlg;
630   if ( mySimulation ) delete mySimulation;
631   myFacePicker->Delete();
632 }
633
634 //================================================================================
635 /*!
636  * \brief SLOT called when destination coordinates are changed
637 */
638 //================================================================================
639 void SMESHGUI_AddNodeOnFaceOp::onDestCoordChanged() 
640 {
641   if (myUpdateDestination)
642     return;
643   pointLocationChanged(myDlg->myPointOnFace->isChecked());
644   redisplayPreview();
645 }
646
647 //================================================================================
648 /*!
649  * \brief SLOT called when 'Node on face' checkbox is changed
650 */
651 //================================================================================
652 void SMESHGUI_AddNodeOnFaceOp::pointLocationChanged(bool onFace) {
653   if (onFace) {
654     QString msg;
655     if (myMeshActor && isValid(msg)) {
656       SMESH::smIdType id = myDlg->myId->text().toLong();
657       if (id > 0) {
658         if (SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
659           if (const SMDS_MeshElement* face = aMesh->FindElement(id))
660           {
661             if (face->GetType() == SMDSAbs_Face) {
662               gp_Pnt point(myDlg->myDestinationX->GetValue(), myDlg->myDestinationY->GetValue(),myDlg->myDestinationZ->GetValue());
663               gp_XYZ closestPnt;
664               double dist = SMESH_MeshAlgos::GetDistance( face, point, &closestPnt);
665               if (dist > TOLERANCE) {
666                 myUpdateDestination = true;
667                 myDlg->myDestinationX->SetValue(closestPnt.X());
668                 myDlg->myDestinationY->SetValue(closestPnt.Y());
669                 myDlg->myDestinationZ->SetValue(closestPnt.Z());
670                 myUpdateDestination = false;
671                 redisplayPreview();
672               }
673             }
674           }
675         }
676       }
677     }
678   }
679 }
680
681 //================================================================================
682 /*!
683  * \brief Gets dialog of this operation
684  * \retval LightApp_Dialog* - pointer to dialog of this operation
685  */
686 //================================================================================
687
688 LightApp_Dialog* SMESHGUI_AddNodeOnFaceOp::dlg() const
689 {
690   return myDlg;
691 }
692
693 //================================================================================
694 /*
695 * \brief Process InteractiveSelectionChanged event
696 */
697 //================================================================================
698 void SMESHGUI_AddNodeOnFaceOp::processStyleEvents(unsigned long theEvent, void* theCallData)
699 {
700   (void*)theCallData;
701   QString msg;
702   if (isValid(msg)) {
703     if (theEvent == SVTK::InteractiveSelectionChanged) {
704       if (myDlg->myPointOnFace->isChecked()) {
705         // Pick point on a mesh surface
706         QString msg;
707         if (myMeshActor && isValid(msg)) {
708           SMESH::smIdType id = myDlg->myId->text().toLong();
709           if (id > 0) {
710             if (SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
711               if (const SMDS_MeshElement* face = aMesh->FindElement(id))
712               {
713                 if (face->GetType() == SMDSAbs_Face) {
714                   int xClick, yClick; // Last event (move or left button bown) position
715                   myRWInteractor->GetDevice()->GetEventPosition(xClick, yClick);
716                   gp_XYZ faceNode(0, 0, 0);
717                   SMDS_Mesh tmp;
718                   double Ni[3];
719                   std::vector<const SMDS_MeshNode*> tmpNodes;
720                   for (int i = 0; i < face->NbCornerNodes(); ++i) {
721                     faceNode = SMESH_NodeXYZ(face->GetNode(i));
722                     vtkInteractorObserver::ComputeWorldToDisplay(myRWInteractor->GetRenderer()->GetDevice(),
723                       faceNode.X(), faceNode.Y(), faceNode.Z(), Ni);
724                     tmpNodes.push_back(tmp.AddNode(Ni[0], Ni[1], 0));
725                   }
726                   SMDS_MeshFace* face2D = tmp.AddPolygonalFace(tmpNodes);
727                   gp_Pnt point(double(xClick), double(yClick), 0);
728                   gp_XYZ closestPnt;
729                   double dist = SMESH_MeshAlgos::GetDistance(face2D, point, &closestPnt);
730                   double xPick = 0, yPick = 0;
731                   if (dist < TOLERANCE) {
732                     xPick = xClick;
733                     yPick = yClick;
734                   }
735                   else {
736                     xPick = closestPnt.X();
737                     yPick = closestPnt.Y();
738                   }
739                   myFacePicker->Pick(xPick, yPick, 0.0, myRWInteractor->GetRenderer()->GetDevice());
740                   double closest[3];
741                   myFacePicker->GetPickPosition(closest);
742                   myUpdateDestination = true;
743                   myDlg->myDestinationX->SetValue(closest[0]);
744                   myDlg->myDestinationY->SetValue(closest[1]);
745                   myDlg->myDestinationZ->SetValue(closest[2]);
746                   myUpdateDestination = false;
747                   redisplayPreview();
748                 }
749               }
750             }
751           }
752         }
753       }
754       else {
755         double* aCoord = (double*)theCallData;
756         myUpdateDestination = true;
757         myDlg->myDestinationX->SetValue(aCoord[0]);
758         myDlg->myDestinationY->SetValue(aCoord[1]);
759         myDlg->myDestinationZ->SetValue(aCoord[2]);
760         myUpdateDestination = false;
761         redisplayPreview();
762       }
763     }
764   }
765 }
766
767 //================================================================================
768 /*
769 * \brief Process LeftButtonPressEvent event: activate interactive selection
770 */
771 //================================================================================
772 void SMESHGUI_AddNodeOnFaceOp::processInteractorEvents(unsigned long theEvent, void* theCallData)
773 {
774   (void*)theCallData;
775   if (theEvent == vtkCommand::LeftButtonPressEvent && myDlg->myDestBtn->isChecked()) {
776     bool control = myRWInteractor->GetDevice()->GetControlKey();
777     bool shift = myRWInteractor->GetDevice()->GetControlKey();
778     SVTK_ViewWindow* svtkViewWindow = SMESH::GetViewWindow(mySMESHGUI);
779     if (svtkViewWindow && !shift && !control) {
780       QString msg;
781       if (isValid(msg)) {
782         svtkViewWindow->activateInteractiveSelection();
783       }
784     }
785   }
786 }