forked from hlorus/CAD_Sketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_entity.py
121 lines (95 loc) · 3.94 KB
/
delete_entity.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import logging
from bpy.utils import register_classes_factory
from bpy.props import IntProperty, BoolProperty
from bpy.types import Operator, Context
from ..model.types import SlvsSketch
from ..functions import show_ui_message_popup, refresh
from ..declarations import Operators
from ..utilities.data_handling import (
get_constraint_local_indices,
get_entity_deps,
get_sketch_deps_indicies,
is_entity_dependency,
)
from ..utilities.highlighting import HighlightElement
from .utilities import activate_sketch
logger = logging.getLogger(__name__)
class View3D_OT_slvs_delete_entity(Operator, HighlightElement):
"""Delete Entity by index or based on the selection if index isn't provided"""
bl_idname = Operators.DeleteEntity
bl_label = "Delete Solvespace Entity"
bl_description = (
"Delete Entity by index or based on the selection if index isn't provided"
)
bl_options = {"UNDO"}
index: IntProperty(default=-1)
do_report: BoolProperty(
name="Report", description="Report entities that prevent deletion", default=True
)
@staticmethod
def main(context: Context, index: int, operator: Operator):
entities = context.scene.sketcher.entities
entity = entities.get(index)
if not entity:
return {"CANCELLED"}
if isinstance(entity, SlvsSketch):
if context.scene.sketcher.active_sketch_i != -1:
activate_sketch(context, -1, operator)
entity.remove_objects()
deps = get_sketch_deps_indicies(entity, context)
for i in reversed(deps):
operator.delete(entities.get(i), context)
elif is_entity_dependency(entity, context):
if operator.do_report:
deps = list(get_entity_deps(entity, context))
msg_deps = "\n".join([f" - {d}" for d in deps])
message = f"Unable to delete {entity.name}, other entities depend on it:\n{msg_deps}"
show_ui_message_popup(message=message, icon="ERROR")
operator.report(
{"WARNING"},
"Cannot delete {}, other entities depend on it.".format(
entity.name
),
)
return {"CANCELLED"}
operator.delete(entity, context)
@staticmethod
def delete(entity, context: Context):
entity.selected = False
# Delete constraints that depend on entity
constraints = context.scene.sketcher.constraints
for data_coll, indices in reversed(
get_constraint_local_indices(entity, context)
):
if not indices:
continue
for i in indices:
logger.debug("Delete: {}".format(data_coll[i]))
data_coll.remove(i)
logger.debug("Delete: {}".format(entity))
entities = context.scene.sketcher.entities
entities.remove(entity.slvs_index)
def execute(self, context: Context):
index = self.index
selected = context.scene.sketcher.entities.selected_entities
if index != -1:
# Entity is specified via property
self.main(context, index, self)
elif len(selected) == 1:
# Treat single selection same as specified entity
self.main(context, selected[0].slvs_index, self)
else:
# Batch deletion
indices = []
for e in selected:
indices.append(e.slvs_index)
indices.sort(reverse=True)
for i in indices:
e = context.scene.sketcher.entities.get(i)
# NOTE: this might be slow when a lot of entities are selected, improve!
if is_entity_dependency(e, context):
continue
self.delete(e, context)
refresh(context)
return {"FINISHED"}
register, unregister = register_classes_factory((View3D_OT_slvs_delete_entity,))