Import file from Project Files into current document

Import file from Project Files into current document

ebunn3
Advocate Advocate
973 Views
4 Replies
Message 1 of 5

Import file from Project Files into current document

ebunn3
Advocate
Advocate

Hi,

 

Is there a way to import an existing file from one of your Project Folders into the currently open document.  I see that there is one called "importToTarget2" that allows you to import something that is local to your computer.  Can this be setup to access your Project Files?

 

Thanks

 

Eric

0 Likes
Accepted solutions (1)
974 Views
4 Replies
Replies (4)
Message 2 of 5

kandennti
Mentor
Mentor

Hi @ebunn3 .

 

Do you mean importing the Data Panel documentation?

 

Once you have saved the document, make it active and run the script.
The documents in the same folder will be imported as occurrence.

 

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core


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

        # get active datafile
        actDataFile: adsk.core.DataFile = app.activeDocument.dataFile
        if not actDataFile:
            ui.messageBox('Save the document once.')
            return

        # get datafile ID
        actId = app.activeDocument.dataFile.id

        # get the unique data file
        dataFolder: adsk.core.DataFolder = app.activeDocument.dataFile.parentFolder
        targetDatafile: adsk.core.DataFile = None
        dataFiles = dataFolder.dataFiles.asArray()
        for df in dataFiles:
            if df.id != actId and df.fileExtension == 'f3d':
                targetDatafile = df
                break

        if not targetDatafile:
            ui.messageBox('Could not find the data file to import.')
            return

        # import datafile
        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-D2921ADB-3556-4B9E-98ED-2598EDDB852A
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent
        root.occurrences.addByInsert(
            targetDatafile,
            adsk.core.Matrix3D.create(),
            False  # True <- with a link
        )

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

 

0 Likes
Message 3 of 5

ebunn3
Advocate
Advocate

@kandennti 

 

I would like to browse for the file, select the one I want to insert and insert it.  I do see a function in there called addByInsert that accomplishes the actual insert into the open file.  Is there a way to browse for a file first?  Can one use ui.createFileDialog to browse for the file first?

 

I'm trying to mimic this:

 

ebunn3_2-1629549810440.png

 

 

 

 

 

Eric

0 Likes
Message 4 of 5

kandennti
Mentor
Mentor
Accepted solution

@ebunn3 .

 

Probably, UserInterface.createFileDialog can only be used for local files.

 

The only thing I can think of is to use TableCommandInput to display a list of files and let the user select one.

https://help.autodesk.com/view/fusion360/ENU/?guid#TableCommandInput 

 

This is a meaningless sample, but it uses TableCommandInput to display the file names in the root folder of the active data panel.

1.png

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

_app = None
_ui = None
_handlers = []


class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args):
        try:
            global _handlers
            cmd = adsk.core.Command.cast(args.command)
            inputs = cmd.commandInputs

            # event
            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            # get activeProject.rootFolder.dataFiles
            app: adsk.core.Application = adsk.core.Application.get()
            actDataFiles: adsk.core.DataFiles = app.data.activeProject.rootFolder.dataFiles.asArray()

            # Table
            rowCount = len(actDataFiles)

            tblStyle = adsk.core.TablePresentationStyles
            tbl = inputs.addTableCommandInput('table', 'Table', 0, '1:10')
            tbl.hasGrid = False
            tbl.tablePresentationStyle = tblStyle.itemBorderTablePresentationStyle
            if rowCount > 10:
                tbl.maximumVisibleRows = 10
            elif rowCount > 4:
                tbl.maximumVisibleRows = rowCount

            for idx, dataFile in enumerate(actDataFiles):
                # check box
                chk = inputs.addBoolValueInput(
                    f'check{idx}',
                    'Checkbox',
                    True,
                    '',
                    False)
                tbl.addCommandInput(chk, idx, 0)

                # datafile name
                dataName = inputs.addStringValueInput(
                    f'dataName{idx}',
                    'DataName',
                    dataFile.name)
                dataName.isReadOnly = True
                tbl.addCommandInput(dataName, idx, 1)

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


class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args):
        global _ui

        cmdDef: adsk.core.CommandDefinition = _ui.commandDefinitions.itemById(
            'Test')
        if cmdDef:
            cmdDef.deleteMe()

        adsk.terminate()


def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        cmdDef = _ui.commandDefinitions.itemById('Test')
        if cmdDef:
            cmdDef.deleteMe()

        cmdDef = _ui.commandDefinitions.addButtonDefinition(
            'Test', 'Test', 'Test')

        global _handlers
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        cmdDef.execute()

        adsk.autoTerminate(False)

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

I think it would be a good idea to create a mechanism so that when the OK button is pressed, only those files whose checkboxes are checked will be processed.

 

I don't understand it because I've never used it, but BrowserCommandInput might do the same thing.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-8B9041D5-75CC-4515-B4BB-4CF2CD5BC359#Browser... 

 

0 Likes
Message 5 of 5

ebunn3
Advocate
Advocate

@kandennti 

 

That is very cool.  This will work for me just fine.  You're the best!!!

 

Eric

0 Likes