forked from hlorus/CAD_Sketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
392 lines (308 loc) · 11.7 KB
/
ui.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import bpy
from bpy.types import Panel, Menu, UIList, Context, UILayout
from . import functions, class_defines
from .declarations import Menus, Operators, Panels, ConstraintOperators
from .stateful_operator.constants import Operators as StatefulOperators
class VIEW3D_UL_sketches(UIList):
def draw_item(
self, context, layout, data, item, icon, active_data, active_propname, index=0
):
if self.layout_type in {"DEFAULT", "COMPACT"}:
if item:
active = index == getattr(active_data, active_propname)
row = layout.row(align=True)
row.alignment = "LEFT"
row.prop(
item,
"visible",
icon_only=True,
icon=("HIDE_OFF" if item.visible else "HIDE_ON"),
emboss=False,
)
row.prop(item, "name", text="", emboss=False, icon_value=icon)
row = layout.row()
row.alignment = "RIGHT"
if item.solver_state != "OKAY":
row.operator(
Operators.ShowSolverState,
text="",
emboss=False,
icon_value=layout.enum_item_icon(
item, "solver_state", item.solver_state
),
).index = item.slvs_index
row.operator(
Operators.SetActiveSketch,
icon="OUTLINER_DATA_GP_LAYER",
text="",
emboss=False,
).index = item.slvs_index
if active:
row.operator(
Operators.DeleteEntity, text="", icon="X", emboss=False,
).index = item.slvs_index
else:
row.separator()
row.separator()
else:
layout.label(text="", translate=False, icon_value=icon)
elif self.layout_type in {"GRID"}:
layout.alignment = "CENTER"
layout.label(text="", icon_value=icon)
class VIEW3D_PT_sketcher_base(Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Sketcher"
class VIEW3D_PT_sketcher(VIEW3D_PT_sketcher_base):
bl_label = "Sketcher"
bl_idname = Panels.Sketcher
def draw(self, context: Context):
layout = self.layout
sketch_selector(context, layout, show_selector=False)
sketch = context.scene.sketcher.active_sketch
layout.use_property_split = True
layout.use_property_decorate = False
if sketch:
row = layout.row()
row.alignment = "CENTER"
row.scale_y = 1.2
if sketch.solver_state != "OKAY":
state = sketch.get_solver_state()
row.operator(
Operators.ShowSolverState,
text=state.name,
icon=state.icon,
emboss=False,
).index = sketch.slvs_index
else:
dof = sketch.dof
dof_ok = dof <= 0
dof_msg = (
"Fully defined sketch"
if dof_ok
else "Degrees of freedom: " + str(dof)
)
dof_icon = "CHECKMARK" if dof_ok else "ERROR"
row.label(text=dof_msg, icon=dof_icon)
layout.separator()
row = layout.row()
row.prop(sketch, "name")
layout.prop(sketch, "convert_type")
if sketch.convert_type != "NONE":
layout.prop(sketch, "fill_shape")
layout.operator(
Operators.DeleteEntity, text="Delete Sketch", icon="X",
).index = sketch.slvs_index
else:
layout.template_list(
"VIEW3D_UL_sketches",
"",
context.scene.sketcher.entities,
"sketches",
context.scene.sketcher,
"ui_active_sketch",
)
class VIEW3D_PT_sketcher_debug(VIEW3D_PT_sketcher_base):
bl_label = "Debug Settings"
bl_idname = Panels.SketcherDebugPanel
def draw(self, context: Context):
layout = self.layout
prefs = functions.get_prefs()
layout.operator(Operators.WriteSelectionTexture)
layout.operator(Operators.Solve)
layout.operator(Operators.Solve, text="Solve All").all = True
layout.operator(StatefulOperators.Test)
layout.prop(context.scene.sketcher, "show_origin")
layout.prop(prefs, "hide_inactive_constraints")
layout.prop(prefs, "all_entities_selectable")
layout.prop(prefs, "force_redraw")
layout.prop(context.scene.sketcher, "selectable_constraints")
layout.prop(prefs, "use_align_view")
@classmethod
def poll(cls, context: Context):
prefs = functions.get_prefs()
return prefs.show_debug_settings
class VIEW3D_PT_sketcher_add_constraints(VIEW3D_PT_sketcher_base):
bl_label = "Add Constraints"
bl_idname = Panels.SketcherAddContraint
bl_options = {"DEFAULT_CLOSED"}
def draw(self, context: Context):
layout = self.layout
layout.label(text="Constraints:")
col = layout.column(align=True)
for op in ConstraintOperators:
col.operator(op)
class VIEW3D_PT_sketcher_entities(VIEW3D_PT_sketcher_base):
bl_label = "Entities"
bl_idname = Panels.SketcherEntities
bl_options = {"DEFAULT_CLOSED"}
def draw(self, context: Context):
layout = self.layout
box = layout.box()
col = box.column(align=True)
col.scale_y = 0.8
sketch = context.scene.sketcher.active_sketch
for e in context.scene.sketcher.entities.all:
if not e.is_active(sketch):
continue
if isinstance(e, class_defines.SlvsSketch):
continue
row = col.row()
row.alert = e.selected
# Left part
sub = row.row(align=True)
sub.alignment = "LEFT"
# Select operator
props = sub.operator(
Operators.Select,
text="",
emboss=False,
icon=("RADIOBUT_ON" if e.selected else "RADIOBUT_OFF"),
)
props.index = e.slvs_index
props.highlight_hover = True
# Visibility toggle
sub.prop(
e,
"visible",
icon_only=True,
icon=("HIDE_OFF" if e.visible else "HIDE_ON"),
emboss=False,
)
sub.prop(e, "name", text="")
# Right part
sub = row.row()
sub.alignment = "RIGHT"
# Context menu
props = sub.operator(
Operators.ContextMenu,
text="",
icon="OUTLINER_DATA_GP_LAYER",
emboss=False,
)
props.highlight_hover = True
props.highlight_active = True
props.index = e.slvs_index
# Delete operator
props = sub.operator(
Operators.DeleteEntity, text="", icon="X", emboss=False,
)
props.index = e.slvs_index
props.highlight_hover = True
class VIEW3D_PT_sketcher_constraints(VIEW3D_PT_sketcher_base):
bl_label = "Constraints"
bl_idname = Panels.SketcherContraints
bl_options = {"DEFAULT_CLOSED"}
def draw(self, context: Context):
layout = self.layout
box = layout.box()
col = box.column(align=True)
col.scale_y = 0.8
layout.operator_enum(Operators.SetAllConstraintsVisibility, "visibility")
sketch = context.scene.sketcher.active_sketch
for c in context.scene.sketcher.constraints.all:
if not c.is_active(sketch):
continue
row = col.row()
# Left part
sub = row.row()
sub.alignment = "LEFT"
sub.prop(
c,
"visible",
icon_only=True,
icon=("HIDE_OFF" if c.visible else "HIDE_ON"),
emboss=False,
)
# Failed hint
sub.label(
text="", icon=("ERROR" if c.failed else "CHECKMARK"),
)
index = context.scene.sketcher.constraints.get_index(c)
# Context menu, shows constraint name
props = sub.operator(Operators.ContextMenu, text=str(c), emboss=False,)
props.type = c.type
props.index = index
props.highlight_hover = True
props.highlight_active = True
props.highlight_members = True
# Right part
sub = row.row()
sub.alignment = "RIGHT"
# Delete operator
props = sub.operator(
Operators.DeleteConstraint, text="", icon="X", emboss=False,
)
props.type = c.type
props.index = index
props.highlight_hover = True
props.highlight_members = True
class VIEW3D_MT_sketches(Menu):
bl_label = "Sketches"
bl_idname = Menus.Sketches
def draw(self, context: Context):
layout = self.layout
sse = context.scene.sketcher.entities
layout.operator(Operators.AddSketch).wait_for_input = True
if len(sse.sketches):
layout.separator()
for i, sk in enumerate(sse.sketches):
layout.operator(
Operators.SetActiveSketch, text=sk.name
).index = sk.slvs_index
def sketch_selector(
context: Context,
layout: UILayout,
is_header: bool = False,
show_selector: bool = True,
):
row = layout.row(align=True)
index = context.scene.sketcher.active_sketch_i
name = "Sketches"
scale_y = 1 if is_header else 1.8
if index != -1:
sketch = context.scene.sketcher.active_sketch
name = sketch.name
row.operator(
Operators.SetActiveSketch, text="Leave: " + name, icon="BACK", depress=True,
).index = -1
row.active = True
row.scale_y = scale_y
else:
row.scale_y = scale_y
# TODO: Don't show text when is_header
row.operator(Operators.AddSketch, icon="ADD").wait_for_input = True
if show_selector:
row.menu(VIEW3D_MT_sketches.bl_idname, text=name)
row.operator(Operators.Update, icon="FILE_REFRESH", text="")
def draw_object_context_menu(self, context: Context):
layout = self.layout
ob = context.active_object
row = layout.row()
props = row.operator(Operators.SetActiveSketch, text="Edit Sketch")
if ob and ob.sketch_index != -1:
row.active = True
props.index = ob.sketch_index
else:
row.active = False
layout.separator()
def draw_add_sketch_in_add_menu(self, context: Context):
self.layout.separator()
self.layout.operator_context = "INVOKE_DEFAULT"
self.layout.operator("view3d.slvs_add_sketch", text="Sketch")
classes = [
VIEW3D_UL_sketches,
VIEW3D_MT_sketches,
]
classes.extend(panel for panel in VIEW3D_PT_sketcher_base.__subclasses__())
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.VIEW3D_MT_object_context_menu.prepend(draw_object_context_menu)
bpy.types.VIEW3D_MT_add.append(draw_add_sketch_in_add_menu)
def unregister():
bpy.types.VIEW3D_MT_object_context_menu.remove(draw_object_context_menu)
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
bpy.types.VIEW3D_MT_object_context_menu.remove(draw_object_context_menu)
bpy.types.VIEW3D_MT_add.remove(draw_add_sketch_in_add_menu)