Salome HOME
Dialogs were divided to "operation and dialog"
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_NodesOp.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_NodesOp.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_NodesOp.h"
30 #include <SMESHGUI_NodesDlg.h>
31 #include <SMESHGUI_Utils.h>
32 #include <SMESHGUI_VTKUtils.h>
33 #include <SMESHGUI_MeshUtils.h>
34 #include <SMESHGUI.h>
35
36 #include <SMESH_ActorUtils.h>
37 #include <SMESH_Actor.h>
38 #include <SMESH_ObjectDef.h>
39
40 #include <SalomeApp_SelectionMgr.h>
41
42 #include <SALOME_ListIO.hxx>
43
44 #include <SUIT_OverrideCursor.h>
45 #include <SUIT_MessageBox.h>
46
47 #include <SVTK_ViewWindow.h>
48 #include <SVTK_Selector.h>
49
50 #include <SMDS_Mesh.hxx>
51 #include <SMDS_MeshNode.hxx>
52
53 #include "utilities.h"
54
55 #include <vtkDataSetMapper.h>
56 #include <vtkPoints.h>
57 #include <vtkUnstructuredGrid.h>
58 #include <vtkUnsignedCharArray.h>
59 #include <vtkIntArray.h>
60 #include <vtkIdList.h>
61 #include <vtkCellArray.h>
62 #include <vtkActorCollection.h>
63 #include <vtkRenderer.h>
64
65 namespace SMESH {
66
67   void AddNode (SMESH::SMESH_Mesh_ptr theMesh, float x, float y, float z)
68   {
69     SUIT_OverrideCursor wc;
70     try {
71       _PTR(SObject) aSobj = SMESH::FindSObject(theMesh);
72       SMESH::SMESH_MeshEditor_var aMeshEditor = theMesh->GetMeshEditor();
73       aMeshEditor->AddNode(x,y,z);
74       _PTR(Study) aStudy = GetActiveStudyDocument();
75       CORBA::Long anId = aStudy->StudyId();
76       if (TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId, aSobj->GetID().c_str())) {
77         aVisualObj->Update(true);
78       }
79     } catch (SALOME::SALOME_Exception& exc) {
80       INFOS("Follow exception was cought:\n\t" << exc.details.text);
81     } catch (const std::exception& exc) {
82       INFOS("Follow exception was cought:\n\t" << exc.what());
83     } catch (...) {
84       INFOS("Unknown exception was cought !!!");
85     }
86   }
87
88   class TNodeSimulation {
89     SVTK_ViewWindow* myViewWindow;
90
91     SALOME_Actor *myPreviewActor;
92     vtkDataSetMapper* myMapper;
93     vtkPoints* myPoints;
94
95   public:
96     TNodeSimulation(SVTK_ViewWindow* theViewWindow):
97       myViewWindow(theViewWindow)
98     {
99       vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
100
101       // Create points
102       myPoints = vtkPoints::New();
103       myPoints->SetNumberOfPoints(1);
104       myPoints->SetPoint(0,0.0,0.0,0.0);
105
106       // Create cells
107       vtkIdList *anIdList = vtkIdList::New();
108       anIdList->SetNumberOfIds(1);
109
110       vtkCellArray *aCells = vtkCellArray::New();
111       aCells->Allocate(2, 0);
112
113       vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
114       aCellTypesArray->SetNumberOfComponents(1);
115       aCellTypesArray->Allocate(1);
116
117       anIdList->SetId(0, 0);
118       aCells->InsertNextCell(anIdList);
119       aCellTypesArray->InsertNextValue(VTK_VERTEX);
120
121       vtkIntArray* aCellLocationsArray = vtkIntArray::New();
122       aCellLocationsArray->SetNumberOfComponents(1);
123       aCellLocationsArray->SetNumberOfTuples(1);
124
125       aCells->InitTraversal();
126       vtkIdType npts;
127       aCellLocationsArray->SetValue(0, aCells->GetTraversalLocation(npts));
128
129       aGrid->SetCells(aCellTypesArray, aCellLocationsArray, aCells);
130
131       aGrid->SetPoints(myPoints);
132       aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aCells);
133       aCellLocationsArray->Delete();
134       aCellTypesArray->Delete();
135       aCells->Delete();
136       anIdList->Delete();
137
138       // Create and display actor
139       myMapper = vtkDataSetMapper::New();
140       myMapper->SetInput(aGrid);
141       aGrid->Delete();
142
143       myPreviewActor = SALOME_Actor::New();
144       myPreviewActor->SetInfinitive(true);
145       myPreviewActor->VisibilityOff();
146       myPreviewActor->PickableOff();
147       myPreviewActor->SetMapper(myMapper);
148
149       vtkProperty* aProp = vtkProperty::New();
150       aProp->SetRepresentationToPoints();
151
152       float anRGB[3];
153       GetColor( "SMESH", "node_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 255, 0 ) );
154       aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
155
156       float aPointSize = GetFloat( "SMESH:node_size", 3 );
157       aProp->SetPointSize( aPointSize );
158
159       myPreviewActor->SetProperty( aProp );
160       aProp->Delete();
161
162       myViewWindow->AddActor(myPreviewActor);
163     }
164
165     void SetPosition (float x, float y, float z)
166     {
167       myPoints->SetPoint(0,x,y,z);
168       myPoints->Modified();
169       SetVisibility(true);
170     }
171
172     void SetVisibility (bool theVisibility)
173     {
174       myPreviewActor->SetVisibility(theVisibility);
175       RepaintCurrentView();
176     }
177
178     ~TNodeSimulation()
179     {
180       SetVisibility( false );
181       myViewWindow->RemoveActor(myPreviewActor);
182       myPreviewActor->Delete();
183
184       myMapper->RemoveAllInputs();
185       myMapper->Delete();
186
187       myPoints->Delete();
188     }
189   };
190 }
191
192
193 //=================================================================================
194 // function : 
195 // purpose  :
196 //=================================================================================
197 SMESHGUI_NodesOp::SMESHGUI_NodesOp()
198 : SMESHGUI_SelectionOp( NodeSelection ),
199   myDlg( 0 ),
200   mySimulation( 0 )
201 {
202 }
203
204 //=================================================================================
205 // function : 
206 // purpose  :
207 //=================================================================================
208 SMESHGUI_NodesOp::~SMESHGUI_NodesOp()
209 {
210   if( myDlg )
211     delete myDlg;
212 }
213
214 //=================================================================================
215 // function :
216 // purpose  :
217 //=================================================================================
218 SalomeApp_Dialog* SMESHGUI_NodesOp::dlg() const
219 {
220   return myDlg;
221 }
222
223 //=================================================================================
224 // function :
225 // purpose  :
226 //=================================================================================
227 void SMESHGUI_NodesOp::startOperation()
228 {
229   if( !myDlg )
230   {
231     myDlg = new SMESHGUI_NodesDlg( getSMESHGUI() );
232     connect( myDlg, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged( double ) ) );
233     connect( myDlg, SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
234   }
235
236   SMESHGUI_SelectionOp::startOperation();
237     
238   mySimulation = new SMESH::TNodeSimulation( viewWindow() );
239   myDlg->show();
240 }
241
242 //=================================================================================
243 // function :
244 // purpose  :
245 //=================================================================================
246 void SMESHGUI_NodesOp::commitOperation()
247 {
248   delete mySimulation;
249   mySimulation = 0;
250   SMESHGUI_SelectionOp::commitOperation();
251 }
252
253 //=================================================================================
254 // function :
255 // purpose  :
256 //=================================================================================
257 void SMESHGUI_NodesOp::abortOperation()
258 {
259   delete mySimulation;
260   mySimulation = 0;
261   SMESHGUI_SelectionOp::abortOperation();
262 }
263
264 //=================================================================================
265 // function :
266 // purpose  :
267 //=================================================================================
268 void SMESHGUI_NodesOp::selectionDone()
269 {
270   if( !mySimulation )
271     return;
272     
273   mySimulation->SetVisibility(false);
274   SMESH::SetPointRepresentation(true);
275
276   const SALOME_ListIO& aList = selector()->StoredIObjects();
277   if (aList.Extent() == 1) {
278     Handle(SALOME_InteractiveObject) anIO = aList.First();
279     if (anIO->hasEntry()) {
280       myMesh = SMESH::GetMeshByIO(anIO);
281       if (myMesh->_is_nil()) return;
282       QString aText;
283       if (SMESH::GetNameOfSelectedNodes(selector(),anIO,aText) == 1) {
284         if (SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh.in())) {
285           if (SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh()) {
286             if (const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
287         myDlg->setCoords( aNode->X(), aNode->Y(), aNode->Z() );
288             }
289           }
290         }
291       }
292       double x, y, z;
293       myDlg->coords( x, y, z );
294       mySimulation->SetPosition( x, y, z );
295     }
296   } 
297 }
298
299 //=================================================================================
300 // function :
301 // purpose  :
302 //=================================================================================
303 void SMESHGUI_NodesOp::initDialog()
304 {
305   myDlg->setCoords( 0, 0, 0 );
306 }
307
308 //=================================================================================
309 // function :
310 // purpose  :
311 //=================================================================================
312 bool SMESHGUI_NodesOp::onApply()
313 {
314   if( getSMESHGUI()->isActiveStudyLocked() || !mySimulation )
315     return false;
316
317   if (myMesh->_is_nil()) {
318     SUIT_MessageBox::warn1( myDlg, tr("SMESH_WRN_WARNING"),
319                             tr("MESH_IS_NOT_SELECTED"), tr("SMESH_BUT_OK"));
320     return false;
321   }
322
323   double x, y, z; myDlg->coords( x, y, z );
324   mySimulation->SetVisibility(false);
325   SMESH::AddNode(myMesh,x,y,z);
326   SMESH::SetPointRepresentation(true);
327
328   // select myMesh
329   SALOME_ListIO aList;
330   selectionMgr()->selectedObjects(aList);
331   if (aList.Extent() != 1) {
332     if (SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView()) {
333       vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
334       aCollection->InitTraversal();
335       while (vtkActor *anAct = aCollection->GetNextActor()) {
336         if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
337           if (anActor->hasIO()) {
338             if (SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>(anActor->GetObject().get())) {
339               if (myMesh->_is_equivalent(aMeshObj->GetMeshServer())) {
340                 aList.Clear();
341                 aList.Append(anActor->getIO());
342                 selectionMgr()->setSelectedObjects(aList, false);
343                 break;
344               }
345             }
346           }
347         }
348       }
349     }
350   }
351   return true; 
352 }
353
354 //=================================================================================
355 // function :
356 // purpose  :
357 //=================================================================================
358 void SMESHGUI_NodesOp::onValueChanged( double )
359 {
360   double vx, vy, vz; myDlg->coords( vx, vy, vz );
361   if( mySimulation )
362     mySimulation->SetPosition( vx, vy, vz );
363 }
364
365
366