Package windSimSuite :: Package interface :: Module main
[hide private]

Source Code for Module windSimSuite.interface.main

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # This file is part of MBDyn sim suite. 
  5  # Copyright (C) 2007 André ESPAZE, as part of a Master thesis supervised by 
  6  # Martin O.L.Hansen (DTU) and Nicolas Chauvat (Logilab) 
  7   
  8  # MBDyn sim suite is free software; you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  # 
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program; if not, write to the Free Software 
 20  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 21  # 
 22  """Define a L{WindTurbine} and L{Simulation} in a graphical environment 
 23  """ 
 24  from windSimSuite.main import WindTurbine as WindTurbineBase 
 25  from windSimSuite.main import Simulation as SimulationBase 
 26  from windSimSuite.main import PyMBDynFile as PyMBDynFileBase 
 27   
 28  from windSimSuite.interface.rotor import Rotor 
 29  from windSimSuite.interface.nacelle import Nacelle 
 30  from windSimSuite.interface.tower import Tower 
 31   
 32  from mbdyn.interface.groups import ReferenceList, Nodes 
 33  from mbdyn.interface.mapper import MAPPER_CLASS 
 34  from mbdyn.interface.vectors import Arrow as ArrowMapper 
 35   
 36  CLASS = {"rotor" : Rotor, 
 37           "nacelle" : Nacelle, 
 38           "tower": Tower} 
 39   
40 -class WindTurbine(WindTurbineBase):
41 """A GTK wind turbine. 42 """ 43
44 - def display_on(self, gtk_tree):
45 """Display the wind turbine on the GTk treeview""" 46 gtk_tree.clean() 47 for compo_name in ["tower", "nacelle", "rotor"]: 48 if getattr(self, "has_" + compo_name): 49 compo = getattr(self, compo_name) 50 giter = gtk_tree.get_top() 51 gtk_tree.add_at(giter, compo) 52 compo.display_on(gtk_tree, giter)
53
54 - def init_from_loaded_file(self):
55 """Make the wind turbine objects aware of their 56 available results""" 57 for compo_name in ["tower", "nacelle", "rotor"]: 58 if getattr(self, "has_" + compo_name): 59 compo = getattr(self, compo_name) 60 compo.init_from_loaded_file()
61
62 - def set_parameters(self, para):
63 """Will set graphical wind turbine objects as attribute""" 64 self.set_own_parameters(para) 65 for children_name in self.children_names: 66 children = CLASS[children_name]() 67 children.set_parameters(para[children_name]) 68 setattr(self, children_name, children)
69 70
71 -class Simulation(SimulationBase):
72 """A graphical simulation. Able to deal 73 with the C{Manager}. 74 """ 75
76 - def __init__(self):
77 SimulationBase.__init__(self) 78 self.refs = ReferenceList() 79 self.nodes = Nodes() 80 81 self.unit_arrow = None 82 self.scale_table = None 83 self.items = [] 84 self.actors = [] 85 86 self.frame_step = 1 87 self.current_frame_id = 0 88 self.nb_frames = 0. 89 self.has_time = False
90
91 - def set_scale_table(self, scale_table):
92 """Set the scale table for resizing the VTK 93 mappers""" 94 self.scale_table = scale_table
95
96 - def set_frame_step(self, frame_step):
97 """Set the user frame step to play the simulation""" 98 self.frame_step = frame_step
99
100 - def display_on(self, gtk_tree):
101 """Display the simulation on the GTK treeview 102 for MBDyn components (reference frames and nodes)""" 103 gtk_tree.clean() 104 105 giter = gtk_tree.get_top() 106 gtk_tree.add_at(giter, self.refs) 107 for ref in self.refs: 108 giter1 = gtk_tree.get_from(giter) 109 gtk_tree.add_at(giter1, ref) 110 111 giter = gtk_tree.get_top() 112 gtk_tree.add_at(giter, self.nodes) 113 for group in self.nodes.groups.values(): 114 giter1 = gtk_tree.get_from(giter) 115 gtk_tree.add_at(giter1, group) 116 for node in group: 117 giter2 = gtk_tree.get_from(giter1) 118 gtk_tree.add_at(giter2, node)
119
120 - def create_vtk_actors(self):
121 """Create the VTK mappers and actors of the simulation. 122 Currently, only the reference frames and nodes exist.""" 123 unit_arrow = ArrowMapper() 124 self.scale_table.reset() 125 self.scale_table.add_vector(unit_arrow) 126 127 self.items = self.nodes.all 128 for node in self.nodes.all: 129 node.init_once_file_loaded() 130 MapperClass = MAPPER_CLASS["NODE"][node.group_key]\ 131 [node.class_key] 132 mapper = MapperClass() 133 node.set_mapper(mapper) 134 self.scale_table.add(mapper) 135 node.set_unit_arrow(unit_arrow) 136 node.set_color() 137 node.set_description(0) 138 139 self.refs.set_unit_arrow(unit_arrow) 140 141 self.scale_table.redraw() 142 # For the absolute referential 143 self.unit_arrow = unit_arrow
144
145 - def set_description(self, idx):
146 """Show the simulation at the C{idx} index. 147 The simulation is completely described by the 148 frame index in the results.""" 149 for node in self.items: 150 node.set_description(idx)
151
152 - def add_actors_to_select(self, vtk_area):
153 """Add the actors that can be selected on the VTK area""" 154 self.actors = [] 155 for node in self.nodes.all: 156 vtk_area.add_actor(node.actor) 157 vtk_area.can_be_selected(node.actor, node) 158 self.actors.append(node.actor)
159
160 - def set_parameters(self, para):
161 """Create a wind turbine able to deal with graphical libraries""" 162 self.set_own_parameters(para) 163 self.refs.set_parameters(para["refs"]) 164 if self.has_turbine: 165 self.turbine = WindTurbine() 166 self.turbine.set_parameters(self.turbine_para) 167 self.turbine.add_on_simulation(self)
168
169 - def init_from_loaded_file(self):
170 """Made the simulation aware of its available results 171 to the interface.""" 172 self.turbine.init_from_loaded_file() 173 174 if hasattr(self, "results"): 175 if hasattr(self.results, "time"): 176 if len(self.results.time) > 0: 177 self.nb_frames = len(self.results.time) 178 self.has_time = True
179 180
181 -class PyMBDynFile(PyMBDynFileBase):
182 """The container file that will set a L{Simulation} instance 183 suited for graphical interaction. 184 """ 185
186 - def __init__(self, name, mode="r", path=".", autoload=True):
187 PyMBDynFileBase.__init__(self, name, mode, path, autoload=False) 188 self.CLASS = Simulation 189 if (mode=="r") and autoload: 190 self.load()
191