forked from hlorus/CAD_Sketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrim.py
91 lines (69 loc) · 2.62 KB
/
trim.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
91
import logging
from bpy.types import Operator, Context
from ..model.categories import SEGMENT
from ..declarations import Operators
from ..stateful_operator.utilities.register import register_stateops_factory
from ..stateful_operator.state import state_from_args
from ..utilities.trimming import TrimSegment
from .base_2d import Operator2d
from ..utilities.view import refresh, get_pos_2d
logger = logging.getLogger(__name__)
class View3D_OT_slvs_trim(Operator, Operator2d):
"""Trim segment to its closest intersections"""
bl_idname = Operators.Trim
bl_label = "Trim Segment"
bl_options = {"REGISTER", "UNDO"}
trim_state1_doc = ("Segment", "Segment to trim.")
states = (
state_from_args(
trim_state1_doc[0],
description=trim_state1_doc[1],
pointer="segment",
types=SEGMENT,
pick_element="pick_element_coords",
use_create=False,
# interactive=True
),
)
# TODO: Disable execution based on selection
# NOTE: That does not work if run with select -> action
def pick_element_coords(self, context, coords):
data = self.state_data
data["mouse_pos"] = get_pos_2d(context, self.sketch.wp, coords)
return super().pick_element(context, coords)
def main(self, context: Context):
return True
def fini(self, context: Context, succeede: bool):
if not succeede:
return False
sketch = context.scene.sketcher.active_sketch
segment = self.segment
mouse_pos = self._state_data[0].get("mouse_pos")
if mouse_pos is None:
return False
trim = TrimSegment(segment, mouse_pos)
# Find intersections
for e in sketch.sketch_entities(context):
if not e.is_segment():
continue
if e == segment:
continue
for co in segment.intersect(e):
# print("intersect", co)
trim.add(e, co)
# Find points that are connected to the segment through a coincident constraint
for c in (
*context.scene.sketcher.constraints.coincident,
*context.scene.sketcher.constraints.midpoint,
):
ents = c.entities()
if segment not in ents:
continue
p = ents[0]
trim.add(c, p.co)
# TODO: Get rid of the coincident constraint as it will be a shared connection point
if not trim.check():
return
trim.replace(context)
refresh(context)
register, unregister = register_stateops_factory((View3D_OT_slvs_trim,))