Skip to content

Commit 84e086b

Browse files
committed
Add first testing version
1 parent da41dab commit 84e086b

File tree

7 files changed

+96
-22
lines changed

7 files changed

+96
-22
lines changed
File renamed without changes.

.idea/modules.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

InkSvgNest/inkSvgNest.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,9 @@
2727
This script
2828
2929
"""
30-
3130
import os
3231
import inkex
33-
from lxml import etree, objectify
34-
35-
try:
36-
from ruamel.yaml import YAML
37-
except:
38-
YAML = None
32+
from lxml import etree
3933
import inkex.utils
4034
import inkex.base
4135

@@ -48,6 +42,25 @@ def add_arguments(self, pars):
4842
pars.add_argument("--a_file", type=str, dest="a_file") # self.options.a_file
4943
pars.add_argument("--r_file", type=str, dest="r_file") # self.options.r_file
5044

45+
def yaml_write(self, data, filename):
46+
f = open(filename, "w+")
47+
for key in data:
48+
val = data[key]
49+
f.write(f"{key}:\n- {val}\n")
50+
f.close()
51+
52+
def yaml_read(self, data, filename):
53+
try:
54+
with open(filename) as f:
55+
for lineKey in f:
56+
key = lineKey[:-2]
57+
lineVal = next(f)
58+
val = lineVal[2:-1]
59+
data[key] = val
60+
f.close()
61+
except StopIteration:
62+
self.raise_error("End")
63+
5164
def raise_error(self, message: str):
5265
raise inkex.AbortExtension("ERROR: %s" % message)
5366

@@ -65,39 +78,36 @@ def file_check(self, filename: str, suffix: str):
6578

6679
def svg_to_yaml(self, yaml_file: str):
6780
self.file_suffix(yaml_file, ".yaml")
68-
yaml = YAML()
6981
data = {}
7082

7183
for el in self.svg.getiterator():
7284
if "transform" in el.attrib:
73-
data[el.attrib["id"]] = [el.attrib["transform"]]
85+
data[el.attrib["id"]] = [el.attrib["transform"]][0]
7486

75-
with open(yaml_file, "w") as f:
76-
yaml.dump(data, f)
87+
self.yaml_write(data, yaml_file)
7788

7889
def yaml_to_svg(self, yaml_file):
7990
self.file_check(yaml_file, ".yaml")
80-
yaml = YAML()
81-
with open(yaml_file) as f:
82-
data = yaml.load(f)
91+
data = {}
92+
self.yaml_read(data, yaml_file)
8393

8494
for el in self.svg.getiterator():
8595
if "id" in el.attrib:
8696
if el.attrib["id"] in data:
87-
el.transform = str(data[el.attrib["id"]][0])
97+
el.transform = str(data[el.attrib["id"]])
8898

8999
def svg_to_svg(self, svg_file_nested):
90100
self.file_check(svg_file_nested, ".svg")
91101
data = {}
92102
root = etree.parse(svg_file_nested).getroot()
93103
for el in root.getiterator():
94104
if "transform" in el.attrib:
95-
data[el.attrib["id"]] = [el.attrib["transform"]]
105+
data[el.attrib["id"]] = [el.attrib["transform"]][0]
96106

97107
for el in self.svg.getiterator():
98108
if "id" in el.attrib:
99109
if el.attrib["id"] in data:
100-
el.transform = str(data[el.attrib["id"]][0])
110+
el.transform = str(data[el.attrib["id"]])
101111

102112
def effect(self):
103113
input_svg = self.document_path()
@@ -106,10 +116,10 @@ def effect(self):
106116
if tab == "relative":
107117
mode = self.options.r_mode
108118
r_file = self.options.r_file
109-
if(r_file == None):
119+
if (r_file == None):
110120
raise inkex.AbortExtension("Relative path input file mustn't be empty")
111121
file = os.path.join(os.path.split(input_svg)[0], r_file)
112-
else: # absolute and help
122+
else: # absolute and help
113123
mode = self.options.a_mode
114124
file = self.options.a_file
115125

@@ -128,11 +138,12 @@ def effect(self):
128138
if __name__ == "__main__":
129139
Inknest().run()
130140

141+
# For testing
131142
# if '/' in __file__:
132143
# # We are running in PyCharm
133144
# input_file = r'/home/kuba/Documents/moje/moje.svg'
134145
# output_file = input_file
135-
# Inknest().run([input_file, '--mode=' + "svgToYaml"])
146+
# Inknest().run([input_file, '--a_mode=' + "svgToYaml"])
136147
# else:
137148
# # We are running in Inkscape
138149
# Inknest().run()

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
# InkSvgNest
1+
# InkSvgNest
2+
3+
InkSvgNest je transformační doplněk pro vektorovou aplikaci Inkscape.
4+
Slouží k jednoduchému ukládání, čtení a upravování pozice jednotlivých objektů v souboru.
5+
Rozšíření zjednodušuje prototypování dílů, které jsou přesně rozmístěné na pracovní poše.
6+
7+
![](doc/app.png)
8+
9+
## Použití
10+
11+
### Původní SVG na nové SVG
12+
Rozšíření načte předchozí nanestovanou verzi souboru a pokusí se aplikovat veškeré transformace i na právě otevřený soubor.
13+
14+
Ukázka:
15+
16+
### SVG na souřadnicový YAML
17+
Rozšíření přečte souřadnice právě otevřeného souboru a uloží je do YAML souboru ve formátu `"Název objektu" -> "SOuřadnice"`.
18+
19+
### Souřadnicový YAML na SVG
20+
Opačná variantak k předchozí volbě otevře YAML soubor se souřadnicemi a pokusí se je podle názvu objektu aplikovat na otevřený soubor.
21+
22+
## Varianty
23+
24+
### Absolutní cesta
25+
Při výběru je nutné vybrat celou cestu k konkrétní složce a v ní i soubor do/z kterého se soubor uloží/přečte.
26+
Trojtečka slouží ke grafickému výberu cesty.
27+
Pokud požadovaný soubor neexistuje je vytvořen, v opačném případě je soubor přepsán.
28+
29+
### Relativní cesta
30+
Relativní cestou je mýšlena stejná složka jako pro aktuálně otevřený soubor.

doc/app.png

12.2 KB
Loading

test/custom_yaml.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def yaml_write(data, filename):
2+
f = open(filename, "w+")
3+
for key in data:
4+
f.write(f"{key}:\n- {data[key]}\n")
5+
6+
7+
def yaml_read(data, filename):
8+
try:
9+
with open(filename) as f:
10+
for lineKey in f:
11+
key = lineKey[:-2]
12+
lineVal = next(f)
13+
val = lineVal[2:-1]
14+
data[key] = val
15+
except StopIteration:
16+
print("End")
17+
18+
19+
if __name__ == "__main__":
20+
data = {}
21+
# data["a"] = "egfsgs"
22+
# data["b"] = "plmdwd"
23+
# data["c"] = "wdfegd"
24+
25+
# yaml_write(data, "data.yaml")
26+
yaml_read(data, "data.yaml")
27+
28+
print(data)

test/data.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
a:
2+
- egfsgs
3+
b:
4+
- plmdwd
5+
c:
6+
- wdfegd

0 commit comments

Comments
 (0)