How to import file into Fusion 360 from web-server using Import Manager?

How to import file into Fusion 360 from web-server using Import Manager?

Anonymous
Not applicable
1,916 Views
7 Replies
Message 1 of 8

How to import file into Fusion 360 from web-server using Import Manager?

Anonymous
Not applicable

Hello guys! 
We are working on our project in the university and we faced some problems during the development process. 
We are creating an add-in application that can import standard details and assemblies from web-server into Fusion 360 using Import Manager. Here's the code: (we want to import one 3D-model as test version), but the file is not selected. What might be the selected path? 


app = adsk.core.Application.get()
ui = app.userInterface
importManager = app.importManager

product = app.activeProduct
design = adsk.fusion.Design.cast(product)
rootComp = design.rootComponent


url = "http://195.133.144.86:4200//Half-coupling1.f3d"
archiveOptions = importManager.createFusionArchiveImportOptions(url)
importManager.importToTarget(archiveOptions)

0 Likes
Accepted solutions (2)
1,917 Views
7 Replies
Replies (7)
Message 2 of 8

BrianEkins
Mentor
Mentor

There is a problem with the API; either with the implementation or the documentation.  The documentation says you can provide a local filename or a URL but it looks like the implementation is only handling local files.  I don't know which one is wrong.  I would try and use just local files.  One way to do that is to have a local cache folder and if the file doesn't exist in the cache, then download it using something other than the Fusion API and then import it.  One simple method of downloading a file is to use curl.  It's included with the Mac OS and is small enough to package with the windows version of your program.  You can use the subprocess library or Python to run curl.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 8

kandennti
Mentor
Mentor
Accepted solution

Hi @Anonymous .

 

It looked interesting, so I gave it a try.

I was able to import the file at that URL as a component.

# Fusion360API Python script
import adsk.core, adsk.fusion, traceback

import urllib.request
import urllib.error
import urllib.parse
import pathlib

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        url = "http://195.133.144.86:4200//Half-coupling1.f3d"
        occ = importComponentFromURL(url)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# F3D File Only
def importComponentFromURL(url) -> adsk.fusion.Occurrence:
    app = adsk.core.Application.get()
    ui = app.userInterface

    # doc check
    doc :adsk.fusion.FusionDocument = app.activeDocument
    if not doc.dataFile:
        ui.messageBox('Please save the document once.')
        return adsk.fusion.Occurrence.cast(None)

    # get path
    folder = pathlib.Path(__file__).resolve().parent
    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(f'Fusion.ImportComponent {str(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]

When running the script, the active document needs to be saved once.

Message 4 of 8

Anonymous
Not applicable

Thank you so much for your answer, we do really appreciate that! 
But could you please tell me how useful a couplings design system would be? 

0 Likes
Message 5 of 8

Anonymous
Not applicable

Thank you so much for your answer! 

0 Likes
Message 6 of 8

Anonymous
Not applicable

There's a problem anytime I try to run your script. 
What might cause an error? 

0 Likes
Message 7 of 8

BrianEkins
Mentor
Mentor

I'm not sure what a "couplings design system" is so I'm probably not the right person to answer your question.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 8 of 8

kandennti
Mentor
Mentor
Accepted solution

@Anonymous .

 

In that sample, the f3d file is downloaded in the script's folder and imported.

Perhaps there is a problem with the path to the script folder. (PC login user name, etc.)

 

Please try to fix it here.

・・・
    # import f3d
    # app.executeTextCommand(f'Fusion.ImportComponent {str(dlFile)}')
    app.executeTextCommand(u'Fusion.ImportComponent {}'.format(dlFile))
    app.executeTextCommand(u'NuCommands.CommitCmd')
・・・

 

If that doesn't work, try modifying it like this to force a folder to be created for the download.

    # get path
    # folder = pathlib.Path(__file__).resolve().parent
    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