#This file is part of Beremiz, a Integrated Development Environment for
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
#See COPYING file for copyrights details.
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#General Public License for more details.
#You should have received a copy of the GNU General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
__version__ = "$Revision$"
from time import localtime
from datetime import datetime
import os, re, platform, sys, time, traceback, getopt, commands
base_folder = os.path.split(sys.path[0])[0]
sys.path.append(os.path.join(base_folder, "plcopeneditor"))
sys.path.append(os.path.join(base_folder, "CanFestival-3", "objdictgen"))
sys.path.append(os.path.join(base_folder, "wxsvg", "svgui", "defeditor"))
iec2cc_path = os.path.join(base_folder, "matiec", "iec2cc")
ieclib_path = os.path.join(base_folder, "matiec", "lib")
from PLCOpenEditor import PLCOpenEditor, ProjectDialog
from TextViewer import TextViewer
from plcopen.structures import IEC_KEYWORDS#, AddPlugin
from plugger import PluginsRoot
""" Base class for file like objects to facilitate StdOut for the Shell."""
def __init__(self, output = None):
self.red_white = wx.TextAttr("RED", "WHITE")
self.red_yellow = wx.TextAttr("RED", "YELLOW")
self.black_white = wx.TextAttr("BLACK", "WHITE")
self.default_style = None
if self.default_style != self.black_white:
self.output.SetDefaultStyle(self.black_white)
self.default_style = self.black_white
self.output.AppendText(s)
def write_warning(self, s):
if self.default_style != self.red_white:
self.output.SetDefaultStyle(self.red_white)
self.default_style = self.red_white
self.output.AppendText(s)
def write_error(self, s):
if self.default_style != self.red_yellow:
self.output.SetDefaultStyle(self.red_yellow)
self.default_style = self.red_yellow
self.output.AppendText(s)
class AttributesTable(wx.grid.PyGridTableBase):
A custom wxGrid Table using user supplied data
def __init__(self, parent, data, colnames):
# The base class must be initialized *first*
wx.grid.PyGridTableBase.__init__(self)
# we need to store the row length and collength to
# see if the table has changed size
self._rows = self.GetNumberRows()
self._cols = self.GetNumberCols()
return len(self.colnames)
def GetColLabelValue(self, col):
if col < len(self.colnames):
return self.colnames[col]
def GetRowLabelValues(self, row):
def GetValue(self, row, col):
if row < self.GetNumberRows():
name = str(self.data[row].get(self.GetColLabelValue(col), ""))
def GetValueByName(self, row, colname):
return self.data[row].get(colname)
def SetValue(self, row, col, value):
if col < len(self.colnames):
self.data[row][self.GetColLabelValue(col)] = value
def ResetView(self, grid):
(wxGrid) -> Reset the grid view. Call this to
update the grid if rows and columns have been added or deleted
for current, new, delmsg, addmsg in [
(self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
(self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED),
msg = wx.grid.GridTableMessage(self,delmsg,new,current-new)
grid.ProcessTableMessage(msg)
msg = wx.grid.GridTableMessage(self,addmsg,new-current)
grid.ProcessTableMessage(msg)
self._rows = self.GetNumberRows()
self._cols = self.GetNumberCols()
# update the column rendering scheme
self._updateColAttrs(grid)
# update the scrollbars and the displayed part of the grid
def UpdateValues(self, grid):
"""Update all displayed values"""
# This sends an event to the grid table to update all of the values
msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
grid.ProcessTableMessage(msg)
def _updateColAttrs(self, grid):
wxGrid -> update the column attributes to add the
appropriate renderer given the column name.
Otherwise default to the default renderer.
for row in range(self.GetNumberRows()):
for col in range(self.GetNumberCols()):
colname = self.GetColLabelValue(col)
grid.SetReadOnly(row, col, False)
value_type = self.data[row]["Type"]
if isinstance(value_type, types.ListType):
editor = wx.grid.GridCellChoiceEditor()
editor.SetParameters(",".join(value_type))
elif value_type == "boolean":
editor = wx.grid.GridCellChoiceEditor()
editor.SetParameters("True,False")
elif value_type in ["unsignedLong","long","integer"]:
editor = wx.grid.GridCellNumberEditor()
elif value_type == "decimal":
editor = wx.grid.GridCellFloatEditor()
editor = wx.grid.GridCellTextEditor()
grid.SetReadOnly(row, col, True)
attr = wx.grid.GridCellAttr()
attr.SetAlignment(align, wx.ALIGN_CENTRE)
grid.SetColAttr(col, attr)
grid.SetColSize(col, colSize)
grid.SetCellEditor(row, col, editor)
grid.SetCellRenderer(row, col, renderer)
grid.SetCellBackgroundColour(row, col, wx.WHITE)
def AppendRow(self, row_content):
self.data.append(row_content)
def RemoveRow(self, row_index):
def GetRow(self, row_index):
return self.data[row_index]
[ID_BEREMIZ, ID_BEREMIZMAINSPLITTER,
ID_BEREMIZSECONDSPLITTER, ID_BEREMIZLEFTPANEL,
ID_BEREMIZPARAMSPANEL, ID_BEREMIZLOGCONSOLE,
ID_BEREMIZPLUGINTREE, ID_BEREMIZPLUGINCHILDS,
ID_BEREMIZADDBUTTON, ID_BEREMIZDELETEBUTTON,
ID_BEREMIZPARAMSSTATICBOX, ID_BEREMIZPARAMSENABLE,
ID_BEREMIZPARAMSTARGETTYPE, ID_BEREMIZPARAMSIECCHANNEL,
ID_BEREMIZPARAMSSTATICTEXT1, ID_BEREMIZPARAMSSTATICTEXT2,
ID_BEREMIZPARAMSATTRIBUTESGRID,
] = [wx.NewId() for _init_ctrls in range(17)]
[ID_BEREMIZFILEMENUITEMS0, ID_BEREMIZFILEMENUITEMS1,
ID_BEREMIZFILEMENUITEMS2, ID_BEREMIZFILEMENUITEMS3,
ID_BEREMIZFILEMENUITEMS5, ID_BEREMIZFILEMENUITEMS7,
] = [wx.NewId() for _init_coll_FileMenu_Items in range(6)]
[ID_BEREMIZEDITMENUITEMS0, ID_BEREMIZEDITMENUITEMS2,
ID_BEREMIZEDITMENUITEMS3,
] = [wx.NewId() for _init_coll_EditMenu_Items in range(3)]
[ID_BEREMIZRUNMENUITEMS0, ID_BEREMIZRUNMENUITEMS2,
ID_BEREMIZRUNMENUITEMS3, ID_BEREMIZRUNMENUITEMS5,
] = [wx.NewId() for _init_coll_EditMenu_Items in range(4)]
[ID_BEREMIZHELPMENUITEMS0, ID_BEREMIZHELPMENUITEMS1,
] = [wx.NewId() for _init_coll_HelpMenu_Items in range(2)]
def _init_coll_FileMenu_Items(self, parent):
parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS0,
kind=wx.ITEM_NORMAL, text=u'New\tCTRL+N')
parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS1,
kind=wx.ITEM_NORMAL, text=u'Open\tCTRL+O')
parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS2,
kind=wx.ITEM_NORMAL, text=u'Save\tCTRL+S')
parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS3,
kind=wx.ITEM_NORMAL, text=u'Close Project')
parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS5,
kind=wx.ITEM_NORMAL, text=u'Properties')
parent.Append(help='', id=ID_BEREMIZFILEMENUITEMS7,
kind=wx.ITEM_NORMAL, text=u'Quit\tCTRL+Q')
self.Bind(wx.EVT_MENU, self.OnNewProjectMenu,
id=ID_BEREMIZFILEMENUITEMS0)
self.Bind(wx.EVT_MENU, self.OnOpenProjectMenu,
id=ID_BEREMIZFILEMENUITEMS1)
self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu,
id=ID_BEREMIZFILEMENUITEMS2)
self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu,
id=ID_BEREMIZFILEMENUITEMS3)
self.Bind(wx.EVT_MENU, self.OnPropertiesMenu,
id=ID_BEREMIZFILEMENUITEMS5)
self.Bind(wx.EVT_MENU, self.OnQuitMenu,
id=ID_BEREMIZFILEMENUITEMS7)
def _init_coll_EditMenu_Items(self, parent):
parent.Append(help='', id=ID_BEREMIZEDITMENUITEMS0,
kind=wx.ITEM_NORMAL, text=u'Edit PLC\tCTRL+R')
parent.Append(help='', id=ID_BEREMIZEDITMENUITEMS2,
kind=wx.ITEM_NORMAL, text=u'Add Plugin')
parent.Append(help='', id=ID_BEREMIZEDITMENUITEMS3,
kind=wx.ITEM_NORMAL, text=u'Delete Plugin')
self.Bind(wx.EVT_MENU, self.OnEditPLCMenu,
id=ID_BEREMIZEDITMENUITEMS0)
self.Bind(wx.EVT_MENU, self.OnAddMenu,
id=ID_BEREMIZEDITMENUITEMS2)
self.Bind(wx.EVT_MENU, self.OnDeleteMenu,
id=ID_BEREMIZEDITMENUITEMS3)
def _init_coll_RunMenu_Items(self, parent):
parent.Append(help='', id=ID_BEREMIZRUNMENUITEMS0,
kind=wx.ITEM_NORMAL, text=u'Build\tCTRL+R')
parent.Append(help='', id=ID_BEREMIZRUNMENUITEMS2,
kind=wx.ITEM_NORMAL, text=u'Simulate')
parent.Append(help='', id=ID_BEREMIZRUNMENUITEMS3,
kind=wx.ITEM_NORMAL, text=u'Run')
parent.Append(help='', id=ID_BEREMIZRUNMENUITEMS5,
kind=wx.ITEM_NORMAL, text=u'Save Log')
self.Bind(wx.EVT_MENU, self.OnBuildMenu,
id=ID_BEREMIZRUNMENUITEMS0)
self.Bind(wx.EVT_MENU, self.OnSimulateMenu,
id=ID_BEREMIZRUNMENUITEMS2)
self.Bind(wx.EVT_MENU, self.OnRunMenu,
id=ID_BEREMIZRUNMENUITEMS3)
self.Bind(wx.EVT_MENU, self.OnSaveLogMenu,
id=ID_BEREMIZRUNMENUITEMS5)
def _init_coll_HelpMenu_Items(self, parent):
parent.Append(help='', id=ID_BEREMIZHELPMENUITEMS0,
kind=wx.ITEM_NORMAL, text=u'Beremiz\tF1')
parent.Append(help='', id=ID_BEREMIZHELPMENUITEMS1,
kind=wx.ITEM_NORMAL, text=u'About')
self.Bind(wx.EVT_MENU, self.OnBeremizMenu,
id=ID_BEREMIZHELPMENUITEMS0)
self.Bind(wx.EVT_MENU, self.OnAboutMenu,
id=ID_BEREMIZHELPMENUITEMS1)
def _init_coll_menuBar1_Menus(self, parent):
parent.Append(menu=self.FileMenu, title=u'File')
parent.Append(menu=self.EditMenu, title=u'Edit')
parent.Append(menu=self.RunMenu, title=u'Run')
parent.Append(menu=self.HelpMenu, title=u'Help')
self.menuBar1 = wx.MenuBar()
self.FileMenu = wx.Menu(title=u'')
self.EditMenu = wx.Menu(title=u'')
self.RunMenu = wx.Menu(title=u'')
self.HelpMenu = wx.Menu(title=u'')
self._init_coll_menuBar1_Menus(self.menuBar1)
self._init_coll_FileMenu_Items(self.FileMenu)
self._init_coll_EditMenu_Items(self.EditMenu)
self._init_coll_RunMenu_Items(self.RunMenu)
self._init_coll_HelpMenu_Items(self.HelpMenu)
def _init_coll_LeftGridSizer_Items(self, parent):
parent.AddWindow(self.PluginTree, 0, border=0, flag=wx.GROW)
parent.AddSizer(self.ButtonGridSizer, 0, border=0, flag=wx.GROW)
def _init_coll_LeftGridSizer_Growables(self, parent):
def _init_coll_ButtonGridSizer_Items(self, parent):
parent.AddWindow(self.PluginChilds, 0, border=0, flag=wx.GROW)
parent.AddWindow(self.AddButton, 0, border=0, flag=0)
parent.AddWindow(self.DeleteButton, 0, border=0, flag=0)
def _init_coll_ButtonGridSizer_Growables(self, parent):
def _init_coll_ParamsPanelMainSizer_Items(self, parent):
parent.AddSizer(self.ParamsPanelChildSizer, 1, border=10, flag=wx.GROW|wx.ALL)
parent.AddSizer(self.ParamsPanelPluginSizer, 1, border=10, flag=wx.GROW|wx.ALL)
parent.AddWindow(self.AttributesGrid, 2, border=10, flag=wx.GROW|wx.TOP|wx.RIGHT|wx.BOTTOM)
def _init_coll_ParamsPanelChildSizer_Items(self, parent):
parent.AddWindow(self.ParamsEnable, 0, border=5, flag=wx.GROW|wx.BOTTOM)
parent.AddWindow(self.ParamsStaticText1, 0, border=5, flag=wx.GROW|wx.BOTTOM)
parent.AddWindow(self.ParamsIECChannel, 0, border=0, flag=wx.GROW)
def _init_coll_ParamsPanelPluginSizer_Items(self, parent):
parent.AddWindow(self.ParamsStaticText2, 0, border=5, flag=wx.GROW|wx.BOTTOM)
parent.AddWindow(self.ParamsTargetType, 0, border=0, flag=wx.GROW)
self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=2, rows=2, vgap=2)
self.ButtonGridSizer = wx.FlexGridSizer(cols=3, hgap=2, rows=1, vgap=2)
self.ParamsPanelMainSizer = wx.StaticBoxSizer(self.ParamsStaticBox, wx.HORIZONTAL)
self.ParamsPanelChildSizer = wx.BoxSizer(wx.VERTICAL)
self.ParamsPanelPluginSizer = wx.BoxSizer(wx.VERTICAL)
self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer)
self._init_coll_LeftGridSizer_Items(self.LeftGridSizer)
self._init_coll_ButtonGridSizer_Growables(self.ButtonGridSizer)
self._init_coll_ButtonGridSizer_Items(self.ButtonGridSizer)
self._init_coll_ParamsPanelMainSizer_Items(self.ParamsPanelMainSizer)
self._init_coll_ParamsPanelChildSizer_Items(self.ParamsPanelChildSizer)
self._init_coll_ParamsPanelPluginSizer_Items(self.ParamsPanelPluginSizer)
self.LeftPanel.SetSizer(self.LeftGridSizer)
self.ParamsPanel.SetSizer(self.ParamsPanelMainSizer)
def _init_ctrls(self, prnt):
wx.Frame.__init__(self, id=ID_BEREMIZ, name=u'Beremiz',
parent=prnt, pos=wx.Point(0, 0), size=wx.Size(1000, 600),
style=wx.DEFAULT_FRAME_STYLE, title=u'Beremiz')
self.SetClientSize(wx.Size(1000, 600))
self.SetMenuBar(self.menuBar1)
self.MainSplitter = wx.SplitterWindow(id=ID_BEREMIZMAINSPLITTER,
name='MainSplitter', parent=self, point=wx.Point(0, 0),
size=wx.Size(0, 0), style=wx.SP_3D)
self.MainSplitter.SetNeedUpdating(True)
self.MainSplitter.SetMinimumPaneSize(1)
self.LeftPanel = wx.Panel(id=ID_BEREMIZLEFTPANEL,
name='LeftPanel', parent=self.MainSplitter, pos=wx.Point(0, 0),
size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)
self.PluginTree = wx.TreeCtrl(id=ID_BEREMIZPLUGINTREE,
name='PluginTree', parent=self.LeftPanel, pos=wx.Point(0, 0),
size=wx.Size(-1, -1), style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER)
self.PluginTree.Bind(wx.EVT_RIGHT_UP, self.OnPluginTreeRightUp)
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnPluginTreeItemSelected,
self.PluginChilds = wx.Choice(id=ID_BEREMIZPLUGINCHILDS,
name='PluginChilds', parent=self.LeftPanel, pos=wx.Point(0, 0),
size=wx.Size(-1, -1), style=0)
self.AddButton = wx.Button(id=ID_BEREMIZADDBUTTON, label='Add',
name='AddBusButton', parent=self.LeftPanel, pos=wx.Point(0, 0),
size=wx.Size(48, 30), style=0)
self.AddButton.Bind(wx.EVT_BUTTON, self.OnAddButton,
self.DeleteButton = wx.Button(id=ID_BEREMIZDELETEBUTTON, label='Delete',
name='DeleteBusButton', parent=self.LeftPanel, pos=wx.Point(0, 0),
size=wx.Size(64, 30), style=0)
self.DeleteButton.Bind(wx.EVT_BUTTON, self.OnDeleteButton,
id=ID_BEREMIZDELETEBUTTON)
self.SecondSplitter = wx.SplitterWindow(id=ID_BEREMIZSECONDSPLITTER,
name='SecondSplitter', parent=self.MainSplitter, point=wx.Point(0, 0),
size=wx.Size(0, 0), style=wx.SP_3D)
self.SecondSplitter.SetNeedUpdating(True)
self.SecondSplitter.SetMinimumPaneSize(1)
self.MainSplitter.SplitVertically(self.LeftPanel, self.SecondSplitter,
self.ParamsPanel = wx.Panel(id=ID_BEREMIZPARAMSPANEL,
name='ParamsPanel', parent=self.SecondSplitter, pos=wx.Point(0, 0),
size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)
self.ParamsStaticBox = wx.StaticBox(id=ID_BEREMIZPARAMSSTATICBOX,
label='', name='staticBox1', parent=self.ParamsPanel,
pos=wx.Point(0, 0), size=wx.Size(0, 0), style=0)
self.ParamsEnable = wx.CheckBox(id=ID_BEREMIZPARAMSENABLE,
label='Plugin enabled', name='ParamsEnable', parent=self.ParamsPanel,
pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
self.Bind(wx.EVT_CHECKBOX, self.OnParamsEnableChanged, id=ID_BEREMIZPARAMSENABLE)
self.ParamsStaticText1 = wx.StaticText(id=ID_BEREMIZPARAMSSTATICTEXT1,
label='IEC Channel:', name='ParamsStaticText1', parent=self.ParamsPanel,
pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
self.ParamsIECChannel = wx.SpinCtrl(id=ID_BEREMIZPARAMSIECCHANNEL,
name='ParamsIECChannel', parent=self.ParamsPanel, pos=wx.Point(0, 0),
size=wx.Size(0, 24), style=wx.SP_ARROW_KEYS, min=0)
self.Bind(wx.EVT_SPINCTRL, self.OnParamsIECChannelChanged, id=ID_BEREMIZPARAMSIECCHANNEL)
self.ParamsStaticText2 = wx.StaticText(id=ID_BEREMIZPARAMSSTATICTEXT2,
label='Target Type:', name='ParamsStaticText2', parent=self.ParamsPanel,
pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
self.ParamsTargetType = wx.Choice(id=ID_BEREMIZPARAMSTARGETTYPE,
name='TargetType', choices=[""], parent=self.ParamsPanel,
pos=wx.Point(0, 0), size=wx.Size(0, 24), style=wx.LB_SINGLE)
self.Bind(wx.EVT_CHOICE, self.OnParamsTargetTypeChanged, id=ID_BEREMIZPARAMSTARGETTYPE)
self.AttributesGrid = wx.grid.Grid(id=ID_BEREMIZPARAMSATTRIBUTESGRID,
name='AttributesGrid', parent=self.ParamsPanel, pos=wx.Point(0, 0),
size=wx.Size(0, 150), style=wx.VSCROLL)
self.AttributesGrid.SetFont(wx.Font(12, 77, wx.NORMAL, wx.NORMAL, False,
self.AttributesGrid.SetLabelFont(wx.Font(10, 77, wx.NORMAL, wx.NORMAL,
self.AttributesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnAttributesGridCellChange)
self.LogConsole = wx.TextCtrl(id=ID_BEREMIZLOGCONSOLE, value='',
name='LogConsole', parent=self.SecondSplitter, pos=wx.Point(0, 0),
size=wx.Size(0, 0), style=wx.TE_MULTILINE|wx.TE_RICH2)
self.SecondSplitter.SplitHorizontally(self.ParamsPanel, self.LogConsole,
def __init__(self, parent, projectOpen):
self.Log = LogPseudoFile(self.LogConsole)
self.PluginRoot = PluginsRoot()
for value in self.PluginRoot.GetTargetTypes():
self.ParamsTargetType.Append(value)
self.Table = AttributesTable(self, [], ["Attribute", "Value"])
self.AttributesGrid.SetTable(self.Table)
self.AttributesGrid.SetRowLabelSize(0)
self.PluginRoot.LoadProject(projectOpen)
self.RefreshPluginParams()
def RefreshButtons(self):
if self.PluginRoot.HasProjectOpened():
self.PluginChilds.Enable(True)
self.AddButton.Enable(True)
self.DeleteButton.Enable(True)
self.PluginChilds.Enable(False)
self.AddButton.Enable(False)
self.DeleteButton.Enable(False)
def RefreshMainMenu(self):
if self.PluginRoot.HasProjectOpened():
self.menuBar1.EnableTop(1, True)
self.menuBar1.EnableTop(2, True)
self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS2, True)
self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS3, True)
self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS5, True)
self.menuBar1.EnableTop(1, False)
self.menuBar1.EnableTop(2, False)
self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS2, False)
self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS3, False)
self.FileMenu.Enable(ID_BEREMIZFILEMENUITEMS5, False)
def RefreshPluginTree(self):
infos = self.PluginRoot.GetPlugInfos()
root = self.PluginTree.GetRootItem()
self.GenerateTreeBranch(root, infos)
self.PluginTree.Expand(self.PluginTree.GetRootItem())
self.RefreshPluginParams()
def GenerateTreeBranch(self, root, infos):
self.PluginTree.SetItemText(root, infos["name"])
root = self.PluginTree.AddRoot(infos["name"])
self.PluginTree.SetPyData(root, infos["type"])
item, root_cookie = self.PluginTree.GetFirstChild(root)
if len(infos["values"]) > 0:
for values in infos["values"]:
item = self.PluginTree.AppendItem(root, "")
item, root_cookie = self.PluginTree.GetNextChild(root, root_cookie)
self.GenerateTreeBranch(item, values)
item, root_cookie = self.PluginTree.GetNextChild(root, root_cookie)
item, root_cookie = self.PluginTree.GetNextChild(root, root_cookie)
self.PluginTree.Delete(item)
def GetSelectedPlugin(self):
selected = self.PluginTree.GetSelection()
if selected == self.PluginTree.GetRootItem():
name = self.PluginTree.GetItemText(selected)
item = self.PluginTree.GetItemParent(selected)
while item.IsOk() and item != self.PluginTree.GetRootItem():
name = "%s.%s"%(self.PluginTree.GetItemText(item), name)
item = self.PluginTree.GetItemParent(item)
return self.PluginRoot.GetChildByName(name)
def OnPluginTreeItemSelected(self, event):
wx.CallAfter(self.RefreshPluginParams)
def _GetAddPluginFunction(self, name):
def OnPluginTreeRightUp(self, event):
plugin = self.GetSelectedPlugin()
main_menu = wx.Menu(title='')
if len(plugin.PlugChildsTypes) > 0:
plugin_menu = wx.Menu(title='')
for name, XSDClass in self.GetSelectedPlugin().PlugChildsTypes:
plugin_menu.Append(help='', id=new_id, kind=wx.ITEM_NORMAL, text=name)
self.Bind(wx.EVT_MENU, self._GetAddPluginFunction(name), id=new_id)
main_menu.AppendMenu(-1, "Add", plugin_menu, '')
main_menu.Append(help='', id=new_id, kind=wx.ITEM_NORMAL, text="Delete")
self.Bind(wx.EVT_MENU, self.OnDeleteButton, id=new_id)
rect = self.PluginTree.GetBoundingRect(self.PluginTree.GetSelection())
self.PluginTree.PopupMenuXY(main_menu, rect.x + rect.width, rect.y)
def RefreshPluginParams(self):
plugin = self.GetSelectedPlugin()
self.PluginChilds.Clear()
self.PluginChilds.Enable(False)
self.AddButton.Enable(False)
self.DeleteButton.Enable(False)
self.ParamsStaticBox.SetLabel(plugin.BaseParams.getName())
if plugin == self.PluginRoot:
self.ParamsPanelMainSizer.Hide(self.ParamsPanelChildSizer)
self.ParamsPanelMainSizer.Show(self.ParamsPanelPluginSizer)
self.ParamsTargetType.SetStringSelection(self.PluginRoot.GetTargetType())
self.ParamsPanelMainSizer.Show(self.ParamsPanelChildSizer)
self.ParamsPanelMainSizer.Hide(self.ParamsPanelPluginSizer)
self.ParamsEnable.SetValue(plugin.BaseParams.getEnabled())
self.ParamsEnable.Enable(True)
self.ParamsStaticText1.Enable(True)
self.ParamsIECChannel.SetValue(plugin.BaseParams.getIEC_Channel())
self.ParamsIECChannel.Enable(True)
self.ParamsPanelMainSizer.Layout()
self.RefreshAttributesGrid()
self.PluginChilds.Clear()
if len(plugin.PlugChildsTypes) > 0:
self.PluginChilds.Append("")
for name, XSDClass in plugin.PlugChildsTypes:
self.PluginChilds.Append(name)
self.PluginChilds.Enable(True)
self.AddButton.Enable(True)
self.PluginChilds.Enable(False)
self.AddButton.Enable(False)
self.DeleteButton.Enable(True)
def RefreshAttributesGrid(self):
plugin = self.GetSelectedPlugin()
if plugin == self.PluginRoot:
attr_infos = self.PluginRoot.GetTargetAttributes()
attr_infos = plugin.GetPlugParamsAttributes()
data.append({"Attribute" : infos["name"], "Value" : infos["value"],
self.Table.ResetView(self.AttributesGrid)
def OnParamsEnableChanged(self, event):
plugin = self.GetSelectedPlugin()
if plugin and plugin != self.PluginRoot:
plugin.BaseParams.setEnabled(event.Checked())
def OnParamsIECChannelChanged(self, event):
plugin = self.GetSelectedPlugin()
if plugin and plugin != self.PluginRoot:
plugin.BaseParams.setIEC_Channel(self.ParamsIECChannel.GetValue())
def OnParamsTargetTypeChanged(self, event):
plugin = self.GetSelectedPlugin()
if plugin and plugin == self.PluginRoot:
self.PluginRoot.ChangeTargetType(self.ParamsTargetType.GetStringSelection())
self.RefreshAttributesGrid()
def OnAttributesGridCellChange(self, event):
plugin = self.GetSelectedPlugin()
name = self.Table.GetValueByName(row, "Attribute")
value = self.Table.GetValueByName(row, "Value")
if plugin == self.PluginRoot:
self.PluginRoot.SetTargetAttribute(name, value)
plugin.SetPlugParamsAttribute(name, value)
def OnNewProjectMenu(self, event):
defaultpath = self.PluginRoot.GetProjectPath()
defaultpath = os.getcwd()
dialog = wx.DirDialog(self , "Choose a project", defaultpath, wx.DD_NEW_DIR_BUTTON)
if dialog.ShowModal() == wx.ID_OK:
projectpath = dialog.GetPath()
if os.path.isdir(projectpath) and len(os.listdir(projectpath)) == 0:
dialog = ProjectDialog(self)
if dialog.ShowModal() == wx.ID_OK:
values = dialog.GetValues()
values["creationDateTime"] = datetime(*localtime()[:6])
self.PluginRoot.NewProject(projectpath, values)
message = wx.MessageDialog(self, "Folder choosen isn't empty. You can't use it for a new project!", "ERROR", wx.OK|wx.ICON_ERROR)
def OnOpenProjectMenu(self, event):
defaultpath = self.PluginRoot.GetProjectPath()
defaultpath = os.getcwd()
dialog = wx.DirDialog(self , "Choose a project", defaultpath, wx.DD_NEW_DIR_BUTTON)
if dialog.ShowModal() == wx.ID_OK:
projectpath = dialog.GetPath()
if os.path.isdir(projectpath):
result = self.PluginRoot.LoadProject(projectpath)
message = wx.MessageDialog(self, result, "Error", wx.OK|wx.ICON_ERROR)
message = wx.MessageDialog(self, "\"%s\" folder is not a valid Beremiz project\n"%projectpath, "Error", wx.OK|wx.ICON_ERROR)
def OnCloseProjectMenu(self, event):
self.CurrentProjectPath = projectpath
def OnSaveProjectMenu(self, event):
if self.PluginRoot.HasProjectOpened():
self.PluginRoot.SaveProject()
def OnPropertiesMenu(self, event):
def OnQuitMenu(self, event):
def OnEditPLCMenu(self, event):
def OnAddMenu(self, event):
def OnDeleteMenu(self, event):
def OnBuildMenu(self, event):
def OnSimulateMenu(self, event):
def OnRunMenu(self, event):
def OnSaveLogMenu(self, event):
def OnBeremizMenu(self, event):
def OnAboutMenu(self, event):
def OnAddButton(self, event):
PluginType = self.PluginChilds.GetStringSelection()
self.AddPlugin(PluginType)
def OnDeleteButton(self, event):
def CloseEditor(self, bus_id):
if self.BusManagers.get(bus_id, None) != None:
self.BusManagers[bus_id]["Editor"] = None
def AddPlugin(self, PluginType):
dialog = wx.TextEntryDialog(self, "Please enter a name for plugin:", "Add Plugin", "", wx.OK|wx.CANCEL)
if dialog.ShowModal() == wx.ID_OK:
PluginName = dialog.GetValue()
plugin = self.GetSelectedPlugin()
plugin.PlugAddChild(PluginName, PluginType)
self.PLCEditor = PLCOpenEditor(self, self.PluginRoot.PLCManager)
self.PLCEditor.RefreshProjectTree()
self.PLCEditor.RefreshFileMenu()
self.PLCEditor.RefreshEditMenu()
self.PLCEditor.RefreshToolBar()
def LogCommand(self, Command, sz_limit = 100):
import os, popen2, fcntl, select, signal
child = popen2.Popen3(Command, 1) # capture stdout and stderr from command
child.tochild.close() # don't need to talk to child
outfile = child.fromchild
ready = select.select([outfd,errfd],[],[]) # wait for input
outchunk = outfile.readline()
if outchunk == '': outeof = 1
errchunk = errfile.readline()
if errchunk == '': erreof = 1
self.Log.write_warning(errchunk)
if outeof and erreof : break
if errlen > sz_limit or outlen > sz_limit :
os.kill(child.pid, signal.SIGTERM)
self.Log.write_error("Output size reached limit -- killed\n")
return (err, outdata, errdata)
LOCATED_MODEL = re.compile("__LOCATED_VAR\(([A-Z]*),([_A-Za-z0-9]*)\)")
self.TargetDir = os.path.join(self.CurrentProjectPath, "build")
if not os.path.exists(self.TargetDir):
self.Log.write("Generating IEC-61131 code...\n")
plc_file = os.path.join(self.TargetDir, "plc.st")
result = self.PLCManager.GenerateProgram(plc_file)
raise Exception, "Error : ST/IL/SFC code generator returned %d"%result
self.Log.write("Compiling ST Program in to C Program...\n")
status, result, err_result = self.LogCommand("%s %s -I %s %s"%(iec2cc_path, plc_file, ieclib_path, self.TargetDir))
new_dialog = wx.Frame(None)
ST_viewer = TextViewer(new_dialog, None, None)
ST_viewer.SetKeywords(IEC_KEYWORDS)
ST_viewer.SetText(file(plc_file).read())
raise Exception, "Error : IEC to C compiler returned %d"%status
C_files = result.splitlines()
C_files = map(lambda filename:os.path.join(self.TargetDir, filename), C_files)
self.Log.write("Extracting Located Variables...\n")
location_file = open(os.path.join(self.TargetDir,"LOCATED_VARIABLES.h"))
lines = [line.strip() for line in location_file.readlines()]
result = LOCATED_MODEL.match(line)
locations.append(result.groups())
self.Log.write("Generating Network Configurations...\n")
for bus_id, bus_infos in self.BusManagers.items():
c_filename = "%s.c"%os.path.join(self.TargetDir, gen_cfile.FormatName(bus_infos["Name"]))
result = bus_infos["Manager"].GenerateBus(c_filename, locations)
C_files.append(c_filename)
self.Log.write("Generating Makefiles...\n")
self.Log.write(str(C_files))
self.Log.write("Compiling Project...\n")
self.Log.write("\nBuild Project completed\n")
except Exception, message:
self.Log.write_error("\nBuild Failed\n")
self.Log.write(str(message))
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
[ID_ADDBUSDIALOG, ID_ADDBUSDIALOGBUSID,
ID_ADDBUSDIALOGBUSNAME, ID_ADDBUSDIALOGBUSTYPE,
ID_ADDBUSDIALOGSTATICTEXT1, ID_ADDBUSDIALOGSTATICTEXT2,
ID_ADDBUSDIALOGSTATICTEXT3,
] = [wx.NewId() for _init_ctrls in range(7)]
class AddBusDialog(wx.Dialog):
def _init_coll_flexGridSizer1_Items(self, parent):
parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
def _init_coll_flexGridSizer1_Growables(self, parent):
def _init_coll_MainSizer_Items(self, parent):
parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW)
parent.AddWindow(self.BusId, 0, border=0, flag=wx.GROW)
parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW)
parent.AddWindow(self.BusType, 0, border=0, flag=wx.GROW)
parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW)
parent.AddWindow(self.BusName, 0, border=0, flag=wx.GROW)
def _init_coll_MainSizer_Growables(self, parent):
self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
self.MainSizer = wx.FlexGridSizer(cols=2, hgap=0, rows=3, vgap=15)
self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1)
self._init_coll_MainSizer_Items(self.MainSizer)
self._init_coll_MainSizer_Growables(self.MainSizer)
self.SetSizer(self.flexGridSizer1)
def _init_ctrls(self, prnt):
wx.Dialog.__init__(self, id=ID_ADDBUSDIALOG,
name='PouDialog', parent=prnt, pos=wx.Point(376, 183),
size=wx.Size(300, 200), style=wx.DEFAULT_DIALOG_STYLE,
title='Create a new POU')
self.SetClientSize(wx.Size(300, 200))
self.staticText1 = wx.StaticText(id=ID_ADDBUSDIALOGSTATICTEXT1,
label='Bus ID:', name='staticText1', parent=self,
pos=wx.Point(0, 0), size=wx.Size(100, 17), style=0)
self.BusId = wx.TextCtrl(id=ID_ADDBUSDIALOGBUSID,
name='BusId', parent=self, pos=wx.Point(0, 0),
size=wx.Size(0, 24), style=0)
self.staticText2 = wx.StaticText(id=ID_ADDBUSDIALOGSTATICTEXT2,
label='Bus Type:', name='staticText2', parent=self,
pos=wx.Point(0, 0), size=wx.Size(100, 17), style=0)
self.BusType = wx.Choice(id=ID_ADDBUSDIALOGBUSTYPE,
name='BusType', parent=self, pos=wx.Point(0, 0),
size=wx.Size(0, 24), style=0)
self.staticText3 = wx.StaticText(id=ID_ADDBUSDIALOGSTATICTEXT3,
label='Bus Name:', name='staticText3', parent=self,
pos=wx.Point(0, 0), size=wx.Size(100, 17), style=0)
self.BusName = wx.TextCtrl(id=ID_ADDBUSDIALOGBUSNAME,
name='BusName', parent=self, pos=wx.Point(0, 0),
size=wx.Size(0, 24), style=0)
self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
def __init__(self, parent):
for option in [""] + plugins.__all__:
self.BusType.Append(option)
bus_id = self.BusId.GetValue()
if self.BusType.GetStringSelection() == "":
if self.BusName.GetValue() == "":
for i, item in enumerate(error):
elif i == len(error) - 1:
message = wxMessageDialog(self, "Form isn't complete. %s must be filled!"%text, "Error", wxOK|wxICON_ERROR)
elif bus_id.startswith("0x"):
message = wxMessageDialog(self, "Bus ID must be a decimal or hexadecimal number!", "Error", wxOK|wxICON_ERROR)
elif not bus_id.startswith("-"):
message = wxMessageDialog(self, "Bus ID must be a decimal or hexadecimal number!", "Error", wxOK|wxICON_ERROR)
message = wxMessageDialog(self, "Bus Id must be greater than 0!", "Error", wxOK|wxICON_ERROR)
def SetValues(self, values):
for item, value in values.items():
self.BusID.SetValue(value)
self.BusType.SetStringSelection(value)
self.BusName.SetValue(value)
values["busID"] = self.BusId.GetValue()
values["busType"] = self.BusType.GetStringSelection()
values["busName"] = self.BusName.GetValue()
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
Max_Traceback_List_Size = 20
def Display_Exception_Dialog(e_type,e_value,e_tb):
for i,line in enumerate(traceback.extract_tb(e_tb)):
trcbck = " " + str(i+1) + ". "
if line[0].find(os.getcwd()) == -1:
trcbck += "file : " + str(line[0]) + ", "
trcbck += "file : " + str(line[0][len(os.getcwd()):]) + ", "
trcbck += "line : " + str(line[1]) + ", " + "function : " + str(line[2])
trcbck_lst.append(trcbck)
cap = wx.Window_GetCapture()
dlg = wx.SingleChoiceDialog(None,
Click on OK for saving an error report.
Please contact LOLITech at:
str(e_type) + " : " + str(e_value),
res = (dlg.ShowModal() == wx.ID_OK)
def Display_Error_Dialog(e_value):
message = wxMessageDialog(None, str(e_value), "Error", wxOK|wxICON_ERROR)
def get_last_traceback(tb):
def format_namespace(d, indent=' '):
return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()])
ignored_exceptions = [] # a problem with a line in a module is only reported once per session
def AddExceptHook(path, app_version='[No version]'):#, ignored_exceptions=[]):
def handle_exception(e_type, e_value, e_traceback):
traceback.print_exception(e_type, e_value, e_traceback) # this is very helpful when there's an exception in the rest of this func
last_tb = get_last_traceback(e_traceback)
ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno)
if str(e_value).startswith("!!!"):
Display_Error_Dialog(e_value)
elif ex not in ignored_exceptions:
result = Display_Exception_Dialog(e_type,e_value,e_traceback)
ignored_exceptions.append(ex)
'app-title' : wx.GetApp().GetAppName(), # app_title
'app-version' : app_version,
'wx-version' : wx.VERSION_STRING,
'wx-platform' : wx.Platform,
'python-version' : platform.python_version(), #sys.version.split()[0],
'platform' : platform.platform(),
info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value)
last_tb = get_last_traceback(e_traceback)
exception_locals = last_tb.tb_frame.f_locals # the locals at the level of the stack trace where the exception actually occurred
info['locals'] = format_namespace(exception_locals)
if 'self' in exception_locals:
info['self'] = format_namespace(exception_locals['self'].__dict__)
output = open(path+os.sep+"bug_report_"+info['date'].replace(':','-').replace(' ','_')+".txt",'w')
output.write(a+":\n"+str(info[a])+"\n\n")
#sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args)
sys.excepthook = handle_exception
if __name__ == '__main__':
print "\nUsage of Beremiz.py :"
print "\n %s [Projectpath]\n"%sys.argv[0]
opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
except getopt.GetoptError:
# print help information and exit:
if o in ("-h", "--help"):
wx.InitAllImageHandlers()
# Install a exception handle for bug reports
AddExceptHook(os.getcwd(),__version__)
frame = Beremiz(None, projectOpen)