etisserant@0: #!/usr/bin/env python etisserant@0: # -*- coding: utf-8 -*- etisserant@0: etisserant@0: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor etisserant@0: #based on the plcopen standard. etisserant@0: # lbessard@58: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD etisserant@0: # etisserant@0: #See COPYING file for copyrights details. etisserant@0: # etisserant@0: #This library is free software; you can redistribute it and/or etisserant@5: #modify it under the terms of the GNU General Public etisserant@0: #License as published by the Free Software Foundation; either etisserant@0: #version 2.1 of the License, or (at your option) any later version. etisserant@0: # etisserant@0: #This library is distributed in the hope that it will be useful, etisserant@0: #but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU lbessard@58: #General Public License for more details. etisserant@0: # etisserant@5: #You should have received a copy of the GNU General Public etisserant@0: #License along with this library; if not, write to the Free Software etisserant@0: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: lbessard@27: from xml.dom import minidom lbessard@67: from types import StringType, UnicodeType etisserant@0: import cPickle etisserant@0: import os,sys,re lbessard@145: from time import localtime lbessard@24: from datetime import * etisserant@0: etisserant@0: from plcopen import plcopen etisserant@0: from plcopen.structures import * etisserant@0: from graphics.GraphicCommons import * etisserant@0: from PLCGenerator import * etisserant@0: lbessard@42: duration_model = re.compile("(?:([0-9]{1,2})h)?(?:([0-9]{1,2})m(?!s))?(?:([0-9]{1,2})s)?(?:([0-9]{1,3}(?:.[0-9]*)?)ms)?") lbessard@24: etisserant@0: [ITEM_UNEDITABLE, ITEM_PROJECT, ITEM_POU, ITEM_CLASS, ITEM_VARIABLE, lbessard@125: ITEM_TRANSITION, ITEM_ACTION, ITEM_CONFIGURATION, ITEM_RESOURCE, lbessard@125: ITEM_DATATYPE] = range(10) lbessard@125: lbessard@125: ScriptDirectory = os.path.split(os.path.realpath(__file__))[0] etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Undo Buffer for PLCOpenEditor etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: # Length of the buffer etisserant@0: UNDO_BUFFER_LENGTH = 20 etisserant@0: etisserant@0: """ etisserant@0: Class implementing a buffer of changes made on the current editing Object Dictionary etisserant@0: """ etisserant@0: class UndoBuffer: etisserant@0: etisserant@0: # Constructor initialising buffer etisserant@0: def __init__(self, currentstate, issaved = False): etisserant@0: self.Buffer = [] etisserant@0: self.CurrentIndex = -1 etisserant@0: self.MinIndex = -1 etisserant@0: self.MaxIndex = -1 etisserant@0: # if current state is defined etisserant@0: if currentstate: etisserant@0: self.CurrentIndex = 0 etisserant@0: self.MinIndex = 0 etisserant@0: self.MaxIndex = 0 etisserant@0: # Initialising buffer with currentstate at the first place etisserant@0: for i in xrange(UNDO_BUFFER_LENGTH): etisserant@0: if i == 0: etisserant@0: self.Buffer.append(currentstate) etisserant@0: else: etisserant@0: self.Buffer.append(None) etisserant@0: # Initialising index of state saved etisserant@0: if issaved: etisserant@0: self.LastSave = 0 etisserant@0: else: etisserant@0: self.LastSave = -1 etisserant@0: etisserant@0: # Add a new state in buffer etisserant@0: def Buffering(self, currentstate): etisserant@0: self.CurrentIndex = (self.CurrentIndex + 1) % UNDO_BUFFER_LENGTH etisserant@0: self.Buffer[self.CurrentIndex] = currentstate etisserant@0: # Actualising buffer limits etisserant@0: self.MaxIndex = self.CurrentIndex etisserant@0: if self.MinIndex == self.CurrentIndex: etisserant@0: # If the removed state was the state saved, there is no state saved in the buffer etisserant@0: if self.LastSave == self.MinIndex: etisserant@0: self.LastSave = -1 etisserant@0: self.MinIndex = (self.MinIndex + 1) % UNDO_BUFFER_LENGTH etisserant@0: self.MinIndex = max(self.MinIndex, 0) etisserant@0: etisserant@0: # Return current state of buffer etisserant@0: def Current(self): etisserant@0: return self.Buffer[self.CurrentIndex] etisserant@0: etisserant@0: # Change current state to previous in buffer and return new current state etisserant@0: def Previous(self): etisserant@0: if self.CurrentIndex != self.MinIndex: etisserant@0: self.CurrentIndex = (self.CurrentIndex - 1) % UNDO_BUFFER_LENGTH etisserant@0: return self.Buffer[self.CurrentIndex] etisserant@0: return None etisserant@0: etisserant@0: # Change current state to next in buffer and return new current state etisserant@0: def Next(self): etisserant@0: if self.CurrentIndex != self.MaxIndex: etisserant@0: self.CurrentIndex = (self.CurrentIndex + 1) % UNDO_BUFFER_LENGTH etisserant@0: return self.Buffer[self.CurrentIndex] etisserant@0: return None etisserant@0: etisserant@0: # Return True if current state is the first in buffer etisserant@0: def IsFirst(self): etisserant@0: return self.CurrentIndex == self.MinIndex etisserant@0: etisserant@0: # Return True if current state is the last in buffer etisserant@0: def IsLast(self): etisserant@0: return self.CurrentIndex == self.MaxIndex etisserant@0: etisserant@0: # Note that current state is saved etisserant@0: def CurrentSaved(self): etisserant@0: self.LastSave = self.CurrentIndex etisserant@0: etisserant@0: # Return True if current state is saved etisserant@0: def IsCurrentSaved(self): etisserant@0: return self.LastSave == self.CurrentIndex etisserant@0: etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Controler for PLCOpenEditor etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class which controls the operations made on the plcopen model and answers to view requests etisserant@0: """ etisserant@0: class PLCControler: etisserant@0: etisserant@0: # Create a new PLCControler etisserant@0: def __init__(self): etisserant@0: self.LastNewIndex = 0 etisserant@0: self.Reset() etisserant@0: etisserant@0: # Reset PLCControler internal variables etisserant@0: def Reset(self): etisserant@0: self.Project = None etisserant@0: self.ProjectBuffer = None lbessard@56: self.Buffering = False etisserant@0: self.FilePath = "" etisserant@0: self.FileName = "" lbessard@42: self.ProgramFilePath = "" lbessard@125: self.RefreshDataTypeUsingTree() lbessard@125: self.RefreshDataTypes() etisserant@0: self.RefreshPouUsingTree() etisserant@0: self.RefreshBlockTypes() etisserant@0: etisserant@0: def GetQualifierTypes(self): etisserant@0: return plcopen.QualifierList etisserant@0: etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Project management functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: # Return if a project is opened etisserant@0: def HasOpenedProject(self): etisserant@0: return self.Project != None etisserant@0: etisserant@0: # Create a new project by replacing the current one lbessard@145: def CreateNewProject(self, properties): etisserant@0: # Create the project etisserant@0: self.Project = plcopen.project() lbessard@145: properties["creationDateTime"] = datetime(*localtime()[:6]) lbessard@151: self.Project.setfileHeader(properties) lbessard@151: self.Project.setcontentHeader(properties) lbessard@56: self.SetFilePath("") etisserant@0: # Initialize the project buffer lbessard@56: self.ProjectBuffer = UndoBuffer(self.Copy(self.Project), False) lbessard@56: self.Buffering = False etisserant@0: lbessard@125: # Return project data type names lbessard@125: def GetProjectDataTypeNames(self): lbessard@125: if self.Project: lbessard@151: return [datatype.getname() for datatype in self.Project.getdataTypes()] lbessard@125: return [] lbessard@125: etisserant@0: # Return project pou names etisserant@0: def GetProjectPouNames(self): lbessard@80: if self.Project: lbessard@151: return [pou.getname() for pou in self.Project.getpous()] lbessard@80: return [] etisserant@0: etisserant@0: # Return project pou names etisserant@0: def GetProjectConfigNames(self): lbessard@80: if self.Project: lbessard@151: return [config.getName() for config in self.Project.getconfigurations()] lbessard@80: return [] etisserant@0: lbessard@6: # Return project pou variables lbessard@6: def GetProjectPouVariables(self, pou_name=None): lbessard@6: variables = [] lbessard@151: for pou in self.Project.getpous(): lbessard@151: if not pou_name or pou_name == pou.getname(): lbessard@6: variables.extend([var["Name"] for var in self.GetPouInterfaceVars(pou)]) lbessard@151: for transition in pou.gettransitionList(): lbessard@151: variables.append(transition.getname()) lbessard@151: for action in pou.getactionList(): lbessard@151: variables.append(action.getname()) lbessard@6: return variables lbessard@6: lbessard@56: # Return if project is saved lbessard@56: def ProjectIsSaved(self): lbessard@56: return self.ProjectBuffer.IsCurrentSaved() lbessard@56: etisserant@0: # Return file path if project is an open file etisserant@0: def GetFilePath(self): etisserant@0: return self.FilePath etisserant@0: lbessard@42: # Return file path if project is an open file lbessard@42: def GetProgramFilePath(self): lbessard@42: return self.ProgramFilePath lbessard@42: etisserant@0: # Return file name and point out if file is up to date etisserant@0: def GetFilename(self): etisserant@0: if self.ProjectBuffer.IsCurrentSaved(): etisserant@0: return self.FileName etisserant@0: else: etisserant@0: return "~%s~"%self.FileName etisserant@0: etisserant@0: # Change file path and save file name or create a default one if file path not defined etisserant@0: def SetFilePath(self, filepath): etisserant@0: self.FilePath = filepath etisserant@0: if filepath == "": etisserant@0: self.LastNewIndex += 1 etisserant@0: self.FileName = "Unnamed%d"%self.LastNewIndex etisserant@0: else: etisserant@0: self.FileName = os.path.splitext(os.path.basename(filepath))[0] etisserant@0: etisserant@0: # Change project properties lbessard@56: def SetProjectProperties(self, name = None, properties = None): lbessard@56: if name != None: lbessard@151: self.Project.setname(name) lbessard@56: if properties != None: lbessard@151: self.Project.setfileHeader(properties) lbessard@151: self.Project.setcontentHeader(properties) lbessard@56: if name != None or properties != None: lbessard@56: self.BufferProject() lbessard@56: etisserant@0: # Return project properties etisserant@0: def GetProjectProperties(self): lbessard@151: properties = self.Project.getfileHeader() lbessard@151: properties.update(self.Project.getcontentHeader()) lbessard@56: return properties etisserant@0: etisserant@0: # Return project informations etisserant@0: def GetProjectInfos(self): etisserant@0: if self.Project: lbessard@151: infos = {"name": self.Project.getname(), "type": ITEM_PROJECT} lbessard@125: datatypes = {"name": "Data Types", "type": ITEM_UNEDITABLE, "values":[]} lbessard@151: for datatype in self.Project.getdataTypes(): lbessard@151: datatypes["values"].append({"name": datatype.getname(), "type": ITEM_DATATYPE, "values": []}) etisserant@0: pou_types = {"function": {"name": "Functions", "type": ITEM_UNEDITABLE, "values":[]}, etisserant@0: "functionBlock": {"name": "Function Blocks", "type": ITEM_UNEDITABLE, "values":[]}, etisserant@0: "program": {"name": "Programs", "type": ITEM_UNEDITABLE, "values":[]}} lbessard@151: for pou in self.Project.getpous(): lbessard@151: pou_type = pou.getpouType() lbessard@151: pou_infos = {"name": pou.getname(), "type": ITEM_POU} etisserant@0: pou_values = [] lbessard@151: if pou.getbodyType() == "SFC": etisserant@0: transitions = [] lbessard@151: for transition in pou.gettransitionList(): lbessard@151: transitions.append({"name": transition.getname(), "type": ITEM_TRANSITION, "values": []}) etisserant@0: pou_values.append({"name": "Transitions", "type": ITEM_UNEDITABLE, "values": transitions}) etisserant@0: actions = [] lbessard@151: for action in pou.getactionList(): lbessard@151: actions.append({"name": action.getname(), "type": ITEM_ACTION, "values": []}) etisserant@0: pou_values.append({"name": "Actions", "type": ITEM_UNEDITABLE, "values": actions}) etisserant@0: if pou_type in pou_types: etisserant@0: pou_infos["values"] = pou_values etisserant@0: pou_types[pou_type]["values"].append(pou_infos) etisserant@0: configurations = {"name": "Configurations", "type": ITEM_UNEDITABLE, "values": []} lbessard@151: for config in self.Project.getconfigurations(): lbessard@151: config_name = config.getname() etisserant@0: config_infos = {"name": config_name, "type": ITEM_CONFIGURATION, "values": []} etisserant@0: resources = {"name": "Resources", "type": ITEM_UNEDITABLE, "values": []} lbessard@151: for resource in config.getresource(): lbessard@151: resource_name = resource.getname() etisserant@0: resource_infos = {"name": resource_name, "type": ITEM_RESOURCE, "values": []} etisserant@0: resources["values"].append(resource_infos) lbessard@47: config_infos["values"] = [resources] etisserant@0: configurations["values"].append(config_infos) etisserant@0: infos["values"] = [{"name": "Properties", "type": ITEM_UNEDITABLE, "values": []}, lbessard@125: datatypes, pou_types["function"], pou_types["functionBlock"], etisserant@0: pou_types["program"], configurations] etisserant@0: return infos etisserant@0: return None etisserant@0: lbessard@125: # Refresh the tree of user-defined data type cross-use lbessard@125: def RefreshDataTypeUsingTree(self): lbessard@125: # Reset the tree of user-defined pou cross-use lbessard@125: self.DataTypeUsingTree = {} lbessard@125: if self.Project: lbessard@151: datatypes = self.Project.getdataTypes() lbessard@125: # Reference all the user-defined data type names and initialize the tree of lbessard@125: # user-defined data type cross-use lbessard@151: datatypenames = [datatype.getname() for datatype in datatypes] lbessard@125: for name in datatypenames: lbessard@125: self.DataTypeUsingTree[name] = [] lbessard@125: # Analyze each data type lbessard@125: for datatype in datatypes: lbessard@151: name = datatype.getname() lbessard@151: basetype_content = datatype.getbaseType().getcontent() lbessard@141: if basetype_content["name"] == "derived": lbessard@151: basetype_name = basetype_content["value"].getname() lbessard@141: if basetype_name in datatypenames and name not in self.DataTypeUsingTree[basetype_name]: lbessard@141: self.DataTypeUsingTree[basetype_name].append(name) lbessard@141: elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned", "array"]: lbessard@151: base_type = basetype_content["value"].baseType.getcontent() lbessard@141: if base_type["value"] is not None: lbessard@151: basetype_name = base_type["value"].getname() lbessard@125: if basetype_name in datatypenames and name not in self.DataTypeUsingTree[basetype_name]: lbessard@125: self.DataTypeUsingTree[basetype_name].append(name) lbessard@125: etisserant@0: # Refresh the tree of user-defined pou cross-use etisserant@0: def RefreshPouUsingTree(self): etisserant@0: # Reset the tree of user-defined pou cross-use etisserant@0: self.PouUsingTree = {} etisserant@0: if self.Project: lbessard@151: pous = self.Project.getpous() etisserant@0: # Reference all the user-defined pou names and initialize the tree of etisserant@0: # user-defined pou cross-use lbessard@151: pounames = [pou.getname() for pou in pous] etisserant@0: for name in pounames: etisserant@0: self.PouUsingTree[name] = [] etisserant@0: # Analyze each pou etisserant@0: for pou in pous: lbessard@151: name = pou.getname() lbessard@71: if pou.interface: lbessard@71: # Extract variables from every varLists lbessard@151: for type, varlist in pou.getvars(): lbessard@151: for var in varlist.getvariable(): lbessard@151: vartype_content = var.gettype().getcontent() lbessard@141: if vartype_content["name"] == "derived": lbessard@151: typename = vartype_content["value"].getname() lbessard@71: if typename in pounames and name not in self.PouUsingTree[typename]: lbessard@71: self.PouUsingTree[typename].append(name) lbessard@151: bodytype = pou.getbodyType() etisserant@0: # If pou is written in a graphical language etisserant@0: if bodytype in ["FBD","LD","SFC"]: etisserant@0: # Analyze each instance of the pou lbessard@151: for instance in pou.getinstances(): lbessard@151: if isinstance(instance, plcopen.fbdObjects_block): lbessard@151: typename = instance.gettypeName() etisserant@0: # Update tree if there is a cross-use etisserant@0: if typename in pounames and name not in self.PouUsingTree[typename]: etisserant@0: self.PouUsingTree[typename].append(name) etisserant@0: # If pou is written in a textual language etisserant@0: elif bodytype in ["IL", "ST"]: lbessard@151: text = pou.gettext() etisserant@0: # Search if each pou is mentioned in the pou text etisserant@0: for typename in pounames: etisserant@0: typename_model = re.compile("[ \t\n]%s[ \t\n]"%typename) etisserant@0: # Update tree if there is a cross-use etisserant@0: if typename != name and typename_model.search(text): etisserant@0: self.PouUsingTree[typename].append(name) etisserant@0: lbessard@125: # Return if data type given by name is used by another data type or pou lbessard@125: def DataTypeIsUsed(self, name): lbessard@125: if name in self.DataTypeUsingTree: lbessard@125: return len(self.DataTypeUsingTree[name]) > 0 lbessard@125: return False lbessard@125: etisserant@0: # Return if pou given by name is used by another pou etisserant@0: def PouIsUsed(self, name): etisserant@0: if name in self.PouUsingTree: etisserant@0: return len(self.PouUsingTree[name]) > 0 etisserant@0: return False etisserant@0: lbessard@125: # Return if data type given by name is directly or undirectly used by the reference data type lbessard@125: def DataTypeIsUsedBy(self, name, reference): lbessard@125: if name in self.DataTypeUsingTree: lbessard@125: list = self.DataTypeUsingTree[name] lbessard@125: # Test if data type is directly used by reference lbessard@125: if reference in list: lbessard@125: return True lbessard@125: else: lbessard@125: # Test if data type is undirectly used by reference, by testing if data types lbessard@125: # that directly use data type is directly or undirectly used by reference lbessard@125: used = False lbessard@125: for element in list: lbessard@125: used |= self.DataTypeIsUsedBy(element, reference) lbessard@125: return used lbessard@125: return False lbessard@125: etisserant@0: # Return if pou given by name is directly or undirectly used by the reference pou etisserant@0: def PouIsUsedBy(self, name, reference): etisserant@0: if name in self.PouUsingTree: etisserant@0: list = self.PouUsingTree[name] etisserant@0: # Test if pou is directly used by reference etisserant@0: if reference in list: etisserant@0: return True etisserant@0: else: etisserant@0: # Test if pou is undirectly used by reference, by testing if pous etisserant@0: # that directly use pou is directly or undirectly used by reference etisserant@0: used = False etisserant@0: for element in list: etisserant@0: used |= self.PouIsUsedBy(element, reference) etisserant@0: return used etisserant@0: return False etisserant@0: lbessard@4: def GenerateProgram(self, filepath): etisserant@0: if self.Project: lbessard@97: try: lbessard@97: program = GenerateCurrentProgram(self.Project) lbessard@97: programfile = open(filepath, "w") lbessard@97: programfile.write(program) lbessard@97: programfile.close() lbessard@97: self.ProgramFilePath = filepath lbessard@97: return True lbessard@97: except: lbessard@97: pass lbessard@4: return False etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Project Pous management functions etisserant@0: #------------------------------------------------------------------------------- lbessard@125: lbessard@125: # Add a Data Type to Project lbessard@125: def ProjectAddDataType(self, datatype_name): lbessard@125: # Add the pou to project lbessard@151: self.Project.appenddataType(datatype_name) lbessard@125: self.RefreshDataTypeUsingTree() lbessard@125: self.RefreshDataTypes() lbessard@125: self.BufferProject() lbessard@125: lbessard@125: # Remove a Data Type from project lbessard@125: def ProjectRemoveDataType(self, datatype_name): lbessard@151: self.Project.removedataType(datatype_name) lbessard@125: self.RefreshDataTypeUsingTree() lbessard@125: self.RefreshDataTypes() lbessard@125: self.BufferProject() lbessard@125: etisserant@0: # Add a Pou to Project lbessard@107: def ProjectAddPou(self, pou_name, pou_type, body_type): etisserant@0: # Add the pou to project lbessard@151: self.Project.appendpou(pou_name, pou_type, body_type) lbessard@107: if pou_type == "function": lbessard@107: self.SetPouInterfaceReturnType(pou_name, "BOOL") etisserant@0: self.RefreshPouUsingTree() etisserant@0: self.RefreshBlockTypes() lbessard@56: self.BufferProject() etisserant@0: lbessard@125: # Remove a Pou from project lbessard@107: def ProjectRemovePou(self, pou_name): lbessard@151: self.Project.removepou(pou_name) etisserant@0: self.RefreshPouUsingTree() etisserant@0: self.RefreshBlockTypes() lbessard@56: self.BufferProject() etisserant@0: etisserant@0: # Add a configuration to Project lbessard@107: def ProjectAddConfiguration(self, config_name): lbessard@151: self.Project.addconfiguration(config_name) lbessard@56: self.BufferProject() etisserant@0: etisserant@0: # Remove a configuration from project lbessard@107: def ProjectRemoveConfiguration(self, config_name): lbessard@151: self.Project.removeconfiguration(config_name) lbessard@107: self.BufferProject() lbessard@107: lbessard@107: # Add a resource to a configuration of the Project lbessard@107: def ProjectAddConfigurationResource(self, config_name, resource_name): lbessard@151: self.Project.addconfigurationResource(config_name, resource_name) lbessard@56: self.BufferProject() etisserant@0: etisserant@0: # Remove a resource from a configuration of the project lbessard@107: def ProjectRemoveConfigurationResource(self, config_name, resource_name): lbessard@151: self.Project.removeconfigurationResource(config_name, resource_name) lbessard@56: self.BufferProject() etisserant@0: etisserant@0: # Add a Transition to a Project Pou etisserant@0: def ProjectAddPouTransition(self, pou_name, transition_name, transition_type): lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: pou.addtransition(transition_name, transition_type) lbessard@56: self.BufferProject() etisserant@0: lbessard@107: # Remove a Transition from a Project Pou lbessard@107: def ProjectRemovePouTransition(self, pou_name, transition_name): lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: pou.removetransition(transition_name) lbessard@107: self.BufferProject() lbessard@107: lbessard@107: # Add an Action to a Project Pou etisserant@0: def ProjectAddPouAction(self, pou_name, action_name, action_type): lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: pou.addaction(action_name, action_type) lbessard@56: self.BufferProject() lbessard@107: lbessard@107: # Remove an Action from a Project Pou lbessard@107: def ProjectRemovePouAction(self, pou_name, action_name): lbessard@107: # Search if the pou removed is currently opened lbessard@107: for i, element in enumerate(self.ElementsOpened): lbessard@107: words = element.split("::") lbessard@107: if words[0] == "A" and words[1] == pou_name and words[2] == action_name: lbessard@107: self.RemoveElementEditing(i) lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: pou.removeaction(action_name) lbessard@107: self.BufferProject() lbessard@125: lbessard@125: # Change the name of a pou lbessard@125: def ChangeDataTypeName(self, old_name, new_name): lbessard@125: # Found the pou corresponding to old name and change its name to new name lbessard@151: datatype = self.Project.getdataType(old_name) lbessard@151: datatype.setname(new_name) lbessard@125: self.Project.updateElementName(old_name, new_name) lbessard@125: self.BufferProject() lbessard@125: etisserant@0: # Change the name of a pou etisserant@0: def ChangePouName(self, old_name, new_name): etisserant@0: # Found the pou corresponding to old name and change its name to new name lbessard@151: pou = self.Project.getpou(old_name) lbessard@151: pou.setname(new_name) lbessard@58: self.Project.updateElementName(old_name, new_name) etisserant@0: self.RefreshPouUsingTree() etisserant@0: self.RefreshBlockTypes() lbessard@56: self.BufferProject() etisserant@0: etisserant@0: # Change the name of a pou transition etisserant@0: def ChangePouTransitionName(self, pou_name, old_name, new_name): etisserant@0: # Found the pou transition corresponding to old name and change its name to new name lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: transition = pou.gettransition(old_name) lbessard@151: transition.setname(new_name) lbessard@108: pou.updateElementName(old_name, new_name) lbessard@56: self.BufferProject() etisserant@0: etisserant@0: # Change the name of a pou action etisserant@0: def ChangePouActionName(self, pou_name, old_name, new_name): etisserant@0: # Found the pou action corresponding to old name and change its name to new name lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: action = pou.getaction(old_name) lbessard@151: action.setname(new_name) lbessard@108: pou.updateElementName(old_name, new_name) lbessard@56: self.BufferProject() lbessard@56: lbessard@80: # Change the name of a pou variable lbessard@6: def ChangePouVariableName(self, pou_name, old_name, new_name): lbessard@6: # Found the pou action corresponding to old name and change its name to new name lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: for type, varlist in pou.getvars(): lbessard@151: for var in varlist.getvariable(): lbessard@151: if var.getname() == old_name: lbessard@151: var.setname(new_name) lbessard@6: self.RefreshBlockTypes() lbessard@56: self.BufferProject() lbessard@6: etisserant@0: # Change the name of a configuration etisserant@0: def ChangeConfigurationName(self, old_name, new_name): etisserant@0: # Found the configuration corresponding to old name and change its name to new name lbessard@151: configuration = self.Project.getconfiguration(old_name) lbessard@151: configuration.setname(new_name) lbessard@56: self.BufferProject() lbessard@56: etisserant@0: # Change the name of a configuration resource etisserant@0: def ChangeConfigurationResourceName(self, config_name, old_name, new_name): etisserant@0: # Found the resource corresponding to old name and change its name to new name lbessard@151: resource = self.Project.getconfigurationResource(config_name) etisserant@0: resource.setName(new_name) lbessard@56: self.BufferProject() etisserant@0: etisserant@0: # Return the type of the pou given by its name etisserant@0: def GetPouType(self, name): etisserant@0: # Found the pou correponding to name and return its type lbessard@151: pou = self.Project.getpou(name) lbessard@151: return pou.getpouType() etisserant@0: etisserant@0: # Return pous with SFC language etisserant@0: def GetSFCPous(self): etisserant@0: list = [] etisserant@0: if self.Project: lbessard@151: for pou in self.Project.getpous(): etisserant@0: if pou.getBodyType() == "SFC": lbessard@151: list.append(pou.getname()) etisserant@0: return list etisserant@0: etisserant@0: # Return the body language of the pou given by its name etisserant@0: def GetPouBodyType(self, name): etisserant@0: # Found the pou correponding to name and return its body language lbessard@151: pou = self.Project.getpou(name) lbessard@151: return pou.getbodyType() etisserant@0: lbessard@46: # Return the actions of a pou lbessard@46: def GetPouTransitions(self, pou_name): lbessard@46: transitions = [] lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: if pou.getbodyType() == "SFC": lbessard@151: for transition in pou.gettransitionList(): lbessard@151: transitions.append(transition.getname()) lbessard@46: return transitions lbessard@46: etisserant@0: # Return the body language of the transition given by its name etisserant@0: def GetTransitionBodyType(self, pou_name, pou_transition): etisserant@0: # Found the pou correponding to name and return its body language lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: transition = pou.gettransition(pou_transition) lbessard@151: return transition.getbodyType() etisserant@0: lbessard@46: # Return the actions of a pou lbessard@46: def GetPouActions(self, pou_name): lbessard@46: actions = [] lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: if pou.getbodyType() == "SFC": lbessard@151: for action in pou.getactionList(): lbessard@151: actions.append(action.getname()) lbessard@46: return actions lbessard@46: etisserant@0: # Return the body language of the pou given by its name etisserant@0: def GetActionBodyType(self, pou_name, pou_action): etisserant@0: # Found the pou correponding to name and return its body language lbessard@151: pou = self.Project.getpou(pou_name) lbessard@151: action = pou.getaction(pou_action) lbessard@151: return action.getbodyType() etisserant@0: etisserant@0: # Extract varlists from a list of vars etisserant@0: def ExtractVarLists(self, vars): etisserant@0: varlist_list = [] etisserant@0: current_varlist = None etisserant@0: current_type = None etisserant@0: for var in vars: lbessard@34: if current_type != (var["Class"], var["Retain"], var["Constant"], var["Location"] in ["", None]): lbessard@34: current_type = (var["Class"], var["Retain"], var["Constant"], var["Location"] in ["", None]) lbessard@151: if var["Class"] == "Local": lbessard@151: current_varlist = plcopen.interface_localVars() lbessard@151: elif var["Class"] == "Global": lbessard@151: current_varlist = plcopen.interface_globalVars() lbessard@151: elif var["Class"] == "External": lbessard@151: current_varlist = plcopen.interface_externalVars() lbessard@151: elif var["Class"] == "Temp": lbessard@151: current_varlist = plcopen.interface_tempVars() lbessard@151: elif var["Class"] == "Input": lbessard@151: current_varlist = plcopen.interface_inputVars() lbessard@151: elif var["Class"] == "Output": lbessard@151: current_varlist = plcopen.interface_outputVars() lbessard@151: elif var["Class"] == "InOut": lbessard@151: current_varlist = plcopen.interface_inOutVars() lbessard@151: else: lbessard@151: current_varlist = plcopen.varList() etisserant@0: varlist_list.append((var["Class"], current_varlist)) etisserant@0: if var["Retain"] == "Yes": lbessard@151: current_varlist.setretain(True) etisserant@0: if var["Constant"] == "Yes": lbessard@151: current_varlist.setconstant(True) etisserant@0: # Create variable and change its properties etisserant@0: tempvar = plcopen.varListPlain_variable() lbessard@151: tempvar.setname(var["Name"]) etisserant@0: var_type = plcopen.dataType() lbessard@151: if var["Type"] in self.GetBaseTypes(): lbessard@141: if var["Type"] == "STRING": lbessard@151: var_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) lbessard@141: elif var["Type"] == "WSTRING": lbessard@151: var_type.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()}) lbessard@141: else: lbessard@151: var_type.setcontent({"name" : var["Type"], "value" : None}) lbessard@67: else: lbessard@151: derived_type = plcopen.derivedTypes_derived() lbessard@151: derived_type.setname(var["Type"]) lbessard@151: var_type.setcontent({"name" : "derived", "value" : derived_type}) lbessard@151: tempvar.settype(var_type) etisserant@0: if var["Initial Value"] != "": etisserant@0: value = plcopen.value() lbessard@151: value.setvalue(var["Initial Value"]) lbessard@151: tempvar.setinitialValue(value) etisserant@0: if var["Location"] != "": lbessard@151: tempvar.setaddress(var["Location"]) lbessard@53: else: lbessard@151: tempvar.setaddress(None) etisserant@0: # Add variable to varList lbessard@151: current_varlist.appendvariable(tempvar) etisserant@0: return varlist_list etisserant@0: etisserant@0: # Replace the configuration globalvars by those given etisserant@0: def SetConfigurationGlobalVars(self, name, vars): etisserant@0: # Found the configuration corresponding to name lbessard@151: configuration = self.Project.getconfiguration(name) etisserant@0: if configuration: etisserant@0: # Set configuration global vars lbessard@151: configuration.setglobalVars([]) etisserant@0: for vartype, varlist in self.ExtractVarLists(vars): etisserant@0: configuration.globalVars.append(varlist) etisserant@0: self.RefreshBlockTypes() lbessard@56: etisserant@0: # Return the configuration globalvars etisserant@0: def GetConfigurationGlobalVars(self, name): etisserant@0: vars = [] etisserant@0: # Found the configuration corresponding to name lbessard@151: configuration = self.Project.getconfiguration(name) etisserant@0: if configuration: etisserant@0: # Extract variables from every varLists lbessard@151: for varlist in configuration.getglobalVars(): lbessard@151: for var in varlist.getvariable(): lbessard@151: tempvar = {"Name" : var.getname(), "Class" : "Global"} lbessard@151: vartype_content = var.gettype().getcontent() lbessard@141: if vartype_content["name"] == "derived": lbessard@151: tempvar["Type"] = vartype_content["value"].getname() lbessard@141: elif vartype_content["name"] in ["string", "wstring"]: lbessard@141: tempvar["Type"] = vartype_content["name"].upper() lbessard@141: else: lbessard@125: tempvar["Type"] = vartype_content["name"] lbessard@68: tempvar["Edit"] = True lbessard@151: initial = var.getinitialValue() etisserant@0: if initial: lbessard@151: tempvar["Initial Value"] = initial.getvalue() etisserant@0: else: etisserant@0: tempvar["Initial Value"] = "" lbessard@151: address = var.getaddress() lbessard@53: if address: lbessard@53: tempvar["Location"] = address lbessard@53: else: lbessard@53: tempvar["Location"] = "" lbessard@151: if varlist.getretain(): etisserant@0: tempvar["Retain"] = "Yes" etisserant@0: else: etisserant@0: tempvar["Retain"] = "No" lbessard@151: if varlist.getconstant(): etisserant@0: tempvar["Constant"] = "Yes" etisserant@0: else: etisserant@0: tempvar["Constant"] = "No" etisserant@0: vars.append(tempvar) etisserant@0: return vars etisserant@0: etisserant@0: # Replace the resource globalvars by those given etisserant@0: def SetConfigurationResourceGlobalVars(self, config_name, name, vars): etisserant@0: # Found the resource corresponding to name lbessard@151: resource = self.Project.getconfigurationResource(config_name, name) etisserant@0: # Set resource global vars etisserant@0: if resource: lbessard@151: resource.setglobalVars([]) etisserant@0: for vartype, varlist in self.ExtractVarLists(vars): etisserant@0: resource.globalVars.append(varlist) etisserant@0: self.RefreshBlockTypes() lbessard@56: etisserant@0: # Return the resource globalvars etisserant@0: def GetConfigurationResourceGlobalVars(self, config_name, name): etisserant@0: vars = [] etisserant@0: # Found the resource corresponding to name lbessard@151: resource = self.Project.getconfigurationResource(config_name, name) etisserant@0: if resource: etisserant@0: # Extract variables from every varLists lbessard@151: for varlist in resource.getglobalVars(): lbessard@151: for var in varlist.getvariable(): lbessard@151: tempvar = {"Name" : var.getname(), "Class" : "Global"} lbessard@151: vartype_content = var.gettype().getcontent() lbessard@141: if vartype_content["name"] == "derived": lbessard@151: tempvar["Type"] = vartype_content["value"].getname() lbessard@141: elif vartype_content["name"] in ["string", "wstring"]: lbessard@141: tempvar["Type"] = vartype_content["name"].upper() lbessard@141: else: lbessard@125: tempvar["Type"] = vartype_content["name"] lbessard@68: tempvar["Edit"] = True lbessard@151: initial = var.getinitialValue() etisserant@0: if initial: lbessard@151: tempvar["Initial Value"] = initial.getvalue() etisserant@0: else: etisserant@0: tempvar["Initial Value"] = "" lbessard@151: address = var.getaddress() lbessard@53: if address: lbessard@53: tempvar["Location"] = address lbessard@53: else: lbessard@53: tempvar["Location"] = "" lbessard@151: if varlist.getretain(): etisserant@0: tempvar["Retain"] = "Yes" etisserant@0: else: etisserant@0: tempvar["Retain"] = "No" lbessard@151: if varlist.getconstant(): etisserant@0: tempvar["Constant"] = "Yes" etisserant@0: else: etisserant@0: tempvar["Constant"] = "No" etisserant@0: vars.append(tempvar) etisserant@0: return vars etisserant@0: etisserant@0: # Return the interface of the pou given by its name etisserant@0: def GetPouInterfaceVarsByName(self, name): etisserant@0: # Found the pou correponding to name and return the interface lbessard@151: return self.GetPouInterfaceVars(self.Project.getpou(name)) etisserant@0: etisserant@0: # Return the interface for the given pou etisserant@0: def GetPouInterfaceVars(self, pou): etisserant@0: vars = [] etisserant@0: # Verify that the pou has an interface etisserant@0: if pou.interface: etisserant@0: # Extract variables from every varLists lbessard@151: for type, varlist in pou.getvars(): lbessard@151: for var in varlist.getvariable(): lbessard@151: tempvar = {"Name" : var.getname(), "Class" : type} lbessard@151: vartype_content = var.gettype().getcontent() lbessard@141: if vartype_content["name"] == "derived": lbessard@151: tempvar["Type"] = vartype_content["value"].getname() lbessard@151: tempvar["Edit"] = not pou.hasblock(tempvar["Name"]) lbessard@141: else: lbessard@141: if vartype_content["name"] in ["string", "wstring"]: lbessard@141: tempvar["Type"] = vartype_content["name"].upper() lbessard@141: else: lbessard@141: tempvar["Type"] = vartype_content["name"] lbessard@141: tempvar["Edit"] = True lbessard@151: initial = var.getinitialValue() etisserant@0: if initial: lbessard@151: tempvar["Initial Value"] = initial.getvalue() etisserant@0: else: etisserant@0: tempvar["Initial Value"] = "" lbessard@151: address = var.getaddress() lbessard@53: if address: lbessard@53: tempvar["Location"] = address lbessard@53: else: lbessard@53: tempvar["Location"] = "" lbessard@151: if varlist.getretain(): etisserant@0: tempvar["Retain"] = "Yes" etisserant@0: else: etisserant@0: tempvar["Retain"] = "No" lbessard@151: if varlist.getconstant(): etisserant@0: tempvar["Constant"] = "Yes" etisserant@0: else: etisserant@0: tempvar["Constant"] = "No" etisserant@0: vars.append(tempvar) etisserant@0: return vars etisserant@0: etisserant@0: # Replace the Pou interface by the one given etisserant@0: def SetPouInterfaceVars(self, name, vars): etisserant@0: # Found the pou corresponding to name and add interface if there isn't one yet lbessard@151: pou = self.Project.getpou(name) etisserant@0: if not pou.interface: etisserant@0: pou.interface = plcopen.pou_interface() etisserant@0: # Set Pou interface lbessard@151: pou.setvars(self.ExtractVarLists(vars)) lbessard@71: self.RefreshPouUsingTree() etisserant@0: self.RefreshBlockTypes() lbessard@56: etisserant@0: # Replace the return type of the pou given by its name (only for functions) etisserant@0: def SetPouInterfaceReturnType(self, name, type): lbessard@151: pou = self.Project.getpou(name) etisserant@0: if not pou.interface: etisserant@0: pou.interface = plcopen.pou_interface() etisserant@0: # If there isn't any return type yet, add it lbessard@151: return_type = pou.interface.getreturnType() etisserant@0: if not return_type: etisserant@0: return_type = plcopen.dataType() lbessard@151: pou.interface.setreturnType(return_type) etisserant@0: # Change return type lbessard@125: if type in self.GetBaseTypes(): lbessard@151: if type == "STRING": lbessard@151: return_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) lbessard@151: elif type == "WSTRING": lbessard@151: return_type.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()}) lbessard@151: else: lbessard@151: return_type.setcontent({"name" : type, "value" : None}) lbessard@125: else: lbessard@151: derived_type = plcopen.derivedTypes_derived() lbessard@151: derived_type.setname(type) lbessard@151: return_type.setcontent({"name" : "derived", "value" : derived_type}) etisserant@0: self.RefreshBlockTypes() lbessard@58: lbessard@58: def UpdateProjectUsedPous(self, old_name, new_name): lbessard@58: if self.Project: lbessard@58: self.Project.updateElementName(old_name, new_name) lbessard@58: lbessard@121: def UpdateEditedElementUsedVariable(self, tagname, old_name, new_name): lbessard@121: pou = self.GetEditedElement(tagname) lbessard@58: if pou: lbessard@58: pou.updateElementName(old_name, new_name) etisserant@0: etisserant@0: # Return the return type of the pou given by its name etisserant@0: def GetPouInterfaceReturnTypeByName(self, name): etisserant@0: # Found the pou correponding to name and return the return type lbessard@151: return self.GetPouInterfaceReturnType(self.Project.getpou(name)) etisserant@0: etisserant@0: # Return the return type of the given pou etisserant@0: def GetPouInterfaceReturnType(self, pou): etisserant@0: # Verify that the pou has an interface etisserant@0: if pou.interface: etisserant@0: # Return the return type if there is one lbessard@151: return_type = pou.interface.getreturnType() etisserant@0: if return_type: lbessard@151: returntype_content = return_type.getcontent() lbessard@141: if returntype_content["name"] == "derived": lbessard@151: return returntype_content["value"].getname() lbessard@141: elif returntype_content["name"] in ["string", "wstring"]: lbessard@141: return returntype_content["name"].upper() lbessard@141: else: lbessard@125: return returntype_content["name"] etisserant@0: return None lbessard@125: lbessard@125: # Update data types with user-defined data types added lbessard@125: def RefreshDataTypes(self): lbessard@125: ResetTypeHierarchy() lbessard@125: ResetEnumeratedDataValues() lbessard@125: if self.Project: lbessard@151: for datatype in self.Project.getdataTypes(): lbessard@151: name = datatype.getname() lbessard@151: basetype_content = datatype.getbaseType().getcontent() lbessard@125: if basetype_content["value"] is None: lbessard@125: AddDataTypeHierarchy(name, basetype_content["name"]) lbessard@141: elif basetype_content["name"] in ["string", "wstring"]: lbessard@141: AddDataTypeHierarchy(name, basetype_content["name"].upper()) lbessard@125: elif basetype_content["name"] == "derived": lbessard@151: AddDataTypeHierarchy(name, basetype_content["value"].getname()) lbessard@125: elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]: lbessard@151: base_type = basetype_content["value"].baseType.getcontent() lbessard@125: if base_type["value"] is None: lbessard@125: AddDataTypeHierarchy(name, base_type["name"]) lbessard@125: else: lbessard@151: AddDataTypeHierarchy(name, base_type["value"].getname()) lbessard@125: else: lbessard@125: if basetype_content["name"] == "enum": lbessard@125: values = [] lbessard@151: for value in basetype_content["value"].values.getvalue(): lbessard@151: values.append(value.getname()) lbessard@125: AddEnumeratedDataValues(values) lbessard@125: AddDataTypeHierarchy(name, "ANY_DERIVED") lbessard@125: etisserant@0: # Update Block types with user-defined pou added etisserant@0: def RefreshBlockTypes(self): etisserant@0: if BlockTypes[-1]["name"] == "User-defined POUs": etisserant@0: BlockTypes[-1]["list"] = [] etisserant@0: else: etisserant@0: BlockTypes.append({"name" : "User-defined POUs", "list": []}) etisserant@0: if self.Project: lbessard@151: for pou in self.Project.getpous(): lbessard@151: pou_name = pou.getname() lbessard@151: pou_type = pou.getpouType() etisserant@0: if pou_type != "program": etisserant@0: block_infos = {"name" : pou_name, "type" : pou_type, "extensible" : False, etisserant@81: "inputs" : [], "outputs" : [], "comment" : "", etisserant@100: "generate" : generate_block, "initialise" : initialise_block } lbessard@151: if pou.getinterface(): lbessard@151: for type, varlist in pou.getvars(): etisserant@0: if type == "InOut": lbessard@151: for var in varlist.getvariable(): lbessard@151: var_type = var.type.getcontent() lbessard@141: if var_type["name"] == "derived": lbessard@151: block_infos["inputs"].append((var.getname(), var_type["value"].getname(), "none")) lbessard@151: block_infos["outputs"].append((var.getname(), var_type["value"].getname(), "none")) lbessard@141: elif var_type["name"] in ["string", "wstring"]: lbessard@151: block_infos["inputs"].append((var.getname(), var_type["name"].upper(), "none")) lbessard@151: block_infos["outputs"].append((var.getname(), var_type["name"].upper(), "none")) lbessard@141: else: lbessard@151: block_infos["inputs"].append((var.getname(), var_type["name"], "none")) lbessard@151: block_infos["outputs"].append((var.getname(), var_type["name"], "none")) etisserant@0: elif type == "Input": lbessard@151: for var in varlist.getvariable(): lbessard@151: var_type = var.type.getcontent() lbessard@141: if var_type["name"] == "derived": lbessard@151: block_infos["inputs"].append((var.getname(), var_type["value"].getname(), "none")) lbessard@141: elif var_type["name"] in ["string", "wstring"]: lbessard@151: block_infos["inputs"].append((var.getname(), var_type["name"].upper(), "none")) lbessard@141: else: lbessard@151: block_infos["inputs"].append((var.getname(), var_type["name"], "none")) etisserant@0: elif type == "Output": lbessard@151: for var in varlist.getvariable(): lbessard@151: var_type = var.type.getcontent() lbessard@141: if var_type["name"] == "derived": lbessard@151: block_infos["outputs"].append((var.getname(), var_type["value"].getname(), "none")) lbessard@141: elif var_type["name"] in ["string", "wstring"]: lbessard@151: block_infos["outputs"].append((var.getname(), var_type["name"].upper(), "none")) lbessard@125: else: lbessard@151: block_infos["outputs"].append((var.getname(), var_type["name"], "none")) lbessard@151: return_type = pou.interface.getreturnType() etisserant@0: if return_type: lbessard@151: var_type = return_type.getcontent() lbessard@141: if var_type["name"] == "derived": lbessard@151: block_infos["outputs"].append(("", var_type["value"].getname(), "none")) lbessard@141: elif var_type["name"] in ["string", "wstring"]: lbessard@151: block_infos["outputs"].append(("", var_type["name"].upper(), "none")) lbessard@141: else: lbessard@151: block_infos["outputs"].append(("", var_type["name"], "none")) lbessard@151: if pou.getbodyType() in ["FBD","LD","SFC"]: lbessard@151: for instance in pou.getinstances(): lbessard@151: if isinstance(instance, plcopen.commonObjects_comment): lbessard@151: block_infos["comment"] = instance.getcontentText() etisserant@0: BlockTypes[-1]["list"].append(block_infos) lbessard@92: etisserant@0: # Return Block types checking for recursion lbessard@121: def GetBlockTypes(self, tagname = ""): lbessard@125: name = "" lbessard@125: type = None lbessard@121: if self.Project: lbessard@121: words = tagname.split("::") lbessard@125: if words[0] in ["P","T","A"]: lbessard@121: name = words[1] lbessard@125: type = self.GetPouType(name) lbessard@121: if type == "function": lbessard@121: blocktypes = [] lbessard@121: for category in BlockTypes[:-1] + PluginTypes: lbessard@121: cat = {"name" : category["name"], "list" : []} lbessard@121: for block in category["list"]: lbessard@121: if block["type"] == "function": lbessard@121: cat["list"].append(block) lbessard@121: if len(cat["list"]) > 0: lbessard@121: blocktypes.append(cat) lbessard@121: else: lbessard@121: blocktypes = [category for category in BlockTypes[:-1] + PluginTypes] lbessard@121: if self.Project: lbessard@121: blocktypes.append({"name" : "User-defined POUs", "list": []}) lbessard@121: for blocktype in BlockTypes[-1]["list"]: lbessard@121: if blocktype["name"] != name and not self.PouIsUsedBy(name, blocktype["name"]) and not (type == "function" and blocktype["type"] != "function"): lbessard@121: blocktypes[-1]["list"].append(blocktype) lbessard@121: return blocktypes lbessard@121: lbessard@121: # Return Function Block types checking for recursion lbessard@121: def GetFunctionBlockTypes(self, tagname = ""): lbessard@121: name = "" lbessard@121: type = None lbessard@121: if self.Project: lbessard@121: words = tagname.split("::") lbessard@121: if words[0] in ["P","T","A"]: lbessard@125: name = words[1] lbessard@13: type = self.GetPouType(name) lbessard@121: blocktypes = [] lbessard@121: for category in BlockTypes[:-1]: lbessard@121: for block in category["list"]: lbessard@121: if block["type"] != "function": lbessard@121: blocktypes.append(block["name"]) lbessard@121: if self.Project: lbessard@121: for blocktype in BlockTypes[-1]["list"]: lbessard@121: if blocktype["name"] != name and not self.PouIsUsedBy(name, blocktype["name"]) and not (type == "function" and blocktype["type"] != "function"): lbessard@121: blocktypes.append(blocktype["name"]) lbessard@121: return blocktypes lbessard@68: etisserant@0: # Return Block types checking for recursion etisserant@0: def GetBlockResource(self): etisserant@0: blocktypes = [] etisserant@0: for category in BlockTypes[:-1]: etisserant@0: for blocktype in category["list"]: lbessard@29: if blocktype["type"] == "program": etisserant@0: blocktypes.append(blocktype["name"]) etisserant@0: if self.Project: lbessard@151: for pou in self.Project.getpous(): lbessard@151: if pou.getpouType() == "program": lbessard@151: blocktypes.append(pou.getname()) etisserant@0: return blocktypes etisserant@0: lbessard@125: # Return Data Types checking for recursion lbessard@125: def GetDataTypes(self, tagname = "", basetypes = True): lbessard@125: if basetypes: lbessard@125: datatypes = self.GetBaseTypes() lbessard@125: else: lbessard@125: datatypes = [] lbessard@125: if self.Project: lbessard@125: words = tagname.split("::") lbessard@125: if words[0] in ["D"]: lbessard@125: name = words[1] lbessard@125: else: lbessard@125: name = "" lbessard@151: for datatype in self.Project.getdataTypes(): lbessard@151: datatype_name = datatype.getname() lbessard@125: if datatype_name != name and not self.DataTypeIsUsedBy(name, datatype_name): lbessard@125: datatypes.append(datatype_name) lbessard@125: return datatypes lbessard@125: lbessard@125: # Return Base Types lbessard@125: def GetBaseTypes(self): lbessard@125: return [value for value, parent in TypeHierarchy_list if not value.startswith("ANY")] lbessard@125: lbessard@125: # Return Subrange types lbessard@125: def GetSubrangeTypes(self): lbessard@125: return [value for value, range in DataTypeRange_list] lbessard@125: lbessard@125: # Return Enumerated Values lbessard@125: def GetEnumeratedDataValues(self): lbessard@125: return EnumeratedDataValues lbessard@125: etisserant@0: #------------------------------------------------------------------------------- lbessard@121: # Project Element tag name computation functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: lbessard@125: # Compute a data type name lbessard@125: def ComputeDataTypeName(self, datatype): lbessard@125: return "D::%s" % datatype lbessard@125: lbessard@125: # Compute a pou name lbessard@80: def ComputePouName(self, pou): lbessard@80: return "P::%s" % pou lbessard@80: lbessard@80: # Compute a pou transition name etisserant@0: def ComputePouTransitionName(self, pou, transition): etisserant@0: return "T::%s::%s" % (pou, transition) etisserant@0: etisserant@0: # Compute a pou action name etisserant@0: def ComputePouActionName(self, pou, action): etisserant@0: return "A::%s::%s" % (pou, action) etisserant@0: etisserant@0: # Compute a pou name lbessard@47: def ComputeConfigurationName(self, config): lbessard@47: return "C::%s" % config lbessard@47: lbessard@47: # Compute a pou name etisserant@0: def ComputeConfigurationResourceName(self, config, resource): etisserant@0: return "R::%s::%s" % (config, resource) lbessard@125: lbessard@125: #------------------------------------------------------------------------------- lbessard@125: # Project opened Data types management functions lbessard@125: #------------------------------------------------------------------------------- lbessard@125: lbessard@125: # Return the data type informations lbessard@125: def GetDataTypeInfos(self, tagname): lbessard@125: words = tagname.split("::") lbessard@125: if words[0] == "D": lbessard@125: infos = {} lbessard@151: datatype = self.Project.getdataType(words[1]) lbessard@151: basetype_content = datatype.baseType.getcontent() lbessard@125: if basetype_content["value"] is None: lbessard@125: infos["type"] = "Directly" lbessard@125: infos["base_type"] = basetype_content["name"] lbessard@125: elif basetype_content["name"] == "derived": lbessard@125: infos["type"] = "Directly" lbessard@151: infos["base_type"] = basetype_content["value"].getname() lbessard@125: elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]: lbessard@125: infos["type"] = "Subrange" lbessard@151: infos["min"] = basetype_content["value"].range.getlower() lbessard@151: infos["max"] = basetype_content["value"].range.getupper() lbessard@151: base_type = basetype_content["value"].baseType.getcontent() lbessard@125: if base_type["value"] is None: lbessard@125: infos["base_type"] = base_type["name"] lbessard@125: else: lbessard@151: infos["base_type"] = base_type["value"].getname() lbessard@125: elif basetype_content["name"] == "enum": lbessard@125: infos["type"] = "Enumerated" lbessard@125: infos["values"] = [] lbessard@151: for value in basetype_content["value"].values.getvalue(): lbessard@151: infos["values"].append(value.getname()) lbessard@125: elif basetype_content["name"] == "array": lbessard@125: infos["type"] = "Array" lbessard@125: infos["dimensions"] = [] lbessard@151: for dimension in basetype_content["value"].getdimension(): lbessard@151: infos["dimensions"].append(str(dimension.getupper())) lbessard@151: base_type = basetype_content["value"].baseType.getcontent() lbessard@125: if base_type["value"] is None: lbessard@125: infos["base_type"] = base_type["name"] lbessard@125: else: lbessard@151: infos["base_type"] = base_type["value"].getname() lbessard@125: if datatype.initialValue is not None: lbessard@151: infos["initial"] = str(datatype.initialValue.getvalue()) lbessard@125: else: lbessard@125: infos["initial"] = "" lbessard@125: return infos lbessard@125: return None lbessard@125: lbessard@125: # Change the data type informations lbessard@125: def SetDataTypeInfos(self, tagname, infos): lbessard@125: words = tagname.split("::") lbessard@125: if words[0] == "D": lbessard@151: datatype = self.Project.getdataType(words[1]) lbessard@125: if infos["type"] == "Directly": lbessard@125: if infos["base_type"] in self.GetBaseTypes(): lbessard@141: if infos["base_type"] == "STRING": lbessard@151: datatype.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) lbessard@141: elif infos["base_type"] == "WSTRING": lbessard@151: datatype.baseType.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()}) lbessard@151: else: lbessard@151: datatype.baseType.setcontent({"name" : infos["base_type"], "value" : None}) lbessard@125: else: lbessard@151: derived_datatype = plcopen.derivedTypes_derived() lbessard@151: derived_datatype.setname(infos["base_type"]) lbessard@151: datatype.baseType.setcontent({"name" : "derived", "value" : derived_datatype}) lbessard@125: elif infos["type"] == "Subrange": lbessard@125: if infos["base_type"] in GetSubTypes("ANY_UINT"): lbessard@151: subrange = plcopen.derivedTypes_subrangeUnsigned() lbessard@151: datatype.baseType.setcontent({"name" : "subrangeUnsigned", "value" : subrange}) lbessard@125: else: lbessard@151: subrange = plcopen.derivedTypes_subrangeSigned() lbessard@151: datatype.baseType.setcontent({"name" : "subrangeSigned", "value" : subrange}) lbessard@151: subrange.range.setlower(infos["min"]) lbessard@151: subrange.range.setupper(infos["max"]) lbessard@125: if infos["base_type"] in self.GetBaseTypes(): lbessard@151: subrange.baseType.setcontent({"name" : infos["base_type"], "value" : None}) lbessard@125: else: lbessard@151: derived_datatype = plcopen.derivedTypes_derived() lbessard@151: derived_datatype.setname(infos["base_type"]) lbessard@151: subrange.baseType.setcontent({"name" : "derived", "value" : derived_datatype}) lbessard@125: elif infos["type"] == "Enumerated": lbessard@151: enumerated = plcopen.derivedTypes_enum() lbessard@151: for i, enum_value in enumerate(infos["values"]): lbessard@125: value = plcopen.values_value() lbessard@151: value.setname(enum_value) lbessard@151: if i == 0: lbessard@151: enumerated.values.setvalue([value]) lbessard@151: else: lbessard@151: enumerated.values.appendvalue(value) lbessard@151: datatype.baseType.setcontent({"name" : "enum", "value" : enumerated}) lbessard@125: elif infos["type"] == "Array": lbessard@151: array = plcopen.derivedTypes_array() lbessard@125: for dimension in infos["dimensions"]: lbessard@125: dimension_range = plcopen.rangeSigned() lbessard@151: dimension_range.setlower(1) lbessard@151: dimension_range.setupper(int(dimension)) lbessard@151: array.appenddimension(dimension_range) lbessard@125: if infos["base_type"] in self.GetBaseTypes(): lbessard@141: if infos["base_type"] == "STRING": lbessard@151: array.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) lbessard@141: elif infos["base_type"] == "WSTRING": lbessard@151: array.baseType.setcontent({"name" : "wstring", "value" : plcopen.wstring()}) lbessard@151: else: lbessard@151: array.baseType.setcontent({"name" : infos["base_type"], "value" : None}) lbessard@125: else: lbessard@151: derived_datatype = plcopen.derivedTypes_derived() lbessard@151: derived_datatype.setname(infos["base_type"]) lbessard@151: array.baseType.setcontent({"name" : "derived", "value" : derived_datatype}) lbessard@151: datatype.baseType.setcontent({"name" : "array", "value" : array}) lbessard@125: if infos["initial"] != "": lbessard@125: if datatype.initialValue is None: lbessard@125: datatype.initialValue = plcopen.value() lbessard@151: datatype.initialValue.setvalue(infos["initial"]) lbessard@125: else: lbessard@125: datatype.initialValue = None lbessard@125: self.RefreshDataTypeUsingTree() lbessard@125: self.RefreshDataTypes() lbessard@125: self.BufferProject() etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Project opened Pous management functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: lbessard@121: # Return edited element lbessard@121: def GetEditedElement(self, tagname): lbessard@121: words = tagname.split("::") lbessard@121: if words[0] == "P": lbessard@151: return self.Project.getpou(words[1]) lbessard@121: if words[0] in ['T', 'A']: lbessard@151: pou = self.Project.getpou(words[1]) lbessard@121: if words[0] == 'T': lbessard@151: return pou.gettransition(words[2]) lbessard@121: elif words[0] == 'A': lbessard@151: return pou.getaction(words[2]) lbessard@121: elif words[0] == 'C': lbessard@151: return self.Project.getconfiguration(words[1]) lbessard@121: elif words[0] == 'R': lbessard@151: return self.Project.getconfigurationResource(words[1], words[2]) etisserant@0: return None etisserant@0: lbessard@121: # Return edited element name lbessard@121: def GetEditedElementName(self, tagname): lbessard@121: words = tagname.split("::") lbessard@121: if words[0] in ["P","C"]: lbessard@121: return words[1] lbessard@121: else: lbessard@121: return words[2] lbessard@121: return None lbessard@121: lbessard@121: # Return edited element name and type lbessard@121: def GetEditedElementType(self, tagname): lbessard@121: words = tagname.split("::") lbessard@121: if words[0] in ["P","T","A"]: lbessard@121: return words[1], self.GetPouType(words[1]) lbessard@121: return None, None lbessard@121: lbessard@121: # Return language in which edited element is written lbessard@121: def GetEditedElementBodyType(self, tagname): lbessard@121: words = tagname.split("::") lbessard@121: if words[0] == "P": lbessard@121: return self.GetPouBodyType(words[1]) lbessard@121: elif words[0] == 'T': lbessard@121: return self.GetTransitionBodyType(words[1], words[2]) lbessard@121: elif words[0] == 'A': lbessard@121: return self.GetActionBodyType(words[1], words[2]) lbessard@121: return None lbessard@121: lbessard@121: # Return the edited element variables lbessard@121: def GetEditedElementInterfaceVars(self, tagname): lbessard@121: words = tagname.split("::") lbessard@121: if words[0] in ["P","T","A"]: lbessard@151: pou = self.Project.getpou(words[1]) lbessard@121: return self.GetPouInterfaceVars(pou) lbessard@121: return [] lbessard@121: lbessard@121: # Return the edited element return type lbessard@121: def GetEditedElementInterfaceReturnType(self, tagname): lbessard@121: words = tagname.split("::") lbessard@121: if words[0] == "P": lbessard@151: pou = self.Project.getpou(words[1]) lbessard@121: return self.GetPouInterfaceReturnType(pou) lbessard@121: elif words[0] == 'T': lbessard@121: return "BOOL" lbessard@121: return None lbessard@121: lbessard@125: # Change the edited element text lbessard@121: def SetEditedElementText(self, tagname, text): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element != None: lbessard@151: element.settext(text) lbessard@121: self.RefreshPouUsingTree() lbessard@121: lbessard@121: # Return the edited element text lbessard@121: def GetEditedElementText(self, tagname): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element != None: lbessard@151: return element.gettext() lbessard@121: return "" lbessard@121: lbessard@121: # Return the edited element transitions lbessard@121: def GetEditedElementTransitions(self, tagname): lbessard@121: pou = self.GetEditedElement(tagname) lbessard@151: if pou != None and pou.getbodyType() == "SFC": lbessard@121: transitions = [] lbessard@151: for transition in pou.gettransitionList(): lbessard@151: transitions.append(transition.getname()) lbessard@121: return transitions lbessard@121: return [] lbessard@121: lbessard@121: # Return edited element transitions lbessard@121: def GetEditedElementActions(self, tagname): lbessard@121: pou = self.GetEditedElement(tagname) lbessard@151: if pou != None and pou.getbodyType() == "SFC": lbessard@121: actions = [] lbessard@151: for action in pou.getactionList(): lbessard@151: actions.append(action.getname()) lbessard@121: return actions lbessard@121: return [] lbessard@121: lbessard@121: # Return the names of the pou elements lbessard@121: def GetEditedElementVariables(self, tagname): lbessard@121: words = tagname.split("::") lbessard@121: if words[0] in ["P","T","A"]: lbessard@121: return self.GetProjectPouVariables(words[1]) lbessard@121: return [] lbessard@121: lbessard@121: # Return the current pou editing informations lbessard@121: def GetEditedElementInstanceInfos(self, tagname, id = None, exclude = []): lbessard@121: infos = {} lbessard@121: instance = None lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@121: # if id is defined lbessard@121: if id is not None: lbessard@151: instance = element.getinstance(id) etisserant@0: else: lbessard@151: instance = element.getrandomInstance(exclude) lbessard@121: if instance is not None: lbessard@121: if id is not None: etisserant@0: infos["id"] = id etisserant@0: else: lbessard@151: infos["id"] = instance.getlocalId() lbessard@151: infos["x"] = instance.getx() lbessard@151: infos["y"] = instance.gety() lbessard@151: infos["height"] = instance.getheight() lbessard@151: infos["width"] = instance.getwidth() lbessard@151: if isinstance(instance, plcopen.fbdObjects_block): lbessard@151: infos["name"] = instance.getinstanceName() lbessard@151: infos["type"] = instance.gettypeName() lbessard@151: executionOrder = instance.getexecutionOrderId() lbessard@118: if executionOrder is not None: lbessard@118: infos["executionOrder"] = executionOrder lbessard@118: else: lbessard@118: infos["executionOrder"] = 0 etisserant@0: infos["connectors"] = {"inputs":[],"outputs":[]} lbessard@151: for variable in instance.inputVariables.getvariable(): etisserant@0: connector = {} lbessard@151: connector["name"] = variable.getformalParameter() lbessard@151: connector["position"] = variable.connectionPointIn.getrelPositionXY() lbessard@151: connector["negated"] = variable.getnegated() lbessard@151: connector["edge"] = variable.getedge() etisserant@0: connector["links"] = [] lbessard@151: connections = variable.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: connector["links"].append(dic) etisserant@0: infos["connectors"]["inputs"].append(connector) lbessard@151: for variable in instance.outputVariables.getvariable(): etisserant@0: connector = {} lbessard@151: connector["name"] = variable.getformalParameter() lbessard@151: connector["position"] = variable.connectionPointOut.getrelPositionXY() lbessard@151: connector["negated"] = variable.getnegated() lbessard@151: connector["edge"] = variable.getedge() etisserant@0: infos["connectors"]["outputs"].append(connector) lbessard@151: elif isinstance(instance, plcopen.fbdObjects_inVariable): lbessard@151: infos["name"] = instance.getexpression() lbessard@121: infos["value_type"] = self.GetEditedElementVarValueType(tagname, infos["name"]) etisserant@0: infos["type"] = "input" lbessard@151: executionOrder = instance.getexecutionOrderId() lbessard@118: if executionOrder is not None: lbessard@118: infos["executionOrder"] = executionOrder lbessard@118: else: lbessard@118: infos["executionOrder"] = 0 etisserant@0: infos["connector"] = {} lbessard@151: infos["connector"]["position"] = instance.connectionPointOut.getrelPositionXY() lbessard@151: infos["connector"]["negated"] = instance.getnegated() lbessard@151: infos["connector"]["edge"] = instance.getedge() lbessard@151: elif isinstance(instance, plcopen.fbdObjects_outVariable): lbessard@151: infos["name"] = instance.getexpression() lbessard@121: infos["value_type"] = self.GetEditedElementVarValueType(tagname, infos["name"]) etisserant@0: infos["type"] = "output" lbessard@151: executionOrder = instance.getexecutionOrderId() lbessard@118: if executionOrder is not None: lbessard@118: infos["executionOrder"] = executionOrder lbessard@118: else: lbessard@118: infos["executionOrder"] = 0 etisserant@0: infos["connector"] = {} lbessard@151: infos["connector"]["position"] = instance.connectionPointIn.getrelPositionXY() lbessard@151: infos["connector"]["negated"] = instance.getnegated() lbessard@151: infos["connector"]["edge"] = instance.getedge() etisserant@0: infos["connector"]["links"] = [] lbessard@151: connections = instance.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: infos["connector"]["links"].append(dic) lbessard@151: elif isinstance(instance, plcopen.fbdObjects_inOutVariable): lbessard@151: infos["name"] = instance.getexpression() lbessard@121: infos["value_type"] = self.GetEditedElementVarValueType(tagname, infos["name"]) etisserant@0: infos["type"] = "inout" lbessard@151: executionOrder = instance.getexecutionOrderId() lbessard@118: if executionOrder is not None: lbessard@118: infos["executionOrder"] = executionOrder lbessard@118: else: lbessard@118: infos["executionOrder"] = 0 etisserant@0: infos["connectors"] = {"input":{},"output":{}} lbessard@151: infos["connectors"]["output"]["position"] = instance.connectionPointOut.getrelPositionXY() lbessard@151: infos["connectors"]["output"]["negated"] = instance.getnegatedOut() lbessard@151: infos["connectors"]["output"]["edge"] = instance.getedgeOut() lbessard@151: infos["connectors"]["input"]["position"] = instance.connectionPointIn.getrelPositionXY() lbessard@151: infos["connectors"]["input"]["negated"] = instance.getnegatedIn() lbessard@151: infos["connectors"]["input"]["edge"] = instance.getedgeIn() etisserant@0: infos["connectors"]["input"]["links"] = [] lbessard@151: connections = instance.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: infos["connectors"]["input"]["links"].append(dic) lbessard@151: elif isinstance(instance, plcopen.commonObjects_continuation): lbessard@151: infos["name"] = instance.getname() etisserant@0: infos["type"] = "continuation" etisserant@0: infos["connector"] = {} lbessard@151: infos["connector"]["position"] = instance.connectionPointOut.getrelPositionXY() lbessard@151: elif isinstance(instance, plcopen.commonObjects_connector): lbessard@154: infos["name"] = instance.getname() etisserant@0: infos["type"] = "connection" etisserant@0: infos["connector"] = {} lbessard@151: infos["connector"]["position"] = instance.connectionPointIn.getrelPositionXY() etisserant@0: infos["connector"]["links"] = [] lbessard@151: connections = instance.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: infos["connector"]["links"].append(dic) lbessard@151: elif isinstance(instance, plcopen.commonObjects_comment): etisserant@0: infos["type"] = "comment" lbessard@151: infos["content"] = instance.getcontentText() lbessard@151: elif isinstance(instance, plcopen.ldObjects_leftPowerRail): etisserant@0: infos["type"] = "leftPowerRail" etisserant@0: infos["connectors"] = [] lbessard@151: for connection in instance.getconnectionPointOut(): etisserant@0: connector = {} lbessard@151: connector["position"] = connection.getrelPositionXY() etisserant@0: infos["connectors"].append(connector) lbessard@151: elif isinstance(instance, plcopen.ldObjects_rightPowerRail): etisserant@0: infos["type"] = "rightPowerRail" etisserant@0: infos["connectors"] = [] lbessard@151: for connection in instance.getconnectionPointIn(): etisserant@0: connector = {} lbessard@151: connector["position"] = connection.getrelPositionXY() etisserant@0: connector["links"] = [] lbessard@151: connections = connection.getconnections() lbessard@144: if connections: lbessard@151: for link in connection.getconnections(): lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} lbessard@144: connector["links"].append(dic) etisserant@0: infos["connectors"].append(connector) lbessard@151: elif isinstance(instance, plcopen.ldObjects_contact): lbessard@151: infos["name"] = instance.getvariable() etisserant@0: infos["type"] = "contact" lbessard@151: executionOrder = instance.getexecutionOrderId() lbessard@118: if executionOrder is not None: lbessard@118: infos["executionOrder"] = executionOrder lbessard@118: else: lbessard@118: infos["executionOrder"] = 0 lbessard@151: infos["negated"] = instance.getnegated() lbessard@151: infos["edge"] = instance.getedge() etisserant@0: infos["connectors"] = {"input":{},"output":{}} lbessard@151: infos["connectors"]["input"]["position"] = instance.connectionPointIn.getrelPositionXY() etisserant@0: infos["connectors"]["input"]["links"] = [] lbessard@151: connections = instance.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: infos["connectors"]["input"]["links"].append(dic) lbessard@151: infos["connectors"]["output"]["position"] = instance.connectionPointOut.getrelPositionXY() lbessard@151: elif isinstance(instance, plcopen.ldObjects_coil): lbessard@151: infos["name"] = instance.getvariable() etisserant@0: infos["type"] = "coil" lbessard@151: executionOrder = instance.getexecutionOrderId() lbessard@118: if executionOrder is not None: lbessard@118: infos["executionOrder"] = executionOrder lbessard@118: else: lbessard@118: infos["executionOrder"] = 0 lbessard@151: infos["negated"] = instance.getnegated() lbessard@151: infos["storage"] = instance.getstorage() etisserant@0: infos["connectors"] = {"input":{},"output":{}} lbessard@151: infos["connectors"]["input"]["position"] = instance.connectionPointIn.getrelPositionXY() etisserant@0: infos["connectors"]["input"]["links"] = [] lbessard@151: connections = instance.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: infos["connectors"]["input"]["links"].append(dic) lbessard@151: infos["connectors"]["output"]["position"] = instance.connectionPointOut.getrelPositionXY() lbessard@151: elif isinstance(instance, plcopen.sfcObjects_step): lbessard@151: infos["name"] = instance.getname() etisserant@0: infos["type"] = "step" lbessard@151: infos["initial"] = instance.getinitialStep() etisserant@0: infos["connectors"] = {} etisserant@0: if instance.connectionPointIn: etisserant@0: infos["connectors"]["input"] = {} lbessard@151: infos["connectors"]["input"]["position"] = instance.connectionPointIn.getrelPositionXY() etisserant@0: infos["connectors"]["input"]["links"] = [] lbessard@151: connections = instance.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: infos["connectors"]["input"]["links"].append(dic) etisserant@0: if instance.connectionPointOut: lbessard@151: infos["connectors"]["output"] = {"position" : instance.connectionPointOut.getrelPositionXY()} etisserant@0: if instance.connectionPointOutAction: lbessard@151: infos["connectors"]["action"] = {"position" : instance.connectionPointOutAction.getrelPositionXY()} lbessard@151: elif isinstance(instance, plcopen.sfcObjects_transition): etisserant@0: infos["type"] = "transition" lbessard@151: condition = instance.getconditionContent() lbessard@151: priority = instance.getpriority() lbessard@80: if priority == None: lbessard@80: infos["priority"] = 0 lbessard@80: else: lbessard@80: infos["priority"] = priority etisserant@0: infos["condition_type"] = condition["type"] etisserant@0: infos["connectors"] = {"input":{},"output":{}} lbessard@151: infos["connectors"]["input"]["position"] = instance.connectionPointIn.getrelPositionXY() etisserant@0: infos["connectors"]["input"]["links"] = [] lbessard@151: connections = instance.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: infos["connectors"]["input"]["links"].append(dic) lbessard@151: infos["connectors"]["output"]["position"] = instance.connectionPointOut.getrelPositionXY() lbessard@63: if infos["condition_type"] == "connection": lbessard@63: infos["connectors"]["connection"] = {} lbessard@63: infos["connectors"]["connection"]["links"] = [] lbessard@151: connections = instance.getconnections() lbessard@63: if connections: lbessard@63: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} lbessard@63: infos["connectors"]["connection"]["links"].append(dic) lbessard@63: infos["condition"] = None lbessard@63: else: lbessard@63: infos["condition"] = condition["value"] lbessard@151: elif isinstance(instance, (plcopen.sfcObjects_selectionDivergence, plcopen.sfcObjects_simultaneousDivergence)): lbessard@151: if isinstance(instance, plcopen.sfcObjects_selectionDivergence): etisserant@0: infos["type"] = "selectionDivergence" etisserant@0: else: etisserant@0: infos["type"] = "simultaneousDivergence" etisserant@0: infos["connectors"] = {"inputs":[],"outputs":[]} etisserant@0: connector = {} lbessard@151: connector["position"] = instance.connectionPointIn.getrelPositionXY() etisserant@0: connector["links"] = [] lbessard@151: connections = instance.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: connector["links"].append(dic) etisserant@0: infos["connectors"]["inputs"].append(connector) lbessard@151: for sequence in instance.getconnectionPointOut(): etisserant@0: connector = {} lbessard@151: connector["position"] = sequence.getrelPositionXY() etisserant@0: infos["connectors"]["outputs"].append(connector) lbessard@151: elif isinstance(instance, (plcopen.sfcObjects_selectionConvergence, plcopen.sfcObjects_simultaneousConvergence)): lbessard@151: if isinstance(instance, plcopen.sfcObjects_selectionConvergence): etisserant@0: infos["type"] = "selectionConvergence" etisserant@0: else: etisserant@0: infos["type"] = "simultaneousConvergence" etisserant@0: infos["connectors"] = {"inputs":[],"outputs":[]} lbessard@151: for sequence in instance.getconnectionPointIn(): etisserant@0: connector = {} lbessard@151: connector["position"] = sequence.getrelPositionXY() etisserant@0: connector["links"] = [] lbessard@151: connections = sequence.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: connector["links"].append(dic) etisserant@0: infos["connectors"]["inputs"].append(connector) etisserant@0: connector = {} lbessard@151: connector["position"] = instance.connectionPointOut.getrelPositionXY() etisserant@0: infos["connectors"]["outputs"].append(connector) lbessard@151: elif isinstance(instance, plcopen.sfcObjects_jumpStep): etisserant@0: infos["type"] = "jump" lbessard@151: infos["target"] = instance.gettargetName() etisserant@0: infos["connector"] = {} lbessard@151: infos["connector"]["position"] = instance.connectionPointIn.getrelPositionXY() etisserant@0: infos["connector"]["links"] = [] lbessard@151: connections = instance.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: infos["connector"]["links"].append(dic) lbessard@151: elif isinstance(instance, plcopen.commonObjects_actionBlock): etisserant@0: infos["type"] = "actionBlock" lbessard@151: infos["actions"] = instance.getactions() etisserant@0: infos["connector"] = {} lbessard@151: infos["connector"]["position"] = instance.connectionPointIn.getrelPositionXY() etisserant@0: infos["connector"]["links"] = [] lbessard@151: connections = instance.connectionPointIn.getconnections() etisserant@0: if connections: etisserant@0: for link in connections: lbessard@151: dic = {"refLocalId":link.getrefLocalId(),"points":link.getpoints(),"formalParameter":link.getformalParameter()} etisserant@0: infos["connector"]["links"].append(dic) etisserant@0: return infos etisserant@0: return False etisserant@0: lbessard@121: def ClearEditedElementExecutionOrder(self, tagname): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: element.resetexecutionOrder() lbessard@121: lbessard@121: def ResetEditedElementExecutionOrder(self, tagname): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: element.compileexecutionOrder() lbessard@118: etisserant@0: # Return the variable type of the given pou lbessard@121: def GetEditedElementVarValueType(self, tagname, varname): lbessard@121: words = tagname.split("::") lbessard@80: if words[0] in ["P","T","A"]: lbessard@151: pou = self.Project.getpou(words[1]) lbessard@151: for type, varlist in pou.getvars(): lbessard@151: for var in varlist.getvariable(): lbessard@151: if var.getname() == varname: lbessard@151: vartype_content = var.gettype().getcontent() lbessard@141: if vartype_content["name"] == "derived": lbessard@151: return vartype_content["value"].getname() lbessard@141: elif vartype_content["name"] in ["string", "wstring"]: lbessard@141: return vartype_content["name"].upper() lbessard@141: else: lbessard@125: return vartype_content["name"] etisserant@101: return None etisserant@0: etisserant@0: def SetConnectionWires(self, connection, connector): etisserant@0: wires = connector.GetWires() etisserant@0: idx = 0 etisserant@0: for wire, handle in wires: etisserant@0: points = wire.GetPoints(handle != 0) etisserant@0: if handle == 0: lbessard@27: result = wire.GetConnectedInfos(-1) etisserant@0: else: lbessard@27: result = wire.GetConnectedInfos(0) lbessard@27: if result != None: lbessard@27: refLocalId, formalParameter = result lbessard@151: connection.addconnection() lbessard@151: connection.setconnectionId(idx, refLocalId) lbessard@151: connection.setconnectionPoints(idx, points) lbessard@27: if formalParameter != "": lbessard@151: connection.setconnectionParameter(idx, formalParameter) lbessard@27: else: lbessard@151: connection.setconnectionParameter(idx, None) etisserant@0: idx += 1 etisserant@0: lbessard@145: def AddEditedElementPouVar(self, tagname, type, name): lbessard@145: words = tagname.split("::") lbessard@145: if words[0] in ['P', 'T', 'A']: lbessard@151: pou = self.Project.getpou(words[1]) lbessard@151: pou.addpouVar(type, name) lbessard@145: lbessard@145: def ChangeEditedElementPouVar(self, tagname, old_type, old_name, new_type, new_name): lbessard@145: words = tagname.split("::") lbessard@145: if words[0] in ['P', 'T', 'A']: lbessard@151: pou = self.Project.getpou(words[1]) lbessard@151: pou.changepouVar(old_type, old_name, new_type, new_name) lbessard@145: lbessard@154: def RemoveEditedElementPouVar(self, tagname, type, name): lbessard@154: words = tagname.split("::") lbessard@154: if words[0] in ['P', 'T', 'A']: lbessard@154: pou = self.Project.getpou(words[1]) lbessard@154: pou.removepouVar(type, name) lbessard@154: lbessard@121: def AddEditedElementBlock(self, tagname, id, blocktype, blockname = None): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: block = plcopen.fbdObjects_block() lbessard@151: block.setlocalId(id) lbessard@151: block.settypeName(blocktype) lbessard@121: blocktype_infos = GetBlockType(blocktype) lbessard@121: if blocktype_infos["type"] != "function" and blockname is not None: lbessard@151: block.setinstanceName(blockname) lbessard@145: self.AddEditedElementPouVar(tagname, blocktype, blockname) lbessard@151: element.addinstance("block", block) lbessard@121: self.RefreshPouUsingTree() lbessard@121: lbessard@121: def SetEditedElementBlockInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: block = element.getinstance(id) lbessard@154: old_name = block.getinstanceName() lbessard@154: old_type = block.gettypeName() lbessard@154: new_name = infos.get("name", old_name) lbessard@154: new_type = infos.get("type", old_type) lbessard@154: old_typeinfos = GetBlockType(old_type) lbessard@154: new_typeinfos = GetBlockType(new_type) lbessard@154: if new_typeinfos["type"] != old_typeinfos["type"]: lbessard@154: if new_typeinfos["type"] == "function": lbessard@154: self.RemoveEditedElementPouVar(tagname, old_type, old_name) lbessard@154: else: lbessard@154: self.AddEditedElementPouVar(tagname, new_type, new_name) lbessard@154: elif new_typeinfos["type"] != "function" and old_name != new_name: lbessard@154: self.ChangeEditedElementPouVar(tagname, old_type, old_name, new_type, new_name) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "name": lbessard@151: block.setinstanceName(value) lbessard@121: elif param == "type": lbessard@151: block.settypeName(value) lbessard@151: elif param == "executionOrder" and block.getexecutionOrderId() != value: lbessard@151: self.GetEditedElement(tagname).setelementExecutionOrder(block, value) lbessard@121: elif param == "height": lbessard@151: block.setheight(value) lbessard@121: elif param == "width": lbessard@151: block.setwidth(value) lbessard@121: elif param == "x": lbessard@151: block.setx(value) lbessard@121: elif param == "y": lbessard@151: block.sety(value) lbessard@121: elif param == "connectors": lbessard@151: block.inputVariables.setvariable([]) lbessard@151: block.outputVariables.setvariable([]) lbessard@121: for connector in value["inputs"]: lbessard@121: variable = plcopen.inputVariables_variable() lbessard@151: variable.setformalParameter(connector.GetName()) lbessard@121: if connector.IsNegated(): lbessard@151: variable.setnegated(True) lbessard@121: if connector.GetEdge() != "none": lbessard@151: variable.setedge(connector.GetEdge()) lbessard@121: position = connector.GetRelPosition() lbessard@151: variable.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(variable.connectionPointIn, connector) lbessard@151: block.inputVariables.appendvariable(variable) lbessard@121: for connector in value["outputs"]: lbessard@121: variable = plcopen.outputVariables_variable() lbessard@151: variable.setformalParameter(connector.GetName()) lbessard@121: if connector.IsNegated(): lbessard@151: variable.setnegated(True) lbessard@121: if connector.GetEdge() != "none": lbessard@151: variable.setedge(connector.GetEdge()) lbessard@121: position = connector.GetRelPosition() lbessard@151: variable.addconnectionPointOut() lbessard@151: variable.connectionPointOut.setrelPositionXY(position.x, position.y) lbessard@151: block.outputVariables.appendvariable(variable) lbessard@121: self.RefreshPouUsingTree() etisserant@0: lbessard@121: def AddEditedElementVariable(self, tagname, id, type): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@121: if type == INPUT: lbessard@121: name = "inVariable" lbessard@151: variable = plcopen.fbdObjects_inVariable() lbessard@121: elif type == OUTPUT: lbessard@121: name = "outVariable" lbessard@151: variable = plcopen.fbdObjects_outVariable() lbessard@121: elif type == INOUT: lbessard@121: name = "inOutVariable" lbessard@151: variable = plcopen.fbdObjects_inOutVariable() lbessard@151: variable.setlocalId(id) lbessard@151: element.addinstance(name, variable) etisserant@0: lbessard@121: def SetEditedElementVariableInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: variable = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "name": lbessard@151: variable.setexpression(value) lbessard@151: elif param == "executionOrder" and variable.getexecutionOrderId() != value: lbessard@151: self.GetEditedElement(tagname).setelementExecutionOrder(variable, value) lbessard@121: elif param == "height": lbessard@151: variable.setheight(value) lbessard@121: elif param == "width": lbessard@151: variable.setwidth(value) lbessard@121: elif param == "x": lbessard@151: variable.setx(value) lbessard@121: elif param == "y": lbessard@151: variable.sety(value) lbessard@121: elif param == "connectors": lbessard@151: if isinstance(variable, plcopen.fbdObjects_inVariable): lbessard@121: if value["output"].IsNegated(): lbessard@151: variable.setnegated(True) lbessard@121: if value["output"].GetEdge() != "none": lbessard@151: variable.setedge(value["output"].GetEdge()) lbessard@121: position = value["output"].GetRelPosition() lbessard@151: variable.addconnectionPointOut() lbessard@151: variable.connectionPointOut.setrelPositionXY(position.x, position.y) lbessard@151: elif isinstance(variable, plcopen.fbdObjects_outVariable): lbessard@121: if value["input"].IsNegated(): lbessard@151: variable.setnegated(True) lbessard@121: if value["input"].GetEdge() != "none": lbessard@151: variable.setedge(value["input"].GetEdge()) lbessard@121: position = value["input"].GetRelPosition() lbessard@151: variable.addconnectionPointIn() lbessard@151: variable.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(variable.connectionPointIn, value["input"]) lbessard@151: elif isinstance(variable, plcopen.fbdObjects_inOutVariable): lbessard@121: if value["input"].IsNegated(): lbessard@151: variable.setnegatedIn(True) lbessard@121: if value["input"].GetEdge() != "none": lbessard@151: variable.setedgeIn(value["input"].GetEdge()) lbessard@121: if value["output"].IsNegated(): lbessard@151: variable.setnegatedOut(True) lbessard@121: if value["output"].GetEdge() != "none": lbessard@151: variable.setedgeOut(value["output"].GetEdge()) lbessard@121: position = value["output"].GetRelPosition() lbessard@151: variable.addconnectionPointOut() lbessard@151: variable.connectionPointOut.setrelPositionXY(position.x, position.y) lbessard@121: position = value["input"].GetRelPosition() lbessard@151: variable.addconnectionPointIn() lbessard@151: variable.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(variable.connectionPointIn, value["input"]) lbessard@121: lbessard@121: def AddEditedElementConnection(self, tagname, id, type): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@121: if type == CONNECTOR: lbessard@121: name = "connector" lbessard@151: connection = plcopen.commonObjects_connector() lbessard@121: elif type == CONTINUATION: lbessard@121: name = "continuation" lbessard@151: connection = plcopen.commonObjects_continuation() lbessard@151: connection.setlocalId(id) lbessard@151: element.addinstance(name, connection) etisserant@0: lbessard@121: def SetEditedElementConnectionInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: connection = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "name": lbessard@151: connection.setname(value) lbessard@121: elif param == "height": lbessard@151: connection.setheight(value) lbessard@121: elif param == "width": lbessard@151: connection.setwidth(value) lbessard@121: elif param == "x": lbessard@151: connection.setx(value) lbessard@121: elif param == "y": lbessard@151: connection.sety(value) lbessard@121: elif param == "connector": lbessard@121: position = value.GetRelPosition() lbessard@151: if isinstance(connection, plcopen.commonObjects_continuation): lbessard@151: connection.addconnectionPointOut() lbessard@151: connection.connectionPointOut.setrelPositionXY(position.x, position.y) lbessard@151: elif isinstance(connection, plcopen.commonObjects_connector): lbessard@151: connection.addconnectionPointIn() lbessard@151: connection.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(connection.connectionPointIn, value) lbessard@121: lbessard@121: def AddEditedElementComment(self, tagname, id): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: comment = plcopen.commonObjects_comment() lbessard@151: comment.setlocalId(id) lbessard@151: element.addinstance("comment", comment) lbessard@121: lbessard@121: def SetEditedElementCommentInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: comment = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "content": lbessard@151: comment.setcontentText(value) lbessard@121: elif param == "height": lbessard@151: comment.setheight(value) lbessard@121: elif param == "width": lbessard@151: comment.setwidth(value) lbessard@121: elif param == "x": lbessard@151: comment.setx(value) lbessard@121: elif param == "y": lbessard@151: comment.sety(value) lbessard@121: lbessard@121: def AddEditedElementPowerRail(self, tagname, id, type): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@121: if type == LEFTRAIL: lbessard@121: name = "leftPowerRail" lbessard@151: powerrail = plcopen.ldObjects_leftPowerRail() lbessard@121: elif type == RIGHTRAIL: lbessard@121: name = "rightPowerRail" lbessard@151: powerrail = plcopen.ldObjects_rightPowerRail() lbessard@151: powerrail.setlocalId(id) lbessard@151: element.addinstance(name, powerrail) lbessard@121: lbessard@121: def SetEditedElementPowerRailInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: powerrail = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "height": lbessard@151: powerrail.setheight(value) lbessard@121: elif param == "width": lbessard@151: powerrail.setwidth(value) lbessard@121: elif param == "x": lbessard@151: powerrail.setx(value) lbessard@121: elif param == "y": lbessard@151: powerrail.sety(value) lbessard@121: elif param == "connectors": lbessard@151: if isinstance(powerrail, plcopen.ldObjects_leftPowerRail): lbessard@151: powerrail.setconnectionPointOut([]) lbessard@121: for connector in value: lbessard@121: position = connector.GetRelPosition() lbessard@121: connection = plcopen.leftPowerRail_connectionPointOut() lbessard@151: connection.setrelPositionXY(position.x, position.y) lbessard@121: powerrail.connectionPointOut.append(connection) lbessard@151: elif isinstance(powerrail, plcopen.ldObjects_rightPowerRail): lbessard@151: powerrail.setconnectionPointIn([]) lbessard@121: for connector in value: lbessard@121: position = connector.GetRelPosition() lbessard@121: connection = plcopen.connectionPointIn() lbessard@151: connection.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(connection, connector) lbessard@121: powerrail.connectionPointIn.append(connection) lbessard@121: lbessard@128: def AddEditedElementContact(self, tagname, id): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: contact = plcopen.ldObjects_contact() lbessard@151: contact.setlocalId(id) lbessard@151: element.addinstance("contact", contact) lbessard@121: lbessard@121: def SetEditedElementContactInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: contact = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "name": lbessard@151: contact.setvariable(value) lbessard@121: elif param == "type": lbessard@121: if value == CONTACT_NORMAL: lbessard@151: contact.setnegated(False) lbessard@151: contact.setedge("none") lbessard@121: elif value == CONTACT_REVERSE: lbessard@151: contact.setnegated(True) lbessard@151: contact.setedge("none") lbessard@121: elif value == CONTACT_RISING: lbessard@151: contact.setnegated(False) lbessard@151: contact.setedge("rising") lbessard@121: elif value == CONTACT_FALLING: lbessard@151: contact.setnegated(False) lbessard@151: contact.setedge("falling") lbessard@121: elif param == "height": lbessard@151: contact.setheight(value) lbessard@121: elif param == "width": lbessard@151: contact.setwidth(value) lbessard@121: elif param == "x": lbessard@151: contact.setx(value) lbessard@121: elif param == "y": lbessard@151: contact.sety(value) lbessard@121: elif param == "connectors": lbessard@121: input_connector = value["input"] etisserant@0: position = input_connector.GetRelPosition() lbessard@151: contact.addconnectionPointIn() lbessard@151: contact.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(contact.connectionPointIn, input_connector) lbessard@121: output_connector = value["output"] etisserant@0: position = output_connector.GetRelPosition() lbessard@151: contact.addconnectionPointOut() lbessard@151: contact.connectionPointOut.setrelPositionXY(position.x, position.y) lbessard@121: lbessard@121: def AddEditedElementCoil(self, tagname, id): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: coil = plcopen.ldObjects_coil() lbessard@151: coil.setlocalId(id) lbessard@151: element.addinstance("coil", coil) lbessard@121: lbessard@121: def SetEditedElementCoilInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: coil = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "name": lbessard@151: coil.setvariable(value) lbessard@121: elif param == "type": lbessard@121: if value == COIL_NORMAL: lbessard@151: coil.setnegated(False) lbessard@151: coil.setstorage("none") lbessard@121: elif value == COIL_REVERSE: lbessard@151: coil.setnegated(True) lbessard@151: coil.setstorage("none") lbessard@121: elif value == COIL_SET: lbessard@151: coil.setnegated(False) lbessard@151: coil.setstorage("set") lbessard@121: elif value == COIL_RESET: lbessard@151: coil.setnegated(False) lbessard@151: coil.setstorage("reset") lbessard@121: elif param == "height": lbessard@151: coil.setheight(value) lbessard@121: elif param == "width": lbessard@151: coil.setwidth(value) lbessard@121: elif param == "x": lbessard@151: coil.setx(value) lbessard@121: elif param == "y": lbessard@151: coil.sety(value) lbessard@121: elif param == "connectors": lbessard@121: input_connector = value["input"] lbessard@121: position = input_connector.GetRelPosition() lbessard@151: coil.addconnectionPointIn() lbessard@151: coil.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(coil.connectionPointIn, input_connector) lbessard@121: output_connector = value["output"] lbessard@121: position = output_connector.GetRelPosition() lbessard@151: coil.addconnectionPointOut() lbessard@151: coil.connectionPointOut.setrelPositionXY(position.x, position.y) lbessard@121: lbessard@121: def AddEditedElementStep(self, tagname, id): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: step = plcopen.sfcObjects_step() lbessard@151: step.setlocalId(id) lbessard@151: element.addinstance("step", step) lbessard@121: lbessard@121: def SetEditedElementStepInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: step = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "name": lbessard@151: step.setname(value) lbessard@121: elif param == "initial": lbessard@151: step.setinitialStep(value) lbessard@121: elif param == "height": lbessard@151: step.setheight(value) lbessard@121: elif param == "width": lbessard@151: step.setwidth(value) lbessard@121: elif param == "x": lbessard@151: step.setx(value) lbessard@121: elif param == "y": lbessard@151: step.sety(value) lbessard@121: elif param == "connectors": lbessard@121: input_connector = value["input"] lbessard@121: if input_connector: etisserant@0: position = input_connector.GetRelPosition() lbessard@151: step.addconnectionPointIn() lbessard@151: step.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(step.connectionPointIn, input_connector) lbessard@121: else: lbessard@151: step.deleteconnectionPointIn() lbessard@121: output_connector = value["output"] lbessard@121: if output_connector: etisserant@0: position = output_connector.GetRelPosition() lbessard@151: step.addconnectionPointOut() lbessard@151: step.connectionPointOut.setrelPositionXY(position.x, position.y) lbessard@151: else: lbessard@151: step.deleteconnectionPointOut() lbessard@121: action_connector = value["action"] lbessard@121: if action_connector: lbessard@121: position = action_connector.GetRelPosition() lbessard@151: step.addconnectionPointOutAction() lbessard@151: step.connectionPointOutAction.setrelPositionXY(position.x, position.y) lbessard@151: else: lbessard@151: step.deleteconnectionPointOutAction() lbessard@121: lbessard@121: def AddEditedElementTransition(self, tagname, id): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: transition = plcopen.sfcObjects_transition() lbessard@151: transition.setlocalId(id) lbessard@151: element.addinstance("transition", transition) lbessard@121: lbessard@121: def SetEditedElementTransitionInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: transition = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "type" and value != "connection": lbessard@151: transition.setconditionContent(value, infos["condition"]) lbessard@121: elif param == "height": lbessard@151: transition.setheight(value) lbessard@121: elif param == "width": lbessard@151: transition.setwidth(value) lbessard@121: elif param == "x": lbessard@151: transition.setx(value) lbessard@121: elif param == "y": lbessard@151: transition.sety(value) lbessard@121: elif param == "priority": lbessard@121: if value != 0: lbessard@151: transition.setpriority(value) lbessard@151: else: lbessard@151: transition.setpriority(None) lbessard@121: elif param == "connectors": lbessard@121: input_connector = value["input"] lbessard@121: position = input_connector.GetRelPosition() lbessard@151: transition.addconnectionPointIn() lbessard@151: transition.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(transition.connectionPointIn, input_connector) lbessard@121: output_connector = value["output"] lbessard@121: position = output_connector.GetRelPosition() lbessard@151: transition.addconnectionPointOut() lbessard@151: transition.connectionPointOut.setrelPositionXY(position.x, position.y) lbessard@121: if infos.get("type", None) == "connection": lbessard@151: transition.setconditionContent("connection", None) lbessard@121: connection_connector = value["connection"] lbessard@121: self.SetConnectionWires(transition, connection_connector) lbessard@121: lbessard@121: def AddEditedElementDivergence(self, tagname, id, type): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@121: if type == SELECTION_DIVERGENCE: lbessard@121: name = "selectionDivergence" lbessard@151: divergence = plcopen.sfcObjects_selectionDivergence() lbessard@121: elif type == SELECTION_CONVERGENCE: lbessard@121: name = "selectionConvergence" lbessard@151: divergence = plcopen.sfcObjects_selectionConvergence() lbessard@121: elif type == SIMULTANEOUS_DIVERGENCE: lbessard@121: name = "simultaneousDivergence" lbessard@151: divergence = plcopen.sfcObjects_simultaneousDivergence() lbessard@121: elif type == SIMULTANEOUS_CONVERGENCE: lbessard@121: name = "simultaneousConvergence" lbessard@151: divergence = plcopen.sfcObjects_simultaneousConvergence() lbessard@151: divergence.setlocalId(id) lbessard@151: element.addinstance(name, divergence) lbessard@121: lbessard@121: def SetEditedElementDivergenceInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: divergence = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "height": lbessard@151: divergence.setheight(value) lbessard@121: elif param == "width": lbessard@151: divergence.setwidth(value) lbessard@121: elif param == "x": lbessard@151: divergence.setx(value) lbessard@121: elif param == "y": lbessard@151: divergence.sety(value) lbessard@121: elif param == "connectors": lbessard@121: input_connectors = value["inputs"] lbessard@151: if isinstance(divergence, (plcopen.sfcObjects_selectionDivergence, plcopen.sfcObjects_simultaneousDivergence)): lbessard@121: position = input_connectors[0].GetRelPosition() lbessard@151: divergence.addconnectionPointIn() lbessard@151: divergence.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(divergence.connectionPointIn, input_connectors[0]) lbessard@121: else: lbessard@151: divergence.setconnectionPointIn([]) lbessard@121: for input_connector in input_connectors: lbessard@121: position = input_connector.GetRelPosition() lbessard@151: if isinstance(divergence, plcopen.sfcObjects_selectionConvergence): lbessard@121: connection = plcopen.selectionConvergence_connectionPointIn() lbessard@121: else: lbessard@121: connection = plcopen.connectionPointIn() lbessard@151: connection.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(connection, input_connector) lbessard@151: divergence.appendconnectionPointIn(connection) lbessard@121: output_connectors = value["outputs"] lbessard@151: if isinstance(divergence, (plcopen.sfcObjects_selectionConvergence, plcopen.sfcObjects_simultaneousConvergence)): lbessard@121: position = output_connectors[0].GetRelPosition() lbessard@151: divergence.addconnectionPointOut() lbessard@151: divergence.connectionPointOut.setrelPositionXY(position.x, position.y) lbessard@151: else: lbessard@151: divergence.setconnectionPointOut([]) lbessard@121: for output_connector in output_connectors: lbessard@121: position = output_connector.GetRelPosition() lbessard@151: if isinstance(divergence, plcopen.sfcObjects_selectionDivergence): lbessard@121: connection = plcopen.selectionDivergence_connectionPointOut() lbessard@121: else: lbessard@121: connection = plcopen.simultaneousDivergence_connectionPointOut() lbessard@151: connection.setrelPositionXY(position.x, position.y) lbessard@151: divergence.appendconnectionPointOut(connection) lbessard@121: lbessard@121: def AddEditedElementJump(self, tagname, id): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: jump = plcopen.sfcObjects_jumpStep() lbessard@151: jump.setlocalId(id) lbessard@151: element.addinstance("jumpStep", jump) lbessard@121: lbessard@121: def SetEditedElementJumpInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: jump = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "target": lbessard@151: jump.settargetName(value) lbessard@121: elif param == "height": lbessard@151: jump.setheight(value) lbessard@121: elif param == "width": lbessard@151: jump.setwidth(value) lbessard@121: elif param == "x": lbessard@151: jump.setx(value) lbessard@121: elif param == "y": lbessard@151: jump.sety(value) lbessard@121: elif param == "connector": lbessard@121: position = value.GetRelPosition() lbessard@151: jump.addconnectionPointIn() lbessard@151: jump.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(jump.connectionPointIn, value) etisserant@0: lbessard@121: def AddEditedElementActionBlock(self, tagname, id): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: actionBlock = plcopen.commonObjects_actionBlock() lbessard@151: actionBlock.setlocalId(id) lbessard@151: element.addinstance("actionBlock", actionBlock) lbessard@121: lbessard@121: def SetEditedElementActionBlockInfos(self, tagname, id, infos): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: actionBlock = element.getinstance(id) lbessard@121: for param, value in infos.items(): lbessard@121: if param == "actions": lbessard@151: actionBlock.setactions(value) lbessard@121: elif param == "height": lbessard@151: actionBlock.setheight(value) lbessard@121: elif param == "width": lbessard@151: actionBlock.setwidth(value) lbessard@121: elif param == "x": lbessard@151: actionBlock.setx(value) lbessard@121: elif param == "y": lbessard@151: actionBlock.sety(value) lbessard@121: elif param == "connector": lbessard@121: position = value.GetRelPosition() lbessard@151: actionBlock.addconnectionPointIn() lbessard@151: actionBlock.connectionPointIn.setrelPositionXY(position.x, position.y) lbessard@121: self.SetConnectionWires(actionBlock.connectionPointIn, value) lbessard@121: lbessard@121: def RemoveEditedElementInstance(self, tagname, id): lbessard@121: element = self.GetEditedElement(tagname) lbessard@121: if element is not None: lbessard@151: instance = element.getinstance(id) lbessard@151: if isinstance(instance, plcopen.fbdObjects_block): lbessard@154: self.RemoveEditedElementPouVar(tagname, instance.gettypeName(), instance.getinstanceName()) lbessard@151: element.removeinstance(id) lbessard@121: self.RefreshPouUsingTree() lbessard@121: lbessard@121: def GetEditedResourceVariables(self, tagname): etisserant@0: varlist = [] lbessard@121: words = tagname.split("::") etisserant@0: for var in self.GetConfigurationGlobalVars(words[1]): etisserant@0: if var["Type"] == "BOOL": etisserant@0: varlist.append(var["Name"]) etisserant@0: for var in self.GetConfigurationResourceGlobalVars(words[1], words[2]): etisserant@0: if var["Type"] == "BOOL": etisserant@0: varlist.append(var["Name"]) etisserant@0: return varlist etisserant@0: lbessard@137: def SetEditedResourceInfos(self, tagname, tasks, instances): lbessard@121: resource = self.GetEditedElement(tagname) lbessard@121: if resource is not None: lbessard@151: resource.settask([]) lbessard@151: resource.setpouInstance([]) lbessard@121: task_list = {} lbessard@121: for task in tasks: lbessard@121: new_task = plcopen.resource_task() lbessard@151: new_task.setname(task["Name"]) lbessard@121: if task["Single"] != "": lbessard@151: new_task.setsingle(task["Single"]) lbessard@121: result = duration_model.match(task["Interval"]).groups() lbessard@121: if reduce(lambda x, y: x or y != None, result): lbessard@121: values = [] lbessard@121: for value in result[:-1]: lbessard@121: if value != None: lbessard@121: values.append(int(value)) lbessard@121: else: lbessard@121: values.append(0) lbessard@121: values.append(int(float(result[-1]) * 1000)) lbessard@151: new_task.setinterval(time(*values)) lbessard@151: new_task.setpriority(int(task["Priority"])) lbessard@121: if task["Name"] != "": lbessard@121: task_list[task["Name"]] = new_task lbessard@151: resource.appendtask(new_task) lbessard@121: for instance in instances: lbessard@121: new_instance = plcopen.pouInstance() lbessard@151: new_instance.setname(instance["Name"]) lbessard@151: new_instance.settype(instance["Type"]) lbessard@121: if instance["Task"] != "": lbessard@151: task_list[instance["Task"]].appendpouInstance(new_instance) lbessard@121: else: lbessard@151: resource.appendpouInstance(new_instance) lbessard@121: lbessard@121: def GetEditedResourceInfos(self, tagname): lbessard@121: resource = self.GetEditedElement(tagname) lbessard@121: if resource is not None: lbessard@151: tasks = resource.gettask() lbessard@151: instances = resource.getpouInstance() lbessard@121: tasks_data = [] lbessard@121: instances_data = [] lbessard@121: for task in tasks: lbessard@121: new_task = {} lbessard@151: new_task["Name"] = task.getname() lbessard@151: single = task.getsingle() lbessard@121: if single: lbessard@121: new_task["Single"] = single lbessard@121: else: lbessard@121: new_task["Single"] = "" lbessard@151: interval = task.getinterval() lbessard@121: if interval: lbessard@121: text = "" lbessard@121: if interval.hour != 0: lbessard@121: text += "%dh"%interval.hour lbessard@121: if interval.minute != 0: lbessard@121: text += "%dm"%interval.minute lbessard@121: if interval.second != 0: lbessard@121: text += "%ds"%interval.second lbessard@121: if interval.microsecond != 0: lbessard@121: if interval.microsecond % 1000 != 0: lbessard@121: text += "%.3fms"%(float(interval.microsecond) / 1000) lbessard@121: else: lbessard@121: text += "%dms"%(interval.microsecond / 1000) lbessard@121: new_task["Interval"] = text lbessard@121: else: lbessard@121: new_task["Interval"] = "" lbessard@151: new_task["Priority"] = str(task.getpriority()) lbessard@121: tasks_data.append(new_task) lbessard@151: for instance in task.getpouInstance(): lbessard@121: new_instance = {} lbessard@151: new_instance["Name"] = instance.getname() lbessard@151: new_instance["Type"] = instance.gettype() lbessard@151: new_instance["Task"] = task.getname() lbessard@121: instances_data.append(new_instance) lbessard@121: for instance in instances: etisserant@0: new_instance = {} lbessard@151: new_instance["Name"] = instance.getname() lbessard@151: new_instance["Type"] = instance.gettype() lbessard@121: new_instance["Task"] = "" etisserant@0: instances_data.append(new_instance) lbessard@121: return tasks_data, instances_data etisserant@0: etisserant@0: def OpenXMLFile(self, filepath): lbessard@151: xmlfile = open(filepath, 'r') lbessard@151: tree = minidom.parse(xmlfile) lbessard@151: xmlfile.close() etisserant@0: etisserant@0: self.Project = plcopen.project() lbessard@116: for child in tree.childNodes: lbessard@116: if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "project": lbessard@151: self.Project.loadXMLTree(child, ["xmlns", "xmlns:xhtml", "xmlns:xsi", "xsi:schemaLocation"]) lbessard@116: self.SetFilePath(filepath) lbessard@116: self.ProjectBuffer = UndoBuffer(self.Copy(self.Project), True) lbessard@116: self.Buffering = False lbessard@116: self.ElementsOpened = [] lbessard@116: self.CurrentElementEditing = None lbessard@125: self.RefreshDataTypeUsingTree() lbessard@125: self.RefreshDataTypes() lbessard@116: self.RefreshPouUsingTree() lbessard@116: self.RefreshBlockTypes() lbessard@117: return None lbessard@117: return "No PLC project found" etisserant@0: etisserant@0: def SaveXMLFile(self, filepath = None): etisserant@0: if not filepath and self.FilePath == "": etisserant@0: return False etisserant@0: else: lbessard@151: contentheader = self.Project.getcontentHeader() lbessard@145: contentheader["modificationDateTime"] = datetime(*localtime()[:6]) lbessard@151: self.Project.setcontentHeader(contentheader) lbessard@145: etisserant@0: text = "\n" etisserant@0: extras = {"xmlns" : "http://www.plcopen.org/xml/tc6.xsd", etisserant@0: "xmlns:xhtml" : "http://www.w3.org/1999/xhtml", etisserant@0: "xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", etisserant@0: "xsi:schemaLocation" : "http://www.plcopen.org/xml/tc6.xsd http://www.plcopen.org/xml/tc6.xsd"} etisserant@0: text += self.Project.generateXMLText("project", 0, extras) lbessard@125: etisserant@0: if filepath: etisserant@0: xmlfile = open(filepath,"w") etisserant@0: else: etisserant@0: xmlfile = open(self.FilePath,"w") etisserant@0: xmlfile.write(text) etisserant@0: xmlfile.close() lbessard@56: self.ProjectBuffer.CurrentSaved() etisserant@0: if filepath: etisserant@0: self.SetFilePath(filepath) etisserant@0: return True etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Current Buffering Management Functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Return a copy of the project etisserant@0: """ etisserant@0: def Copy(self, model): etisserant@0: return cPickle.loads(cPickle.dumps(model)) etisserant@0: etisserant@0: def BufferProject(self): lbessard@56: self.ProjectBuffer.Buffering(self.Copy(self.Project)) lbessard@56: lbessard@56: def StartBuffering(self): lbessard@56: self.ProjectBuffer.Buffering(self.Project) lbessard@56: self.Buffering = True lbessard@56: lbessard@56: def EndBuffering(self): lbessard@56: if self.Buffering: lbessard@56: self.Project = self.Copy(self.Project) lbessard@56: self.Buffering = False etisserant@0: etisserant@0: def ProjectIsSaved(self): lbessard@58: if self.ProjectBuffer: lbessard@58: return self.ProjectBuffer.IsCurrentSaved() lbessard@58: else: lbessard@62: return True etisserant@0: etisserant@0: def LoadPrevious(self): lbessard@27: self.Project = self.Copy(self.ProjectBuffer.Previous()) etisserant@0: etisserant@0: def LoadNext(self): lbessard@27: self.Project = self.Copy(self.ProjectBuffer.Next()) etisserant@0: etisserant@0: def GetBufferState(self): lbessard@27: first = self.ProjectBuffer.IsFirst() lbessard@27: last = self.ProjectBuffer.IsLast() etisserant@0: return not first, not last