author | Edouard Tisserant |
Fri, 09 Aug 2019 12:11:31 +0200 | |
branch | svghmi |
changeset 2753 | 9a7e12e96399 |
parent 2750 | 2694170cd88e |
child 2756 | f94bc35a023e |
permissions | -rw-r--r-- |
2745 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
# This file is part of Beremiz |
|
5 |
# Copyright (C) 2019: Edouard TISSERANT |
|
6 |
# |
|
7 |
# See COPYING file for copyrights details. |
|
8 |
||
9 |
from __future__ import absolute_import |
|
10 |
import os |
|
11 |
import shutil |
|
12 |
||
13 |
import wx |
|
14 |
||
15 |
import util.paths as paths |
|
16 |
from POULibrary import POULibrary |
|
17 |
from docutil import open_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
|
18 |
from lxml import etree |
2745 | 19 |
|
2749
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
20 |
HMI_TYPES_DESC = { |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
21 |
"HMI_CLASS":{}, |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
22 |
"HMI_LABEL":{}, |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
23 |
"HMI_STRING":{}, |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
24 |
"HMI_INT":{}, |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
25 |
"HMI_REAL":{} |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
26 |
} |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
27 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
28 |
HMI_TYPES = HMI_TYPES_DESC.keys() |
2745 | 29 |
|
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
|
30 |
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
|
31 |
|
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
|
32 |
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
|
33 |
|
2745 | 34 |
class SVGHMILibrary(POULibrary): |
35 |
def GetLibraryPath(self): |
|
2750 | 36 |
return paths.AbsNeighbourFile(__file__, "pous.xml") |
2745 | 37 |
|
2749
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
38 |
def Generate_C(self, buildpath, varlist, IECCFLAGS): |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
39 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
40 |
# Filter known HMI types |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
41 |
hmi_types_instances = [v for v in varlist if v["derived"] in HMI_TYPES] |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
42 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
43 |
# TODO deduce HMI tree |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
44 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
45 |
# 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
|
46 |
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
|
47 |
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
|
48 |
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
|
49 |
svghmi_c_file.close() |
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
|
50 |
svghmi_c_code = svghmi_c_code % { "hmi_tree": "TODO !!!"} |
2749
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
51 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
gen_svghmi_c.close() |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
56 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
57 |
return (["svghmi"], [(gen_svghmi_c_path, IECCFLAGS)], True), "" |
2745 | 58 |
|
59 |
class SVGHMI(object): |
|
60 |
XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?> |
|
61 |
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
|
62 |
<xsd:element name="SVGHMI"> |
|
63 |
<xsd:complexType> |
|
64 |
<xsd:attribute name="enableHTTP" type="xsd:boolean" use="optional" default="false"/> |
|
65 |
<xsd:attribute name="bindAddress" type="xsd:string" use="optional" default="localhost"/> |
|
66 |
<xsd:attribute name="port" type="xsd:string" use="optional" default="8080"/> |
|
67 |
</xsd:complexType> |
|
68 |
</xsd:element> |
|
69 |
</xsd:schema> |
|
70 |
""" |
|
71 |
||
72 |
ConfNodeMethods = [ |
|
73 |
{ |
|
74 |
"bitmap": "ImportSVG", |
|
75 |
"name": _("Import SVG"), |
|
76 |
"tooltip": _("Import SVG"), |
|
77 |
"method": "_ImportSVG" |
|
78 |
}, |
|
79 |
{ |
|
80 |
"bitmap": "ImportSVG", # should be something different |
|
81 |
"name": _("Inkscape"), |
|
82 |
"tooltip": _("Edit HMI"), |
|
83 |
"method": "_StartInkscape" |
|
84 |
}, |
|
85 |
] |
|
86 |
||
87 |
def _getSVGpath(self, project_path=None): |
|
88 |
if project_path is None: |
|
89 |
project_path = self.CTNPath() |
|
90 |
# define name for SVG file containing gui layout |
|
91 |
return os.path.join(project_path, "gui.svg") |
|
92 |
||
93 |
||
94 |
def OnCTNSave(self, from_project_path=None): |
|
95 |
if from_project_path is not None: |
|
96 |
shutil.copyfile(self._getSVGpath(from_project_path), |
|
97 |
self._getSVGpath()) |
|
2750 | 98 |
return True |
2745 | 99 |
|
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
|
100 |
def GetSVGGeometry(self): |
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
|
101 |
# TODO : invoke inskscape -S, csv-parse output, produce elements |
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
|
102 |
return [etree.Element("bbox", id="blah0", x="1", y="2", w="3", h="4"), |
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
|
103 |
etree.Element("bbox", id="blah1", x="5", y="6", w="7", h="8")] |
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
|
104 |
|
2745 | 105 |
def CTNGenerate_C(self, buildpath, locations): |
106 |
""" |
|
107 |
Return C code generated by iec2c compiler |
|
108 |
when _generate_softPLC have been called |
|
109 |
@param locations: ignored |
|
110 |
@return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND |
|
111 |
""" |
|
112 |
||
2749
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
113 |
# TODO fetch HMI tree from library |
2745 | 114 |
|
115 |
svgfile = self._getSVGpath() |
|
116 |
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
|
117 |
|
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
|
118 |
# 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
|
119 |
transform = XSLTransform(os.path.join(ScriptDirectory, "gen_index_xhtml.xslt"), |
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
|
120 |
[("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry())]) |
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
|
121 |
|
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
|
122 |
|
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
|
123 |
# 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
|
124 |
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
|
125 |
|
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
|
126 |
# 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
|
127 |
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
|
128 |
|
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
|
129 |
print(str(result)) |
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
|
130 |
print(transform.xslt.error_log) |
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
|
131 |
|
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
|
132 |
# TODO |
2745 | 133 |
# - Errors on HMI semantics |
134 |
# - ... 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
|
135 |
|
2745 | 136 |
else: |
137 |
# TODO : use default svg that expose the HMI tree as-is |
|
138 |
pass |
|
139 |
||
140 |
||
141 |
res = ([], "", False) |
|
142 |
||
143 |
targetpath = os.path.join(self._getBuildPath(), "target.xhtml") |
|
144 |
targetfile = open(targetpath, 'w') |
|
145 |
||
146 |
# TODO : DOM to string |
|
147 |
targetfile.write("TODO") |
|
148 |
targetfile.close() |
|
149 |
res += (("target.js", open(targetpath, "rb")),) |
|
150 |
||
151 |
# TODO add C code to expose HMI tree variables to shared memory |
|
152 |
# TODO generate a description of shared memory (xml or CSV) |
|
153 |
# that can be loaded by svghmi QTWeb* app or svghmi server |
|
154 |
||
155 |
||
156 |
return res |
|
157 |
||
158 |
def _ImportSVG(self): |
|
159 |
dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "", _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN) |
|
160 |
if dialog.ShowModal() == wx.ID_OK: |
|
161 |
svgpath = dialog.GetPath() |
|
162 |
if os.path.isfile(svgpath): |
|
163 |
shutil.copy(svgpath, self._getSVGpath()) |
|
164 |
else: |
|
165 |
self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n") % svgpath) |
|
166 |
dialog.Destroy() |
|
167 |
||
168 |
def _StartInkscape(self): |
|
169 |
svgfile = self._getSVGpath() |
|
170 |
open_inkscape = True |
|
171 |
if not self.GetCTRoot().CheckProjectPathPerm(): |
|
172 |
dialog = wx.MessageDialog(self.GetCTRoot().AppFrame, |
|
173 |
_("You don't have write permissions.\nOpen Inkscape anyway ?"), |
|
174 |
_("Open Inkscape"), |
|
175 |
wx.YES_NO | wx.ICON_QUESTION) |
|
176 |
open_inkscape = dialog.ShowModal() == wx.ID_YES |
|
177 |
dialog.Destroy() |
|
178 |
if open_inkscape: |
|
179 |
if not os.path.isfile(svgfile): |
|
180 |
svgfile = None |
|
181 |
open_svg(svgfile) |