svghmi/svghmi.py
author Edouard Tisserant
Fri, 23 Aug 2019 13:01:37 +0200
branchsvghmi
changeset 2765 887aba5ef178
parent 2764 b75cc2cf4e50
child 2767 302347f48193
permissions -rw-r--r--
SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
     3
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
     4
# This file is part of Beremiz
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
     5
# Copyright (C) 2019: Edouard TISSERANT
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
     6
#
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
     7
# See COPYING file for copyrights details.
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
     8
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
     9
from __future__ import absolute_import
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
    10
import os
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
    11
import shutil
2763
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    12
from itertools import izip, imap
2757
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    13
from pprint import pprint, pformat
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
    14
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
    15
import wx
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
    16
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
    17
import util.paths as paths
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
    18
from POULibrary import POULibrary
2756
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
    19
from docutil import open_svg, get_inkscape_path
2753
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
    20
from lxml import etree
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
    21
2756
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
    22
from util.ProcessLogger import ProcessLogger
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    23
from runtime.typemapping import DebugTypesSize
2756
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
    24
2749
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
    25
HMI_TYPES_DESC = {
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
    26
    "HMI_CLASS":{},
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
    27
    "HMI_LABEL":{},
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
    28
    "HMI_STRING":{},
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
    29
    "HMI_INT":{},
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
    30
    "HMI_REAL":{}
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
    31
}
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
    32
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
    33
HMI_TYPES = HMI_TYPES_DESC.keys()
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
    34
2753
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
    35
from XSLTransform import XSLTransform
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
    36
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
    37
ScriptDirectory = paths.AbsDir(__file__)
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
    38
2757
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    39
class HMITreeNode(object):
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    40
    def __init__(self, path, name, nodetype, iectype = None, vartype = None):
2757
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    41
        self.path = path
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    42
        self.name = name
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    43
        self.nodetype = nodetype
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    44
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    45
        if iectype is not None:
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    46
            self.iectype = iectype
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    47
            self.vartype = vartype
2757
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    48
        if nodetype in ["HMI_LABEL", "HMI_ROOT"]:
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    49
            self.children = []
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    50
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    51
    def pprint(self, indent = 0):
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    52
        res = ">"*indent + pformat(self.__dict__, indent = indent, depth = 1) + "\n"
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    53
        if hasattr(self, "children"): 
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    54
            res += "\n".join([child.pprint(indent = indent + 1)
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    55
                              for child in self.children])
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    56
            res += "\n"
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    57
            
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    58
        return res
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    59
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    60
    def place_node(self, node):
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    61
        best_child = None
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    62
        known_best_match = 0
2762
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    63
        for child in self.children : 
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    64
            if child.path is not None:
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    65
                in_common = 0
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    66
                for child_path_item, node_path_item in izip(child.path, node.path):
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    67
                    if child_path_item == node_path_item:
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    68
                        in_common +=1
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    69
                    else:
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    70
                        break
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    71
                if in_common > known_best_match:
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    72
                    known_best_match = in_common
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
    73
                    best_child = child
2757
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    74
        if best_child is not None and best_child.nodetype == "HMI_LABEL":
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    75
            best_child.place_node(node)
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    76
        else:
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    77
            self.children.append(node)
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
    78
            
2763
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    79
    def etree(self):
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    80
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    81
        attribs = dict(name=self.name)
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    82
        if self.path is not None:
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    83
            attribs["path"]=".".join(self.path)
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    84
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    85
        res = etree.Element(self.nodetype, **attribs)
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    86
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    87
        if hasattr(self, "children"): 
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    88
            for child_etree in imap(lambda c:c.etree(), self.children):
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    89
                res.append(child_etree)
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    90
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    91
        return res
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
    92
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    93
    def traverse(self):
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    94
        yield self
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    95
        if hasattr(self, "children"): 
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    96
            for c in self.children:
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    97
                for yoodl in c.traverse():
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    98
                    yield yoodl
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
    99
2763
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   100
# module scope for HMITree root
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   101
# so that CTN can use HMITree deduced in Library
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   102
# note: this only works because library's Generate_C is 
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   103
#       systematicaly invoked before CTN's CTNGenerate_C
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   104
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   105
hmi_tree_root = None
2757
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   106
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   107
class SVGHMILibrary(POULibrary):
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   108
    def GetLibraryPath(self):
