-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
svgwriter.py
1443 lines (1263 loc) · 61.9 KB
/
svgwriter.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# BlenderBIM Add-on - OpenBIM Blender Add-on
# Copyright (C) 2020, 2021 Dion Moult <dion@thinkmoult.com>
#
# This file is part of BlenderBIM Add-on.
#
# BlenderBIM Add-on 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 3 of the License, or
# (at your option) any later version.
#
# BlenderBIM Add-on 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 BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import os
import re
import bpy
import math
import bmesh
import shutil
import pystache
import mathutils
import xml.etree.ElementTree as ET
import svgwrite
import ifcopenshell
import ifcopenshell.util.element
import ifcopenshell.util.representation
import ifcopenshell.util.selector
import blenderbim.tool as tool
import blenderbim.bim.module.drawing.helper as helper
import blenderbim.bim.module.drawing.annotation as annotation
from blenderbim.bim.module.drawing.data import DrawingsData
from blenderbim.bim.module.drawing.data import DecoratorData
from blenderbim.bim.ifc import IfcStore
from math import pi, ceil, atan, degrees, acos
from mathutils import geometry, Vector
from bpy_extras import view3d_utils
from typing import Optional
class External(svgwrite.container.Group):
def __init__(self, xml, **extra):
self.xml = xml
# Remove namespace
ns = "{http://www.w3.org/2000/svg}"
nsl = len(ns)
for elem in self.xml.iter():
if elem.tag.startswith(ns):
elem.tag = elem.tag[nsl:]
super(External, self).__init__(**extra)
def get_xml(self):
return self.xml
class SvgWriter:
def __init__(self):
self.data_dir = None
self.human_scale = "NTS"
self.metadata = []
self.scale = 1 / 100 # 1:100
self.camera_width = None
self.camera_height = None
self.resource_paths = {}
def create_blank_svg(self, output_path):
self.calculate_scale()
self.svg = svgwrite.Drawing(
output_path,
size=("{}mm".format(self.width), "{}mm".format(self.height)),
viewBox=("0 0 {} {}".format(self.width, self.height)),
id="root",
data_scale=self.human_scale,
debug=False, # Disable validation so that we can insert the IFC namespace
)
self.svg.attribs["xmlns:ifc"] = "http://www.ifcopenshell.org/ns"
return self
def save(self):
self.svg.save(pretty=True)
def draw_underlay(self, image):
self.svg.add(self.svg.image(os.path.basename(image), width=self.width, height=self.height))
return self
def setup_drawing_resource_paths(self, element):
pset = ifcopenshell.util.element.get_pset(element, "EPset_Drawing")
for resource in ("Stylesheet", "Markers", "Symbols", "Patterns", "ShadingStyles"):
resource_path = pset.get(resource)
if not resource_path:
self.resource_paths[resource] = None
continue
resource_path = tool.Ifc.resolve_uri(resource_path)
os.makedirs(os.path.dirname(resource_path), exist_ok=True)
if not os.path.exists(resource_path):
resource_basename = os.path.basename(resource_path)
ootb_resource = os.path.join(bpy.context.scene.BIMProperties.data_dir, "assets", resource_basename)
print(
f"WARNING. Couldn't find {resource} for the drawing by the path: {resource_path}. Default BBIM resource will be copied from {ootb_resource}"
)
if os.path.exists(ootb_resource):
shutil.copy(ootb_resource, resource_path)
self.resource_paths[resource] = resource_path
def define_boilerplate(self):
self.add_stylesheet()
self.add_markers()
self.add_symbols()
self.add_patterns()
return self
def calculate_scale(self):
# svg_scale is for conversion from m to paper units
# paper units = mm x drawing scale
self.svg_scale = self.scale * 1000 # IFC is in meters, SVG is in mm
self.raw_width = self.camera_width
self.raw_height = self.camera_height
self.width = self.raw_width * self.svg_scale
self.height = self.raw_height * self.svg_scale
def add_stylesheet(self):
path = self.resource_paths["Stylesheet"]
if not path:
return
if not os.path.exists(path):
print(f"WARNING. Couldn't find stylesheet for the drawing by the path: {path}")
return
with open(path, "r") as stylesheet:
self.svg.defs.add(self.svg.style(stylesheet.read()))
def add_markers(self):
path = self.resource_paths["Markers"]
if not path:
return
if not os.path.exists(path):
print(f"WARNING. Couldn't find markers for the drawing by the path: {path}")
return
tree = ET.parse(path)
root = tree.getroot()
for child in root:
self.svg.defs.add(External(child))
def add_symbols(self):
path = self.resource_paths["Symbols"]
if not path:
return
if not os.path.exists(path):
print(f"WARNING. Couldn't find symbols for the drawing by the path: {path}")
return
tree = ET.parse(path)
root = tree.getroot()
for child in root:
self.svg.defs.add(External(child))
def find_xml_symbol_by_id(self, id):
tree = ET.parse(self.resource_paths["Symbols"])
xml_symbol = tree.find(f'.//*[@id="{id}"]')
return External(xml_symbol) if xml_symbol is not None else None
def add_patterns(self):
path = self.resource_paths["Patterns"]
if not path:
return
if not os.path.exists(path):
print(f"WARNING. Couldn't find patterns for the drawing by the path: {path}")
return
if not path or not os.path.exists(path):
return
tree = ET.parse(path)
root = tree.getroot()
for child in root:
self.svg.defs.add(External(child))
def draw_annotations(self, annotations, precision, decimal_places):
self.precision = precision
self.decimal_places = decimal_places
for element in annotations:
obj = tool.Ifc.get_object(element)
if not obj or element.ObjectType == "DRAWING":
continue
elif element.ObjectType == "GRID":
self.draw_grid_annotation(obj)
elif element.ObjectType == "TEXT_LEADER":
self.draw_leader_annotation(obj)
elif element.ObjectType == "STAIR_ARROW":
self.draw_stair_annotation(obj)
elif element.ObjectType == "DIMENSION":
self.draw_dimension_annotations(obj)
elif element.ObjectType == "ANGLE":
self.draw_angle_annotations(obj)
elif element.ObjectType == "RADIUS":
self.draw_radius_annotations(obj)
elif element.ObjectType == "DIAMETER":
self.draw_diameter_annotations(obj)
elif element.ObjectType == "ELEVATION":
self.draw_elevation_annotation(obj)
elif element.ObjectType == "SECTION":
self.draw_section_annotation(obj)
elif element.ObjectType == "BREAKLINE":
self.draw_break_annotations(obj)
elif element.ObjectType == "PLAN_LEVEL":
self.draw_plan_level_annotation(obj)
elif element.ObjectType == "SECTION_LEVEL":
self.draw_section_level_annotation(obj)
elif element.ObjectType == "TEXT":
self.draw_text_annotation(obj, obj.location)
elif element.ObjectType in ("FALL", "SLOPE_ANGLE", "SLOPE_FRACTION", "SLOPE_PERCENT"):
self.draw_fall_annotations(obj)
else:
self.draw_misc_annotation(obj)
# Experimental integration with the MeasureIt-ARCH Add-on
self.draw_measureit_arch_dimension_annotations()
return self
def draw_section_level_annotation(self, obj):
offset = Vector([self.raw_width, self.raw_height]) / 2
matrix_world = obj.matrix_world
classes = self.get_attribute_classes(obj)
element = tool.Ifc.get_entity(obj)
storey = tool.Drawing.get_annotation_element(element)
tag = storey.Name if storey else element.Description
dimension_data = DecoratorData.get_dimension_data(obj)
suppress_zero_inches = dimension_data["suppress_zero_inches"]
base_offset_y = 3.5
for spline in obj.data.splines:
points = self.get_spline_points(spline)
projected_points = [self.project_point_onto_camera(matrix_world @ p.co.xyz) for p in points]
projected_points_svg = [(offset + p.xy * Vector((1, -1))) * self.svg_scale for p in projected_points]
d = " ".join(["L {} {}".format(*p) for p in projected_points_svg])
d = "M{}".format(d[1:])
path = self.svg.add(self.svg.path(d=d, class_=" ".join(classes)))
text_position = projected_points_svg[0] - Vector((0, base_offset_y))
vector = projected_points_svg[0] - projected_points_svg[1]
angle = math.degrees(vector.angle_signed(Vector((1, 0))))
# TODO: allow metric to be configurable
def get_text():
z = (matrix_world @ points[0].co.xyz).z
rl = helper.format_distance(
z,
precision=self.precision,
decimal_places=self.decimal_places,
suppress_zero_inches=suppress_zero_inches,
)
text = "RL {}{}".format("" if z < 0 else "+", rl)
return text
self.draw_dimension_text(
get_text, tag, dimension_data, text_position=text_position, angle=angle, class_str="SECTIONLEVEL"
)
def draw_stair_annotation(self, obj):
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
matrix_world = obj.matrix_world
classes = self.get_attribute_classes(obj)
for spline in obj.data.splines:
points = self.get_spline_points(spline)
projected_points = [self.project_point_onto_camera(matrix_world @ p.co.xyz) for p in points]
d = " ".join(
[
"L {} {}".format((x_offset + p.x) * self.svg_scale, (y_offset - p.y) * self.svg_scale)
for p in projected_points
]
)
d = "M{}".format(d[1:])
start = Vector(((x_offset + projected_points[0].x), (y_offset - projected_points[0].y)))
next_point = Vector(((x_offset + projected_points[1].x), (y_offset - projected_points[1].y)))
text_position = (start * self.svg_scale) - ((next_point - start).normalized() * 5)
path = self.svg.add(self.svg.path(d=d, class_=" ".join(classes)))
self.svg.add(
self.svg.text(
"UP",
insert=tuple(text_position),
class_="STAIR",
**SvgWriter.get_box_alignment_parameters("center"),
)
)
def draw_grid_annotation(self, obj):
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
matrix_world = obj.matrix_world
classes = self.get_attribute_classes(obj)
for edge in obj.data.edges:
v0_global = matrix_world @ obj.data.vertices[edge.vertices[0]].co.xyz
v1_global = matrix_world @ obj.data.vertices[edge.vertices[1]].co.xyz
v0 = self.project_point_onto_camera(v0_global)
v1 = self.project_point_onto_camera(v1_global)
start = Vector(((x_offset + v0.x), (y_offset - v0.y)))
end = Vector(((x_offset + v1.x), (y_offset - v1.y)))
vector = end - start
line = self.svg.add(
self.svg.line(
start=tuple(start * self.svg_scale), end=tuple(end * self.svg_scale), class_=" ".join(classes)
)
)
line["stroke-dasharray"] = "12.5, 3, 3, 3"
axis_tag = tool.Ifc.get_entity(obj).Name
text_style = SvgWriter.get_box_alignment_parameters("center")
self.svg.add(
self.svg.text(
axis_tag,
insert=tuple(start * self.svg_scale),
class_="GRID",
**text_style,
)
)
self.svg.add(
self.svg.text(
axis_tag,
insert=tuple(end * self.svg_scale),
class_="GRID",
**text_style,
)
)
def draw_misc_annotation(self, obj: bpy.types.Object) -> None:
# We have to decide whether this should come from Blender or from IFC.
# For the moment, for convenience of experimenting with ideas, it comes
# from Blender. In the future, it should probably come from IFC.
classes = self.get_attribute_classes(obj)
if obj.data is None:
return self.draw_empty_annotation(obj, classes)
elif not isinstance(obj.data, bpy.types.Mesh):
return
elif len(obj.data.vertices) and not len(obj.data.edges):
return self.draw_point_annotation(obj, classes)
elif len(obj.data.polygons) == 0:
return self.draw_edge_annotation(obj, classes)
bm = bmesh.new()
bm.from_mesh(obj.data)
bmesh.ops.dissolve_limit(bm, angle_limit=pi / 180 * 1, verts=bm.verts, edges=bm.edges)
bm.faces.ensure_lookup_table()
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
matrix_world = obj.matrix_world
for face in bm.faces:
projected_points = [self.project_point_onto_camera(matrix_world @ p.co.xyz) for p in face.verts]
projected_points.append(projected_points[0])
d = " ".join(
[
"L {} {}".format((x_offset + p.x) * self.svg_scale, (y_offset - p.y) * self.svg_scale)
for p in projected_points
]
)
d = "M{}".format(d[1:])
path = self.svg.add(self.svg.path(d=d, class_=" ".join(classes)))
def get_attribute_classes(self, obj: bpy.types.Object) -> list[str]:
element = tool.Ifc.get_entity(obj)
global_id = "GlobalId-{}".format(element.GlobalId)
predefined_type = "PredefinedType-" + tool.Drawing.canonicalise_class_name(
str(ifcopenshell.util.element.get_predefined_type(element))
)
classes = [global_id, element.is_a(), predefined_type]
custom_classes: str = ifcopenshell.util.element.get_pset(element, "EPset_Annotation", "Classes")
if custom_classes:
classes.extend(custom_classes.split())
for key in self.metadata:
value = ifcopenshell.util.selector.get_element_value(element, key)
if value:
classes.append(
tool.Drawing.canonicalise_class_name(key) + "-" + tool.Drawing.canonicalise_class_name(str(value))
)
return classes
def draw_line_annotation(self, obj):
# TODO: properly scope these offsets
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
classes = self.get_attribute_classes(obj)
matrix_world = obj.matrix_world
if isinstance(obj.data, bpy.types.Curve):
for spline in obj.data.splines:
points = self.get_spline_points(spline)
projected_points = [self.project_point_onto_camera(matrix_world @ p.co.xyz) for p in points]
d = " ".join(
[
"L {} {}".format((x_offset + p.x) * self.svg_scale, (y_offset - p.y) * self.svg_scale)
for p in projected_points
]
)
d = "M{}".format(d[1:])
path = self.svg.add(self.svg.path(d=d, class_=" ".join(classes)))
elif isinstance(obj.data, bpy.types.Mesh):
self.draw_edge_annotation(obj, classes)
def draw_edge_annotation(self, obj, classes):
predefined_type = classes[2].split("-", 1)[1]
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
matrix_world = obj.matrix_world
def draw_simple_edge_annotation(v0, v1):
v0_global = matrix_world @ obj.data.vertices[v0].co.xyz
v1_global = matrix_world @ obj.data.vertices[v1].co.xyz
v0 = self.project_point_onto_camera(v0_global)
v1 = self.project_point_onto_camera(v1_global)
start = Vector(((x_offset + v0.x), (y_offset - v0.y)))
end = Vector(((x_offset + v1.x), (y_offset - v1.y)))
line = self.svg.add(
self.svg.line(start=start * self.svg_scale, end=end * self.svg_scale, class_=" ".join(classes))
)
def draw_batting_annotation():
v0_global = matrix_world @ obj.data.vertices[0].co.xyz
v1_global = matrix_world @ obj.data.vertices[1].co.xyz
v0 = self.project_point_onto_camera(v0_global)
v1 = self.project_point_onto_camera(v1_global)
start_svg = Vector(((x_offset + v0.x), (y_offset - v0.y))) * self.svg_scale
end_svg = Vector(((x_offset + v1.x), (y_offset - v1.y))) * self.svg_scale
element = tool.Ifc.get_entity(obj)
pset_data = ifcopenshell.util.element.get_pset(element, "BBIM_Batting") or {}
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())
# default thickness set to 15 mm in paper space to keep the insulation visible
thickness = pset_data["Thickness"] * unit_scale * self.svg_scale if "Thickness" in pset_data else 15.0
reverse_x = pset_data.get("Reverse pattern direction", False)
if reverse_x:
start_svg, end_svg = end_svg, start_svg
pattern_dir = (end_svg - start_svg).normalized()
pattern_length = (end_svg - start_svg).length
segment_width = thickness / 2.5
segments = ceil(pattern_length / segment_width)
end_marker_width = pattern_length - segment_width * (segments - 1)
points = [start_svg + pattern_dir * segment_width * i for i in range(segments)]
marker_id = f"batting-{element.GlobalId}"
marker_end_id = f"batting-end-{element.GlobalId}"
path_data = f"""M 0 {0.2*thickness}
A {0.5*segment_width} {0.2*thickness} 0 0 1 {segment_width} {0.2*thickness}
L {0.5*segment_width} {0.8*thickness}
M 0 {0.2*thickness}
L {0.5*segment_width} {0.8*thickness}
A {0.5*segment_width} {0.2*thickness} 0 0 0 {segment_width} {1.0*thickness}
M {0.5*segment_width} {0.8*thickness}
A {0.5*segment_width} {0.2*thickness} 0 0 1 0 {1.0*thickness}
"""
path_data = " ".join(path_data.split())
svg_path = self.svg.path(style="fill: none; stroke:black; stroke-width:0.18", d=path_data)
if reverse_x:
svg_path.update({"transform": f"scale(-1,-1) translate(-{segment_width}, -{thickness})"})
marker = self.svg.marker(
markerUnits="userSpaceOnUse",
insert=(0, thickness / 2),
size=(segment_width, thickness),
orient="auto",
id=marker_id,
)
marker.add(svg_path)
self.svg.add(marker)
# separate marker for the end of the pattern to truncate it more gracefully
marker_end = marker.copy()
marker_end.update({"markerWidth": end_marker_width, "id": marker_end_id})
self.svg.add(marker_end)
polyline_style = (
f"marker-start: url(#{marker_id}); "
f"marker-mid: url(#{marker_id}); stroke: none; "
f"marker-end: url(#{marker_end_id}); stroke: none; "
)
self.svg.add(self.svg.polyline(points=points, class_=" ".join(classes), style=polyline_style))
def draw_revision_cloud_annotation():
segment_width = 15.0
base_height = 1
width = 5
def get_svg_half_circle(height, width):
cp0 = f"0,-{height}"
cp1 = f"{width},-{height}"
end_point = f"{width},0"
circle = f"c{cp0} {cp1} {end_point}"
return circle
def get_revision_pattern(base_offset):
pattern = f"m{base_offset.x},{base_offset.y}"
pattern += " " + get_svg_half_circle(2 * base_height, width)
pattern += " " + get_svg_half_circle(2.5 * base_height, width)
pattern += " " + get_svg_half_circle(1.5 * base_height, width)
return pattern
def get_scale(size, direction):
original_edge = direction * size
current_svg_segments = ceil(size / segment_width) * segment_width * direction
scale = [1 if original_edge[i] == 0 else original_edge[i] / current_svg_segments[i] for i in range(2)]
return "scale(%f, %f)" % (scale[0], scale[1])
def poly_to_edges(poly):
edges = []
n_verts = len(poly)
lats_index = n_verts - 1
for i in range(len(poly)):
edge = [poly[i], (poly[i + 1]) if i != lats_index else poly[0]]
edges.append(edge)
return edges
element = tool.Ifc.get_entity(obj)
safe_offset_x = 2.0
marker_width = segment_width + safe_offset_x * 2
market_height = 15.0
ref_y = 5.0
revision_pattern = get_revision_pattern(Vector([safe_offset_x, ref_y]))
bm = tool.Blender.get_bmesh_for_mesh(obj.data).copy()
bmesh.ops.contextual_create(bm, geom=bm.edges[:])
faces = bm.faces[:]
assert len(faces) == 1, "Revision cloud edges must form just 1 polygon"
# ensure clockwise order of polygon verts
# given default blender counter-clockwise order
polygon = faces[0]
if polygon.normal.z > 0:
polygon.normal_flip()
marker_id = f"revision-cloud-{element.GlobalId}"
svg_path = self.svg.path(style="fill: none; stroke:red; stroke-width:0.20", d=revision_pattern)
marker = self.svg.marker(
markerUnits="userSpaceOnUse",
insert=(safe_offset_x, ref_y),
size=(marker_width, market_height),
orient="auto",
id=marker_id,
)
marker.add(svg_path)
self.svg.add(marker)
for v0, v1 in poly_to_edges(polygon.verts):
v0_global = matrix_world @ v0.co.xyz
v1_global = matrix_world @ v1.co.xyz
v0 = self.project_point_onto_camera(v0_global)
v1 = self.project_point_onto_camera(v1_global)
start_svg = Vector(((x_offset + v0.x), (y_offset - v0.y))) * self.svg_scale
end_svg = Vector(((x_offset + v1.x), (y_offset - v1.y))) * self.svg_scale
pattern_edge = end_svg - start_svg
pattern_dir = pattern_edge.normalized()
pattern_length = pattern_edge.length
segments = ceil(pattern_length / segment_width)
pattern_dir_step = pattern_dir * segment_width
# it takes atleast 2 points to preserve the edge direction
# if there is just 1 segment then we still add second point and then hide the "marker-end"
n_points = max(segments, 2)
points = [pattern_dir_step * i for i in range(n_points)]
polyline_style = f"marker: url(#{marker_id}); stroke: none; "
if segments == 1:
polyline_style += "marker-end: none; "
polyline_transform = f"translate({start_svg.x}, {start_svg.y}) {get_scale(pattern_length, pattern_dir)}"
polyline = self.svg.polyline(
points=points, class_=" ".join(classes), style=polyline_style, transform=polyline_transform
)
self.svg.add(polyline)
def draw_section_annotation():
display_data = DecoratorData.get_section_markers_display_data(obj)
connect_markers = display_data["connect_markers"]
if connect_markers:
for edge in obj.data.edges:
draw_simple_edge_annotation(*edge.vertices[:])
else:
for edge in obj.data.edges:
v0_marker_position = "start" if edge.vertices[0] == 0 else "end"
v1_marker_position = "start" if edge.vertices[1] == 0 else "end"
v0_global = matrix_world @ obj.data.vertices[edge.vertices[0]].co.xyz
v1_global = matrix_world @ obj.data.vertices[edge.vertices[1]].co.xyz
v0 = self.project_point_onto_camera(v0_global)
v1 = self.project_point_onto_camera(v1_global)
start = Vector(((x_offset + v0.x), (y_offset - v0.y)))
end = Vector(((x_offset + v1.x), (y_offset - v1.y)))
edge_dir = (end - start).normalized()
circle_radius = 5
segment_size = circle_radius * 3
if display_data[v0_marker_position]["add_symbol"]:
self.svg.add(
self.svg.line(
start=start * self.svg_scale,
end=start * self.svg_scale + edge_dir * segment_size,
class_=" ".join(classes),
)
)
if display_data[v1_marker_position]["add_symbol"]:
self.svg.add(
self.svg.line(
start=end * self.svg_scale,
end=end * self.svg_scale - edge_dir * segment_size,
class_=" ".join(classes),
)
)
if predefined_type == "BATTING":
draw_batting_annotation()
elif predefined_type == "REVISIONCLOUD":
draw_revision_cloud_annotation()
elif predefined_type == "SECTION":
draw_section_annotation()
else:
for edge in obj.data.edges:
draw_simple_edge_annotation(*edge.vertices[:])
def draw_leader_annotation(self, obj):
self.draw_line_annotation(obj)
spline = obj.data.splines[0]
spline_points = spline.bezier_points if spline.bezier_points else spline.points
if spline_points:
position = obj.matrix_world @ spline_points[0].co.xyz
else:
position = Vector((0, 0, 0))
self.draw_text_annotation(obj, position)
def draw_section_annotation(self, obj):
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
self.draw_line_annotation(obj)
display_data = DecoratorData.get_section_markers_display_data(obj)
for edge in obj.data.edges:
edge_verts = [obj.data.vertices[v_i] for v_i in edge.vertices]
# convert edge vertiices coordinates to svg space
# to calculate the edge angle and for later use
edge_verts_svg = []
for vert in edge_verts:
point = obj.matrix_world @ vert.co
symbol_position = self.project_point_onto_camera(point)
symbol_position = Vector(((x_offset + symbol_position.x), (y_offset - symbol_position.y)))
symbol_position_svg = symbol_position * self.svg_scale
edge_verts_svg.append(symbol_position_svg)
edge_dir = (edge_verts_svg[1] - edge_verts_svg[0]).normalized()
angle = degrees(edge_dir.xy.angle_signed(Vector([1, 0])))
for v_i, symbol_position_svg in zip(edge.vertices, edge_verts_svg):
current_marker_position = "start" if v_i == 0 else "end"
# marker arrow symbol
if display_data[current_marker_position]["add_symbol"]:
transform = "rotate({}, {}, {})".format(angle, *symbol_position_svg.xy)
symbol_id = display_data[current_marker_position]["symbol"]
self.svg.add(self.svg.use(f"#{symbol_id}", insert=symbol_position_svg, transform=transform))
# marker circle and it's text
if display_data[current_marker_position]["add_circle"]:
self.svg.add(self.svg.use("#section-tag", insert=symbol_position_svg))
reference_id, sheet_id = self.get_reference_and_sheet_id_from_annotation(tool.Ifc.get_entity(obj))
text_position = symbol_position_svg
text_style = SvgWriter.get_box_alignment_parameters("center")
self.svg.add(
self.svg.text(
reference_id,
insert=(text_position[0], text_position[1] - 2.5),
class_="SECTION",
**text_style,
)
)
self.svg.add(
self.svg.text(
sheet_id, insert=(text_position[0], text_position[1] + 2.5), class_="SECTION", **text_style
)
)
def draw_elevation_annotation(self, obj):
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
symbol_position = self.project_point_onto_camera(obj.location)
symbol_position = Vector(((x_offset + symbol_position.x), (y_offset - symbol_position.y)))
symbol_position_svg = symbol_position * self.svg_scale
v1 = self.project_point_onto_camera(obj.matrix_world @ Vector((0, 0, 0)))
v2 = self.project_point_onto_camera(obj.matrix_world @ Vector((0, 0, -1)))
angle = -math.degrees((v2 - v1).xy.angle_signed(Vector((0, 1))))
transform = "rotate({}, {}, {})".format(angle, *symbol_position_svg.xy)
self.svg.add(self.svg.use("#elevation-arrow", insert=symbol_position_svg, transform=transform))
self.svg.add(self.svg.use("#elevation-tag", insert=symbol_position_svg))
reference_id, sheet_id = self.get_reference_and_sheet_id_from_annotation(tool.Ifc.get_entity(obj))
text_position = symbol_position_svg
text_style = SvgWriter.get_box_alignment_parameters("center")
self.svg.add(
self.svg.text(
reference_id, insert=(text_position[0], text_position[1] - 2.5), class_="ELEVATION", **text_style
)
)
self.svg.add(
self.svg.text(sheet_id, insert=(text_position[0], text_position[1] + 2.5), class_="ELEVATION", **text_style)
)
def get_reference_and_sheet_id_from_annotation(self, element):
reference_id = "-"
sheet_id = "-"
drawing = tool.Drawing.get_annotation_element(element)
reference = tool.Drawing.get_drawing_reference(drawing)
if reference:
for sheet_reference in tool.Ifc.get().by_type("IfcDocumentReference"):
reference_description = tool.Drawing.get_reference_description(sheet_reference)
if reference_description != "DRAWING" or sheet_reference.Location != reference.Location:
continue
sheet = tool.Drawing.get_reference_document(sheet_reference)
if sheet:
if tool.Ifc.get_schema() == "IFC2X3":
reference_id = sheet_reference.ItemReference or "-"
sheet_id = sheet.DocumentId or "-"
else:
reference_id = sheet_reference.Identification or "-"
sheet_id = sheet.Identification or "-"
return (reference_id, sheet_id)
break
return ("-", "-")
@staticmethod
def get_box_alignment_parameters(box_alignment):
"""Convenience method to get svg parameters for text alignment
in a readable way.
Metehod expecting values like:
`top-left`, `top-middle`, `top-right`,
`middle-left`, `center`, `middle-right`,
`bottom-left`, `bottom-middle`, `bottom-right`
"""
# reference for alignment values:
# https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor
# https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/alignment-baseline
vertical_alignment = {
"top": "hanging",
"bottom": "baseline",
"center": "middle",
"middle": "middle",
}
alignment_baseline = vertical_alignment[next(align for align in vertical_alignment if align in box_alignment)]
horizontal_alignment = {
"left": "start",
"right": "end",
"center": "middle",
"middle": "middle",
}
text_anchor = horizontal_alignment[next(align for align in horizontal_alignment if align in box_alignment)]
# using dominant-baseline because we plan to use <tspan> subtags
# otherwise alignment-baseline would be sufficient
return {
"dominant-baseline": alignment_baseline,
"text-anchor": text_anchor,
}
def add_fill_bg(self, element, copy=True):
if copy:
element = element.copy()
if hasattr(element, "xml"):
attrib = element.xml.attrib
elif isinstance(element, ET.Element):
attrib = element.attrib
else: # assuming it's svgwrite.base.BaseElement
attrib = element.attribs
attrib["filter"] = "url(#fill-background)"
return element
def draw_text_annotation(self, text_obj: bpy.types.Object, position: Vector) -> None:
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
element = tool.Ifc.get_entity(text_obj)
text_literals = tool.Drawing.get_text_literal(text_obj, return_list=True)
product = tool.Drawing.get_assigned_product(element)
text_position = self.project_point_onto_camera(position)
text_position = Vector(((x_offset + text_position.x), (y_offset - text_position.y)))
text_position_svg = text_position * self.svg_scale
text_position_svg_str = ", ".join(map(str, text_position_svg))
angle = self.get_empty_object_angle(text_obj)
classes = self.get_attribute_classes(text_obj)
classes_str = " ".join(classes)
fill_bg = "fill-bg" in classes
symbol = tool.Drawing.get_annotation_symbol(element)
template_text_fields = []
if symbol:
symbol_transform = self.get_symbol_transform(text_position_svg_str, angle, text_obj)
symbol_svg = self.find_xml_symbol_by_id(symbol)
if symbol_svg:
symbol_xml = symbol_svg.get_xml()
template_text_fields = symbol_xml.findall('.//text[@data-type="text-template"]')
# if there is a symbol with template text fields
# then we just populate it's fields with the data from text literals
if template_text_fields:
symbol_xml.attrib["transform"] = symbol_transform
symbol_xml.attrib.pop("id")
# NOTE: zip makes sure that we iterate over the shortest list
for field, text_literal in zip(template_text_fields, text_literals):
field.text = tool.Drawing.replace_text_literal_variables(
text_literal.Literal, product or element
)
field.attrib["class"] = classes_str
if fill_bg:
symbol_copied = symbol_svg.copy()
for text_tag in symbol_copied.xml.findall("text"):
self.add_fill_bg(text_tag, copy=False)
# NOTE: in case we'll later need to add fill-bg for the entire symbol:
# self.add_fill_bg(symbol_svg, copy=False)
self.svg.add(symbol_copied)
self.svg.add(symbol_svg)
return None
if not symbol_svg or not template_text_fields:
self.draw_symbol(symbol, symbol_transform)
line_number = 0
for text_literal in text_literals:
text = tool.Drawing.replace_text_literal_variables(text_literal.Literal, product or element)
text_tags = self.create_text_tag(
text,
text_position_svg,
angle,
text_literal.BoxAlignment,
classes_str,
fill_bg=fill_bg,
line_number_start=line_number,
)
for tag in text_tags:
self.svg.add(tag)
line_number += len(tag.elements)
def draw_empty_annotation(self, obj: bpy.types.Object, classes: list[str]) -> None:
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
point = self.project_point_onto_camera(obj.matrix_world.translation)
element = tool.Ifc.get_entity(obj)
svg_id = tool.Drawing.get_annotation_symbol(element)
if not svg_id:
svg_id = str(ifcopenshell.util.element.get_predefined_type(element))
if not svg_id:
return
point = Vector(((x_offset + point.x), (y_offset - point.y)))
symbol_position_svg = point * self.svg_scale
svg_position_str = ", ".join(map(str, symbol_position_svg))
angle = self.get_empty_object_angle(obj)
symbol_transform = self.get_symbol_transform(svg_position_str, angle, obj)
self.draw_symbol(svg_id, symbol_transform)
def get_empty_object_angle(self, obj: bpy.types.Object) -> float:
def get_basis_vector(matrix: bpy.types.Object, i: int = 0) -> Vector:
"""returns basis vector for i in world space, unaffected by object scale"""
return matrix.inverted()[i].to_3d().normalized()
text_dir_world_x_axis = get_basis_vector(obj.matrix_world)
# RCP cameras may be scaled, so reset scales.
camera_matrix = tool.Drawing.get_camera_matrix(self.camera)
text_dir = (camera_matrix.inverted().to_quaternion() @ text_dir_world_x_axis).to_2d().normalized()
return math.degrees(-text_dir.angle_signed(Vector((1, 0))))
def get_symbol_transform(
self, svg_position_str: str, angle: float = 0.0, obj: Optional[bpy.types.Object] = None
) -> str:
"""`obj` will be used to identify symbol scale,
ignore it if object scale doesn't match the symbol scale"""
# NOTE: for now we assume that scale is uniform
scale = 1.0 if not obj else obj.scale.x
return f"translate({svg_position_str}) rotate({angle}) scale({scale})"
def draw_symbol(self, symbol_id: str, symbol_transform: str) -> None:
self.svg.add(self.svg.use(f"#{symbol_id}", transform=symbol_transform))
def draw_point_annotation(self, obj: bpy.types.Object, classes: list[str]) -> None:
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
matrix_world = obj.matrix_world
projected_points = [self.project_point_onto_camera(matrix_world @ v.co) for v in obj.data.vertices]
element = tool.Ifc.get_entity(obj)
svg_id = tool.Drawing.get_annotation_symbol(element)
if not svg_id:
svg_id = str(ifcopenshell.util.element.get_predefined_type(element))
if not svg_id:
return
for symbol_position in projected_points:
symbol_position = Vector(((x_offset + symbol_position.x), (y_offset - symbol_position.y)))
symbol_position_svg = symbol_position * self.svg_scale
svg_position_str = ", ".join(map(str, symbol_position_svg))
symbol_transform = self.get_symbol_transform(svg_position_str)
self.draw_symbol(svg_id, symbol_transform)
def draw_break_annotations(self, obj):
x_offset = self.raw_width / 2
y_offset = self.raw_height / 2
classes = self.get_attribute_classes(obj)
matrix_world = obj.matrix_world
for edge in obj.data.edges:
points = [obj.data.vertices[v] for v in edge.vertices]
projected_points = [self.project_point_onto_camera(matrix_world @ p.co.xyz) for p in points]
projected_points = [
projected_points[0],
(projected_points[0] + projected_points[1]) / 2,
projected_points[1],
]
d = " ".join(
[
"L {} {}".format((x_offset + p.x) * self.svg_scale, (y_offset - p.y) * self.svg_scale)
for p in projected_points
]
)
d = "M{}".format(d[1:])
path = self.svg.add(self.svg.path(d=d, class_=" ".join(classes)))
def draw_plan_level_annotation(self, obj):
offset = Vector([self.raw_width, self.raw_height]) / 2
matrix_world = obj.matrix_world
classes = self.get_attribute_classes(obj)
element = tool.Ifc.get_entity(obj)
description = element.Description
dimension_data = DecoratorData.get_dimension_data(obj)
suppress_zero_inches = dimension_data["suppress_zero_inches"]
base_offset_y = 1.0
for spline in obj.data.splines:
points = self.get_spline_points(spline)
projected_points = [self.project_point_onto_camera(matrix_world @ p.co.xyz) for p in points]
projected_points_svg = [(offset + p.xy * Vector((1, -1))) * self.svg_scale for p in projected_points]
d = " ".join(["L {} {}".format(*p) for p in projected_points_svg])
d = "M{}".format(d[1:])
path = self.svg.add(self.svg.path(d=d, class_=" ".join(classes)))
text_position = projected_points_svg[0] - Vector((0, base_offset_y))
text_dir = projected_points_svg[1] - projected_points_svg[0]
if text_dir.x < 0:
box_alignment = "bottom-right"
text_dir *= -1
else:
box_alignment = "bottom-left"
angle = math.degrees(text_dir.angle_signed(Vector((1, 0))))
# TODO: allow metric to be configurable
def get_text():
z = (matrix_world @ points[0].co.xyz).z
rl = helper.format_distance(
z,
precision=self.precision,
decimal_places=self.decimal_places,
suppress_zero_inches=suppress_zero_inches,
)
text = "{}{}".format("" if z < 0 else "+", rl)
return text
self.draw_dimension_text(
get_text,
description,
dimension_data,
text_position=text_position,
angle=angle,
class_str="PLANLEVEL",
box_alignment=box_alignment,
)