forked from hlorus/CAD_Sketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspacetools.py
341 lines (294 loc) · 10.5 KB
/
workspacetools.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from bpy.types import WorkSpaceTool
from . import operators, gizmos
import os
tool_prefix = "sketcher."
def get_addon_icon_path(icon_name):
return os.path.join(os.path.dirname(__file__), "icons", icon_name)
def tool_invoke_kmi(button, tool, operator):
return (
operators.View3D_OT_invoke_tool.bl_idname,
{"type": button, "value": "PRESS"},
{"properties": [("tool_name", tool), ("operator", operator)]},
)
tool_access = (
tool_invoke_kmi(
"P",
"sketcher.slvs_add_point2d",
operators.View3D_OT_slvs_add_point2d.bl_idname,
),
tool_invoke_kmi(
"L", "sketcher.slvs_add_line2d", operators.View3D_OT_slvs_add_line2d.bl_idname
),
tool_invoke_kmi(
"C",
"sketcher.slvs_add_circle2d",
operators.View3D_OT_slvs_add_circle2d.bl_idname,
),
tool_invoke_kmi(
"A", "sketcher.slvs_add_arc2d", operators.View3D_OT_slvs_add_arc2d.bl_idname
),
tool_invoke_kmi(
"R",
"sketcher.slvs_add_rectangle",
operators.View3D_OT_slvs_add_rectangle.bl_idname,
),
(
operators.View3D_OT_slvs_add_sketch.bl_idname,
{"type": "S", "value": "PRESS"},
{"properties": [("wait_for_input", True), ]}
)
)
def tool_numeric_invoke_km(operator):
km = []
for event in operators.numeric_events:
km.append(
(
operator,
{"type": event, "value": "PRESS"},
None,
)
)
return km
class VIEW3D_T_slvs_select(WorkSpaceTool):
bl_space_type = "VIEW_3D"
bl_context_mode = "OBJECT"
bl_idname = tool_prefix + "slvs_select"
bl_label = "Solvespace Select"
bl_description = "Select Solvespace Entities"
bl_icon = "ops.generic.select"
bl_widget = gizmos.VIEW3D_GGT_slvs_preselection.bl_idname
bl_keymap = (
(
operators.View3D_OT_slvs_select_all.bl_idname,
{"type": "ESC", "value": "PRESS"},
{"properties": [("deselect", True)]},
),
(
operators.View3D_OT_slvs_select_all.bl_idname,
{"type": "A", "value": "PRESS", "ctrl": True},
{"properties": [("deselect", False)]},
),
(
operators.View3D_OT_slvs_select.bl_idname,
{"type": "LEFTMOUSE", "value": "CLICK"},
None,
),
(
operators.View3D_OT_slvs_tweak.bl_idname,
{"type": "LEFTMOUSE", "value": "CLICK_DRAG"},
None,
),
(
operators.View3D_OT_slvs_tweak.bl_idname,
{"type": "LEFTMOUSE", "value": "PRESS", "ctrl": True},
None,
),
(
operators.View3D_OT_slvs_context_menu.bl_idname,
{"type": "RIGHTMOUSE", "value": "PRESS"},
None,
),
(
operators.View3D_OT_slvs_delete_entity.bl_idname,
{"type": "DEL", "value": "PRESS"},
None,
),
*tool_access,
)
def draw_settings(context, layout, tool):
props = tool.operator_properties(operators.View3D_OT_slvs_select.bl_idname)
tool_keymap = (
(
"wm.tool_set_by_id",
{"type": "ESC", "value": "PRESS"},
{"properties": [("name", VIEW3D_T_slvs_select.bl_idname)]},
),
(
"wm.tool_set_by_id",
{"type": "RIGHTMOUSE", "value": "PRESS"},
{"properties": [("name", VIEW3D_T_slvs_select.bl_idname)]},
),
)
def operator_access(operator):
return (
*tool_numeric_invoke_km(operator),
(
operator,
{"type": "LEFTMOUSE", "value": "PRESS"},
{"properties": [("wait_for_input", False)]},
),
)
class GenericStateTool():
@classmethod
def bl_description(cls, context, item, keymap):
op_name = cls.bl_operator if hasattr(cls, "bl_operator") else ""
desc = ""
if op_name:
import _bpy
desc = _bpy.ops.get_rna_type(op_name).description
for kmi in tool_access:
if kmi[0] == operators.View3D_OT_invoke_tool.bl_idname:
props = kmi[2]["properties"]
i = [prop[0] for prop in props].index("tool_name")
if props[i][1] == cls.bl_idname:
button = kmi[1]["type"]
desc += "\n\nInvoke Shortcut: " + button
return desc
class View3D_T_slvs_add_point3d(GenericStateTool, WorkSpaceTool):
bl_space_type = "VIEW_3D"
bl_context_mode = "OBJECT"
bl_idname = tool_prefix + "slvs_add_point3d"
bl_label = "Add 3D Point"
bl_operator = operators.View3D_OT_slvs_add_point3d.bl_idname
bl_icon = get_addon_icon_path("ops.bgs.add_point")
bl_widget = gizmos.VIEW3D_GGT_slvs_preselection.bl_idname
bl_keymap = (
*tool_keymap,
*tool_access,
*operator_access(operators.View3D_OT_slvs_add_point3d.bl_idname),
)
class View3D_T_slvs_add_point2d(GenericStateTool, WorkSpaceTool):
bl_space_type = "VIEW_3D"
bl_context_mode = "OBJECT"
bl_idname = tool_prefix + "slvs_add_point2d"
bl_label = "Add 2D Point"
bl_operator = operators.View3D_OT_slvs_add_point2d.bl_idname
bl_icon = get_addon_icon_path("ops.bgs.add_point")
bl_widget = gizmos.VIEW3D_GGT_slvs_preselection.bl_idname
bl_keymap = (
*tool_keymap,
*tool_access,
*operator_access(operators.View3D_OT_slvs_add_point2d.bl_idname),
)
class View3D_T_slvs_add_line3d(GenericStateTool, WorkSpaceTool):
bl_space_type = "VIEW_3D"
bl_context_mode = "OBJECT"
bl_idname = tool_prefix + "slvs_add_line3d"
bl_label = "Add 3D Line"
bl_operator = operators.View3D_OT_slvs_add_line3d.bl_idname
bl_icon = "ops.gpencil.primitive_line"
bl_widget = gizmos.VIEW3D_GGT_slvs_preselection.bl_idname
bl_keymap = (
*tool_keymap,
*tool_access,
*operator_access(operators.View3D_OT_slvs_add_line3d.bl_idname),
)
def draw_settings(context, layout, tool):
props = tool.operator_properties(operators.View3D_OT_slvs_add_line3d.bl_idname)
layout.prop(props, "continuose_draw")
class View3D_T_slvs_add_line2d(GenericStateTool, WorkSpaceTool):
bl_space_type = "VIEW_3D"
bl_context_mode = "OBJECT"
bl_idname = tool_prefix + "slvs_add_line2d"
bl_label = "Add 2D Line"
bl_operator = operators.View3D_OT_slvs_add_line2d.bl_idname
bl_icon = "ops.gpencil.primitive_line"
bl_widget = gizmos.VIEW3D_GGT_slvs_preselection.bl_idname
bl_keymap = (
*tool_keymap,
*tool_access,
*operator_access(operators.View3D_OT_slvs_add_line2d.bl_idname),
)
def draw_settings(context, layout, tool):
props = tool.operator_properties(operators.View3D_OT_slvs_add_line2d.bl_idname)
layout.prop(props, "continuose_draw")
class View3D_T_slvs_add_circle2d(GenericStateTool, WorkSpaceTool):
bl_space_type = "VIEW_3D"
bl_context_mode = "OBJECT"
bl_idname = tool_prefix + "slvs_add_circle2d"
bl_label = "Add 2D Circle"
bl_operator = operators.View3D_OT_slvs_add_circle2d.bl_idname
bl_icon = "ops.gpencil.primitive_circle"
bl_widget = gizmos.VIEW3D_GGT_slvs_preselection.bl_idname
bl_keymap = (
*tool_keymap,
*tool_access,
*operator_access(operators.View3D_OT_slvs_add_circle2d.bl_idname),
)
class View3D_T_slvs_add_arc2d(GenericStateTool, WorkSpaceTool):
bl_space_type = "VIEW_3D"
bl_context_mode = "OBJECT"
bl_idname = tool_prefix + "slvs_add_arc2d"
bl_label = "Add 2D Arc"
bl_operator = operators.View3D_OT_slvs_add_arc2d.bl_idname
bl_icon = "ops.gpencil.primitive_arc"
bl_widget = gizmos.VIEW3D_GGT_slvs_preselection.bl_idname
bl_keymap = (
*tool_keymap,
*tool_access,
*operator_access(operators.View3D_OT_slvs_add_arc2d.bl_idname),
)
class View3D_T_slvs_add_rectangle(GenericStateTool, WorkSpaceTool):
bl_space_type = "VIEW_3D"
bl_context_mode = "OBJECT"
bl_idname = tool_prefix + "slvs_add_rectangle"
bl_label = "Add Rectangle"
bl_operator = operators.View3D_OT_slvs_add_rectangle.bl_idname
bl_icon = "ops.gpencil.primitive_box"
bl_widget = gizmos.VIEW3D_GGT_slvs_preselection.bl_idname
bl_keymap = (
*tool_keymap,
*tool_access,
*operator_access(operators.View3D_OT_slvs_add_rectangle.bl_idname),
)
class View3D_T_slvs_add_workplane_face(GenericStateTool, WorkSpaceTool):
bl_space_type = "VIEW_3D"
bl_context_mode = "OBJECT"
bl_idname = tool_prefix + "slvs_add_workplane_face"
bl_label = "Add Workplane on mesh face"
bl_operator = operators.View3D_OT_slvs_add_workplane_face.bl_idname
bl_icon = "ops.mesh.primitive_grid_add_gizmo"
bl_widget = gizmos.VIEW3D_GGT_slvs_preselection.bl_idname
bl_keymap = (
*tool_keymap,
*tool_access,
*operator_access(operators.View3D_OT_slvs_add_workplane_face.bl_idname),
)
class View3D_T_slvs_add_workplane(GenericStateTool, WorkSpaceTool):
bl_space_type = "VIEW_3D"
bl_context_mode = "OBJECT"
bl_idname = tool_prefix + "slvs_add_workplane"
bl_label = "Add Workplane"
bl_operator = operators.View3D_OT_slvs_add_workplane.bl_idname
bl_icon = "ops.mesh.primitive_grid_add_gizmo"
bl_widget = gizmos.VIEW3D_GGT_slvs_preselection.bl_idname
bl_keymap = (
*tool_keymap,
*tool_access,
*operator_access(operators.View3D_OT_slvs_add_workplane.bl_idname),
)
tools = (
(VIEW3D_T_slvs_select, {"separator": True, "group": False}),
(View3D_T_slvs_add_point2d, {"separator": True, "group": True}),
(
View3D_T_slvs_add_point3d,
{
"after": {View3D_T_slvs_add_point2d.bl_idname},
},
),
(View3D_T_slvs_add_line2d, {"separator": False, "group": True}),
(
View3D_T_slvs_add_line3d,
{
"after": {View3D_T_slvs_add_line2d.bl_idname},
},
),
(View3D_T_slvs_add_circle2d, {"separator": False, "group": False}),
(View3D_T_slvs_add_arc2d, {"separator": False, "group": False}),
(View3D_T_slvs_add_rectangle, {"separator": False, "group": False}),
(View3D_T_slvs_add_workplane_face, {"separator": True, "group": True}),
(View3D_T_slvs_add_workplane, {"after": {View3D_T_slvs_add_workplane_face.bl_idname}}),
)
import bpy
def register():
if bpy.app.background:
return
from bpy.utils import register_tool
for tool in tools:
register_tool(tool[0], **tool[1])
def unregister():
if bpy.app.background:
return
from bpy.utils import unregister_tool
for tool in reversed(tools):
unregister_tool(tool[0])