PLCControler.py
changeset 145 4fb225afddf4
parent 144 b67a5de5a24a
child 151 aaa80b48bead
equal deleted inserted replaced
144:b67a5de5a24a 145:4fb225afddf4
    25 from minixsv import pyxsval
    25 from minixsv import pyxsval
    26 from xml.dom import minidom
    26 from xml.dom import minidom
    27 from types import StringType, UnicodeType
    27 from types import StringType, UnicodeType
    28 import cPickle
    28 import cPickle
    29 import os,sys,re
    29 import os,sys,re
       
    30 from time import localtime
    30 from datetime import *
    31 from datetime import *
    31 
    32 
    32 from plcopen import plcopen
    33 from plcopen import plcopen
    33 from plcopen.structures import *
    34 from plcopen.structures import *
    34 from graphics.GraphicCommons import *
    35 from graphics.GraphicCommons import *
   186     # Return if a project is opened
   187     # Return if a project is opened
   187     def HasOpenedProject(self):
   188     def HasOpenedProject(self):
   188         return self.Project != None
   189         return self.Project != None
   189 
   190 
   190     # Create a new project by replacing the current one
   191     # Create a new project by replacing the current one
   191     def CreateNewProject(self, name):
   192     def CreateNewProject(self, properties):
   192         # Create the project
   193         # Create the project
   193         self.Project = plcopen.project()
   194         self.Project = plcopen.project()
   194         self.Project.setName(name)
   195         properties["creationDateTime"] = datetime(*localtime()[:6])
       
   196         self.Project.setFileHeader(properties)
       
   197         self.Project.setContentHeader(properties)
   195         self.SetFilePath("")
   198         self.SetFilePath("")
   196         # Initialize the project buffer
   199         # Initialize the project buffer
   197         self.ProjectBuffer = UndoBuffer(self.Copy(self.Project), False)
   200         self.ProjectBuffer = UndoBuffer(self.Copy(self.Project), False)
   198         self.Buffering = False
   201         self.Buffering = False
   199     
   202     
   259     def SetProjectProperties(self, name = None, properties = None):
   262     def SetProjectProperties(self, name = None, properties = None):
   260         if name != None:
   263         if name != None:
   261             self.Project.setName(name)
   264             self.Project.setName(name)
   262         if properties != None:
   265         if properties != None:
   263             self.Project.setFileHeader(properties)
   266             self.Project.setFileHeader(properties)
       
   267             self.Project.setContentHeader(properties)
   264         if name != None or properties != None:
   268         if name != None or properties != None:
   265             self.BufferProject()
   269             self.BufferProject()
   266             
   270             
   267     # Return project properties
   271     # Return project properties
   268     def GetProjectProperties(self):
   272     def GetProjectProperties(self):
   269         properties = self.Project.getFileHeader()
   273         properties = self.Project.getFileHeader()
   270         properties["projectName"] = self.Project.getName()
   274         properties.update(self.Project.getContentHeader())
   271         return properties
   275         return properties
   272     
   276     
   273     # Return project informations
   277     # Return project informations
   274     def GetProjectInfos(self):
   278     def GetProjectInfos(self):
   275         if self.Project:
   279         if self.Project:
  1418                     for link in connections:
  1422                     for link in connections:
  1419                         dic = {"refLocalId":link.getRefLocalId(),"points":link.getPoints(),"formalParameter":link.getFormalParameter()}
  1423                         dic = {"refLocalId":link.getRefLocalId(),"points":link.getPoints(),"formalParameter":link.getFormalParameter()}
  1420                         infos["connectors"]["input"]["links"].append(dic)
  1424                         infos["connectors"]["input"]["links"].append(dic)
  1421             elif isinstance(instance, plcopen.continuation):
  1425             elif isinstance(instance, plcopen.continuation):
  1422                 infos["name"] = instance.getName()
  1426                 infos["name"] = instance.getName()
  1423                 infos["value_type"] = self.GetCurrentPouVarValueType(infos["name"])
       
  1424                 infos["type"] = "continuation"
  1427                 infos["type"] = "continuation"
  1425                 executionOrder = instance.getExecutionOrderId()
       
  1426                 if executionOrder is not None:
       
  1427                     infos["executionOrder"] = executionOrder
       
  1428                 else:
       
  1429                     infos["executionOrder"] = 0
       
  1430                 infos["connector"] = {}
  1428                 infos["connector"] = {}
  1431                 infos["connector"]["position"] = instance.connectionPointOut.getRelPosition()
  1429                 infos["connector"]["position"] = instance.connectionPointOut.getRelPosition()
  1432             elif isinstance(instance, plcopen.connector):
  1430             elif isinstance(instance, plcopen.connector):
  1433                 infos["name"] = instance.getName()
  1431                 infos["name"] = instance.getName()
  1434                 infos["value_type"] = self.GetCurrentPouVarValueType(infos["name"])
       
  1435                 infos["type"] = "connection"
  1432                 infos["type"] = "connection"
  1436                 executionOrder = instance.getExecutionOrderId()
       
  1437                 if executionOrder is not None:
       
  1438                     infos["executionOrder"] = executionOrder
       
  1439                 else:
       
  1440                     infos["executionOrder"] = 0
       
  1441                 infos["connector"] = {}
  1433                 infos["connector"] = {}
  1442                 infos["connector"]["position"] = instance.connectionPointIn.getRelPosition()
  1434                 infos["connector"]["position"] = instance.connectionPointIn.getRelPosition()
  1443                 infos["connector"]["links"] = []
  1435                 infos["connector"]["links"] = []
  1444                 connections = instance.connectionPointIn.getConnections()
  1436                 connections = instance.connectionPointIn.getConnections()
  1445                 if connections:
  1437                 if connections:
  1662                     connection.setConnectionParameter(idx, formalParameter)
  1654                     connection.setConnectionParameter(idx, formalParameter)
  1663                 else:
  1655                 else:
  1664                     connection.setConnectionParameter(idx, None)
  1656                     connection.setConnectionParameter(idx, None)
  1665                 idx += 1
  1657                 idx += 1
  1666     
  1658     
       
  1659     def AddEditedElementPouVar(self, tagname, type, name):
       
  1660         words = tagname.split("::")
       
  1661         if words[0] in ['P', 'T', 'A']:
       
  1662             pou = self.Project.getPou(words[1])
       
  1663             pou.addPouVar(type, name)
       
  1664             
       
  1665     def ChangeEditedElementPouVar(self, tagname, old_type, old_name, new_type, new_name):
       
  1666         words = tagname.split("::")
       
  1667         if words[0] in ['P', 'T', 'A']:
       
  1668             pou = self.Project.getPou(words[1])
       
  1669             pou.changePouVar(old_type, old_name, new_type, new_name)
       
  1670     
  1667     def AddEditedElementBlock(self, tagname, id, blocktype, blockname = None):
  1671     def AddEditedElementBlock(self, tagname, id, blocktype, blockname = None):
  1668         element = self.GetEditedElement(tagname)
  1672         element = self.GetEditedElement(tagname)
  1669         if element is not None:
  1673         if element is not None:
  1670             block = plcopen.block()
  1674             block = plcopen.block()
  1671             block.setLocalId(id)
  1675             block.setLocalId(id)
  1672             block.setTypeName(blocktype)
  1676             block.setTypeName(blocktype)
  1673             blocktype_infos = GetBlockType(blocktype)
  1677             blocktype_infos = GetBlockType(blocktype)
  1674             if blocktype_infos["type"] != "function" and blockname is not None:
  1678             if blocktype_infos["type"] != "function" and blockname is not None:
  1675                 block.setInstanceName(blockname)
  1679                 block.setInstanceName(blockname)
  1676                 element.addPouVar(blocktype, blockname)    
  1680                 self.AddEditedElementPouVar(tagname, blocktype, blockname)
  1677             element.addInstance("block", block)
  1681             element.addInstance("block", block)
  1678             self.RefreshPouUsingTree()
  1682             self.RefreshPouUsingTree()
  1679     
  1683     
  1680     def SetEditedElementBlockInfos(self, tagname, id, infos):
  1684     def SetEditedElementBlockInfos(self, tagname, id, infos):
  1681         element = self.GetEditedElement(tagname)
  1685         element = self.GetEditedElement(tagname)
  1682         if element is not None:
  1686         if element is not None:
  1683             block = element.getInstance(id)
  1687             block = element.getInstance(id)
  1684             if "name" in infos or "type" in infos:
  1688             blocktype = infos.get("type", block.getTypeName())
  1685                 old_name = block.getInstanceName()
  1689             blocktype_infos = GetBlockType(blocktype)
  1686                 old_type = block.getTypeName()
  1690             if blocktype_infos["type"] != "function":
  1687                 new_name = infos.get("name", old_name)
  1691                 if "name" in infos or "type" in infos:
  1688                 new_type = infos.get("type", old_type)
  1692                     old_name = block.getInstanceName()
  1689                 self.GetEditedElement(tagname).changePouVar(old_type, old_name, new_type, new_name)
  1693                     old_type = block.getTypeName()
       
  1694                     new_name = infos.get("name", old_name)
       
  1695                     new_type = infos.get("type", old_type)
       
  1696                     self.ChangeEditedElementPouVar(tagname, old_type, old_name, new_type, new_name)
  1690             for param, value in infos.items():
  1697             for param, value in infos.items():
  1691                 if param == "name":
  1698                 if param == "name":
  1692                     block.setInstanceName(value)
  1699                     block.setInstanceName(value)
  1693                 elif param == "type":
  1700                 elif param == "type":
  1694                     block.setTypeName(value)
  1701                     block.setTypeName(value)
  2335 
  2342 
  2336     def SaveXMLFile(self, filepath = None):
  2343     def SaveXMLFile(self, filepath = None):
  2337         if not filepath and self.FilePath == "":
  2344         if not filepath and self.FilePath == "":
  2338             return False
  2345             return False
  2339         else:
  2346         else:
       
  2347             contentheader = self.Project.getContentHeader()
       
  2348             contentheader["modificationDateTime"] = datetime(*localtime()[:6])
       
  2349             self.Project.setContentHeader(contentheader)
       
  2350             
  2340             text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  2351             text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  2341             extras = {"xmlns" : "http://www.plcopen.org/xml/tc6.xsd",
  2352             extras = {"xmlns" : "http://www.plcopen.org/xml/tc6.xsd",
  2342                       "xmlns:xhtml" : "http://www.w3.org/1999/xhtml",
  2353                       "xmlns:xhtml" : "http://www.w3.org/1999/xhtml",
  2343                       "xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
  2354                       "xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
  2344                       "xsi:schemaLocation" : "http://www.plcopen.org/xml/tc6.xsd http://www.plcopen.org/xml/tc6.xsd"}
  2355                       "xsi:schemaLocation" : "http://www.plcopen.org/xml/tc6.xsd http://www.plcopen.org/xml/tc6.xsd"}