PLCGenerator.py
changeset 194 1b3f8b4f8e04
parent 191 d77f9b783ce8
child 201 d5b778dab4b0
--- a/PLCGenerator.py	Thu Apr 03 18:30:02 2008 +0200
+++ b/PLCGenerator.py	Thu Apr 03 18:31:35 2008 +0200
@@ -118,9 +118,7 @@
             pou_program = PouProgram(pou.getname(), pouTypeNames[pou_type])
         else:
             raise PLCGenException, "Undefined pou type"
-        interface = pou.getinterface()
-        if interface is not None:
-            pou_program.GenerateInterface(interface)
+        pou_program.GenerateInterface(pou)
         pou_program.GenerateConnectionTypes(pou)
         pou_program.GenerateProgram(pou)
         currentProgram += pou_program.GenerateSTProgram()
@@ -311,28 +309,47 @@
                 return self.RelatedConnections.pop(i)
         return [connection]
     
-    def GenerateInterface(self, interface):
-        if self.Type == "FUNCTION":
-            returntype_content = interface.getreturnType().getcontent()
-            if returntype_content["value"] is None:
-                self.ReturnType = returntype_content["name"]
-            else:
-                self.ReturnType = returntype_content["value"].getname()
-        for varlist in interface.getcontent():
-            variables = []
-            located = []
-            for var in varlist["value"].getvariable():
-                vartype_content = var.gettype().getcontent()
-                if vartype_content["name"] == "derived":
-                    var_type = vartype_content["value"].getname()
-                    GeneratePouProgram(var_type)
-                    blocktype = GetBlockType(var_type)
-                    if blocktype is not None:
-                        for variable in blocktype["initialise"](var_type, var.getname()):
-                            if variable[2] is not None:
-                                located.append(variable)
+    def GenerateInterface(self, pou):
+        interface = pou.getinterface()
+        if interface is not None:
+            body = pou.getbody()
+            body_content = body.getcontent()
+            if self.Type == "FUNCTION":
+                returntype_content = interface.getreturnType().getcontent()
+                if returntype_content["value"] is None:
+                    self.ReturnType = returntype_content["name"]
+                else:
+                    self.ReturnType = returntype_content["value"].getname()
+            for varlist in interface.getcontent():
+                variables = []
+                located = []
+                for var in varlist["value"].getvariable():
+                    vartype_content = var.gettype().getcontent()
+                    if vartype_content["name"] == "derived":
+                        var_type = vartype_content["value"].getname()
+                        GeneratePouProgram(var_type)
+                        blocktype = GetBlockType(var_type)
+                        if blocktype is not None:
+                            if body_content["name"] in ["FBD", "LD", "SFC"]:
+                                block = pou.getinstanceByName(var.getname())
                             else:
-                                variables.append(variable)
+                                block = None
+                            for variable in blocktype["initialise"](var_type, var.getname(), block):
+                                if variable[2] is not None:
+                                    located.append(variable)
+                                else:
+                                    variables.append(variable)
+                        else:
+                            initial = var.getinitialValue()
+                            if initial:
+                                initial_value = initial.getvalue()
+                            else:
+                                initial_value = None
+                            address = var.getaddress()
+                            if address is not None:
+                                located.append((vartype_content["value"].getname(), var.getname(), address, initial_value))
+                            else:
+                                variables.append((vartype_content["value"].getname(), var.getname(), None, initial_value))
                     else:
                         initial = var.getinitialValue()
                         if initial:
@@ -340,32 +357,21 @@
                         else:
                             initial_value = None
                         address = var.getaddress()
-                        if address is not None:
-                            located.append((vartype_content["value"].getname(), var.getname(), address, initial_value))
+                        if vartype_content["name"] in ["string", "wstring"]:
+                            if address is not None:
+                                located.append((vartype_content["name"].upper(), var.getname(), address, initial_value))
+                            else:
+                                variables.append((vartype_content["name"].upper(), var.getname(), None, initial_value))
+                        elif address is not None:
+                            located.append((vartype_content["name"], var.getname(), address, initial_value))
                         else:
-                            variables.append((vartype_content["value"].getname(), var.getname(), None, initial_value))
-                else:
-                    initial = var.getinitialValue()
-                    if initial:
-                        initial_value = initial.getvalue()
-                    else:
-                        initial_value = None
-                    address = var.getaddress()
-                    if vartype_content["name"] in ["string", "wstring"]:
-                        if address is not None:
-                            located.append((vartype_content["name"].upper(), var.getname(), address, initial_value))
-                        else:
-                            variables.append((vartype_content["name"].upper(), var.getname(), None, initial_value))
-                    elif address is not None:
-                        located.append((vartype_content["name"], var.getname(), address, initial_value))
-                    else:
-                        variables.append((vartype_content["name"], var.getname(), None, initial_value))
-            if len(variables) > 0:
-                self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getretain(), 
-                            varlist["value"].getconstant(), False, variables))
-            if len(located) > 0:
-                self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getretain(), 
-                            varlist["value"].getconstant(), True, located))
+                            variables.append((vartype_content["name"], var.getname(), None, initial_value))
+                if len(variables) > 0:
+                    self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getretain(), 
+                                varlist["value"].getconstant(), False, variables))
+                if len(located) > 0:
+                    self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getretain(), 
+                                varlist["value"].getconstant(), True, located))
         
     def GenerateConnectionTypes(self, pou):
         body = pou.getbody()