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: 

csv script

28 REPLIES 28
SOLVED
Reply
Message 1 of 29
daniel_lyall
1328 Views, 28 Replies

csv script

is it possible at this time to create a script to take all parameters from a file, they will be user created parameters put it out as a csv file, then have a script to import the csv file and it put's the user parameter back in as user parameters or would it be easyer to put them back in as modle parameters.

 

this just a question at this time as this would be handy for doing cabint makeing. it is something I may try to do soon but I am not very good with codeing.


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

28 REPLIES 28
Message 2 of 29
marshaltu
in reply to: daniel_lyall

Hello,

 

If you just want to import data from a file and use them to create user parameters, the following codes would demostrate that. The file content is like below:

 

Spoiler

d1=10 mm

d2=5 in

d3=30 degree

 

 

#Author-
#Description-

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = adsk.fusion.Design.cast(app.activeProduct)
        if not design:
            ui.messageBox('No active Fusion design', 'Import Spline csv')
            return
        
        dlg = ui.createFileDialog()
        dlg.title = 'Open CSV File'
        dlg.filter = 'Comma Separated Values (*.csv);;All Files (*.*)'
        if dlg.showOpen() != adsk.core.DialogResults.DialogOK :
            return
        
        filename = dlg.filename
        f = open(filename, 'r')
        parameters = design.userParameters
        line = f.readline()
        data = []
        i = 0
        while line:
            pntStrArr = line.split('=')
            for pntStr in pntStrArr:
                data.append( pntStr)
        
            if len(data) >= 2 :
                i += 1
                parameters.add(data[0], adsk.core.ValueInput.createByString(data[1]), '', 'MyParameter{}'.format(i))

            line = f.readline()
            data.clear()            
        f.close()         

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

Thanks,

Marshal



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 3 of 29
daniel_lyall
in reply to: marshaltu

Thank you very much @marshaltu at the moment I cant do anything with it, for some reasion all paramaters are missing from all of my designs. I am going to have to fix them all before I get back to this.

 

thanks


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

Message 4 of 29
daniel_lyall
in reply to: daniel_lyall

I have been playing with the code if I run it to open a .csv with just the 3 lines you did it works if I try one taken from a sketch it does nothing.

 

this is the code I am useing to get the paramaters

#Author-Brian Ekins
#Description-Dumps out parameter info to a specified csv file.

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        # Get the name of the file to write to.
        fileDialog = ui.createFileDialog()
        fileDialog.isMultiSelectEnabled = False
        fileDialog.title = "Specify result filename"
        fileDialog.filter = 'CSV files (*.csv)'
        fileDialog.filterIndex = 0
        dialogResult = fileDialog.showSave()
        if dialogResult == adsk.core.DialogResults.DialogOK:
            filename = fileDialog.filename
        else:
            return

        des = app.activeProduct

        result = 'Name,Expression,Units,Value (database units),Comment,Type,Component\n'

        # Get the data for all user parameters.
        for userParam in des.userParameters:
            result += (userParam.name + ',' + userParam.expression + ',' + userParam.unit + ',' + 
                       str(userParam.value) + ',' + userParam.comment + ',User\n')

        for comp in des.allComponents:
            for modelParam in comp.modelParameters:
                result += (modelParam.name + ',' + modelParam.expression + ',' + modelParam.unit + 
                           ',' + str(modelParam.value) + ',' + modelParam.comment + ',Model,' + comp.name + '\n')

        output = open(filename, 'w')
        output.writelines(result)
        output.close()
        
        ui.messageBox('File written to "' + filename + '"')

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

 

do i need to change this bit, parameters = design.userParameters to something like below

 

# Get the data for all user parameters.
for userParam in des.userParameters:
result += (userParam.name + ',' + userParam.expression + ',' + userParam.unit + ',' +
str(userParam.value) + ',' + userParam.comment + ',User\n')

