1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SMESHGUI_MakeNodeAtPointDlg.cxx
21 // Author : Edward AGAPOV, Open CASCADE S.A.S.
25 #include "SMESHGUI_MakeNodeAtPointDlg.h"
28 #include "SMESHGUI_IdValidator.h"
29 #include "SMESHGUI_MeshUtils.h"
30 #include "SMESHGUI_VTKUtils.h"
31 #include "SMESHGUI_SpinBox.h"
32 #include "SMESHGUI_MeshEditPreview.h"
34 #include <SMDS_Mesh.hxx>
35 #include <SMESH_Actor.h>
36 #include <SMESH_ActorUtils.h>
37 #include <SMESH_NumberFilter.hxx>
38 #include <SMESH_LogicalFilter.hxx>
40 // SALOME GEOM includes
43 // SALOME GUI includes
44 #include <LightApp_SelectionMgr.h>
45 #include <SALOME_ListIO.hxx>
46 #include <SUIT_Desktop.h>
47 #include <SVTK_ViewModel.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>
55 #include <TColStd_MapOfInteger.hxx>
56 #include <TopoDS_Vertex.hxx>
57 #include <BRep_Tool.hxx>
62 #include <QGridLayout>
63 #include <QHBoxLayout>
64 #include <QVBoxLayout>
66 #include <QPushButton>
68 #include <QRadioButton>
70 #include <QButtonGroup>
73 #include <vtkProperty.h>
76 #include <SALOMEconfig.h>
77 #include CORBA_SERVER_HEADER(SMESH_Mesh)
78 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
84 * \brief Dialog to publish a sub-shape of the mesh main shape
85 * by selecting mesh elements
87 SMESHGUI_MakeNodeAtPointDlg::SMESHGUI_MakeNodeAtPointDlg()
88 : SMESHGUI_Dialog( 0, false, true )
90 setWindowTitle(tr("CAPTION"));
92 QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
93 aDlgLay->setMargin(MARGIN);;
94 aDlgLay->setSpacing(SPACING);
96 QWidget* aMainFrame = createMainFrame (mainFrame());
98 aDlgLay->addWidget(aMainFrame);
100 aDlgLay->setStretchFactor(aMainFrame, 1);
103 //=======================================================================
104 // function : createMainFrame()
105 // purpose : Create frame containing dialog's input fields
106 //=======================================================================
107 QWidget* SMESHGUI_MakeNodeAtPointDlg::createMainFrame (QWidget* theParent)
109 QWidget* aFrame = new QWidget(theParent);
111 SUIT_ResourceMgr* rm = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
112 QPixmap iconMoveNode (rm->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
113 QPixmap iconSelect (rm->loadPixmap("SMESH", tr("ICON_SELECT")));
117 QGroupBox* aPixGrp = new QGroupBox(tr("MESH_PASS_THROUGH_POINT"), aFrame);
118 QButtonGroup* aBtnGrp = new QButtonGroup(this);
119 QHBoxLayout* aPixGrpLayout = new QHBoxLayout(aPixGrp);
120 aPixGrpLayout->setMargin(MARGIN);
121 aPixGrpLayout->setSpacing(SPACING);
123 QRadioButton* aRBut = new QRadioButton(aPixGrp);
124 aRBut->setIcon(iconMoveNode);
125 aRBut->setChecked(true);
126 aPixGrpLayout->addWidget(aRBut);
127 aBtnGrp->addButton(aRBut, 0);
131 QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
132 QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
133 aCoordGrpLayout->setMargin(MARGIN);
134 aCoordGrpLayout->setSpacing(SPACING);
136 myCoordBtn = new QPushButton(aCoordGrp);
137 myCoordBtn->setIcon(iconSelect);
138 myCoordBtn->setCheckable(true);
140 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
141 myX = new SMESHGUI_SpinBox(aCoordGrp);
143 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
144 myY = new SMESHGUI_SpinBox(aCoordGrp);
146 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
147 myZ = new SMESHGUI_SpinBox(aCoordGrp);
149 myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
150 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
151 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
153 aCoordGrpLayout->addWidget(myCoordBtn);
154 aCoordGrpLayout->addWidget(aXLabel);
155 aCoordGrpLayout->addWidget(myX);
156 aCoordGrpLayout->addWidget(aYLabel);
157 aCoordGrpLayout->addWidget(myY);
158 aCoordGrpLayout->addWidget(aZLabel);
159 aCoordGrpLayout->addWidget(myZ);
163 QGroupBox* aMethodGrp = new QGroupBox(tr("METHOD"), aFrame);
164 QHBoxLayout* aMethodGrpLayout = new QHBoxLayout(aMethodGrp);
165 aMethodGrpLayout->setMargin(MARGIN);
166 aMethodGrpLayout->setSpacing(SPACING);
168 myMoveRBtn = new QRadioButton(tr("MOVE_EXISTING_METHOD"), aMethodGrp);
169 myCreateRBtn = new QRadioButton(tr("CREATE_NEW_METHOD"), aMethodGrp);
171 aMethodGrpLayout->addWidget(myMoveRBtn);
172 aMethodGrpLayout->addWidget(myCreateRBtn);
176 myNodeToMoveGrp = new QGroupBox(tr("NODE_2MOVE"), aFrame);
178 QLabel* idLabel = new QLabel(tr("NODE_2MOVE_ID"), myNodeToMoveGrp);
179 myIdBtn = new QPushButton(myNodeToMoveGrp);
180 myIdBtn->setIcon(iconSelect);
181 myIdBtn->setCheckable(true);
182 myId = new QLineEdit(myNodeToMoveGrp);
183 myId->setValidator(new SMESHGUI_IdValidator(this, 1));
184 myAutoSearchChkBox = new QCheckBox( tr("AUTO_SEARCH"), myNodeToMoveGrp);
185 myPreviewChkBox = new QCheckBox( tr("PREVIEW"), myNodeToMoveGrp);
187 QGridLayout* myNodeToMoveGrpLayout = new QGridLayout(myNodeToMoveGrp);
188 myNodeToMoveGrpLayout->setSpacing(SPACING);
189 myNodeToMoveGrpLayout->setMargin(MARGIN);
191 myNodeToMoveGrpLayout->addWidget( idLabel, 0, 0 );
192 myNodeToMoveGrpLayout->addWidget( myIdBtn, 0, 1 );
193 myNodeToMoveGrpLayout->addWidget( myId, 0, 2 );
194 myNodeToMoveGrpLayout->addWidget( myAutoSearchChkBox, 1, 0, 1, 3 );
195 myNodeToMoveGrpLayout->addWidget( myPreviewChkBox, 2, 0, 1, 3 );
197 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
198 aLay->addWidget(aPixGrp);
199 aLay->addWidget(aCoordGrp);
200 aLay->addWidget(aMethodGrp);
201 aLay->addWidget(myNodeToMoveGrp);
203 connect(myCoordBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
204 connect(myMoveRBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
205 connect(myCreateRBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
206 connect(myIdBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
207 connect(myAutoSearchChkBox, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
209 myMoveRBtn->setChecked(true);
210 myIdBtn->setDown(true);
211 myAutoSearchChkBox->setChecked(true);
216 //================================================================================
218 * \brief SLOT called when any button is toggled
219 * \param bool - on or off
221 //================================================================================
223 void SMESHGUI_MakeNodeAtPointDlg::ButtonToggled (bool on)
225 const QObject* aSender = sender();
227 if ( aSender == myCoordBtn ) // button to set coord by node selection
229 if ( myIdBtn->isEnabled() )
230 myIdBtn->setDown( !on );
232 else if ( aSender == myIdBtn ) // button to select a node to move
234 myCoordBtn->setDown( !on );
236 else if ( aSender == myMoveRBtn ) // move node method
238 myNodeToMoveGrp->setEnabled( true );
240 else if ( aSender == myCreateRBtn ) // create node method
242 myNodeToMoveGrp->setEnabled( false );
243 myCoordBtn->setDown( true );
246 if ( aSender == myAutoSearchChkBox ) // automatic node search
250 myId->setReadOnly ( true );
251 myIdBtn->setDown( false );
252 myIdBtn->setEnabled( false );
253 myCoordBtn->setDown( true );
256 myId->setReadOnly ( false );
257 myIdBtn->setEnabled( true );
262 //================================================================================
266 //================================================================================
268 SMESHGUI_MakeNodeAtPointOp::SMESHGUI_MakeNodeAtPointOp()
271 myDlg = new SMESHGUI_MakeNodeAtPointDlg;
273 myHelpFileName = "mesh_through_point_page.html";
275 // connect signals and slots
276 connect(myDlg->myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
277 connect(myDlg->myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
278 connect(myDlg->myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
279 connect(myDlg->myId,SIGNAL (textChanged(const QString&)),SLOT(redisplayPreview()));
280 connect(myDlg->myPreviewChkBox, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
281 connect(myDlg->myAutoSearchChkBox,SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
282 connect(myDlg->myMoveRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
283 connect(myDlg->myCreateRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
286 //=======================================================================
287 // function : startOperation()
288 // purpose : Init dialog fields, connect signals and slots, show dialog
289 //=======================================================================
290 void SMESHGUI_MakeNodeAtPointOp::startOperation()
295 // init simulation with a current View
296 if ( mySimulation ) delete mySimulation;
297 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
298 vtkProperty* aProp = vtkProperty::New();
299 aProp->SetRepresentationToWireframe();
300 aProp->SetColor(250, 0, 250);
301 aProp->SetPointSize(5);
302 aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
303 mySimulation->GetActor()->SetProperty(aProp);
306 // SalomeApp_TypeFilter depends on a current study
307 if ( myFilter ) delete myFilter;
308 QList<SUIT_SelectionFilter*> filters;
309 filters.append( new SalomeApp_TypeFilter((SalomeApp_Study*)study(), "SMESH" ));
310 TColStd_MapOfInteger vertexType;
311 vertexType.Add( TopAbs_VERTEX );
312 filters.append( new SMESH_NumberFilter("GEOM", TopAbs_VERTEX, 1, vertexType ));
313 myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
316 SMESHGUI_SelectionOp::startOperation(); // this method should be called only after filter creation
317 //activateSelection(); // set filters // called inside of previous statement
319 myDlg->myX->SetValue(0);
320 myDlg->myY->SetValue(0);
321 myDlg->myZ->SetValue(0);
322 myDlg->myId->setText("");
325 onSelectionDone(); // init myMeshActor
328 // myMeshOldDisplayMode = myMeshActor->GetRepresentation();
329 // myMeshActor->SetRepresentation( VTK_WIREFRAME );
330 myMeshActor->SetPointRepresentation(true);
331 SMESH::RepaintCurrentView();
336 //================================================================================
338 * \brief Stops operation
340 //================================================================================
342 void SMESHGUI_MakeNodeAtPointOp::stopOperation()
345 mySimulation->SetVisibility(false);
347 // myMeshActor->SetRepresentation( myMeshOldDisplayMode );
348 myMeshActor->SetPointRepresentation(false);
349 SMESH::RepaintCurrentView();
352 selectionMgr()->removeFilter( myFilter );
353 SMESHGUI_SelectionOp::stopOperation();
356 //================================================================================
358 * \brief perform it's intention action: move or create a node
360 //================================================================================
362 bool SMESHGUI_MakeNodeAtPointOp::onApply()
364 if( isStudyLocked() )
367 if ( !myMeshActor ) {
368 SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ),
369 tr("INVALID_MESH") );
374 if ( !isValid() ) { // node id is invalid
375 SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ),
383 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
384 if (aMesh->_is_nil()) {
385 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
386 tr("SMESHG_NO_MESH") );
389 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
390 if (aMeshEditor->_is_nil())
394 if ( myDlg->myCreateRBtn->isDown() )
396 aResult = aMeshEditor->AddNode(myDlg->myX->GetValue(),
397 myDlg->myY->GetValue(),
398 myDlg->myZ->GetValue());
402 int anId = myDlg->myId->text().toInt();
403 aResult = aMeshEditor->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
404 myDlg->myY->GetValue(),
405 myDlg->myZ->GetValue(),
410 myDlg->myId->setText("");
413 selectionMgr()->setSelectedObjects(aList,false);
414 aList.Append(myMeshActor->getIO());
415 selectionMgr()->setSelectedObjects(aList,false);
419 catch (const SALOME::SALOME_Exception& S_ex) {
420 SalomeApp_Tools::QtCatchCorbaException(S_ex);
428 //================================================================================
430 * \brief Check selected node id validity
432 //================================================================================
434 bool SMESHGUI_MakeNodeAtPointOp::isValid()
439 myDlg->myMoveRBtn->isDown() &&
440 !myDlg->myAutoSearchChkBox->isChecked() )
443 int id = myDlg->myId->text().toInt();
445 if (SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh())
446 ok = aMesh->FindNode( id );
451 //================================================================================
453 * \brief SLOT called when selection changed
455 //================================================================================
457 void SMESHGUI_MakeNodeAtPointOp::onSelectionDone()
459 if ( !myDlg->isVisible() || !myDlg->isEnabled() )
463 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
464 if (aList.Extent() != 1)
466 Handle(SALOME_InteractiveObject) anIO = aList.First();
467 SMESH_Actor* aMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
469 if (!aMeshActor) { // coord by geom
470 if ( myDlg->myCoordBtn->isDown() ) {
471 GEOM::GEOM_Object_var geom = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
472 if ( !geom->_is_nil() ) {
473 TopoDS_Vertex aShape;
474 if ( GEOMBase::GetShape(geom, aShape) &&
475 aShape.ShapeType() == TopAbs_VERTEX ) {
476 gp_Pnt P = BRep_Tool::Pnt(aShape);
478 myDlg->myX->SetValue(P.X());
479 myDlg->myY->SetValue(P.Y());
480 myDlg->myZ->SetValue(P.Z());
490 myMeshActor = aMeshActor;
493 int nbElems = SMESH::GetNameOfSelectedElements(selector(),anIO, aString);
495 if (SMDS_Mesh* aMesh = aMeshActor->GetObject()->GetMesh()) {
496 if (const SMDS_MeshNode* aNode = aMesh->FindNode(aString.toInt())) {
498 if ( myDlg->myCoordBtn->isDown() ) { // set coord
499 myDlg->myX->SetValue(aNode->X());
500 myDlg->myY->SetValue(aNode->Y());
501 myDlg->myZ->SetValue(aNode->Z());
505 else if ( myDlg->myIdBtn->isDown() &&
506 myDlg->myIdBtn->isEnabled() ) { // set node to move
507 myDlg->myId->setText(aString);
518 //================================================================================
520 * \brief update preview
522 //================================================================================
524 void SMESHGUI_MakeNodeAtPointOp::redisplayPreview()
530 SMESH::MeshPreviewStruct_var aMeshPreviewStruct;
532 bool moveShown = false;
533 if ( myDlg->myMoveRBtn->isDown() && // Move method
536 const bool autoSearch = myDlg->myAutoSearchChkBox->isChecked();
537 const bool preview = myDlg->myPreviewChkBox->isChecked();
539 myDlg->myId->setText("");
540 if ( preview && ( autoSearch || isValid() ))
543 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
544 if (!aMesh->_is_nil()) {
545 SMESH::SMESH_MeshEditor_var aPreviewer = aMesh->GetMeshEditPreviewer();
546 if (!aPreviewer->_is_nil())
548 SUIT_OverrideCursor aWaitCursor;
550 // find id and/or just compute preview
551 int anId = aPreviewer->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
552 myDlg->myY->GetValue(),
553 myDlg->myZ->GetValue(),
554 myDlg->myId->text().toInt());
555 if ( autoSearch ) { // set found id
558 idTxt = idTxt.arg( anId );
561 myDlg->myId->setText( idTxt );
563 if ( preview ) { // fill preview data
564 aMeshPreviewStruct = aPreviewer->GetPreviewData();
565 moveShown = ( anId > 0 );
576 aMeshPreviewStruct = new SMESH::MeshPreviewStruct();
578 aMeshPreviewStruct->nodesXYZ.length(1);
579 aMeshPreviewStruct->nodesXYZ[0].x = myDlg->myX->GetValue();
580 aMeshPreviewStruct->nodesXYZ[0].y = myDlg->myY->GetValue();
581 aMeshPreviewStruct->nodesXYZ[0].z = myDlg->myZ->GetValue();
583 aMeshPreviewStruct->elementTypes.length(1);
584 aMeshPreviewStruct->elementTypes[0].SMDS_ElementType = SMESH::NODE;
585 aMeshPreviewStruct->elementTypes[0].isPoly = false;
586 aMeshPreviewStruct->elementTypes[0].nbNodesInElement = 1;
588 aMeshPreviewStruct->elementConnectivities.length(1);
589 aMeshPreviewStruct->elementConnectivities[0] = 0;
593 if ( aMeshPreviewStruct.operator->() )
595 mySimulation->SetData(aMeshPreviewStruct._retn());
599 mySimulation->SetVisibility(false);
605 //================================================================================
607 * \brief Activate Node selection
609 //================================================================================
611 void SMESHGUI_MakeNodeAtPointOp::activateSelection()
613 selectionMgr()->clearFilters();
614 SMESH::SetPointRepresentation(false);
615 selectionMgr()->installFilter( myFilter );
616 setSelectionMode( NodeSelection );
619 //================================================================================
623 //================================================================================
625 SMESHGUI_MakeNodeAtPointOp::~SMESHGUI_MakeNodeAtPointOp()
627 if ( myDlg ) delete myDlg;
628 if ( mySimulation ) delete mySimulation;
629 if ( myFilter ) delete myFilter;
632 //================================================================================
634 * \brief Gets dialog of this operation
635 * \retval LightApp_Dialog* - pointer to dialog of this operation
637 //================================================================================
639 LightApp_Dialog* SMESHGUI_MakeNodeAtPointOp::dlg() const