forked from hlorus/CAD_Sketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_line_2d.py
90 lines (69 loc) · 2.85 KB
/
add_line_2d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import logging
from bpy.types import Operator, Context
from bpy.props import BoolProperty
from mathutils import Vector
from ..utilities.constants import HALF_TURN, QUARTER_TURN
from ..declarations import Operators
from ..stateful_operator.utilities.register import register_stateops_factory
from ..stateful_operator.state import state_from_args
from ..solver import solve_system
from .base_2d import Operator2d
from .constants import types_point_2d
from .utilities import ignore_hover
logger = logging.getLogger(__name__)
class View3D_OT_slvs_add_line2d(Operator, Operator2d):
"""Add a line to the active sketch"""
bl_idname = Operators.AddLine2D
bl_label = "Add Solvespace 2D Line"
bl_options = {"REGISTER", "UNDO"}
l2d_state1_doc = ("Startpoint", "Pick or place line's starting Point.")
l2d_state2_doc = ("Endpoint", "Pick or place line's ending Point.")
continuous_draw: BoolProperty(name="Continuous Draw", default=True)
states = (
state_from_args(
l2d_state1_doc[0],
description=l2d_state1_doc[1],
pointer="p1",
types=types_point_2d,
),
state_from_args(
l2d_state2_doc[0],
description=l2d_state2_doc[1],
pointer="p2",
types=types_point_2d,
interactive=True,
),
)
def main(self, context: Context):
wp = self.sketch.wp
p1, p2 = self.get_point(context, 0), self.get_point(context, 1)
self.target = context.scene.sketcher.entities.add_line_2d(p1, p2, self.sketch)
if context.scene.sketcher.use_construction:
self.target.construction = True
# auto vertical/horizontal constraint
constraints = context.scene.sketcher.constraints
vec_dir = self.target.direction_vec()
if vec_dir.length:
angle = vec_dir.angle(Vector((1, 0)))
threshold = 0.1
if angle < threshold or angle > HALF_TURN - threshold:
constraints.add_horizontal(self.target, sketch=self.sketch)
elif (QUARTER_TURN - threshold) < angle < (QUARTER_TURN + threshold):
constraints.add_vertical(self.target, sketch=self.sketch)
ignore_hover(self.target)
return True
def continue_draw(self):
last_state = self._state_data[1]
if last_state["is_existing_entity"]:
return False
# also not when last state has coincident constraint
if last_state.get("coincident"):
return False
return True
def fini(self, context: Context, succeede: bool):
if hasattr(self, "target"):
logger.debug("Add: {}".format(self.target))
if succeede:
if self.has_coincident:
solve_system(context, sketch=self.sketch)
register, unregister = register_stateops_factory((View3D_OT_slvs_add_line2d,))