-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNoComponentWarn.py
239 lines (207 loc) · 9.53 KB
/
NoComponentWarn.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
#Author-Thomas Axelsson
#Description-Warn when features are created outside components
# This file is part of NoComponentWarn, a Fusion 360 add-in that
# warns when features are created outside components.
#
# Copyright (c) 2020 Thomas Axelsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import adsk.core, adsk.fusion, adsk.cam, traceback
import os
import platform
import re
import subprocess
import sys
import time
NAME = 'NoComponentWarn'
platform_ = platform.system()
# Must import lib as unique name, to avoid collision with other versions
# loaded by other add-ins
from .thomasa88lib import events
from .thomasa88lib import manifest
from .thomasa88lib import error
if platform_ == 'Windows':
from .thomasa88lib.win import msgbox
# Force modules to be fresh during development
import importlib
importlib.reload(thomasa88lib.events)
importlib.reload(thomasa88lib.manifest)
importlib.reload(thomasa88lib.error)
if platform_ == 'Windows':
importlib.reload(thomasa88lib.win.msgbox)
COMPONENT_WARN_ID = 'thomasa88_componentWarn'
# (command ID, is prefix)
CREATION_COMMANDS_ = [
('SketchCreate', False),
('Primitive', True),
# 'WorkOffline' matches 'Work'-only prefix
('WorkPlane', True),
('WorkAxis', True),
('WorkPoint', True),
('Extrude', False),
('Revolve', False),
('Sweep', False),
('SolidLoft', False),
('FusionRibCommand', False),
('FusionWebCommand', False),
('EmbossCmd', False),
('FusionHoleCommand', False),
('FusionThreadCommand', False),
('PatternRectangular', False),
('PatternCircular', False),
('PatternOnPath', False),
('MirrorCommand', False),
('FusionSurfaceThickenCommand', False),
('SurfaceSculpt', False), # Boundary Fill
('SurfaceExtrude', False),
('SurfaceRevolve', False),
('SurfaceSweep', False),
('SurfaceLoft', False),
('FusionSurfacePatchCommand', False),
('FusionSurfaceRuledCommand', False),
('FusionSurfaceOffsetCommand', False),
]
app_ = None
ui_ = None
error_catcher_ = thomasa88lib.error.ErrorCatcher(msgbox_in_debug=False)
events_manager_ = thomasa88lib.events.EventsManager(error_catcher_)
manifest_ = thomasa88lib.manifest.read()
disabled_for_documents_ = []
cmd_starting_handler_info_ = None
# Hold-off timer. Sketch create button triggers 2 starting SketchCreate in a row.
last_continue_time_ = 0.0
def command_handler(args: adsk.core.ApplicationCommandEventArgs):
global last_continue_time_
#print("COMMAND", args.commandId, args.terminationReason, app_.activeEditObject.name, app_.activeEditObject.classType())
#args.isCanceled = True
# Checking disabled here. We would improve performance by checking in documentActivated, but
# that event does not always fire (2020-08-02)
# Bug: https://forums.autodesk.com/t5/fusion-360-api-and-scripts/api-bug-application-documentactivated-event-do-not-raise/m-p/9020750
if app_.activeDocument in disabled_for_documents_:
return
if time.time() - last_continue_time_ < 3:
# Sketch create button hold-off
return
for cmd, prefix in CREATION_COMMANDS_:
if ((prefix and args.commandId.startswith(cmd)) or
args.commandId == cmd):
break
else:
return
error_msg = None
if app_.activeEditObject == app_.activeProduct.rootComponent:
error_msg = 'You are creating a feature without any component.'
elif isinstance(app_.activeEditObject, adsk.fusion.Component):
for sel in ui_.activeSelections:
if hasattr(sel, 'entity') and getattr(sel.entity, 'assemblyContext', None) is not None:
if sel.entity.assemblyContext.component != app_.activeEditObject:
error_msg = 'You are creating a feature referencing another component.'
break
if error_msg:
# We must use "created" or "starting" to catch Box and the other solids.
# Also, starting must be used for setting isCanceled.
# If we execute our own command in starting or created, we abort the command the user
# started. We could in theory re-execute that command, but we might lose
# data(?). Going for a MessageBox together with isCanceled instead.
RET_CANCEL = 1
RET_DISABLE = 2
RET_CONTINUE = 3
dialog_title = f"No Component Warning (v {manifest_['version']})"
if platform_ == 'Windows':
# Using a button combo with "Cancel", as that will map to Esc
ret = thomasa88lib.win.msgbox.custom_msgbox(error_msg,
dialog_title,
(thomasa88lib.win.msgbox.MB_ICONWARNING |
thomasa88lib.win.msgbox.MB_CANCELTRYCONTINUE |
thomasa88lib.win.msgbox.MB_DEFBUTTON2),
{ thomasa88lib.win.msgbox.IDCANCEL: 'Cancel', # No localization
thomasa88lib.win.msgbox.IDTRYAGAIN: '&Continue',
thomasa88lib.win.msgbox.IDCONTINUE: "&Stop warning" })
# Not checking for error return. Just let user continue in that case.
# Note the mapping of the labels in custom_msgbox()!
if ret == thomasa88lib.win.msgbox.IDCANCEL:
ret = RET_CANCEL
elif ret == thomasa88lib.win.msgbox.IDCONTINUE:
ret = RET_DISABLE
else:
ret = RET_CONTINUE
else:
TEXT_CANCEL = 'Cancel'
TEXT_CONTINUE = 'Continue'
TEXT_DISABLE = 'Stop warning'
# Map buttons to map Windows version
# It looks like Mac has no equivalen to Windows' "&" hotkey markers.
result = subprocess.run(['osascript', '-e',
'button returned of ('
f'display dialog "{error_msg}" with title "{dialog_title}" '
f'buttons {{"{TEXT_CANCEL}", "{TEXT_CONTINUE}", "{TEXT_DISABLE}"}} '
'default button 2 cancel button 1 with icon caution'
')'],
encoding='utf-8', capture_output=True)
reply = result.stdout.strip()
# "Esc" key results in returnCode == 1. It seems that the clicking the button gives the same result.
if result.returncode == 1 or reply == TEXT_CANCEL:
ret = RET_CANCEL
elif reply == TEXT_DISABLE:
ret = RET_DISABLE
else:
ret = RET_CONTINUE
if ret == RET_CANCEL:
# Cancel
args.isCanceled = True
elif ret == RET_DISABLE:
# Stop warning
# Document.dataFile only works when the document is saved (then we can use "id")
# Document.name always works but is the document name - include the vX version indicator.
disabled_for_documents_.append(app_.activeDocument)
else:
# Continue
last_continue_time_ = time.time()
def workspace_activated_handler(args: adsk.core.WorkspaceEventArgs):
if args.workspace.id == 'FusionSolidEnvironment':
enable()
def workspace_pre_deactivate_handler(args: adsk.core.WorkspaceEventArgs):
disable()
def enable():
global cmd_starting_handler_info_
if not cmd_starting_handler_info_:
cmd_starting_handler_info_ = events_manager_.add_handler(ui_.commandStarting,
callback=command_handler)
def disable():
global cmd_starting_handler_info_
if cmd_starting_handler_info_:
cmd_starting_handler_info_ = events_manager_.remove_handler(cmd_starting_handler_info_)
def run(context):
global app_
global ui_
with error_catcher_:
app_ = adsk.core.Application.get()
ui_ = app_.userInterface
events_manager_.add_handler(ui_.workspaceActivated,
callback=workspace_activated_handler)
events_manager_.add_handler(ui_.workspacePreDeactivate,
callback=workspace_pre_deactivate_handler)
if app_.isStartupComplete:
# Cannot check the workspaces if Fusion is starting up.
# Rely on the workspace event in that case.
if ui_.activeWorkspace.id == 'FusionSolidEnvironment':
enable()
def stop(context):
with error_catcher_:
events_manager_.clean_up()