1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ATOMSOLVGUI_Displayer.h"
21 #include "ATOMSOLVGUI.h"
23 #include <SUIT_Session.h>
24 #include <SUIT_ResourceMgr.h>
25 #include <SUIT_Application.h>
26 #include <SUIT_Study.h>
28 #include <SVTK_ViewModel.h>
30 #include <SALOME_Actor.h>
32 #include <SALOMEconfig.h>
33 #include CORBA_CLIENT_HEADER(ATOMSOLV)
35 #include <vtkSphereSource.h>
36 #include <vtkPolyDataMapper.h>
37 #include <vtkActorCollection.h>
39 const float radius = 5.; // radius of a sphere that represents an atom in VTK viewer
40 const float quality_coefficient = .1; // for VTK viewer presentation: parameter that controls
41 // the quality of presentation of sphere (atom). 0 - very bad, 1. - very good (and slow).
43 // default representation mode, is taken from preferences
44 int ATOMSOLVGUI_Displayer::myRepresentation = SUIT_Session::session()->resourceMgr(
45 )->integerValue( "ATOMSOLV", "Representation", 2 );
46 // default radius, is taken from preferences
47 double ATOMSOLVGUI_Displayer::myRadius = SUIT_Session::session()->resourceMgr(
48 )->doubleValue( "ATOMSOLV", "Radius", 5. );
50 ATOMSOLVGUI_Displayer::ATOMSOLVGUI_Displayer()
54 ATOMSOLVGUI_Displayer::~ATOMSOLVGUI_Displayer()
58 bool ATOMSOLVGUI_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
60 QStringList es = entry.split( "_" );
61 bool result = ( es.count() == 3 && es[ 0 ] == "ATOMSOLVGUI" && viewer_type == SVTK_Viewer::Type() );
62 // printf ( " canBeDisplayed : entry = %s, count = %d, res = %d \n", entry.latin1(), es.count(), result );
63 return result; // entry of an atom for sure
66 ATOMGEN_ORB::Atom_ptr getAtom( const QString& entry, const int studyID, double& temprature )
68 QStringList es = entry.split( "_" );
70 bool isAtom = (es.count() == 3 && es[ 0 ] == "ATOMSOLVGUI");
72 return ATOMGEN_ORB::Atom::_nil();
74 const int molID = es[1].toInt();
75 const int atomID = es[2].toInt();
77 ATOMSOLV_ORB::ATOMSOLV_Gen_var engine = ATOMSOLVGUI::GetATOMSOLVGen();
78 ATOMSOLV_ORB::TMoleculeList_var molecules;
79 if ( engine->getData( studyID, molecules ) && molID >= 0 && molID < molecules->length() ) {
80 ATOMSOLV_ORB::TMolecule tmol = molecules[ molID ];
81 ATOMGEN_ORB::Molecule_var mol = tmol.molecule;
82 if ( atomID < mol->getNbAtoms() ) {
83 temprature = tmol.temperature;
84 return mol->getAtom( atomID );
87 return ATOMGEN_ORB::Atom::_nil();
90 void setTemperature( SALOME_Actor* actor, double temperature )
93 temperature = (((int)temperature) % 10 + 1.) / 10.;
94 actor->SetColor( temperature, 0., 1. - temperature );
101 if ( SUIT_Application* app = SUIT_Session::session()->activeApplication() )
102 if ( SUIT_Study* study = app->activeStudy() )
103 studyID = study->id();
107 SALOME_Prs* ATOMSOLVGUI_Displayer::buildPresentation( const QString& entry, SALOME_View* view )
109 const int studyID = getStudyID();
113 SVTK_Prs* prs = dynamic_cast<SVTK_Prs*>( LightApp_Displayer::buildPresentation( entry, view ) );
115 if ( !prs ) return 0;
118 ATOMGEN_ORB::Atom_var atom = getAtom( entry, studyID, temperature );
120 if ( !CORBA::is_nil( atom ) ) {
121 // PRESENTATION FOR OCC VIEWER
122 // double x = atom->getX(), y = atom->getY(), z = atom->getZ();
123 // BRepPrimAPI_MakeSphere makeSphere( gp_Pnt( x, y, z ), radius );
124 // Handle(AIS_Shape) aisShape = new AIS_Shape( makeSphere.Shell() );
125 // if( !aisShape.IsNull() ) {
126 // aisShape->SetOwner( new SALOME_InteractiveObject( entry.latin1(), "ATOMSOLV" ) );
127 // return new SOCC_Prs( aisShape );
130 center[ 0 ] = atom->getX();
131 center[ 1 ] = atom->getY();
132 center[ 2 ] = atom->getZ();
134 vtkSphereSource* vtkObj = vtkSphereSource::New();
135 vtkObj->SetRadius( radius );
136 vtkObj->SetCenter( center );
137 vtkObj->SetThetaResolution( (int)( vtkObj->GetEndTheta() * quality_coefficient ) );
138 vtkObj->SetPhiResolution( (int)( vtkObj->GetEndPhi() * quality_coefficient ) );
140 vtkPolyDataMapper* vtkMapper = vtkPolyDataMapper::New();
141 vtkMapper->SetInputConnection( vtkObj->GetOutputPort() );
145 SALOME_Actor* actor = SALOME_Actor::New();
146 actor->SetMapper( vtkMapper );
147 actor->setIO( new SALOME_InteractiveObject( entry.toLatin1(), "ATOMSOLV" ) );
148 setTemperature( actor, temperature );
150 actor->SetRepresentation( 2 ); // 2 == surface mode
154 prs->AddObject( actor );
160 void ATOMSOLVGUI_Displayer::updateActor( SALOME_Actor* actor )
162 const int studyID = getStudyID();
163 if ( actor && actor->hasIO() && studyID >= 0 ) {
165 getAtom( actor->getIO()->getEntry(), studyID, temperature );
166 setTemperature( actor, temperature );
171 void ATOMSOLVGUI_Displayer::setDisplayMode( const QStringList& entries, const QString& mode )
173 SALOME_View* view = GetActiveView();
174 if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
175 for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {
176 if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
177 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
178 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
180 lst->InitTraversal();
181 while ( vtkActor* actor = lst->GetNextActor() )
182 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
183 salomeActor->SetRepresentation( mode == "Points" ? 0 : mode == "Wireframe" ? 1 : 2 );
184 salomeActor->Update();
192 void ATOMSOLVGUI_Displayer::setColor( const QStringList& entries, const QColor& color )
194 SALOME_View* view = GetActiveView();
195 if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
196 for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {
197 if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
198 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
199 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
201 lst->InitTraversal();
202 while ( vtkActor* actor = lst->GetNextActor() )
203 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
204 salomeActor->SetColor( (float)(color.red()) / 255., (float)(color.green()) / 255., (float)(color.blue()) / 255.);
205 salomeActor->Update();
213 QColor ATOMSOLVGUI_Displayer::getColor( const QString& entry )
215 SALOME_View* view = GetActiveView();
216 if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
217 if ( SALOME_Prs* prs = view->CreatePrs( entry.toLatin1() ) ) {
218 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
219 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
221 lst->InitTraversal();
222 while ( vtkActor* actor = lst->GetNextActor() )
223 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
225 salomeActor->GetColor( r, g, b );
226 return QColor( (int)(r * 255.), (int)(g * 255.), (int)(b * 255.) );
234 void ATOMSOLVGUI_Displayer::setTransparency( const QStringList& entries, const float transparency )
236 SALOME_View* view = GetActiveView();
237 if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
238 for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {
239 if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
240 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
241 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
243 lst->InitTraversal();
244 while ( vtkActor* actor = lst->GetNextActor() )
245 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
246 salomeActor->SetOpacity( transparency );
247 salomeActor->Update();
255 float ATOMSOLVGUI_Displayer::getTransparency( const QString& entry )
257 SALOME_View* view = GetActiveView();
258 if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
259 if ( SALOME_Prs* prs = view->CreatePrs( entry.toLatin1() ) ) {
260 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
261 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
263 lst->InitTraversal();
264 while ( vtkActor* actor = lst->GetNextActor() )
265 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) )
266 return salomeActor->GetOpacity();