for comp in des.allComponents:
for modelParam in comp.modelParameters:
result += (modelParam.name + ',' + modelParam.expression + ',' + modelParam.unit +
',' + str(modelParam.value) + ',' + modelParam.comment + ',Model,' + comp.name + '\n')


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

Message 5 of 29
marshaltu
in reply to: daniel_lyall

Hello,

 

I changed the codes a little bit to read the data exported from Brain's scripts. Now it should work as expected.

 

Please note I have to give one space to comments if it is empty in file. There was a bug to create user parameter with empty comments. I have fixed it in our development build. The fix should be availabe in next major update.

 

Thanks,

Marshal

 

#Author-
#Description-

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = adsk.fusion.Design.cast(app.activeProduct)
        if not design:
            ui.messageBox('No active Fusion design', 'Import csv')
            return
        
        dlg = ui.createFileDialog()
        dlg.title = 'Open CSV File'
        dlg.filter = 'Comma Separated Values (*.csv);;All Files (*.*)'
        if dlg.showOpen() != adsk.core.DialogResults.DialogOK :
            return
        
        filename = dlg.filename
        f = open(filename, 'r')
        parameters = design.userParameters
        line = f.readline()
        data = []
        while line:
            pntStrArr = line.split(',')
            for pntStr in pntStrArr:
                data.append( pntStr)
        
            if len(data) >= 5 :
                if data[0] != 'Name' and data[1] != 'Expression': 
                    comments = data[4]
                    if comments == '':
                        comments = ' '
                    parameters.add(data[0], adsk.core.ValueInput.createByString(data[1]), data[2], comments)

            line = f.readline()
            data.clear()            
        f.close()         

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


Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 6 of 29
daniel_lyall
in reply to: marshaltu

thanks @marshaltu it's not working very well it got the first 4 parameters then stopped and came up with an error. i have attached the .csv file I am getting the parameters from, thank you for your help so far.

 

I just tryed it on another file and it worked. as fails, 11 works, this is going to save me a lot of time thank you

 

I think what is makeing it fail is what is in the Expression 11 has number and letter's, AS has words it fail on the 5th line what has words it does not matter to much as if this is the problem I can just change it what is in 11 is how I do it now  


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

Message 7 of 29
marshaltu
in reply to: daniel_lyall

Hello,

 

I am not clear how you generate the file? There were two kinds of errors in the file:

 

1. Special character to express "deg". I would like to suggest to replace all of them by "deg".

d52,0.0 ∞,deg,0,,Model,layout sheet

2. There was an additional item in Line #6. We have to remove "Draw_depth".

d43,Draw_depth - 18 mm,mm,38.2,,Model,Draw`s

 

After I correct the errors, I can import those data and create user parameters.

 

Thanks,

Marshal



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 8 of 29
prainsberry
in reply to: marshaltu

Hey Daniel I am actually working on a script to do something really similar.  Like you want to be able to switch between different versions of the design really quickly?  Might have it done this weekend.

 

Stay tuned.



Patrick Rainsberry
Developer Advocate, Fusion 360
Message 9 of 29
daniel_lyall
in reply to: marshaltu


@marshaltu wrote:

Hello,

 

I am not clear how you generate the file? There were two kinds of errors in the file:

 

1. Special character to express "deg". I would like to suggest to replace all of them by "deg".

d52,0.0 ∞,deg,0,,Model,layout sheet

2. There was an additional item in Line #6. We have to remove "Draw_depth".

d43,Draw_depth - 18 mm,mm,38.2,,Model,Draw`s

 

After I correct the errors, I can import those data and create user parameters.

 

Thanks,

Marshal


@marshaltu I have look at some other Model's they have 1. the same that's down to fusion.

 

I just did a quick test this is what should comes up from a press pull

d20.00 °deg0 Model(Unsaved)

 

all the spots where there is a deg in the files paramaters like above for press pull, some are like above, but others have a number string changeing them to 0 fixes the problem like you said. 

