How to link an add-in application with web-server?
Not applicable
04-28-2021
10:31 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I try to make an add-in for Fusion 360 where I could get a parametric assembly from web-server. I made a try but there was an error which I have no idea how to make it solved.
User has to click on add-in and then parametric assembly and palette should appear at the same time (custom palette is for choosing user's parameters).
Here are the code and an error:
import adsk.core, adsk.fusion, adsk.cam, traceback
import urllib.request
import urllib.error
import urllib.parse
import pathlib
handlers = []
app = adsk.core.Application.get()
ui = app.userInterface
def run(context😞
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('Добавлена надстройка для соединительных муфт')
workSpace = ui.workspaces.itemById('FusionSolidEnvironment')
tbPanels = workSpace.toolbarPanels
tbPanel = tbPanels.itemById('NewPanel')
if tbPanel:
tbPanel.deleteMe()
tbPanel = tbPanels.add('NewPanel', 'БИБЛИОТЕКА МУФТ', 'SelectPanel', False)
cmdDef = ui.commandDefinitions.itemById('NewCommand')
if cmdDef:
cmdDef.deleteMe()
cmdDef = ui.commandDefinitions.addButtonDefinition('NewCommand', 'Создать муфту', 'Ввод параметров для соединительных муфт','.//resource')
tbPanel.controls.addCommand(cmdDef)
sampleCommandCreated = SampleCommandCreatedEventHandler()
cmdDef.commandCreated.add(sampleCommandCreated)
handlers.append(sampleCommandCreated)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class SampleCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler😞
def __init__(self😞
super().__init__()
def notify(self, args😞
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
cmd = eventArgs.command
# Connect to the execute event.
onExecute = SampleCommandExecuteHandler()
cmd.execute.add(onExecute)
handlers.append(onExecute)
class SampleCommandExecuteHandler(adsk.core.CommandEventHandler😞
def __init__(self😞
super().__init__()
def notify(self, args😞
try:
app = adsk.core.Application.get()
ui = app.userInterface
# Create and display the palette.
palette = ui.palettes.itemById('myExport')
if palette:
palette.deleteMe()
if not palette:
#make the [close] button invisible
palette = ui.palettes.add('myExport', 'БИБЛИОТЕКА МУФТ', 'index.html', True, True, True, 400, 200)
# Dock the palette to the right side of Fusion window.
palette.dockingState = adsk.core.PaletteDockingStates.PaletteDockStateRight
occ = importComponentFromURL(url)
else:
palette.isVisible = True
except:
ui.messageBox('Command executed failed: {}'.format(traceback.format_exc()))
def importComponentFromURL(url) -> adsk.fusion.Occurrence:
app = adsk.core.Application.get()
ui = app.userInterface
# doc check
doc = app.activeDocument
if not doc.dataFile:
ui.messageBox('Please save the document once.')
return adsk.fusion.Occurrence.cast(None)
folder = pathlib.Path(r'C:\Temp')
if not folder.exists():
folder.mkdir()
parsed = urllib.parse.urlparse(url)
filename = parsed.path.split('//')[-1]
dlFile = folder / filename
# suffix check
if dlFile.suffix != '.f3d':
ui.messageBox('F3D File Only')
return adsk.fusion.Occurrence.cast(None)
# delete download file
if dlFile.is_file():
dlFile.unlink()
# file download
try:
data = urllib.request.urlopen(url).read()
with open(str(dlFile), mode="wb") as f:
f.write(data)
except:
ui.messageBox(f'File not found in URL\n{url}')
return adsk.fusion.Occurrence.cast(None)
# import f3d
app.executeTextCommand(u'Fusion.ImportComponent {}'.format(dlFile))
app.executeTextCommand(u'NuCommands.CommitCmd')
# delete download file
if dlFile.is_file():
dlFile.unlink()
des = adsk.fusion.Design.cast(app.activeProduct)
comp = des.activeComponent
return comp.occurrences[-1]
def stop(context😞
ui = None
try:
ui.messageBox('Stop addin')
workSpace = ui.workspaces.itemById('FusionSolidEnvironment')
tbPanels = workSpace.toolbarPanels
tbPanel = tbPanels.itemById('NewPanel')
if tbPanel:
tbPanel.deleteMe()
cmdDef = ui.commandDefinitions.itemById('NewCommand')
if cmdDef:
cmdDef.deleteMe()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Link copied