Fix small issue with area and length measurement text in polyline tool.

This commit is contained in:
Bruno Perdigão
2024-10-11 09:43:23 -03:00
parent 1225073bef
commit 0235d84f25
2 changed files with 19 additions and 16 deletions
@@ -583,16 +583,15 @@ class PolylineDecorator:
# Area and Length text # Area and Length text
polyline_verts = [Vector((p.x, p.y, p.z)) for p in polyline_points] polyline_verts = [Vector((p.x, p.y, p.z)) for p in polyline_points]
if polyline_verts[0] == polyline_verts[-1]:
polyline_verts = polyline_verts[:-1]
center = sum(polyline_verts, Vector()) / len(polyline_verts) # Center between all polyline points
area_text_coords = view3d_utils.location_3d_to_region_2d(region, rv3d, center)
total_length_text_coords = view3d_utils.location_3d_to_region_2d(region, rv3d, polyline_verts[-1])
# Area # Area
if measure_type == "AREA" and polyline_data.area: if measure_type == "AREA" and polyline_data.area:
if len(polyline_verts) < 3: if len(polyline_verts) < 3:
return return
center = sum(polyline_verts, Vector()) / len(polyline_verts) # Center between all polyline points
if polyline_verts[0] == polyline_verts[-1]:
center = sum(polyline_verts[:-1], Vector()) / len(polyline_verts[:-1]) # Doesn't use the last point if is a closed polyline
area_text_coords = view3d_utils.location_3d_to_region_2d(region, rv3d, center)
value = polyline_data.area value = polyline_data.area
text = f"area: {value}" text = f"area: {value}"
text_length = blf.dimensions(self.font_id, text) text_length = blf.dimensions(self.font_id, text)
@@ -603,8 +602,9 @@ class PolylineDecorator:
# Length # Length
if measure_type in {"POLYLINE", "AREA"}: if measure_type in {"POLYLINE", "AREA"}:
if len(polyline_verts) < 2: if len(polyline_verts) < 3:
return return
total_length_text_coords = view3d_utils.location_3d_to_region_2d(region, rv3d, polyline_verts[-1])
blf.position(self.font_id, total_length_text_coords[0], total_length_text_coords[1], 0) blf.position(self.font_id, total_length_text_coords[0], total_length_text_coords[1], 0)
value = polyline_data.total_length value = polyline_data.total_length
text = f"length: {value}" text = f"length: {value}"
@@ -360,14 +360,15 @@ class MeasureDecorator:
# Area and Length text # Area and Length text
polyline_verts = [Vector((p.x, p.y, p.z)) for p in polyline_points] polyline_verts = [Vector((p.x, p.y, p.z)) for p in polyline_points]
center = sum(polyline_verts, Vector()) / len(polyline_verts) # Center between all polyline points
area_text_coords = view3d_utils.location_3d_to_region_2d(region, rv3d, center)
total_length_text_coords = view3d_utils.location_3d_to_region_2d(region, rv3d, polyline_verts[-1])
# Area # Area
if measure_type == "AREA": if measure_type == "AREA" and polyline_data.area:
if len(polyline_verts) < 3: if len(polyline_verts) < 3:
return return
center = sum(polyline_verts, Vector()) / len(polyline_verts) # Center between all polyline points
if polyline_verts[0] == polyline_verts[-1]:
center = sum(polyline_verts[:-1], Vector()) / len(polyline_verts[:-1]) # Doesn't use the last point if is a closed polyline
area_text_coords = view3d_utils.location_3d_to_region_2d(region, rv3d, center)
value = polyline_data.area value = polyline_data.area
text = f"area: {value}" text = f"area: {value}"
text_length = blf.dimensions(self.font_id, text) text_length = blf.dimensions(self.font_id, text)
@@ -378,8 +379,9 @@ class MeasureDecorator:
# Length # Length
if measure_type in {"POLYLINE", "AREA"}: if measure_type in {"POLYLINE", "AREA"}:
if len(polyline_verts) < 2: if len(polyline_verts) < 3:
return return
total_length_text_coords = view3d_utils.location_3d_to_region_2d(region, rv3d, polyline_verts[-1])
blf.position(self.font_id, total_length_text_coords[0], total_length_text_coords[1], 0) blf.position(self.font_id, total_length_text_coords[0], total_length_text_coords[1], 0)
value = polyline_data.total_length value = polyline_data.total_length
text = f"length: {value}" text = f"length: {value}"
@@ -437,11 +439,12 @@ class MeasureDecorator:
self.draw_batch("LINES", [*z_axis], decorator_color_z_axis, [(0, 1)]) self.draw_batch("LINES", [*z_axis], decorator_color_z_axis, [(0, 1)])
# Area highlight # Area highlight
_, area = tool.Polyline.validate_input(polyline_data.area, "AREA") if polyline_data:
if area: _, area = tool.Polyline.validate_input(polyline_data.area, "AREA")
if float(area) > 0: if area:
tris, _ = self.calculate_polygon(polyline_verts) if float(area) > 0:
self.draw_batch("TRIS", polyline_verts, transparent_color(decorator_color_special), tris) tris = self.calculate_polygon(polyline_verts)
self.draw_batch("TRIS", polyline_verts, transparent_color(decorator_color_special), tris)
# Draw polyline with selected points # Draw polyline with selected points
self.line_shader.uniform_float("lineWidth", 2.0) self.line_shader.uniform_float("lineWidth", 2.0)