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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESHGUI_AddMeshElementOp.cxx
25 // Author : Alexander SOLOVYOV
29 #include "SMESHGUI_AddMeshElementOp.h"
30 #include <SMESHGUI_AddMeshElementDlg.h>
33 #include <SalomeApp_SelectionMgr.h>
35 #include <SVTK_ViewWindow.h>
36 #include <SVTK_ViewModel.h>
37 #include <SVTK_Selector.h>
38 #include <SMESH_Actor.h>
39 #include <SMESH_ActorUtils.h>
40 #include <SMESHGUI_Utils.h>
41 #include <SMESHGUI_VTKUtils.h>
42 #include <SMESHGUI_MeshUtils.h>
44 #include <SALOME_ListIO.hxx>
47 #include <vtkDataSetMapper.h>
48 #include <vtkUnstructuredGrid.h>
49 #include <vtkIdList.h>
51 #include <TColStd_MapOfInteger.hxx>
53 #include <SMDS_Mesh.hxx>
54 #include <SMDS_MeshNode.hxx>
58 class TElementSimulation {
59 SVTK_ViewWindow* myVTKViewWindow;
60 SALOME_Actor* myPreviewActor;
61 vtkDataSetMapper* myMapper;
62 vtkUnstructuredGrid* myGrid;
65 TElementSimulation( SVTK_ViewWindow* wnd )
67 myVTKViewWindow = wnd;
69 myGrid = vtkUnstructuredGrid::New();
71 // Create and display actor
72 myMapper = vtkDataSetMapper::New();
73 myMapper->SetInput(myGrid);
75 myPreviewActor = SALOME_Actor::New();
76 myPreviewActor->PickableOff();
77 myPreviewActor->VisibilityOff();
78 myPreviewActor->SetMapper(myMapper);
81 vtkProperty* aProp = vtkProperty::New();
82 GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
83 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
84 myPreviewActor->SetProperty( aProp );
87 vtkProperty* aBackProp = vtkProperty::New();
88 GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
89 aBackProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
90 myPreviewActor->SetBackfaceProperty( aBackProp );
93 myVTKViewWindow->AddActor(myPreviewActor);
96 typedef std::vector<vtkIdType> TVTKIds;
97 void SetPosition (SMESH_Actor* theActor,
99 const TVTKIds& theIds)
101 vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
102 myGrid->SetPoints(aGrid->GetPoints());
104 const int* aConn = NULL;
108 static int anIds[] = {0,2,1,3};
114 static int anIds[] = {0,3,2,1,4};
120 static int anIds[] = {0,3,2,1,4,7,6,5};
127 vtkIdList *anIds = vtkIdList::New();
130 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
131 anIds->InsertId(i,theIds[aConn[i]]);
133 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
134 anIds->InsertId(i,theIds[i]);
136 myGrid->InsertNextCell(theType,anIds);
145 void SetVisibility (bool theVisibility)
147 myPreviewActor->SetVisibility(theVisibility);
148 RepaintCurrentView();
152 ~TElementSimulation()
154 SetVisibility( false );
155 if( myVTKViewWindow )
156 myVTKViewWindow->RemoveActor(myPreviewActor);
158 myPreviewActor->Delete();
160 myMapper->RemoveAllInputs();
168 //=================================================================================
169 // function : SMESHGUI_AddMeshElementDlg()
170 // purpose : constructor
171 //=================================================================================
172 SMESHGUI_AddMeshElementOp::SMESHGUI_AddMeshElementOp( const SMDSAbs_ElementType t, const int nbNodes )
173 : SMESHGUI_SelectionOp( NodeSelection ),
175 myNbNodes( nbNodes ),
176 myIsPoly( myElementType==SMDSAbs_Face && myNbNodes==5 ),
182 //=================================================================================
183 // function : ~SMESHGUI_AddMeshElementDlg()
184 // purpose : destructor
185 //=================================================================================
186 SMESHGUI_AddMeshElementOp::~SMESHGUI_AddMeshElementOp()
192 //=================================================================================
195 //=================================================================================
196 SalomeApp_Dialog* SMESHGUI_AddMeshElementOp::dlg() const
201 //=================================================================================
202 // function : startOperation()
204 //=================================================================================
205 void SMESHGUI_AddMeshElementOp::startOperation()
212 case 2: elemName = "EDGE"; break;
213 case 3: elemName = "TRIANGLE"; break;
214 case 4: if (myElementType == SMDSAbs_Face)
215 elemName = "QUADRANGLE";
219 case 5: elemName = "POLYGON"; break;
220 case 8: elemName = "HEXAS"; break;
223 myDlg = new SMESHGUI_AddMeshElementDlg( elemName, myElementType == SMDSAbs_Face );
224 connect( myDlg, SIGNAL( objectChanged( int, const QStringList& ) ),
225 this, SLOT( onTextChanged( int, const QStringList& ) ) );
226 connect( myDlg, SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
227 connect( myDlg, SIGNAL( reverse( int ) ), this, SLOT( onReverse( int ) ) );
230 SMESHGUI_SelectionOp::startOperation();
232 mySimulation = new SMESH::TElementSimulation( viewWindow() );
237 //=================================================================================
238 // function : selectionDone()
240 //=================================================================================
241 void SMESHGUI_AddMeshElementOp::selectionDone()
246 mySimulation->SetVisibility(false);
250 selectionMgr()->selectedObjects(aList,SVTK_Viewer::Type());
252 if( aList.Extent() != 1)
255 Handle(SALOME_InteractiveObject) anIO = aList.First();
256 myMesh = SMESH::GetMeshByIO(anIO);
258 QStringList names, ids; SalomeApp_Dialog::TypesList types;
259 selected( names, types, ids );
260 myDlg->selectObject( names, types, ids );
266 //=================================================================================
267 // function : commitOperation()
269 //=================================================================================
270 void SMESHGUI_AddMeshElementOp::commitOperation()
275 SMESHGUI_SelectionOp::commitOperation();
278 //=================================================================================
279 // function : abortOperation()
281 //=================================================================================
282 void SMESHGUI_AddMeshElementOp::abortOperation()
287 SMESHGUI_SelectionOp::abortOperation();
290 //=================================================================================
291 // function : onApply()
293 //=================================================================================
294 bool SMESHGUI_AddMeshElementOp::onApply()
299 QStringList ids; myDlg->selectedObject( 0, ids );
300 if( ids.count()>=myNbNodes && !getSMESHGUI()->isActiveStudyLocked() ) {
302 SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
303 anArrayOfIdeces->length(myNbNodes);
304 bool reverse = myDlg->isReverse();
306 for (int i = 0; i < myNbNodes; i++)
308 QString id_str = ids[ i ];
309 int pos = id_str.find( idChar() );
312 id = id_str.mid( pos+1 ).toInt();
316 printf( "SMESHGUI_AddMeshElementOp::onApply(): Error!!!\n" );
321 anArrayOfIdeces[ myNbNodes - i - 1 ] = id;
323 anArrayOfIdeces[i] = id;
326 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
327 switch (myElementType) {
329 aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
331 aMeshEditor->AddFace(anArrayOfIdeces.inout()); break;
333 aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
337 //SALOME_ListIO aList; aList.Append( myActor->getIO() );
338 //mySelectionMgr->setSelectedObjects( aList, false );
341 mySimulation->SetVisibility(false);
351 //=================================================================================
352 // function : onTextChanged()
354 //=================================================================================
355 void SMESHGUI_AddMeshElementOp::onTextChanged( int, const QStringList& aListId )
357 myDlg->setBusy( true );
358 TColStd_MapOfInteger newIndices;
359 SALOME_ListIO list; selectionMgr()->selectedObjects( list );
360 if( list.Extent()==0 )
363 SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh.in() );
367 SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
369 for (int i = 0; i < aListId.count(); i++)
370 if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
371 newIndices.Add( n->GetID() );
373 selector()->AddOrRemoveIndex( list.First(), newIndices, false );
374 highlight( list.First(), true, true );
377 myDlg->setBusy( false );
380 //=================================================================================
381 // function : displaySimulation()
383 //=================================================================================
384 void SMESHGUI_AddMeshElementOp::displaySimulation()
386 QStringList ids; myDlg->selectedObject( 0, ids );
387 if( ids.count()>=myNbNodes ) {
388 SMESH::TElementSimulation::TVTKIds anIds;
390 SMESH_Actor* anActor = SMESH::FindActorByObject( myMesh.in() );
394 for (int i = 0; i < myNbNodes; i++)
396 QString id_str = ids[ i ];
397 int pos = id_str.find( idChar() );
400 id = id_str.mid( pos+1 ).toInt();
404 printf( "SMESHGUI_AddMeshElementOp::displaySimulation(): Error!!!\n" );
408 anIds.push_back( anActor->GetObject()->GetNodeVTKId( id ) );
411 if( myDlg->isReverse() )
412 reverse( anIds.begin(), anIds.end() );
416 switch ( myElementType ) {
417 case SMDSAbs_Face : aType = VTK_POLYGON; break;
422 case 2: aType = VTK_LINE; break;
423 case 3: aType = VTK_TRIANGLE; break;
424 case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
425 case 8: aType = VTK_HEXAHEDRON; break;
430 mySimulation->SetPosition( anActor, aType, anIds );
435 //=================================================================================
436 // function : onReverse()
438 //=================================================================================
439 void SMESHGUI_AddMeshElementOp::onReverse( int )
443 mySimulation->SetVisibility( false );
448 //=================================================================================
449 // function : updateDialog()
451 //=================================================================================
452 void SMESHGUI_AddMeshElementOp::updateDialog()
456 QStringList ids; myDlg->selectedObject( 0, ids );
457 myDlg->setButtonEnabled( ids.count()>=myNbNodes, QtxDialog::OK | QtxDialog::Apply );