Salome HOME
PAL13312 (EDF262 SMESH mesh concatenation operation needed):
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MakeNodeAtPointDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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.
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 //
24 //  File   : SMESHGUI_MakeNodeAtPointDlg.cxx
25 //  Author : Edward AGAPOV
26 //  Module : SMESH
27
28 #include "SMESHGUI_MakeNodeAtPointDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_GEOMGenUtils.h"
32 #include "SMESHGUI_IdValidator.h"
33 #include "SMESHGUI_MeshUtils.h"
34 #include "SMESHGUI_Utils.h"
35 #include "SMESHGUI_VTKUtils.h"
36 #include "SMESHGUI_SpinBox.h"
37 #include "SMESHGUI_MeshEditPreview.h"
38
39 #include "SMDS_Mesh.hxx"
40 #include "SMDS_MeshNode.hxx"
41 #include "SMESH_Actor.h"
42 #include "SMESH_ActorUtils.h"
43
44 #include "GEOMBase.h"
45 #include "GeometryGUI.h"
46
47 #include "LightApp_DataOwner.h"
48 #include "LightApp_SelectionMgr.h"
49 #include "SALOMEDSClient_SObject.hxx"
50 #include "SALOME_ListIO.hxx"
51 #include "SUIT_Desktop.h"
52 #include "SVTK_Selector.h"
53 #include "SVTK_ViewWindow.h"
54 #include "SVTK_ViewModel.h"
55 #include "SalomeApp_Tools.h"
56 #include "SUIT_ResourceMgr.h"
57 #include "SUIT_OverrideCursor.h"
58 #include "SUIT_MessageBox.h"
59
60 // OCCT Includes
61 #include <TColStd_MapOfInteger.hxx>
62 #include <TopoDS_Shape.hxx>
63 #include <TopExp_Explorer.hxx>
64
65 // QT Includes
66 #include <qframe.h>
67 #include <qlayout.h>
68 #include <qlineedit.h>
69 #include <qpushbutton.h>
70 #include <qlabel.h>
71 #include <qradiobutton.h>
72 #include <qbuttongroup.h>
73 #include <qapplication.h>
74 #include <qstringlist.h>
75 #include <qcheckbox.h>
76 #include <qmessagebox.h>
77
78 #include <vtkProperty.h>
79
80 // IDL Headers
81 #include <SALOMEconfig.h>
82 #include CORBA_SERVER_HEADER(SMESH_Mesh)
83 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
84
85 #define SPACING 5
86 #define MARGIN  10
87
88 /*!
89  * \brief Dialog to publish a sub-shape of the mesh main shape
90  *        by selecting mesh elements
91  */
92 SMESHGUI_MakeNodeAtPointDlg::SMESHGUI_MakeNodeAtPointDlg()
93   : SMESHGUI_Dialog( 0, false, true )
94 {
95   setCaption(tr("CAPTION"));
96
97   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame(), MARGIN, SPACING);
98
99   QFrame* aMainFrame = createMainFrame  (mainFrame());
100
101   aDlgLay->addWidget(aMainFrame);
102
103   aDlgLay->setStretchFactor(aMainFrame, 1);
104 }
105
106 //=======================================================================
107 // function : createMainFrame()
108 // purpose  : Create frame containing dialog's input fields
109 //=======================================================================
110 QFrame* SMESHGUI_MakeNodeAtPointDlg::createMainFrame (QWidget* theParent)
111 {
112   QFrame* aFrame = new QFrame(theParent);
113
114   SUIT_ResourceMgr* rm = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
115   QPixmap iconMoveNode (rm->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
116   QPixmap iconSelect   (rm->loadPixmap("SMESH", tr("ICON_SELECT")));
117
118   // constructor
119
120   QButtonGroup* aPixGrp = new QButtonGroup(1, Qt::Vertical, tr("MESH_PASS_THROUGH_POINT"), aFrame);
121   aPixGrp->setExclusive(TRUE);
122   QRadioButton* aRBut = new QRadioButton(aPixGrp);
123   aRBut->setPixmap(iconMoveNode);
124   aRBut->setChecked(TRUE);
125
126   // coordinates
127
128   QGroupBox* aCoordGrp = new QGroupBox(1, Qt::Vertical, tr("SMESH_COORDINATES"), aFrame);
129   myCoordBtn = new QPushButton(aCoordGrp);
130   myCoordBtn->setPixmap(iconSelect);
131   myCoordBtn->setToggleButton(TRUE);
132
133   QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
134   aXLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
135   myX = new SMESHGUI_SpinBox(aCoordGrp);
136
137   QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
138   aYLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
139   myY = new SMESHGUI_SpinBox(aCoordGrp);
140
141   QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
142   aZLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
143   myZ = new SMESHGUI_SpinBox(aCoordGrp);
144
145   myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, 3);
146   myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, 3);
147   myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, 3);
148
149   // Method selection
150
151   QButtonGroup* aMethodGrp = new QButtonGroup(1, Qt::Vertical, tr("METHOD"), aFrame);
152   aMethodGrp->setExclusive(TRUE);
153   myMoveRBtn = new QRadioButton(tr("MOVE_EXISTING_METHOD"), aMethodGrp);
154   myCreateRBtn = new QRadioButton(tr("CREATE_NEW_METHOD"), aMethodGrp);
155
156   // node ID
157
158   myNodeToMoveGrp = new QGroupBox(tr("NODE_2MOVE"), aFrame, "anIdGrp");
159
160   QLabel* idLabel = new QLabel(tr("NODE_2MOVE_ID"), myNodeToMoveGrp, "idLabel");
161   myIdBtn = new QPushButton(myNodeToMoveGrp);
162   myIdBtn->setPixmap(iconSelect);
163   myIdBtn->setToggleButton(TRUE);
164   myId = new QLineEdit(myNodeToMoveGrp,"myId");
165   myId->setValidator(new SMESHGUI_IdValidator(this, "validator", 1));
166   myAutoSearchChkBox = new QCheckBox( tr("AUTO_SEARCH"), myNodeToMoveGrp, "myAutoSearchChkBox");
167   myPreviewChkBox = new QCheckBox( tr("PREVIEW"), myNodeToMoveGrp, "myPreviewChkBox");
168
169   myNodeToMoveGrp->setColumnLayout(0, Qt::Vertical);
170   myNodeToMoveGrp->layout()->setSpacing(0);
171   myNodeToMoveGrp->layout()->setMargin(0);
172   QGridLayout* myNodeToMoveGrpLayout = new QGridLayout(myNodeToMoveGrp->layout());
173   myNodeToMoveGrpLayout->setAlignment(Qt::AlignTop);
174   myNodeToMoveGrpLayout->setSpacing(SPACING);
175   myNodeToMoveGrpLayout->setMargin(MARGIN);
176   myNodeToMoveGrpLayout->addWidget( idLabel, 0, 0 );
177   myNodeToMoveGrpLayout->addWidget( myIdBtn, 0, 1 );
178   myNodeToMoveGrpLayout->addWidget( myId,    0, 2 );
179   myNodeToMoveGrpLayout->addMultiCellWidget( myAutoSearchChkBox, 1, 1, 0, 2 );
180   myNodeToMoveGrpLayout->addMultiCellWidget( myPreviewChkBox,    2, 2, 0, 2 );
181
182   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
183   aLay->addWidget(aPixGrp);
184   aLay->addWidget(aCoordGrp);
185   aLay->addWidget(aMethodGrp);
186   aLay->addWidget(myNodeToMoveGrp);
187
188   connect(myCoordBtn,         SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
189   connect(myMoveRBtn,         SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
190   connect(myCreateRBtn,       SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
191   connect(myIdBtn,            SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
192   connect(myAutoSearchChkBox, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
193
194   myMoveRBtn->setChecked(TRUE);
195   myIdBtn->setOn(TRUE);
196   myAutoSearchChkBox->setChecked(TRUE);
197
198   return aFrame;
199 }
200
201 //================================================================================
202 /*!
203  * \brief SLOT called when any button is toggled
204   * \param bool - on or off
205  */
206 //================================================================================
207
208 void SMESHGUI_MakeNodeAtPointDlg::ButtonToggled (bool on)
209 {
210   const QObject* aSender = sender();
211   if ( on ) {
212     if ( aSender == myCoordBtn ) // button to set coord by node selection
213     {
214       if ( myIdBtn->isEnabled() )
215         myIdBtn->setOn( !on );
216     }
217     else if ( aSender == myIdBtn ) // button to select a node to move
218     {
219       myCoordBtn->setOn( !on );
220     }
221     else if ( aSender == myMoveRBtn ) // move node method
222     {
223       myNodeToMoveGrp->setEnabled( TRUE );
224     }
225     else if ( aSender == myCreateRBtn ) // create node method
226     {
227       myNodeToMoveGrp->setEnabled( FALSE );
228       myCoordBtn->setOn( TRUE ); 
229     }
230   }      
231   if ( aSender == myAutoSearchChkBox ) // automatic node search
232   {
233     if ( on ) {
234       myId->setText("");
235       myId->setReadOnly ( TRUE );
236       myIdBtn->setOn( FALSE );
237       myIdBtn->setEnabled( FALSE );
238       myCoordBtn->setOn( TRUE );
239     }
240     else {
241       myId->setReadOnly ( FALSE );
242       myIdBtn->setEnabled( TRUE );
243     }
244   }
245 }
246
247 //================================================================================
248 /*!
249  * \brief Constructor
250 */
251 //================================================================================
252
253 SMESHGUI_MakeNodeAtPointOp::SMESHGUI_MakeNodeAtPointOp()
254 {
255   mySimulation = 0;
256   myDlg = new SMESHGUI_MakeNodeAtPointDlg;
257
258   // connect signals and slots
259   connect(myDlg->myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
260   connect(myDlg->myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
261   connect(myDlg->myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
262   connect(myDlg->myId,SIGNAL (textChanged(const QString&)),SLOT(redisplayPreview()));
263   connect(myDlg->myPreviewChkBox,   SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
264   connect(myDlg->myAutoSearchChkBox,SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
265   connect(myDlg->myMoveRBtn,        SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
266   connect(myDlg->myCreateRBtn,      SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
267 }
268
269 //=======================================================================
270 // function : startOperation()
271 // purpose  : Init dialog fields, connect signals and slots, show dialog
272 //=======================================================================
273 void SMESHGUI_MakeNodeAtPointOp::startOperation()
274 {
275   myNoPreview = false;
276   myMeshActor = 0;
277
278   // init simulation with a current View
279   if ( mySimulation ) delete mySimulation;
280   mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
281   vtkProperty* aProp = vtkProperty::New();
282   aProp->SetRepresentationToWireframe();
283   aProp->SetColor(250, 0, 250);
284   aProp->SetPointSize(5);
285   aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
286   mySimulation->GetActor()->SetProperty(aProp);
287   aProp->Delete();
288
289   SMESHGUI_SelectionOp::startOperation();
290
291   activateSelection(); // set filters
292
293   myDlg->myX->SetValue(0);
294   myDlg->myY->SetValue(0);
295   myDlg->myZ->SetValue(0);
296   myDlg->myId->setText("");
297   myDlg->show();
298
299   onSelectionDone(); // init myMeshActor
300
301   if ( myMeshActor ) {
302 //     myMeshOldDisplayMode = myMeshActor->GetRepresentation();
303 //     myMeshActor->SetRepresentation( VTK_WIREFRAME );
304     myMeshActor->SetPointRepresentation(true);
305     SMESH::RepaintCurrentView();
306     redisplayPreview();
307   }
308 }
309
310 //================================================================================
311 /*!
312  * \brief Stops operation
313  */
314 //================================================================================
315
316 void SMESHGUI_MakeNodeAtPointOp::stopOperation()
317 {
318   myNoPreview = true;
319   mySimulation->SetVisibility(false);
320   if ( myMeshActor ) {
321 //     myMeshActor->SetRepresentation( myMeshOldDisplayMode );
322     myMeshActor->SetPointRepresentation(false);
323     SMESH::RepaintCurrentView();
324     myMeshActor = 0;
325   }
326   SMESHGUI_SelectionOp::stopOperation();
327 }
328
329 //================================================================================
330 /*!
331  * \brief perform it's intention action: move or create a node
332  */
333 //================================================================================
334
335 bool SMESHGUI_MakeNodeAtPointOp::onApply()
336 {
337   if ( !myMeshActor ) {
338     SUIT_MessageBox::warn1( dlg(), tr( "SMESH_WRN_WARNING" ),
339                             tr("INVALID_MESH"), tr( "SMESH_BUT_OK" ) );
340     dlg()->show();
341     return false;
342   }
343
344   if ( !isValid() ) { // node id is invalid
345     SUIT_MessageBox::warn1( dlg(), tr( "SMESH_WRN_WARNING" ),
346                             tr("INVALID_ID"), tr( "SMESH_BUT_OK" ) );
347     dlg()->show();
348     return false;
349   }
350
351
352   try {
353     SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
354     if (aMesh->_is_nil()) {
355       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
356                                tr("SMESHG_NO_MESH"), QMessageBox::Ok);
357       return true;
358     }
359     SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
360     if (aMeshEditor->_is_nil())
361       return true;
362
363     int aResult = 0;
364     if ( myDlg->myCreateRBtn->isOn() )
365     {
366       aResult = aMeshEditor->AddNode(myDlg->myX->GetValue(),
367                                      myDlg->myY->GetValue(),
368                                      myDlg->myZ->GetValue());
369     }
370     else
371     {
372       int anId = myDlg->myId->text().toInt();
373       aResult = aMeshEditor->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
374                                                     myDlg->myY->GetValue(),
375                                                     myDlg->myZ->GetValue(),
376                                                     anId);
377     }
378     if (aResult)
379     {
380       myDlg->myId->setText("");
381       
382       SALOME_ListIO aList;
383       aList.Append(myMeshActor->getIO());
384       selectionMgr()->setSelectedObjects(aList,false);
385       SMESH::UpdateView();
386     }
387   }
388   catch (const SALOME::SALOME_Exception& S_ex) {
389     SalomeApp_Tools::QtCatchCorbaException(S_ex);
390   }
391   catch (...) {
392   }
393
394   return true;
395 }
396
397 //================================================================================
398 /*!
399  * \brief Check selected node id validity
400  */
401 //================================================================================
402
403 bool SMESHGUI_MakeNodeAtPointOp::isValid()
404 {
405   bool ok = true;
406
407   if ( myMeshActor &&
408        myDlg->myMoveRBtn->isOn() &&
409        !myDlg->myAutoSearchChkBox->isChecked() )
410   {
411     ok = false;
412     int id = myDlg->myId->text().toInt();
413     if ( id > 0 )
414       if (SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh())
415         ok = aMesh->FindNode( id );
416   }
417   return ok;
418 }
419
420 //================================================================================
421 /*!
422  * \brief SLOT called when selection changed
423  */
424 //================================================================================
425
426 void SMESHGUI_MakeNodeAtPointOp::onSelectionDone()
427 {
428   if ( !myDlg->isShown() || !myDlg->isEnabled() )
429     return;
430   try {
431     SALOME_ListIO aList;
432     selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
433     if (aList.Extent() != 1)
434       return;
435     Handle(SALOME_InteractiveObject) anIO = aList.First();
436     SMESH_Actor* aMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
437     if (!aMeshActor)
438       return;
439     if ( !myMeshActor )
440       myMeshActor = aMeshActor;
441     QString aString;
442     int nbElems = SMESH::GetNameOfSelectedElements(selector(),anIO, aString);
443     if (nbElems == 1) {
444       if (SMDS_Mesh* aMesh = aMeshActor->GetObject()->GetMesh()) {
445         if (const SMDS_MeshNode* aNode = aMesh->FindNode(aString.toInt())) {
446           myNoPreview = true;
447           if ( myDlg->myCoordBtn->isOn() ) { // set coord
448             myDlg->myX->SetValue(aNode->X());
449             myDlg->myY->SetValue(aNode->Y());
450             myDlg->myZ->SetValue(aNode->Z());
451             myNoPreview = false;
452             redisplayPreview();
453           }
454           else if ( myDlg->myIdBtn->isOn() &&
455                     myDlg->myIdBtn->isEnabled() ) { // set node to move
456             myDlg->myId->setText(aString);
457             myNoPreview = false;
458             redisplayPreview();
459           }
460         }
461       }
462     }
463   } catch (...) {
464   }
465 }
466
467 //================================================================================
468 /*!
469  * \brief update preview
470  */
471 //================================================================================
472
473 void SMESHGUI_MakeNodeAtPointOp::redisplayPreview()
474 {
475   if ( myNoPreview )
476     return;
477   myNoPreview = true;
478
479   SMESH::MeshPreviewStruct_var aMeshPreviewStruct;
480
481   bool moveShown = false;
482   if ( myDlg->myMoveRBtn->isOn() && // Move method
483        myMeshActor)
484   {
485     const bool autoSearch = myDlg->myAutoSearchChkBox->isChecked();
486     const bool preview    = myDlg->myPreviewChkBox->isChecked();
487     if ( autoSearch )
488       myDlg->myId->setText("");
489     if ( preview && ( autoSearch || isValid() ))
490     {
491       try {
492         SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
493         if (!aMesh->_is_nil()) {
494           SMESH::SMESH_MeshEditor_var aPreviewer = aMesh->GetMeshEditPreviewer();
495           if (!aPreviewer->_is_nil())
496           {
497             SUIT_OverrideCursor aWaitCursor;
498
499             // find id and/or just compute preview
500             int anId = aPreviewer->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
501                                                           myDlg->myY->GetValue(),
502                                                           myDlg->myZ->GetValue(),
503                                                           myDlg->myId->text().toInt());
504             if ( autoSearch ) { // set found id
505               QString idTxt("%1");
506               if ( anId > 0 )
507                 idTxt = idTxt.arg( anId );
508               else
509                 idTxt = "";
510               myDlg->myId->setText( idTxt );
511             }
512             if ( preview ) { // fill preview data
513               aMeshPreviewStruct = aPreviewer->GetPreviewData();
514               moveShown = ( anId > 0 );
515             }
516           }
517         }
518       }catch (...) {
519       }
520     }
521   }
522
523   if ( !moveShown )
524   {
525     aMeshPreviewStruct = new SMESH::MeshPreviewStruct();
526
527     aMeshPreviewStruct->nodesXYZ.length(1);
528     aMeshPreviewStruct->nodesXYZ[0].x = myDlg->myX->GetValue();
529     aMeshPreviewStruct->nodesXYZ[0].y = myDlg->myY->GetValue();
530     aMeshPreviewStruct->nodesXYZ[0].z = myDlg->myZ->GetValue();
531
532     aMeshPreviewStruct->elementTypes.length(1);
533     aMeshPreviewStruct->elementTypes[0].SMDS_ElementType = SMESH::NODE;
534     aMeshPreviewStruct->elementTypes[0].isPoly = false;
535     aMeshPreviewStruct->elementTypes[0].nbNodesInElement = 1;
536
537     aMeshPreviewStruct->elementConnectivities.length(1);
538     aMeshPreviewStruct->elementConnectivities[0] = 0;
539   }
540
541   // display data
542   if ( aMeshPreviewStruct.operator->() )
543   {
544     mySimulation->SetData(aMeshPreviewStruct._retn());
545   }
546   else
547   {
548     mySimulation->SetVisibility(false);
549   }
550
551   myNoPreview = false;
552 }
553
554 //================================================================================
555 /*!
556  * \brief Activate Node selection
557  */
558 //================================================================================
559
560 void SMESHGUI_MakeNodeAtPointOp::activateSelection()
561 {
562   selectionMgr()->clearFilters();
563   SMESH::SetPointRepresentation(false);
564   setSelectionMode( NodeSelection );
565 }
566
567 //================================================================================
568 /*!
569  * \brief Destructor
570 */
571 //================================================================================
572
573 SMESHGUI_MakeNodeAtPointOp::~SMESHGUI_MakeNodeAtPointOp()
574 {
575   if ( myDlg )
576     delete myDlg;
577   delete mySimulation;
578 }
579
580 //================================================================================
581 /*!
582  * \brief Gets dialog of this operation
583  * \retval LightApp_Dialog* - pointer to dialog of this operation
584  */
585 //================================================================================
586
587 LightApp_Dialog* SMESHGUI_MakeNodeAtPointOp::dlg() const
588 {
589   return myDlg;
590 }
591