pyNastran  0.5.0
pyNastran BDF Reader/Writer, OP2 Parser, and GUI
box.py
Go to the documentation of this file.
00001 ## GNU Lesser General Public License
00002 ## 
00003 ## Program pyNastran - a python interface to NASTRAN files
00004 ## Copyright (C) 2011-2012  Steven Doyle, Al Danial
00005 ## 
00006 ## Authors and copyright holders of pyNastran
00007 ## Steven Doyle <mesheb82@gmail.com>
00008 ## Al Danial    <al.danial@gmail.com>
00009 ## 
00010 ## This file is part of pyNastran.
00011 ## 
00012 ## pyNastran is free software: you can redistribute it and/or modify
00013 ## it under the terms of the GNU Lesser General Public License as published by
00014 ## the Free Software Foundation, either version 3 of the License, or
00015 ## (at your option) any later version.
00016 ## 
00017 ## pyNastran is distributed in the hope that it will be useful,
00018 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 ## GNU General Public License for more details.
00021 ## 
00022 ## You should have received a copy of the GNU Lesser General Public License
00023 ## along with pyNastran.  If not, see <http://www.gnu.org/licenses/>.
00024 ## 
00025 import os
00026 import wx
00027 
00028 
00029 #ID_OPEN = 801
00030 ID_SAVEAS = 803
00031 ID_ABOUT = 3
00032 
00033 class MyPopupMenu(wx.Menu):
00034     def __init__(self, parent):
00035         super(MyPopupMenu, self).__init__()
00036 
00037         self.parent = parent
00038 
00039         mmi = wx.MenuItem(self, wx.NewId(), 'Minimize')
00040         self.AppendItem(mmi)
00041         self.Bind(wx.EVT_MENU, self.OnMinimize, mmi)
00042 
00043         cmi = wx.MenuItem(self, wx.NewId(), 'Close')
00044         self.AppendItem(cmi)
00045         self.Bind(wx.EVT_MENU, self.OnClose, cmi)
00046 
00047     def OnMinimize(self, e):
00048         self.parent.Iconize()
00049 
00050     def OnClose(self, e):
00051         self.parent.Close()
00052         
00053 class Example(wx.Frame):
00054     def __init__(self, *args, **kwargs):
00055         super(Example, self).__init__(*args, **kwargs) 
00056         self.InitUI()
00057         
00058     def InitUI(self):
00059         # max undo count
00060         self.count = 5
00061         
00062         menubar = wx.MenuBar()
00063         vbox = wx.BoxSizer(wx.VERTICAL)
00064 
00065         # file menu
00066         fileMenu = wx.Menu()
00067         fileMenu.Append(wx.ID_NEW,  '&New','does nothing')
00068         fileMenu.Append(wx.ID_OPEN, '&Load BDF','Loads a BDF')
00069         fileMenu.Append(wx.ID_OPEN, 'Load OP2 &Results','Loads a OP2 - does nothing')
00070         fileMenu.Append(wx.ID_SAVE, '&Save','does nothing')
00071         exitButton = fileMenu.Append(wx.ID_EXIT, 'E&xit pyNastran','Exits the program')
00072         exitButton.SetBitmap(wx.Image('icons/texit.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
00073         #print "exitButton = ",'\n'.join(dir(exitButton))
00074         #print "exitButton = ",help(exitButton.SetBitmap)
00075 
00076         fileMenu.AppendSeparator()
00077 
00078         imp = wx.Menu()
00079         imp.Append(wx.ID_ANY, 'Import newsfeed list...')
00080         imp.Append(wx.ID_ANY, 'Import bookmarks...')
00081         imp.Append(wx.ID_ANY, 'Import mail...')
00082 
00083         fileMenu.AppendMenu(wx.ID_ANY, 'I&mport', imp)
00084 
00085         qmi = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+W')
00086         fileMenu.AppendItem(qmi)
00087 
00088         self.Bind(wx.EVT_MENU, self.OnQuit, qmi)
00089 
00090         # view menu
00091         viewMenu = wx.Menu()
00092         
00093         self.shst = viewMenu.Append(wx.ID_ANY, 'Show statusbar', 
00094             'Show Statusbar', kind=wx.ITEM_CHECK)
00095         self.shtl = viewMenu.Append(wx.ID_ANY, 'Show toolbar', 
00096             'Show Toolbar', kind=wx.ITEM_CHECK)
00097             
00098         viewMenu.Check(self.shst.GetId(), True)
00099         viewMenu.Check(self.shtl.GetId(), True)
00100 
00101         self.Bind(wx.EVT_MENU, self.ToggleStatusBar, self.shst)
00102         self.Bind(wx.EVT_MENU, self.ToggleToolBar, self.shtl)
00103         self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
00104 
00105         # help/about menu
00106         helpMenu = wx.Menu()
00107         helpMenu.Append(ID_ABOUT, '&About', 'About pyNastran')
00108         self.Bind(wx.EVT_MENU, self.OnAbout, id=ID_ABOUT)
00109 
00110         # menu bar
00111         menubar.Append(fileMenu, '&File')
00112         menubar.Append(viewMenu, '&View')
00113         menubar.Append(helpMenu, '&Help')
00114         self.SetMenuBar(menubar)
00115         
00116         # toolbar at top - toggles
00117         self.toolbar1 = wx.ToolBar(self)
00118         #self.toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('icons/new.png'))
00119         self.toolbar1.AddLabelTool(wx.ID_OPEN, '', wx.Bitmap('icons/topen.png'))
00120         self.toolbar1.AddLabelTool(ID_SAVEAS,  '', wx.Bitmap('icons/tsave.png'))
00121         self.Bind(wx.EVT_TOOL, self.OnSaveAsFile, id=ID_SAVEAS)
00122         self.Bind(wx.EVT_TOOL, self.OnOpenBDF,    id=wx.ID_OPEN)
00123         #self.toolbar1.AddSeparator()
00124         tundo = self.toolbar1.AddLabelTool(wx.ID_UNDO, '', wx.Bitmap('icons/tundo.png'))
00125         #self.toolbar1.AddSeparator()
00126         tredo = self.toolbar1.AddLabelTool(wx.ID_REDO, '', wx.Bitmap('icons/tredo.png'))
00127         self.toolbar1.EnableTool(wx.ID_REDO, False)
00128         
00129         self.toolbar1.Realize()
00130 
00131         # toolbar 2
00132         toolbar2 = wx.ToolBar(self)
00133         qtool = toolbar2.AddLabelTool(wx.ID_EXIT, '', wx.Bitmap('icons/texit.png'))
00134         toolbar2.Realize()
00135 
00136         vbox.Add(self.toolbar1, 0, wx.EXPAND)
00137         vbox.Add(toolbar2, 0, wx.EXPAND)
00138         self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
00139         self.Bind(wx.EVT_TOOL, self.OnUndo, tundo)
00140         self.Bind(wx.EVT_TOOL, self.OnRedo, tredo)
00141         self.SetSizer(vbox)        
00142 
00143         
00144         
00145         # status bar at bottom - toggles
00146         self.statusbar = self.CreateStatusBar()
00147         self.statusbar.SetStatusText('Ready')
00148 
00149         self.SetSize((350, 250))
00150         #self.SetIcon(wx.Icon('icons/tbat.png', wx.BITMAP_TYPE_ICO))
00151         self.SetTitle('pyNastran')
00152         self.Centre()
00153         self.Show(True)
00154         
00155     def OnRightDown(self, e):
00156         self.PopupMenu(MyPopupMenu(self), e.GetPosition())
00157 
00158     def ToggleStatusBar(self, e):
00159         if self.shst.IsChecked():
00160             self.statusbar.Show()
00161         else:
00162             self.statusbar.Hide()
00163 
00164     def ToggleToolBar(self, e):
00165         if self.shtl.IsChecked():
00166             self.toolbar1.Show()
00167         else:
00168             self.toolbar1.Hide()
00169 
00170     def OnUndo(self, e):
00171         if self.count > 1 and self.count <= 5:
00172             self.count = self.count - 1
00173 
00174         if self.count == 1:
00175             self.toolbar1.EnableTool(wx.ID_UNDO, False)
00176 
00177         if self.count == 4:
00178             self.toolbar1.EnableTool(wx.ID_REDO, True)
00179 
00180     def OnRedo(self, e):
00181         if self.count < 5 and self.count >= 1:
00182             self.count = self.count + 1
00183 
00184         if self.count == 5:
00185             self.toolbar1.EnableTool(wx.ID_REDO, False)
00186 
00187         if self.count == 2:
00188             self.toolbar1.EnableTool(wx.ID_UNDO, True)
00189 
00190     def OnAbout(self, event):
00191         about = [
00192             'pyNastran v0.3.0',
00193             'Copyright 2011-2012\n',
00194             'code.google.com/p/pynastran/',
00195             '',
00196             'Controls',
00197               'X/x - snap to x axis',
00198               'Y/y - snap to axis',
00199               'Z/z - snap to axis',
00200               '',
00201               'left arrow  - pan left',
00202               'right arrow - pan right',
00203               'up arrow    - pan up',
00204               'down arrow   - pan down',
00205               '',
00206               'm/M /tscale up/scale down',
00207               'p   /tproject point (not done)',
00208               'f   /tfly to rotation point (not done)',
00209               'q/e /texit (to disable)',
00210               'o/O /trotate counter-clockwise/clockwise 5 degrees',
00211               'w   /twireframe',
00212               's   /tsurface',
00213               'i   /ttake a screenshot (image, not done)',]
00214 
00215         dlg = wx.MessageDialog(self, '\n'.join(about), 'About',
00216                  wx.OK | wx.ICON_INFORMATION)
00217         dlg.ShowModal()
00218         dlg.Destroy()
00219   
00220     def OnLoadBDF(self, event):
00221         """ Open a file"""
00222         #print "OnOpen..."
00223         self.dirname = ''
00224         dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
00225         if dlg.ShowModal() == wx.ID_OK:
00226             self.filename = dlg.GetFilename()
00227             self.dirname = dlg.GetDirectory()
00228             fname = os.path.join(self.dirname, self.filename)
00229             print "fname = ",fname
00230             #f = open(fname, 'r')
00231             #self.control.SetValue(f.read())
00232             #print f
00233             #f.close()
00234         dlg.Destroy()
00235 
00236     def OnSaveAsFile(self, event):
00237         wcd='All files(*)|*|pyNastran Database (*.pndb)|*.pndb|'
00238         dir = os.getcwd()
00239         save_dlg = wx.FileDialog(self, message='Save file as...', defaultDir=dir, defaultFile='',
00240                         wildcard=wcd, style=wx.SAVE | wx.OVERWRITE_PROMPT)
00241         if save_dlg.ShowModal() == wx.ID_OK:
00242             path = save_dlg.GetPath()
00243             try:
00244                 print "save path = ",path
00245             except IOError, error:
00246                 dlg = wx.MessageDialog(self, 'Error saving file\n' + str(error))
00247                 #dlg.ShowModal()
00248         save_dlg.Destroy()
00249 
00250     def OnQuit(self, e):
00251         self.Close()
00252 
00253 def main():
00254     
00255     ex = wx.App(0)
00256     Example(None)
00257     #Example(title='pyNastran GUI')
00258     ex.MainLoop()
00259 
00260 
00261 if __name__ == '__main__':
00262     main()
00263 
 All Classes Namespaces Files Functions Variables