Bug with array dimensions edition fixed
authorlbessard
Tue, 22 Apr 2008 10:25:24 +0200
changeset 207 b1144bb36605
parent 206 f7c85a5939dc
child 208 c70aefcadf66
Bug with array dimensions edition fixed
Adding support for plain IEC 61131-3 array dimensions definition
DataTypeEditor.py
PLCControler.py
PLCGenerator.py
--- a/DataTypeEditor.py	Wed Apr 16 10:26:33 2008 +0200
+++ b/DataTypeEditor.py	Tue Apr 22 10:25:24 2008 +0200
@@ -27,6 +27,10 @@
 import wx.gizmos
 from plcopen.structures import GetDataTypeRange, IEC_KEYWORDS
 
+import re
+
+DIMENSION_MODEL = re.compile("([0-9]+)\.\.([0-9]+)$")
+
 if wx.VERSION >= (2, 8, 0):
     import wx.aui
 
@@ -393,7 +397,7 @@
                 self.EnumeratedInitialValue.SetStringSelection(type_infos["initial"])
             elif type_infos["type"] == "Array":
                 self.ArrayBaseType.SetStringSelection(type_infos["base_type"])
-                self.ArrayDimensions.SetStrings(type_infos["dimensions"])
+                self.ArrayDimensions.SetStrings(map(lambda x : "..".join(map(str, x)), type_infos["dimensions"]))
                 self.ArrayInitialValue.SetValue(type_infos["initial"])
             self.RefreshDisplayedInfos()
         self.Initializing = False
@@ -495,7 +499,23 @@
             infos["initial"] = self.EnumeratedInitialValue.GetStringSelection()
         elif selected == "Array":
             infos["base_type"] = self.ArrayBaseType.GetStringSelection()
-            infos["dimensions"] = self.ArrayDimensions.GetStrings()
+            infos["dimensions"] = []
+            for dimensions in self.ArrayDimensions.GetStrings():
+                result = DIMENSION_MODEL.match(dimensions)
+                if result is None:
+                    message = wx.MessageDialog(self, "\"%s\" value isn't a valid array dimension!"%dimensions, "Error", wx.OK|wx.ICON_ERROR)
+                    message.ShowModal()
+                    message.Destroy()
+                    self.RefreshView()
+                    return
+                bounds = result.groups()
+                if bounds[0] >= bounds[1]:
+                    message = wx.MessageDialog(self, "\"%s\" value isn't a valid array dimension!\nRight value must be greater than left value."%dimensions, "Error", wx.OK|wx.ICON_ERROR)
+                    message.ShowModal()
+                    message.Destroy()
+                    self.RefreshView()
+                    return
+                infos["dimensions"].append(map(int, bounds))
             infos["initial"] = self.ArrayInitialValue.GetValue()
         self.Controler.SetDataTypeInfos(self.TagName, infos)
         self.ParentWindow.RefreshTitle()
--- a/PLCControler.py	Wed Apr 16 10:26:33 2008 +0200
+++ b/PLCControler.py	Tue Apr 22 10:25:24 2008 +0200
@@ -1227,7 +1227,7 @@
                 infos["type"] = "Array"
                 infos["dimensions"] = []
                 for dimension in basetype_content["value"].getdimension():
-                    infos["dimensions"].append(str(dimension.getupper()))
+                    infos["dimensions"].append((dimension.getlower(), dimension.getupper()))
                 base_type = basetype_content["value"].baseType.getcontent()
                 if base_type["value"] is None:
                     infos["base_type"] = base_type["name"]
@@ -1284,11 +1284,14 @@
                 datatype.baseType.setcontent({"name" : "enum", "value" : enumerated})
             elif infos["type"] == "Array":
                 array = plcopen.derivedTypes_array()
-                for dimension in infos["dimensions"]:
+                for i, dimension in enumerate(infos["dimensions"]):
                     dimension_range = plcopen.rangeSigned()
-                    dimension_range.setlower(1)
-                    dimension_range.setupper(int(dimension))
-                    array.appenddimension(dimension_range)
+                    dimension_range.setlower(dimension[0])
+                    dimension_range.setupper(dimension[1])
+                    if i == 0:
+                        array.setdimension([dimension_range])
+                    else:
+                        array.appenddimension(dimension_range)
                 if infos["base_type"] in self.GetBaseTypes():
                     if infos["base_type"] == "STRING":
                         array.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
--- a/PLCGenerator.py	Wed Apr 16 10:26:33 2008 +0200
+++ b/PLCGenerator.py	Tue Apr 22 10:25:24 2008 +0200
@@ -100,7 +100,7 @@
                 GenerateDataType(basetype_name)
             dimensions = []
             for dimension in basetype_content["value"].getdimension():
-                dimensions.append("0..%d"%(dimension.getupper() - 1))
+                dimensions.append("%d..%d"%(dimension.getlower(), dimension.getupper()))
             datatype_def += " ARRAY [%s] OF %s"%(",".join(dimensions), basetype_name)
         else:
             datatype_def += " %s"%basetype_content["name"]