# HG changeset patch
# User Edouard Tisserant
# Date 1560433788 -7200
# Node ID 6b4061f6ced642b8b478c78951d388fc55cfcc36
# Parent  e05458405ff479b066d81f8034cf5005aeb65d9f
'Change POU Type To' becomes 'Duplicate as...', avoiding side effects of type change when POU is already instanciated. Also remove leftover returType tag in ex-function POUs, triggering exceptions at build time.

diff -r e05458405ff4 -r 6b4061f6ced6 IDEFrame.py
--- a/IDEFrame.py	Thu Jun 06 14:03:16 2019 +0200
+++ b/IDEFrame.py	Thu Jun 13 15:49:48 2019 +0200
@@ -1997,7 +1997,7 @@
                     new_id = wx.NewId()
                     AppendMenu(change_menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Program"))
                     self.Bind(wx.EVT_MENU, self.GenerateChangePouTypeFunction(name, "program"), id=new_id)
-                    menu.AppendMenu(wx.NewId(), _("Change POU Type To"), change_menu)
+                    menu.AppendMenu(wx.NewId(), _("Duplicate as..."), change_menu)
                 new_id = wx.NewId()
                 AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Rename"))
                 self.Bind(wx.EVT_MENU, self.OnRenamePouMenu, id=new_id)
diff -r e05458405ff4 -r 6b4061f6ced6 PLCControler.py
--- a/PLCControler.py	Thu Jun 06 14:03:16 2019 +0200
+++ b/PLCControler.py	Thu Jun 13 15:49:48 2019 +0200
@@ -532,7 +532,26 @@
         if self.Project is not None:
             pou = self.Project.getpou(name)
             if pou is not None:
-                pou.setpouType(pou_type)
+                new_pou = self.Copy(pou)
+                idx = 0
+                new_name = name + "_" + pou_type
+                while self.Project.getpou(new_name) is not None:
+                    idx += 1
+                    new_name = "%s%d" % (name, idx)
+                new_pou.setname(new_name)
+
+                orig_type = pou.getpouType()
+                if orig_type == 'function' and pou_type in ['functionBlock', 'program']:
+                    # delete return type 
+                    return_type_obj = new_pou.interface.getreturnType()
+                    new_pou.interface.remove(return_type_obj)
+                    # To be ultimately correct we could re-create an 
+                    # output variable with same name+_out or so 
+                    # but in any case user will have to connect/assign 
+                    # this output, so better leave it as-is
+
+                new_pou.setpouType(pou_type)
+                self.Project.insertpou(0, new_pou)
                 self.BufferProject()
 
     def GetPouXml(self, pou_name):