]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHGUI/SMESHGUI_NodesOp.cxx
Salome HOME
2313f1aca387a1f1ffc0eae38ec7a83eb976b174
[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();
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   if( mySimulation )
249     delete mySimulation;
250   mySimulation = 0;
251   SMESHGUI_SelectionOp::commitOperation();
252 }
253
254 //=================================================================================
255 // function :
256 // purpose  :
257 //=================================================================================
258 void SMESHGUI_NodesOp::abortOperation()
259 {
260   if( mySimulation )
261     delete mySimulation;
262   mySimulation = 0;
263   SMESHGUI_SelectionOp::abortOperation();
264 }
265
266 //=================================================================================
267 // function :
268 // purpose  :
269 //=================================================================================
270 void SMESHGUI_NodesOp::selectionDone()
271 {
272   if( !mySimulation )
273     return;
274     
275   mySimulation->SetVisibility(false);
276   SMESH::SetPointRepresentation(true);
277
278   const SALOME_ListIO& aList = selector()->StoredIObjects();
279   if (aList.Extent() == 1) {
280     Handle(SALOME_InteractiveObject) anIO = aList.First();
281     if (anIO->hasEntry()) {
282       myMesh = SMESH::GetMeshByIO(anIO);
283       if (myMesh->_is_nil()) return;
284       QString aText;
285       if (SMESH::GetNameOfSelectedNodes(selector(),anIO,aText) == 1) {
286         if (SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh.in())) {
287           if (SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh()) {
288             if (const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
289         myDlg->setCoords( aNode->X(), aNode->Y(), aNode->Z() );
290             }
291           }
292         }
293       }
294       double x, y, z;
295       myDlg->coords( x, y, z );
296       mySimulation->SetPosition( x, y, z );
297     }
298   } 
299 }
300
301 //=================================================================================
302 // function :
303 // purpose  :
304 //=================================================================================
305 void SMESHGUI_NodesOp::initDialog()
306 {
307   myDlg->setCoords( 0, 0, 0 );
308 }
309
310 //=================================================================================
311 // function :
312 // purpose  :
313 //=================================================================================
314 bool SMESHGUI_NodesOp::onApply()
315 {
316   if( getSMESHGUI()->isActiveStudyLocked() || !mySimulation )
317     return false;
318
319   if (myMesh->_is_nil()) {
320     SUIT_MessageBox::warn1( myDlg, tr("SMESH_WRN_WARNING"),
321                             tr("MESH_IS_NOT_SELECTED"), tr("SMESH_BUT_OK"));
322     return false;
323   }
324
325   double x, y, z; myDlg->coords( x, y, z );
326   mySimulation->SetVisibility(false);
327   SMESH::AddNode(myMesh,x,y,z);
328   SMESH::SetPointRepresentation(true);
329
330   // select myMesh
331   SALOME_ListIO aList;
332   selectionMgr()->selectedObjects(aList);
333   if (aList.Extent() != 1) {
334     if (SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView()) {
335       vtkActorCollection *aCollection = aViewWindow->getRenderer()->GetActors();
336       aCollection->InitTraversal();
337       while (vtkActor *anAct = aCollection->GetNextActor()) {
338         if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
339           if (anActor->hasIO()) {
340             if (SMESH_MeshObj *aMeshObj = dynamic_cast<SMESH_MeshObj*>(anActor->GetObject().get())) {
341               if (myMesh->_is_equivalent(aMeshObj->GetMeshServer())) {
342                 aList.Clear();
343                 aList.Append(anActor->getIO());
344                 selectionMgr()->setSelectedObjects(aList, false);
345                 break;
346               }
347             }
348           }
349         }
350       }
351     }
352   }
353   return true; 
354 }
355
356 //=================================================================================
357 // function :
358 // purpose  :
359 //=================================================================================
360 void SMESHGUI_NodesOp::onValueChanged( double )
361 {
362   double vx, vy, vz; myDlg->coords( vx, vy, vz );
363   if( mySimulation )
364     mySimulation->SetPosition( vx, vy, vz );
365 }
366
367
368