-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChangeComparer_Python.py
More file actions
211 lines (179 loc) · 8.21 KB
/
Copy pathChangeComparer_Python.py
File metadata and controls
211 lines (179 loc) · 8.21 KB
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
#Author - Adam Nagy
#Description - An AddIn that enables the user to keep a snapshot of
# the current state of the model then compare it to a later state
# of the model
import adsk.core, adsk.fusion, traceback, os
# WebPageDirectory path, where we'll store the images and the html page
wpd = os.path.dirname(os.path.realpath(__file__)) + '/resources/webpage'
snapshotCamera = None
def getCamera():
app = adsk.core.Application.get()
vp = app.activeViewport
return vp.camera
def restoreCamera(camera):
app = adsk.core.Application.get()
vp = app.activeViewport
vp.camera = camera
# save the current view of the model
def saveImage(fileName):
app = adsk.core.Application.get()
vp = app.activeViewport
ret = app.activeViewport.saveAsImageFile(fileName, vp.width, vp.height)
ui = app.userInterface
global snapshotCamera
snapshotCamera = vp.camera
if not ret:
ui.messageBox('Could not save image')
# open the html page that will show the two recorded images
def showWebPage():
# show our html file with the content of the two
# image files and you can switch between them
import webbrowser
webbrowser.open_new('file://' + wpd + '/comparer.html')
# global set of event handlers to keep them referenced for the duration of the command
handlers = []
isSavingSnapshot = True
commandId = 'ComparerCmd'
workspaceToUse = 'FusionSolidEnvironment'
panelToUse = 'InspectPanel'
# some utility functions
def commandDefinitionById(id):
app = adsk.core.Application.get()
ui = app.userInterface
if not id:
ui.messageBox('commandDefinition id is not specified')
return None
commandDefinitions_ = ui.commandDefinitions
commandDefinition_ = commandDefinitions_.itemById(id)
return commandDefinition_
def commandControlByIdForPanel(id):
global workspaceToUse
global panelToUse
app = adsk.core.Application.get()
ui = app.userInterface
if not id:
ui.messageBox('commandControl id is not specified')
return None
workspaces_ = ui.workspaces
modelingWorkspace_ = workspaces_.itemById(workspaceToUse)
toolbarPanels_ = modelingWorkspace_.toolbarPanels
toolbarPanel_ = toolbarPanels_.itemById(panelToUse)
toolbarControls_ = toolbarPanel_.controls
toolbarControl_ = toolbarControls_.itemById(id)
return toolbarControl_
def destroyObject(uiObj, objToDelete):
if uiObj and objToDelete:
if objToDelete.isValid:
objToDelete.deleteMe()
else:
uiObj.messageBox('objToDelete is not a valid object')
def addCommandToPanel(panel, commandId, commandName, commandDescription, commandResources, onCommandCreated):
app = adsk.core.Application.get()
ui = app.userInterface
commandDefinitions_ = ui.commandDefinitions
toolbarControlsPanel_ = panel.controls
toolbarControlPanel_ = toolbarControlsPanel_.itemById(commandId)
if not toolbarControlPanel_:
commandDefinitionPanel_ = commandDefinitions_.itemById(commandId)
if not commandDefinitionPanel_:
commandDefinitionPanel_ = commandDefinitions_.addButtonDefinition(commandId, commandName, commandName, commandResources)
commandDefinitionPanel_.tooltipDescription = commandDescription
commandDefinitionPanel_.commandCreated.add(onCommandCreated)
# keep the handler referenced beyond this function
handlers.append(onCommandCreated)
toolbarControlPanel_ = toolbarControlsPanel_.addCommand(commandDefinitionPanel_, commandId)
toolbarControlPanel_.isVisible = True
def getControlAndDefinition(commandId, objects):
commandControl_ = commandControlByIdForPanel(commandId)
if commandControl_:
objects.append(commandControl_)
commandDefinition_ = commandDefinitionById(commandId)
if commandDefinition_:
objects.append(commandDefinition_)
# the main function that is called when our addin is loaded
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
# command properties
saveCommandName = 'Save Snapshot For Comparison'
saveCommandDescription = 'Saves a snapshot of the current model state so that ' \
'we can compare to it later'
saveCommandResources = './resources/comparer'
viewCommandName = 'Compare To Snapshot'
viewCommandDescription = 'Saves a snapshot of the current model state and opens ' \
'a web page where you can compare it to the previously saved state'
# our command
class CommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
global isSavingSnapshot
try:
# are we saving a snapshot ...
if isSavingSnapshot:
# save view snapshot as previous.png
# this will also store the current camera
# in snapshotCamera global variable
saveImage(wpd + '/previous.png')
# update the command text
saveCommand = commandDefinitionById(commandId)
saveCommand.controlDefinition.name = viewCommandName
saveCommand.tooltip = viewCommandName
saveCommand.tooltipDescription = viewCommandDescription
isSavingSnapshot = False
# ... or showing the difference to the previously saved snapshot
else:
camera = getCamera()
restoreCamera(snapshotCamera)
saveImage(wpd + '/current.png')
restoreCamera(camera)
# now open the webpage that shows our two images
showWebPage()
# update the command text
saveCommand = commandDefinitionById(commandId)
saveCommand.controlDefinition.name = saveCommandName
saveCommand.tooltip = saveCommandName
saveCommand.tooltipDescription = saveCommandDescription
isSavingSnapshot = True
except:
if ui:
ui.messageBox('command executed failed:\n{}'.format(traceback.format_exc()))
class CommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
cmd = args.command
onExecute = CommandExecuteHandler()
cmd.execute.add(onExecute)
# keep the handler referenced beyond this function
handlers.append(onExecute)
except:
if ui:
ui.messageBox('Panel command created failed:\n{}'.format(traceback.format_exc()))
# add our command on "Inspect" panel in "Modeling" workspace
global workspaceToUse
global panelToUse
workspaces_ = ui.workspaces
modelingWorkspace_ = workspaces_.itemById(workspaceToUse)
toolbarPanels_ = modelingWorkspace_.toolbarPanels
# add the new command under the fifth panel / "Inspect"
toolbarPanel_ = toolbarPanels_.itemById(panelToUse)
addCommandToPanel(toolbarPanel_, commandId, saveCommandName, saveCommandDescription, saveCommandResources, CommandCreatedEventHandler())
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def stop(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
objArray = []
getControlAndDefinition(commandId, objArray)
for obj in objArray:
destroyObject(ui, obj)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))