Measure tools now shows decorator for the dimension of every line and the angles between them. Area calculation was removed and will be introduced in another operator

This commit is contained in:
Bruno Perdigão
2024-08-22 15:54:11 -03:00
parent 2100528cbe
commit f0338ffdda
5 changed files with 55 additions and 10 deletions
@@ -118,6 +118,7 @@ classes = (
mep.RegenerateDistributionElement,
prop.SnapMousePoint,
prop.PolylinePoint,
prop.PolylineMeasurement,
prop.BIMModelProperties,
prop.BIMArrayProperties,
prop.BIMStairProperties,
@@ -24,6 +24,7 @@ import bonsai.tool as tool
from math import sin, cos, radians, degrees, atan2, acos
from bpy.types import SpaceView3D
import math
from bpy_extras import view3d_utils
from mathutils import Vector, Matrix
from gpu_extras.batch import batch_for_shader
from typing import Union
@@ -311,7 +312,8 @@ class PolylineDecorator:
if cls.is_installed:
cls.uninstall()
handler = cls()
cls.handlers.append(SpaceView3D.draw_handler_add(handler.draw_text, (context,), "WINDOW", "POST_PIXEL"))
cls.handlers.append(SpaceView3D.draw_handler_add(handler.draw_input_panel, (context,), "WINDOW", "POST_PIXEL"))
cls.handlers.append(SpaceView3D.draw_handler_add(handler.draw_measurements, (context,), "WINDOW", "POST_PIXEL"))
cls.handlers.append(SpaceView3D.draw_handler_add(handler, (context,), "WINDOW", "POST_VIEW"))
cls.is_installed = True
@@ -507,7 +509,7 @@ class PolylineDecorator:
shader.uniform_float("color", color)
batch.draw(shader)
def draw_text(self, context):
def draw_input_panel(self, context):
texts = {"D": "Distance:", "A": "Angle:", "X": "X coord:", "Y": "Y coord:", "Z": "Z coord:", "AREA": "Area:"}
self.addon_prefs = tool.Blender.get_addon_preferences()
self.font_id = 0
@@ -525,9 +527,37 @@ class PolylineDecorator:
blf.color(self.font_id, *color_highlight)
else:
blf.color(self.font_id, *color)
blf.position(self.font_id, self.mouse_pos[0] + offset, self.mouse_pos[1] + offset - (new_line * i), 0)
blf.position(self.font_id, self.mouse_pos[0] + offset, self.mouse_pos[1] - (new_line * i), 0)
blf.draw(self.font_id, texts[key] + value)
def draw_measurements(self, context):
region = context.region
rv3d = region.data
measurement_prop = context.scene.BIMModelProperties.polyline_measurement
self.addon_prefs = tool.Blender.get_addon_preferences()
self.font_id = 0
blf.size(self.font_id, 12)
blf.enable(self.font_id, blf.SHADOW)
blf.shadow(self.font_id, 6, 0, 0, 0, 1)
color = self.addon_prefs.decorations_colour
blf.color(self.font_id, *color)
for i in range(len(measurement_prop)):
if i == 0:
continue
pos_dim = (Vector(measurement_prop[i].position) + Vector(measurement_prop[i-1].position)) / 2
coords_dim = view3d_utils.location_3d_to_region_2d(region, rv3d, pos_dim)
blf.position(self.font_id, coords_dim[0], coords_dim[1], 0)
blf.draw(self.font_id, "d: " + measurement_prop[i].dim)
pos_angle = measurement_prop[i-1].position
coords_angle = view3d_utils.location_3d_to_region_2d(region, rv3d, pos_angle)
blf.position(self.font_id, coords_angle[0], coords_angle[1], 0)
blf.draw(self.font_id, "a: " + measurement_prop[i].angle)
def __call__(self, context):
self.addon_prefs = tool.Blender.get_addon_preferences()
@@ -111,6 +111,10 @@ class PolylinePoint(PropertyGroup):
y: bpy.props.FloatProperty(name="Y")
z: bpy.props.FloatProperty(name="Z")
class PolylineMeasurement(PropertyGroup):
dim: bpy.props.StringProperty(name="Dimension")
angle: bpy.props.StringProperty(name="Angle")
position: bpy.props.FloatVectorProperty(name="Decorator Position", size=3)
class BIMModelProperties(PropertyGroup):
ifc_class: bpy.props.EnumProperty(items=get_ifc_class, name="Construction Class", update=update_ifc_class)
@@ -206,6 +210,7 @@ class BIMModelProperties(PropertyGroup):
snap_mouse_point: bpy.props.CollectionProperty(type=SnapMousePoint)
snap_mouse_ref: bpy.props.CollectionProperty(type=SnapMousePoint)
polyline_point: bpy.props.CollectionProperty(type=PolylinePoint)
polyline_measurement: bpy.props.CollectionProperty(type=PolylineMeasurement)
class BIMArrayProperties(PropertyGroup):
@@ -2311,7 +2311,7 @@ class MeasureTool(bpy.types.Operator):
self.input_options = ["D", "A", "X", "Y", "Z"]
self.input_type = "OFF"
self.input_value_xy = [None, None]
self.input_panel = {"D": "", "A": "", "X": "", "Y": "", "Z": "", "AREA": ""}
self.input_panel = {"D": "", "A": "", "X": "", "Y": "", "Z": ""}
self.snap_angle = None
def recalculate_inputs(self, context):
@@ -2354,7 +2354,6 @@ class MeasureTool(bpy.types.Operator):
PolylineDecorator.set_mouse_position(event)
self.input_panel = PolylineDecorator.calculate_distance_and_angle(context, self.is_input_on)
PolylineDecorator.set_input_panel(self.input_panel, self.input_type)
self.input_panel = PolylineDecorator.calculate_area(context)
tool.Blender.update_viewport()
return {"RUNNING_MODAL"}
@@ -2363,7 +2362,7 @@ class MeasureTool(bpy.types.Operator):
tool.Blender.update_viewport()
if event.value == "RELEASE" and event.type == "LEFTMOUSE":
tool.Snap.insert_polyline_point()
tool.Snap.insert_polyline_point(self.input_panel)
tool.Blender.update_viewport()
if event.value == "PRESS" and event.type == "C":
@@ -2426,9 +2425,7 @@ class MeasureTool(bpy.types.Operator):
if self.is_input_on and event.value == "RELEASE" and event.type in {"RET", "NUMPAD_ENTER", "RIGHTMOUSE"}:
self.recalculate_inputs(context)
tool.Snap.insert_polyline_point(
float(self.input_panel["X"]), float(self.input_panel["Y"]), float(self.input_panel["Z"])
)
tool.Snap.insert_polyline_point(self.input_panel)
self.is_input_on = False
self.input_type = "OFF"
self.number_input = []
+13 -1
View File
@@ -117,7 +117,13 @@ class Snap(bonsai.core.tool.Snap):
bpy.context.scene.BIMModelProperties.snap_mouse_ref.clear()
@classmethod
def insert_polyline_point(cls, x=None, y=None, z=None):
def insert_polyline_point(cls, input_panel):
x = float(input_panel['X'])
y = float(input_panel['Y'])
z = float(input_panel['Z'])
d = input_panel['D']
a = input_panel['A']
snap_vertex = bpy.context.scene.BIMModelProperties.snap_mouse_point[0]
if cls.use_default_container:
z = tool.Ifc.get_object(tool.Root.get_default_container()).location.z
@@ -139,6 +145,11 @@ class Snap(bonsai.core.tool.Snap):
polyline_point.y = y
polyline_point.z = z
polyline_measurement = bpy.context.scene.BIMModelProperties.polyline_measurement.add()
polyline_measurement.dim = d
polyline_measurement.angle = a
polyline_measurement.position = Vector((x, y, z))
@classmethod
def close_polyline(cls):
polyline_data = bpy.context.scene.BIMModelProperties.polyline_point
@@ -152,6 +163,7 @@ class Snap(bonsai.core.tool.Snap):
@classmethod
def clear_polyline(cls):
bpy.context.scene.BIMModelProperties.polyline_point.clear()
bpy.context.scene.BIMModelProperties.polyline_measurement.clear()
@classmethod
def remove_last_polyline_point(cls):