Getting the path from the inported file.

Getting the path from the inported file.

pedro_babo
Advocate Advocate
632 Views
6 Replies
Message 1 of 7

Getting the path from the inported file.

pedro_babo
Advocate
Advocate

I'm creating a script to Export the visible faces to the same path that I've imported the first file.

 

I’m using this code.

"

def get_imported_file_path():

    # Get the active design

    app = adsk.core.Application.get()

    design = adsk.fusion.Design.cast(app.activeProduct)

 

    # Get the root component

    root_comp = design.rootComponent

 

    # Get the first occurrence in the root component

    #first_occurrence = root_comp.occurrences.item(0)

 

    # Get the import component of the first occurrence

    #import_comp = adsk.fusion.ImportComponent.cast(first_occurrence.component)

 

    # Get the full path of the imported file

    #import_path = import_comp.importedFileName

 

    # Notify the user of the imported file path

    return root_comp.name

"

Has I've not saved any model I get "not saved".

 

The objective is exporting a SAT file back to the same location on the PC of the original file.

Is there any other Way?

0 Likes
633 Views
6 Replies
Replies (6)
Message 2 of 7

kandennti
Mentor
Mentor

Hi @pedro_babo .

 

There is no object called "adsk.fusion.ImportComponent."
What kind of comport are you referring to when you say "import component"?
Do you mean a component with external links?

 

It would be easier to understand if you could attach the f3d/f3z files.

0 Likes
Message 3 of 7

pedro_babo
Advocate
Advocate

Hi Kandennti

 

First thanks for replying.

 

I’ve also looked for it and didn’t find it.

I’m importing a local SAT file into Fusion. Make some surfaces and need a way to get the path from the imported geometry file to export back one other file.

 

How can I Get that Path.

0 Likes
Message 4 of 7

kandennti
Mentor
Mentor

@pedro_babo .

Basically, there should be no function in the occurrence or component to record the imported path.

Therefore, it is necessary to create some kind of mechanism to record the path when performing the import process.

 

As an example, the file path is recorded in the attributes of the occurrence that is created when the file is imported.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-33519517-DC34-4B04-91A7-27E40AD2D010 

 

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

SAT_IMPORT_ATTR: dict = {
    'group': 'sat_import_test',
    'name': 'path'
}

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

        # get sat filepath
        path = get_filepath()
        if len(path) < 1:
            return

        # import sat file & add path to attribute
        occ: fusion.Occurrence = import_sat(
            path,
            SAT_IMPORT_ATTR,
        )

        # attribute path checking
        attr: core.Attribute = occ.attributes.itemByName(
            SAT_IMPORT_ATTR['group'],
            SAT_IMPORT_ATTR['name'],
        )
        if not attr:
            return

        ui.messageBox(attr.value)

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


def import_sat(path: str, attrInfo: dict) -> fusion.Occurrence:
    app: core.Application = core.Application.get()

    impMgr: core.ImportManager = app.importManager
    satOpt: core.SATImportOptions = impMgr.createSATImportOptions(path)

    des: fusion.Design = app.activeProduct
    root: fusion.Component = des.rootComponent

    occLst: core.ObjectCollection = impMgr.importToTarget2(satOpt, root)
    occSat: fusion.Occurrence = occLst.item(0)

    occSat.attributes.add(
        attrInfo['group'],
        attrInfo['name'],
        path,
    )

    return occSat


def get_filepath() -> str:
    ui: core.UserInterface = core.Application.get().userInterface

    dlg = ui.createFileDialog()
    dlg.title = 'SAT Import'
    dlg.isMultiSelectEnabled = False
    dlg.filter = 'SAT(*.sat)'
    if dlg.showOpen() != core.DialogResults.DialogOK :
        return ''
    return dlg.filename

 

In the last part, the import path is called from an attribute of the actual imported occurrence.

0 Likes
Message 5 of 7

pedro_babo
Advocate
Advocate

Hi @Kindenty.

 

I was already thinking about some way to do this. I was thinking about description of root component.

Thanks this is one invisible way for the user.

 

Is there a way to run this scripts upon double clicking a SAT file that will start FusionLauncher?

 

0 Likes
Message 6 of 7

kandennti
Mentor
Mentor

@pedro_babo .

 

You can't do that with scripts.
I think it would be possible to change it to an add-in and use the UserInterface.commandTerminated event.

0 Likes
Message 7 of 7

pedro_babo
Advocate
Advocate

Thanks @kandennti this need to be mature But I will post the Ideas.

0 Likes