plcopen/plcopen.py
changeset 1276 29bfd39e8e7a
parent 1261 46425f3248b7
child 1281 47131e3388f4
equal deleted inserted replaced
1275:8d4de18c9f29 1276:29bfd39e8e7a
   498         block_infos["usage"] = "\n (%s) => (%s)" % (", ".join(["%s:%s" % (input[1], input[0]) for input in block_infos["inputs"]]),
   498         block_infos["usage"] = "\n (%s) => (%s)" % (", ".join(["%s:%s" % (input[1], input[0]) for input in block_infos["inputs"]]),
   499                                                     ", ".join(["%s:%s" % (output[1], output[0]) for output in block_infos["outputs"]]))
   499                                                     ", ".join(["%s:%s" % (output[1], output[0]) for output in block_infos["outputs"]]))
   500         self.CustomBlockTypes.append(block_infos)
   500         self.CustomBlockTypes.append(block_infos)
   501     setattr(cls, "AddCustomBlockType", AddCustomBlockType)
   501     setattr(cls, "AddCustomBlockType", AddCustomBlockType)
   502 
   502 
       
   503     def AddElementUsingTreeInstance(self, name, type_infos):
       
   504         typename = type_infos.getname()
       
   505         if not self.ElementUsingTree.has_key(typename):
       
   506             self.ElementUsingTree[typename] = [name]
       
   507         elif name not in self.ElementUsingTree[typename]:
       
   508             self.ElementUsingTree[typename].append(name)
       
   509     setattr(cls, "AddElementUsingTreeInstance", AddElementUsingTreeInstance)
       
   510     
   503     def RefreshElementUsingTree(self):
   511     def RefreshElementUsingTree(self):
   504         # Reset the tree of user-defined element cross-use
   512         # Reset the tree of user-defined element cross-use
   505         self.ElementUsingTree = {}
   513         self.ElementUsingTree = {}
   506         pous = self.getpous()
   514         pous = self.getpous()
   507         datatypes = self.getdataTypes()
   515         datatypes = self.getdataTypes()
   508         # Reference all the user-defined elementu names and initialize the tree of 
       
   509         # user-defined elemnt cross-use
       
   510         elementnames = [datatype.getname() for datatype in datatypes] + \
       
   511                        [pou.getname() for pou in pous]
       
   512         for name in elementnames:
       
   513             self.ElementUsingTree[name] = []
       
   514         # Analyze each datatype
   516         # Analyze each datatype
   515         for datatype in datatypes:
   517         for datatype in datatypes:
   516             name = datatype.getname()
   518             name = datatype.getname()
   517             basetype_content = datatype.baseType.getcontent()
   519             basetype_content = datatype.baseType.getcontent()
   518             if basetype_content["name"] == "derived":
   520             if basetype_content["name"] == "derived":
   520                 if name in self.ElementUsingTree[typename]:
   522                 if name in self.ElementUsingTree[typename]:
   521                     self.ElementUsingTree[typename].append(name)
   523                     self.ElementUsingTree[typename].append(name)
   522             elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned", "array"]:
   524             elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned", "array"]:
   523                 base_type = basetype_content["value"].baseType.getcontent()
   525                 base_type = basetype_content["value"].baseType.getcontent()
   524                 if base_type["name"] == "derived":
   526                 if base_type["name"] == "derived":
   525                     typename = base_type["value"].getname()
   527                     self.AddElementUsingTreeInstance(name, base_type["value"])
   526                     if self.ElementUsingTree.has_key(typename) and name not in self.ElementUsingTree[typename]:
       
   527                         self.ElementUsingTree[typename].append(name)
       
   528             elif basetype_content["name"] == "struct":
   528             elif basetype_content["name"] == "struct":
   529                 for element in basetype_content["value"].getvariable():
   529                 for element in basetype_content["value"].getvariable():
   530                     type_content = element.type.getcontent()
   530                     type_content = element.type.getcontent()
   531                     if type_content["name"] == "derived":
   531                     if type_content["name"] == "derived":
   532                         typename = type_content["value"].getname()
   532                         self.AddElementUsingTreeInstance(name, type_content["value"])
   533                         if self.ElementUsingTree.has_key(typename) and name not in self.ElementUsingTree[typename]:
       
   534                             self.ElementUsingTree[typename].append(name)
       
   535         # Analyze each pou
   533         # Analyze each pou
   536         for pou in pous:
   534         for pou in pous:
   537             name = pou.getname()
   535             name = pou.getname()
   538             if pou.interface:
   536             if pou.interface:
   539                 # Extract variables from every varLists
   537                 # Extract variables from every varLists
   540                 for type, varlist in pou.getvars():
   538                 for type, varlist in pou.getvars():
   541                     for var in varlist.getvariable():
   539                     for var in varlist.getvariable():
   542                         vartype_content = var.gettype().getcontent()
   540                         vartype_content = var.gettype().getcontent()
   543                         if vartype_content["name"] == "derived":
   541                         if vartype_content["name"] == "derived":
   544                             typename = vartype_content["value"].getname()
   542                             self.AddElementUsingTreeInstance(name, vartype_content["value"])
   545                             if self.ElementUsingTree.has_key(typename) and name not in self.ElementUsingTree[typename]:
       
   546                                 self.ElementUsingTree[typename].append(name)
       
   547             for typename in self.ElementUsingTree.iterkeys():
   543             for typename in self.ElementUsingTree.iterkeys():
   548                 if typename != name and pou.hasblock(block_type=typename) and name not in self.ElementUsingTree[typename]:
   544                 if typename != name and pou.hasblock(block_type=typename) and name not in self.ElementUsingTree[typename]:
   549                     self.ElementUsingTree[typename].append(name)
   545                     self.ElementUsingTree[typename].append(name)
   550         
   546         
   551     setattr(cls, "RefreshElementUsingTree", RefreshElementUsingTree)
   547     setattr(cls, "RefreshElementUsingTree", RefreshElementUsingTree)