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>
72 #include <vtkProperty.h>
75 #include <SALOMEconfig.h>
76 #include CORBA_SERVER_HEADER(SMESH_Mesh)
77 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
83 * \brief Dialog to publish a sub-shape of the mesh main shape
84 * by selecting mesh elements
86 SMESHGUI_MakeNodeAtPointDlg::SMESHGUI_MakeNodeAtPointDlg()
87 : SMESHGUI_Dialog( 0, false, true )
89 setWindowTitle(tr("CAPTION"));
91 QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
92 aDlgLay->setMargin(MARGIN);;
93 aDlgLay->setSpacing(SPACING);
95 QWidget* aMainFrame = createMainFrame (mainFrame());
97 aDlgLay->addWidget(aMainFrame);
99 aDlgLay->setStretchFactor(aMainFrame, 1);
102 //=======================================================================
103 // function : createMainFrame()
104 // purpose : Create frame containing dialog's input fields
105 //=======================================================================
106 QWidget* SMESHGUI_MakeNodeAtPointDlg::createMainFrame (QWidget* theParent)
108 QWidget* aFrame = new QWidget(theParent);
110 SUIT_ResourceMgr* rm = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
111 QPixmap iconMoveNode (rm->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
112 QPixmap iconSelect (rm->loadPixmap("SMESH", tr("ICON_SELECT")));
116 QGroupBox* aPixGrp = new QGroupBox(tr("MESH_PASS_THROUGH_POINT"), aFrame);
117 QHBoxLayout* aPixGrpLayout = new QHBoxLayout(aPixGrp);
118 aPixGrpLayout->setMargin(MARGIN);
119 aPixGrpLayout->setSpacing(SPACING);
121 QRadioButton* aRBut = new QRadioButton(aPixGrp);
122 aRBut->setIcon(iconMoveNode);
123 aRBut->setChecked(true);
124 aPixGrpLayout->addWidget(aRBut);
128 QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
129 QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
130 aCoordGrpLayout->setMargin(MARGIN);
131 aCoordGrpLayout->setSpacing(SPACING);
133 myCoordBtn = new QPushButton(aCoordGrp);
134 myCoordBtn->setIcon(iconSelect);
135 myCoordBtn->setCheckable(true);
137 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
138 myX = new SMESHGUI_SpinBox(aCoordGrp);
140 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
141 myY = new SMESHGUI_SpinBox(aCoordGrp);
143 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
144 myZ = new SMESHGUI_SpinBox(aCoordGrp);
146 myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
147 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
148 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
150 aCoordGrpLayout->addWidget(myCoordBtn);
151 aCoordGrpLayout->addWidget(aXLabel);
152 aCoordGrpLayout->addWidget(myX);
153 aCoordGrpLayout->addWidget(aYLabel);
154 aCoordGrpLayout->addWidget(myY);
155 aCoordGrpLayout->addWidget(aZLabel);
156 aCoordGrpLayout->addWidget(myZ);
160 QGroupBox* aMethodGrp = new QGroupBox(tr("METHOD"), aFrame);
161 QHBoxLayout* aMethodGrpLayout = new QHBoxLayout(aMethodGrp);
162 aMethodGrpLayout->setMargin(MARGIN);
163 aMethodGrpLayout->setSpacing(SPACING);
165 myMoveRBtn = new QRadioButton(tr("MOVE_EXISTING_METHOD"), aMethodGrp);
166 myCreateRBtn = new QRadioButton(tr("CREATE_NEW_METHOD"), aMethodGrp);
168 aMethodGrpLayout->addWidget(myMoveRBtn);
169 aMethodGrpLayout->addWidget(myCreateRBtn);
173 myNodeToMoveGrp = new QGroupBox(tr("NODE_2MOVE"), aFrame);
175 QLabel* idLabel = new QLabel(tr("NODE_2MOVE_ID"), myNodeToMoveGrp);
176 myIdBtn = new QPushButton(myNodeToMoveGrp);
177 myIdBtn->setIcon(iconSelect);
178 myIdBtn->setCheckable(true);
179 myId = new QLineEdit(myNodeToMoveGrp);
180 myId->setValidator(new SMESHGUI_IdValidator(this, 1));
181 myAutoSearchChkBox = new QCheckBox( tr("AUTO_SEARCH"), myNodeToMoveGrp);
182 myPreviewChkBox = new QCheckBox( tr("PREVIEW"), myNodeToMoveGrp);
184 QGridLayout* myNodeToMoveGrpLayout = new QGridLayout(myNodeToMoveGrp);
185 myNodeToMoveGrpLayout->setSpacing(SPACING);
186 myNodeToMoveGrpLayout->setMargin(MARGIN);
188 myNodeToMoveGrpLayout->addWidget( idLabel, 0, 0 );
189 myNodeToMoveGrpLayout->addWidget( myIdBtn, 0, 1 );
190 myNodeToMoveGrpLayout->addWidget( myId, 0, 2 );
191 myNodeToMoveGrpLayout->addWidget( myAutoSearchChkBox, 1, 0, 1, 3 );
192 myNodeToMoveGrpLayout->addWidget( myPreviewChkBox, 2, 0, 1, 3 );
194 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
195 aLay->addWidget(aPixGrp);
196 aLay->addWidget(aCoordGrp);
197 aLay->addWidget(aMethodGrp);
198 aLay->addWidget(myNodeToMoveGrp);
200 connect(myCoordBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
201 connect(myMoveRBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
202 connect(myCreateRBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
203 connect(myIdBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
204 connect(myAutoSearchChkBox, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
206 myMoveRBtn->setChecked(true);
207 myIdBtn->setDown(true);
208 myAutoSearchChkBox->setChecked(true);
213 //================================================================================
215 * \brief SLOT called when any button is toggled
216 * \param bool - on or off
218 //================================================================================
220 void SMESHGUI_MakeNodeAtPointDlg::ButtonToggled (bool on)
222 const QObject* aSender = sender();
224 if ( aSender == myCoordBtn ) // button to set coord by node selection
226 if ( myIdBtn->isEnabled() )
227 myIdBtn->setDown( !on );
229 else if ( aSender == myIdBtn ) // button to select a node to move
231 myCoordBtn->setDown( !on );
233 else if ( aSender == myMoveRBtn ) // move node method
235 myNodeToMoveGrp->setEnabled( true );
237 else if ( aSender == myCreateRBtn ) // create node method
239 myNodeToMoveGrp->setEnabled( false );
240 myCoordBtn->setDown( true );
243 if ( aSender == myAutoSearchChkBox ) // automatic node search
247 myId->setReadOnly ( true );
248 myIdBtn->setDown( false );
249 myIdBtn->setEnabled( false );
250 myCoordBtn->setDown( true );
253 myId->setReadOnly ( false );
254 myIdBtn->setEnabled( true );
259 //================================================================================
263 //================================================================================
265 SMESHGUI_MakeNodeAtPointOp::SMESHGUI_MakeNodeAtPointOp()
268 myDlg = new SMESHGUI_MakeNodeAtPointDlg;
270 myHelpFileName = "mesh_through_point_page.html";
272 // connect signals and slots
273 connect(myDlg->myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
274 connect(myDlg->myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
275 connect(myDlg->myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
276 connect(myDlg->myId,SIGNAL (textChanged(const QString&)),SLOT(redisplayPreview()));
277 connect(myDlg->myPreviewChkBox, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
278 connect(myDlg->myAutoSearchChkBox,SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
279 connect(myDlg->myMoveRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
280 connect(myDlg->myCreateRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
283 //=======================================================================
284 // function : startOperation()
285 // purpose : Init dialog fields, connect signals and slots, show dialog
286 //=======================================================================
287 void SMESHGUI_MakeNodeAtPointOp::startOperation()
292 // init simulation with a current View
293 if ( mySimulation ) delete mySimulation;
294 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
295 vtkProperty* aProp = vtkProperty::New();
296 aProp->SetRepresentationToWireframe();
297 aProp->SetColor(250, 0, 250);
298 aProp->SetPointSize(5);
299 aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
300 mySimulation->GetActor()->SetProperty(aProp);
303 SMESHGUI_SelectionOp::startOperation();
305 // SalomeApp_TypeFilter depends on a current study
306 if ( myFilter ) delete myFilter;
307 QList<SUIT_SelectionFilter*> filters;
308 filters.append( new SalomeApp_TypeFilter((SalomeApp_Study*)study(), "SMESH" ));
309 TColStd_MapOfInteger vertexType;
310 vertexType.Add( TopAbs_VERTEX );
311 filters.append( new SMESH_NumberFilter("GEOM", TopAbs_VERTEX, 1, vertexType ));
312 myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
314 activateSelection(); // set filters
316 myDlg->myX->SetValue(0);
317 myDlg->myY->SetValue(0);
318 myDlg->myZ->SetValue(0);
319 myDlg->myId->setText("");
322 onSelectionDone(); // init myMeshActor
325 // myMeshOldDisplayMode = myMeshActor->GetRepresentation();
326 // myMeshActor->SetRepresentation( VTK_WIREFRAME );
327 myMeshActor->SetPointRepresentation(true);
328 SMESH::RepaintCurrentView();
333 //================================================================================
335 * \brief Stops operation
337 //================================================================================
339 void SMESHGUI_MakeNodeAtPointOp::stopOperation()
342 mySimulation->SetVisibility(false);
344 // myMeshActor->SetRepresentation( myMeshOldDisplayMode );
345 myMeshActor->SetPointRepresentation(false);
346 SMESH::RepaintCurrentView();
349 selectionMgr()->removeFilter( myFilter );
350 SMESHGUI_SelectionOp::stopOperation();
353 //================================================================================
355 * \brief perform it's intention action: move or create a node
357 //================================================================================
359 bool SMESHGUI_MakeNodeAtPointOp::onApply()
361 if( isStudyLocked() )
364 if ( !myMeshActor ) {
365 SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ),
366 tr("INVALID_MESH") );
371 if ( !isValid() ) { // node id is invalid
372 SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ),
380 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
381 if (aMesh->_is_nil()) {
382 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
383 tr("SMESHG_NO_MESH") );
386 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
387 if (aMeshEditor->_is_nil())
391 if ( myDlg->myCreateRBtn->isDown() )
393 aResult = aMeshEditor->AddNode(myDlg->myX->GetValue(),
394 myDlg->myY->GetValue(),
395 myDlg->myZ->GetValue());
399 int anId = myDlg->myId->text().toInt();
400 aResult = aMeshEditor->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
401 myDlg->myY->GetValue(),
402 myDlg->myZ->GetValue(),
407 myDlg->myId->setText("");
410 selectionMgr()->setSelectedObjects(aList,false);
411 aList.Append(myMeshActor->getIO());
412 selectionMgr()->setSelectedObjects(aList,false);
416 catch (const SALOME::SALOME_Exception& S_ex) {
417 SalomeApp_Tools::QtCatchCorbaException(S_ex);
425 //================================================================================
427 * \brief Check selected node id validity
429 //================================================================================
431 bool SMESHGUI_MakeNodeAtPointOp::isValid()
436 myDlg->myMoveRBtn->isDown() &&
437 !myDlg->myAutoSearchChkBox->isChecked() )
440 int id = myDlg->myId->text().toInt();
442 if (SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh())
443 ok = aMesh->FindNode( id );
448 //================================================================================
450 * \brief SLOT called when selection changed
452 //================================================================================
454 void SMESHGUI_MakeNodeAtPointOp::onSelectionDone()
456 if ( !myDlg->isVisible() || !myDlg->isEnabled() )
460 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
461 if (aList.Extent() != 1)
463 Handle(SALOME_InteractiveObject) anIO = aList.First();
464 SMESH_Actor* aMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
466 if (!aMeshActor) { // coord by geom
467 if ( myDlg->myCoordBtn->isDown() ) {
468 GEOM::GEOM_Object_var geom = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
469 if ( !geom->_is_nil() ) {
470 TopoDS_Vertex aShape;
471 if ( GEOMBase::GetShape(geom, aShape) &&
472 aShape.ShapeType() == TopAbs_VERTEX ) {
473 gp_Pnt P = BRep_Tool::Pnt(aShape);
475 myDlg->myX->SetValue(P.X());
476 myDlg->myY->SetValue(P.Y());
477 myDlg->myZ->SetValue(P.Z());
487 myMeshActor = aMeshActor;
490 int nbElems = SMESH::GetNameOfSelectedElements(selector(),anIO, aString);
492 if (SMDS_Mesh* aMesh = aMeshActor->GetObject()->GetMesh()) {
493 if (const SMDS_MeshNode* aNode = aMesh->FindNode(aString.toInt())) {
495 if ( myDlg->myCoordBtn->isDown() ) { // set coord
496 myDlg->myX->SetValue(aNode->X());
497 myDlg->myY->SetValue(aNode->Y());
498 myDlg->myZ->SetValue(aNode->Z());
502 else if ( myDlg->myIdBtn->isDown() &&
503 myDlg->myIdBtn->isEnabled() ) { // set node to move
504 myDlg->myId->setText(aString);
515 //================================================================================
517 * \brief update preview
519 //================================================================================
521 void SMESHGUI_MakeNodeAtPointOp::redisplayPreview()
527 SMESH::MeshPreviewStruct_var aMeshPreviewStruct;
529 bool moveShown = false;
530 if ( myDlg->myMoveRBtn->isDown() && // Move method
533 const bool autoSearch = myDlg->myAutoSearchChkBox->isChecked();
534 const bool preview = myDlg->myPreviewChkBox->isChecked();
536 myDlg->myId->setText("");
537 if ( preview && ( autoSearch || isValid() ))
540 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
541 if (!aMesh->_is_nil()) {
542 SMESH::SMESH_MeshEditor_var aPreviewer = aMesh->GetMeshEditPreviewer();
543 if (!aPreviewer->_is_nil())
545 SUIT_OverrideCursor aWaitCursor;
547 // find id and/or just compute preview
548 int anId = aPreviewer->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
549 myDlg->myY->GetValue(),
550 myDlg->myZ->GetValue(),
551 myDlg->myId->text().toInt());
552 if ( autoSearch ) { // set found id
555 idTxt = idTxt.arg( anId );
558 myDlg->myId->setText( idTxt );
560 if ( preview ) { // fill preview data
561 aMeshPreviewStruct = aPreviewer->GetPreviewData();
562 moveShown = ( anId > 0 );
573 aMeshPreviewStruct = new SMESH::MeshPreviewStruct();
575 aMeshPreviewStruct->nodesXYZ.length(1);
576 aMeshPreviewStruct->nodesXYZ[0].x = myDlg->myX->GetValue();
577 aMeshPreviewStruct->nodesXYZ[0].y = myDlg->myY->GetValue();
578 aMeshPreviewStruct->nodesXYZ[0].z = myDlg->myZ->GetValue();
580 aMeshPreviewStruct->elementTypes.length(1);
581 aMeshPreviewStruct->elementTypes[0].SMDS_ElementType = SMESH::NODE;
582 aMeshPreviewStruct->elementTypes[0].isPoly = false;
583 aMeshPreviewStruct->elementTypes[0].nbNodesInElement = 1;
585 aMeshPreviewStruct->elementConnectivities.length(1);
586 aMeshPreviewStruct->elementConnectivities[0] = 0;
590 if ( aMeshPreviewStruct.operator->() )
592 mySimulation->SetData(aMeshPreviewStruct._retn());
596 mySimulation->SetVisibility(false);
602 //================================================================================
604 * \brief Activate Node selection
606 //================================================================================
608 void SMESHGUI_MakeNodeAtPointOp::activateSelection()
610 selectionMgr()->clearFilters();
611 SMESH::SetPointRepresentation(false);
612 selectionMgr()->installFilter( myFilter );
613 setSelectionMode( NodeSelection );
616 //================================================================================
620 //================================================================================
622 SMESHGUI_MakeNodeAtPointOp::~SMESHGUI_MakeNodeAtPointOp()
624 if ( myDlg ) delete myDlg;
625 if ( mySimulation ) delete mySimulation;
626 if ( myFilter ) delete myFilter;
629 //================================================================================
631 * \brief Gets dialog of this operation
632 * \retval LightApp_Dialog* - pointer to dialog of this operation
634 //================================================================================
636 LightApp_Dialog* SMESHGUI_MakeNodeAtPointOp::dlg() const