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_Tools.h>
50 #include <SalomeApp_TypeFilter.h>
51 #include <SUIT_ResourceMgr.h>
52 #include <SUIT_OverrideCursor.h>
53 #include <SUIT_MessageBox.h>
56 #include <TColStd_MapOfInteger.hxx>
57 #include <TopoDS_Vertex.hxx>
58 #include <BRep_Tool.hxx>
63 #include <QGridLayout>
64 #include <QHBoxLayout>
65 #include <QVBoxLayout>
67 #include <QPushButton>
69 #include <QRadioButton>
71 #include <QButtonGroup>
74 #include <vtkProperty.h>
77 #include <SALOMEconfig.h>
78 #include CORBA_SERVER_HEADER(SMESH_Mesh)
79 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
85 * \brief Dialog to publish a sub-shape of the mesh main shape
86 * by selecting mesh elements
88 SMESHGUI_MakeNodeAtPointDlg::SMESHGUI_MakeNodeAtPointDlg()
89 : SMESHGUI_Dialog( 0, false, true )
91 setWindowTitle(tr("CAPTION"));
93 QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
94 aDlgLay->setMargin(MARGIN);;
95 aDlgLay->setSpacing(SPACING);
97 QWidget* aMainFrame = createMainFrame (mainFrame());
99 aDlgLay->addWidget(aMainFrame);
101 aDlgLay->setStretchFactor(aMainFrame, 1);
104 //=======================================================================
105 // function : createMainFrame()
106 // purpose : Create frame containing dialog's input fields
107 //=======================================================================
108 QWidget* SMESHGUI_MakeNodeAtPointDlg::createMainFrame (QWidget* theParent)
110 QWidget* aFrame = new QWidget(theParent);
112 SUIT_ResourceMgr* rm = SMESH::GetResourceMgr( SMESHGUI::GetSMESHGUI() );
113 QPixmap iconMoveNode (rm->loadPixmap("SMESH", tr("ICON_DLG_MOVE_NODE")));
114 QPixmap iconSelect (rm->loadPixmap("SMESH", tr("ICON_SELECT")));
118 QGroupBox* aPixGrp = new QGroupBox(tr("MESH_PASS_THROUGH_POINT"), aFrame);
119 QButtonGroup* aBtnGrp = new QButtonGroup(this);
120 QHBoxLayout* aPixGrpLayout = new QHBoxLayout(aPixGrp);
121 aPixGrpLayout->setMargin(MARGIN);
122 aPixGrpLayout->setSpacing(SPACING);
124 QRadioButton* aRBut = new QRadioButton(aPixGrp);
125 aRBut->setIcon(iconMoveNode);
126 aRBut->setChecked(true);
127 aPixGrpLayout->addWidget(aRBut);
128 aBtnGrp->addButton(aRBut, 0);
132 QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
133 QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
134 aCoordGrpLayout->setMargin(MARGIN);
135 aCoordGrpLayout->setSpacing(SPACING);
137 myCoordBtn = new QPushButton(aCoordGrp);
138 myCoordBtn->setIcon(iconSelect);
139 myCoordBtn->setCheckable(true);
141 QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
142 myX = new SMESHGUI_SpinBox(aCoordGrp);
144 QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
145 myY = new SMESHGUI_SpinBox(aCoordGrp);
147 QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
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);
154 aCoordGrpLayout->addWidget(myCoordBtn);
155 aCoordGrpLayout->addWidget(aXLabel);
156 aCoordGrpLayout->addWidget(myX);
157 aCoordGrpLayout->addWidget(aYLabel);
158 aCoordGrpLayout->addWidget(myY);
159 aCoordGrpLayout->addWidget(aZLabel);
160 aCoordGrpLayout->addWidget(myZ);
164 QGroupBox* aMethodGrp = new QGroupBox(tr("METHOD"), aFrame);
165 QHBoxLayout* aMethodGrpLayout = new QHBoxLayout(aMethodGrp);
166 aMethodGrpLayout->setMargin(MARGIN);
167 aMethodGrpLayout->setSpacing(SPACING);
169 myMoveRBtn = new QRadioButton(tr("MOVE_EXISTING_METHOD"), aMethodGrp);
170 myCreateRBtn = new QRadioButton(tr("CREATE_NEW_METHOD"), aMethodGrp);
172 aMethodGrpLayout->addWidget(myMoveRBtn);
173 aMethodGrpLayout->addWidget(myCreateRBtn);
177 myNodeToMoveGrp = new QGroupBox(tr("NODE_2MOVE"), aFrame);
179 QLabel* idLabel = new QLabel(tr("NODE_2MOVE_ID"), myNodeToMoveGrp);
180 myIdBtn = new QPushButton(myNodeToMoveGrp);
181 myIdBtn->setIcon(iconSelect);
182 myIdBtn->setCheckable(true);
183 myId = new QLineEdit(myNodeToMoveGrp);
184 myId->setValidator(new SMESHGUI_IdValidator(this, 1));
185 myAutoSearchChkBox = new QCheckBox( tr("AUTO_SEARCH"), myNodeToMoveGrp);
186 myPreviewChkBox = new QCheckBox( tr("PREVIEW"), myNodeToMoveGrp);
188 QGridLayout* myNodeToMoveGrpLayout = new QGridLayout(myNodeToMoveGrp);
189 myNodeToMoveGrpLayout->setSpacing(SPACING);
190 myNodeToMoveGrpLayout->setMargin(MARGIN);
192 myNodeToMoveGrpLayout->addWidget( idLabel, 0, 0 );
193 myNodeToMoveGrpLayout->addWidget( myIdBtn, 0, 1 );
194 myNodeToMoveGrpLayout->addWidget( myId, 0, 2 );
195 myNodeToMoveGrpLayout->addWidget( myAutoSearchChkBox, 1, 0, 1, 3 );
196 myNodeToMoveGrpLayout->addWidget( myPreviewChkBox, 2, 0, 1, 3 );
198 QVBoxLayout* aLay = new QVBoxLayout(aFrame);
199 aLay->addWidget(aPixGrp);
200 aLay->addWidget(aCoordGrp);
201 aLay->addWidget(aMethodGrp);
202 aLay->addWidget(myNodeToMoveGrp);
204 connect(myCoordBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
205 connect(myMoveRBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
206 connect(myCreateRBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
207 connect(myIdBtn, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
208 connect(myAutoSearchChkBox, SIGNAL (toggled(bool)), this, SLOT(ButtonToggled(bool)));
210 myMoveRBtn->setChecked(true);
211 myIdBtn->setDown(true);
212 myAutoSearchChkBox->setChecked(true);
217 //================================================================================
219 * \brief SLOT called when any button is toggled
220 * \param bool - on or off
222 //================================================================================
224 void SMESHGUI_MakeNodeAtPointDlg::ButtonToggled (bool on)
226 const QObject* aSender = sender();
228 if ( aSender == myCoordBtn ) // button to set coord by node selection
230 if ( myIdBtn->isEnabled() )
231 myIdBtn->setDown( !on );
233 else if ( aSender == myIdBtn ) // button to select a node to move
235 myCoordBtn->setDown( !on );
237 else if ( aSender == myMoveRBtn ) // move node method
239 myNodeToMoveGrp->setEnabled( true );
241 else if ( aSender == myCreateRBtn ) // create node method
243 myNodeToMoveGrp->setEnabled( false );
244 myCoordBtn->setDown( true );
247 if ( aSender == myAutoSearchChkBox ) // automatic node search
251 myId->setReadOnly ( true );
252 myIdBtn->setDown( false );
253 myIdBtn->setEnabled( false );
254 myCoordBtn->setDown( true );
257 myId->setReadOnly ( false );
258 myIdBtn->setEnabled( true );
263 //================================================================================
267 //================================================================================
269 SMESHGUI_MakeNodeAtPointOp::SMESHGUI_MakeNodeAtPointOp()
272 myDlg = new SMESHGUI_MakeNodeAtPointDlg;
274 myHelpFileName = "mesh_through_point_page.html";
276 // connect signals and slots
277 connect(myDlg->myX, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
278 connect(myDlg->myY, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
279 connect(myDlg->myZ, SIGNAL (valueChanged(double)), this, SLOT(redisplayPreview()));
280 connect(myDlg->myId,SIGNAL (textChanged(const QString&)),SLOT(redisplayPreview()));
281 connect(myDlg->myPreviewChkBox, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
282 connect(myDlg->myAutoSearchChkBox,SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
283 connect(myDlg->myMoveRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
284 connect(myDlg->myCreateRBtn, SIGNAL (toggled(bool)),SLOT(redisplayPreview()));
287 //=======================================================================
288 // function : startOperation()
289 // purpose : Init dialog fields, connect signals and slots, show dialog
290 //=======================================================================
291 void SMESHGUI_MakeNodeAtPointOp::startOperation()
296 // init simulation with a current View
297 if ( mySimulation ) delete mySimulation;
298 mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
299 vtkProperty* aProp = vtkProperty::New();
300 aProp->SetRepresentationToWireframe();
301 aProp->SetColor(250, 0, 250);
302 aProp->SetPointSize(5);
303 aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
304 mySimulation->GetActor()->SetProperty(aProp);
307 // SalomeApp_TypeFilter depends on a current study
308 if ( myFilter ) delete myFilter;
309 QList<SUIT_SelectionFilter*> filters;
310 filters.append( new SalomeApp_TypeFilter((SalomeApp_Study*)study(), "SMESH" ));
311 TColStd_MapOfInteger vertexType;
312 vertexType.Add( TopAbs_VERTEX );
313 filters.append( new SMESH_NumberFilter("GEOM", TopAbs_VERTEX, 1, vertexType ));
314 myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
317 SMESHGUI_SelectionOp::startOperation(); // this method should be called only after filter creation
318 //activateSelection(); // set filters // called inside of previous statement
320 myDlg->myX->SetValue(0);
321 myDlg->myY->SetValue(0);
322 myDlg->myZ->SetValue(0);
323 myDlg->myId->setText("");
326 onSelectionDone(); // init myMeshActor
329 // myMeshOldDisplayMode = myMeshActor->GetRepresentation();
330 // myMeshActor->SetRepresentation( VTK_WIREFRAME );
331 myMeshActor->SetPointRepresentation(true);
332 SMESH::RepaintCurrentView();
337 //================================================================================
339 * \brief Stops operation
341 //================================================================================
343 void SMESHGUI_MakeNodeAtPointOp::stopOperation()
346 mySimulation->SetVisibility(false);
348 // myMeshActor->SetRepresentation( myMeshOldDisplayMode );
349 myMeshActor->SetPointRepresentation(false);
350 SMESH::RepaintCurrentView();
353 selectionMgr()->removeFilter( myFilter );
354 SMESHGUI_SelectionOp::stopOperation();
357 //================================================================================
359 * \brief perform it's intention action: move or create a node
361 //================================================================================
363 bool SMESHGUI_MakeNodeAtPointOp::onApply()
365 if( isStudyLocked() )
368 if ( !myMeshActor ) {
369 SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ),
370 tr("INVALID_MESH") );
376 if ( !isValid( msg ) ) { // node id is invalid
378 SUIT_MessageBox::warning( dlg(), tr( "SMESH_WRN_WARNING" ),
386 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
387 if (aMesh->_is_nil()) {
388 SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
389 tr("SMESHG_NO_MESH") );
392 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
393 if (aMeshEditor->_is_nil())
397 if ( myDlg->myCreateRBtn->isDown() )
399 aResult = aMeshEditor->AddNode(myDlg->myX->GetValue(),
400 myDlg->myY->GetValue(),
401 myDlg->myZ->GetValue());
405 int anId = myDlg->myId->text().toInt();
406 aResult = aMeshEditor->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
407 myDlg->myY->GetValue(),
408 myDlg->myZ->GetValue(),
413 QStringList aParameters;
414 aParameters << myDlg->myX->text();
415 aParameters << myDlg->myY->text();
416 aParameters << myDlg->myZ->text();
417 aMesh->SetParameters( SMESHGUI::JoinObjectParameters(aParameters) );
419 myDlg->myId->setText("");
422 selectionMgr()->setSelectedObjects(aList,false);
423 aList.Append(myMeshActor->getIO());
424 selectionMgr()->setSelectedObjects(aList,false);
428 catch (const SALOME::SALOME_Exception& S_ex) {
429 SalomeApp_Tools::QtCatchCorbaException(S_ex);
437 //================================================================================
439 * \brief Check selected node id validity
441 //================================================================================
443 bool SMESHGUI_MakeNodeAtPointOp::isValid( QString& msg )
447 myDlg->myMoveRBtn->isDown() &&
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";
459 ok = myDlg->myX->isValid( msg, !myNoPreview ) && ok;
460 ok = myDlg->myY->isValid( msg, !myNoPreview ) && ok;
461 ok = myDlg->myZ->isValid( msg, !myNoPreview ) && ok;
466 //================================================================================
468 * \brief SLOT called when selection changed
470 //================================================================================
472 void SMESHGUI_MakeNodeAtPointOp::onSelectionDone()
474 if ( !myDlg->isVisible() || !myDlg->isEnabled() )
478 selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
479 if (aList.Extent() != 1)
481 Handle(SALOME_InteractiveObject) anIO = aList.First();
482 SMESH_Actor* aMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
484 if (!aMeshActor) { // coord by geom
485 if ( myDlg->myCoordBtn->isDown() ) {
486 GEOM::GEOM_Object_var geom = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
487 if ( !geom->_is_nil() ) {
488 TopoDS_Vertex aShape;
489 if ( GEOMBase::GetShape(geom, aShape) &&
490 aShape.ShapeType() == TopAbs_VERTEX ) {
491 gp_Pnt P = BRep_Tool::Pnt(aShape);
493 myDlg->myX->SetValue(P.X());
494 myDlg->myY->SetValue(P.Y());
495 myDlg->myZ->SetValue(P.Z());
505 myMeshActor = aMeshActor;
508 int nbElems = SMESH::GetNameOfSelectedElements(selector(),anIO, aString);
510 if (SMDS_Mesh* aMesh = aMeshActor->GetObject()->GetMesh()) {
511 if (const SMDS_MeshNode* aNode = aMesh->FindNode(aString.toInt())) {
513 if ( myDlg->myCoordBtn->isDown() ) { // set coord
514 myDlg->myX->SetValue(aNode->X());
515 myDlg->myY->SetValue(aNode->Y());
516 myDlg->myZ->SetValue(aNode->Z());
520 else if ( myDlg->myIdBtn->isDown() &&
521 myDlg->myIdBtn->isEnabled() ) { // set node to move
522 myDlg->myId->setText(aString);
533 //================================================================================
535 * \brief update preview
537 //================================================================================
539 void SMESHGUI_MakeNodeAtPointOp::redisplayPreview()
545 SMESH::MeshPreviewStruct_var aMeshPreviewStruct;
547 bool moveShown = false;
548 if ( myDlg->myMoveRBtn->isDown() && // Move method
551 const bool autoSearch = myDlg->myAutoSearchChkBox->isChecked();
552 const bool preview = myDlg->myPreviewChkBox->isChecked();
554 myDlg->myId->setText("");
556 if ( preview && ( autoSearch || isValid( msg ) ))
559 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(myMeshActor->getIO());
560 if (!aMesh->_is_nil()) {
561 SMESH::SMESH_MeshEditor_var aPreviewer = aMesh->GetMeshEditPreviewer();
562 if (!aPreviewer->_is_nil())
564 SUIT_OverrideCursor aWaitCursor;
566 // find id and/or just compute preview
567 int anId = aPreviewer->MoveClosestNodeToPoint(myDlg->myX->GetValue(),
568 myDlg->myY->GetValue(),
569 myDlg->myZ->GetValue(),
570 myDlg->myId->text().toInt());
571 if ( autoSearch ) { // set found id
574 idTxt = idTxt.arg( anId );
577 myDlg->myId->setText( idTxt );
579 if ( preview ) { // fill preview data
580 aMeshPreviewStruct = aPreviewer->GetPreviewData();
581 moveShown = ( anId > 0 );
592 aMeshPreviewStruct = new SMESH::MeshPreviewStruct();
594 aMeshPreviewStruct->nodesXYZ.length(1);
595 aMeshPreviewStruct->nodesXYZ[0].x = myDlg->myX->GetValue();
596 aMeshPreviewStruct->nodesXYZ[0].y = myDlg->myY->GetValue();
597 aMeshPreviewStruct->nodesXYZ[0].z = myDlg->myZ->GetValue();
599 aMeshPreviewStruct->elementTypes.length(1);
600 aMeshPreviewStruct->elementTypes[0].SMDS_ElementType = SMESH::NODE;
601 aMeshPreviewStruct->elementTypes[0].isPoly = false;
602 aMeshPreviewStruct->elementTypes[0].nbNodesInElement = 1;
604 aMeshPreviewStruct->elementConnectivities.length(1);
605 aMeshPreviewStruct->elementConnectivities[0] = 0;
609 if ( aMeshPreviewStruct.operator->() )
611 mySimulation->SetData(aMeshPreviewStruct._retn());
615 mySimulation->SetVisibility(false);
621 //================================================================================
623 * \brief Activate Node selection
625 //================================================================================
627 void SMESHGUI_MakeNodeAtPointOp::activateSelection()
629 selectionMgr()->clearFilters();
630 SMESH::SetPointRepresentation(false);
631 selectionMgr()->installFilter( myFilter );
632 setSelectionMode( NodeSelection );
635 //================================================================================
639 //================================================================================
641 SMESHGUI_MakeNodeAtPointOp::~SMESHGUI_MakeNodeAtPointOp()
643 if ( myDlg ) delete myDlg;
644 if ( mySimulation ) delete mySimulation;
645 if ( myFilter ) delete myFilter;
648 //================================================================================
650 * \brief Gets dialog of this operation
651 * \retval LightApp_Dialog* - pointer to dialog of this operation
653 //================================================================================
655 LightApp_Dialog* SMESHGUI_MakeNodeAtPointOp::dlg() const