Salome HOME
558f02cb986ad747ad77b5dc1218a2334d595d3b
[samples/atomic.git] / src / AtomicPy / AtomicPy.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File   : SalomePyQt.cxx
21 // Author : Roman NIKOLAEV, Open CASCADE S.A.S. (roman.nikolaev@opencascade.com)
22
23 #include <SUIT_Session.h>
24 #include <LightApp_Application.h>
25 #include <LightApp_Module.h>
26 #include <SALOME_Event.h>
27
28 #include "AtomicPy.h"
29 #include "ATOMICGUI_Data.h"
30 #include "ATOMICGUI_DataModel.h"
31
32 class TCreateMoleculeEvent: public SALOME_Event
33 {
34 public:
35   QString myName;
36   typedef QString TResult;
37   TResult myResult;
38   
39   TCreateMoleculeEvent(const QString& name) : myResult(""), myName(name) {}
40   virtual void Execute()
41   {
42     if ( !SUIT_Session::session() )
43       return;
44     LightApp_Application* app =  dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
45     if(!app)
46       return;
47     
48     LightApp_Module* module = dynamic_cast<LightApp_Module*>( app->module("Atomic") );
49     if(!module)
50       return;
51       
52     ATOMICGUI_DataModel* model = dynamic_cast<ATOMICGUI_DataModel*>(module->dataModel());
53     if(!model)
54       return;
55     
56     myResult = model->createMolecule();
57     model->renameObj(myResult,myName);
58   }
59 };
60
61 class TAddAtomEvent: public SALOME_Event
62 {
63 public:
64   QString myId;
65   QString myName;
66   double myX, myY, myZ;
67
68   TAddAtomEvent(const QString& id, 
69                 const QString& name,
70                 const double x,
71                 const double y,
72                 const double z) : myId(id), myName(name), myX(x), myY(y), myZ(z) {}
73   virtual void Execute()
74   {
75     if ( !SUIT_Session::session() )
76       return;
77     LightApp_Application* app =  dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
78     if(!app)
79       return;
80     
81     LightApp_Module* module = dynamic_cast<LightApp_Module*>( app->module("Atomic") );
82     if(!module)
83       return;
84     
85     ATOMICGUI_DataModel* model = dynamic_cast<ATOMICGUI_DataModel*>(module->dataModel());
86     if(!model)
87       return;
88     
89     model->addAtom(myId,myName,myX,myY,myZ);
90   }
91 };
92
93
94 AtomicMolecule::AtomicMolecule( const QString& name )
95 {
96   myId = ProcessEvent( new TCreateMoleculeEvent( name ) );  
97 }
98
99 void AtomicMolecule::addAtom( const QString& atom, const double x, const double y, const double z ){
100   ProcessVoidEvent( new TAddAtomEvent( myId, atom, x, y, x ) );
101 }