249 |
249 |
250 # Return project pou names |
250 # Return project pou names |
251 def GetProjectConfigNames(self, debug = False): |
251 def GetProjectConfigNames(self, debug = False): |
252 project = self.GetProject(debug) |
252 project = self.GetProject(debug) |
253 if project is not None: |
253 if project is not None: |
254 return [config.getName() for config in project.getconfigurations()] |
254 return [config.getname() for config in project.getconfigurations()] |
255 return [] |
255 return [] |
256 |
256 |
257 # Return project pou variables |
257 # Return project pou variables |
258 def GetProjectPouVariables(self, pou_name = None, debug = False): |
258 def GetProjectPouVariables(self, pou_name = None, debug = False): |
259 variables = [] |
259 variables = [] |
597 new_pou = self.Copy(pou) |
597 new_pou = self.Copy(pou) |
598 new_pou.setname(name) |
598 new_pou.setname(name) |
599 self.Project.insertpou(-1, new_pou) |
599 self.Project.insertpou(-1, new_pou) |
600 self.BufferProject() |
600 self.BufferProject() |
601 |
601 |
|
602 def PastePou(self, pou_type, pou_xml): |
|
603 ''' |
|
604 Adds the POU defined by 'pou_xml' to the current project with type 'pou_type' |
|
605 ''' |
|
606 try: |
|
607 tree = minidom.parseString(pou_xml) |
|
608 root = tree.childNodes[0] |
|
609 except: |
|
610 return _("Couldn't paste non-POU object.") |
|
611 |
|
612 if root.nodeName == "pou": |
|
613 new_pou = plcopen.pous_pou() |
|
614 new_pou.loadXMLTree(root) |
|
615 |
|
616 name = new_pou.getname() |
|
617 orig_type = new_pou.getpouType() |
|
618 |
|
619 # prevent violations of POU content restrictions: |
|
620 # function blocks cannot be pasted as functions, |
|
621 # programs cannot be pasted as functions or function blocks |
|
622 if orig_type == 'functionBlock' and pou_type == 'function' or \ |
|
623 orig_type == 'program' and pou_type in ['function', 'functionBlock']: |
|
624 return _('''%s "%s" can't be pasted as a %s.''') % (orig_type, name, pou_type) |
|
625 |
|
626 while self.Project.getpou(name): |
|
627 # a POU with that name already exists. |
|
628 # make a new name and test if a POU with that name exists. |
|
629 |
|
630 # append an incrementing numeric suffix to the POU name. it |
|
631 # doesn't count up perfectly, but as long as it's unique who cares? |
|
632 if name[-1] >= '0' and name[-1] <= '9': |
|
633 last_digit = int(name[-1]) |
|
634 name = name[0:-1] + str(last_digit+1) |
|
635 else: |
|
636 name = name + '1' |
|
637 |
|
638 # we've found a name that does not already exist, use it |
|
639 new_pou.setname(name) |
|
640 new_pou.setpouType(pou_type) |
|
641 |
|
642 self.Project.insertpou(-1, new_pou) |
|
643 self.BufferProject() |
|
644 else: |
|
645 return _("Couldn't paste non-POU object.") |
|
646 |
602 # Remove a Pou from project |
647 # Remove a Pou from project |
603 def ProjectRemovePou(self, pou_name): |
648 def ProjectRemovePou(self, pou_name): |
604 if self.Project is not None: |
649 if self.Project is not None: |
605 self.Project.removepou(pou_name) |
650 self.Project.removepou(pou_name) |
606 self.BufferProject() |
651 self.BufferProject() |
735 |
780 |
736 # Change the name of a configuration resource |
781 # Change the name of a configuration resource |
737 def ChangeConfigurationResourceName(self, config_name, old_name, new_name): |
782 def ChangeConfigurationResourceName(self, config_name, old_name, new_name): |
738 if self.Project is not None: |
783 if self.Project is not None: |
739 # Found the resource corresponding to old name and change its name to new name |
784 # Found the resource corresponding to old name and change its name to new name |
740 resource = self.Project.getconfigurationResource(config_name) |
785 resource = self.Project.getconfigurationResource(config_name, old_name) |
741 if resource is not None: |
786 if resource is not None: |
742 resource.setName(new_name) |
787 resource.setname(new_name) |
743 self.BufferProject() |
788 self.BufferProject() |
744 |
789 |
745 # Return the type of the pou given by its name |
790 # Return the type of the pou given by its name |
746 def GetPouType(self, name, debug = False): |
791 def GetPouType(self, name, debug = False): |
747 project = self.GetProject(debug) |
792 project = self.GetProject(debug) |
848 if var["Constant"] == "Yes": |
893 if var["Constant"] == "Yes": |
849 current_varlist.setconstant(True) |
894 current_varlist.setconstant(True) |
850 # Create variable and change its properties |
895 # Create variable and change its properties |
851 tempvar = plcopen.varListPlain_variable() |
896 tempvar = plcopen.varListPlain_variable() |
852 tempvar.setname(var["Name"]) |
897 tempvar.setname(var["Name"]) |
|
898 |
853 var_type = plcopen.dataType() |
899 var_type = plcopen.dataType() |
854 if var["Type"] in self.GetBaseTypes(): |
900 if var["Type"] in self.GetBaseTypes(): |
855 if var["Type"] == "STRING": |
901 if var["Type"] == "STRING": |
856 var_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
902 var_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
857 elif var["Type"] == "WSTRING": |
903 elif var["Type"] == "WSTRING": |
861 else: |
907 else: |
862 derived_type = plcopen.derivedTypes_derived() |
908 derived_type = plcopen.derivedTypes_derived() |
863 derived_type.setname(var["Type"]) |
909 derived_type.setname(var["Type"]) |
864 var_type.setcontent({"name" : "derived", "value" : derived_type}) |
910 var_type.setcontent({"name" : "derived", "value" : derived_type}) |
865 tempvar.settype(var_type) |
911 tempvar.settype(var_type) |
|
912 |
866 if var["Initial Value"] != "": |
913 if var["Initial Value"] != "": |
867 value = plcopen.value() |
914 value = plcopen.value() |
868 value.setvalue(var["Initial Value"]) |
915 value.setvalue(var["Initial Value"]) |
869 tempvar.setinitialValue(value) |
916 tempvar.setinitialValue(value) |
870 if var["Location"] != "": |
917 if var["Location"] != "": |
871 tempvar.setaddress(var["Location"]) |
918 tempvar.setaddress(var["Location"]) |
872 else: |
919 else: |
873 tempvar.setaddress(None) |
920 tempvar.setaddress(None) |
|
921 if var['Documentation'] != "": |
|
922 ft = plcopen.formattedText() |
|
923 ft.settext(var['Documentation']) |
|
924 tempvar.setdocumentation(ft) |
|
925 |
874 # Add variable to varList |
926 # Add variable to varList |
875 current_varlist.appendvariable(tempvar) |
927 current_varlist.appendvariable(tempvar) |
876 return varlist_list |
928 return varlist_list |
|
929 |
|
930 def GetVariableDictionary(self, varlist, var): |
|
931 ''' |
|
932 convert a PLC variable to the dictionary representation |
|
933 returned by Get*Vars) |
|
934 ''' |
|
935 |
|
936 tempvar = {"Name" : var.getname()} |
|
937 |
|
938 vartype_content = var.gettype().getcontent() |
|
939 if vartype_content["name"] == "derived": |
|
940 tempvar["Type"] = vartype_content["value"].getname() |
|
941 elif vartype_content["name"] in ["string", "wstring"]: |
|
942 tempvar["Type"] = vartype_content["name"].upper() |
|
943 else: |
|
944 tempvar["Type"] = vartype_content["name"] |
|
945 |
|
946 tempvar["Edit"] = True |
|
947 |
|
948 initial = var.getinitialValue() |
|
949 if initial: |
|
950 tempvar["Initial Value"] = initial.getvalue() |
|
951 else: |
|
952 tempvar["Initial Value"] = "" |
|
953 |
|
954 address = var.getaddress() |
|
955 if address: |
|
956 tempvar["Location"] = address |
|
957 else: |
|
958 tempvar["Location"] = "" |
|
959 |
|
960 if varlist.getretain(): |
|
961 tempvar["Retain"] = "Yes" |
|
962 else: |
|
963 tempvar["Retain"] = "No" |
|
964 |
|
965 if varlist.getconstant(): |
|
966 tempvar["Constant"] = "Yes" |
|
967 else: |
|
968 tempvar["Constant"] = "No" |
|
969 |
|
970 doc = var.getdocumentation() |
|
971 if doc: |
|
972 tempvar["Documentation"] = doc.gettext() |
|
973 else: |
|
974 tempvar["Documentation"] = "" |
|
975 |
|
976 return tempvar |
877 |
977 |
878 # Replace the configuration globalvars by those given |
978 # Replace the configuration globalvars by those given |
879 def SetConfigurationGlobalVars(self, name, vars): |
979 def SetConfigurationGlobalVars(self, name, vars): |
880 if self.Project is not None: |
980 if self.Project is not None: |
881 # Found the configuration corresponding to name |
981 # Found the configuration corresponding to name |
895 configuration = project.getconfiguration(name) |
995 configuration = project.getconfiguration(name) |
896 if configuration is not None: |
996 if configuration is not None: |
897 # Extract variables from every varLists |
997 # Extract variables from every varLists |
898 for varlist in configuration.getglobalVars(): |
998 for varlist in configuration.getglobalVars(): |
899 for var in varlist.getvariable(): |
999 for var in varlist.getvariable(): |
900 tempvar = {"Name" : var.getname(), "Class" : "Global"} |
1000 tempvar = self.GetVariableDictionary(varlist, var) |
901 vartype_content = var.gettype().getcontent() |
1001 tempvar["Class"] = "Global" |
902 if vartype_content["name"] == "derived": |
|
903 tempvar["Type"] = vartype_content["value"].getname() |
|
904 elif vartype_content["name"] in ["string", "wstring"]: |
|
905 tempvar["Type"] = vartype_content["name"].upper() |
|
906 else: |
|
907 tempvar["Type"] = vartype_content["name"] |
|
908 tempvar["Edit"] = True |
|
909 initial = var.getinitialValue() |
|
910 if initial: |
|
911 tempvar["Initial Value"] = initial.getvalue() |
|
912 else: |
|
913 tempvar["Initial Value"] = "" |
|
914 address = var.getaddress() |
|
915 if address: |
|
916 tempvar["Location"] = address |
|
917 else: |
|
918 tempvar["Location"] = "" |
|
919 if varlist.getretain(): |
|
920 tempvar["Retain"] = "Yes" |
|
921 else: |
|
922 tempvar["Retain"] = "No" |
|
923 if varlist.getconstant(): |
|
924 tempvar["Constant"] = "Yes" |
|
925 else: |
|
926 tempvar["Constant"] = "No" |
|
927 vars.append(tempvar) |
1002 vars.append(tempvar) |
928 return vars |
1003 return vars |
929 |
1004 |
930 # Replace the resource globalvars by those given |
1005 # Replace the resource globalvars by those given |
931 def SetConfigurationResourceGlobalVars(self, config_name, name, vars): |
1006 def SetConfigurationResourceGlobalVars(self, config_name, name, vars): |
947 resource = project.getconfigurationResource(config_name, name) |
1022 resource = project.getconfigurationResource(config_name, name) |
948 if resource: |
1023 if resource: |
949 # Extract variables from every varLists |
1024 # Extract variables from every varLists |
950 for varlist in resource.getglobalVars(): |
1025 for varlist in resource.getglobalVars(): |
951 for var in varlist.getvariable(): |
1026 for var in varlist.getvariable(): |
952 tempvar = {"Name" : var.getname(), "Class" : "Global"} |
1027 tempvar = self.GetVariableDictionary(varlist, var) |
953 vartype_content = var.gettype().getcontent() |
1028 tempvar["Class"] = "Global" |
954 if vartype_content["name"] == "derived": |
|
955 tempvar["Type"] = vartype_content["value"].getname() |
|
956 elif vartype_content["name"] in ["string", "wstring"]: |
|
957 tempvar["Type"] = vartype_content["name"].upper() |
|
958 else: |
|
959 tempvar["Type"] = vartype_content["name"] |
|
960 tempvar["Edit"] = True |
|
961 initial = var.getinitialValue() |
|
962 if initial: |
|
963 tempvar["Initial Value"] = initial.getvalue() |
|
964 else: |
|
965 tempvar["Initial Value"] = "" |
|
966 address = var.getaddress() |
|
967 if address: |
|
968 tempvar["Location"] = address |
|
969 else: |
|
970 tempvar["Location"] = "" |
|
971 if varlist.getretain(): |
|
972 tempvar["Retain"] = "Yes" |
|
973 else: |
|
974 tempvar["Retain"] = "No" |
|
975 if varlist.getconstant(): |
|
976 tempvar["Constant"] = "Yes" |
|
977 else: |
|
978 tempvar["Constant"] = "No" |
|
979 vars.append(tempvar) |
1029 vars.append(tempvar) |
980 return vars |
1030 return vars |
981 |
1031 |
982 # Recursively generate element name tree for a structured variable |
1032 # Recursively generate element name tree for a structured variable |
983 def GenerateVarTree(self, typename, debug = False): |
1033 def GenerateVarTree(self, typename, debug = False): |
1012 tree.append((element.getname(), element_type["value"].getname(), self.GenerateVarTree(element_type["value"].getname()))) |
1062 tree.append((element.getname(), element_type["value"].getname(), self.GenerateVarTree(element_type["value"].getname()))) |
1013 else: |
1063 else: |
1014 tree.append((element.getname(), element_type["name"], ([], []))) |
1064 tree.append((element.getname(), element_type["name"], ([], []))) |
1015 return tree, [] |
1065 return tree, [] |
1016 return [], [] |
1066 return [], [] |
1017 |
1067 |
1018 # Return the interface for the given pou |
1068 # Return the interface for the given pou |
1019 def GetPouInterfaceVars(self, pou, debug = False): |
1069 def GetPouInterfaceVars(self, pou, debug = False): |
1020 vars = [] |
1070 vars = [] |
1021 # Verify that the pou has an interface |
1071 # Verify that the pou has an interface |
1022 if pou.interface is not None: |
1072 if pou.interface is not None: |
1023 # Extract variables from every varLists |
1073 # Extract variables from every varLists |
1024 for type, varlist in pou.getvars(): |
1074 for type, varlist in pou.getvars(): |
1025 for var in varlist.getvariable(): |
1075 for var in varlist.getvariable(): |
1026 tempvar = {"Name" : var.getname(), "Class" : type, "Tree" : ([], [])} |
1076 tempvar = self.GetVariableDictionary(varlist, var) |
|
1077 |
|
1078 tempvar["Class"] = type |
|
1079 tempvar["Tree"] = ([], []) |
|
1080 |
1027 vartype_content = var.gettype().getcontent() |
1081 vartype_content = var.gettype().getcontent() |
1028 if vartype_content["name"] == "derived": |
1082 if vartype_content["name"] == "derived": |
1029 tempvar["Type"] = vartype_content["value"].getname() |
|
1030 tempvar["Edit"] = not pou.hasblock(tempvar["Name"]) |
1083 tempvar["Edit"] = not pou.hasblock(tempvar["Name"]) |
1031 tempvar["Tree"] = self.GenerateVarTree(tempvar["Type"], debug) |
1084 tempvar["Tree"] = self.GenerateVarTree(tempvar["Type"], debug) |
1032 else: |
1085 |
1033 if vartype_content["name"] in ["string", "wstring"]: |
|
1034 tempvar["Type"] = vartype_content["name"].upper() |
|
1035 else: |
|
1036 tempvar["Type"] = vartype_content["name"] |
|
1037 tempvar["Edit"] = True |
|
1038 initial = var.getinitialValue() |
|
1039 if initial: |
|
1040 tempvar["Initial Value"] = initial.getvalue() |
|
1041 else: |
|
1042 tempvar["Initial Value"] = "" |
|
1043 address = var.getaddress() |
|
1044 if address: |
|
1045 tempvar["Location"] = address |
|
1046 else: |
|
1047 tempvar["Location"] = "" |
|
1048 if varlist.getretain(): |
|
1049 tempvar["Retain"] = "Yes" |
|
1050 else: |
|
1051 tempvar["Retain"] = "No" |
|
1052 if varlist.getconstant(): |
|
1053 tempvar["Constant"] = "Yes" |
|
1054 else: |
|
1055 tempvar["Constant"] = "No" |
|
1056 vars.append(tempvar) |
1086 vars.append(tempvar) |
1057 return vars |
1087 return vars |
1058 |
1088 |
1059 # Replace the Pou interface by the one given |
1089 # Replace the Pou interface by the one given |
1060 def SetPouInterfaceVars(self, name, vars): |
1090 def SetPouInterfaceVars(self, name, vars): |
1229 project = self.GetProject(debug) |
1259 project = self.GetProject(debug) |
1230 if project is not None: |
1260 if project is not None: |
1231 return project.GetBaseType(type) |
1261 return project.GetBaseType(type) |
1232 return None |
1262 return None |
1233 |
1263 |
1234 # Return Base Types |
|
1235 def GetBaseTypes(self): |
1264 def GetBaseTypes(self): |
1236 return [value for value in TypeHierarchy.keys() if not value.startswith("ANY")] |
1265 ''' |
|
1266 return the list of datatypes defined in IEC 61131-3. |
|
1267 TypeHierarchy_list has a rough order to it (e.g. SINT, INT, DINT, ...), |
|
1268 which makes it easy for a user to find a type in a menu. |
|
1269 ''' |
|
1270 return [x for x,y in TypeHierarchy_list if not x.startswith("ANY")] |
1237 |
1271 |
1238 def IsOfType(self, type, reference, debug = False): |
1272 def IsOfType(self, type, reference, debug = False): |
1239 project = self.GetProject(debug) |
1273 project = self.GetProject(debug) |
1240 if project is not None: |
1274 if project is not None: |
1241 return project.IsOfType(type, reference) |
1275 return project.IsOfType(type, reference) |
1659 exclude = {} |
1693 exclude = {} |
1660 for root in tree.childNodes: |
1694 for root in tree.childNodes: |
1661 if root.nodeType == tree.ELEMENT_NODE and root.nodeName == "paste": |
1695 if root.nodeType == tree.ELEMENT_NODE and root.nodeName == "paste": |
1662 for child in root.childNodes: |
1696 for child in root.childNodes: |
1663 if child.nodeType == tree.ELEMENT_NODE: |
1697 if child.nodeType == tree.ELEMENT_NODE: |
|
1698 if not child.nodeName in plcopen.ElementNameToClass: |
|
1699 return _("\"%s\" element can't be pasted here!!!")%child.nodeName |
|
1700 |
1664 classname = plcopen.ElementNameToClass[child.nodeName] |
1701 classname = plcopen.ElementNameToClass[child.nodeName] |
1665 if not self.CheckPasteCompatibility[bodytype](classname): |
1702 if not self.CheckPasteCompatibility[bodytype](classname): |
1666 return _("\"%s\" element can't be paste here!!!")%child.nodeName |
1703 return _("\"%s\" element can't be pasted here!!!")%child.nodeName |
|
1704 |
1667 classobj = getattr(plcopen, classname, None) |
1705 classobj = getattr(plcopen, classname, None) |
1668 if classobj is not None: |
1706 if classobj is not None: |
1669 instance = classobj() |
1707 instance = classobj() |
1670 instance.loadXMLTree(child) |
1708 instance.loadXMLTree(child) |
1671 if child.nodeName == "block": |
1709 if child.nodeName == "block": |
1672 blockname = instance.getinstanceName() |
1710 blockname = instance.getinstanceName() |
1673 if blockname is not None: |
1711 if blockname is not None: |
1674 blocktype = instance.gettypeName() |
1712 blocktype = instance.gettypeName() |
1675 if element_type == "function": |
1713 if element_type == "function": |
1676 return _("FunctionBlock \"%s\" can't be paste in a Function!!!")%blocktype |
1714 return _("FunctionBlock \"%s\" can't be pasted in a Function!!!")%blocktype |
1677 blockname = self.GenerateNewName(tagname, blockname, "Block%d", debug=debug) |
1715 blockname = self.GenerateNewName(tagname, blockname, "Block%d", debug=debug) |
1678 exclude[blockname] = True |
1716 exclude[blockname] = True |
1679 instance.setinstanceName(blockname) |
1717 instance.setinstanceName(blockname) |
1680 self.AddEditedElementPouVar(tagname, blocktype, blockname) |
1718 self.AddEditedElementPouVar(tagname, blocktype, blockname) |
1681 elif child.nodeName == "step": |
1719 elif child.nodeName == "step": |