PLCControler.py
changeset 107 255eada20688
parent 101 3f06a178b960
child 108 9aa1fdfb7cb2
equal deleted inserted replaced
106:3fc63036de16 107:255eada20688
   381 
   381 
   382 #-------------------------------------------------------------------------------
   382 #-------------------------------------------------------------------------------
   383 #                        Project Pous management functions
   383 #                        Project Pous management functions
   384 #-------------------------------------------------------------------------------
   384 #-------------------------------------------------------------------------------
   385     
   385     
       
   386     def RemoveElementEditing(self, index):
       
   387         self.ElementsOpened.pop(index)
       
   388         if self.CurrentElementEditing > index:
       
   389             self.CurrentElementEditing -= 1
       
   390         if len(self.ElementsOpened) > 0:
       
   391             self.CurrentElementEditing = max(0, min(self.CurrentElementEditing, len(self.ElementsOpened) - 1))
       
   392         else:
       
   393             self.CurrentElementEditing = None
       
   394         
   386     # Add a Pou to Project
   395     # Add a Pou to Project
   387     def ProjectAddPou(self, name, pou_type, body_type):
   396     def ProjectAddPou(self, pou_name, pou_type, body_type):
   388         # Add the pou to project
   397         # Add the pou to project
   389         self.Project.appendPou(name, pou_type, body_type)
   398         self.Project.appendPou(pou_name, pou_type, body_type)
   390         self.SetPouInterfaceReturnType(name, "BOOL")
   399         if pou_type == "function":
       
   400             self.SetPouInterfaceReturnType(pou_name, "BOOL")
   391         self.RefreshPouUsingTree()
   401         self.RefreshPouUsingTree()
   392         self.RefreshBlockTypes()
   402         self.RefreshBlockTypes()
   393         self.BufferProject()
   403         self.BufferProject()
   394     
   404     
   395     # Remove a pou from project
   405     # Remove a pou from project
   396     def ProjectRemovePou(self, name):
   406     def ProjectRemovePou(self, pou_name):
   397         removed = None
       
   398         # Search if the pou removed is currently opened
   407         # Search if the pou removed is currently opened
   399         for i, pou in enumerate(self.ElementsOpened):
   408         for i, element in enumerate(self.ElementsOpened):
   400             if pou == name:
   409             words = element.split("::")
   401                 removed = i
   410             if words[0] == "P" and words[1] == pou_name:
   402         # If found, remove pou from list of opened pous and actualize current edited
   411                 self.RemoveElementEditing(i)
   403         if removed != None:
       
   404             self.ElementsOpened.pop(removed)
       
   405             if self.CurrentElementEditing > removed:
       
   406                 self.CurrentElementEditing -= 1
       
   407             if len(self.ElementsOpened) > 0:
       
   408                 self.CurrentElementEditing = max(0, min(self.CurrentElementEditing, len(self.ElementsOpened) - 1))
       
   409             else:
       
   410                 self.CurrentElementEditing = None
       
   411         # Remove pou from project
   412         # Remove pou from project
   412         self.Project.removePou(name)
   413         self.Project.removePou(pou_name)
   413         self.RefreshPouUsingTree()
   414         self.RefreshPouUsingTree()
   414         self.RefreshBlockTypes()
   415         self.RefreshBlockTypes()
   415         self.BufferProject()
   416         self.BufferProject()
   416     
   417     
   417     # Add a configuration to Project
   418     # Add a configuration to Project
   418     def ProjectAddConfiguration(self, name):
   419     def ProjectAddConfiguration(self, config_name):
   419         self.Project.addConfiguration(name)
   420         self.Project.addConfiguration(config_name)
   420         self.RefreshPouUsingTree()
   421         self.RefreshPouUsingTree()
   421         self.RefreshBlockTypes()
   422         self.RefreshBlockTypes()
   422         self.BufferProject()
   423         self.BufferProject()
   423     
   424     
   424     # Remove a configuration from project
   425     # Remove a configuration from project
   425     def ProjectRemoveConfiguration(self, name):
   426     def ProjectRemoveConfiguration(self, config_name):
   426         self.Project.removeConfiguration(name)
   427         # Search if the pou removed is currently opened
       
   428         for i, element in enumerate(self.ElementsOpened):
       
   429             words = element.split("::")
       
   430             if words[0] == "C" and words[1] == config_name:
       
   431                 self.RemoveElementEditing(i)
       
   432         self.Project.removeConfiguration(config_name)
       
   433         self.BufferProject()
       
   434     
       
   435     # Add a resource to a configuration of the Project
       
   436     def ProjectAddConfigurationResource(self, config_name, resource_name):
       
   437         self.Project.addConfigurationResource(config_name, resource_name)
   427         self.RefreshPouUsingTree()
   438         self.RefreshPouUsingTree()
   428         self.RefreshBlockTypes()
   439         self.RefreshBlockTypes()
   429         self.BufferProject()
   440         self.BufferProject()
   430     
   441     
   431     # Add a resource to a configuration of the Project
       
   432     def ProjectAddConfigurationResource(self, config, name):
       
   433         self.Project.addConfigurationResource(config, name)
       
   434         self.RefreshPouUsingTree()
       
   435         self.RefreshBlockTypes()
       
   436         self.BufferProject()
       
   437     
       
   438     # Remove a resource from a configuration of the project
   442     # Remove a resource from a configuration of the project
   439     def ProjectRemoveConfigurationResource(self, config, name):
   443     def ProjectRemoveConfigurationResource(self, config_name, resource_name):
   440         self.Project.removeConfigurationResource(config, name)
   444         # Search if the pou removed is currently opened
   441         self.RefreshPouUsingTree()
   445         for i, element in enumerate(self.ElementsOpened):
   442         self.RefreshBlockTypes()
   446             words = element.split("::")
       
   447             if words[0] == "R" and words[1] == config_name and words[2] == resource_name:
       
   448                 self.RemoveElementEditing(i)
       
   449         self.Project.removeConfigurationResource(config_name, resource_name)
   443         self.BufferProject()
   450         self.BufferProject()
   444     
   451     
   445     # Add a Transition to a Project Pou
   452     # Add a Transition to a Project Pou
   446     def ProjectAddPouTransition(self, pou_name, transition_name, transition_type):
   453     def ProjectAddPouTransition(self, pou_name, transition_name, transition_type):
   447         pou = self.Project.getPou(pou_name)
   454         pou = self.Project.getPou(pou_name)
   448         pou.addTransition(transition_name, transition_type)
   455         pou.addTransition(transition_name, transition_type)
   449         self.BufferProject()
   456         self.BufferProject()
   450     
   457     
   451     # Add a Transition to a Project Pou
   458     # Remove a Transition from a Project Pou
       
   459     def ProjectRemovePouTransition(self, pou_name, transition_name):
       
   460         # Search if the pou removed is currently opened
       
   461         for i, element in enumerate(self.ElementsOpened):
       
   462             words = element.split("::")
       
   463             if words[0] == "T" and words[1] == pou_name and words[2] == transition_name:
       
   464                 self.RemoveElementEditing(i)
       
   465         pou = self.Project.getPou(pou_name)
       
   466         pou.removeTransition(transition_name)
       
   467         self.BufferProject()
       
   468     
       
   469     # Add an Action to a Project Pou
   452     def ProjectAddPouAction(self, pou_name, action_name, action_type):
   470     def ProjectAddPouAction(self, pou_name, action_name, action_type):
   453         pou = self.Project.getPou(pou_name)
   471         pou = self.Project.getPou(pou_name)
   454         pou.addAction(action_name, action_type)
   472         pou.addAction(action_name, action_type)
       
   473         self.BufferProject()
       
   474     
       
   475     # Remove an Action from a Project Pou
       
   476     def ProjectRemovePouAction(self, pou_name, action_name):
       
   477         # Search if the pou removed is currently opened
       
   478         for i, element in enumerate(self.ElementsOpened):
       
   479             words = element.split("::")
       
   480             if words[0] == "A" and words[1] == pou_name and words[2] == action_name:
       
   481                 self.RemoveElementEditing(i)
       
   482         pou = self.Project.getPou(pou_name)
       
   483         pou.removeAction(action_name)
   455         self.BufferProject()
   484         self.BufferProject()
   456         
   485         
   457     # Change the name of a pou
   486     # Change the name of a pou
   458     def ChangePouName(self, old_name, new_name):
   487     def ChangePouName(self, old_name, new_name):
   459         # Found the pou corresponding to old name and change its name to new name
   488         # Found the pou corresponding to old name and change its name to new name
   591     def GetActionBodyType(self, pou_name, pou_action):
   620     def GetActionBodyType(self, pou_name, pou_action):
   592         # Found the pou correponding to name and return its body language
   621         # Found the pou correponding to name and return its body language
   593         pou = self.Project.getPou(pou_name)
   622         pou = self.Project.getPou(pou_name)
   594         action = pou.getAction(pou_action)
   623         action = pou.getAction(pou_action)
   595         return action.getBodyType()
   624         return action.getBodyType()
   596     
       
   597     # Add a Transition to a Project Pou
       
   598     def ProjectRemovePouTransition(self, pou_name, transition_name):
       
   599         pou = self.Project.getPou(pou_name)
       
   600         pou.removeTransition(transition_name)
       
   601         self.BufferProject()
       
   602     
       
   603     # Add a Transition to a Project Pou
       
   604     def ProjectRemovePouAction(self, pou_name, action_name):
       
   605         pou = self.Project.getPou(pou_name)
       
   606         pou.removeAction(action_name)
       
   607         self.BufferProject()
       
   608     
   625     
   609     # Extract varlists from a list of vars
   626     # Extract varlists from a list of vars
   610     def ExtractVarLists(self, vars):
   627     def ExtractVarLists(self, vars):
   611         varlist_list = []
   628         varlist_list = []
   612         current_varlist = None
   629         current_varlist = None
   899         return []
   916         return []
   900 
   917 
   901     # Return Function Block types checking for recursion
   918     # Return Function Block types checking for recursion
   902     def GetFunctionBlockTypes(self):
   919     def GetFunctionBlockTypes(self):
   903         if self.CurrentElementEditing != None:
   920         if self.CurrentElementEditing != None:
       
   921             name = ""
       
   922             type = None
   904             if self.Project:
   923             if self.Project:
   905                 current_name = self.ElementsOpened[self.CurrentElementEditing]
   924                 current_name = self.ElementsOpened[self.CurrentElementEditing]
   906                 words = current_name.split("::")
   925                 words = current_name.split("::")
   907                 if len(words) == 1:
   926                 if words[0] in ["P","T","A"]:
   908                     name = current_name
   927                     if len(words) == 1:
   909                 else:
   928                         name = current_name
   910                     name = words[1]
   929                     else:
   911                 type = self.GetPouType(name)
   930                         name = words[1]
   912             else:
   931                     type = self.GetPouType(name)
   913                 name = ""
       
   914                 type = None
       
   915             blocktypes = []
   932             blocktypes = []
   916             for category in BlockTypes[:-1]:
   933             for category in BlockTypes[:-1]:
   917                 for block in category["list"]:
   934                 for block in category["list"]:
   918                     if block["type"] != "function":
   935                     if block["type"] != "function":
   919                         blocktypes.append(block["name"])
   936                         blocktypes.append(block["name"])
  1029         # Update index of current element editing
  1046         # Update index of current element editing
  1030         if len(self.ElementsOpened) > 0:
  1047         if len(self.ElementsOpened) > 0:
  1031             self.CurrentElementEditing = min(self.CurrentElementEditing, len(self.ElementsOpened) - 1)
  1048             self.CurrentElementEditing = min(self.CurrentElementEditing, len(self.ElementsOpened) - 1)
  1032         else:
  1049         else:
  1033             self.CurrentElementEditing = None
  1050             self.CurrentElementEditing = None
       
  1051 
       
  1052     # Close current element editing
       
  1053     def CloseAllElements(self):
       
  1054         # Clear the pou opened list
       
  1055         self.ElementsOpened = []
       
  1056         self.CurrentElementEditing = None
  1034 
  1057 
  1035     # Change current element editing for pou given by name
  1058     # Change current element editing for pou given by name
  1036     def ChangeElementEditing(self, name):
  1059     def ChangeElementEditing(self, name):
  1037         # Verify that element is opened
  1060         # Verify that element is opened
  1038         if name in self.ElementsOpened:
  1061         if name in self.ElementsOpened: