1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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 #include "ATOMSOLVGUI_Displayer.h"
23 #include "ATOMSOLVGUI.h"
25 #include <SUIT_Session.h>
26 #include <SUIT_ResourceMgr.h>
27 #include <SUIT_Application.h>
28 #include <SUIT_Study.h>
30 #include <SVTK_ViewModel.h>
32 #include <SALOME_Actor.h>
34 #include <SALOMEconfig.h>
35 #include CORBA_CLIENT_HEADER(ATOMSOLV)
37 #include <vtkSphereSource.h>
38 #include <vtkPolyDataMapper.h>
39 #include <vtkActorCollection.h>
41 const float radius = 5.; // radius of a sphere that represents an atom in VTK viewer
42 const float quality_coefficient = .1; // for VTK viewer presentation: parameter that controls
43 // the quality of presentation of sphere (atom). 0 - very bad, 1. - very good (and slow).
45 // default representation mode, is taken from preferences
46 int ATOMSOLVGUI_Displayer::myRepresentation = SUIT_Session::session()->resourceMgr(
47 )->integerValue( "ATOMSOLV", "Representation", 2 );
48 // default radius, is taken from preferences
49 double ATOMSOLVGUI_Displayer::myRadius = SUIT_Session::session()->resourceMgr(
50 )->doubleValue( "ATOMSOLV", "Radius", 5. );
52 ATOMSOLVGUI_Displayer::ATOMSOLVGUI_Displayer()
56 ATOMSOLVGUI_Displayer::~ATOMSOLVGUI_Displayer()
60 bool ATOMSOLVGUI_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
62 QStringList es = entry.split( "_" );
63 bool result = ( es.count() == 3 && es[ 0 ] == "ATOMSOLVGUI" && viewer_type == SVTK_Viewer::Type() );
64 // printf ( " canBeDisplayed : entry = %s, count = %d, res = %d \n", entry.latin1(), es.count(), result );
65 return result; // entry of an atom for sure
68 ATOMGEN_ORB::Atom_ptr getAtom( const QString& entry, const int studyID, double& temprature )
70 QStringList es = entry.split( "_" );
72 bool isAtom = (es.count() == 3 && es[ 0 ] == "ATOMSOLVGUI");
74 return ATOMGEN_ORB::Atom::_nil();
76 const int molID = es[1].toInt();
77 const int atomID = es[2].toInt();
79 ATOMSOLV_ORB::ATOMSOLV_Gen_var engine = ATOMSOLVGUI::GetATOMSOLVGen();
80 ATOMSOLV_ORB::TMoleculeList_var molecules;
81 if ( engine->getData( studyID, molecules ) && molID >= 0 && molID < molecules->length() ) {
82 ATOMSOLV_ORB::TMolecule tmol = molecules[ molID ];
83 ATOMGEN_ORB::Molecule_var mol = tmol.molecule;
84 if ( atomID < mol->getNbAtoms() ) {
85 temprature = tmol.temperature;
86 return mol->getAtom( atomID );
89 return ATOMGEN_ORB::Atom::_nil();
92 void setTemperature( SALOME_Actor* actor, double temperature )
95 temperature = (((int)temperature) % 10 + 1.) / 10.;
96 actor->SetColor( temperature, 0., 1. - temperature );
103 if ( SUIT_Application* app = SUIT_Session::session()->activeApplication() )
104 if ( SUIT_Study* study = app->activeStudy() )
105 studyID = study->id();
109 SALOME_Prs* ATOMSOLVGUI_Displayer::buildPresentation( const QString& entry, SALOME_View* view )
111 const int studyID = getStudyID();
115 SVTK_Prs* prs = dynamic_cast<SVTK_Prs*>( LightApp_Displayer::buildPresentation( entry, view ) );
117 if ( !prs ) return 0;
120 ATOMGEN_ORB::Atom_var atom = getAtom( entry, studyID, temperature );
122 if ( !CORBA::is_nil( atom ) ) {
123 // PRESENTATION FOR OCC VIEWER
124 // double x = atom->getX(), y = atom->getY(), z = atom->getZ();
125 // BRepPrimAPI_MakeSphere makeSphere( gp_Pnt( x, y, z ), radius );
126 // Handle(AIS_Shape) aisShape = new AIS_Shape( makeSphere.Shell() );
127 // if( !aisShape.IsNull() ) {
128 // aisShape->SetOwner( new SALOME_InteractiveObject( entry.latin1(), "ATOMSOLV" ) );
129 // return new SOCC_Prs( aisShape );
132 center[ 0 ] = atom->getX();
133 center[ 1 ] = atom->getY();
134 center[ 2 ] = atom->getZ();
136 vtkSphereSource* vtkObj = vtkSphereSource::New();
137 vtkObj->SetRadius( radius );
138 vtkObj->SetCenter( center );
139 vtkObj->SetThetaResolution( (int)( vtkObj->GetEndTheta() * quality_coefficient ) );
140 vtkObj->SetPhiResolution( (int)( vtkObj->GetEndPhi() * quality_coefficient ) );
142 vtkPolyDataMapper* vtkMapper = vtkPolyDataMapper::New();
143 vtkMapper->SetInput( vtkObj->GetOutput() );
147 SALOME_Actor* actor = SALOME_Actor::New();
148 actor->SetMapper( vtkMapper );
149 actor->setIO( new SALOME_InteractiveObject( entry.toLatin1(), "ATOMSOLV" ) );
150 setTemperature( actor, temperature );
152 actor->SetRepresentation( 2 ); // 2 == surface mode
156 prs->AddObject( actor );
162 void ATOMSOLVGUI_Displayer::updateActor( SALOME_Actor* actor )
164 const int studyID = getStudyID();
165 if ( actor && actor->hasIO() && studyID >= 0 ) {
167 getAtom( actor->getIO()->getEntry(), studyID, temperature );
168 setTemperature( actor, temperature );
173 void ATOMSOLVGUI_Displayer::setDisplayMode( const QStringList& entries, const QString& mode )
175 SALOME_View* view = GetActiveView();
176 if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
177 for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {
178 if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
179 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
180 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
182 lst->InitTraversal();
183 while ( vtkActor* actor = lst->GetNextActor() )
184 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
185 salomeActor->SetRepresentation( mode == "Points" ? 0 : mode == "Wireframe" ? 1 : 2 );
186 salomeActor->Update();
194 void ATOMSOLVGUI_Displayer::setColor( const QStringList& entries, const QColor& color )
196 SALOME_View* view = GetActiveView();
197 if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
198 for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {
199 if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
200 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
201 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
203 lst->InitTraversal();
204 while ( vtkActor* actor = lst->GetNextActor() )
205 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
206 salomeActor->SetColor( (float)(color.red()) / 255., (float)(color.green()) / 255., (float)(color.blue()) / 255.);
207 salomeActor->Update();
215 QColor ATOMSOLVGUI_Displayer::getColor( const QString& entry )
217 SALOME_View* view = GetActiveView();
218 if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
219 if ( SALOME_Prs* prs = view->CreatePrs( entry.toLatin1() ) ) {
220 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
221 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
223 lst->InitTraversal();
224 while ( vtkActor* actor = lst->GetNextActor() )
225 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
226 vtkFloatingPointType r, g, b;
227 salomeActor->GetColor( r, g, b );
228 return QColor( (int)(r * 255.), (int)(g * 255.), (int)(b * 255.) );
236 void ATOMSOLVGUI_Displayer::setTransparency( const QStringList& entries, const float transparency )
238 SALOME_View* view = GetActiveView();
239 if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
240 for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {
241 if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
242 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
243 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
245 lst->InitTraversal();
246 while ( vtkActor* actor = lst->GetNextActor() )
247 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
248 salomeActor->SetOpacity( transparency );
249 salomeActor->Update();
257 float ATOMSOLVGUI_Displayer::getTransparency( const QString& entry )
259 SALOME_View* view = GetActiveView();
260 if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
261 if ( SALOME_Prs* prs = view->CreatePrs( entry.toLatin1() ) ) {
262 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
263 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
265 lst->InitTraversal();
266 while ( vtkActor* actor = lst->GetNextActor() )
267 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) )
268 return salomeActor->GetOpacity();