PLCControler.py
changeset 67 3a1b0afdaf84
parent 63 04a02b4b2a57
child 68 66308e07402c
equal deleted inserted replaced
66:fd138fc77510 67:3a1b0afdaf84
    22 #License along with this library; if not, write to the Free Software
    22 #License along with this library; if not, write to the Free Software
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24 
    24 
    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 import cPickle
    28 import cPickle
    28 import os,sys,re
    29 import os,sys,re
    29 from datetime import *
    30 from datetime import *
    30 
    31 
    31 from plcopen import plcopen
    32 from plcopen import plcopen
   380                 return used
   381                 return used
   381         return False
   382         return False
   382 
   383 
   383     def GenerateProgram(self, filepath):
   384     def GenerateProgram(self, filepath):
   384         if self.Project:
   385         if self.Project:
   385             try:
   386             #try:
   386                 program = GenerateCurrentProgram(self.Project)
   387             program = GenerateCurrentProgram(self.Project)
   387                 programfile = open(filepath, "w")
   388             programfile = open(filepath, "w")
   388                 programfile.write(program)
   389             programfile.write(program)
   389                 programfile.close()
   390             programfile.close()
   390                 self.ProgramFilePath = filepath
   391             self.ProgramFilePath = filepath
   391                 return True
   392             return True
   392             except:
   393             #except:
   393                 pass
   394             #    pass
   394         return False
   395         return False
   395 
   396 
   396 #-------------------------------------------------------------------------------
   397 #-------------------------------------------------------------------------------
   397 #                        Project Pous management functions
   398 #                        Project Pous management functions
   398 #-------------------------------------------------------------------------------
   399 #-------------------------------------------------------------------------------
   624                     varlist.setConstant(True)
   625                     varlist.setConstant(True)
   625             # Create variable and change its properties
   626             # Create variable and change its properties
   626             tempvar = plcopen.varListPlain_variable()
   627             tempvar = plcopen.varListPlain_variable()
   627             tempvar.setName(var["Name"])
   628             tempvar.setName(var["Name"])
   628             var_type = plcopen.dataType()
   629             var_type = plcopen.dataType()
   629             var_type.setValue(var["Type"])
   630             if GetBlockType(var["Type"]) != None:
       
   631                 derived_type = plcopen.derived()
       
   632                 derived_type.setName(var["Type"])
       
   633                 var_type.setValue(derived_type)
       
   634             else:
       
   635                 var_type.setValue(var["Type"])
   630             tempvar.setType(var_type)
   636             tempvar.setType(var_type)
   631             if var["Initial Value"] != "":
   637             if var["Initial Value"] != "":
   632                 value = plcopen.value()
   638                 value = plcopen.value()
   633                 value.setValue(var["Initial Value"])
   639                 value.setValue(var["Initial Value"])
   634                 tempvar.setInitialValue(value)
   640                 tempvar.setInitialValue(value)
   658         configuration = self.Project.getConfiguration(name)
   664         configuration = self.Project.getConfiguration(name)
   659         if configuration:
   665         if configuration:
   660             # Extract variables from every varLists
   666             # Extract variables from every varLists
   661             for varlist in configuration.getGlobalVars():
   667             for varlist in configuration.getGlobalVars():
   662                 for var in varlist.getVariable():
   668                 for var in varlist.getVariable():
   663                     tempvar = {"Name":var.getName(),"Class":"Global","Type":var.getType().getValue()}
   669                     tempvar = {"Name":var.getName(),"Class":"Global"}
       
   670                     var_type = var.getType().getValue()
       
   671                     if isinstance(var_type, (StringType, UnicodeType)):
       
   672                         tempvar["Type"] = var_type
       
   673                     else:
       
   674                         tempvar["Type"] = var_type.getName()
   664                     initial = var.getInitialValue()
   675                     initial = var.getInitialValue()
   665                     if initial:
   676                     if initial:
   666                         tempvar["Initial Value"] = initial.getValue()
   677                         tempvar["Initial Value"] = initial.getValue()
   667                     else:
   678                     else:
   668                         tempvar["Initial Value"] = ""
   679                         tempvar["Initial Value"] = ""
   700         resource = self.Project.getConfigurationResource(config_name, name)
   711         resource = self.Project.getConfigurationResource(config_name, name)
   701         if resource:
   712         if resource:
   702             # Extract variables from every varLists
   713             # Extract variables from every varLists
   703             for varlist in resource.getGlobalVars():
   714             for varlist in resource.getGlobalVars():
   704                 for var in varlist.getVariable():
   715                 for var in varlist.getVariable():
   705                     tempvar = {"Name":var.getName(),"Class":"Global","Type":var.getType().getValue()}
   716                     tempvar = {"Name":var.getName(),"Class":"Global"}
       
   717                     var_type = var.getType().getValue()
       
   718                     if isinstance(var_type, (StringType, UnicodeType)):
       
   719                         tempvar["Type"] = var_type
       
   720                     else:
       
   721                         tempvar["Type"] = var_type.getName()
   706                     initial = var.getInitialValue()
   722                     initial = var.getInitialValue()
   707                     if initial:
   723                     if initial:
   708                         tempvar["Initial Value"] = initial.getValue()
   724                         tempvar["Initial Value"] = initial.getValue()
   709                     else:
   725                     else:
   710                         tempvar["Initial Value"] = ""
   726                         tempvar["Initial Value"] = ""
   735         # Verify that the pou has an interface
   751         # Verify that the pou has an interface
   736         if pou.interface:
   752         if pou.interface:
   737             # Extract variables from every varLists
   753             # Extract variables from every varLists
   738             for type, varlist in pou.getVars():
   754             for type, varlist in pou.getVars():
   739                 for var in varlist.getVariable():
   755                 for var in varlist.getVariable():
   740                     tempvar = {"Name":var.getName(),"Class":type,"Type":var.getType().getValue()}
   756                     tempvar = {"Name":var.getName(),"Class":type}
       
   757                     var_type = var.getType().getValue()
       
   758                     if isinstance(var_type, (StringType, UnicodeType)):
       
   759                         tempvar["Type"] = var_type
       
   760                     else:
       
   761                         tempvar["Type"] = var_type.getName()
   741                     initial = var.getInitialValue()
   762                     initial = var.getInitialValue()
   742                     if initial:
   763                     if initial:
   743                         tempvar["Initial Value"] = initial.getValue()
   764                         tempvar["Initial Value"] = initial.getValue()
   744                     else:
   765                     else:
   745                         tempvar["Initial Value"] = ""
   766                         tempvar["Initial Value"] = ""
  1416                     connection.setConnectionParameter(idx, formalParameter)
  1437                     connection.setConnectionParameter(idx, formalParameter)
  1417                 else:
  1438                 else:
  1418                     connection.setConnectionParameter(idx, None)
  1439                     connection.setConnectionParameter(idx, None)
  1419                 idx += 1
  1440                 idx += 1
  1420     
  1441     
  1421     def AddCurrentElementEditingBlock(self, id):
  1442     def AddCurrentElementEditingBlock(self, id, blocktype, blockname = None):
  1422         block = plcopen.block()
  1443         block = plcopen.block()
  1423         block.setLocalId(id)
  1444         block.setLocalId(id)
  1424         self.GetCurrentElementEditing().addInstance("block", block)
  1445         block.setTypeName(blocktype)
       
  1446         if blockname:
       
  1447             block.setInstanceName(blockname)
       
  1448         element = self.GetCurrentElementEditing()
       
  1449         blocktype_infos = GetBlockType(blocktype)
       
  1450         if blocktype_infos["type"] != "function":
       
  1451             if self.CurrentElementEditing != None:
       
  1452                 name = self.ElementsOpened[self.CurrentElementEditing]
       
  1453                 words = name.split("::")
       
  1454                 if len(words) == 1:
       
  1455                     element.addPouVar(blocktype, blockname)
       
  1456                 elif words[0] in ['T', 'A']:
       
  1457                     pou = self.Project.getPou(words[1])
       
  1458                     pou.addPouVar(blocktype, blockname)    
       
  1459         element.addInstance("block", block)
  1425         self.RefreshPouUsingTree()
  1460         self.RefreshPouUsingTree()
  1426     
  1461     
  1427     def SetCurrentElementEditingBlockInfos(self, id, infos):
  1462     def SetCurrentElementEditingBlockInfos(self, id, infos):
  1428         block = self.GetCurrentElementEditing().getInstance(id)
  1463         block = self.GetCurrentElementEditing().getInstance(id)
  1429         for param, value in infos.items():
  1464         for param, value in infos.items():
  1886                 actionBlock.addConnectionPointIn()
  1921                 actionBlock.addConnectionPointIn()
  1887                 actionBlock.connectionPointIn.setRelPosition(position.x, position.y)
  1922                 actionBlock.connectionPointIn.setRelPosition(position.x, position.y)
  1888                 self.SetConnectionWires(actionBlock.connectionPointIn, value)
  1923                 self.SetConnectionWires(actionBlock.connectionPointIn, value)
  1889     
  1924     
  1890     def RemoveCurrentElementEditingInstance(self, id):
  1925     def RemoveCurrentElementEditingInstance(self, id):
  1891         self.GetCurrentElementEditing().removeInstance(id)
  1926         element = self.GetCurrentElementEditing()
       
  1927         instance = element.getInstance(id)
       
  1928         if isinstance(instance, plcopen.block):
       
  1929             blocktype = instance.getTypeName()
       
  1930             blocktype_infos = GetBlockType(blocktype)
       
  1931             if blocktype_infos["type"] != "function":
       
  1932                 if self.CurrentElementEditing != None:
       
  1933                     name = self.ElementsOpened[self.CurrentElementEditing]
       
  1934                     words = name.split("::")
       
  1935                     if len(words) == 1:
       
  1936                         element.removePouVar(blocktype, instance.getInstanceName())
       
  1937                     elif words[0] in ['T', 'A']:
       
  1938                         pou = self.Project.getPou(words[1])
       
  1939                         pou.removePouVar(blocktype, instance.getInstanceName())    
       
  1940         element.removeInstance(id)
  1892         self.RefreshPouUsingTree()
  1941         self.RefreshPouUsingTree()
  1893 
  1942 
  1894     def GetCurrentResourceEditingVariables(self):
  1943     def GetCurrentResourceEditingVariables(self):
  1895         varlist = []
  1944         varlist = []
  1896         name = self.ElementsOpened[self.CurrentElementEditing]
  1945         name = self.ElementsOpened[self.CurrentElementEditing]