2750
2694170cd88e intermediate commit, work in progress
Edouard Tisserant
parents: 2749
diff changeset
   109
         return paths.AbsNeighbourFile(__file__, "pous.xml")
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   110
2749
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   111
    def Generate_C(self, buildpath, varlist, IECCFLAGS):
2763
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   112
        global hmi_tree_root
2749
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   113
2757
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   114
        """
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   115
        PLC Instance Tree:
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   116
          prog0
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   117
           +->v1 HMI_INT
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   118
           +->v2 HMI_INT
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   119
           +->fb0 (type mhoo)
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   120
           |   +->va HMI_LABEL
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   121
           |   +->v3 HMI_INT
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   122
           |   +->v4 HMI_INT
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   123
           |
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   124
           +->fb1 (type mhoo)
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   125
           |   +->va HMI_LABEL
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   126
           |   +->v3 HMI_INT
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   127
           |   +->v4 HMI_INT
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   128
           |
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   129
           +->fb2
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   130
               +->v5 HMI_IN
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   131
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   132
        HMI tree:
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   133
          hmi0
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   134
           +->v1
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   135
           +->v2
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   136
           +->fb0_va
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   137
           |   +-> v3
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   138
           |   +-> v4
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   139
           |
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   140
           +->fb1_va
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   141
           |   +-> v3
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   142
           |   +-> v4
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   143
           |
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   144
           +->v5
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   145
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   146
        """
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   147
2749
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   148
        # Filter known HMI types
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   149
        hmi_types_instances = [v for v in varlist if v["derived"] in HMI_TYPES]
2758
5f79b194fa63 SVGHMI: filter out temporary variables created while generating ST code out of FBD.
Edouard Tisserant
parents: 2757
diff changeset
   150
5f79b194fa63 SVGHMI: filter out temporary variables created while generating ST code out of FBD.
Edouard Tisserant
parents: 2757
diff changeset
   151
        hmi_tree_root = HMITreeNode(None, "/", "HMI_ROOT")
2757
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   152
2762
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
   153
        # add special nodes 
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
   154
        map(lambda (n,t): hmi_tree_root.children.append(HMITreeNode(None,n,t)), [
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
   155
                ("plc_status", "HMI_PLC_STATUS"),
282500e03dbc Add special nodes at HMI Tree root, fix code to handle special node (no path).
Edouard Tisserant
parents: 2758
diff changeset
   156
                ("current_page", "HMI_CURRENT_PAGE")])
2757
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   157
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   158
        # deduce HMI tree from PLC HMI_* instances
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   159
        for v in hmi_types_instances:
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   160
            path = v["C_path"].split(".")
2758
5f79b194fa63 SVGHMI: filter out temporary variables created while generating ST code out of FBD.
Edouard Tisserant
parents: 2757
diff changeset
   161
            # ignores variables starting with _TMP_
5f79b194fa63 SVGHMI: filter out temporary variables created while generating ST code out of FBD.
Edouard Tisserant
parents: 2757
diff changeset
   162
            if path[-1].startswith("_TMP_"):
5f79b194fa63 SVGHMI: filter out temporary variables created while generating ST code out of FBD.
Edouard Tisserant
parents: 2757
diff changeset
   163
                continue
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   164
            new_node = HMITreeNode(path, path[-1], v["derived"], v["type"], v["vartype"])
2757
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   165
            hmi_tree_root.place_node(new_node)
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   166
c901baa36bb3 SVGHMI: added deduction of HMI tree from list of HMI_* instances.
Edouard Tisserant
parents: 2756
diff changeset
   167
        print(hmi_tree_root.pprint())
