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.

Complex Formulas in Variables

Greetings All

I tried to perform a function that failed; perhaps it is impossible at present or I did it incorrectly.  I identified two variables as - #stair_riser = 7.5" and #stair_tread = 9".

I created a sloped line to dimension the pitch with another variable as #stair_pitch to equal - here it is - ArcTan (#stair_riser / #stair_tread) to equal the angle of the pitch.

The Question - can we perform complex math as indicated?  Thank you always for your great support and advise - this is a remarkable forum for learning.
Tagged:

Comments

  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    ArcTan in onshape transcription is:

    atan (value is number) returns ValueWithUnits

    Arctangent, i.e. inverse tangent.

    also make sure you defined values with units correctly ie 7.5*in and 9*in
  • robert_melascagliarobert_melascaglia Member Posts: 42 EDU
    Thank you that is it.
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,014 PRO
    edited February 2019
    @robert_melascaglia please look into featurescript.

    editting a variable equation in a single line is painful, I think you're ready to move on.

    You're one step away from a featurescript which is a great environment for your equations. It doesn't take much to kick off a featurescript which has variables and equations that you can see and work with.

    Take your feet out of the cold water and come over here and put'm in some warm water. Try it.

    Someone should come up with a 3 minute video doing this.....




  • robert_melascagliarobert_melascaglia Member Posts: 42 EDU
    billy2 - you have convinced me and certainly will begin to learn featurescript, thanks for the nudge.
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,014 PRO
    edited February 2019
    I was trying to create equations on the variable input and it wasn't easy. Here's the code I started using instead of the single input line. Look closely at getVariable() & setVariable(). This is how you read and put back the variables. Once you have them in featurescript, it's easy to write & maintain the equations. I'm not dealing with units properly here. OS is in meters and I'm working in mm and I don't handle units correctly in this example. This is good enough for me though.

    FeatureScript 993;
    import(path : "onshape/std/geometry.fs", version : "993.0");
    
    annotation { "Feature Type Name" : "Volume" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            //input area
            annotation { "Name" : "Parts", "Filter" : EntityType.BODY }
            definition.myQuery is Query;
            
        }
        {
            //function area
            
            const volume=evVolume(context, {"entities" : definition.myQuery}).value*1e9;
            
            println(volume);
            
            const X=getVariable(context, "X");
            const Y=getVariable(context, "Y");
            const Z=volume/(X*Y);
            
            println("X="~X~" Y="~Y~" Z="~Z);
            
            setVariable(context, "X", X);
            setVariable(context, "Y", Y);
            setVariable(context, "Z", Z);
      
        });
    The IDE OS built for featurescript is really quite good. I think you'll like it.




  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    @billy2

    Onshape uses the ValueWithUnits type to correctly deal with units.  If you just leave `volume` as-is instead of doing `.value*1e9`, `volume / (X*Y)` will come out as the correct value (assuming that X and Z are length values and not raw numbers).  If X and Y are currently raw numbers, I think it will be much easier to maintain your code if you make them lengths instead, and use the ValueWithUnits as intended.

    If it is not possible to do this, you can convert the volume to millimeters using the following:
    const volume = evVolume(...) / (millimeter ^ 3);

    But, as I said, I think it will be easier to work with these if you set X and Y to be actual lengths, and then just do the division without any hackery.

    Here is some documentation:
    https://cad.onshape.com/FsDoc/library.html#ValueWithUnits

    And here is an example I put together of how I would organize the code you are writing:
    https://cad.onshape.com/documents/754a3d4aaea3998e3633f0c1/w/5e7b3e7733fa80bea100a5b8/e/e811aed1f73d45af81c1846e

            const volume = evVolume(context, { "entities" : definition.part });
            
            println("Raw volume as ValueWithUnits: " ~ volume);
            println("Volume printed in the units that I care about: " ~ (volume / (inch ^ 3)) ~ " in^3");
            println("Volume printed nicely in the units that I care about: " ~ roundToPrecision((volume / (inch ^ 3)), 0) ~ " in^3");
            
            println("");
            
            const X = getVariable(context, "X");
            const Y = getVariable(context, "Y");
            const Z = volume / (X * Y);
            
            println("Raw Z as ValueWithUnits: " ~ Z);
            println("Z printed in the units that I care about: " ~ (Z / inch) ~ " in^3");
            println("Z printed nicely in the units that I care about: " ~ roundToPrecision(Z / inch, 0) ~ " in^3");
            
            // You don't need to set X and Y again as they are already set and you are not changing them.
            setVariable(context, "Z", Z);


    tl;dr: let Onshape take care of the units for you instead of trying to manage them yourself.  The only time you should need to worry about units is when you want to print out their values, and then it is as easy as just dividing the ValueWithUnits by the unit that you care about.
    Jake Rosenfeld - Modeling Team
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,014 PRO
    @Jake_Rosenfeld Thanks for cleaning up my garbage.




Sign In or Register to comment.