'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.
authorEdouard Tisserant
Thu, 13 Jun 2019 15:49:48 +0200
changeset 2614 6b4061f6ced6
parent 2613 e05458405ff4
child 2615 2eb66f155c2c
child 2618 4eca95f91cba
'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.
IDEFrame.py
PLCControler.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)
--- 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):