-
Notifications
You must be signed in to change notification settings - Fork 1
/
quick_measure.py
104 lines (63 loc) · 3.02 KB
/
quick_measure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
# coding=utf-8
# inkscape 1.2
import inkex
from inkex.bezier import csplength
class MeasureLength(inkex.EffectExtension):
def add_arguments(self, pars):
pars.add_argument(
"-u", "--unit", default="px", help="The unit of the measurement"
)
def effect(self):
try:
if float(inkex.__version__[:3]) >= 1.2:
viewport_code = True
else:
viewport_code = False
except Exception:
viewport_code = False
# Calculate the unit factor depending on the inkscape version
# unittouu is not officially depreciated but is not recommended
# The output of unittouu has changed in the past.
if viewport_code == False:
#############################
# inkscape 1.1 code block
prec = 2
scale = self.svg.unittouu('1px') # convert to document units
factor = 1.0
if self.svg.get('viewBox'):
factor = self.svg.scale / self.svg.unittouu('1px')
factor *= scale / self.svg.unittouu('1' + self.options.unit)
else:
#inkscape 1.2 and newer
prec = 2
scale = self.svg.viewport_to_unit(
"1" + self.svg.document_unit
) # convert to document units
factor = self.svg.unit_to_viewport(1, self.options.unit)
# loop over all selected paths
filtered = self.svg.selection.filter(inkex.PathElement)
if not filtered:
raise inkex.AbortExtension(_("Please select at least one path object."))
pathlengths = []
for node in filtered:
csp = node.path.transform(node.composed_transform()).to_superpath()
slengths, stotal = csplength(csp)
# convert segment lengths into user defined units.
slengths_uu = []
cumulative = [0]
count = 0
for number in slengths[0]:
length_user_unit = round(number * factor, prec)
slengths_uu.append(length_user_unit)
count += number
cumulative.append(round(count * factor,prec))
self.msg("Total length: " + str(round(stotal * factor,2))+" "+self.options.unit)
self.msg("Segment Lengths: " + str(slengths_uu)+" "+self.options.unit)
self.msg("Cumulative Lengths: " + str(cumulative)+" "+self.options.unit)
self.msg("")
pathlengths.append(stotal * factor)
if len(pathlengths)==2:
self.msg("Difference: " + str(abs(round(pathlengths[0]-pathlengths[1],2)))+ " " + self.options.unit)
if __name__ == "__main__":
MeasureLength().run()