API for Component Save Copy As

API for Component Save Copy As

schengnz
Enthusiast Enthusiast
2,127 Views
4 Replies
Message 1 of 5

API for Component Save Copy As

schengnz
Enthusiast
Enthusiast

Is there a API for Component "save copy as"?

 

Context: 

At the moment, I have a script that takes a CSV with User Parameters and generate a whole family of parts and saved as .f3d files. These automatically generated parts are then uploaded (manually) back into Fusion360 as separate designs. Subsequent updates of these automated generated parts are (manually since API is not yet available) uploaded into Fusion360 using the "Import New Version" functionality.

 

You might argue that my workflow is unnatural, however I have to do this becaue Fusion 360 does not allow to replace a component with another component at the moment, but only a different version of the component. I tried to get around the compoment replacement limitation while retaining the ability to automatically generate component family by utilizing the "Import New Version" functionality.

 

Now because of the "Clone" limitation as described in http://forums.autodesk.com/t5/design-and-documentation/insert-into-current-desing-clone-error/m-p/56... I am stuck. Fusion360 thinks the automatically generated parts are all clone of each other. As such I can't build anything that use more than one of these generated parts; unfortunately this is a very common scenario for us. The work around is to use "Save Copy As" which I have verified that it works, but this is very time consuming to do manually for each iteration of automatically generated parts, and it pretty much breaks the workflow from a practical point of view.

 

What would be a suggested workaround the combination of inability to replace a component with another component, and the inability to import more than one clone into a design? Is there an API for Save Copy As?

Thanks,
Stephen

0 Likes
2,128 Views
4 Replies
Replies (4)
Message 2 of 5

ekinsb
Alumni
Alumni

There is an API for Save As, although as I was testing it to respond to your question I found some issues.  Luckily there is also a workaround so you can use it now.  The code below uses the file that is currently open, has a .csv file selected, reads the data from the .csv file and uses it to update parameters in the open model.  It then saves the model using the name specified in the csv file and then does it all over again for the next row in the csv file.

 

import adsk.core, adsk.fusion, traceback
import os

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

        # Have the table file selected.
        dialog = ui.createFileDialog()
        dialog.filter = 'Comma Separated Values (*.csv)'
        dialog.initialDirectory = os.path.dirname(os.path.realpath(__file__))
        if dialog.showOpen() != adsk.core.DialogResults.DialogOK:
            return
            
        filename = dialog.filename
        
        # The project and folder is hard coded.
        projectName = 'ParamBlockTest'
        folderName = 'Test7'
        
        proj = None
        for project in app.data.dataProjects:
            if project.name == projectName:
                proj = project
                break

        targetFolder = None            
        if proj:
            if folderName == '':
                targetFolder = project.rootFolder
            else:
                for folder in project.rootFolder.dataFolders:
                    if folder.name == folderName:
                        targetFolder = folder
                        break

            if not targetFolder:
                return
        else:
            return

        # Open the file.
        f = open(filename, 'r')

        # Read the first line, which is the header row.
        # This contains the names of the parameters that
        # will be edited.  The first column is the name
        # of that row and each additional column is a parameter.
        firstLine = f.readline()
        firstLine = firstLine.rstrip('\n')
        paramNames = firstLine.split(',')
        paramNames = paramNames[1:]
              
        # Iterate through the value lines.
        line = f.readline()
        while line:
            doc = app.activeDocument
            design = app.activeProduct
            params = design.allParameters        

            line = line.rstrip('\n')
            values = line.split(',')

            # Iterate through the values and change the corresponding parameter.            
            for i in range(1,len(values)):
                param = params.itemByName(paramNames[i-1])
                param.expression = values[i]

            # Save the file.
            adsk.doEvents()
            doc.saveAs(values[0], targetFolder, '', '')
            adsk.doEvents()
            
            line = f.readline()
        
        f.close()        

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

 

 

Here's the contents of my test csv file.  It's assumes that the first column is the name that will be used when doing the save as.  The next columns are the values for the parameters to change, with the first row being the names of the parameters.  In my case, there are three parameters; "Width", "Height", and "Length".

Name,Width,Height,Length
Size1,1 in,1 in,0.25 in
Size2,1 in,1 in,0.5 in
Size3,1 in,1 in,1 in
Size4 ,1 in,1 in,2 in

 

The workaround is the doEvents calls around the saveAs call.  They shouldn't be needed. 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 5

schengnz
Enthusiast
Enthusiast

@ekinsb Thanks for such detail reply. One question: does your example achieve "Save As" or "Save a Copy As"? I need the saved copy to be no longer considered as a clone by Fusion360. Does your proposed solution achieve that? Thanks!

0 Likes
Message 4 of 5

ekinsb
Alumni
Alumni

You're right, it's doing a Save As.  I glossed over that issue when I first read your initial question.  Unfortunately, the API doesn't currently support the Save Copy As functionality.  I've finished the API design and development team is starting to look at it but I don't know if we'll have time to make it into the next release.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 5 of 5

schengnz
Enthusiast
Enthusiast
I really hope that either this or http://forums.autodesk.com/t5/fusion-360-ideastation-request-a/breaking-parent-child-relation-in-sit... would make it to the next release. Having either one of them would (partially) solve a major short-coming of Fusion 360 which is stopping the creation of large assemblies using part families/interchangable parts. Of course if Fusion360 has proper Inventor iParts capability all these are moot.
0 Likes