| 1 |
# Bonsai - OpenBIM Blender Add-on |
| 2 |
# Copyright (C) 2024 Bruno Perdigão <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of Bonsai. |
| 5 |
# |
| 6 |
# Bonsai is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Bonsai is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
import bpy |
| 20 |
import bmesh |
| 21 |
import math |
| 22 |
import ifcopenshell |
| 23 |
import ifcopenshell.util.unit |
| 24 |
import bonsai.core.tool |
| 25 |
import bonsai.tool as tool |
| 26 |
from bonsai.bim.module.drawing.helper import format_distance |
| 27 |
from dataclasses import dataclass, field |
| 28 |
from lark import Lark, Transformer |
| 29 |
from math import degrees, radians, sin, cos, tan |
| 30 |
from mathutils import Vector, Matrix |
| 31 |
from typing import Optional, Union, Literal, List |
| 32 |
|
| 33 |
|
| 34 |
class Polyline(bonsai.core.tool.Polyline): |
| 35 |
@dataclass |
| 36 |
class PolylineUI: |
| 37 |
_D: str = "" |
| 38 |
_A: str = "" # Relative to previous polyline points |
| 39 |
_WORLD_ANGLE: str = "" # Relative to World Origin. Only used for specific operation. Not used on the UI. |
| 40 |
_X: str = "" |
| 41 |
_Y: str = "" |
| 42 |
_Z: str = "" |
| 43 |
_AREA: str = "0" |
| 44 |
input_options: List[str] = field(default_factory=list) |
| 45 |
|
| 46 |
def set_value(self, attribute_name, value): |
| 47 |
value = str(value) |
| 48 |
setattr(self, f"_{attribute_name}", value) |
| 49 |
|
| 50 |
def get_text_value(self, attribute_name): |
| 51 |
value = getattr(self, f"_{attribute_name}") |
| 52 |
return value |
| 53 |
|
| 54 |
def get_number_value(self, attribute_name): |
| 55 |
value = getattr(self, f"_{attribute_name}") |
| 56 |
if value: |
| 57 |
return float(value) |
| 58 |
else: |
| 59 |
return value |
| 60 |
|
| 61 |
def get_formatted_value(self, attribute_name): |
| 62 |
value = self.get_number_value(attribute_name) |
| 63 |
context = bpy.context |
| 64 |
if value is None: |
| 65 |
return None |
| 66 |
if attribute_name == "A": |
| 67 |
value = float(self.get_text_value(attribute_name)) |
| 68 |
return f"{value:.2f}°" |
| 69 |
if attribute_name == "AREA": |
| 70 |
return Polyline.format_input_ui_units(value, True) |
| 71 |
else: |
| 72 |
return Polyline.format_input_ui_units(value) |
| 73 |
|
| 74 |
@dataclass |
| 75 |
class ToolState: |
| 76 |
use_default_container: bool = None |
| 77 |
snap_angle: float = None |
| 78 |
is_input_on: bool = None |
| 79 |
lock_axis: bool = False |
| 80 |
# angle_axis_start: Vector |
| 81 |
# angle_axis_end: Vector |
| 82 |
axis_method: Literal["X", "Y", "Z", None] = None |
| 83 |
plane_method: Literal["XY", "XZ", "YZ", None] = None |
| 84 |
plane_origin: Vector = Vector((0.0, 0.0, 0.0)) |
| 85 |
instructions: str = """TAB: Cycle Input |
| 86 |
M: Modify Snap Point |
| 87 |
C: Close |
| 88 |
Backspace: Remove |
| 89 |
X Y: Axis |
| 90 |
L: Lock axis |
| 91 |
""" |
| 92 |
snap_info: str = None |
| 93 |
mode: Literal["Mouse", "Select", "Edit", None] = None |
| 94 |
input_type: "Polyline.InputType" = None |
| 95 |
|
| 96 |
@classmethod |
| 97 |
def create_input_ui(cls, input_options: List[str] = []) -> PolylineUI: |
| 98 |
return cls.PolylineUI(input_options=input_options) |
| 99 |
|
| 100 |
@classmethod |
| 101 |
def create_tool_state(cls) -> ToolState: |
| 102 |
return cls.ToolState() |
| 103 |
|
| 104 |
@classmethod |
| 105 |
def calculate_distance_and_angle( |
| 106 |
cls, context: bpy.types.Context, input_ui: PolylineUI, tool_state: ToolState, should_round: bool = False |
| 107 |
) -> None: |
| 108 |
|
| 109 |
polyline_data = context.scene.BIMPolylineProperties.insertion_polyline |
| 110 |
if len(polyline_data) > 0: |
| 111 |
polyline_data = polyline_data[0] |
| 112 |
polyline_points = polyline_data.polyline_points |
| 113 |
if len(polyline_points) > 0: |
| 114 |
last_point_data = polyline_points[len(polyline_points) - 1] |
| 115 |
else: |
| 116 |
last_point_data = None |
| 117 |
else: |
| 118 |
polyline_points = [] |
| 119 |
last_point_data = None |
| 120 |
|
| 121 |
if tool.Ifc.get(): |
| 122 |
default_container_elevation = tool.Ifc.get_object(tool.Root.get_default_container()).location.z |
| 123 |
else: |
| 124 |
default_container_elevation = 0 |
| 125 |
|
| 126 |
mouse_point = context.scene.BIMPolylineProperties.snap_mouse_point[0] |
| 127 |
|
| 128 |
if last_point_data: |
| 129 |
last_point = Vector((last_point_data.x, last_point_data.y, last_point_data.z)) |
| 130 |
else: |
| 131 |
last_point = Vector((0, 0, 0)) |
| 132 |
|
| 133 |
if tool_state.is_input_on: |
| 134 |
if tool_state.use_default_container: |
| 135 |
mouse_vector = Vector( |
| 136 |
(input_ui.get_number_value("X"), input_ui.get_number_value("Y"), default_container_elevation) |
| 137 |
) |
| 138 |
else: |
| 139 |
mouse_vector = Vector( |
| 140 |
(input_ui.get_number_value("X"), input_ui.get_number_value("Y"), input_ui.get_number_value("Z")) |
| 141 |
) |
| 142 |
else: |
| 143 |
if tool_state.use_default_container: |
| 144 |
mouse_vector = Vector((mouse_point.x, mouse_point.y, default_container_elevation)) |
| 145 |
else: |
| 146 |
mouse_vector = Vector((mouse_point.x, mouse_point.y, mouse_point.z)) |
| 147 |
|
| 148 |
second_to_last_point = None |
| 149 |
if len(polyline_points) > 1: |
| 150 |
second_to_last_point_data = polyline_points[len(polyline_points) - 2] |
| 151 |
second_to_last_point = Vector( |
| 152 |
(second_to_last_point_data.x, second_to_last_point_data.y, second_to_last_point_data.z) |
| 153 |
) |
| 154 |
else: |
| 155 |
# Creates a fake "second to last" point away from the first point but in the same x axis |
| 156 |
# this allows to calculate the angle relative to x axis when there is only one point |
| 157 |
second_to_last_point = Vector((last_point.x + 1000000000, last_point.y, last_point.z)) |
| 158 |
if tool_state.plane_method == "YZ": |
| 159 |
second_to_last_point = Vector((last_point.x, last_point.y + 1000000000, last_point.z)) |
| 160 |
second_to_last_point = tool.Polyline.use_transform_orientations(second_to_last_point) |
| 161 |
|
| 162 |
world_second_to_last_point = Vector((last_point.x + 1000000000, last_point.y, last_point.z)) |
| 163 |
if tool_state.plane_method == "YZ": |
| 164 |
world_second_to_last_point = Vector((last_point.x, last_point.y + 1000000000, last_point.z)) |
| 165 |
world_second_to_last_point = tool.Polyline.use_transform_orientations(world_second_to_last_point) |
| 166 |
|
| 167 |
distance = (mouse_vector - last_point).length |
| 168 |
if distance < 0: |
| 169 |
return |
| 170 |
if distance > 0: |
| 171 |
angle = tool.Cad.angle_3_vectors( |
| 172 |
second_to_last_point, last_point, mouse_vector, new_angle=None, degrees=True |
| 173 |
) |
| 174 |
|
| 175 |
# Round angle to the nearest 0.05 |
| 176 |
angle = round(angle / 0.05) * 0.05 |
| 177 |
|
| 178 |
orientation_angle = tool.Cad.angle_3_vectors( |
| 179 |
world_second_to_last_point, last_point, mouse_vector, new_angle=None, degrees=True |
| 180 |
) |
| 181 |
|
| 182 |
# Round angle to the nearest 0.05 |
| 183 |
orientation_angle = round(orientation_angle / 0.05) * 0.05 |
| 184 |
|
| 185 |
if distance == 0: |
| 186 |
angle = 0 |
| 187 |
orientation_angle = 0 |
| 188 |
if input_ui: |
| 189 |
if should_round: |
| 190 |
angle = 5 * round(angle / 5) |
| 191 |
factor = tool.Snap.get_increment_snap_value(context) |
| 192 |
distance = factor * round(distance / factor) |
| 193 |
input_ui.set_value("X", mouse_vector.x) |
| 194 |
input_ui.set_value("Y", mouse_vector.y) |
| 195 |
if input_ui.get_number_value("Z") is not None: |
| 196 |
input_ui.set_value("Z", mouse_vector.z) |
| 197 |
|
| 198 |
input_ui.set_value("D", distance) |
| 199 |
input_ui.set_value("A", angle) |
| 200 |
input_ui.set_value("WORLD_ANGLE", orientation_angle) |
| 201 |
return |
| 202 |
|
| 203 |
return |
| 204 |
|
| 205 |
@classmethod |
| 206 |
def calculate_area(cls, context: bpy.types.Context, input_ui: PolylineUI) -> Union[PolylineUI, None]: |
| 207 |
try: |
| 208 |
polyline_data = context.scene.BIMPolylineProperties.insertion_polyline[0] |
| 209 |
polyline_points = polyline_data.polyline_points |
| 210 |
except: |
| 211 |
return input_ui |
| 212 |
|
| 213 |
if len(polyline_points) < 3: |
| 214 |
return input_ui |
| 215 |
|
| 216 |
points = [] |
| 217 |
for data in polyline_points: |
| 218 |
points.append(Vector((data.x, data.y, data.z))) |
| 219 |
|
| 220 |
if points[0] == points[-1]: |
| 221 |
points = points[1:] |
| 222 |
|
| 223 |
# TODO move this to CAD |
| 224 |
# Calculate the normal vector of the plane formed by the first three vertices |
| 225 |
v1, v2, v3 = points[:3] |
| 226 |
normal = (v2 - v1).cross(v3 - v1).normalized() |
| 227 |
|
| 228 |
# Check if all points are coplanar |
| 229 |
is_coplanar = True |
| 230 |
tolerance = 1e-6 # Adjust this value as needed |
| 231 |
for v in points: |
| 232 |
if abs((v - v1).dot(normal)) > tolerance: |
| 233 |
is_coplanar = False |
| 234 |
|
| 235 |
if is_coplanar: |
| 236 |
area = 0 |
| 237 |
for i in range(len(points)): |
| 238 |
j = (i + 1) % len(points) |
| 239 |
area += points[i].cross(points[j]).dot(normal) |
| 240 |
|
| 241 |
area = abs(area) / 2 |
| 242 |
else: |
| 243 |
area = 0 |
| 244 |
|
| 245 |
if input_ui.get_text_value("AREA") is not None: |
| 246 |
input_ui.set_value("AREA", area) |
| 247 |
|
| 248 |
area = input_ui.get_number_value("AREA") |
| 249 |
if area: |
| 250 |
area = tool.Polyline.format_input_ui_units(area, is_area=True) |
| 251 |
polyline_data.area = area |
| 252 |
return |
| 253 |
|
| 254 |
@classmethod |
| 255 |
def calculate_x_y_and_z(cls, context: bpy.types.Context, input_ui: PolylineUI, tool_state: ToolState) -> None: |
| 256 |
polyline_data = context.scene.BIMPolylineProperties.insertion_polyline |
| 257 |
if len(polyline_data) > 0: |
| 258 |
polyline_data = context.scene.BIMPolylineProperties.insertion_polyline[0] |
| 259 |
polyline_points = polyline_data.polyline_points |
| 260 |
if len(polyline_points) > 0: |
| 261 |
last_point_data = polyline_points[len(polyline_points) - 1] |
| 262 |
last_point = Vector((last_point_data.x, last_point_data.y, last_point_data.z)) |
| 263 |
else: |
| 264 |
last_point = Vector((0, 0, 0)) |
| 265 |
else: |
| 266 |
polyline_points = [] |
| 267 |
last_point = Vector((0, 0, 0)) |
| 268 |
|
| 269 |
if tool.Ifc.get(): |
| 270 |
default_container_elevation = tool.Ifc.get_object(tool.Root.get_default_container()).location.z |
| 271 |
else: |
| 272 |
default_container_elevation = 0 |
| 273 |
|
| 274 |
snap_prop = context.scene.BIMPolylineProperties.snap_mouse_point[0] |
| 275 |
snap_vector = Vector((snap_prop.x, snap_prop.y, snap_prop.z)) |
| 276 |
|
| 277 |
if tool_state.is_input_on: |
| 278 |
if tool_state.use_default_container: |
| 279 |
mouse_vector = Vector( |
| 280 |
(input_ui.get_number_value("X"), input_ui.get_number_value("Y"), default_container_elevation) |
| 281 |
) |
| 282 |
else: |
| 283 |
mouse_vector = Vector( |
| 284 |
(input_ui.get_number_value("X"), input_ui.get_number_value("Y"), input_ui.get_number_value("Z")) |
| 285 |
) |
| 286 |
else: |
| 287 |
if tool_state.use_default_container: |
| 288 |
snap_vector = Vector((snap_prop.x, snap_prop.y, default_container_elevation)) |
| 289 |
else: |
| 290 |
snap_vector = Vector((snap_prop.x, snap_prop.y, snap_prop.z)) |
| 291 |
|
| 292 |
if len(polyline_points) > 1: |
| 293 |
second_to_last_point_data = polyline_points[len(polyline_points) - 2] |
| 294 |
second_to_last_point = Vector( |
| 295 |
(second_to_last_point_data.x, second_to_last_point_data.y, second_to_last_point_data.z) |
| 296 |
) |
| 297 |
else: |
| 298 |
# Creates a fake "second to last" point away from the first point but in the same x axis |
| 299 |
# this allows to calculate the angle relative to x axis when there is only one point |
| 300 |
second_to_last_point = Vector((last_point.x + 1000000000, last_point.y, last_point.z)) |
| 301 |
if tool_state.plane_method == "YZ": |
| 302 |
second_to_last_point = Vector((last_point.x, last_point.y + 1000000000, last_point.z)) |
| 303 |
second_to_last_point = tool.Polyline.use_transform_orientations(second_to_last_point) |
| 304 |
|
| 305 |
distance = input_ui.get_number_value("D") |
| 306 |
|
| 307 |
if distance < 0 or distance > 0: |
| 308 |
angle = radians(input_ui.get_number_value("A")) |
| 309 |
|
| 310 |
rot_vector = tool.Cad.angle_3_vectors(second_to_last_point, last_point, snap_vector, angle, degrees=True) |
| 311 |
|
| 312 |
# When the angle in 180 degrees it might create a rotation vector that is equal to |
| 313 |
# when the angle is 0 degrees, leading the insertion point to the opposite direction |
| 314 |
# This prevents the issue by ensuring the negative x direction |
| 315 |
if round(angle, 4) == round(math.pi, 4): |
| 316 |
rot_vector.x = -1.0 |
| 317 |
|
| 318 |
coords = rot_vector * distance + last_point |
| 319 |
|
| 320 |
x = coords[0] |
| 321 |
y = coords[1] |
| 322 |
z = coords[2] |
| 323 |
if input_ui: |
| 324 |
input_ui.set_value("X", x) |
| 325 |
input_ui.set_value("Y", y) |
| 326 |
if input_ui.get_number_value("Z") is not None: |
| 327 |
input_ui.set_value("Z", z) |
| 328 |
|
| 329 |
return |
| 330 |
|
| 331 |
input_ui.set_value("X", last_point.x) |
| 332 |
input_ui.set_value("Y", last_point.y) |
| 333 |
if input_ui.get_number_value("Z") is not None: |
| 334 |
input_ui.set_value("Z", last_point.z) |
| 335 |
|
| 336 |
return |
| 337 |
|
| 338 |
@classmethod |
| 339 |
def validate_input(cls, input_number: str, input_type: str) -> tuple[bool, str]: |
| 340 |
""" |
| 341 |
:return: Tuple with a boolean indicating if the input is valid |
| 342 |
and the final string output. |
| 343 |
Distance units converted to meters, angles input/output is in degrees. |
| 344 |
""" |
| 345 |
|
| 346 |
grammar_imperial = """ |
| 347 |
start: (FORMULA dim expr) | dim |
| 348 |
dim: imperial |
| 349 |
|
| 350 |
FORMULA: "=" |
| 351 |
|
| 352 |
imperial: (inches?) | (feet? "-"? inches?) |
| 353 |
feet: NUMBER? "-"? fraction? "'"? |
| 354 |
inches: NUMBER? "-"? fraction? "\\"" |
| 355 |
fraction: NUMBER "/" NUMBER |
| 356 |
|
| 357 |
expr: (ADD | SUB) dim | (MUL | DIV) NUMBER |
| 358 |
|
| 359 |
NUMBER: /-?\\d+(?:\\.\\d+)?/ |
| 360 |
ADD: "+" |
| 361 |
SUB: "-" |
| 362 |
MUL: "*" |
| 363 |
DIV: "/" |
| 364 |
|
| 365 |
%ignore " " |
| 366 |
""" |
| 367 |
|
| 368 |
grammar_metric = """ |
| 369 |
start: FORMULA? dim expr? |
| 370 |
dim: metric |
| 371 |
|
| 372 |
FORMULA: "=" |
| 373 |
|
| 374 |
metric: NUMBER "mm"? "cm"? "dm"? "m"? "°"? |
| 375 |
|
| 376 |
expr: (ADD | SUB | MUL | DIV) dim |
| 377 |
|
| 378 |
NUMBER: /-?\\d+(?:\\.\\d+)?/ |
| 379 |
ADD: "+" |
| 380 |
SUB: "-" |
| 381 |
MUL: "*" |
| 382 |
DIV: "/" |
| 383 |
|
| 384 |
%ignore " " |
| 385 |
""" |
| 386 |
|
| 387 |
class InputTransform(Transformer): |
| 388 |
def NUMBER(self, n): |
| 389 |
return float(n) |
| 390 |
|
| 391 |
def fraction(self, numbers): |
| 392 |
return numbers[0] / numbers[1] |
| 393 |
|
| 394 |
def inches(self, args): |
| 395 |
if len(args) > 1: |
| 396 |
result = args[0] + args[1] |
| 397 |
else: |
| 398 |
result = args[0] |
| 399 |
return result / 12 |
| 400 |
|
| 401 |
def feet(self, args): |
| 402 |
return args[0] |
| 403 |
|
| 404 |
def imperial(self, args): |
| 405 |
if len(args) > 1: |
| 406 |
if args[0] <= 0: |
| 407 |
result = args[0] - args[1] |
| 408 |
else: |
| 409 |
result = args[0] + args[1] |
| 410 |
else: |
| 411 |
result = args[0] or 0.0 |
| 412 |
return result |
| 413 |
|
| 414 |
def metric(self, args): |
| 415 |
return args[0] |
| 416 |
|
| 417 |
def dim(self, args): |
| 418 |
return args[0] |
| 419 |
|
| 420 |
def expr(self, args): |
| 421 |
op = args[0] |
| 422 |
value = float(args[1]) |
| 423 |
if op == "+": |
| 424 |
return lambda x: x + value |
| 425 |
elif op == "-": |
| 426 |
return lambda x: x - value |
| 427 |
elif op == "*": |
| 428 |
return lambda x: x * value |
| 429 |
elif op == "/": |
| 430 |
return lambda x: x / value |
| 431 |
|
| 432 |
def FORMULA(self, args): |
| 433 |
return args[0] |
| 434 |
|
| 435 |
def start(self, args): |
| 436 |
i = 0 |
| 437 |
if args[0] == "=": |
| 438 |
i += 1 |
| 439 |
else: |
| 440 |
if len(args) > 1: |
| 441 |
raise ValueError("Invalid input.") |
| 442 |
dimension = args[i] |
| 443 |
if len(args) > i + 1: |
| 444 |
expression = args[i + 1] |
| 445 |
return expression(dimension) * unit_scale |
| 446 |
else: |
| 447 |
return dimension * unit_scale |
| 448 |
|
| 449 |
try: |
| 450 |
if tool.Ifc.get(): |
| 451 |
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get()) |
| 452 |
else: |
| 453 |
unit_scale = tool.Blender.get_unit_scale() |
| 454 |
if bpy.context.scene.unit_settings.system == "IMPERIAL": |
| 455 |
parser = Lark(grammar_imperial) |
| 456 |
else: |
| 457 |
parser = Lark(grammar_metric) |
| 458 |
|
| 459 |
if input_type == "A": |
| 460 |
parser = Lark(grammar_metric) |
| 461 |
unit_scale = 1 |
| 462 |
|
| 463 |
parse_tree = parser.parse(input_number) |
| 464 |
|
| 465 |
transformer = InputTransform() |
| 466 |
result = transformer.transform(parse_tree) |
| 467 |
result = round(result, 4) |
| 468 |
return True, str(result) |
| 469 |
except: |
| 470 |
return False, "0" |
| 471 |
|
| 472 |
@classmethod |
| 473 |
def format_input_ui_units(cls, value: float, is_area: bool = False) -> str: |
| 474 |
if tool.Ifc.get(): |
| 475 |
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get()) |
| 476 |
else: |
| 477 |
unit_scale = tool.Blender.get_unit_scale() |
| 478 |
if bpy.context.scene.unit_settings.system == "IMPERIAL": |
| 479 |
dprops = tool.Drawing.get_document_props() |
| 480 |
precision = dprops.imperial_precision |
| 481 |
if is_area: |
| 482 |
unit_scale = 1 |
| 483 |
else: |
| 484 |
precision = None |
| 485 |
|
| 486 |
return format_distance( |
| 487 |
value / unit_scale, |
| 488 |
precision=precision, |
| 489 |
hide_units=False, |
| 490 |
isArea=is_area, |
| 491 |
suppress_zero_inches=True, |
| 492 |
in_unit_length=True, |
| 493 |
) |
| 494 |
|
| 495 |
@classmethod |
| 496 |
def insert_polyline_point(cls, input_ui: PolylineUI, tool_state: Optional[ToolState] = None) -> Union[str, None]: |
| 497 |
x = input_ui.get_number_value("X") |
| 498 |
y = input_ui.get_number_value("Y") |
| 499 |
if input_ui.get_number_value("Z") is not None: |
| 500 |
z = input_ui.get_number_value("Z") |
| 501 |
else: |
| 502 |
z = 0 |
| 503 |
d = input_ui.get_formatted_value("D") |
| 504 |
a = input_ui.get_formatted_value("A") |
| 505 |
|
| 506 |
snap_vertex = bpy.context.scene.BIMPolylineProperties.snap_mouse_point[0] |
| 507 |
if tool_state and tool_state.use_default_container: |
| 508 |
z = tool.Ifc.get_object(tool.Root.get_default_container()).location.z |
| 509 |
|
| 510 |
# Lock one dimension when in plane method |
| 511 |
if tool_state.plane_origin: |
| 512 |
if tool_state.plane_method == "XY": |
| 513 |
z = tool_state.plane_origin.z |
| 514 |
elif tool_state.plane_method == "XZ": |
| 515 |
y = tool_state.plane_origin.y |
| 516 |
elif tool_state.plane_method == "YZ": |
| 517 |
x = tool_state.plane_origin.x |
| 518 |
|
| 519 |
if x is None and y is None: |
| 520 |
x = snap_vertex.x |
| 521 |
y = snap_vertex.y |
| 522 |
z = snap_vertex.z |
| 523 |
|
| 524 |
polyline_data = bpy.context.scene.BIMPolylineProperties.insertion_polyline |
| 525 |
if not polyline_data: |
| 526 |
polyline_data = bpy.context.scene.BIMPolylineProperties.insertion_polyline.add() |
| 527 |
else: |
| 528 |
polyline_data = polyline_data[0] |
| 529 |
polyline_points = polyline_data.polyline_points |
| 530 |
if polyline_points: |
| 531 |
# Avoids creating two points at the same location |
| 532 |
for point in polyline_points[1:]: # The first can be repeated to form a wall loop |
| 533 |
if (x, y, z) == (point.x, point.y, point.z): |
| 534 |
return "Cannot create two points at the same location" |
| 535 |
# Avoids creating overlapping edges |
| 536 |
if len(polyline_points) > 1: |
| 537 |
v1 = Vector((x, y, z)) |
| 538 |
v2 = Vector((polyline_points[-1].x, polyline_points[-1].y, polyline_points[-1].z)) |
| 539 |
v3 = Vector((polyline_points[-2].x, polyline_points[-2].y, polyline_points[-2].z)) |
| 540 |
angle = tool.Cad.angle_3_vectors(v1, v2, v3, new_angle=None, degrees=True) |
| 541 |
if tool.Cad.is_x(angle, 0): |
| 542 |
return |
| 543 |
|
| 544 |
polyline_point = polyline_points.add() |
| 545 |
polyline_point.x = x |
| 546 |
polyline_point.y = y |
| 547 |
polyline_point.z = z |
| 548 |
|
| 549 |
polyline_point.dim = d |
| 550 |
polyline_point.angle = a |
| 551 |
polyline_point.position = Vector((x, y, z)) |
| 552 |
|
| 553 |
# Add total length |
| 554 |
total_length = 0 |
| 555 |
for i, point in enumerate(polyline_points): |
| 556 |
if i == 0: |
| 557 |
continue |
| 558 |
dim = float(tool.Polyline.validate_input(point.dim, "D")[1]) |
| 559 |
total_length += dim |
| 560 |
total_length = tool.Polyline.format_input_ui_units(total_length) |
| 561 |
polyline_data.total_length = total_length |
| 562 |
|
| 563 |
@classmethod |
| 564 |
def clear_polyline(cls) -> None: |
| 565 |
bpy.context.scene.BIMPolylineProperties.insertion_polyline.clear() |
| 566 |
|
| 567 |
@classmethod |
| 568 |
def remove_last_polyline_point(cls) -> None: |
| 569 |
polyline_data = bpy.context.scene.BIMPolylineProperties.insertion_polyline |
| 570 |
polyline_points = polyline_data[0].polyline_points if polyline_data else [] |
| 571 |
polyline_points.remove(len(polyline_points) - 1) |
| 572 |
|
| 573 |
@classmethod |
| 574 |
def move_polyline_to_measure(cls, context: bpy.types.Context, input_ui: PolylineUI) -> None: |
| 575 |
polyline_data = bpy.context.scene.BIMPolylineProperties.insertion_polyline |
| 576 |
polyline_points = polyline_data[0].polyline_points if polyline_data else [] |
| 577 |
measurement_data = bpy.context.scene.BIMPolylineProperties.measurement_polyline.add() |
| 578 |
measurement_type = bpy.context.scene.MeasureToolSettings.measurement_type |
| 579 |
measurement_data.measurement_type = measurement_type |
| 580 |
if measurement_type == "AREA" and len(polyline_points) < 3: |
| 581 |
return |
| 582 |
for point in polyline_points: |
| 583 |
measurement_point = measurement_data.polyline_points.add() |
| 584 |
measurement_point.x = point.x |
| 585 |
measurement_point.y = point.y |
| 586 |
measurement_point.z = point.z |
| 587 |
measurement_point.dim = point.dim |
| 588 |
measurement_point.angle = point.angle |
| 589 |
measurement_point.position = point.position |
| 590 |
|
| 591 |
measurement_data.total_length = polyline_data[0].total_length |
| 592 |
measurement_data.area = polyline_data[0].area |
| 593 |
|
| 594 |
@classmethod |
| 595 |
def use_transform_orientations(cls, value: Union[Vector, Matrix]) -> Union[Vector, Matrix]: |
| 596 |
custom_orientation = bpy.context.scene.transform_orientation_slots[0].custom_orientation |
| 597 |
if custom_orientation: |
| 598 |
custom_matrix = custom_orientation.matrix |
| 599 |
if isinstance(value, Vector): |
| 600 |
result = custom_matrix @ value |
| 601 |
else: |
| 602 |
result = custom_matrix.inverted() @ value |
| 603 |
return result |
| 604 |
return value |