in the paramater's they are fine it is when they are dumped to the csv file, some have gone bad.

 

here is a link to the file http://a360.co/1Q09V2U if you have a look your self you will see they are fine until you dump the file to a csv, it could mean the are not at 0 deg they could be a bit out of squire

 

I just did this file http://a360.co/1NaBCRt it has the same problem as the file above. 

so what is makeing this happen I would not have a clue

 

2. that's how I use to do the paramater's I use letter code's now 


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

Message 10 of 29
daniel_lyall
in reply to: prainsberry


@prainsberry wrote:

Hey Daniel I am actually working on a script to do something really similar.  Like you want to be able to switch between different versions of the design really quickly?  Might have it done this weekend.

 

Stay tuned.


take you time your meant to be on holiday.

 

sound good it would be good to have a script where you build one model say a cabint then you can run a script that will fire out a copy but it opens a dialog so you can change the sizeing before it builds that model as a new model. this would make the hobby boys quite happy and cabint maker's. doing scripts like this would make fusion a almost do everything program and should get you a pay rise. Smiley Wink


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

Message 11 of 29
prainsberry
in reply to: daniel_lyall

Now you are talking sounds good to me haha.  Unfortunately this is actually what I think is fun to do on my holiday 🙂

 

Check this out.  I am still seeing some problems when you try to un-suppress many features at once but the parameters parts is working pretty good if you want to take a look at it.

 

https://github.com/tapnair/configSaver

 

Will still probably work on it a bit more before making a video or anything.  

Let me know what you think.



Patrick Rainsberry
Developer Advocate, Fusion 360
Message 12 of 29
daniel_lyall
in reply to: prainsberry

@prainsberry it took a bit to work out how to use it, I think I am not useing it correctly what am I supost to do with the xml file 


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

Message 13 of 29
prainsberry
in reply to: daniel_lyall

You should not have to do anything with the xml. Just save a config then you can set the model back to it. I'll make a quick video. Regards, Patrick Rainsberry


Patrick Rainsberry
Developer Advocate, Fusion 360
Message 14 of 29
prainsberry
in reply to: daniel_lyall

@daniel_lyall hey i think i made it a little easier to use.  Download the updated version.  Seperated the save configuration from the switch configuration into two commands.  Let me know what you think.

 

Also made a super quick video here:

https://youtu.be/9VTTctoid2E

 

 



Patrick Rainsberry
Developer Advocate, Fusion 360
Message 15 of 29
daniel_lyall
in reply to: prainsberry

@prainsberry I was using it wrong got the idea now.

 

supper dupper job dude. it will save time for making bespoke stuff, now I just need to see if it will work with a nested cabinet what is linked to the original model, if it does you just need to make one Model, one nested copy and just run through each change in cam and post as you are going.

 

it's going to make changing my custom wheelchair table east as.

 

thank you @prainsberry I will put you in for a payrise Smiley Wink


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

Message 16 of 29
daniel_lyall
in reply to: daniel_lyall

it's good for finding mistakes as well 


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

Message 17 of 29
marshaltu
in reply to: daniel_lyall

Hello,

 

I am aware that there is a setting to control if unit (deg) or symbol(o) is shown in parameter dialog, which might impact the results outputed to dump file by Brian's script.

 

You could find the setting by right-top corner -> "Your account" -> Preferences -> Units and Value Display -> "Display symbols for units" and just uncheck the option for workaround. Though I can export unit as "deg" since I check the option in my machine. I am not clear if any region settings in your machine impact the results. My region is en-US.

 

About "d43,Draw_depth - 18 mm,mm,38.2,,Model,Draw`s", I found it wasn't an error. Sorry for misleading. It was saying parameter "d43" depended on parameter "Draw_depth". We might need some enhancement to deal with the dependencies among parameters in Brian's script so that the dependent parameters can be exported firstly. For example: "Draw_depth" would be prior to "d43".

 

