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
22 // File : SMESHGUI_MeshEditPreview.cxx
23 // Author : Open CASCADE S.A.S.
27 #include "SMESHGUI_MeshEditPreview.h"
29 #include "SMESHGUI_VTKUtils.h"
31 #include <SMESH_Actor.h>
32 #include <SMESH_ActorUtils.h>
34 // SALOME GUI includes
35 #include <VTKViewer_CellLocationsArray.h>
36 #include <SVTK_ViewWindow.h>
39 #include <vtkPoints.h>
40 #include <vtkIdList.h>
41 #include <vtkCellArray.h>
42 #include <vtkUnsignedCharArray.h>
43 #include <vtkUnstructuredGrid.h>
44 #include <vtkUnstructuredGridWriter.h>
45 #include <vtkDataSetMapper.h>
46 #include <vtkProperty.h>
52 #include <SALOMEconfig.h>
53 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
55 //================================================================================
59 //================================================================================
61 SMESHGUI_MeshEditPreview::SMESHGUI_MeshEditPreview(SVTK_ViewWindow* theViewWindow):
62 myViewWindow(theViewWindow)
64 myGrid = vtkUnstructuredGrid::New();
66 // Create and display actor
67 vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
68 aMapper->SetInput( myGrid );
70 myPreviewActor = SALOME_Actor::New();
71 myPreviewActor->SetInfinitive(true);
72 myPreviewActor->VisibilityOn();
73 myPreviewActor->PickableOff();
75 vtkFloatingPointType anRGB[3];
76 SMESH::GetColor( "SMESH", "selection_element_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
77 SetColor( anRGB[0], anRGB[1], anRGB[2] );
79 myPreviewActor->SetMapper( aMapper );
82 myViewWindow->AddActor(myPreviewActor);
86 //================================================================================
90 //================================================================================
92 SMESHGUI_MeshEditPreview::~SMESHGUI_MeshEditPreview()
96 myViewWindow->RemoveActor(myPreviewActor);
97 myPreviewActor->Delete();
101 //================================================================================
103 * \brief Returns vtk cell type
105 //================================================================================
107 vtkIdType getCellType( const SMDSAbs_ElementType theType,
109 const int theNbNodes )
113 case SMDSAbs_Node: return VTK_VERTEX;
115 if( theNbNodes == 2 ) return VTK_LINE;
116 else if ( theNbNodes == 3 ) return VTK_QUADRATIC_EDGE;
117 else return VTK_EMPTY_CELL;
120 if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
121 else if ( theNbNodes == 3 ) return VTK_TRIANGLE;
122 else if ( theNbNodes == 4 ) return VTK_QUAD;
123 else if ( theNbNodes == 6 ) return VTK_QUADRATIC_TRIANGLE;
124 else if ( theNbNodes == 8 ) return VTK_QUADRATIC_QUAD;
125 else return VTK_EMPTY_CELL;
128 if (thePoly && theNbNodes>3 ) return VTK_CONVEX_POINT_SET;
129 else if ( theNbNodes == 4 ) return VTK_TETRA;
130 else if ( theNbNodes == 5 ) return VTK_PYRAMID;
131 else if ( theNbNodes == 6 ) return VTK_WEDGE;
132 else if ( theNbNodes == 8 ) return VTK_HEXAHEDRON;
133 else if ( theNbNodes == 10 ) {
134 return VTK_QUADRATIC_TETRA;
136 else if ( theNbNodes == 20 ) {
137 return VTK_QUADRATIC_HEXAHEDRON;
139 else if ( theNbNodes==13 || theNbNodes==15 ) {
140 return VTK_CONVEX_POINT_SET;
142 else return VTK_EMPTY_CELL;
144 default: return VTK_EMPTY_CELL;
148 //================================================================================
150 * \brief Set preview data
152 //================================================================================
154 void SMESHGUI_MeshEditPreview::SetData (const SMESH::MeshPreviewStruct* previewData)
157 const SMESH::nodes_array& aNodesXYZ = previewData->nodesXYZ;
158 vtkPoints* aPoints = vtkPoints::New();
159 aPoints->SetNumberOfPoints(aNodesXYZ.length());
161 for ( int i = 0; i < aNodesXYZ.length(); i++ ) {
162 aPoints->SetPoint( i, aNodesXYZ[i].x, aNodesXYZ[i].y, aNodesXYZ[i].z );
164 myGrid->SetPoints(aPoints);
169 const SMESH::long_array& anElemConnectivity = previewData->elementConnectivities;
170 const SMESH::types_array& anElemTypes = previewData->elementTypes;
172 vtkIdType aCellsSize = anElemConnectivity.length() + anElemTypes.length();
173 vtkIdType aNbCells = anElemTypes.length();
175 vtkCellArray* aConnectivity = vtkCellArray::New();
176 aConnectivity->Allocate( aCellsSize, 0 );
178 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
179 aCellTypesArray->SetNumberOfComponents( 1 );
180 aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
182 vtkIdList *anIdList = vtkIdList::New();
185 for ( int i = 0; i < anElemTypes.length(); i++ ) {
186 const SMESH::ElementSubType& anElementSubType = anElemTypes[i];
187 SMDSAbs_ElementType aType = SMDSAbs_ElementType(anElementSubType.SMDS_ElementType);
188 vtkIdType aNbNodes = anElementSubType.nbNodesInElement;
189 anIdList->SetNumberOfIds( aNbNodes );
191 for ( vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++ ){
192 anIdList->SetId( aNodeId, anElemConnectivity[aNodePos] );
196 aConnectivity->InsertNextCell( anIdList );
197 aCellTypesArray->InsertNextValue( getCellType( aType,
198 anElemTypes[i].isPoly,
203 // Insert cells in grid
204 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
205 aCellLocationsArray->SetNumberOfComponents( 1 );
206 aCellLocationsArray->SetNumberOfTuples( aNbCells );
208 aConnectivity->InitTraversal();
209 for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
210 aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
212 myGrid->SetCells( aCellTypesArray, aCellLocationsArray, aConnectivity );
214 myPreviewActor->GetMapper()->Update();
216 aCellTypesArray->Delete();
217 aCellLocationsArray->Delete();
218 aConnectivity->Delete();
223 //================================================================================
225 * \brief Set visibility
227 //================================================================================
229 void SMESHGUI_MeshEditPreview::SetVisibility (bool theVisibility)
231 myPreviewActor->SetVisibility(theVisibility);
232 SMESH::RepaintCurrentView();
235 //================================================================================
237 * \brief Set preview color
239 //================================================================================
241 void SMESHGUI_MeshEditPreview::SetColor(double R, double G, double B)
243 myPreviewActor->SetColor( R, G, B );
246 //================================================================================
248 * \brief Get preview actor
250 //================================================================================
251 SALOME_Actor* SMESHGUI_MeshEditPreview::GetActor() const
253 return myPreviewActor;