Home | Trees | Indices | Help |
|
---|
|
1 """ The Figure class as well as many wrapper functions for Matlab-style syntax. 2 """ 3 4 5 import os 6 from Axes import Axes 7 import wx 8 import wx.lib.buttonpanel as bp 9 from wxVTKRenderWindow import wxVTKRenderWindow 10 import vtk 11 12 currentFigure = 0 # keep track of current figure 13 """ Handle to the current figure window. 14 """ 15 16 17 pyllarPath = os.path.split(os.path.join(os.getcwd(), __file__))[0] 18 """ Path of the pillar package. 19 """ 20 2123 """ The figure is the top level window that contains the axes and figure controls. 24 """68 69 70 71 ## End of the Figure class definition ## 72 73 74 7526 """ Create a new instance of the Figure class. 27 """ 28 self.app = wx.PySimpleApp() 29 30 # create the frame (top-level window), panel, and sizer 31 self.frame = wx.Frame(None, -1, "Pyllar Figure", size=wx.Size(600,600)) 32 self.panel = wx.Panel(self.frame, -1) 33 self.sizer = wx.BoxSizer(wx.VERTICAL) 34 self.panel.SetSizer(self.sizer) 35 36 # create the figure toolbar, an intance of FigToolbar 37 self.toolbar = FigToolbar(self.panel, self) 38 39 # create the render window 40 self.widget = wxVTKRenderWindow(self.panel,-1) 41 self.widget.Enable(1) 42 self.renWin = self.widget.GetRenderWindow() 43 44 # create an axes 45 self.axes = Axes(self.renWin) 46 47 # add everything to the sizer 48 self.sizer.Add(self.widget, 1, wx.EXPAND|wx.ALL, 5) 49 50 self.sizer.Layout() 51 52 currentFigure = self5355 """ Show the contents of the figure. 56 57 Makes the axes update its bounds based on its contents. 58 This should be called at the end of a script so the figure contents are displayed. 59 """ 60 self.frame.Show(1) 61 62 self.axes.UpdateBounds() 63 self.axes.ScaleChildren() 64 self.widget.Render() # hack to make the axes in the far corner (doesn't work with wxPython now) 65 self.axes.camera.SetPosition(5, 2, 1.5) 66 67 self.app.MainLoop()263 264 265 ## End FigMenu class definition ## 266 267 268 269 ### The following functions map Matlab commands to the axes object. ### 270 271 # returns a handle to the current figure78 """ Create a new instance of the figure toolbar. 79 80 The toolbar contains controls for saving the axes to a png, 81 moving the camera to a pre-defined position, and adjusting 82 the camera projection (parallel of perspective). 83 """ 84 self.panel = panel 85 self.fig = fig 86 87 # create the toolbar 88 self.toolbar = bp.ButtonPanel(panel, -1, "", bp.BP_ALIGN_TOP) 89 self.toolbar.style = bp.BP_USE_GRADIENT 90 91 # export button 92 exportButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/export.png"), wx.BITMAP_TYPE_PNG)) 93 self.toolbar.AddButton(exportButton) 94 self.panel.Bind(wx.EVT_BUTTON, self.OnExport, exportButton) 95 96 self.toolbar.AddSeparator() 97 98 # standard view button 99 standardViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/standardView.png"), wx.BITMAP_TYPE_PNG)) 100 self.toolbar.AddButton(standardViewButton) 101 self.panel.Bind(wx.EVT_BUTTON, self.OnStandardView, standardViewButton) 102 103 # front view button 104 frontViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/frontView.png"), wx.BITMAP_TYPE_PNG)) 105 self.toolbar.AddButton(frontViewButton) 106 self.panel.Bind(wx.EVT_BUTTON, self.OnFrontView, frontViewButton) 107 108 # back view button 109 backViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/backView.png"), wx.BITMAP_TYPE_PNG)) 110 self.toolbar.AddButton(backViewButton) 111 self.panel.Bind(wx.EVT_BUTTON, self.OnBackView, backViewButton) 112 113 # left view button 114 leftViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/leftView.png"), wx.BITMAP_TYPE_PNG)) 115 self.toolbar.AddButton(leftViewButton) 116 self.panel.Bind(wx.EVT_BUTTON, self.OnLeftView, leftViewButton) 117 118 # right view button 119 rightViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/rightView.png"), wx.BITMAP_TYPE_PNG)) 120 self.toolbar.AddButton(rightViewButton) 121 self.panel.Bind(wx.EVT_BUTTON, self.OnRightView, rightViewButton) 122 123 # top view button 124 topViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/topView.png"), wx.BITMAP_TYPE_PNG)) 125 self.toolbar.AddButton(topViewButton) 126 self.panel.Bind(wx.EVT_BUTTON, self.OnTopView, topViewButton) 127 128 # bottom view button 129 bottomViewButton = bp.ButtonInfo(self.toolbar, wx.NewId(), wx.Bitmap(os.path.join(pyllarPath, "icons/bottomView.png"), wx.BITMAP_TYPE_PNG)) 130 self.toolbar.AddButton(bottomViewButton) 131 self.panel.Bind(wx.EVT_BUTTON, self.OnBottonView, bottomViewButton) 132 133 self.toolbar.AddSeparator() 134 135 # projection style choice 136 self.projectionChoice = wx.Choice(self.toolbar, -1, choices=["Perspective", "Parallel"]) 137 self.toolbar.AddControl(self.projectionChoice) 138 self.panel.Bind(wx.EVT_CHOICE, self.OnChangeProjection, self.projectionChoice) 139 140 141 # set the toolbar colors 142 bpArt = self.toolbar.GetBPArt() 143 144 145 # set the color the text is drawn with 146 bpArt.SetColor(bp.BP_TEXT_COLOR, wx.WHITE) 147 148 # These default to white and whatever is set in the system 149 # settings for the wx.SYS_COLOUR_ACTIVECAPTION. We'll use 150 # some specific settings to ensure a consistent look for the 151 # demo. 152 bpArt.SetColor(bp.BP_BORDER_COLOR, wx.Colour(224,224,224)) 153 bpArt.SetColor(bp.BP_GRADIENT_COLOR_FROM, wx.Colour(112,112,112)) 154 bpArt.SetColor(bp.BP_GRADIENT_COLOR_TO, wx.Colour(224,224,224)) 155 bpArt.SetColor(bp.BP_BUTTONTEXT_COLOR, wx.Colour(70,143,255)) 156 bpArt.SetColor(bp.BP_SEPARATOR_COLOR, bp.BrightenColour(wx.Colour(60, 11, 112), 0.85)) 157 bpArt.SetColor(bp.BP_SELECTION_BRUSH_COLOR, wx.Color(225, 225, 255)) 158 bpArt.SetColor(bp.BP_SELECTION_PEN_COLOR, wx.SystemSettings_GetColour(wx.SYS_COLOUR_ACTIVECAPTION)) 159 160 161 bpArt.SetGradientType( bp.BP_GRADIENT_VERTICAL) 162 163 self.toolbar.SetStyle(self.toolbar.style) 164 165 # add the panel to the sizer 166 fig.sizer.Add(self.toolbar, 0, wx.EXPAND)167 168170 """ Exports the axes to a bitmap. 171 172 Opens a dialog that prompts the user for the bitmap's name. 173 """ 174 # get the file name 175 fileDialog = wx.FileDialog(self.fig.frame, "Export Figure", "", "export.png", "PNG file (*.png)|BMP file (*.bmp)", wx.FD_SAVE) 176 fileDialog.ShowModal() 177 178 path = fileDialog.GetPath() 179 180 if len(path)>0: 181 # write the image 182 w2i = vtk.vtkWindowToImageFilter() 183 writer = vtk.vtkPNGWriter() 184 w2i.SetInput(self.fig.renWin) 185 w2i.Update() 186 writer.SetInputConnection(w2i.GetOutputPort()) 187 writer.SetFileName(path) 188 self.fig.renWin.Render() 189 writer.Write()190 191193 """ Set the camera to the standard 3D view. 194 """ 195 self.fig.axes.SetViewDirection(Axes.standardDirection) 196 self.fig.axes.SetViewUp(Axes.standardViewUp) 197 self.fig.widget.Render()198200 """ Set the camera to the front view. 201 """ 202 self.fig.axes.SetViewDirection(Axes.frontDirection) 203 self.fig.axes.SetViewUp(Axes.frontViewUp) 204 self.fig.widget.Render()205207 """ Set the camera to the rear view. 208 """ 209 self.fig.axes.SetViewDirection(Axes.backDirection) 210 self.fig.axes.SetViewUp(Axes.backViewUp) 211 self.fig.widget.Render()212214 """ Set the camera to the left view. 215 """ 216 self.fig.axes.SetViewDirection(Axes.leftDirection) 217 self.fig.axes.SetViewUp(Axes.leftViewUp) 218 self.fig.widget.Render()219221 """ Set the camera to the right view. 222 """ 223 self.fig.axes.SetViewDirection(Axes.rightDirection) 224 self.fig.axes.SetViewUp(Axes.rightViewUp) 225 self.fig.widget.Render()226228 """ Set the camera to the top view. 229 """ 230 self.fig.axes.SetViewDirection(Axes.topDirection) 231 self.fig.axes.SetViewUp(Axes.topViewUp) 232 self.fig.widget.Render()233235 """ Set the camera to the bottom view. 236 """ 237 self.fig.axes.SetViewDirection(Axes.bottomDirection) 238 self.fig.axes.SetViewUp(Axes.bottomViewUp) 239 self.fig.widget.Render()240242 """ Changes the projection between parallel and projection. 243 """ 244 if self.projectionChoice.GetCurrentSelection()==0: 245 self.fig.axes.camera.ParallelProjectionOff() 246 self.fig.axes.SetDistance(Axes.perspectiveDistance) 247 else: 248 self.fig.axes.camera.ParallelProjectionOn() 249 self.fig.axes.SetDistance(Axes.parallelDistance) 250 self.fig.axes.SetViewDirection(Axes.standardDirection) 251 self.fig.axes.SetViewUp(Axes.standardViewUp) 252 self.fig.widget.Render()253 258273 """ Get the current figure. 274 275 The current figure will be returned. 276 If no figures are present, one will be created. 277 """ 278 if currentFigure is 0: 279 Figure() 280 return currentFigure281 282 # returns a handle to the current axes284 """ Get the current axes. 285 286 The current axes will be returned. 287 If no axes are present, one will be created. 288 """ 289 if Axes.num == 0: 290 Figure() 291 return Axes.current292 293 294 # x label interface296 """ Set the label on the x axis of the current axes. 297 """ 298 if label is "": # there was no label specified 299 return gca().GetXLabel() 300 else: 301 gca().SetXLabel(label)302 303 # y label interface305 """ Set the label on the y axis of the current axes. 306 """ 307 308 if label is "": # there was no label specified 309 return gca().GetYLabel() 310 else: 311 gca().SetYLabel(label)312 313 # z label interface315 """ Set the label on the z axis of the current axes. 316 """ 317 if label is "": # there was no label specified 318 return gca().GetZLabel() 319 else: 320 gca().SetZLabel(label)321 322 # x limits interface324 """ Set the limis of the x axis of the current axes. 325 326 Calling this function will override the automatic limit sizing of the axes. 327 """ 328 if lim is not [0,0]: 329 gca().SetXLim(lim) 330 else: 331 return gca().GetXLim()332 333 # y limits interface 343 344 # z limits interface 354
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0beta1 on Tue Mar 20 23:12:15 2007 | http://epydoc.sourceforge.net |