Read variables from CSV, export STEP to folder

Read variables from CSV, export STEP to folder

Anonymous
Not applicable
3,774 Views
22 Replies
Message 1 of 23

Read variables from CSV, export STEP to folder

Anonymous
Not applicable

I have to make about 300 variations on a single part. I can define that part fully with variables, and feed Fusion a 300 line CSV file.

 

How do I go about reading variables from the CSV file line by line and exporting a STEP file for each part?

 

Thanks

Luke

0 Likes
Accepted solutions (1)
3,775 Views
22 Replies
Replies (22)
Message 2 of 23

prainsberry
Autodesk
Autodesk

THere are a couple of examples I would look at.  Under file/scripts look at ImportSplineCSV.  THis will show you the basic tools to read in a csv file from disc and do some parsing of the text.  I did a similar importer and used the logic from this script for the text parsing.  Also look at the help article for Programming INterface->Fusion API Reference Manual->Objects->Export Manager, at the bottom of that page there is a sample code that show hows to use the export functions.

 

Also check out the Bolt and Spur gear examples to see how to create geometry based on some inputs.

 

Taking from these places you should be able to get there I think.



Patrick Rainsberry
Developer Advocate, Fusion 360
0 Likes
Message 3 of 23

Anonymous
Not applicable

Hi Prainsbury, I appreciate it. So it sounds like this is totally doable, I just have to set aside some time to create the script.

 

Luke

0 Likes
Message 4 of 23

Anonymous
Not applicable

Hi prainsbury,

 

Both of those add model parameters, I can't seem to find any examples of editing existing user parameters.

 

The idea is you draw the part with the user parameters, and then "apply" a CSV file which will make the changes on each line and export a STEP file for each one.

0 Likes
Message 5 of 23

Anonymous
Not applicable

Also I'm not seeing any examples for UserParameters.

 

I tried "UserParameters.add("ID","7") to modify a user parameter named "ID" to value "7" but it just gives me this error.

 

I know python requires you to make objects and then "use" them but it's been so long, I have no idea (based on the API documentation) what kind of object I should be creating.

 Screen Shot 2015-02-12 at 5.19.08 PM.png

0 Likes
Message 6 of 23

Anonymous
Not applicable

Is there no "SetParameter("ID","7") command?

 

I'm feeling pretty defeated right now. I can pull the data in just fine but I'm having such a hard time with the documentation (very very light on examples!!) that I cannot figure out how to simple change an existing parameter

0 Likes
Message 7 of 23

Anonymous
Not applicable

i'm getting more info from Spyder than the documentation. What is the object hierarchy here? Where is parameters?

 

I'm just typing random stuff in with a . after it and getting a LOT more information from spyder than the documentation online

0 Likes
Message 8 of 23

Anonymous
Not applicable

So I'm seeing that we can do:

 

adsk.fusion.Parameter._set_value() but it doesn't give me enough arguments to choose which parameter I'm changing the value for

0 Likes
Message 9 of 23

Anonymous
Not applicable

It looks like I'm going to have to snag all of the parameters as a hash and then run a check on each entry to see if it matches my target parameter name and then change it? there's got to be an easier way

0 Likes
Message 10 of 23

prainsberry
Autodesk
Autodesk

You need use parameterlist to get the list of parameters first I believe.  Let me put together a little example and see if I can get it working for you.  

 

I'd highly recomend reading a couple of the articles in the the API user manual.  Specifically the basic concepts one that talks about the object model and lists.



Patrick Rainsberry
Developer Advocate, Fusion 360
0 Likes
Message 11 of 23

Anonymous
Not applicable

OK so I've figured out how to find a parameter's index, but I can't seem to create a parameter object using that index. So I have no idea how to actually change a parameter value

0 Likes
Message 12 of 23

Anonymous
Not applicable

parameter = adsk.fusion.ParameterList.itemByName("ID")

parameter._set_value("7")

 

Am I getting closer? 🙂

0 Likes
Message 13 of 23

Anonymous
Not applicable

Screen Shot 2015-02-12 at 5.46.24 PM.png

 

 

Hmm... I dont think it needs two arguments?

 

Screen Shot 2015-02-12 at 5.46.58 PM.png

0 Likes
Message 14 of 23

Anonymous
Not applicable

Tried adding the (self,) arugment but got this error

 

Screen Shot 2015-02-12 at 5.47.57 PM.png

0 Likes
Message 15 of 23

ekinsb
Alumni
Alumni
Accepted solution

Here's a script that I believe does what you want. 

 

import adsk.core, adsk.fusion, traceback

def main():
    ui = None
    try:
        app = adsk.core.Application.get()
        design = app.activeProduct
        
        # Read the csv file.
        cnt = 0
        file = open('C://Temp//values.csv')
        for line in file:
            # Get the values from the csv file.
            pieces = line.split(',')
            
            length = pieces[0]
            width = pieces[1]
            height = pieces[2]
            
            # Set the parameters.
            lengthParam = design.userParameters.itemByName('Length')
            lengthParam.expression = length
            
            widthParam = design.userParameters.itemByName('Width')
            widthParam.expression = width

            heightParam = design.userParameters.itemByName('Height')
            heightParam.expression = height
            
            #Export the STEP file.
            exportMgr = design.exportManager
            stepOptions = exportMgr.createSTEPExportOptions('C:\\Temp\\test_box' + str(cnt) + '.stp')
            cnt += 1
            res = exportMgr.execute(stepOptions)
                        
        ui  = app.userInterface
        ui.messageBox('Finished')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


main()

 

It's using the expression property of the Parameter object you can use any valid expression just like you would type in the parmaters dialog.  For my test I had a box with the parameters Length, Width, and Height.  Here's the contents of my csv to demonstrate the various options.

 

4 in, 3 in, 1 in
3, 2, 1
3 cm, 2 cm, Length/2
5, 5, 4,

 

 


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

Anonymous
Not applicable

Jesus, I kind of wanted to figure it out but you probably just saved me 20 hours of work LOL. This is why I'm not in software!

 

I'll try it out, thanks!

0 Likes
Message 17 of 23

prainsberry
Autodesk
Autodesk

Haha yah me too, I was about 10% of the way there.  Thanks Brian!!



Patrick Rainsberry
Developer Advocate, Fusion 360
0 Likes
Message 18 of 23

Anonymous
Not applicable

prainsberry, did you happen to try it out?

 

I replicated his setup as best as I could and when I run the script nothing appears to happen. No errors, no change in values, no STEP files.

 

Thanks!

Luke

0 Likes
Message 19 of 23

Anonymous
Not applicable

Woop, it did this! Very strange. Any ideas?

 

edit: nevermind, this wasn't from the script

0 Likes
Message 20 of 23

ekinsb
Alumni
Alumni

I think the parameter names are case sensitive.  I had the names starting with upper case and you have lower case.  Doesn't matter which but they need to be the same.


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