1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 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
24 // File : SMESHGUI_MakeNodeAtPointDlg.cxx
25 // Author : Edward AGAPOV
28 #include "SMESHGUI_MakeNodeAtPointDlg.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"
39 #include "SMDS_Mesh.hxx"
40 #include "SMDS_MeshNode.hxx"
41 #include "SMESH_Actor.h"
42 #include "SMESH_ActorUtils.h"
43 #include "SMESH_NumberFilter.hxx"
44 #include "SMESH_LogicalFilter.hxx"
47 #include "GeometryGUI.h"
49 #include "LightApp_DataOwner.h"
50 #include "LightApp_SelectionMgr.h"
51 #include "SALOMEDSClient_SObject.hxx"
52 #include "SALOME_ListIO.hxx"
53 #include "SUIT_Desktop.h"
54 #include "SVTK_Selector.h"
55 #include "SVTK_ViewWindow.h"
56 #include "SVTK_ViewModel.h"
57 #include "SalomeApp_Tools.h"
58 #include "SalomeApp_TypeFilter.h"
59 #include "SUIT_ResourceMgr.h"
60 #include "SUIT_OverrideCursor.h"
61 #include "SUIT_MessageBox.h"
64 #include <TColStd_MapOfInteger.hxx>
65 #include <TopoDS_Vertex.hxx>
66 #include <BRep_Tool.hxx>
71 #include <qlineedit.h>
72 #include <qpushbutton.h>
74 #include <qradiobutton.h>
75 #include <qbuttongroup.h>
76 #include <qapplication.h>
77 #include <qstringlist.h>
78 #include <qcheckbox.h>
79 #include <qmessagebox.h>
81 #include <vtkProperty.h>
84 #include <SALOMEconfig.h>
85 #include CORBA_SERVER_HEADER(SMESH_Mesh)
86 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
92 * \brief Dialog to publish a sub-shape of the mesh main shape
93 * by selecting mesh elements
95 SMESHGUI_MakeNodeAtPointDlg::SMESHGUI_MakeNodeAtPointDlg()
96 : SMESHGUI_Dialog( 0, false, true )
98 setCaption(tr("CAPTION"));
100 QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame(), MARGIN, SPACING);
102 QFrame* aMainFrame = createMainFrame (mainFrame());
104 aDlgLay->addWidget(aMainFrame);
106 aDlgLay->setStretchFactor(aMainFrame, 1);
109 //=======================================================================
110 // function : createMainFrame()
111 // purpose : Create frame containing dialog's input fields
112 //=======================================================================
113 QFrame* SMESHGUI_MakeNodeAtPointDlg::createMainFrame (QWidget* theParent)
115 QFrame* aFrame = new QFrame(theParent);
117 SUIT_ResourceMgr* rm = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
118 QPixmap iconMoveNode (rm->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
119 QPixmap iconSelect (rm->loadPixmap("SMESH", tr("ICON_SELECT")));
123 QButtonGroup* aPixGrp = new QButtonGroup(1, Qt::Vertical, tr("MESH_PASS_THROUGH_POINT"), aFrame);
124 aPixGrp->setExclusive(TRUE);
125 QRadioButton* aRBut = new QRadioButton(aPixGrp);
126 aRBut->setPixmap(iconMoveNode);
127 aRBut->setChecked(TRUE);
131 QGroupBox* aCoordGrp = new QGroupBox(1, Qt::Vertical, tr("SMESH_COORDINATES"), aFrame);
132 myCoordBtn = new QPushButton(aCoordGrp);
133 myCoordBtn->setPixmap(iconSelect);
134 myCoordBtn->setToggleButton(TRUE);
136 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
137 aXLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
138 myX = new SMESHGUI_SpinBox(aCoordGrp);
140 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
141 //aYLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
142 aYLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
143 myY = new SMESHGUI_SpinBox(aCoordGrp);
145 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
146 //aZLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::ExpandTabs );
147 aZLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
148 myZ = new SMESHGUI_SpinBox(aCoordGrp);
150 myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
151 myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
152 myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, DBL_DIGITS_DISPLAY);
156 QButtonGroup* aMethodGrp = new QButtonGroup(1, Qt::Vertical, tr("METHOD"), aFrame);
157 aMethodGrp->setExclusive(TRUE);
158 myMoveRBtn = new QRadioButton(tr("MOVE_EXISTING_METHOD"), aMethodGrp);
159 myCreateRBtn = new QRadioButton(tr("CREATE_NEW_METHOD"), aMethodGrp);
163 myNodeToMoveGrp = new QGroupBox(tr("NODE_2MOVE"), aFrame, "anIdGrp");
165 QLabel* idLabel = new QLabel(tr("NODE_2MOVE_ID"), myNodeToMoveGrp, "idLabel");
166 myIdBtn = new QPushButton(myNodeToMoveGrp);
167 myIdBtn->setPixmap(iconSelect);
168 myIdBtn->setToggleButton(TRUE);
169 myId = new QLineEdit(myNodeToMoveGrp,"myId");
170 myId->setValidator(new SMESHGUI_IdValidator(this, "validator", 1));
171 myAutoSearchChkBox = new QCheckBox( tr("AUTO_SEARCH"), myNodeToMoveGrp, "myAutoSearchChkBox");
172 myPreviewChkBox = new QCheckBox( tr("PREVIEW"), myNodeToMoveGrp, "myPreviewChkBox");
174 myNodeToMoveGrp->setColumnLayout(0, Qt::Vertical);
175 myNodeToMoveGrp->layout()->setSpacing(0);
176 myNodeToMoveGrp->layout()->setMargin(0);
177 QGridLayout* myNodeToMoveGrpLayout = new QGridLayout(myNodeToMoveGrp->layout());
178 myNodeToMoveGrpLayout->setAlignment(Qt::AlignTop);
179 myNodeToMoveGrpLayout->setSpacing(SPACING);
180 myNodeToMoveGrpLayout->setMargin(MARGIN);
181 myNodeToMoveGrpLayout->addWidget( idLabel, 0, 0 );
182 myNodeToMoveGrpLayout->addWidget( myIdBtn, 0, 1 );
183 myNodeToMoveGrpLayout->addWidget( myId, 0, 2 );
184 myNodeToMoveGrpLayout->addMultiCellWidget( myAutoSearchChkBox, 1, 1, 0, 2 );
185 myNodeToMoveGrpLayout->addMultiCellWidget( myPreviewChkBox, 2, 2, 0, 2 );
187 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
188 aLay->addWidget(aPixGrp);
189 aLay->addWidget(aCoordGrp);
190 aLay->addWidget(aMethodGrp);
191 aLay->addWidget(myNodeToMoveGrp);
193 connect(myCoordBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
194 connect(myMoveRBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
195 connect(myCreateRBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
196 connect(myIdBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
197 connect(myAutoSearchChkBox, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
199 myMoveRBtn->setChecked(TRUE);
200 myIdBtn->setOn(TRUE);
201 myAutoSearchChkBox->setChecked(TRUE);
206 //================================================================================
208 * \brief SLOT called when any button is toggled
209 * \param bool - on or off
211 //================================================================================
213 void SMESHGUI_MakeNodeAtPointDlg::ButtonToggled (bool on)
215 const QObject* aSender = sender();
217 if ( aSender == myCoordBtn ) // button to set coord by node selection
219 if ( myIdBtn->isEnabled() )
220 myIdBtn->setOn( !on );
222 else if ( aSender == myIdBtn ) // button to select a node to move
224 myCoordBtn->setOn( !on );
226 else if ( aSender == myMoveRBtn ) // move node method
228 myNodeToMoveGrp->setEnabled( TRUE );
230 else if ( aSender == myCreateRBtn ) // create node method
232 myNodeToMoveGrp->setEnabled( FALSE );
233 myCoordBtn->setOn( TRUE );
236 if ( aSender == myAutoSearchChkBox ) // automatic node search
240 myId->setReadOnly ( TRUE );
241 myIdBtn->setOn( FALSE );
242 myIdBtn->setEnabled( FALSE );
243 myCoordBtn->setOn( TRUE );
246 myId->setReadOnly ( FALSE );
247 myIdBtn->setEnabled( TRUE );
252 //================================================================================
256 //================================================================================
258 SMESHGUI_MakeNodeAtPointOp::SMESHGUI_MakeNodeAtPointOp()
261 myDlg = new SMESHGUI_MakeNodeAtPointDlg;
263 myHelpFileName = "mesh_through_point_page.html";
265 // connect signals and slots
266 connect(myDlg->myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
267 connect(myDlg->myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
268 connect(myDlg->myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
269 connect(myDlg->myId,SIGNAL (textChanged(const QString&)),SLOT(redisplayPreview()));
270 connect(myDlg->myPreviewChkBox, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
271 connect(myDlg->myAutoSearchChkBox,SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
272 connect(myDlg->myMoveRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
273 connect(myDlg->myCreateRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
276 //=======================================================================
277 // function : startOperation()
278 // purpose : Init dialog fields, connect signals and slots, show dialog
279 //=======================================================================
280 void SMESHGUI_MakeNodeAtPointOp::startOperation()
285 // init simulation with a current View
286 if ( mySimulation ) delete mySimulation;
287 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
288 vtkProperty* aProp = vtkProperty::New();
289 aProp->SetRepresentationToWireframe();
290 aProp->SetColor(250, 0, 250);
291 aProp->SetPointSize(5);
292 aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
293 mySimulation->GetActor()->SetProperty(aProp);
296 SMESHGUI_SelectionOp::startOperation();
298 // SalomeApp_TypeFilter depends on a current study
299 if ( myFilter ) delete myFilter;
300 QPtrList<SUIT_SelectionFilter> filters;
301 filters.append( new SalomeApp_TypeFilter((SalomeApp_Study*)study(), "SMESH" ));
302 TColStd_MapOfInteger vertexType;
303 vertexType.Add( TopAbs_VERTEX );
304 filters.append( new SMESH_NumberFilter("GEOM", TopAbs_VERTEX, 1, vertexType ));
305 myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
307 activateSelection(); // set filters
309 myDlg->myX->SetValue(0);
310 myDlg->myY->SetValue(0);
311 myDlg->myZ->SetValue(0);
312 myDlg->myId->setText("");
315 onSelectionDone(); // init myMeshActor
318 // myMeshOldDisplayMode = myMeshActor->GetRepresentation();
319 // myMeshActor->SetRepresentation( VTK_WIREFRAME );
320 myMeshActor->SetPointRepresentation(true);
321 SMESH::RepaintCurrentView();
326 //================================================================================
328 * \brief Stops operation
330 //================================================================================
332 void SMESHGUI_MakeNodeAtPointOp::stopOperation()
335 mySimulation->SetVisibility(false);
337 // myMeshActor->SetRepresentation( myMeshOldDisplayMode );
338 myMeshActor->SetPointRepresentation(false);
339 SMESH::RepaintCurrentView();
342 selectionMgr()->removeFilter( myFilter );
343 SMESHGUI_SelectionOp::stopOperation();
346 //================================================================================
348 * \brief perform it's intention action: move or create a node
350 //================================================================================
352 bool SMESHGUI_MakeNodeAtPointOp::onApply()
354 if( isStudyLocked() )
357 if ( !myMeshActor ) {
358 SUIT_MessageBox::warn1( dlg(), tr( "SMESH_WRN_WARNING" ),
359 tr("INVALID_MESH"), tr( "SMESH_BUT_OK" ) );
364 if ( !isValid() ) { // node id is invalid
365 SUIT_MessageBox::warn1( dlg(), tr( "SMESH_WRN_WARNING" ),
366 tr("INVALID_ID"), tr( "SMESH_BUT_OK" ) );
373 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
374 if (aMesh->_is_nil()) {
375 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
376 tr("SMESHG_NO_MESH"), QMessageBox::Ok);
379 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
380 if (aMeshEditor->_is_nil())
384 if ( myDlg->myCreateRBtn->isOn() )
386 aResult = aMeshEditor->AddNode(myDlg->myX->GetValue(),
387 myDlg->myY->GetValue(),
388 myDlg->myZ->GetValue());
392 int anId = myDlg->myId->text().toInt();
393 aResult = aMeshEditor->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
394 myDlg->myY->GetValue(),
395 myDlg->myZ->GetValue(),
400 myDlg->myId->setText("");
403 selectionMgr()->setSelectedObjects(aList,false);
404 aList.Append(myMeshActor->getIO());
405 selectionMgr()->setSelectedObjects(aList,false);
409 catch (const SALOME::SALOME_Exception& S_ex) {
410 SalomeApp_Tools::QtCatchCorbaException(S_ex);
418 //================================================================================
420 * \brief Check selected node id validity
422 //================================================================================
424 bool SMESHGUI_MakeNodeAtPointOp::isValid()
429 myDlg->myMoveRBtn->isOn() &&
430 !myDlg->myAutoSearchChkBox->isChecked() )
433 int id = myDlg->myId->text().toInt();
435 if (SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh())
436 ok = aMesh->FindNode( id );
441 //================================================================================
443 * \brief SLOT called when selection changed
445 //================================================================================
447 void SMESHGUI_MakeNodeAtPointOp::onSelectionDone()
449 if ( !myDlg->isShown() || !myDlg->isEnabled() )
453 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
454 if (aList.Extent() != 1)
456 Handle(SALOME_InteractiveObject) anIO = aList.First();
457 SMESH_Actor* aMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
459 if (!aMeshActor) { // coord by geom
460 if ( myDlg->myCoordBtn->isOn() ) {
461 GEOM::GEOM_Object_var geom = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
462 if ( !geom->_is_nil() ) {
463 TopoDS_Vertex aShape;
464 if ( GEOMBase::GetShape(geom, aShape) &&
465 aShape.ShapeType() == TopAbs_VERTEX ) {
466 gp_Pnt P = BRep_Tool::Pnt(aShape);
468 myDlg->myX->SetValue(P.X());
469 myDlg->myY->SetValue(P.Y());
470 myDlg->myZ->SetValue(P.Z());
480 myMeshActor = aMeshActor;
483 int nbElems = SMESH::GetNameOfSelectedElements(selector(),anIO, aString);
485 if (SMDS_Mesh* aMesh = aMeshActor->GetObject()->GetMesh()) {
486 if (const SMDS_MeshNode* aNode = aMesh->FindNode(aString.toInt())) {
488 if ( myDlg->myCoordBtn->isOn() ) { // set coord
489 myDlg->myX->SetValue(aNode->X());
490 myDlg->myY->SetValue(aNode->Y());
491 myDlg->myZ->SetValue(aNode->Z());
495 else if ( myDlg->myIdBtn->isOn() &&
496 myDlg->myIdBtn->isEnabled() ) { // set node to move
497 myDlg->myId->setText(aString);
508 //================================================================================
510 * \brief update preview
512 //================================================================================
514 void SMESHGUI_MakeNodeAtPointOp::redisplayPreview()
520 SMESH::MeshPreviewStruct_var aMeshPreviewStruct;
522 bool moveShown = false;
523 if ( myDlg->myMoveRBtn->isOn() && // Move method
526 const bool autoSearch = myDlg->myAutoSearchChkBox->isChecked();
527 const bool preview = myDlg->myPreviewChkBox->isChecked();
529 myDlg->myId->setText("");
530 if ( preview && ( autoSearch || isValid() ))
533 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
534 if (!aMesh->_is_nil()) {
535 SMESH::SMESH_MeshEditor_var aPreviewer = aMesh->GetMeshEditPreviewer();
536 if (!aPreviewer->_is_nil())
538 SUIT_OverrideCursor aWaitCursor;
540 // find id and/or just compute preview
541 int anId = aPreviewer->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
542 myDlg->myY->GetValue(),
543 myDlg->myZ->GetValue(),
544 myDlg->myId->text().toInt());
545 if ( autoSearch ) { // set found id
548 idTxt = idTxt.arg( anId );
551 myDlg->myId->setText( idTxt );
553 if ( preview ) { // fill preview data
554 aMeshPreviewStruct = aPreviewer->GetPreviewData();
555 moveShown = ( anId > 0 );
566 aMeshPreviewStruct = new SMESH::MeshPreviewStruct();
568 aMeshPreviewStruct->nodesXYZ.length(1);
569 aMeshPreviewStruct->nodesXYZ[0].x = myDlg->myX->GetValue();
570 aMeshPreviewStruct->nodesXYZ[0].y = myDlg->myY->GetValue();
571 aMeshPreviewStruct->nodesXYZ[0].z = myDlg->myZ->GetValue();
573 aMeshPreviewStruct->elementTypes.length(1);
574 aMeshPreviewStruct->elementTypes[0].SMDS_ElementType = SMESH::NODE;
575 aMeshPreviewStruct->elementTypes[0].isPoly = false;
576 aMeshPreviewStruct->elementTypes[0].nbNodesInElement = 1;
578 aMeshPreviewStruct->elementConnectivities.length(1);
579 aMeshPreviewStruct->elementConnectivities[0] = 0;
583 if ( aMeshPreviewStruct.operator->() )
585 mySimulation->SetData(aMeshPreviewStruct._retn());
589 mySimulation->SetVisibility(false);
595 //================================================================================
597 * \brief Activate Node selection
599 //================================================================================
601 void SMESHGUI_MakeNodeAtPointOp::activateSelection()
603 selectionMgr()->clearFilters();
604 SMESH::SetPointRepresentation(false);
605 selectionMgr()->installFilter( myFilter );
606 setSelectionMode( NodeSelection );
609 //================================================================================
613 //================================================================================
615 SMESHGUI_MakeNodeAtPointOp::~SMESHGUI_MakeNodeAtPointOp()
617 if ( myDlg ) delete myDlg;
618 if ( mySimulation ) delete mySimulation;
619 if ( myFilter ) delete myFilter;
622 //================================================================================
624 * \brief Gets dialog of this operation
625 * \retval LightApp_Dialog* - pointer to dialog of this operation
627 //================================================================================
629 LightApp_Dialog* SMESHGUI_MakeNodeAtPointOp::dlg() const