author | Edouard Tisserant |
Tue, 05 Apr 2011 19:01:51 +0200 | |
changeset 524 | 9a5fa6679a94 |
parent 519 | 722714c04dcd |
child 525 | e8d5ab0855d3 |
permissions | -rw-r--r-- |
469 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
3 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
4 |
#based on the plcopen standard. |
|
5 |
# |
|
6 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
7 |
# |
|
8 |
#See COPYING file for copyrights details. |
|
9 |
# |
|
10 |
#This library is free software; you can redistribute it and/or |
|
11 |
#modify it under the terms of the GNU General Public |
|
12 |
#License as published by the Free Software Foundation; either |
|
13 |
#version 2.1 of the License, or (at your option) any later version. |
|
14 |
# |
|
15 |
#This library is distributed in the hope that it will be useful, |
|
16 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
18 |
#General Public License for more details. |
|
19 |
# |
|
20 |
#You should have received a copy of the GNU General Public |
|
21 |
#License along with this library; if not, write to the Free Software |
|
22 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
||
24 |
import wx |
|
519
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
25 |
import re |
516 | 26 |
import datetime |
469 | 27 |
|
28 |
#------------------------------------------------------------------------------- |
|
29 |
# Force Variable Dialog |
|
30 |
#------------------------------------------------------------------------------- |
|
31 |
||
32 |
LOCATIONDATATYPES = {"X" : ["BOOL"], |
|
33 |
"B" : ["SINT", "USINT", "BYTE", "STRING"], |
|
34 |
"W" : ["INT", "UINT", "WORD", "WSTRING"], |
|
35 |
"D" : ["DINT", "UDINT", "REAL", "DWORD"], |
|
36 |
"L" : ["LINT", "ULINT", "LREAL", "LWORD"]} |
|
37 |
||
475
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
38 |
def gen_get_function(f): |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
39 |
def get_function(v): |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
40 |
try: |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
41 |
return f(v) |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
42 |
except: |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
43 |
return None |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
44 |
return get_function |
469 | 45 |
|
476 | 46 |
getinteger = gen_get_function(int) |
47 |
getfloat = gen_get_function(float) |
|
48 |
getstring = gen_get_function(str) |
|
469 | 49 |
|
519
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
50 |
SECOND = 1000000 |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
51 |
MINUTE = 60 * SECOND |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
52 |
HOUR = 60 * MINUTE |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
53 |
DAY = 24 * HOUR |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
54 |
IEC_TIME_MODEL = re.compile("(?:T|TIME)#(-)?(?:(%(float)s)D_?)?(?:(%(float)s)H_?)?(?:(%(float)s)M_?)?(?:(%(float)s)S_?)?(?:(%(float)s)MS)?" % {"float": "[0-9]+(?:\.[0-9]+)?"}) |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
55 |
|
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
56 |
def gettime(v): |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
57 |
result = IEC_TIME_MODEL.match(v.upper()) |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
58 |
if result is not None: |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
59 |
negative, days, hours, minutes, seconds, milliseconds = result.groups() |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
60 |
microseconds = 0 |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
61 |
not_null = False |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
62 |
for value, factor in [(days, DAY), |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
63 |
(hours, HOUR), |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
64 |
(minutes, MINUTE), |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
65 |
(seconds, SECOND), |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
66 |
(milliseconds, 1000)]: |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
67 |
if value is not None: |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
68 |
microseconds += float(value) * factor |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
69 |
not_null = True |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
70 |
if not not_null: |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
71 |
return None |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
72 |
if negative is not None: |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
73 |
microseconds = -microseconds |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
74 |
return datetime.timedelta(microseconds=microseconds) |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
75 |
|
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
76 |
else: |
516 | 77 |
return None |
78 |
||
79 |
||
479
2fab0eefa66e
Adding support for getting current value in ForceVariableDialog default value and adding variable in DebugVariablePanel when forced
laurent
parents:
476
diff
changeset
|
80 |
GetTypeValue = {"BOOL": lambda x: {"TRUE": True, "FALSE": False}.get(x.upper(), None), |
475
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
81 |
"SINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
82 |
"INT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
83 |
"DINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
84 |
"LINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
85 |
"USINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
86 |
"UINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
87 |
"UDINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
88 |
"ULINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
89 |
"BYTE": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
90 |
"WORD": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
91 |
"DWORD": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
92 |
"LWORD": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
93 |
"REAL": getfloat, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
94 |
"LREAL": getfloat, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
95 |
"STRING": getstring, |
516 | 96 |
"WSTRING": getstring, |
97 |
"TIME": gettime} |
|
469 | 98 |
|
99 |
||
100 |
class ForceVariableDialog(wx.TextEntryDialog): |
|
101 |
||
102 |
if wx.VERSION < (2, 6, 0): |
|
103 |
def Bind(self, event, function, id = None): |
|
104 |
if id is not None: |
|
105 |
event(self, id, function) |
|
106 |
else: |
|
107 |
event(self, function) |
|
108 |
||
479
2fab0eefa66e
Adding support for getting current value in ForceVariableDialog default value and adding variable in DebugVariablePanel when forced
laurent
parents:
476
diff
changeset
|
109 |
def __init__(self, parent, iec_type, defaultValue=""): |
469 | 110 |
wx.TextEntryDialog.__init__(self, parent, message = _("Forcing Variable Value"), |
479
2fab0eefa66e
Adding support for getting current value in ForceVariableDialog default value and adding variable in DebugVariablePanel when forced
laurent
parents:
476
diff
changeset
|
111 |
caption = _("Please enter value for a \"%s\" variable:"%iec_type), defaultValue = defaultValue, |
469 | 112 |
style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition) |
113 |
||
114 |
self.IEC_Type = iec_type |
|
115 |
if wx.VERSION >= (2, 8, 0): |
|
116 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton().GetId()) |
|
117 |
elif wx.VERSION >= (2, 6, 0): |
|
118 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId()) |
|
119 |
else: |
|
120 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) |
|
121 |
||
122 |
def OnOK(self, event): |
|
123 |
value = self.GetSizer().GetItem(1).GetWindow().GetValue() |
|
124 |
if value == "": |
|
125 |
message = wx.MessageDialog(self, _("You must type a value!"), _("Error"), wx.OK|wx.ICON_ERROR) |
|
126 |
message.ShowModal() |
|
127 |
message.Destroy() |
|
475
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
128 |
elif GetTypeValue[self.IEC_Type](value) is None: |
469 | 129 |
message = wx.MessageDialog(self, _("Invalid value \"%s\" for \"%s\" variable!")%(value, self.IEC_Type), _("Error"), wx.OK|wx.ICON_ERROR) |
130 |
message.ShowModal() |
|
131 |
message.Destroy() |
|
132 |
else: |
|
133 |
self.EndModal(wx.ID_OK) |
|
134 |
||
135 |
def GetValue(self): |
|
475
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
136 |
return GetTypeValue[self.IEC_Type](self.GetSizer().GetItem(1).GetWindow().GetValue()) |