1 // Copyright (C) 2007-2010 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
23 // File : SMESHGUI_MakeNodeAtPointDlg.cxx
24 // Author : Edward AGAPOV, Open CASCADE S.A.S.
27 #include "SMESHGUI_MakeNodeAtPointDlg.h"
30 #include "SMESHGUI_IdValidator.h"
31 #include "SMESHGUI_MeshUtils.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_SpinBox.h"
34 #include "SMESHGUI_MeshEditPreview.h"
36 #include <SMDS_Mesh.hxx>
37 #include <SMESH_Actor.h>
38 #include <SMESH_ActorUtils.h>
39 #include <SMESH_NumberFilter.hxx>
40 #include <SMESH_LogicalFilter.hxx>
42 // SALOME GEOM includes
45 // SALOME GUI includes
46 #include <LightApp_SelectionMgr.h>
47 #include <SALOME_ListIO.hxx>
48 #include <SUIT_Desktop.h>
49 #include <SVTK_ViewModel.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, "length_precision");
152 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
153 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
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()
273 myDlg = new SMESHGUI_MakeNodeAtPointDlg;
275 myHelpFileName = "mesh_through_point_page.html";
277 // connect signals and slots
278 connect(myDlg->myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
279 connect(myDlg->myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
280 connect(myDlg->myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
281 connect(myDlg->myId,SIGNAL (textChanged(const QString&)),SLOT(redisplayPreview()));
282 connect(myDlg->myPreviewChkBox, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
283 connect(myDlg->myAutoSearchChkBox,SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
284 connect(myDlg->myMoveRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
285 connect(myDlg->myCreateRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
288 //=======================================================================
289 // function : startOperation()
290 // purpose : Init dialog fields, connect signals and slots, show dialog
291 //=======================================================================
292 void SMESHGUI_MakeNodeAtPointOp::startOperation()
297 // init simulation with a current View
298 if ( mySimulation ) delete mySimulation;
299 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
300 vtkProperty* aProp = vtkProperty::New();
301 aProp->SetRepresentationToWireframe();
302 aProp->SetColor(250, 0, 250);
303 aProp->SetPointSize(5);
304 aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
305 mySimulation->GetActor()->SetProperty(aProp);
308 // SalomeApp_TypeFilter depends on a current study
309 if ( myFilter ) delete myFilter;
310 QList<SUIT_SelectionFilter*> filters;
311 filters.append( new SalomeApp_TypeFilter((SalomeApp_Study*)study(), "SMESH" ));
312 TColStd_MapOfInteger vertexType;
313 vertexType.Add( TopAbs_VERTEX );
314 filters.append( new SMESH_NumberFilter("GEOM", TopAbs_VERTEX, 1, vertexType ));
315 myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
318 SMESHGUI_SelectionOp::startOperation(); // this method should be called only after filter creation
319 //activateSelection(); // set filters // called inside of previous statement
321 myDlg->myX->SetValue(0);
322 myDlg->myY->SetValue(0);
323 myDlg->myZ->SetValue(0);
324 myDlg->myId->setText("");
327 onSelectionDone(); // init myMeshActor
330 // myMeshOldDisplayMode = myMeshActor->GetRepresentation();
331 // myMeshActor->SetRepresentation( VTK_WIREFRAME );
332 myMeshActor->SetPointRepresentation(true);
333 SMESH::RepaintCurrentView();
338 //================================================================================
340 * \brief Stops operation
342 //================================================================================
344 void SMESHGUI_MakeNodeAtPointOp::stopOperation()
347 mySimulation->SetVisibility(false);
349 // myMeshActor->SetRepresentation( myMeshOldDisplayMode );
350 myMeshActor->SetPointRepresentation(false);
351 SMESH::RepaintCurrentView();
354 selectionMgr()->removeFilter( myFilter );
355 SMESHGUI_SelectionOp::stopOperation();
358 //================================================================================
360 * \brief perform it's intention action: move or create a node
362 //================================================================================
364 bool SMESHGUI_MakeNodeAtPointOp::onApply()
366 if( isStudyLocked() )
369 if ( !myMeshActor ) {
370 SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ),
371 tr("INVALID_MESH") );
377 if ( !isValid( msg ) ) { // node id is invalid
379 SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ),
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 QStringList aParameters;
415 aParameters << myDlg->myX->text();
416 aParameters << myDlg->myY->text();
417 aParameters << myDlg->myZ->text();
418 aMesh->SetParameters( aParameters.join(":").toLatin1().constData() );
420 myDlg->myId->setText("");
423 selectionMgr()->setSelectedObjects(aList,false);
424 aList.Append(myMeshActor->getIO());
425 selectionMgr()->setSelectedObjects(aList,false);
427 SMESHGUI::Modified();
430 catch (const SALOME::SALOME_Exception& S_ex) {
431 SalomeApp_Tools::QtCatchCorbaException(S_ex);
439 //================================================================================
441 * \brief Check selected node id validity
443 //================================================================================
445 bool SMESHGUI_MakeNodeAtPointOp::isValid( QString& msg )
449 myDlg->myMoveRBtn->isChecked() &&
450 !myDlg->myAutoSearchChkBox->isChecked() )
453 int id = myDlg->myId->text().toInt();
455 if (SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh())
456 ok = aMesh->FindNode( id );
458 msg += tr("INVALID_ID") + "\n";
461 ok = myDlg->myX->isValid( msg, !myNoPreview ) && ok;
462 ok = myDlg->myY->isValid( msg, !myNoPreview ) && ok;
463 ok = myDlg->myZ->isValid( msg, !myNoPreview ) && ok;
468 //================================================================================
470 * \brief SLOT called when selection changed
472 //================================================================================
474 void SMESHGUI_MakeNodeAtPointOp::onSelectionDone()
476 if ( !myDlg->isVisible() || !myDlg->isEnabled() )
480 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
481 if (aList.Extent() != 1)
483 Handle(SALOME_InteractiveObject) anIO = aList.First();
484 SMESH_Actor* aMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
486 if (!aMeshActor) { // coord by geom
487 if ( myDlg->myCoordBtn->isChecked() ) {
488 GEOM::GEOM_Object_var geom = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
489 if ( !geom->_is_nil() ) {
490 TopoDS_Vertex aShape;
491 if ( GEOMBase::GetShape(geom, aShape) &&
492 aShape.ShapeType() == TopAbs_VERTEX ) {
493 gp_Pnt P = BRep_Tool::Pnt(aShape);
495 myDlg->myX->SetValue(P.X());
496 myDlg->myY->SetValue(P.Y());
497 myDlg->myZ->SetValue(P.Z());
507 myMeshActor = aMeshActor;
510 int nbElems = SMESH::GetNameOfSelectedElements(selector(),anIO, aString);
512 if (SMDS_Mesh* aMesh = aMeshActor->GetObject()->GetMesh()) {
513 if (const SMDS_MeshNode* aNode = aMesh->FindNode(aString.toInt())) {
515 if ( myDlg->myCoordBtn->isChecked() ) { // set coord
516 myDlg->myX->SetValue(aNode->X());
517 myDlg->myY->SetValue(aNode->Y());
518 myDlg->myZ->SetValue(aNode->Z());
522 else if ( myDlg->myIdBtn->isChecked() &&
523 myDlg->myIdBtn->isEnabled() ) { // set node to move
524 myDlg->myId->setText(aString);
535 //================================================================================
537 * \brief update preview
539 //================================================================================
541 void SMESHGUI_MakeNodeAtPointOp::redisplayPreview()
547 SMESH::MeshPreviewStruct_var aMeshPreviewStruct;
549 bool moveShown = false;
550 if ( myDlg->myMoveRBtn->isChecked() && // Move method
553 const bool autoSearch = myDlg->myAutoSearchChkBox->isChecked();
554 const bool preview = myDlg->myPreviewChkBox->isChecked();
556 myDlg->myId->setText("");
558 if ( preview && ( autoSearch || isValid( msg ) ))
561 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
562 if (!aMesh->_is_nil()) {
563 SMESH::SMESH_MeshEditor_var aPreviewer = aMesh->GetMeshEditPreviewer();
564 if (!aPreviewer->_is_nil())
566 SUIT_OverrideCursor aWaitCursor;
568 // find id and/or just compute preview
569 int anId = aPreviewer->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
570 myDlg->myY->GetValue(),
571 myDlg->myZ->GetValue(),
572 myDlg->myId->text().toInt());
573 if ( autoSearch ) { // set found id
576 idTxt = idTxt.arg( anId );
579 myDlg->myId->setText( idTxt );
581 if ( preview ) { // fill preview data
582 aMeshPreviewStruct = aPreviewer->GetPreviewData();
583 moveShown = ( anId > 0 );
594 aMeshPreviewStruct = new SMESH::MeshPreviewStruct();
596 aMeshPreviewStruct->nodesXYZ.length(1);
597 aMeshPreviewStruct->nodesXYZ[0].x = myDlg->myX->GetValue();
598 aMeshPreviewStruct->nodesXYZ[0].y = myDlg->myY->GetValue();
599 aMeshPreviewStruct->nodesXYZ[0].z = myDlg->myZ->GetValue();
601 aMeshPreviewStruct->elementTypes.length(1);
602 aMeshPreviewStruct->elementTypes[0].SMDS_ElementType = SMESH::NODE;
603 aMeshPreviewStruct->elementTypes[0].isPoly = false;
604 aMeshPreviewStruct->elementTypes[0].nbNodesInElement = 1;
606 aMeshPreviewStruct->elementConnectivities.length(1);
607 aMeshPreviewStruct->elementConnectivities[0] = 0;
611 if ( aMeshPreviewStruct.operator->() )
613 mySimulation->SetData(aMeshPreviewStruct._retn());
617 mySimulation->SetVisibility(false);
623 //================================================================================
625 * \brief Activate Node selection
627 //================================================================================
629 void SMESHGUI_MakeNodeAtPointOp::activateSelection()
631 selectionMgr()->clearFilters();
632 SMESH::SetPointRepresentation(false);
633 selectionMgr()->installFilter( myFilter );
634 setSelectionMode( NodeSelection );
637 //================================================================================
641 //================================================================================
643 SMESHGUI_MakeNodeAtPointOp::~SMESHGUI_MakeNodeAtPointOp()
645 if ( myDlg ) delete myDlg;
646 if ( mySimulation ) delete mySimulation;
647 if ( myFilter ) delete myFilter;
650 //================================================================================
652 * \brief Gets dialog of this operation
653 * \retval LightApp_Dialog* - pointer to dialog of this operation
655 //================================================================================
657 LightApp_Dialog* SMESHGUI_MakeNodeAtPointOp::dlg() const