-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathdimensions_svg.py
289 lines (238 loc) · 10.4 KB
/
dimensions_svg.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
from math import pi, degrees
from mathutils import Vector, Matrix
import bpy
from bpy.props import EnumProperty, StringProperty, FloatProperty, IntProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import match_long_repeat as mlr, enum_item_4, updateNode
from sverchok.utils.svg import SvgGroup
def draw_edge(p, scale, height):
svg = '<line '
svg += f'x1="{p[0][0]* scale}" y1="{height- p[0][1]* scale}" '
svg += f'x2="{p[1][0]* scale}" y2="{height- p[1][1]* scale}" '
return svg
class SvgDimension():
def __repr__(self):
return "<SVG Dimension>"
def __init__(self, location, location_b, dim_type, dim_offset, font_size, text_offset, text_attrib, lines_attrib, font_family, node):
self.node = node
self.location = location
self.location_b = location_b
self.dim_type = dim_type
self.dim_offset = dim_offset
self.font_size = font_size
self.text_offset = text_offset
self.text_attributes = text_attrib
self.line_attributes = lines_attrib
self.font_family = font_family
def draw(self, document):
height = document.height
scale = document.scale
loc_a = Vector(self.location)
loc_b = Vector(self.location_b)
if self.dim_type == "Horizontal" or self.dim_type == 0:
line_dir = Vector((1, 0, 0))
perp = Vector ((0, 1, 0))
diagonal = (line_dir + perp).normalized()
higher_a = loc_a[1] > loc_b[1]
angle = 0
if higher_a:
dim_loc_a = loc_a + perp * (self.dim_offset)
dim_loc_b = loc_b + perp * (self.dim_offset + loc_a[1] - loc_b[1])
sign = 1 if self.dim_offset > 0 else -1
sign_b = 1 if self.dim_offset + loc_a[1] - loc_b[1] > 0 else -1
else:
dim_loc_a = loc_a + perp * (self.dim_offset + loc_b[1] - loc_a[1])
dim_loc_b = loc_b + perp * (self.dim_offset)
sign = 1 if self.dim_offset+loc_b[1]-loc_a[1] > 0 else -1
sign_b = 1 if self.dim_offset > 0 else -1
if self.dim_type == "Vertical" or self.dim_type == 1:
line_dir = Vector((0, 1, 0))
perp = Vector ((1, 0, 0))
diagonal = (line_dir + perp).normalized()
higher_a = loc_a[0] > loc_b[0]
angle = 90
dim_loc_a = loc_a + perp * (self.dim_offset)
dim_loc_b = loc_b + perp * (self.dim_offset + loc_a[0] - loc_b[0])
sign = 1 if self.dim_offset > 0 else -1
sign_b = 1 if self.dim_offset + loc_a[0] - loc_b[0] > 0 else -1
if self.dim_type == "Aligned" or self.dim_type == 2:
line_dir = (loc_b - loc_a).normalized()
rot = Matrix.Rotation(pi/2, 4, 'Z')
perp = (line_dir @ rot).normalized()
diagonal = (line_dir + perp).normalized()
dim_loc_a = loc_a + perp*(self.dim_offset)
dim_loc_b = loc_b + perp*(self.dim_offset)
angle = 180 + degrees(line_dir.angle(Vector((1, 0, 0))))
sign = 1 if self.dim_offset > 0 else -1
sign_b = sign
line_extension = self.node.line_extension
lines = []
lines.append([loc_a, dim_loc_a + sign * perp * line_extension])
lines.append([loc_b, dim_loc_b + sign_b * perp * line_extension])
lines.append([dim_loc_a - line_dir * line_extension, dim_loc_b + line_dir * line_extension])
lines.append([dim_loc_a - diagonal * line_extension, dim_loc_a + diagonal * line_extension])
lines.append([dim_loc_b - diagonal * line_extension, dim_loc_b + diagonal * line_extension])
lines_svg = ''
for line in lines:
lines_svg += draw_edge(line, scale, height)
if self.line_attributes:
lines_svg += self.line_attributes.draw(document)
else:
lines_svg += 'stroke-width="1px" '
lines_svg += 'stroke="rgb(0,0,0)"'
lines_svg += '/>'
text_svg = self.draw_dimension_text(dim_loc_a, dim_loc_b, angle, document)
return lines_svg + text_svg
def draw_dimension_text(self, dim_loc_a, dim_loc_b, angle, document):
height = document.height
scale = document.scale
text_loc = (dim_loc_a + dim_loc_b)/2
x = text_loc[0] * scale
y = height - text_loc[1] * scale
text_svg = '<text text-anchor="middle" '
text_svg += f'font-size="{self.font_size * scale}px" '
text_svg += f'font-family="{self.font_family}" '
text_svg += f'transform="translate({x},{y})rotate({angle})translate({0},{self.text_offset * scale})" '
if self.text_attributes:
text_svg += self.text_attributes.draw(document)
text_svg += '>'
precision = self.node.decimal_precision
if self.node.units_real == 'Metric':
p_format = "{:10."+str(precision)+"f}"
text_svg += p_format.format((dim_loc_b - dim_loc_a).length)
else:
feets = (dim_loc_b - dim_loc_a).length//0.3048
if feets < 2:
inches = ((dim_loc_b - dim_loc_a).length/0.0254)
else:
p_format = "{:10.0f}"
inches = (((dim_loc_b - dim_loc_a).length%0.3048)/0.0254)
text_svg += p_format.format(feets)
text_svg += "ft"
p_format = "{:10."+str(precision)+"f}"
text_svg += p_format.format(inches)
text_svg += "in"
text_svg += f' {self.node.units}'
text_svg += '</text>'
return text_svg
class SvSvgDimensionNode(SverchCustomTreeNode, bpy.types.Node):
"""
Triggers: Text SVG
Tooltip: Creates SVG Dimensions
"""
bl_idname = 'SvSvgDimensionNode'
bl_label = 'Dimensions SVG'
bl_icon = 'MESH_CIRCLE'
sv_icon = 'SV_DIMENSION_SVG'
font_size: FloatProperty(
name='Font Size',
description='Font Size',
default=10,
update=updateNode)
font_family: EnumProperty(
name='Font',
description='Font Size',
items=enum_item_4(["serif", 'sans-serif', 'monospace', 'cursive', 'fantasy', 'user']),
default='monospace',
update=updateNode)
user_font: StringProperty(
name='Font Name',
description='Define font name',
default='',
update=updateNode)
dimension_type: EnumProperty(
name='Type',
description='Dimension type. Horizontal = 0, Vertical = 1, Aligned = 2',
items=enum_item_4(["Horizontal", 'Vertical', 'Aligned']),
default='Aligned',
update=updateNode)
dimension_offset: FloatProperty(
name='Dim. Offset',
description='Dimension offset',
default=0,
update=updateNode)
line_extension: FloatProperty(
name='Lines Extension',
description='Text Rotation',
default=0,
update=updateNode)
decimal_precision: IntProperty(
name='Decimals',
description='Text Rotation',
default=2,
update=updateNode)
units: StringProperty(
name='Units',
description='units',
default='',
update=updateNode)
units_real: EnumProperty(
name='Units_real',
description='Dimentions feets or meters',
items=enum_item_4(['Metric', 'Imperialistic']),
default='Metric',
update=updateNode)
text: StringProperty(
name='Text',
description='Text',
default='',
update=updateNode)
text_offset: FloatProperty(
name='Text Offset',
description='Text offset',
default=0,
update=updateNode)
def sv_init(self, context):
self.inputs.new('SvVerticesSocket', "Location A")
self.inputs.new('SvVerticesSocket', "Location B")
self.inputs.new('SvStringsSocket', "Dim. Type").prop_name = 'dimension_type'
self.inputs.new('SvStringsSocket', "Dim. Offset").prop_name = 'dimension_offset'
self.inputs.new('SvStringsSocket', "Font Size").prop_name = 'font_size'
self.inputs.new('SvStringsSocket', "Text Offset").prop_name = 'text_offset'
self.inputs.new('SvSvgSocket', "Text Fill / Stroke")
self.inputs.new('SvSvgSocket', "Lines Fill / Stroke")
self.outputs.new('SvSvgSocket', "SVG Objects")
def draw_buttons(self, context, layout):
layout.prop(self, "line_extension", expand=False)
layout.prop(self, "decimal_precision", expand=False)
layout.prop(self, "units", expand=False)
layout.prop(self, "units_real", expand=True)
layout.prop(self, "font_family", expand=False)
if self.font_family == 'user':
layout.prop(self, "user_font")
def process(self):
if not self.outputs[0].is_linked:
return
params_in = [s.sv_get(deepcopy=False) for s in self.inputs[:6]]
texts_out = []
params_in.append(self.inputs['Text Fill / Stroke'].sv_get(deepcopy=False, default=[[None]]))
params_in.append(self.inputs['Lines Fill / Stroke'].sv_get(deepcopy=False, default=[[None]]))
font_family = self.user_font if self.font_family == 'user' else self.font_family
for params in zip(*mlr(params_in)):
svg_texts = []
for local_params in zip(*mlr(params)):
svg_texts.append(SvgDimension(*local_params, font_family, self))
texts_out.append(SvgGroup(svg_texts))
self.outputs[0].sv_set(texts_out)
def register():
bpy.utils.register_class(SvSvgDimensionNode)
def unregister():
bpy.utils.unregister_class(SvSvgDimensionNode)
if __name__ == '__main__': register()