Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Options

Is it possible to import configuration variables as txt file?

joshua_maderojoshua_madero Member Posts: 2
I have a script in python that finds variables I need to use to design a propeller, like hub diameter, chord lengths, etc. I can export these variables as a file (csv/txt). Is it possible to import this file and automatically create the variables and assign values in Onshape? If you are a Solidworks user, this is just importing equations.

(I haven't started using FeatureScript)

Thanks!

Comments

  • Options
    john_mcclaryjohn_mcclary Member, Developers Posts: 3,902 PRO
    edited May 2020
    EDIT: Ignore this, see next post

    Yea, you can either create a featurescript that accepts a CSV and assigns variables

    Or

    You can create a featurescript on the fly that just is a list of setVariable(); commands

    What I end up doing is I have a spreadsheet that spits out all of the parameters into a cell. Then I have the marketing person copy paste into a super plain featurescript so I don't have to worry about them knowing code.


    Then I have a separate FS that gives me global variables when I add it to the feature tree.
    Don't worry about the enum eUnit, that was so I can have a bunch of global variables with overrides for different assemblies...



    If I had half a clue, I would like this to be derived from a .cvs to make it even easier for the marketing person.
    Problem is, then they would need to always export from a spreadsheet when wanting to make minor tweaks.
    At least this way I can edit any parameter in the Parameters FS directly.
  • Options
    john_mcclaryjohn_mcclary Member, Developers Posts: 3,902 PRO
    edited May 2020
    Actually that was Really easy...

    https://cad.onshape.com/documents/75dfbb9091b5bea038398789/w/20b6cc9fc5923631d05184a8/e/97a921379d1437bdaef7fcdf

    FeatureScript 1271;
    import(path : "onshape/std/geometry.fs", version : "1271.0");
    
    annotation { "Feature Type Name" : "CSV Variables" }
    export const CSV_Vars = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "CSV file" }
            definition.myCsvFile is TableData;        
        }
        {
            var file = definition.myCsvFile.csvData;
            var v = {};
                
            for (var i = 0; i < size(file); i += 1)
            {
                v[file[i][0]] = file[i][1];
                setVariable(context, file[i][0], file[i][1]);
            }
            
        });
    

    Edit: The above is bugged, I can't figure out how to get size(file) to output anything other than 8...

    Edit2: Ok, fixed bug, didn't realize I needed to check the size of csvData instead of the TableData object. Above code is  now correct
  • Options
    john_mcclaryjohn_mcclary Member, Developers Posts: 3,902 PRO
    edited May 2020
    Thanks for the idea @joshua_madero !
    Your question got me re-thinking our biggest product's design workflow.

    It works pretty well, now the marketing person can click a button and update the csv in Onshape. Making it even more fool proof.

    and I didn't have to beg @MBartlett21 for featurescript help this time :blush:



    By the way, If you need to tweak or do additional operations on any variable that you defined in the CSV
    you can add these two lines of code (in red)

    Then you can access any of those variables in featurescript like any other map. Example the variable "shingleThickness" was created on line 10 of the spreadsheet, and can be accessed in the featurescript by calling v.shingleThickness


  • Options
    MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,034 EDU
    Thanks for the idea @joshua_madero !
    Your question got me re-thinking our biggest product's design workflow.

    It works pretty well, now the marketing person can click a button and update the csv in Onshape. Making it even more fool proof.

    and I didn't have to beg @MBartlett21 for featurescript help this time :blush:


    @john_mcclary
    That looks good!
    :D
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,066 PRO
    @john_mcclary
    This is cool and I love the simple code! I'm not sure how to get anything other than unitless values or strings. Should I be able to get the value to equal "1*millimeter" or should I tweak the featurescript?
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • Options
    john_mcclaryjohn_mcclary Member, Developers Posts: 3,902 PRO
    edited May 2020
    i just keep everything unitless until I finally use the variable. That way I can freely do calculations at any point without needing to convert back and forth.

    Onshape will automatically enclose the equation with "(____) in" for inches so I don't even think auto it. start typing the variable in the dimension and let autocomplete finish it.

    To add and remove units  at any point it is usually as easy as VARIABLE * UNIT to add and VARIABLE / UNIT to remove the units.
  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,066 PRO
    edited June 2020
    i just keep everything unitless until I finally use the variable. That way I can freely do calculations at any point without needing to convert back and forth.

    Onshape will automatically enclose the equation with "(____) in" for inches so I don't even think auto it. start typing the variable in the dimension and let autocomplete finish it.

    To add and remove units  at any point it is usually as easy as VARIABLE * UNIT to add and VARIABLE / UNIT to remove the units.
    I got ya. I think that would cause issues if you change your workspace units from inch to mm, but if you know you never will, then that works great. I could add a third column for units if I want, I suppose
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • Options
    john_mcclaryjohn_mcclary Member, Developers Posts: 3,902 PRO
    Yea, it's all about how you read your csv in the for loop.
Sign In or Register to comment.