Adding support for forcing PLC variable in Viewer
authorlaurent
Sun, 06 Dec 2009 21:20:55 +0100
changeset 469 17411b970353
parent 468 922da7834c81
child 470 cc64bbb1d654
Adding support for forcing PLC variable in Viewer
Viewer.py
dialogs/ForceVariableDialog.py
dialogs/__init__.py
graphics/GraphicCommons.py
--- a/Viewer.py	Fri Dec 04 15:28:08 2009 +0100
+++ b/Viewer.py	Sun Dec 06 21:20:55 2009 +0100
@@ -1101,16 +1101,30 @@
 #                           Popup menu functions
 #-------------------------------------------------------------------------------
 
+    def GetForceVariableMenuFunction(self, iec_path):
+        iec_type = self.GetDataType(iec_path)
+        def ForceVariableFunction(event):
+            if iec_type is not None:
+                dialog = ForceVariableDialog(self.ParentWindow, iec_type)
+                if dialog.ShowModal() == wx.ID_OK:
+                    self.ForceDataValue(iec_path, dialog.GetValue())
+        return ForceVariableFunction
+
+    def GetReleaseVariableMenuFunction(self, iec_path):
+        def ReleaseVariableFunction(event):
+            self.ReleaseDataValue(iec_path)
+        return ReleaseVariableFunction
+
     def PopupForceMenu(self):
         iec_path = self.GetElementIECPath(self.SelectedElement)
         if iec_path is not None:
             menu = wx.Menu(title='')
             new_id = wx.NewId()
             AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Force value"))
-            #self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(base_type), id=new_id)
+            self.Bind(wx.EVT_MENU, self.GetForceVariableMenuFunction(iec_path.upper()), id=new_id)
             new_id = wx.NewId()
             AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Release value"))
-            #self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(base_type), id=new_id)
+            self.Bind(wx.EVT_MENU, self.GetReleaseVariableMenuFunction(iec_path.upper()), id=new_id)
             if self.SelectedElement.IsForced():
                 menu.Enable(new_id, True)
             else:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dialogs/ForceVariableDialog.py	Sun Dec 06 21:20:55 2009 +0100
@@ -0,0 +1,107 @@
+# -*- coding: utf-8 -*-
+
+#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
+#based on the plcopen standard. 
+#
+#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
+
+import wx
+
+#-------------------------------------------------------------------------------
+#                            Force Variable Dialog
+#-------------------------------------------------------------------------------
+
+LOCATIONDATATYPES = {"X" : ["BOOL"],
+                     "B" : ["SINT", "USINT", "BYTE", "STRING"],
+                     "W" : ["INT", "UINT", "WORD", "WSTRING"],
+                     "D" : ["DINT", "UDINT", "REAL", "DWORD"],
+                     "L" : ["LINT", "ULINT", "LREAL", "LWORD"]} 
+
+def checkbool(v):
+    return v in ["TRUE", "FALSE"]
+
+def gen_check_function(f):
+    def check_function(v):
+        try:
+            f(v)
+            return True
+        except:
+            return False
+    return check_function
+
+checkinteger = gen_check_function(int)
+checkfloat = gen_check_function(float)
+checkstring = gen_check_function(str)
+
+CheckTypeValue = {"BOOL": lambda x: x in ["TRUE", "FALSE"],
+                  "SINT": checkinteger,
+                  "INT": checkinteger,
+                  "DINT": checkinteger,
+                  "LINT": checkinteger,
+                  "USINT": checkinteger,
+                  "UINT": checkinteger,
+                  "UDINT": checkinteger,
+                  "ULINT": checkinteger,
+                  "BYTE": checkinteger,
+                  "WORD": checkinteger,
+                  "DWORD": checkinteger,
+                  "LWORD": checkinteger,
+                  "REAL": checkfloat,
+                  "LREAL": checkfloat,
+                  "STRING": checkstring,
+                  "WSTRING": checkstring,}
+
+
+class ForceVariableDialog(wx.TextEntryDialog):
+
+    if wx.VERSION < (2, 6, 0):
+        def Bind(self, event, function, id = None):
+            if id is not None:
+                event(self, id, function)
+            else:
+                event(self, function)
+
+    def __init__(self, parent, iec_type):
+        wx.TextEntryDialog.__init__(self, parent, message = _("Forcing Variable Value"), 
+                caption = _("Please enter value for a \"%s\" variable:"%iec_type), defaultValue = "", 
+                style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition)
+        
+        self.IEC_Type = iec_type 
+        if wx.VERSION >= (2, 8, 0):
+            self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton().GetId())
+        elif wx.VERSION >= (2, 6, 0):
+            self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId())
+        else:
+            self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
+    
+    def OnOK(self, event):
+        value = self.GetSizer().GetItem(1).GetWindow().GetValue()
+        if value == "":
+            message = wx.MessageDialog(self, _("You must type a value!"), _("Error"), wx.OK|wx.ICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
+        elif not CheckTypeValue[self.IEC_Type](value):
+            message = wx.MessageDialog(self, _("Invalid value \"%s\" for \"%s\" variable!")%(value, self.IEC_Type), _("Error"), wx.OK|wx.ICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
+        else:
+            self.EndModal(wx.ID_OK)
+
+    def GetValue(self):
+        return self.GetSizer().GetItem(1).GetWindow().GetValue()
--- a/dialogs/__init__.py	Fri Dec 04 15:28:08 2009 +0100
+++ b/dialogs/__init__.py	Sun Dec 06 21:20:55 2009 +0100
@@ -34,3 +34,4 @@
 from SFCStepNameDialog import SFCStepNameDialog
 from SFCTransitionDialog import SFCTransitionDialog
 from SFCDivergenceDialog import SFCDivergenceDialog
+from ForceVariableDialog import ForceVariableDialog
\ No newline at end of file
--- a/graphics/GraphicCommons.py	Fri Dec 04 15:28:08 2009 +0100
+++ b/graphics/GraphicCommons.py	Sun Dec 06 21:20:55 2009 +0100
@@ -267,6 +267,19 @@
         if iec_path is not None:
             self.DataProducer.UnsubscribeDebugIECVariable(iec_path, consumer)
     
+    def GetDataType(self, iec_path):
+        if self.DataProducer is not None:
+            return self.DataProducer.GetDebugIECVariableType(iec_path)
+        return None
+    
+    def ForceDataValue(self, iec_path, value):
+        if self.DataProducer is not None:
+            self.DataProducer.ForceDebugIECVariable(iec_path, value)
+    
+    def ReleaseDataValue(self, iec_path):
+        if self.DataProducer is not None:
+            self.DataProducer.ReleaseDebugIECVariable(iec_path)
+    
     def DeleteDataConsumers(self):
         if self.DataProducer is not None:
             for consumer, iec_path in self.DataConsumers.iteritems():
@@ -1757,7 +1770,7 @@
                     self.ToolTip.SetTip(self.ComputedValue)
                 if len(self.ComputedValue) > 4:
                     self.ComputedValue = self.ComputedValue[:4] + "..."
-            if isinstance(self.ComputedValue, StringType):
+            if isinstance(self.ComputedValue, (StringType, UnicodeType)):
                 self.ValueSize = self.Parent.GetMiniTextExtent(self.ComputedValue)
             else:
                 self.ValueSize = None