1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESHGUI_MakeNodeAtPointDlg.cxx
23 // Author : Edward AGAPOV, Open CASCADE S.A.S.
26 #include "SMESHGUI_MakeNodeAtPointDlg.h"
29 #include "SMESHGUI_IdValidator.h"
30 #include "SMESHGUI_MeshUtils.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_SpinBox.h"
33 #include "SMESHGUI_MeshEditPreview.h"
35 #include <SMDS_Mesh.hxx>
36 #include <SMESH_Actor.h>
37 #include <SMESH_ActorUtils.h>
38 #include <SMESH_NumberFilter.hxx>
39 #include <SMESH_LogicalFilter.hxx>
41 // SALOME GEOM includes
44 // SALOME GUI includes
45 #include <LightApp_SelectionMgr.h>
46 #include <SALOME_ListIO.hxx>
47 #include <SUIT_Desktop.h>
48 #include <SVTK_ViewModel.h>
49 #include <SalomeApp_Notebook.h>
50 #include <SalomeApp_Tools.h>
51 #include <SalomeApp_TypeFilter.h>
52 #include <SUIT_ResourceMgr.h>
53 #include <SUIT_OverrideCursor.h>
54 #include <SUIT_MessageBox.h>
57 #include <TColStd_MapOfInteger.hxx>
58 #include <TopoDS_Vertex.hxx>
59 #include <BRep_Tool.hxx>
64 #include <QGridLayout>
65 #include <QHBoxLayout>
66 #include <QVBoxLayout>
68 #include <QPushButton>
70 #include <QRadioButton>
72 #include <QButtonGroup>
75 #include <vtkProperty.h>
78 #include <SALOMEconfig.h>
79 #include CORBA_SERVER_HEADER(SMESH_Mesh)
80 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
86 * \brief Dialog to publish a sub-shape of the mesh main shape
87 * by selecting mesh elements
89 SMESHGUI_MakeNodeAtPointDlg::SMESHGUI_MakeNodeAtPointDlg()
90 : SMESHGUI_Dialog( 0, false, true )
92 setWindowTitle(tr("CAPTION"));
94 QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
95 aDlgLay->setMargin(MARGIN);;
96 aDlgLay->setSpacing(SPACING);
98 QWidget* aMainFrame = createMainFrame (mainFrame());
100 aDlgLay->addWidget(aMainFrame);
102 aDlgLay->setStretchFactor(aMainFrame, 1);
105 //=======================================================================
106 // function : createMainFrame()
107 // purpose : Create frame containing dialog's input fields
108 //=======================================================================
109 QWidget* SMESHGUI_MakeNodeAtPointDlg::createMainFrame (QWidget* theParent)
111 QWidget* aFrame = new QWidget(theParent);
113 SUIT_ResourceMgr* rm = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
114 QPixmap iconMoveNode (rm->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
115 QPixmap iconSelect (rm->loadPixmap("SMESH", tr("ICON_SELECT")));
119 QGroupBox* aPixGrp = new QGroupBox(tr("MESH_PASS_THROUGH_POINT"), aFrame);
120 QButtonGroup* aBtnGrp = new QButtonGroup(this);
121 QHBoxLayout* aPixGrpLayout = new QHBoxLayout(aPixGrp);
122 aPixGrpLayout->setMargin(MARGIN);
123 aPixGrpLayout->setSpacing(SPACING);
125 QRadioButton* aRBut = new QRadioButton(aPixGrp);
126 aRBut->setIcon(iconMoveNode);
127 aRBut->setChecked(true);
128 aPixGrpLayout->addWidget(aRBut);
129 aBtnGrp->addButton(aRBut, 0);
133 QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
134 QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
135 aCoordGrpLayout->setMargin(MARGIN);
136 aCoordGrpLayout->setSpacing(SPACING);
138 myCoordBtn = new QPushButton(aCoordGrp);
139 myCoordBtn->setIcon(iconSelect);
140 myCoordBtn->setCheckable(true);
142 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
143 myX = new SMESHGUI_SpinBox(aCoordGrp);
145 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
146 myY = new SMESHGUI_SpinBox(aCoordGrp);
148 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
149 myZ = new SMESHGUI_SpinBox(aCoordGrp);
151 myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
152 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
153 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
155 aCoordGrpLayout->addWidget(myCoordBtn);
156 aCoordGrpLayout->addWidget(aXLabel);
157 aCoordGrpLayout->addWidget(myX);
158 aCoordGrpLayout->addWidget(aYLabel);
159 aCoordGrpLayout->addWidget(myY);
160 aCoordGrpLayout->addWidget(aZLabel);
161 aCoordGrpLayout->addWidget(myZ);
165 QGroupBox* aMethodGrp = new QGroupBox(tr("METHOD"), aFrame);
166 QHBoxLayout* aMethodGrpLayout = new QHBoxLayout(aMethodGrp);
167 aMethodGrpLayout->setMargin(MARGIN);
168 aMethodGrpLayout->setSpacing(SPACING);
170 myMoveRBtn = new QRadioButton(tr("MOVE_EXISTING_METHOD"), aMethodGrp);
171 myCreateRBtn = new QRadioButton(tr("CREATE_NEW_METHOD"), aMethodGrp);
173 aMethodGrpLayout->addWidget(myMoveRBtn);
174 aMethodGrpLayout->addWidget(myCreateRBtn);
178 myNodeToMoveGrp = new QGroupBox(tr("NODE_2MOVE"), aFrame);
180 QLabel* idLabel = new QLabel(tr("NODE_2MOVE_ID"), myNodeToMoveGrp);
181 myIdBtn = new QPushButton(myNodeToMoveGrp);
182 myIdBtn->setIcon(iconSelect);
183 myIdBtn->setCheckable(true);
184 myId = new QLineEdit(myNodeToMoveGrp);
185 myId->setValidator(new SMESHGUI_IdValidator(this, 1));
186 myAutoSearchChkBox = new QCheckBox( tr("AUTO_SEARCH"), myNodeToMoveGrp);
187 myPreviewChkBox = new QCheckBox( tr("PREVIEW"), myNodeToMoveGrp);
189 QGridLayout* myNodeToMoveGrpLayout = new QGridLayout(myNodeToMoveGrp);
190 myNodeToMoveGrpLayout->setSpacing(SPACING);
191 myNodeToMoveGrpLayout->setMargin(MARGIN);
193 myNodeToMoveGrpLayout->addWidget( idLabel, 0, 0 );
194 myNodeToMoveGrpLayout->addWidget( myIdBtn, 0, 1 );
195 myNodeToMoveGrpLayout->addWidget( myId, 0, 2 );
196 myNodeToMoveGrpLayout->addWidget( myAutoSearchChkBox, 1, 0, 1, 3 );
197 myNodeToMoveGrpLayout->addWidget( myPreviewChkBox, 2, 0, 1, 3 );
199 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
200 aLay->addWidget(aPixGrp);
201 aLay->addWidget(aCoordGrp);
202 aLay->addWidget(aMethodGrp);
203 aLay->addWidget(myNodeToMoveGrp);
205 connect(myCoordBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
206 connect(myMoveRBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
207 connect(myCreateRBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
208 connect(myIdBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
209 connect(myAutoSearchChkBox, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
211 myMoveRBtn->setChecked(true);
212 myIdBtn->setChecked(true);
213 myAutoSearchChkBox->setChecked(true);
218 //================================================================================
220 * \brief SLOT called when any button is toggled
221 * \param bool - on or off
223 //================================================================================
225 void SMESHGUI_MakeNodeAtPointDlg::ButtonToggled (bool on)
227 const QObject* aSender = sender();
229 if ( aSender == myCoordBtn ) // button to set coord by node selection
231 if ( myIdBtn->isEnabled() )
232 myIdBtn->setChecked( !on );
234 else if ( aSender == myIdBtn ) // button to select a node to move
236 myCoordBtn->setChecked( !on );
238 else if ( aSender == myMoveRBtn ) // move node method
240 myNodeToMoveGrp->setEnabled( true );
242 else if ( aSender == myCreateRBtn ) // create node method
244 myNodeToMoveGrp->setEnabled( false );
245 myCoordBtn->setChecked( true );
248 if ( aSender == myAutoSearchChkBox ) // automatic node search
252 myId->setReadOnly ( true );
253 myIdBtn->setChecked( false );
254 myIdBtn->setEnabled( false );
255 myCoordBtn->setChecked( true );
258 myId->setReadOnly ( false );
259 myIdBtn->setEnabled( true );
264 //================================================================================
268 //================================================================================
270 SMESHGUI_MakeNodeAtPointOp::SMESHGUI_MakeNodeAtPointOp()
271 : SMESHGUI_Helper( getSMESHGUI() )
274 myDlg = new SMESHGUI_MakeNodeAtPointDlg;
276 myHelpFileName = "mesh_through_point_page.html";
278 // connect signals and slots
279 connect(myDlg->myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
280 connect(myDlg->myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
281 connect(myDlg->myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
282 connect(myDlg->myId,SIGNAL (textChanged(const QString&)),SLOT(redisplayPreview()));
283 connect(myDlg->myPreviewChkBox, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
284 connect(myDlg->myAutoSearchChkBox,SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
285 connect(myDlg->myMoveRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
286 connect(myDlg->myCreateRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
289 //=======================================================================
290 // function : startOperation()
291 // purpose : Init dialog fields, connect signals and slots, show dialog
292 //=======================================================================
293 void SMESHGUI_MakeNodeAtPointOp::startOperation()
298 // init simulation with a current View
299 if ( mySimulation ) delete mySimulation;
300 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
301 vtkProperty* aProp = vtkProperty::New();
302 aProp->SetRepresentationToWireframe();
303 aProp->SetColor(250, 0, 250);
304 aProp->SetPointSize(5);
305 aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
306 mySimulation->GetActor()->SetProperty(aProp);
309 // SalomeApp_TypeFilter depends on a current study
310 if ( myFilter ) delete myFilter;
311 QList<SUIT_SelectionFilter*> filters;
312 filters.append( new SalomeApp_TypeFilter((SalomeApp_Study*)study(), "SMESH" ));
313 TColStd_MapOfInteger vertexType;
314 vertexType.Add( TopAbs_VERTEX );
315 filters.append( new SMESH_NumberFilter("GEOM", TopAbs_VERTEX, 1, vertexType ));
316 myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
319 SMESHGUI_SelectionOp::startOperation(); // this method should be called only after filter creation
320 //activateSelection(); // set filters // called inside of previous statement
322 myDlg->myX->SetValue(0);
323 myDlg->myY->SetValue(0);
324 myDlg->myZ->SetValue(0);
325 myDlg->myId->setText("");
328 onSelectionDone(); // init myMeshActor
331 // myMeshOldDisplayMode = myMeshActor->GetRepresentation();
332 // myMeshActor->SetRepresentation( VTK_WIREFRAME );
333 myMeshActor->SetPointRepresentation(true);
334 SMESH::RepaintCurrentView();
339 //================================================================================
341 * \brief Stops operation
343 //================================================================================
345 void SMESHGUI_MakeNodeAtPointOp::stopOperation()
348 mySimulation->SetVisibility(false);
350 // myMeshActor->SetRepresentation( myMeshOldDisplayMode );
351 myMeshActor->SetPointRepresentation(false);
352 SMESH::RepaintCurrentView();
355 selectionMgr()->removeFilter( myFilter );
356 SMESHGUI_SelectionOp::stopOperation();
359 //================================================================================
361 * \brief perform it's intention action: move or create a node
363 //================================================================================
365 bool SMESHGUI_MakeNodeAtPointOp::onApply()
367 if( isStudyLocked() )
370 if ( !myMeshActor ) {
371 SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ),
372 tr("INVALID_MESH") );
378 if ( !isValid( msg ) ) { // node id is invalid
380 SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ), msg );
387 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
388 if (aMesh->_is_nil()) {
389 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
390 tr("SMESHG_NO_MESH") );
393 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
394 if (aMeshEditor->_is_nil())
398 if ( myDlg->myCreateRBtn->isChecked() )
400 aResult = aMeshEditor->AddNode(myDlg->myX->GetValue(),
401 myDlg->myY->GetValue(),
402 myDlg->myZ->GetValue());
406 int anId = myDlg->myId->text().toInt();
407 aResult = aMeshEditor->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
408 myDlg->myY->GetValue(),
409 myDlg->myZ->GetValue(),
414 getNotebook()->setParameters( aMesh, 3, myDlg->myX, myDlg->myY, myDlg->myZ );
416 myDlg->myId->setText("");
419 selectionMgr()->setSelectedObjects(aList,false);
420 aList.Append(myMeshActor->getIO());
421 selectionMgr()->setSelectedObjects(aList,false);
425 catch (const SALOME::SALOME_Exception& S_ex) {
426 SalomeApp_Tools::QtCatchCorbaException(S_ex);
434 //================================================================================
436 * \brief Check selected node id validity
438 //================================================================================
440 bool SMESHGUI_MakeNodeAtPointOp::isValid( QString& msg )
442 if( !checkParameters( !myNoPreview, 3, myDlg->myX, myDlg->myY, myDlg->myZ ) )
447 myDlg->myMoveRBtn->isChecked() &&
448 !myDlg->myAutoSearchChkBox->isChecked() )
451 int id = myDlg->myId->text().toInt();
453 if (SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh())
454 ok = aMesh->FindNode( id );
456 msg += tr("INVALID_ID") + "\n";
461 //================================================================================
463 * \brief SLOT called when selection changed
465 //================================================================================
467 void SMESHGUI_MakeNodeAtPointOp::onSelectionDone()
469 if ( !myDlg->isVisible() || !myDlg->isEnabled() )
473 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
474 if (aList.Extent() != 1)
476 Handle(SALOME_InteractiveObject) anIO = aList.First();
477 SMESH_Actor* aMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
479 if (!aMeshActor) { // coord by geom
480 if ( myDlg->myCoordBtn->isChecked() ) {
481 GEOM::GEOM_Object_var geom = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
482 if ( !geom->_is_nil() ) {
483 TopoDS_Vertex aShape;
484 if ( GEOMBase::GetShape(geom, aShape) &&
485 aShape.ShapeType() == TopAbs_VERTEX ) {
486 gp_Pnt P = BRep_Tool::Pnt(aShape);
488 myDlg->myX->SetValue(P.X());
489 myDlg->myY->SetValue(P.Y());
490 myDlg->myZ->SetValue(P.Z());
500 myMeshActor = aMeshActor;
503 int nbElems = SMESH::GetNameOfSelectedElements(selector(),anIO, aString);
505 if (SMDS_Mesh* aMesh = aMeshActor->GetObject()->GetMesh()) {
506 if (const SMDS_MeshNode* aNode = aMesh->FindNode(aString.toInt())) {
508 if ( myDlg->myCoordBtn->isChecked() ) { // set coord
509 myDlg->myX->SetValue(aNode->X());
510 myDlg->myY->SetValue(aNode->Y());
511 myDlg->myZ->SetValue(aNode->Z());
515 else if ( myDlg->myIdBtn->isChecked() &&
516 myDlg->myIdBtn->isEnabled() ) { // set node to move
517 myDlg->myId->setText(aString);
528 //================================================================================
530 * \brief update preview
532 //================================================================================
534 void SMESHGUI_MakeNodeAtPointOp::redisplayPreview()
540 SMESH::MeshPreviewStruct_var aMeshPreviewStruct;
542 bool moveShown = false;
543 if ( myDlg->myMoveRBtn->isChecked() && // Move method
546 const bool autoSearch = myDlg->myAutoSearchChkBox->isChecked();
547 const bool preview = myDlg->myPreviewChkBox->isChecked();
549 myDlg->myId->setText("");
551 if ( preview && ( autoSearch || isValid( msg ) ))
554 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
555 if (!aMesh->_is_nil()) {
556 SMESH::SMESH_MeshEditor_var aPreviewer = aMesh->GetMeshEditPreviewer();
557 if (!aPreviewer->_is_nil())
559 SUIT_OverrideCursor aWaitCursor;
561 // find id and/or just compute preview
562 int anId = aPreviewer->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
563 myDlg->myY->GetValue(),
564 myDlg->myZ->GetValue(),
565 myDlg->myId->text().toInt());
566 if ( autoSearch ) { // set found id
569 idTxt = idTxt.arg( anId );
572 myDlg->myId->setText( idTxt );
574 if ( preview ) { // fill preview data
575 aMeshPreviewStruct = aPreviewer->GetPreviewData();
576 moveShown = ( anId > 0 );
587 aMeshPreviewStruct = new SMESH::MeshPreviewStruct();
589 aMeshPreviewStruct->nodesXYZ.length(1);
590 aMeshPreviewStruct->nodesXYZ[0].x = myDlg->myX->GetValue();
591 aMeshPreviewStruct->nodesXYZ[0].y = myDlg->myY->GetValue();
592 aMeshPreviewStruct->nodesXYZ[0].z = myDlg->myZ->GetValue();
594 aMeshPreviewStruct->elementTypes.length(1);
595 aMeshPreviewStruct->elementTypes[0].SMDS_ElementType = SMESH::NODE;
596 aMeshPreviewStruct->elementTypes[0].isPoly = false;
597 aMeshPreviewStruct->elementTypes[0].nbNodesInElement = 1;
599 aMeshPreviewStruct->elementConnectivities.length(1);
600 aMeshPreviewStruct->elementConnectivities[0] = 0;
604 if ( aMeshPreviewStruct.operator->() )
606 mySimulation->SetData(aMeshPreviewStruct._retn());
610 mySimulation->SetVisibility(false);
616 //================================================================================
618 * \brief Activate Node selection
620 //================================================================================
622 void SMESHGUI_MakeNodeAtPointOp::activateSelection()
624 selectionMgr()->clearFilters();
625 SMESH::SetPointRepresentation(false);
626 selectionMgr()->installFilter( myFilter );
627 setSelectionMode( NodeSelection );
630 //================================================================================
634 //================================================================================
636 SMESHGUI_MakeNodeAtPointOp::~SMESHGUI_MakeNodeAtPointOp()
638 if ( myDlg ) delete myDlg;
639 if ( mySimulation ) delete mySimulation;
640 if ( myFilter ) delete myFilter;
643 //================================================================================
645 * \brief Gets dialog of this operation
646 * \retval LightApp_Dialog* - pointer to dialog of this operation
648 //================================================================================
650 LightApp_Dialog* SMESHGUI_MakeNodeAtPointOp::dlg() const