2749
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   168
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   169
        variable_decl_array = []
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   170
        extern_variables_declarations = []
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   171
        buf_index = 0
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   172
        for node in hmi_tree_root.traverse():
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   173
            if hasattr(node, "iectype"):
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   174
                sz = DebugTypesSize.get(node.iectype, 0)
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   175
                variable_decl_array += [
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   176
                    "{&(" + ".".join(node.path) + "), " + node.iectype + {
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   177
                        "EXT": "_P_ENUM",
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   178
                        "IN":  "_P_ENUM",
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   179
                        "MEM": "_O_ENUM",
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   180
                        "OUT": "_O_ENUM",
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   181
                        "VAR": "_ENUM"
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   182
                    }[node.vartype] + ", " +
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   183
                    str(buf_index) + ", 0}"]
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   184
                buf_index += sz
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   185
                if len(node.path) == 1:
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   186
                    extern_variables_declarations += [
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   187
                        "extern __IEC_" + node.iectype + "_" +
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   188
                        "t" if node.vartype is "VAR" else "p"
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   189
                        + ".".join(node.path) + ";"]
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   190
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   191
        # TODO : filter only requiered external declarations
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   192
        for v in varlist :
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   193
            if v["C_path"].find('.') < 0 and v["vartype"] == "FB" :
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   194
                extern_variables_declarations += [
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   195
                    "extern %(type)s %(C_path)s;" % v]
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   196
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   197
        # TODO check if programs need to be declared separately
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   198
        # "programs_declarations": "\n".join(["extern %(type)s %(C_path)s;" %
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   199
        #                                     p for p in self._ProgramList]),
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   200
2749
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   201
        # TODO generate C code to observe/access HMI tree variables
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   202
        svghmi_c_filepath = paths.AbsNeighbourFile(__file__, "svghmi.c")
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   203
        svghmi_c_file = open(svghmi_c_filepath, 'r')
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   204
        svghmi_c_code = svghmi_c_file.read()
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   205
        svghmi_c_file.close()
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   206
        svghmi_c_code = svghmi_c_code % { 
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   207
            "variable_decl_array": ",\n".join(variable_decl_array),
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   208
            "extern_variables_declarations": "\n".join(extern_variables_declarations),
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   209
            "buffer_size": buf_index
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   210
            }
