Package mbdyn :: Package interface :: Module nodes
[hide private]

Source Code for Module mbdyn.interface.nodes

  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  """The nodes described for VTK. The only node really developed 
 23  is for the L{StructuralNode} class.""" 
 24  import vtk 
 25  import numpy as N 
 26   
 27  from mbdyn.nodes import StructuralNode as StructuralNodeBase 
 28   
 29  from mbdyn.interface.common import BaseMenu 
 30  from mbdyn.interface.vectors import ReferenceFrame 
 31   
 32  # All possible action on the objects 
 33  DESC = {} 
 34   
 35  DESC["vtk"] = [\ 
 36  # Future implementation 
 37  #("velocity", "_Velocity"), 
 38  ("rotation_matrix","_Rotation _matrix"), 
 39  ("position", "_Show") 
 40  ] 
 41   
 42   
43 -class NodeMenu(BaseMenu):
44 """The GTK menu for a node""" 45
46 - def __init__(self):
47 BaseMenu.__init__(self) 48 self.add_items("vtk", DESC["vtk"]) 49 self.build()
50 51
52 -class StructuralNode(StructuralNodeBase):
53 """A structural node receiving instructions 54 from GTK and showing its results in the VTK area. 55 """ 56
57 - def __init__(self):
58 StructuralNodeBase.__init__(self) 59 60 self.actor = vtk.vtkActor() 61 self.prop = None 62 self.vtk_ref = ReferenceFrame() 63 64 self.position_array = N.zeros((3, 1)) 65 66 self.feature_keys = [] 67 self.boolean = {} 68 self.descriptions = [] 69 70 self.menu_type = "node"
71
72 - def init_once_file_loaded(self):
73 """Once the parameters from the result files have 74 been loaded on the node, the initialization is done. 75 """ 76 menu_offer = [desc[0] for desc in DESC["vtk"]] 77 for name in self.results.names: 78 if name in menu_offer: 79 self.feature_keys.append(name) 80 81 for feature_key in self.feature_keys: 82 self.boolean[feature_key] = False 83 84 # By default, show the position of the node if available 85 if "position" in self.results.names: 86 self.boolean["position"] = True 87 self.descriptions.append(self.set_vtk_position)
88
89 - def set_mapper(self, obj_mapper):
90 """Set the node mapper""" 91 self.actor.SetMapper(obj_mapper.mapper) 92 self.prop = self.actor.GetProperty()
93
94 - def set_color(self):
95 """Set the color to blue""" 96 self.prop.SetColor(0.5, 0.5, 1.)
97
98 - def set_unselected(self):
99 """Set a strong opacity""" 100 self.prop.SetOpacity(1.)
101
102 - def set_selected(self):
103 """Set a light opacity""" 104 self.prop.SetOpacity(0.5)
105
106 - def set_description(self, idx):
107 """Set the node description at a C{idx} frame 108 number""" 109 for set_description in self.descriptions: 110 set_description(idx)
111
112 - def _fill_position(self, idx):
113 """Get back the position at C{idx} frame number""" 114 self.position_array = self.results.position[idx]
115
116 - def set_vtk_position(self, idx):
117 """Set the node position in VTK""" 118 self._fill_position(idx) 119 self.actor.SetPosition(self.position_array[0][0], 120 self.position_array[1][0], 121 self.position_array[2][0])
122
123 - def set_vtk_rotation_matrix(self, idx):
124 """Show the node reference frame in the VTK area 125 at the current C{idx} frame""" 126 rot_matrix = self.results.rotation_matrix[idx] 127 self._fill_position(idx) 128 self.vtk_ref.set_orientation_matrix(rot_matrix) 129 self.vtk_ref.set_position(self.position_array)
130
131 - def set_unit_arrow(self, unit_arrow):
132 """Set the arrow mapper representing the unit vector 133 for the node reference frame""" 134 for axe in self.vtk_ref.axes: 135 axe.set_arrow(unit_arrow)
136
137 - def activate_position(self, current_frame_id, vtk_area):
138 """The node will show its position""" 139 self.descriptions.append(self.set_vtk_position) 140 self.set_vtk_position(current_frame_id) 141 vtk_area.add_actor(self.actor) 142 vtk_area.can_be_selected(self.actor, self) 143 self.boolean["position"] = True 144 return "Show " + self.name
145
146 - def desactivate_position(self, vtk_area):
147 """The node position will not be seen""" 148 self.descriptions.remove(self.set_vtk_position) 149 vtk_area.remove_actor(self.actor) 150 vtk_area.remove_from_selection(self.actor) 151 self.boolean["position"] = False 152 return "Hide " + self.name
153
154 - def activate_rotation_matrix(self, current_frame_id, vtk_area):
155 """The reference frame of the node will be seen""" 156 self.descriptions.append(self.set_vtk_rotation_matrix) 157 self.set_vtk_rotation_matrix(current_frame_id) 158 for axe in self.vtk_ref.axes: 159 vtk_area.add_actor(axe.actor) 160 self.boolean["rotation_matrix"] = True 161 return "Show rotation matrix of " + self.name
162
163 - def desactivate_rotation_matrix(self, vtk_area):
164 """The reference frame of the node will not be seen""" 165 self.descriptions.remove(self.set_vtk_rotation_matrix) 166 for axe in self.vtk_ref.axes: 167 vtk_area.remove_actor(axe.actor) 168 self.boolean["rotation_matrix"] = False 169 return "Hide rotation matrix of " + self.name
170
171 - def activate(self, key_feature, current_frame_id, vtk_area):
172 """Activate a feature for the node""" 173 return getattr(self, "activate_" + key_feature)(current_frame_id, 174 vtk_area)
175
176 - def desactivate(self, key_feature, vtk_area):
177 """Deactivate a feature for the node""" 178 return getattr(self, "desactivate_" + key_feature)(vtk_area)
179 180 181 NODE_CLASS = {} 182 NODE_CLASS["STRUCTURAL"] = {} 183 NODE_CLASS["STRUCTURAL"]["structural"] = StructuralNode 184