PLCControler.py
changeset 1291 42ea51d083ce
parent 1290 13ee5f4ab612
child 1293 40117d02601b
equal deleted inserted replaced
1290:13ee5f4ab612 1291:42ea51d083ce
    64                   ITEM_VAR_INPUT,
    64                   ITEM_VAR_INPUT,
    65                   ITEM_VAR_OUTPUT,
    65                   ITEM_VAR_OUTPUT,
    66                   ITEM_VAR_INOUT
    66                   ITEM_VAR_INOUT
    67                  ] = range(17, 24)
    67                  ] = range(17, 24)
    68 
    68 
    69 VAR_CLASS_INFOS = dict(
    69 VAR_CLASS_INFOS = {
    70     [(name, (PLCOpenParser.GetElementClass(classname, "interface"), symbol))
    70     "Local":    ("localVars",    ITEM_VAR_LOCAL),
    71      for name, classname, symbol in 
    71     "Global":   ("globalVars",   ITEM_VAR_GLOBAL),
    72      [("Local",    "localVars",    ITEM_VAR_LOCAL),
    72     "External": ("externalVars", ITEM_VAR_EXTERNAL),
    73       ("Global",   "globalVars",   ITEM_VAR_GLOBAL),
    73     "Temp":     ("tempVars",     ITEM_VAR_TEMP),
    74       ("External", "externalVars", ITEM_VAR_EXTERNAL),
    74     "Input":    ("inputVars",    ITEM_VAR_INPUT),
    75       ("Temp",     "tempVars",     ITEM_VAR_TEMP),
    75     "Output":   ("outputVars",   ITEM_VAR_OUTPUT),
    76       ("Input",    "inputVars",    ITEM_VAR_INPUT),
    76     "InOut":    ("inOutVars",    ITEM_VAR_INOUT)}
    77       ("Output",   "outputVars",   ITEM_VAR_OUTPUT),
       
    78       ("InOut",    "inOutVars",    ITEM_VAR_INOUT)]])
       
    79 
    77 
    80 POU_TYPES = {"program": ITEM_PROGRAM,
    78 POU_TYPES = {"program": ITEM_PROGRAM,
    81              "functionBlock": ITEM_FUNCTIONBLOCK,
    79              "functionBlock": ITEM_FUNCTIONBLOCK,
    82              "function": ITEM_FUNCTION,
    80              "function": ITEM_FUNCTION,
    83             }
    81             }
   239         self.Project = PLCOpenParser.CreateRoot()
   237         self.Project = PLCOpenParser.CreateRoot()
   240         properties["creationDateTime"] = datetime.datetime(*localtime()[:6])
   238         properties["creationDateTime"] = datetime.datetime(*localtime()[:6])
   241         self.Project.setfileHeader(properties)
   239         self.Project.setfileHeader(properties)
   242         self.Project.setcontentHeader(properties)
   240         self.Project.setcontentHeader(properties)
   243         self.SetFilePath("")
   241         self.SetFilePath("")
       
   242         
       
   243         ## To remove when project buffering ready
       
   244         self.ProjectBufferEnabled = False
       
   245         
   244         # Initialize the project buffer
   246         # Initialize the project buffer
   245         self.CreateProjectBuffer(False)
   247         self.CreateProjectBuffer(False)
   246         self.ProgramChunks = []
   248         self.ProgramChunks = []
   247         self.ProgramOffset = 0
   249         self.ProgramOffset = 0
   248         self.NextCompiledProject = self.Copy(self.Project)
   250         self.NextCompiledProject = self.Project #self.Copy(self.Project)
   249         self.CurrentCompiledProject = None
   251         self.CurrentCompiledProject = None
   250         self.Buffering = False
   252         self.Buffering = False
   251     
   253     
   252     # Return project data type names
   254     # Return project data type names
   253     def GetProjectDataTypeNames(self, debug = False):
   255     def GetProjectDataTypeNames(self, debug = False):
   390             return infos
   392             return infos
   391         return None
   393         return None
   392 
   394 
   393     def GetPouVariableInfos(self, project, variable, var_class, debug=False):
   395     def GetPouVariableInfos(self, project, variable, var_class, debug=False):
   394         vartype_content = variable.gettype().getcontent()
   396         vartype_content = variable.gettype().getcontent()
   395         if vartype_content["name"] == "derived":
   397         vartype_content_type = vartype_content.getLocalTag()
   396             var_type = vartype_content["value"].getname()
   398         if vartype_content_type == "derived":
       
   399             var_type = vartype_content.getname()
   397             pou_type = None
   400             pou_type = None
   398             pou = project.getpou(var_type)
   401             pou = project.getpou(var_type)
   399             if pou is not None:
   402             if pou is not None:
   400                 pou_type = pou.getpouType()
   403                 pou_type = pou.getpouType()
   401             edit = debug = pou_type is not None
   404             edit = debug = pou_type is not None
   419                 return {"name": variable.getname(), 
   422                 return {"name": variable.getname(), 
   420                         "type": var_type, 
   423                         "type": var_type, 
   421                         "class": var_class,
   424                         "class": var_class,
   422                         "edit": False,
   425                         "edit": False,
   423                         "debug": False}
   426                         "debug": False}
   424         elif vartype_content["name"] in ["string", "wstring"]:
   427         elif vartype_content_type in ["string", "wstring"]:
   425             return {"name": variable.getname(), 
   428             return {"name": variable.getname(), 
   426                     "type": vartype_content["name"].upper(), 
   429                     "type": vartype_content_type.upper(), 
   427                     "class": var_class,
   430                     "class": var_class,
   428                     "edit": False,
   431                     "edit": False,
   429                     "debug": True}
   432                     "debug": True}
   430         else:
   433         else:
   431             return {"name": variable.getname(),
   434             return {"name": variable.getname(),
   432                     "type": vartype_content["name"], 
   435                     "type": vartype_content_type, 
   433                     "class": var_class,
   436                     "class": var_class,
   434                     "edit": False,
   437                     "edit": False,
   435                     "debug": True}
   438                     "debug": True}
   436         return None
   439         return None
   437 
   440 
   556     def RecursiveSearchPouInstances(self, project, pou_type, parent_path, varlists, debug = False):
   559     def RecursiveSearchPouInstances(self, project, pou_type, parent_path, varlists, debug = False):
   557         instances = []
   560         instances = []
   558         for varlist in varlists:
   561         for varlist in varlists:
   559             for variable in varlist.getvariable():
   562             for variable in varlist.getvariable():
   560                 vartype_content = variable.gettype().getcontent()
   563                 vartype_content = variable.gettype().getcontent()
   561                 if vartype_content["name"] == "derived":
   564                 if vartype_content.getLocalTag() == "derived":
   562                     var_path = "%s.%s" % (parent_path, variable.getname())
   565                     var_path = "%s.%s" % (parent_path, variable.getname())
   563                     var_type = vartype_content["value"].getname()
   566                     var_type = vartype_content.getname()
   564                     if var_type == pou_type:
   567                     if var_type == pou_type:
   565                         instances.append(var_path)
   568                         instances.append(var_path)
   566                     else:
   569                     else:
   567                         pou = project.getpou(var_type)
   570                         pou = project.getpou(var_type)
   568                         if pou is not None and project.ElementIsUsedBy(pou_type, var_type):
   571                         if pou is not None and project.ElementIsUsedBy(pou_type, var_type):
   625             
   628             
   626             for varlist_type, varlist in pou.getvars():
   629             for varlist_type, varlist in pou.getvars():
   627                 for variable in varlist.getvariable():
   630                 for variable in varlist.getvariable():
   628                     if variable.getname() == parts[0]:
   631                     if variable.getname() == parts[0]:
   629                         vartype_content = variable.gettype().getcontent()
   632                         vartype_content = variable.gettype().getcontent()
   630                         if vartype_content["name"] == "derived":
   633                         if vartype_content.getLocalTag() == "derived":
   631                             return self.RecursiveGetPouInstanceTagName(
   634                             return self.RecursiveGetPouInstanceTagName(
   632                                             project, 
   635                                             project, 
   633                                             vartype_content["value"].getname(),
   636                                             vartype_content["value"].getname(),
   634                                             parts[1:], debug)
   637                                             parts[1:], debug)
   635             
   638             
   656     def GetGlobalInstanceTagName(self, project, element, parts, debug = False):
   659     def GetGlobalInstanceTagName(self, project, element, parts, debug = False):
   657         for varlist in element.getglobalVars():
   660         for varlist in element.getglobalVars():
   658             for variable in varlist.getvariable():
   661             for variable in varlist.getvariable():
   659                 if variable.getname() == parts[0]:
   662                 if variable.getname() == parts[0]:
   660                     vartype_content = variable.gettype().getcontent()
   663                     vartype_content = variable.gettype().getcontent()
   661                     if vartype_content["name"] == "derived":                        
   664                     if vartype_content.getLocalTag() == "derived":                        
   662                         if len(parts) == 1:
   665                         if len(parts) == 1:
   663                             return self.ComputePouName(
   666                             return self.ComputePouName(
   664                                         vartype_content["value"].getname())
   667                                         vartype_content.getname())
   665                         else:
   668                         else:
   666                             return self.RecursiveGetPouInstanceTagName(
   669                             return self.RecursiveGetPouInstanceTagName(
   667                                         project, 
   670                                         project, 
   668                                         vartype_content["value"].getname(),
   671                                         vartype_content.getname(),
   669                                         parts[1:], debug)
   672                                         parts[1:], debug)
   670         return None
   673         return None
   671     
   674     
   672     def GetPouInstanceTagName(self, instance_path, debug = False):
   675     def GetPouInstanceTagName(self, instance_path, debug = False):
   673         project = self.GetProject(debug)
   676         project = self.GetProject(debug)
  1170                          var["Class"] == "Global")
  1173                          var["Class"] == "Global")
  1171             if current_type != next_type:
  1174             if current_type != next_type:
  1172                 current_type = next_type
  1175                 current_type = next_type
  1173                 infos = VAR_CLASS_INFOS.get(var["Class"], None)
  1176                 infos = VAR_CLASS_INFOS.get(var["Class"], None)
  1174                 if infos is not None:
  1177                 if infos is not None:
  1175                     current_varlist = infos[0]()
  1178                     current_varlist = PLCOpenParser.CreateElement(infos[0], "interface")
  1176                 else:
  1179                 else:
  1177                     current_varlist = plcopen.varList()
  1180                     current_varlist = PLCOpenParser.CreateElement("varList")
  1178                 varlist_list.append((var["Class"], current_varlist))
  1181                 varlist_list.append((var["Class"], current_varlist))
  1179                 if var["Option"] == "Constant":
  1182                 if var["Option"] == "Constant":
  1180                     current_varlist.setconstant(True)
  1183                     current_varlist.setconstant(True)
  1181                 elif var["Option"] == "Retain":
  1184                 elif var["Option"] == "Retain":
  1182                     current_varlist.setretain(True)
  1185                     current_varlist.setretain(True)
  1183                 elif var["Option"] == "Non-Retain":
  1186                 elif var["Option"] == "Non-Retain":
  1184                     current_varlist.setnonretain(True)
  1187                     current_varlist.setnonretain(True)
  1185             # Create variable and change its properties
  1188             # Create variable and change its properties
  1186             tempvar = plcopen.varListPlain_variable()
  1189             tempvar = PLCOpenParser.CreateElement("variable", "varListPlain")
  1187             tempvar.setname(var["Name"])
  1190             tempvar.setname(var["Name"])
  1188             
  1191             
  1189             var_type = plcopen.dataType()
  1192             var_type = PLCOpenParser.CreateElement("type", "variable")
  1190             if isinstance(var["Type"], TupleType):
  1193             if isinstance(var["Type"], TupleType):
  1191                 if var["Type"][0] == "array":
  1194                 if var["Type"][0] == "array":
  1192                     array_type, base_type_name, dimensions = var["Type"]
  1195                     array_type, base_type_name, dimensions = var["Type"]
  1193                     array = plcopen.derivedTypes_array()
  1196                     array = PLCOpenParser.CreateElement("array", "dataType")
  1194                     for i, dimension in enumerate(dimensions):
  1197                     for i, dimension in enumerate(dimensions):
  1195                         dimension_range = plcopen.rangeSigned()
  1198                         dimension_range = plcopen.rangeSigned()
  1196                         dimension_range.setlower(dimension[0])
  1199                         dimension_range.setlower(dimension[0])
  1197                         dimension_range.setupper(dimension[1])
  1200                         dimension_range.setupper(dimension[1])
  1198                         if i == 0:
  1201                         if i == 0:
  1199                             array.setdimension([dimension_range])
  1202                             array.setdimension([dimension_range])
  1200                         else:
  1203                         else:
  1201                             array.appenddimension(dimension_range)
  1204                             array.appenddimension(dimension_range)
  1202                     if base_type_name in self.GetBaseTypes():
  1205                     if base_type_name in self.GetBaseTypes():
  1203                         if base_type_name == "STRING":
  1206                         array.baseType.setcontent(PLCOpenParser.CreateElement(
  1204                             array.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
  1207                             base_type_name.lower()
  1205                         elif base_type_name == "WSTRING":
  1208                             if base_type_name in ["STRING", "WSTRING"]
  1206                             array.baseType.setcontent({"name" : "wstring", "value" : plcopen.wstring()})
  1209                             else base_type_name, "dataType"))
  1207                         else:
       
  1208                             array.baseType.setcontent({"name" : base_type_name, "value" : None})
       
  1209                     else:
  1210                     else:
  1210                         derived_datatype = plcopen.derivedTypes_derived()
  1211                         derived_datatype = PLCOpenParser.CreateElement("derived", "dataType")
  1211                         derived_datatype.setname(base_type_name)
  1212                         derived_datatype.setname(base_type_name)
  1212                         array.baseType.setcontent({"name" : "derived", "value" : derived_datatype})
  1213                         array.baseType.setcontent(derived_datatype)
  1213                     var_type.setcontent({"name" : "array", "value" : array})
  1214                     var_type.setcontent(array)
  1214             elif var["Type"] in self.GetBaseTypes():
  1215             elif var["Type"] in self.GetBaseTypes():
  1215                 if var["Type"] == "STRING":
  1216                 var_type.setcontent(PLCOpenParser.CreateElement(
  1216                     var_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
  1217                     var["Type"].lower()
  1217                 elif var["Type"] == "WSTRING":
  1218                     if var["Type"] in ["STRING", "WSTRING"]
  1218                     var_type.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()})
  1219                     else var["Type"], "dataType"))
  1219                 else:
       
  1220                     var_type.setcontent({"name" : var["Type"], "value" : None})
       
  1221             else:
  1220             else:
  1222                 derived_type = plcopen.derivedTypes_derived()
  1221                 derived_type = PLCOpenParser.CreateElement("derived", "dataType")
  1223                 derived_type.setname(var["Type"])
  1222                 derived_type.setname(var["Type"])
  1224                 var_type.setcontent({"name" : "derived", "value" : derived_type})
  1223                 var_type.setcontent(derived_type)
  1225             tempvar.settype(var_type)
  1224             tempvar.settype(var_type)
  1226 
  1225 
  1227             if var["Initial Value"] != "":
  1226             if var["Initial Value"] != "":
  1228                 value = plcopen.value()
  1227                 value = PLCOpenParser.CreateElement("initialValue", "variable")
  1229                 value.setvalue(var["Initial Value"])
  1228                 value.setvalue(var["Initial Value"])
  1230                 tempvar.setinitialValue(value)
  1229                 tempvar.setinitialValue(value)
  1231             if var["Location"] != "":
  1230             if var["Location"] != "":
  1232                 tempvar.setaddress(var["Location"])
  1231                 tempvar.setaddress(var["Location"])
  1233             else:
  1232             else:
  1234                 tempvar.setaddress(None)
  1233                 tempvar.setaddress(None)
  1235             if var['Documentation'] != "":
  1234             if var['Documentation'] != "":
  1236                 ft = plcopen.formattedText()
  1235                 ft = plcopen.formattedText()
  1237                 ft.settext(var['Documentation'])
  1236                 ft.setanyText(var['Documentation'])
  1238                 tempvar.setdocumentation(ft)
  1237                 tempvar.setdocumentation(ft)
  1239 
  1238 
  1240             # Add variable to varList
  1239             # Add variable to varList
  1241             current_varlist.appendvariable(tempvar)
  1240             current_varlist.appendvariable(tempvar)
  1242         return varlist_list
  1241         return varlist_list
  1248         '''
  1247         '''
  1249 
  1248 
  1250         tempvar = {"Name": var.getname()}
  1249         tempvar = {"Name": var.getname()}
  1251 
  1250 
  1252         vartype_content = var.gettype().getcontent()
  1251         vartype_content = var.gettype().getcontent()
  1253         if vartype_content["name"] == "derived":
  1252         vartype_content_type = vartype_content.getLocalTag()
  1254             tempvar["Type"] = vartype_content["value"].getname()
  1253         if vartype_content_type == "derived":
  1255         elif vartype_content["name"] == "array":
  1254             tempvar["Type"] = vartype_content.getname()
       
  1255         elif vartype_content_type == "array":
  1256             dimensions = []
  1256             dimensions = []
  1257             for dimension in vartype_content["value"].getdimension():
  1257             for dimension in vartype_content.getdimension():
  1258                 dimensions.append((dimension.getlower(), dimension.getupper()))
  1258                 dimensions.append((dimension.getlower(), dimension.getupper()))
  1259             base_type = vartype_content["value"].baseType.getcontent()
  1259             base_type = vartype_content.baseType.getcontent()
  1260             if base_type["value"] is None or base_type["name"] in ["string", "wstring"]:
  1260             base_type_type = base_type.getLocalTag()
  1261                 base_type_name = base_type["name"].upper()
  1261             if base_type is None or base_type_type in ["string", "wstring"]:
       
  1262                 base_type_name = base_type_type.upper()
  1262             else:
  1263             else:
  1263                 base_type_name = base_type["value"].getname()
  1264                 base_type_name = base_type.getname()
  1264             tempvar["Type"] = ("array", base_type_name, dimensions)
  1265             tempvar["Type"] = ("array", base_type_name, dimensions)
  1265         elif vartype_content["name"] in ["string", "wstring"]:
       
  1266             tempvar["Type"] = vartype_content["name"].upper()
       
  1267         else:
  1266         else:
  1268             tempvar["Type"] = vartype_content["name"]
  1267             tempvar["Type"] = vartype_content_type.upper()
  1269 
  1268         
  1270         tempvar["Edit"] = True
  1269         tempvar["Edit"] = True
  1271 
  1270 
  1272         initial = var.getinitialValue()
  1271         initial = var.getinitialValue()
  1273         if initial:
  1272         if initial is not None:
  1274             tempvar["Initial Value"] = initial.getvalue()
  1273             tempvar["Initial Value"] = initial.getvalue()
  1275         else:
  1274         else:
  1276             tempvar["Initial Value"] = ""
  1275             tempvar["Initial Value"] = ""
  1277 
  1276 
  1278         address = var.getaddress()
  1277         address = var.getaddress()
  1289             tempvar["Option"] = "Non-Retain"
  1288             tempvar["Option"] = "Non-Retain"
  1290         else:
  1289         else:
  1291             tempvar["Option"] = ""
  1290             tempvar["Option"] = ""
  1292 
  1291 
  1293         doc = var.getdocumentation()
  1292         doc = var.getdocumentation()
  1294         if doc:
  1293         if doc is not None:
  1295             tempvar["Documentation"] = doc.gettext()
  1294             tempvar["Documentation"] = doc.getanyText()
  1296         else:
  1295         else:
  1297             tempvar["Documentation"] = ""
  1296             tempvar["Documentation"] = ""
  1298 
  1297 
  1299         return tempvar
  1298         return tempvar
  1300     
  1299     
  1313         if self.Project is not None:
  1312         if self.Project is not None:
  1314             # Found the configuration corresponding to name
  1313             # Found the configuration corresponding to name
  1315             configuration = self.Project.getconfiguration(name)
  1314             configuration = self.Project.getconfiguration(name)
  1316             if configuration is not None:
  1315             if configuration is not None:
  1317                 # Set configuration global vars
  1316                 # Set configuration global vars
  1318                 configuration.setglobalVars([])
  1317                 configuration.setglobalVars([
  1319                 for vartype, varlist in self.ExtractVarLists(vars):
  1318                     varlist for vartype, varlist
  1320                     configuration.globalVars.append(varlist)
  1319                     in self.ExtractVarLists(vars)])
  1321     
  1320     
  1322     # Return the configuration globalvars
  1321     # Return the configuration globalvars
  1323     def GetConfigurationGlobalVars(self, name, debug = False):
  1322     def GetConfigurationGlobalVars(self, name, debug = False):
  1324         vars = []
  1323         vars = []
  1325         project = self.GetProject(debug)
  1324         project = self.GetProject(debug)
  1354         if self.Project is not None:
  1353         if self.Project is not None:
  1355             # Found the resource corresponding to name
  1354             # Found the resource corresponding to name
  1356             resource = self.Project.getconfigurationResource(config_name, name)
  1355             resource = self.Project.getconfigurationResource(config_name, name)
  1357             # Set resource global vars
  1356             # Set resource global vars
  1358             if resource is not None:
  1357             if resource is not None:
  1359                 resource.setglobalVars([])
  1358                 resource.setglobalVars([
  1360                 for vartype, varlist in self.ExtractVarLists(vars):
  1359                     varlist for vartype, varlist
  1361                     resource.globalVars.append(varlist)
  1360                     in self.ExtractVarLists(vars)])
  1362     
  1361     
  1363     # Return the resource globalvars
  1362     # Return the resource globalvars
  1364     def GetConfigurationResourceGlobalVars(self, config_name, name, debug = False):
  1363     def GetConfigurationResourceGlobalVars(self, config_name, name, debug = False):
  1365         vars = []
  1364         vars = []
  1366         project = self.GetProject(debug)
  1365         project = self.GetProject(debug)
  1415             if datatype is None:
  1414             if datatype is None:
  1416                 datatype = self.GetConfNodeDataType(typename)
  1415                 datatype = self.GetConfNodeDataType(typename)
  1417             if datatype is not None:
  1416             if datatype is not None:
  1418                 tree = []
  1417                 tree = []
  1419                 basetype_content = datatype.baseType.getcontent()
  1418                 basetype_content = datatype.baseType.getcontent()
  1420                 if basetype_content["name"] == "derived":
  1419                 basetype_content_type = basetype_content.getLocalTag()
  1421                     return self.GenerateVarTree(basetype_content["value"].getname())
  1420                 if basetype_content_type == "derived":
  1422                 elif basetype_content["name"] == "array":
  1421                     return self.GenerateVarTree(basetype_content.getname())
       
  1422                 elif basetype_content_type == "array":
  1423                     dimensions = []
  1423                     dimensions = []
  1424                     base_type = basetype_content["value"].baseType.getcontent()
  1424                     base_type = basetype_content.baseType.getcontent()
  1425                     if base_type["name"] == "derived":
  1425                     if base_type.getLocalTag() == "derived":
  1426                         tree = self.GenerateVarTree(base_type["value"].getname())
  1426                         tree = self.GenerateVarTree(base_type.getname())
  1427                         if len(tree[1]) == 0:
  1427                         if len(tree[1]) == 0:
  1428                             tree = tree[0]
  1428                             tree = tree[0]
  1429                         for dimension in basetype_content["value"].getdimension():
  1429                         for dimension in basetype_content.getdimension():
  1430                             dimensions.append((dimension.getlower(), dimension.getupper()))
  1430                             dimensions.append((dimension.getlower(), dimension.getupper()))
  1431                     return tree, dimensions
  1431                     return tree, dimensions
  1432                 elif basetype_content["name"] == "struct":
  1432                 elif basetype_content_type == "struct":
  1433                     for element in basetype_content["value"].getvariable():
  1433                     for element in basetype_content.getvariable():
  1434                         element_type = element.type.getcontent()
  1434                         element_type = element.type.getcontent()
  1435                         if element_type["name"] == "derived":
  1435                         element_type_type = element_type.getLocalTag()
  1436                             tree.append((element.getname(), element_type["value"].getname(), self.GenerateVarTree(element_type["value"].getname())))
  1436                         if element_type_type == "derived":
       
  1437                             tree.append((element.getname(), element_type.getname(), self.GenerateVarTree(element_type.getname())))
  1437                         else:
  1438                         else:
  1438                             tree.append((element.getname(), element_type["name"], ([], [])))
  1439                             tree.append((element.getname(), element_type_type, ([], [])))
  1439                     return tree, []
  1440                     return tree, []
  1440         return [], []
  1441         return [], []
  1441 
  1442 
  1442     # Return the interface for the given pou
  1443     # Return the interface for the given pou
  1443     def GetPouInterfaceVars(self, pou, debug = False):
  1444     def GetPouInterfaceVars(self, pou, debug = False):
  1451 
  1452 
  1452                     tempvar["Class"] = type
  1453                     tempvar["Class"] = type
  1453                     tempvar["Tree"] = ([], [])
  1454                     tempvar["Tree"] = ([], [])
  1454 
  1455 
  1455                     vartype_content = var.gettype().getcontent()
  1456                     vartype_content = var.gettype().getcontent()
  1456                     if vartype_content["name"] == "derived":
  1457                     if vartype_content.getLocalTag() == "derived":
  1457                         tempvar["Edit"] = not pou.hasblock(tempvar["Name"])
  1458                         tempvar["Edit"] = not pou.hasblock(tempvar["Name"])
  1458                         tempvar["Tree"] = self.GenerateVarTree(tempvar["Type"], debug)
  1459                         tempvar["Tree"] = self.GenerateVarTree(tempvar["Type"], debug)
  1459 
  1460 
  1460                     vars.append(tempvar)
  1461                     vars.append(tempvar)
  1461         return vars
  1462         return vars
  1465         if self.Project is not None:
  1466         if self.Project is not None:
  1466             # Found the pou corresponding to name and add interface if there isn't one yet
  1467             # Found the pou corresponding to name and add interface if there isn't one yet
  1467             pou = self.Project.getpou(name)
  1468             pou = self.Project.getpou(name)
  1468             if pou is not None:
  1469             if pou is not None:
  1469                 if pou.interface is None:
  1470                 if pou.interface is None:
  1470                     pou.interface = plcopen.pou_interface()
  1471                     pou.interface = PLCOpenParser.CreateElement("interface", "pou")
  1471                 # Set Pou interface
  1472                 # Set Pou interface
  1472                 pou.setvars(self.ExtractVarLists(vars))
  1473                 pou.setvars([varlist for varlist_type, varlist in self.ExtractVarLists(vars)])
  1473                 self.Project.RefreshElementUsingTree()
  1474                 self.Project.RefreshElementUsingTree()
  1474                 self.Project.RefreshCustomBlockTypes()
  1475                 self.Project.RefreshCustomBlockTypes()
  1475     
  1476     
  1476     # Replace the return type of the pou given by its name (only for functions)
  1477     # Replace the return type of the pou given by its name (only for functions)
  1477     def SetPouInterfaceReturnType(self, name, type):
  1478     def SetPouInterfaceReturnType(self, name, return_type):
  1478         if self.Project is not None:
  1479         if self.Project is not None:
  1479             pou = self.Project.getpou(name)
  1480             pou = self.Project.getpou(name)
  1480             if pou is not None:
  1481             if pou is not None:
  1481                 if pou.interface is None:
  1482                 if pou.interface is None:
  1482                     pou.interface = plcopen.pou_interface()
  1483                     pou.interface = PLCOpenParser.CreateElement("interface", "pou")
  1483                 # If there isn't any return type yet, add it
  1484                 # If there isn't any return type yet, add it
  1484                 return_type = pou.interface.getreturnType()
  1485                 return_type_obj = pou.interface.getreturnType()
  1485                 if not return_type:
  1486                 if not return_type_obj:
  1486                     return_type = plcopen.dataType()
  1487                     return_type_obj = PLCOpenParser.CreateElement("returnType", "interface")
  1487                     pou.interface.setreturnType(return_type)
  1488                     pou.interface.setreturnType(return_type_obj)
  1488                 # Change return type
  1489                 # Change return type
  1489                 if type in self.GetBaseTypes():
  1490                 if return_type in self.GetBaseTypes():
  1490                     if type == "STRING":
  1491                     return_type_obj.setcontent(PLCOpenParser.CreateElement(
  1491                         return_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
  1492                         return_type.lower()
  1492                     elif type == "WSTRING":
  1493                         if return_type in ["STRING", "WSTRING"]
  1493                         return_type.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()})
  1494                         else return_type, "dataType"))
  1494                     else:
       
  1495                         return_type.setcontent({"name" : type, "value" : None})
       
  1496                 else:
  1495                 else:
  1497                     derived_type = plcopen.derivedTypes_derived()
  1496                     derived_type = PLCOpenParser.CreateElement("derived", "dataType")
  1498                     derived_type.setname(type)
  1497                     derived_type.setname(return_type)
  1499                     return_type.setcontent({"name" : "derived", "value" : derived_type})
  1498                     return_type.setcontent(derived_type)
  1500                 self.Project.RefreshElementUsingTree()
  1499                 self.Project.RefreshElementUsingTree()
  1501                 self.Project.RefreshCustomBlockTypes()
  1500                 self.Project.RefreshCustomBlockTypes()
  1502     
  1501     
  1503     def UpdateProjectUsedPous(self, old_name, new_name):
  1502     def UpdateProjectUsedPous(self, old_name, new_name):
  1504         if self.Project:
  1503         if self.Project:
  1523     def GetPouInterfaceReturnType(self, pou):
  1522     def GetPouInterfaceReturnType(self, pou):
  1524         # Verify that the pou has an interface
  1523         # Verify that the pou has an interface
  1525         if pou.interface is not None:
  1524         if pou.interface is not None:
  1526             # Return the return type if there is one
  1525             # Return the return type if there is one
  1527             return_type = pou.interface.getreturnType()
  1526             return_type = pou.interface.getreturnType()
  1528             if return_type:
  1527             if return_type is not None:
  1529                 returntype_content = return_type.getcontent()
  1528                 returntype_content = return_type.getcontent()
  1530                 if returntype_content["name"] == "derived":
  1529                 returntype_content_type = returntype_content.getLocalTag()
  1531                     return returntype_content["value"].getname()
  1530                 if returntype_content_type == "derived":
  1532                 elif returntype_content["name"] in ["string", "wstring"]:
  1531                     return returntype_content.getname()
  1533                     return returntype_content["name"].upper()
       
  1534                 else:
  1532                 else:
  1535                     return returntype_content["name"]
  1533                     return returntype_content_type.upper()
  1536         return None
  1534         return None
  1537 
  1535 
  1538     # Function that add a new confnode to the confnode list
  1536     # Function that add a new confnode to the confnode list
  1539     def AddConfNodeTypesList(self, typeslist):
  1537     def AddConfNodeTypesList(self, typeslist):
  1540         self.ConfNodeTypes.extend(typeslist)
  1538         self.ConfNodeTypes.extend(typeslist)
  1577         return []
  1575         return []
  1578 
  1576 
  1579     def GetConfigurationExtraVariables(self):
  1577     def GetConfigurationExtraVariables(self):
  1580         global_vars = []
  1578         global_vars = []
  1581         for var_name, var_type, var_initial in self.GetConfNodeGlobalInstances():
  1579         for var_name, var_type, var_initial in self.GetConfNodeGlobalInstances():
  1582             tempvar = plcopen.varListPlain_variable()
  1580             tempvar = PLCOpenParser.CreateElement("variable", "globalVars")
  1583             tempvar.setname(var_name)
  1581             tempvar.setname(var_name)
  1584             
  1582             
  1585             tempvartype = plcopen.dataType()
  1583             tempvartype = PLCOpenParser.CreateElement("dataType", "variable")
  1586             if var_type in self.GetBaseTypes():
  1584             if var_type in self.GetBaseTypes():
  1587                 if var_type == "STRING":
  1585                 tempvar.setcontent(PLCOpenParser.CreateElement(
  1588                     tempvartype.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
  1586                     var_type.lower()
  1589                 elif var_type == "WSTRING":
  1587                     if var_type in ["STRING", "WSTRING"]
  1590                     tempvartype.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()})
  1588                     else var_type, "dataType"))
  1591                 else:
       
  1592                     tempvartype.setcontent({"name" : var_type, "value" : None})
       
  1593             else:
  1589             else:
  1594                 tempderivedtype = plcopen.derivedTypes_derived()
  1590                 tempderivedtype = PLCOpenParser.CreateElement("derived", "dataType")
  1595                 tempderivedtype.setname(var_type)
  1591                 tempderivedtype.setname(var_type)
  1596                 tempvartype.setcontent({"name" : "derived", "value" : tempderivedtype})
  1592                 tempvartype.setcontent(tempderivedtype)
  1597             tempvar.settype(tempvartype)
  1593             tempvar.settype(tempvartype)
  1598             
  1594             
  1599             if var_initial != "":
  1595             if var_initial != "":
  1600                 value = plcopen.value()
  1596                 value = PLCOpenParser.CreateElement("initialValue", "variable")
  1601                 value.setvalue(var_initial)
  1597                 value.setvalue(var_initial)
  1602                 tempvar.setinitialValue(value)
  1598                 tempvar.setinitialValue(value)
  1603             
  1599             
  1604             global_vars.append(tempvar)
  1600             global_vars.append(tempvar)
  1605         return global_vars
  1601         return global_vars
  1764             datatype = project.getdataType(type)
  1760             datatype = project.getdataType(type)
  1765             if datatype is None:
  1761             if datatype is None:
  1766                 datatype = self.GetConfNodeDataType(type)
  1762                 datatype = self.GetConfNodeDataType(type)
  1767             if datatype is not None:
  1763             if datatype is not None:
  1768                 basetype_content = datatype.baseType.getcontent()
  1764                 basetype_content = datatype.baseType.getcontent()
  1769                 return basetype_content["name"] == "enum"
  1765                 return basetype_content.getLocalTag() == "enum"
  1770         return False
  1766         return False
  1771 
  1767 
  1772     def IsNumType(self, type, debug = False):
  1768     def IsNumType(self, type, debug = False):
  1773         return self.IsOfType(type, "ANY_NUM", debug) or\
  1769         return self.IsOfType(type, "ANY_NUM", debug) or\
  1774                self.IsOfType(type, "ANY_BIT", debug)
  1770                self.IsOfType(type, "ANY_BIT", debug)
  1859                 infos = {}
  1855                 infos = {}
  1860                 datatype = project.getdataType(words[1])
  1856                 datatype = project.getdataType(words[1])
  1861                 if datatype is None:
  1857                 if datatype is None:
  1862                     return None
  1858                     return None
  1863                 basetype_content = datatype.baseType.getcontent()
  1859                 basetype_content = datatype.baseType.getcontent()
  1864                 if basetype_content["value"] is None or basetype_content["name"] in ["string", "wstring"]:
  1860                 basetype_content_type = basetype_content.getLocalTag()
  1865                     infos["type"] = "Directly"
  1861                 if basetype_content_type in ["subrangeSigned", "subrangeUnsigned"]:
  1866                     infos["base_type"] = basetype_content["name"].upper()
       
  1867                 elif basetype_content["name"] == "derived":
       
  1868                     infos["type"] = "Directly"
       
  1869                     infos["base_type"] = basetype_content["value"].getname()
       
  1870                 elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]:
       
  1871                     infos["type"] = "Subrange"
  1862                     infos["type"] = "Subrange"
  1872                     infos["min"] = basetype_content["value"].range.getlower()
  1863                     infos["min"] = basetype_content.range.getlower()
  1873                     infos["max"] = basetype_content["value"].range.getupper()
  1864                     infos["max"] = basetype_content.range.getupper()
  1874                     base_type = basetype_content["value"].baseType.getcontent()
  1865                     base_type = basetype_content.baseType.getcontent()
  1875                     if base_type["value"] is None:
  1866                     base_type_type = base_type.getLocalTag()
  1876                         infos["base_type"] = base_type["name"]
  1867                     infos["base_type"] = (base_type.getname()
  1877                     else:
  1868                         if base_type_type == "derived"
  1878                         infos["base_type"] = base_type["value"].getname()
  1869                         else base_type_type)
  1879                 elif basetype_content["name"] == "enum":
  1870                 elif basetype_content_type == "enum":
  1880                     infos["type"] = "Enumerated"
  1871                     infos["type"] = "Enumerated"
  1881                     infos["values"] = []
  1872                     infos["values"] = []
  1882                     for value in basetype_content["value"].values.getvalue():
  1873                     for value in basetype_content.xpath("ppx:values/ppx:value", namespaces=PLCOpenParser.NSMAP):
  1883                         infos["values"].append(value.getname())
  1874                         infos["values"].append(value.getname())
  1884                 elif basetype_content["name"] == "array":
  1875                 elif basetype_content_type == "array":
  1885                     infos["type"] = "Array"
  1876                     infos["type"] = "Array"
  1886                     infos["dimensions"] = []
  1877                     infos["dimensions"] = []
  1887                     for dimension in basetype_content["value"].getdimension():
  1878                     for dimension in basetype_content.getdimension():
  1888                         infos["dimensions"].append((dimension.getlower(), dimension.getupper()))
  1879                         infos["dimensions"].append((dimension.getlower(), dimension.getupper()))
  1889                     base_type = basetype_content["value"].baseType.getcontent()
  1880                     base_type = basetype_content.baseType.getcontent()
  1890                     if base_type["value"] is None or base_type["name"] in ["string", "wstring"]:
  1881                     base_type_type = base_type.getLocalTag()
  1891                         infos["base_type"] = base_type["name"].upper()
  1882                     infos["base_type"] = (base_type.getname()
  1892                     else:
  1883                         if base_type_type == "derived"
  1893                         infos["base_type"] = base_type["value"].getname()
  1884                         else base_type_type.upper())
  1894                 elif basetype_content["name"] == "struct":
  1885                 elif basetype_content_type == "struct":
  1895                     infos["type"] = "Structure"
  1886                     infos["type"] = "Structure"
  1896                     infos["elements"] = []
  1887                     infos["elements"] = []
  1897                     for element in basetype_content["value"].getvariable():
  1888                     for element in basetype_content.getvariable():
  1898                         element_infos = {}
  1889                         element_infos = {}
  1899                         element_infos["Name"] = element.getname()
  1890                         element_infos["Name"] = element.getname()
  1900                         element_type = element.type.getcontent()
  1891                         element_type = element.type.getcontent()
  1901                         if element_type["value"] is None or element_type["name"] in ["string", "wstring"]:
  1892                         element_type_type = element_type.getLocalTag()
  1902                             element_infos["Type"] = element_type["name"].upper()
  1893                         if element_type_type == "array":
  1903                         elif element_type["name"] == "array":
       
  1904                             dimensions = []
  1894                             dimensions = []
  1905                             for dimension in element_type["value"].getdimension():
  1895                             for dimension in element_type.getdimension():
  1906                                 dimensions.append((dimension.getlower(), dimension.getupper()))
  1896                                 dimensions.append((dimension.getlower(), dimension.getupper()))
  1907                             base_type = element_type["value"].baseType.getcontent()
  1897                             base_type = element_type.baseType.getcontent()
  1908                             if base_type["value"] is None or base_type["name"] in ["string", "wstring"]:
  1898                             base_type_type = element_type.getLocalTag()
  1909                                 base_type_name = base_type["name"].upper()
  1899                             element_infos["Type"] = ("array", 
  1910                             else:
  1900                                 base_type.getname()
  1911                                 base_type_name = base_type["value"].getname()
  1901                                 if base_type_type == "derived"
  1912                             element_infos["Type"] = ("array", base_type_name, dimensions)
  1902                                 else base_type_type.upper(), dimensions)
       
  1903                         elif element_type_type == "derived":
       
  1904                             element_infos["Type"] = element_type.getname()
  1913                         else:
  1905                         else:
  1914                             element_infos["Type"] = element_type["value"].getname()
  1906                             element_infos["Type"] = element_type_type.upper()
  1915                         if element.initialValue is not None:
  1907                         if element.initialValue is not None:
  1916                             element_infos["Initial Value"] = str(element.initialValue.getvalue())
  1908                             element_infos["Initial Value"] = str(element.initialValue.getvalue())
  1917                         else:
  1909                         else:
  1918                             element_infos["Initial Value"] = ""
  1910                             element_infos["Initial Value"] = ""
  1919                         infos["elements"].append(element_infos)
  1911                         infos["elements"].append(element_infos)
       
  1912                 else:
       
  1913                     infos["type"] = "Directly"
       
  1914                     infos["base_type"] = (basetype_content.getname()
       
  1915                         if basetype_content_type == "derived"
       
  1916                         else basetype_content_type.upper())
       
  1917                 
  1920                 if datatype.initialValue is not None:
  1918                 if datatype.initialValue is not None:
  1921                     infos["initial"] = str(datatype.initialValue.getvalue())
  1919                     infos["initial"] = str(datatype.initialValue.getvalue())
  1922                 else:
  1920                 else:
  1923                     infos["initial"] = ""
  1921                     infos["initial"] = ""
  1924                 return infos
  1922                 return infos
  1929         words = tagname.split("::")
  1927         words = tagname.split("::")
  1930         if self.Project is not None and words[0] == "D":
  1928         if self.Project is not None and words[0] == "D":
  1931             datatype = self.Project.getdataType(words[1])
  1929             datatype = self.Project.getdataType(words[1])
  1932             if infos["type"] == "Directly":
  1930             if infos["type"] == "Directly":
  1933                 if infos["base_type"] in self.GetBaseTypes():
  1931                 if infos["base_type"] in self.GetBaseTypes():
  1934                     if infos["base_type"] == "STRING":
  1932                     datatype.baseType.setcontent(PLCOpenParser.CreateElement(
  1935                         datatype.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
  1933                         infos["base_type"].lower()
  1936                     elif infos["base_type"] == "WSTRING":
  1934                         if infos["base_type"] in ["STRING", "WSTRING"]
  1937                         datatype.baseType.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()})
  1935                         else infos["base_type"], "dataType"))
  1938                     else:
       
  1939                         datatype.baseType.setcontent({"name" : infos["base_type"], "value" : None})
       
  1940                 else:
  1936                 else:
  1941                     derived_datatype = plcopen.derivedTypes_derived()
  1937                     derived_datatype = PLCOpenParser.CreateElement("derived", "dataType")
  1942                     derived_datatype.setname(infos["base_type"])
  1938                     derived_datatype.setname(infos["base_type"])
  1943                     datatype.baseType.setcontent({"name" : "derived", "value" : derived_datatype})
  1939                     datatype.baseType.setcontent(derived_datatype)
  1944             elif infos["type"] == "Subrange":
  1940             elif infos["type"] == "Subrange":
  1945                 if infos["base_type"] in GetSubTypes("ANY_UINT"):
  1941                 datatype.baseType.setcontent(PLCOpenParser.CreateElement(
  1946                     subrange = plcopen.derivedTypes_subrangeUnsigned()
  1942                     "subrangeUnsigned" 
  1947                     datatype.baseType.setcontent({"name" : "subrangeUnsigned", "value" : subrange})
  1943                     if infos["base_type"] in GetSubTypes("ANY_UINT")
  1948                 else:
  1944                     else "subrangeSigned", "dataType"))
  1949                     subrange = plcopen.derivedTypes_subrangeSigned()
       
  1950                     datatype.baseType.setcontent({"name" : "subrangeSigned", "value" : subrange})
       
  1951                 subrange.range.setlower(infos["min"])
  1945                 subrange.range.setlower(infos["min"])
  1952                 subrange.range.setupper(infos["max"])
  1946                 subrange.range.setupper(infos["max"])
  1953                 if infos["base_type"] in self.GetBaseTypes():
  1947                 if infos["base_type"] in self.GetBaseTypes():
  1954                     subrange.baseType.setcontent({"name" : infos["base_type"], "value" : None})
  1948                     subrange.baseType.setcontent(
       
  1949                         PLCOpenParser.CreateElement(infos["base_type"]))
  1955                 else:
  1950                 else:
  1956                     derived_datatype = plcopen.derivedTypes_derived()
  1951                     derived_datatype = PLCOpenParser.CreateElement("derived", "dataType")
  1957                     derived_datatype.setname(infos["base_type"])
  1952                     derived_datatype.setname(infos["base_type"])
  1958                     subrange.baseType.setcontent({"name" : "derived", "value" : derived_datatype})
  1953                     subrange.baseType.setcontent(derived_datatype)
  1959             elif infos["type"] == "Enumerated":
  1954             elif infos["type"] == "Enumerated":
  1960                 enumerated = plcopen.derivedTypes_enum()
  1955                 enumerated = PLCOpenParser.CreateElement("enum", "dataType")
       
  1956                 values = PLCOpenParser.CreateElement("values", "enum")
       
  1957                 enumerated.setvalues(values)
  1961                 for i, enum_value in enumerate(infos["values"]):
  1958                 for i, enum_value in enumerate(infos["values"]):
  1962                     value = plcopen.values_value()
  1959                     value = PLCOpenParser.CreateElement("value", "values")
  1963                     value.setname(enum_value)
  1960                     value.setname(enum_value)
  1964                     if i == 0:
  1961                     if i == 0:
  1965                         enumerated.values.setvalue([value])
  1962                         values.setvalue([value])
  1966                     else:
  1963                     else:
  1967                         enumerated.values.appendvalue(value)
  1964                         values.appendvalue(value)
  1968                 datatype.baseType.setcontent({"name" : "enum", "value" : enumerated})
  1965                 datatype.baseType.setcontent(enumerated)
  1969             elif infos["type"] == "Array":
  1966             elif infos["type"] == "Array":
  1970                 array = plcopen.derivedTypes_array()
  1967                 array = PLCOpenParser.CreateElement("array", "dataType")
  1971                 for i, dimension in enumerate(infos["dimensions"]):
  1968                 for i, dimension in enumerate(infos["dimensions"]):
  1972                     dimension_range = plcopen.rangeSigned()
  1969                     dimension_range = PLCOpenParser.CreateElement("dimension", "array")
  1973                     dimension_range.setlower(dimension[0])
  1970                     dimension_range.setlower(dimension[0])
  1974                     dimension_range.setupper(dimension[1])
  1971                     dimension_range.setupper(dimension[1])
  1975                     if i == 0:
  1972                     if i == 0:
  1976                         array.setdimension([dimension_range])
  1973                         array.setdimension([dimension_range])
  1977                     else:
  1974                     else:
  1978                         array.appenddimension(dimension_range)
  1975                         array.appenddimension(dimension_range)
  1979                 if infos["base_type"] in self.GetBaseTypes():
  1976                 if infos["base_type"] in self.GetBaseTypes():
  1980                     if infos["base_type"] == "STRING":
  1977                     array.baseType.setcontent(PLCOpenParser.CreateElement(
  1981                         array.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
  1978                         infos["base_type"].lower()
  1982                     elif infos["base_type"] == "WSTRING":
  1979                         if infos["base_type"] in ["STRING", "WSTRING"]
  1983                         array.baseType.setcontent({"name" : "wstring", "value" : plcopen.wstring()})
  1980                         else infos["base_type"], "dataType"))
  1984                     else:
       
  1985                         array.baseType.setcontent({"name" : infos["base_type"], "value" : None})
       
  1986                 else:
  1981                 else:
  1987                     derived_datatype = plcopen.derivedTypes_derived()
  1982                     derived_datatype = PLCOpenParser.CreateElement("derived", "dataType")
  1988                     derived_datatype.setname(infos["base_type"])
  1983                     derived_datatype.setname(infos["base_type"])
  1989                     array.baseType.setcontent({"name" : "derived", "value" : derived_datatype})
  1984                     array.baseType.setcontent(derived_datatype)
  1990                 datatype.baseType.setcontent({"name" : "array", "value" : array})
  1985                 datatype.baseType.setcontent(array)
  1991             elif infos["type"] == "Structure":
  1986             elif infos["type"] == "Structure":
  1992                 struct = plcopen.varListPlain()
  1987                 struct = PLCOpenParser.CreateElement("struct", "dataType")
  1993                 for i, element_infos in enumerate(infos["elements"]):
  1988                 for i, element_infos in enumerate(infos["elements"]):
  1994                     element = plcopen.varListPlain_variable()
  1989                     element = PLCOpenParser.CreateElement("variable", "struct")
  1995                     element.setname(element_infos["Name"])
  1990                     element.setname(element_infos["Name"])
       
  1991                     element_type = PLCOpenParser.CreateElement("type", "variable")
  1996                     if isinstance(element_infos["Type"], TupleType):
  1992                     if isinstance(element_infos["Type"], TupleType):
  1997                         if element_infos["Type"][0] == "array":
  1993                         if element_infos["Type"][0] == "array":
  1998                             array_type, base_type_name, dimensions = element_infos["Type"]
  1994                             array_type, base_type_name, dimensions = element_infos["Type"]
  1999                             array = plcopen.derivedTypes_array()
  1995                             array = PLCOpenParser.CreateElement("array", "dataType")
  2000                             for j, dimension in enumerate(dimensions):
  1996                             for j, dimension in enumerate(dimensions):
  2001                                 dimension_range = plcopen.rangeSigned()
  1997                                 dimension_range = PLCOpenParser.CreateElement("dimension", "array")
  2002                                 dimension_range.setlower(dimension[0])
  1998                                 dimension_range.setlower(dimension[0])
  2003                                 dimension_range.setupper(dimension[1])
  1999                                 dimension_range.setupper(dimension[1])
  2004                                 if j == 0:
  2000                                 if j == 0:
  2005                                     array.setdimension([dimension_range])
  2001                                     array.setdimension([dimension_range])
  2006                                 else:
  2002                                 else:
  2007                                     array.appenddimension(dimension_range)
  2003                                     array.appenddimension(dimension_range)
  2008                             if base_type_name in self.GetBaseTypes():
  2004                             if base_type_name in self.GetBaseTypes():
  2009                                 if base_type_name == "STRING":
  2005                                 array.baseType.setcontent(PLCOpenParser.CreateElement(
  2010                                     array.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
  2006                                     base_type_name.lower()
  2011                                 elif base_type_name == "WSTRING":
  2007                                     if base_type_name in ["STRING", "WSTRING"]
  2012                                     array.baseType.setcontent({"name" : "wstring", "value" : plcopen.wstring()})
  2008                                     else base_type_name, "dataType"))
  2013                                 else:
       
  2014                                     array.baseType.setcontent({"name" : base_type_name, "value" : None})
       
  2015                             else:
  2009                             else:
  2016                                 derived_datatype = plcopen.derivedTypes_derived()
  2010                                 derived_datatype = PLCOpenParser.CreateElement("derived", "dataType")
  2017                                 derived_datatype.setname(base_type_name)
  2011                                 derived_datatype.setname(base_type_name)
  2018                                 array.baseType.setcontent({"name" : "derived", "value" : derived_datatype})
  2012                                 array.baseType.setcontent(derived_datatype)
  2019                             element.type.setcontent({"name" : "array", "value" : array})
  2013                             element_type.setcontent(array)
  2020                     elif element_infos["Type"] in self.GetBaseTypes():
  2014                     elif element_infos["Type"] in self.GetBaseTypes():
  2021                         if element_infos["Type"] == "STRING":
  2015                         element_type.setcontent(
  2022                             element.type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
  2016                             PLCOpenParser.CreateElement(
  2023                         elif element_infos["Type"] == "WSTRING":
  2017                                 element_infos["Type"].lower()
  2024                             element.type.setcontent({"name" : "wstring", "value" : plcopen.wstring()})
  2018                                 if element_infos["Type"] in ["STRING", "WSTRING"]
  2025                         else:
  2019                                 else element_infos["Type"], "dataType"))
  2026                             element.type.setcontent({"name" : element_infos["Type"], "value" : None})
       
  2027                     else:
  2020                     else:
  2028                         derived_datatype = plcopen.derivedTypes_derived()
  2021                         derived_datatype = PLCOpenParser.CreateElement("derived", "dataType")
  2029                         derived_datatype.setname(element_infos["Type"])
  2022                         derived_datatype.setname(element_infos["Type"])
  2030                         element.type.setcontent({"name" : "derived", "value" : derived_datatype})
  2023                         element_type.setcontent(derived_datatype)
       
  2024                     element.settype(element_type)
  2031                     if element_infos["Initial Value"] != "":
  2025                     if element_infos["Initial Value"] != "":
  2032                         value = plcopen.value()
  2026                         value = PLCOpenParser.CreateElement("initialValue", "variable")
  2033                         value.setvalue(element_infos["Initial Value"])
  2027                         value.setvalue(element_infos["Initial Value"])
  2034                         element.setinitialValue(value)
  2028                         element.setinitialValue(value)
  2035                     if i == 0:
  2029                     if i == 0:
  2036                         struct.setvariable([element])
  2030                         struct.setvariable([element])
  2037                     else:
  2031                     else:
  2038                         struct.appendvariable(element)
  2032                         struct.appendvariable(element)
  2039                 datatype.baseType.setcontent({"name" : "struct", "value" : struct})
  2033                 datatype.baseType.setcontent(struct)
  2040             if infos["initial"] != "":
  2034             if infos["initial"] != "":
  2041                 if datatype.initialValue is None:
  2035                 if datatype.initialValue is None:
  2042                     datatype.initialValue = plcopen.value()
  2036                     datatype.initialValue = PLCOpenParser.CreateElement("initialValue", "dataType")
  2043                 datatype.initialValue.setvalue(infos["initial"])
  2037                 datatype.initialValue.setvalue(infos["initial"])
  2044             else:
  2038             else:
  2045                 datatype.initialValue = None
  2039                 datatype.initialValue = None
  2046             self.Project.RefreshDataTypeHierarchy()
  2040             self.Project.RefreshDataTypeHierarchy()
  2047             self.Project.RefreshElementUsingTree()
  2041             self.Project.RefreshElementUsingTree()
  2201             words = tagname.split("::")
  2195             words = tagname.split("::")
  2202             if words[0] in ["P","T","A"]:
  2196             if words[0] in ["P","T","A"]:
  2203                 element = self.GetEditedElement(tagname, debug)
  2197                 element = self.GetEditedElement(tagname, debug)
  2204                 if element is not None and element.getbodyType() not in ["ST", "IL"]:
  2198                 if element is not None and element.getbodyType() not in ["ST", "IL"]:
  2205                     for instance in element.getinstances():
  2199                     for instance in element.getinstances():
  2206                         if isinstance(instance, (plcopen.sfcObjects_step, 
  2200                         if isinstance(instance, 
  2207                                                  plcopen.commonObjects_connector, 
  2201                             (PLCOpenParser.GetElementClass("step", "sfcObjects"), 
  2208                                                  plcopen.commonObjects_continuation)):
  2202                              PLCOpenParser.GetElementClass("connector", "commonObjects"), 
       
  2203                              PLCOpenParser.GetElementClass("continuation", "commonObjects"))):
  2209                             names[instance.getname().upper()] = True
  2204                             names[instance.getname().upper()] = True
  2210         else:
  2205         else:
  2211             project = self.GetProject(debug)
  2206             project = self.GetProject(debug)
  2212             if project is not None:
  2207             if project is not None:
  2213                 for datatype in project.getdataTypes():
  2208                 for datatype in project.getdataTypes():
  2392                         return self.GetPouInterfaceReturnType(pou)
  2387                         return self.GetPouInterfaceReturnType(pou)
  2393                     for type, varlist in pou.getvars():
  2388                     for type, varlist in pou.getvars():
  2394                         for var in varlist.getvariable():
  2389                         for var in varlist.getvariable():
  2395                             if var.getname() == varname:
  2390                             if var.getname() == varname:
  2396                                 vartype_content = var.gettype().getcontent()
  2391                                 vartype_content = var.gettype().getcontent()
  2397                                 if vartype_content["name"] == "derived":
  2392                                 vartype_content_type = vartype_content.getLocalTag()
  2398                                     return vartype_content["value"].getname()
  2393                                 if vartype_content_type == "derived":
  2399                                 elif vartype_content["name"] in ["string", "wstring"]:
  2394                                     return vartype_content.getname()
  2400                                     return vartype_content["name"].upper()
       
  2401                                 else:
  2395                                 else:
  2402                                     return vartype_content["name"]
  2396                                     return vartype_content_type.upper()
  2403         return None
  2397         return None
  2404     
  2398     
  2405     def SetConnectionWires(self, connection, connector):
  2399     def SetConnectionWires(self, connection, connector):
  2406         wires = connector.GetWires()
  2400         wires = connector.GetWires()
  2407         idx = 0
  2401         idx = 0
  2457                     pou.removepouVar(type, name)
  2451                     pou.removepouVar(type, name)
  2458     
  2452     
  2459     def AddEditedElementBlock(self, tagname, id, blocktype, blockname = None):
  2453     def AddEditedElementBlock(self, tagname, id, blocktype, blockname = None):
  2460         element = self.GetEditedElement(tagname)
  2454         element = self.GetEditedElement(tagname)
  2461         if element is not None:
  2455         if element is not None:
  2462             block = plcopen.fbdObjects_block()
  2456             block = PLCOpenParser.CreateElement("block", "fbdObjects")
  2463             block.setlocalId(id)
  2457             block.setlocalId(id)
  2464             block.settypeName(blocktype)
  2458             block.settypeName(blocktype)
  2465             blocktype_infos = self.GetBlockType(blocktype)
  2459             blocktype_infos = self.GetBlockType(blocktype)
  2466             if blocktype_infos["type"] != "function" and blockname is not None:
  2460             if blocktype_infos["type"] != "function" and blockname is not None:
  2467                 block.setinstanceName(blockname)
  2461                 block.setinstanceName(blockname)