From c0756e6e05475cbfeeddc4252e193a48e35e6ffc Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Tue, 10 Aug 2021 15:00:46 +0200 Subject: [PATCH] added option to select local vektorkollektor.js file --- .../vektorkollektor/vektorkollektor.inx | 10 +++++- .../vektorkollektor/vektorkollektor.py | 33 +++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/extensions/fablabchemnitz/vektorkollektor/vektorkollektor.inx b/extensions/fablabchemnitz/vektorkollektor/vektorkollektor.inx index b04effd4..37010872 100644 --- a/extensions/fablabchemnitz/vektorkollektor/vektorkollektor.inx +++ b/extensions/fablabchemnitz/vektorkollektor/vektorkollektor.inx @@ -4,8 +4,16 @@ fablabchemnitz.de.vektorkollektor + + + + + http://www.vektorkollektor.com/vektordaten/vektorkollektor.js - 0 + C:\Users\ + false + + 0 diff --git a/extensions/fablabchemnitz/vektorkollektor/vektorkollektor.py b/extensions/fablabchemnitz/vektorkollektor/vektorkollektor.py index fca87d53..13ff55dc 100644 --- a/extensions/fablabchemnitz/vektorkollektor/vektorkollektor.py +++ b/extensions/fablabchemnitz/vektorkollektor/vektorkollektor.py @@ -13,28 +13,40 @@ from lxml import etree import inkex from inkex import PathElement, Path import re +import os from ast import literal_eval class Vektorkollektor(inkex.EffectExtension): def add_arguments(self, pars): pars.add_argument("--tab") + pars.add_argument("--vk_inputfile") + pars.add_argument("--download_or_local", default='local') + pars.add_argument("--showinfo", type=inkex.Boolean, default=False) pars.add_argument("--vk_url", default="http://www.vektorkollektor.com/vektordaten/vektorkollektor.js") pars.add_argument("--vk_id", type=int, default=1) def effect(self): - # Download the recent vektorkollektor data file and parse it - handler = urllib.request.HTTPBasicAuthHandler() - opener = urllib.request.build_opener(handler) - + + if self.options.download_or_local == "local": + if not os.path.exists(self.options.vk_inputfile): + self.msg("The input file does not exist. Please select a proper file and try again.") + exit(1) + with open(self.options.vk_inputfile, 'r') as file: + vkData = file.read() + else: + # Download the recent vektorkollektor data file and parse it + handler = urllib.request.HTTPBasicAuthHandler() + opener = urllib.request.build_opener(handler) + vkData = opener.open(self.options.vk_url).read().decode("utf-8") + urllib.request.install_opener(opener) + try: xP = [] #x-coordinate yP = [] #y-coordinate cP = [] #draw fN = [] #original vektorkollektor .PLT file number - - vkData = opener.open(self.options.vk_url).read().decode("utf-8") - urllib.request.install_opener(opener) + for match in re.compile(r"""^var xP = .*;""", re.MULTILINE).finditer(vkData): xP = literal_eval(match.group(0).split("var xP = ")[1].split(";")[0]) for match in re.compile(r"""^var yP = .*;""", re.MULTILINE).finditer(vkData): @@ -44,6 +56,9 @@ class Vektorkollektor(inkex.EffectExtension): for match in re.compile(r"""^var fN = .*;""", re.MULTILINE).finditer(vkData): fN = literal_eval(match.group(0).split("var fN = ")[1].split(";")[0]) + if self.options.showinfo is True: + self.msg("Input file contains {} drawings.".format(fN[-2])) #last number is always 0, so we use the second last + vkGroup = self.document.getroot().add(inkex.Group(id="vektorkollektor-{}".format(self.options.vk_id))) for move in range(0, len(fN)): begin = None @@ -62,6 +77,10 @@ class Vektorkollektor(inkex.EffectExtension): newLine.path = Path("M {},{} L {},{}".format(begin[0], begin[1], end[0], end[1])) newLine.style = "fill:none;stroke:#000000;stroke-width:1;stroke-linecap:round" vkGroup.append(newLine) + + if self.options.showinfo is True: + self.msg("The drawing with id {} contains {} drawable vectors.".format(self.options.vk_id, len(vkGroup))) + except Exception as e: inkex.errormsg(e)