2749
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   211
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   212
        gen_svghmi_c_path = os.path.join(buildpath, "svghmi.c")
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   213
        gen_svghmi_c = open(gen_svghmi_c_path, 'w')
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   214
        gen_svghmi_c.write(svghmi_c_code)
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   215
        gen_svghmi_c.close()
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   216
2769b3aed34d Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents: 2747
diff changeset
   217
        return (["svghmi"], [(gen_svghmi_c_path, IECCFLAGS)], True), ""
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   218
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   219
class SVGHMI(object):
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   220
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   221
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   222
      <xsd:element name="SVGHMI">
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   223
        <xsd:complexType>
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   224
          <xsd:attribute name="enableHTTP" type="xsd:boolean" use="optional" default="false"/>
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   225
          <xsd:attribute name="bindAddress" type="xsd:string" use="optional" default="localhost"/>
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   226
          <xsd:attribute name="port" type="xsd:string" use="optional" default="8080"/>
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   227
        </xsd:complexType>
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   228
      </xsd:element>
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   229
    </xsd:schema>
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   230
    """
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   231
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   232
    ConfNodeMethods = [
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   233
        {
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   234
            "bitmap":    "ImportSVG",
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   235
            "name":    _("Import SVG"),
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   236
            "tooltip": _("Import SVG"),
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   237
            "method":   "_ImportSVG"
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   238
        },
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   239
        {
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   240
            "bitmap":    "ImportSVG",  # should be something different
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   241
            "name":    _("Inkscape"),
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   242
            "tooltip": _("Edit HMI"),
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   243
            "method":   "_StartInkscape"
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   244
        },
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   245
    ]
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   246
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   247
    def _getSVGpath(self, project_path=None):
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   248
        if project_path is None:
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   249
            project_path = self.CTNPath()
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   250
        # define name for SVG file containing gui layout
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   251
        return os.path.join(project_path, "gui.svg")
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   252
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   253
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   254
    def OnCTNSave(self, from_project_path=None):
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   255
        if from_project_path is not None:
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   256
            shutil.copyfile(self._getSVGpath(from_project_path),
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   257
                            self._getSVGpath())
2750
2694170cd88e intermediate commit, work in progress
Edouard Tisserant
parents: 2749
diff changeset
   258
        return True
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   259
2753
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   260
    def GetSVGGeometry(self):
2756
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   261
        # invoke inskscape -S, csv-parse output, produce elements
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   262
        InkscapeGeomColumns = ["Id", "x", "y", "w", "h"]
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   263
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   264
        # TODO : move following line to __init__
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   265
        inkpath = get_inkscape_path()
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   266
        svgpath = self._getSVGpath()
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   267
        _status, result, _err_result = ProcessLogger(None,
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   268
                                                     inkpath + " -S " + svgpath,
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   269
                                                     no_stdout=True,
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   270
                                                     no_stderr=True).spin()
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   271
        res = []
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   272
        for line in result.split():
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   273
            strippedline = line.strip()
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   274
            attrs = dict(
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   275
                zip(InkscapeGeomColumns, line.strip().split(',')))
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   276
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   277
            res.append(etree.Element("bbox", **attrs))
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   278
f94bc35a023e SVGHMI: added extraction of SVG bounding boxes, obtained from "inkscape -S", and passed to XSLT transform as variable.
Edouard Tisserant
parents: 2753
diff changeset
   279
        return res
2753
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   280
2763
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   281
    def GetHMITree(self):
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   282
        global hmi_tree_root
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   283
        res = [hmi_tree_root.etree()]
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   284
        return res
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   285
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   286
    def CTNGenerate_C(self, buildpath, locations):
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   287
        """
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   288
        Return C code generated by iec2c compiler
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   289
        when _generate_softPLC have been called
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   290
        @param locations: ignored
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   291
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   292
        """
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   293
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   294
        svgfile = self._getSVGpath()
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   295
        if os.path.exists(svgfile):
2753
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   296
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   297
            # TODO : move to __init__
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   298
            transform = XSLTransform(os.path.join(ScriptDirectory, "gen_index_xhtml.xslt"),
2763
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   299
                          [("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry()),
ce04d79b8e57 Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.
Edouard Tisserant
parents: 2762
diff changeset
   300
                           ("GetHMITree", lambda *_ignored:self.GetHMITree())])
2753
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   301
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   302
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   303
            # load svg as a DOM with Etree
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   304
            svgdom = etree.parse(svgfile)
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   305
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   306
            # call xslt transform on Inkscape's SVG to generate XHTML
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   307
            result = transform.transform(svgdom)
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   308
           
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   309
            # print(str(result))
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2763
diff changeset
   310
            # print(transform.xslt.error_log)
2753
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   311
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   312
            # TODO
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   313
            #   - Errors on HMI semantics
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   314
            #   - ... maybe something to have a global view of what is declared in SVG.
2753
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents: 2750
diff changeset
   315
2745
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   316
        else:
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   317
            # TODO : use default svg that expose the HMI tree as-is 
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   318
            pass
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   319
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   320
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   321
        res = ([], "", False)
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   322
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   323
        targetpath = os.path.join(self._getBuildPath(), "target.xhtml")
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   324
        targetfile = open(targetpath, 'w')
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   325
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   326
        # TODO : DOM to string
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   327
        targetfile.write("TODO")
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   328
        targetfile.close()
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   329
        res += (("target.js", open(targetpath, "rb")),)
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   330
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   331
        # TODO add C code to expose HMI tree variables to shared memory
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   332
        # TODO generate a description of shared memory (xml or CSV) 
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   333
        #      that can be loaded by svghmi QTWeb* app or svghmi server
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   334
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   335
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   336
        return res
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   337
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   338
    def _ImportSVG(self):
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   339
        dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "",  _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN)
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   340
        if dialog.ShowModal() == wx.ID_OK:
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   341
            svgpath = dialog.GetPath()
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   342
            if os.path.isfile(svgpath):
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   343
                shutil.copy(svgpath, self._getSVGpath())
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   344
            else:
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   345
                self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n") % svgpath)
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   346
        dialog.Destroy()
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   347
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   348
    def _StartInkscape(self):
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   349
        svgfile = self._getSVGpath()
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   350
        open_inkscape = True
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   351
        if not self.GetCTRoot().CheckProjectPathPerm():
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   352
            dialog = wx.MessageDialog(self.GetCTRoot().AppFrame,
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   353
                                      _("You don't have write permissions.\nOpen Inkscape anyway ?"),
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   354
                                      _("Open Inkscape"),
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   355
                                      wx.YES_NO | wx.ICON_QUESTION)
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   356
            open_inkscape = dialog.ShowModal() == wx.ID_YES
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   357
            dialog.Destroy()
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   358
        if open_inkscape:
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   359
            if not os.path.isfile(svgfile):
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   360
                svgfile = None
535eb0b8bd9d Skeleton for svghmi extension
Edouard Tisserant
parents:
diff changeset
   361
            open_svg(svgfile)