It looked "prainsberry" has provided other solution for you. Please let me know if you still need these scripts.

 

Thanks,

Marshal 



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 18 of 29
daniel_lyall
in reply to: marshaltu

@marshaltu yer it's a bit funny that, that paramater failed  and other's did not but you may be correct about the order that info get's spat out, could well be the problem.

 

the deg thing they where from the press pull and they could be at that angle in the sketch but sinces the press pull was not done at a angle it could be stuffing it up, that's over my head.

 

I have changed the way I set up the parmater's now and none of them have failed at all, the way I use to do it they can have the same problem, some have done the same thing.

 

it works fine with how I do it now and yes I still will use it, it saves a lot of typeing if you think you can make it better go for it other wise what ever goes.

 

thank you for all the work you have done. 

 

the stuff @prainsberry has done is really good for doing changes once's the model has been done and the other bits he has added, it's the bees knees. the two together makes life just that bit more easy, for what I do.if he could added it into his script that be even better, it's good haveing all his scripts in the same place. I use them quite often.

 

anyway thankyou both of you.

 

 


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

Message 19 of 29
marshaltu
in reply to: daniel_lyall

Hello,

 

I changed a little bit the script to dump out paramaters from design. Now it should be able to handle dependencies among parameters. Please let me know if you see any issues.

 

Thanks,

Marshal

 

#Author-Brian Ekins
#Description-Dumps out parameter info to a specified csv file.

import adsk.core, adsk.fusion, traceback

def insertParameter(param, allParams):
    allParams.append(param)
    dependentParams = param.dependentParameters
    for dependentParam in dependentParams:
        if dependentParam in allParams:
            allParams.remove(dependentParam)
            allParams.append(dependentParam)

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        # Get the name of the file to write to.
        fileDialog = ui.createFileDialog()
        fileDialog.isMultiSelectEnabled = False
        fileDialog.title = "Specify result filename"
        fileDialog.filter = 'CSV files (*.csv)'
        fileDialog.filterIndex = 0
        dialogResult = fileDialog.showSave()
        if dialogResult == adsk.core.DialogResults.DialogOK:
            filename = fileDialog.filename
        else:
            return

        des = app.activeProduct

        # Get sorted parameter list according to dependencies
        allParams = []
        modelParamCompNames = {}
        for userParam in des.userParameters:
            insertParameter(userParam, allParams)
            
        for comp in des.allComponents:
            for modelParam in comp.modelParameters:
                insertParameter(modelParam, allParams)
                modelParamCompNames[modelParam.name] = comp.name

        result = 'Name,Expression,Units,Value (database units),Comment,Type,Component\n'
        
        # Get the data for all parameters.
        for param in allParams:
            compName = modelParamCompNames.get(param.name)
            if compName != None:
                result += (param.name + ',' + param.expression + ',' + param.unit + 
                           ',' + str(param.value) + ',' + param.comment + ',Model,' + compName + '\n')
            else:
                result += (param.name + ',' + param.expression + ',' + param.unit + ',' + 
                       str(param.value) + ',' + param.comment + ',User\n')

        output = open(filename, 'w')
        output.writelines(result)
        output.close()
        
        ui.messageBox('File written to "' + filename + '"')

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


Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 20 of 29
daniel_lyall
in reply to: marshaltu

thanks @marshaltu that work's better one small problem tryed a file with a scale in it, it failed at the line before it.

 

this is the line

d640.1 0.1 Model12mm finger tenions (2) 

there are a few scale's in the sketch, it fail at the one like above.

 

if it is like this d64_1 it's fine 

 

all other files work, other that if there is a scale like above.


Win10 pro | 16 GB ram | 4 GB graphics Quadro K2200 | Intel(R) 8Xeon(R) CPU E5-1620 v3 @ 3.50GHz 3.50 GHz

Daniel Lyall
The Big Boss
Mach3 User
My Websight, Daniels Wheelchair Customisations.
Facebook | Twitter | LinkedIn

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