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_MeshEditPreview.cxx
28 #include "SMESHGUI_MeshEditPreview.h"
30 #include "VTKViewer_CellLocationsArray.h"
31 #include "SVTK_ViewWindow.h"
33 #include "SMESH_Actor.h"
34 #include "SMESH_ActorUtils.h"
35 #include "SMESHGUI_VTKUtils.h"
38 #include <vtkPoints.h>
39 #include <vtkIdList.h>
40 #include <vtkCellArray.h>
41 #include <vtkUnsignedCharArray.h>
42 #include <vtkUnstructuredGrid.h>
43 #include <vtkUnstructuredGridWriter.h>
44 #include <vtkDataSetMapper.h>
45 #include <vtkProperty.h>
51 #include "SALOMEconfig.h"
52 #include CORBA_SERVER_HEADER(SMESH_Mesh)
54 using namespace SMESH;
56 //================================================================================
60 //================================================================================
62 SMESHGUI_MeshEditPreview::SMESHGUI_MeshEditPreview(SVTK_ViewWindow* theViewWindow):
63 myViewWindow(theViewWindow)
65 myGrid = vtkUnstructuredGrid::New();
67 // Create and display actor
68 vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
69 aMapper->SetInput( myGrid );
71 vtkProperty* aProp = vtkProperty::New();
72 vtkFloatingPointType anRGB[3];
73 GetColor( "SMESH", "selection_element_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
74 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
76 myPreviewActor = SALOME_Actor::New();
77 myPreviewActor->SetInfinitive(true);
78 myPreviewActor->VisibilityOn();
79 myPreviewActor->PickableOff();
80 myPreviewActor->SetProperty( aProp );
83 myPreviewActor->SetMapper( aMapper );
86 myViewWindow->AddActor(myPreviewActor);
90 //================================================================================
94 //================================================================================
96 SMESHGUI_MeshEditPreview::~SMESHGUI_MeshEditPreview()
100 myViewWindow->RemoveActor(myPreviewActor);
101 myPreviewActor->Delete();
105 //================================================================================
107 * \brief Returns vtk cell type
109 //================================================================================
111 vtkIdType getCellType( const SMDSAbs_ElementType theType,
113 const int theNbNodes )
118 if( theNbNodes == 2 ) return VTK_LINE;
119 else if ( theNbNodes == 3 ) return VTK_QUADRATIC_EDGE;
120 else return VTK_EMPTY_CELL;
123 if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
124 else if ( theNbNodes == 3 ) return VTK_TRIANGLE;
125 else if ( theNbNodes == 4 ) return VTK_QUAD;
126 else if ( theNbNodes == 6 ) return VTK_QUADRATIC_TRIANGLE;
127 else if ( theNbNodes == 8 ) return VTK_QUADRATIC_QUAD;
128 else return VTK_EMPTY_CELL;
131 if (thePoly && theNbNodes>3 ) return VTK_CONVEX_POINT_SET;
132 else if ( theNbNodes == 4 ) return VTK_TETRA;
133 else if ( theNbNodes == 5 ) return VTK_PYRAMID;
134 else if ( theNbNodes == 6 ) return VTK_WEDGE;
135 else if ( theNbNodes == 8 ) return VTK_HEXAHEDRON;
136 else if ( theNbNodes == 10 ) {
137 return VTK_QUADRATIC_TETRA;
139 else if ( theNbNodes == 20 ) {
140 return VTK_QUADRATIC_HEXAHEDRON;
142 else if ( theNbNodes==13 || theNbNodes==15 ) {
143 return VTK_CONVEX_POINT_SET;
145 else return VTK_EMPTY_CELL;
147 default: return VTK_EMPTY_CELL;
151 //================================================================================
153 * \brief Set preview data
155 //================================================================================
157 void SMESHGUI_MeshEditPreview::SetData (const SMESH::MeshPreviewStruct* previewData)
160 const SMESH::nodes_array& aNodesXYZ = previewData->nodesXYZ;
161 vtkPoints* aPoints = vtkPoints::New();
162 aPoints->SetNumberOfPoints(aNodesXYZ.length());
164 for ( int i = 0; i < aNodesXYZ.length(); i++ ) {
165 aPoints->SetPoint( i, aNodesXYZ[i].x, aNodesXYZ[i].y, aNodesXYZ[i].z );
167 myGrid->SetPoints(aPoints);
172 const SMESH::long_array& anElemConnectivity = previewData->elementConnectivities;
173 const SMESH::types_array& anElemTypes = previewData->elementTypes;
175 vtkIdType aCellsSize = anElemConnectivity.length() + anElemTypes.length();
176 vtkIdType aNbCells = anElemTypes.length();
178 vtkCellArray* aConnectivity = vtkCellArray::New();
179 aConnectivity->Allocate( aCellsSize, 0 );
181 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
182 aCellTypesArray->SetNumberOfComponents( 1 );
183 aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
185 vtkIdList *anIdList = vtkIdList::New();
188 for ( int i = 0; i < anElemTypes.length(); i++ ) {
189 const ElementSubType& anElementSubType = anElemTypes[i];
190 SMDSAbs_ElementType aType = SMDSAbs_ElementType(anElementSubType.SMDS_ElementType);
191 vtkIdType aNbNodes = anElementSubType.nbNodesInElement;
192 anIdList->SetNumberOfIds( aNbNodes );
194 for ( vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++ ){
195 anIdList->SetId( aNodeId, anElemConnectivity[aNodePos] );
199 aConnectivity->InsertNextCell( anIdList );
200 aCellTypesArray->InsertNextValue( getCellType( aType,
201 anElemTypes[i].isPoly,
206 // Insert cells in grid
207 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
208 aCellLocationsArray->SetNumberOfComponents( 1 );
209 aCellLocationsArray->SetNumberOfTuples( aNbCells );
211 aConnectivity->InitTraversal();
212 for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
213 aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
215 myGrid->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
217 myPreviewActor->GetMapper()->Update();
219 aCellTypesArray->Delete();
220 aCellLocationsArray->Delete();
221 aConnectivity->Delete();
226 //================================================================================
228 * \brief Set visibility
230 //================================================================================
232 void SMESHGUI_MeshEditPreview::SetVisibility (bool theVisibility)
234 myPreviewActor->SetVisibility(theVisibility);
235 RepaintCurrentView();