Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

API: change Parameter and save in current folder

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
maik_staudt
580 Views, 4 Replies

API: change Parameter and save in current folder

Hi,

 

I want to write a function that changes a parameter and then saves the document to the current folder (not locally, but in the cloud) (or the folder where the "template-part" is located. The filename should be something like "projectname_" + the parameter. E.g. "projectname_350".

 

The code I have written is here:

 

 

 

 

 

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback, time

  

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

        

        def param_and_save(abstand):
            abstand_param = parameters.itemByName('Achsabstand')
            abstand_param.expression = str(abstand)
            adsk.doEvents()
            time.sleep(2)
            app.activeDocument.saveAs('test', '', '', '') # <- how can I save it ? 

          

        abstand = [50, 640, 736, 298]
        
        for x in abstand:
            param_and_save(x)
            

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

 

 

 

 

Changing the parameters works perfectly. But how can I save every document separately in the current project-folder  ?

The line "app.activeDocument.saveAs('test', '', '', '')" doesnt work. 

 

It seems that the function needs a name and a Project-folder and two other parameters ? How is this it done right ?

 

And another rather general question: Where can I find the definition of all the functions availibe ? For example in the Object Model-PDF-file I cant find  the saveAs-funktion ? 

 

And would it also be possible to just Copy a component inside a document with different name and different parameter "Achsabstand" ? (copy component and paste new) ? How would that be done ?

 

Thanks 😊

4 REPLIES 4
Message 2 of 5
kandennti
in reply to: maik_staudt

Hi @maik_staudt .

 

For now, let's talk about saveAs.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7b292219-62fa-4a01-b522-2a1a297edb28 
Fusion360's data panel can be saved under the same name, but I think it is better to use a different name to avoid confusion.


The dataFolder needs to be passed a DataFolder object.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-a0938aea-0657-4784-9cfc-f5757627ae99 


In this sample, we will save the four documents in the same folder as the active document when we run the split.
Be sure to execute it with the document saved once.

import adsk.core, adsk.fusion, adsk.cam, traceback, time

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

        def param_and_save(
            abstand :int,
            folder :adsk.core.DataFolder,
            fileHeader :str):

            abstand_param = parameters.itemByName('Achsabstand')
            abstand_param.expression = str(abstand)
            # adsk.doEvents()
            # time.sleep(2)

            # saveAs
            # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7b292219-62fa-4a01-b522-2a1a297edb28
            app.activeDocument.saveAs(f'{fileHeader}_{abstand}', folder, '', '')


        # get DataFolder
        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-a0938aea-0657-4784-9cfc-f5757627ae99
        folder :adsk.core.DataFolder = app.activeDocument.dataFile.parentFolder
        fileHeader = app.activeDocument.name

        abstand = [50, 640, 736, 298]
        for x in abstand:
            param_and_save(x, folder, fileHeader)
            app.activeViewport.refresh()

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

 

1.png

 

 

Message 3 of 5
maik_staudt
in reply to: maik_staudt

Thanks a lot, Kandennti,

 

The script works perfectly.

 

Is there also a way to copy the component inside the main-assembly with different parameter ? 

 

Thanks,

Maik 🙂

 

 

Message 4 of 5
kandennti
in reply to: maik_staudt

@maik_staudt .

 

I interpreted it to mean something like this.

 

There were variations in the execution time.
This video is the one that was processed in a fairly short time.

 

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

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

        start = time.time()

        def param_and_save(
            abstand :int,
            folder :adsk.core.DataFolder,
            fileHeader :str) -> str:

            abstand_param = parameters.itemByName('Achsabstand')
            abstand_param.expression = str(abstand)

            docName = f'{fileHeader}_{abstand}'

            # saveAs
            # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7b292219-62fa-4a01-b522-2a1a297edb28
            app.activeDocument.saveAs(docName, folder, '', '')

            return docName

        def waitFinishedSaving(
            doc :adsk.core.Document):

                try:
                    id = doc.dataFile.id
                except:
                    id = ''

                while True:
                    adsk.doEvents()
                    try:
                        doc.dataFile
                        if id != doc.dataFile.id:
                            break
                    except:
                        time.sleep(0.1)

        def getDataFileFromDataPanel(
            docNames :list,
            folder) -> list:

            wait = 3.0
            while True:
                try:
                    folder.dataFiles.asArray()
                    break
                except:
                    time.sleep(wait)
                    adsk.doEvents()

            names = []
            
            while True:
                for name in docNames:
                    if not name in names:
                        time.sleep(wait)
                        break
                else:
                    app.executeTextCommand('DataPanel.reload')                    
                    names = [f.name for f in folder.dataFiles.asArray()]
                    adsk.doEvents()
                    continue
                break

            dataFiles = folder.dataFiles.asArray()
            files = []
            for name in docNames:
                for df in dataFiles:
                    if name == df.name:
                        files.append(df)
                        break
                else:
                    continue

            return files

        # get DataFolder
        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-a0938aea-0657-4784-9cfc-f5757627ae99
        folder :adsk.core.DataFolder = app.activeDocument.dataFile.parentFolder
        fileHeader = app.activeDocument.name

        # update parameter & saveas
        abstand = [50, 640, 736, 298]
        docNames = []
        viewport = app.activeViewport
        for x in abstand:
            docNames.append(param_and_save(x, folder, fileHeader))
            viewport.refresh()
            viewport.fit()

        # get datafile
        dataFileList = getDataFileFromDataPanel(docNames, folder)

        # clese activeDocument
        app.activeDocument.close(False)

        # create assy doc
        assyDoc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        assyDoc.saveAs(f'{fileHeader}_Assy', folder, '', '')
        waitFinishedSaving(assyDoc)
        assyOccs = assyDoc.design.rootComponent.occurrences

        # insert Doc
        matZero = adsk.core.Matrix3D.create()
        for dataFile in dataFileList:
            assyOccs.addByInsert(dataFile, matZero, True)
            adsk.doEvents()
            viewport.refresh()
            viewport.fit()

        ui.messageBox(f'Done\n{time.time() - start}s')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Message 5 of 5
espablo
in reply to: kandennti

I tried to run this script and I must admit that it stopped working. The effect of its operation is the same as what I described in my post and the saveaAS() method does not work properly.
Link to my post:
https://forums.autodesk.com/t5/fusion-360-api-and-scripts/saveas-multiple-documents/td-p/12467126

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report