svghmi/svghmi.py
branchsvghmi
changeset 2814 2cabc4773885
parent 2812 68ac5bf43525
child 2815 77b2a3757e66
equal deleted inserted replaced
2813:ac736a878188 2814:2cabc4773885
    23 from util.ProcessLogger import ProcessLogger
    23 from util.ProcessLogger import ProcessLogger
    24 from runtime.typemapping import DebugTypesSize
    24 from runtime.typemapping import DebugTypesSize
    25 import targets
    25 import targets
    26 
    26 
    27 HMI_TYPES_DESC = {
    27 HMI_TYPES_DESC = {
    28     "HMI_CLASS":{},
    28     "HMI_NODE":{},
    29     "HMI_LABEL":{},
       
    30     "HMI_STRING":{},
    29     "HMI_STRING":{},
    31     "HMI_INT":{},
    30     "HMI_INT":{},
    32     "HMI_REAL":{}
    31     "HMI_REAL":{}
    33 }
    32 }
    34 
    33 
    35 HMI_TYPES = HMI_TYPES_DESC.keys()
    34 HMI_TYPES = HMI_TYPES_DESC.keys()
    36 
    35 
    37 from XSLTransform import XSLTransform
    36 from XSLTransform import XSLTransform
       
    37 from lxml.etree import XSLTApplyError
    38 
    38 
    39 ScriptDirectory = paths.AbsDir(__file__)
    39 ScriptDirectory = paths.AbsDir(__file__)
    40 
    40 
    41 class HMITreeNode(object):
    41 class HMITreeNode(object):
    42     def __init__(self, path, name, nodetype, iectype = None, vartype = None):
    42     def __init__(self, path, name, nodetype, iectype = None, vartype = None, hmiclass = None):
    43         self.path = path
    43         self.path = path
    44         self.name = name
    44         self.name = name
    45         self.nodetype = nodetype
    45         self.nodetype = nodetype
       
    46         self.hmiclass = hmiclass
    46 
    47 
    47         if iectype is not None:
    48         if iectype is not None:
    48             self.iectype = iectype
    49             self.iectype = iectype
    49             self.vartype = vartype
    50             self.vartype = vartype
    50         if nodetype in ["HMI_LABEL", "HMI_ROOT"]:
    51         if nodetype in ["HMI_NODE", "HMI_ROOT"]:
    51             self.children = []
    52             self.children = []
    52 
    53 
    53     def pprint(self, indent = 0):
    54     def pprint(self, indent = 0):
    54         res = ">"*indent + pformat(self.__dict__, indent = indent, depth = 1) + "\n"
    55         res = ">"*indent + pformat(self.__dict__, indent = indent, depth = 1) + "\n"
    55         if hasattr(self, "children"): 
    56         if hasattr(self, "children"): 
    71                     else:
    72                     else:
    72                         break
    73                         break
    73                 if in_common > known_best_match:
    74                 if in_common > known_best_match:
    74                     known_best_match = in_common
    75                     known_best_match = in_common
    75                     best_child = child
    76                     best_child = child
    76         if best_child is not None and best_child.nodetype == "HMI_LABEL":
    77         if best_child is not None and best_child.nodetype == "HMI_NODE":
    77             best_child.place_node(node)
    78             best_child.place_node(node)
    78         else:
    79         else:
    79             self.children.append(node)
    80             self.children.append(node)
    80             
    81             
    81     def etree(self, add_hash=False):
    82     def etree(self, add_hash=False):
   134         PLC Instance Tree:
   135         PLC Instance Tree:
   135           prog0
   136           prog0
   136            +->v1 HMI_INT
   137            +->v1 HMI_INT
   137            +->v2 HMI_INT
   138            +->v2 HMI_INT
   138            +->fb0 (type mhoo)
   139            +->fb0 (type mhoo)
   139            |   +->va HMI_LABEL
   140            |   +->va HMI_NODE
   140            |   +->v3 HMI_INT
   141            |   +->v3 HMI_INT
   141            |   +->v4 HMI_INT
   142            |   +->v4 HMI_INT
   142            |
   143            |
   143            +->fb1 (type mhoo)
   144            +->fb1 (type mhoo)
   144            |   +->va HMI_LABEL
   145            |   +->va HMI_NODE
   145            |   +->v3 HMI_INT
   146            |   +->v3 HMI_INT
   146            |   +->v4 HMI_INT
   147            |   +->v4 HMI_INT
   147            |
   148            |
   148            +->fb2
   149            +->fb2
   149                +->v5 HMI_IN
   150                +->v5 HMI_IN
   150 
   151 
   151         HMI tree:
   152         HMI tree:
   152           hmi0
   153           hmi0
   153            +->v1
   154            +->v1
   154            +->v2
   155            +->v2
   155            +->fb0_va
   156            +->fb0 class:va
   156            |   +-> v3
   157            |   +-> v3
   157            |   +-> v4
   158            |   +-> v4
   158            |
   159            |
   159            +->fb1_va
   160            +->fb1 class:va
   160            |   +-> v3
   161            |   +-> v3
   161            |   +-> v4
   162            |   +-> v4
   162            |
   163            |
   163            +->v5
   164            +->v5
   164 
   165 
   178         for v in hmi_types_instances:
   179         for v in hmi_types_instances:
   179             path = v["C_path"].split(".")
   180             path = v["C_path"].split(".")
   180             # ignores variables starting with _TMP_
   181             # ignores variables starting with _TMP_
   181             if path[-1].startswith("_TMP_"):
   182             if path[-1].startswith("_TMP_"):
   182                 continue
   183                 continue
   183             new_node = HMITreeNode(path, path[-1], v["derived"], v["type"], v["vartype"])
   184             derived = v["derived"]
       
   185             kwargs={}
       
   186             if derived == "HMI_NODE":
       
   187                 name = path[-2]
       
   188                 kwargs['hmiclass'] = path[-1]
       
   189             else:
       
   190                 name = path[-1]
       
   191             new_node = HMITreeNode(path, name, derived, v["type"], v["vartype"], **kwargs)
   184             hmi_tree_root.place_node(new_node)
   192             hmi_tree_root.place_node(new_node)
   185 
   193 
   186         variable_decl_array = []
   194         variable_decl_array = []
   187         extern_variables_declarations = []
   195         extern_variables_declarations = []
   188         buf_index = 0
   196         buf_index = 0
   189         item_count = 0
   197         item_count = 0
   190         for node in hmi_tree_root.traverse():
   198         for node in hmi_tree_root.traverse():
   191             if hasattr(node, "iectype") and \
   199             if hasattr(node, "iectype") and \
   192                node.nodetype not in ["HMI_CLASS", "HMI_LABEL"]:
   200                node.nodetype not in ["HMI_NODE"]:
   193                 sz = DebugTypesSize.get(node.iectype, 0)
   201                 sz = DebugTypesSize.get(node.iectype, 0)
   194                 variable_decl_array += [
   202                 variable_decl_array += [
   195                     "{&(" + ".".join(node.path) + "), " + node.iectype + {
   203                     "{&(" + ".".join(node.path) + "), " + node.iectype + {
   196                         "EXT": "_P_ENUM",
   204                         "EXT": "_P_ENUM",
   197                         "IN":  "_P_ENUM",
   205                         "IN":  "_P_ENUM",
   355 
   363 
   356             # load svg as a DOM with Etree
   364             # load svg as a DOM with Etree
   357             svgdom = etree.parse(svgfile)
   365             svgdom = etree.parse(svgfile)
   358 
   366 
   359             # call xslt transform on Inkscape's SVG to generate XHTML
   367             # call xslt transform on Inkscape's SVG to generate XHTML
   360             result = transform.transform(svgdom)
   368             try:
       
   369                 result = transform.transform(svgdom)
       
   370             except XSLTApplyError as e:
       
   371                 self.FatalError("SVGHMI " + view_name  + ": " + e.message)
   361            
   372            
   362             result.write(target_file, encoding="utf-8")
   373             result.write(target_file, encoding="utf-8")
   363             # print(str(result))
   374             # print(str(result))
   364             # print(transform.xslt.error_log)
   375             # print(transform.xslt.error_log)
   365 
   376