svghmi/svghmi.py
branchsvghmi
changeset 2763 ce04d79b8e57
parent 2762 282500e03dbc
child 2764 b75cc2cf4e50
equal deleted inserted replaced
2762:282500e03dbc 2763:ce04d79b8e57
     7 # See COPYING file for copyrights details.
     7 # See COPYING file for copyrights details.
     8 
     8 
     9 from __future__ import absolute_import
     9 from __future__ import absolute_import
    10 import os
    10 import os
    11 import shutil
    11 import shutil
    12 from itertools import izip
    12 from itertools import izip, imap
    13 from pprint import pprint, pformat
    13 from pprint import pprint, pformat
    14 
    14 
    15 import wx
    15 import wx
    16 
    16 
    17 import util.paths as paths
    17 import util.paths as paths
    69         if best_child is not None and best_child.nodetype == "HMI_LABEL":
    69         if best_child is not None and best_child.nodetype == "HMI_LABEL":
    70             best_child.place_node(node)
    70             best_child.place_node(node)
    71         else:
    71         else:
    72             self.children.append(node)
    72             self.children.append(node)
    73             
    73             
       
    74     def etree(self):
       
    75 
       
    76         attribs = dict(name=self.name)
       
    77         if self.path is not None:
       
    78             attribs["path"]=".".join(self.path)
       
    79 
       
    80         res = etree.Element(self.nodetype, **attribs)
       
    81 
       
    82         if hasattr(self, "children"): 
       
    83             for child_etree in imap(lambda c:c.etree(), self.children):
       
    84                 res.append(child_etree)
       
    85 
       
    86         return res
       
    87 
       
    88 # module scope for HMITree root
       
    89 # so that CTN can use HMITree deduced in Library
       
    90 # note: this only works because library's Generate_C is 
       
    91 #       systematicaly invoked before CTN's CTNGenerate_C
       
    92 
       
    93 hmi_tree_root = None
    74 
    94 
    75 class SVGHMILibrary(POULibrary):
    95 class SVGHMILibrary(POULibrary):
    76     def GetLibraryPath(self):
    96     def GetLibraryPath(self):
    77          return paths.AbsNeighbourFile(__file__, "pous.xml")
    97          return paths.AbsNeighbourFile(__file__, "pous.xml")
    78 
    98 
    79     def Generate_C(self, buildpath, varlist, IECCFLAGS):
    99     def Generate_C(self, buildpath, varlist, IECCFLAGS):
       
   100         global hmi_tree_root
    80 
   101 
    81         """
   102         """
    82         PLC Instance Tree:
   103         PLC Instance Tree:
    83           prog0
   104           prog0
    84            +->v1 HMI_INT
   105            +->v1 HMI_INT
   207 
   228 
   208             res.append(etree.Element("bbox", **attrs))
   229             res.append(etree.Element("bbox", **attrs))
   209 
   230 
   210         return res
   231         return res
   211 
   232 
       
   233     def GetHMITree(self):
       
   234         global hmi_tree_root
       
   235         res = [hmi_tree_root.etree()]
       
   236         return res
       
   237 
   212     def CTNGenerate_C(self, buildpath, locations):
   238     def CTNGenerate_C(self, buildpath, locations):
   213         """
   239         """
   214         Return C code generated by iec2c compiler
   240         Return C code generated by iec2c compiler
   215         when _generate_softPLC have been called
   241         when _generate_softPLC have been called
   216         @param locations: ignored
   242         @param locations: ignored
   222         svgfile = self._getSVGpath()
   248         svgfile = self._getSVGpath()
   223         if os.path.exists(svgfile):
   249         if os.path.exists(svgfile):
   224 
   250 
   225             # TODO : move to __init__
   251             # TODO : move to __init__
   226             transform = XSLTransform(os.path.join(ScriptDirectory, "gen_index_xhtml.xslt"),
   252             transform = XSLTransform(os.path.join(ScriptDirectory, "gen_index_xhtml.xslt"),
   227                           [("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry())])
   253                           [("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry()),
       
   254                            ("GetHMITree", lambda *_ignored:self.GetHMITree())])
   228 
   255 
   229 
   256 
   230             # load svg as a DOM with Etree
   257             # load svg as a DOM with Etree
   231             svgdom = etree.parse(svgfile)
   258             svgdom = etree.parse(svgfile)
   232 
   259