Skip to content

Commit 74b2fec

Browse files
committed
WIP-ADD create legend
1 parent bbf3cee commit 74b2fec

File tree

3 files changed

+842
-91
lines changed

3 files changed

+842
-91
lines changed

src/gh/components/DF_vizualization/code.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@ def RunScript(self,
3636
# by default we color the target
3737
min_value = min(min(sublist) for sublist in i_results.distances)
3838
max_value = max(max(sublist) for sublist in i_results.distances)
39-
40-
o_source = [df_vizualization.add_color(src, dist, min_value, max_value) for src, dist in zip(o_source, i_results.distances)]
4139

42-
o_target = [df_cvt_bindings.cvt_dfcloud_2_rhcloud(trg) for trg in i_results.target]
43-
44-
o_legend = []
40+
o_source = [df_vizualization.color_pcd(src, dist, min_value, max_value) for src, dist in zip(o_source, i_results.distances)]
4541

46-
# color the source pointcloud based on viz_settings
42+
o_target = [df_cvt_bindings.cvt_dfcloud_2_rhcloud(trg) for trg in i_results.target]
4743

48-
#make a legend
44+
o_legend = df_vizualization.create_legend(min_value, max_value)
4945

5046
return o_source, o_target, o_legend
5147

src/gh/diffCheck/diffCheck/df_vizualization.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,60 @@ def value_to_color(value, min_value, max_value):
6666
return interpolate_color(color1, color2, t)
6767

6868

69-
def add_color(pcd, values, min_value, max_values):
69+
def color_pcd(pcd, values, min_value, max_values):
7070

7171
for i, p in enumerate(pcd):
7272
mapped_color = value_to_color(values[i], min_value, max_values)
7373
p.Color = mapped_color
7474
return pcd
75+
76+
77+
def create_legend(min_value, max_value, steps=10, base_point=rg.Point3d(0, 0, 0), width=0.5, height=1, spacing=0):
78+
"""
79+
Create a legend in Rhino with colored hatches and text labels.
80+
81+
Parameters:
82+
min_value (float): Minimum value for the legend.
83+
max_value (float): Maximum value for the legend.
84+
steps (int): Number of steps in the legend.
85+
base_point (rg.Point3d): The base point where the legend starts.
86+
width (float): Width of each rectangle.
87+
height (float): Height of each rectangle.
88+
spacing (float): Spacing between rectangles.
89+
"""
90+
x, y, z = base_point.X, base_point.Y, base_point.Z
91+
92+
legend_geometry = []
93+
94+
for i in range(steps + 1):
95+
value = min_value + (max_value - min_value) * i / steps
96+
color = value_to_color(value, min_value, max_value)
97+
98+
rect_pts = [
99+
rg.Point3d(x, y + i * (height + spacing), z),
100+
rg.Point3d(x + width, y + i * (height + spacing), z),
101+
rg.Point3d(x + width, y + (i + 1) * height + i * spacing, z),
102+
rg.Point3d(x, y + (i + 1) * height + i * spacing, z),
103+
rg.Point3d(x, y + i * (height + spacing), z)
104+
]
105+
106+
mesh = rg.Mesh()
107+
for pt in rect_pts:
108+
mesh.Vertices.Add(pt)
109+
mesh.Faces.AddFace(0, 1, 2, 3)
110+
mesh.VertexColors.CreateMonotoneMesh(color)
111+
112+
polyline = rg.Polyline(rect_pts)
113+
114+
legend_geometry.append(mesh)
115+
116+
legend_geometry.append(polyline.ToPolylineCurve())
117+
118+
text_pt = rg.Point3d(x + width + spacing, y + i * (height + spacing) + height / 2, z)
119+
text_entity = rg.TextEntity()
120+
text_entity.Plane = rg.Plane(text_pt, rg.Vector3d.ZAxis)
121+
text_entity.Text = f"{value:.2f}"
122+
text_entity.TextHeight = height / 2
123+
legend_geometry.append(text_entity)
124+
125+
return legend_geometry

0 commit comments

Comments
 (0)