<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to import file into Fusion 360 from web-server using Import Manager? in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10258821#M9036</link>
    <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looked interesting, so I gave it a try.&lt;/P&gt;
&lt;P&gt;I was able to import the file at that URL as a component.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;# 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) -&amp;gt; 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]&lt;/LI-CODE&gt;
&lt;P&gt;When running the script, the active document needs to be saved once.&lt;/P&gt;</description>
    <pubDate>Thu, 22 Apr 2021 08:13:18 GMT</pubDate>
    <dc:creator>kandennti</dc:creator>
    <dc:date>2021-04-22T08:13:18Z</dc:date>
    <item>
      <title>How to import file into Fusion 360 from web-server using Import Manager?</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10249989#M9034</link>
      <description>&lt;P&gt;Hello guys!&amp;nbsp;&lt;BR /&gt;We are working on our project in the university and we faced some problems during the development process.&amp;nbsp;&lt;BR /&gt;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?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN&gt;app = adsk.core.Application.get()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ui = app.userInterface&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;importManager = app.importManager&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;product = app.activeProduct&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;design = adsk.fusion.Design.cast(product)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rootComp = design.rootComponent&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;url = "&lt;A href="http://195.133.144.86:4200//Half-coupling1.f3d" target="_blank"&gt;http://195.133.144.86:4200//Half-coupling1.f3d&lt;/A&gt;"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;archiveOptions = importManager.createFusionArchiveImportOptions(url)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;importManager.importToTarget(archiveOptions)&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Apr 2021 10:46:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10249989#M9034</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-04-19T10:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to import file into Fusion 360 from web-server using Import Manager?</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10251776#M9035</link>
      <description>&lt;P&gt;There is a problem with the API; either with the implementation or the documentation.&amp;nbsp; The documentation says you can provide a local filename or a URL but it looks like the implementation is only handling local files.&amp;nbsp; I don't know which one is wrong.&amp;nbsp; I would try and use just local files.&amp;nbsp; 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.&amp;nbsp; One simple method of downloading a file is to use curl.&amp;nbsp; It's included with the Mac OS and is small enough to package with the windows version of your program.&amp;nbsp; You can use the subprocess library or Python to run curl.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Apr 2021 20:36:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10251776#M9035</guid>
      <dc:creator>BrianEkins</dc:creator>
      <dc:date>2021-04-19T20:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to import file into Fusion 360 from web-server using Import Manager?</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10258821#M9036</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looked interesting, so I gave it a try.&lt;/P&gt;
&lt;P&gt;I was able to import the file at that URL as a component.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;# 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) -&amp;gt; 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]&lt;/LI-CODE&gt;
&lt;P&gt;When running the script, the active document needs to be saved once.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 08:13:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10258821#M9036</guid>
      <dc:creator>kandennti</dc:creator>
      <dc:date>2021-04-22T08:13:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to import file into Fusion 360 from web-server using Import Manager?</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10272278#M9037</link>
      <description>&lt;P&gt;Thank you so much for your answer, we do really appreciate that!&amp;nbsp;&lt;BR /&gt;But could you please tell me how useful a couplings design system would be?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 15:22:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10272278#M9037</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-04-27T15:22:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to import file into Fusion 360 from web-server using Import Manager?</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10272283#M9038</link>
      <description>&lt;P&gt;Thank you so much for your answer!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 15:23:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10272283#M9038</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-04-27T15:23:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to import file into Fusion 360 from web-server using Import Manager?</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10272786#M9039</link>
      <description>&lt;P&gt;There's a problem anytime I try to run your script.&amp;nbsp;&lt;BR /&gt;What might cause an error?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 18:31:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10272786#M9039</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-04-27T18:31:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to import file into Fusion 360 from web-server using Import Manager?</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10273199#M9040</link>
      <description>&lt;P&gt;I'm not sure what a "couplings design system" is so I'm probably not the right person to answer your question.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 20:53:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10273199#M9040</guid>
      <dc:creator>BrianEkins</dc:creator>
      <dc:date>2021-04-27T20:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to import file into Fusion 360 from web-server using Import Manager?</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10273525#M9041</link>
      <description>&lt;P&gt;@Anonymous&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In that sample, the f3d file is downloaded in the script's folder and imported.&lt;/P&gt;
&lt;P&gt;Perhaps there is a problem with the path to the script folder. (PC login user name, etc.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please try to fix it here.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;・・・
    # import f3d
    # app.executeTextCommand(f'Fusion.ImportComponent {str(dlFile)}')
    app.executeTextCommand(u'Fusion.ImportComponent {}'.format(dlFile))
    app.executeTextCommand(u'NuCommands.CommitCmd')
・・・&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that doesn't work, try modifying it like this to force a folder to be created for the download.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;    # 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&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 27 Apr 2021 23:46:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/how-to-import-file-into-fusion-360-from-web-server-using-import/m-p/10273525#M9041</guid>
      <dc:creator>kandennti</dc:creator>
      <dc:date>2021-04-27T23:46:01Z</dc:date>
    </item>
  </channel>
</rss>

