@@ -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