The Featurescript skill every serious Onshape user should have

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ย. 2024

ความคิดเห็น • 57

  • @chadoude
    @chadoude 3 ปีที่แล้ว +5

    Quick reason why you need to type * millimeter is because 90mm would be interpreted as a variable (because it is a combination of letters and numbers) and just typing 90 would be interpreted as an integer. The * symbol is overloaded in the architecture of FeatureScript to not only perform necessary conversions (especially when working in other values, like inches) but also to recast the type of assigned variable to a ValueWithUnits data type.

    • @ovyl
      @ovyl  3 ปีที่แล้ว

      Makes sense, thanks!

  • @jreamer0
    @jreamer0 ปีที่แล้ว +1

    Quality Onshape Content. Please keep going.

    • @ovyl
      @ovyl  ปีที่แล้ว

      Thanks, will do!

  • @1xicaradcaf
    @1xicaradcaf 2 ปีที่แล้ว +1

    Thank you very much for the video. Ther is not much content for feature script out there, this was very helpful

    • @ovyl
      @ovyl  2 ปีที่แล้ว

      Glad it was helpful! Now that Onshape launched their Variable Studio, this workflow is pretty much obsolete for creating global variables, but it's still a helpful primer on FeatureScript.

  • @johnrobinson3642
    @johnrobinson3642 2 ปีที่แล้ว +3

    Thanks so much for sharing this, this has been a great help/ I was a programmer in a past life and have been wanting to dabble in FS, and you've just shown me how to get started. Thanks!

  • @MarcelRobitaille
    @MarcelRobitaille 3 ปีที่แล้ว +1

    This is a great video. I am familiar with featurescipt, but I never thought to use it like this.

    • @ovyl
      @ovyl  3 ปีที่แล้ว

      Thanks! Glad to hear it's useful to people who already know their way around.

  • @cde7850
    @cde7850 3 ปีที่แล้ว +2

    Thanks for your lesson. it's very helpful!

  • @johanviljoen4704
    @johanviljoen4704 2 ปีที่แล้ว +8

    This video is the most underrated Onshape video ever! This is really good stuff. Thanks!

    • @ovyl
      @ovyl  2 ปีที่แล้ว +1

      Glad you think so!

  • @tophychris
    @tophychris 2 ปีที่แล้ว +2

    Very important, I was the 69th person to like this video. Also great video thank you

    • @ovyl
      @ovyl  2 ปีที่แล้ว +1

      Thanks for doing your part, Chris

  • @winddrandd1092
    @winddrandd1092 3 ปีที่แล้ว +1

    great video, helped me out a lot - thank you!

  • @SuperMaukam
    @SuperMaukam ปีที่แล้ว +1

    This is gold!

    • @ovyl
      @ovyl  ปีที่แล้ว +1

      Thanks! We have aspirations to do more FeatureScript related videos.

    • @dbuezas
      @dbuezas 23 ชั่วโมงที่ผ่านมา

      ​@@ovylyes please!!!

  • @StuartRobinson123
    @StuartRobinson123 4 หลายเดือนก่อน

    thank you!

    • @ovyl
      @ovyl  4 หลายเดือนก่อน

      Welcome!

  • @johnmcclary754
    @johnmcclary754 3 ปีที่แล้ว +1

    Nice video

    • @ovyl
      @ovyl  3 ปีที่แล้ว

      Thanks, John!

  • @aeropasta
    @aeropasta 2 ปีที่แล้ว

    Good stuff, thanks

    • @ovyl
      @ovyl  2 ปีที่แล้ว

      Glad it was helpful! Now that Onshape launched their Variable Studio, this workflow is pretty much obsolete for creating global variables, but it's still a helpful primer on FeastureScript.

  • @StuartRobinson123
    @StuartRobinson123 4 หลายเดือนก่อน

    post a link to one of your global variables featurescript examples?

    • @ovyl
      @ovyl  4 หลายเดือนก่อน +1

      just added it to the video description

  • @wpegley
    @wpegley 3 ปีที่แล้ว

    I need a dynamic part, referenced with measure distance that updates in Assemblies and annimation. IE hoist wirerope

    • @ovyl
      @ovyl  3 ปีที่แล้ว

      Hi Warren, are you trying to animate a rope winding up on a spool? If so, I'm not sure how to do it in Onshape.

  • @StuartRobinson123
    @StuartRobinson123 4 หลายเดือนก่อน

    do you still use this approach now that variable studio exists for global variables, and why or why not? i could see how it could possibly still be superior

    • @ovyl
      @ovyl  4 หลายเดือนก่อน +1

      Default to Variable Studio where possible. Accessing them in an assembly is a huge win. There are some cases where FS is still better just for declaring variables. For one, anything that's going to need a lot of calculation is a good candidate for featurescript. One example from the past is trying to figure out the diameter of a roll of material based on the total length. One way to solve it is in a for loop where the "circumference" increases with each revolution, so that was done in a custom feature. Another case could be if you want to have any user inputs with the variables. For example, we have magnets we always stock, and want variables for the holes that they fit in, but the clearance needed would change for a 3D print vs a steel part. So we have a custom feature with the nominal size hard coded, and the clearance as a user input. Variable Studio doesn't currently handle functions either, so if you wanted to write a #remap() function that takes a range of numbers (say 14-to-78 range) and remaps them to the 1-to-0 range I'd do it with FS.

    • @StuartRobinson123
      @StuartRobinson123 4 หลายเดือนก่อน

      @@ovyl super helpful thank you!! i'm a noob still trying to wrap my head around everything so this is great

  • @mko9377
    @mko9377 3 ปีที่แล้ว

    Hi
    Ovyl.
    can we have a summary list of the declared variables?
    and exported / linked in TXT or CSV?
    of the type:
    varname; value Default, written value
    - what if I wanted to load these variables in a script on another document by preloading the set data?
    Thank you very much we are hungry for clearly described examples.

    • @ovyl
      @ovyl  3 ปีที่แล้ว +1

      There are two bits of code that might work for what you need, neither of which we had much to do with. I think they'd work well together too.
      Here's a Custom Table that can read all of the variables in a part studio and display them:
      cad.onshape.com/documents/67c66cbcd55f1cb2f3401e45/v/040b95f12cfdfa350c5f45dd/e/4e3ec23e83c088c9b8a60f29
      Here's the CSV variables feature for converting an imported CSV to a list of variables:
      cad.onshape.com/documents/75dfbb9091b5bea038398789/v/2d6edbf047281ce828b3923f/e/97a921379d1437bdaef7fcdf
      With that, you should just need to right click the imported CSV tab and hit "update" and it ought to push it to the features using it as long they're in the same document. Across different documents you'll have to version the doc with the CSV and update the linked doc version in the CSV features manually in the other documents.

    • @mko9377
      @mko9377 3 ปีที่แล้ว

      @@ovyl How can I "retrieve" them for another document so that they are usable with #height ? thank you so much

    • @ovyl
      @ovyl  3 ปีที่แล้ว

      @@mko9377 They are treated just like any other custom feature. See the help doc here on how to add it. Note it can only link to a version, so you'll have to make at least one version once you want to link to another doc. cad.onshape.com/help/Content/customfeature.htm

    • @mko9377
      @mko9377 3 ปีที่แล้ว

      @@ovyl I meant:
      how can i retrieve the variables inserted in your fs "Global variables" to use them in another fs that i launch later?
      example:
      1) launch "global variables"
      and imposed on precondition:
      isLengh (definition.hight, LENGT .....)
      2) launch "my fs"
      how do i retrieve the "definition.hight" that i set to "global variables" without redefining it?
      Thanks for your patience and sorry if I was not clear.
      ... I also tried to ask on the forum .. but there the answers are cryptic .. and for us at the beginning this is a problem.

    • @ovyl
      @ovyl  3 ปีที่แล้ว

      @@mko9377 Ah, I think understand. I don't know of a good way to do that. If you want them used in several places, then set them in the code, and don't set them with the UI. If you feel that you absolutely must set them in the feature UI and use them multiple places, then check out the Super Derive custom feature, which can pull variables from one part studio to another. That's not what I'd do though.

  • @ryanblackwood1753
    @ryanblackwood1753 2 ปีที่แล้ว

    Do you know how to script the 'Variable type' and 'Description'?
    I've done a lot of searching, but haven't found anything on how to get the script to populate the 'Variable type' and 'Description' in the part studio I'm working in.
    I found another example of this "Global Variable" script a while ago and it's been crazy helpful, but it would be nice if I didn't have to pull up my script to see what each variable is.
    I currently have over 300 variables in my script that I use across 3 documents that are different components of the same giant project. So I use a ton of shorthand in my names to keep them less than 8 characters if possible, but it also means I end up with a lot that are very similar and I forget which is which without looking at the comments in my script.
    My version uses ' "name" : #*inch, //description ' for each variable. I'm hoping it's only a matter of punctuation and a minor modification to the for loop to do this.

    • @ovyl
      @ovyl  2 ปีที่แล้ว

      I'm not sure how to set the types, but you can add a description fairly simply. Just add a comma and string to the variable as you set it: setVariable(context is Context, "variableName", 1*inch, "This is the description of the variable.");
      Here's an actual example: cad.onshape.com/documents/383c06e24837a21d66cc58c3/w/ef1f83ea2ab84c9118f8e630/e/5d928f70adafaa7ecb4b294b
      Since your 300+ variables are is already setup, with the //comments, maybe there would be a clever regex script you could use in the find/replace tool to automate it. It probably wouldn't save you time if you don't already have regex experience, but you'd learn something new.
      For example, starting with a variable like this: setVariable(context, "myVariable", 1*inch); //comment
      In the find/replace dialogue in the feature studio, with the regex search on (click the .* button), I searched "([)][^)]*[//])(.+)" and replaced it with ", "$2" );"
      it produces this, instead: setVariable(context, "myVariable", 1*inch, "comment" );

    • @ryanblackwood1753
      @ryanblackwood1753 2 ปีที่แล้ว

      @@ovyl Thanks for the quick reply. It says I don't have permissions to that workspace.
      I tried the example you give, but it's giving me an error and failing to generate when I try to put a description
      My Code:
      const VALUES = {
      "Name" : # * inch, //This is my existing variable
      "Name" : # * inch, "1st description attempt", //This causes an error, says looking for ": "
      "Name" : # * inch : "2nd description attempt", //This causes an error, says looking for ", "
      "Name" : # * inch, "3rd description attempt" //This causes an error, says looking for ", "
      };
      annotation { "Feature Type Name" : "Global Dimensions" }
      export const setGlobals = defineFeature(function(context is Context, id is Id, definition is map)
      precondition
      {
      }
      {
      for (var value in VALUES)
      setVariable(context, value.key, value.value, value.description);
      });

    • @ovyl
      @ovyl  2 ปีที่แล้ว +1

      @@ryanblackwood1753 oh, since you're declaring them into a map and iterating over them, you'll have to actually make each entry contain the variable value and the comment. One way is to put the variable value and comment string into an array like this, and address them in the loop using a 0 or 1
      var v = {};
      v.varOne = [1*inch, "this string describes variables one"];
      v.varTwo = [2*inch, "this string describes variables two"];
      for (var key, value in v)
      {
      setVariable(context, key, value[0], value[1]);
      }

    • @ovyl
      @ovyl  2 ปีที่แล้ว +1

      cad.onshape.com/documents/631ca2ee3804e7ad40c1f88d/w/141131493bff582c39f33431/e/37fe516070fc2a03f750b833

    • @ryanblackwood1753
      @ryanblackwood1753 2 ปีที่แล้ว

      @@ovyl This worked great!
      Pretty easy to replace all and modify my existing script.
      Much Appreciated!

  • @MSWindman
    @MSWindman 22 วันที่ผ่านมา

    As a retired developer, I cringe at the global variables.

  • @MarcelRobitaille
    @MarcelRobitaille 3 ปีที่แล้ว

    What happened to your name?

    • @ovyl
      @ovyl  3 ปีที่แล้ว

      upgrade 😎

  • @dbuezas
    @dbuezas 23 ชั่วโมงที่ผ่านมา

    I keep forgetting heated insert hole diameters, will definitely put them in a custom feature. Thanks for these advanced tips!

  • @jimmulawin6124
    @jimmulawin6124 3 ปีที่แล้ว

    That's amazing! how about linking data from csv to a part studio property?

    • @evanreese8592
      @evanreese8592 3 ปีที่แล้ว

      A prolific Onshaper, John, made this feature that turns a csv into variables (I made the icon for it though 😄). Give it a shot. cad.onshape.com/documents/75dfbb9091b5bea038398789/v/2d6edbf047281ce828b3923f/e/97a921379d1437bdaef7fcdf

    • @jimmulawin6124
      @jimmulawin6124 3 ปีที่แล้ว

      @@evanreese8592 that was simply amazing :) thanks, wanted to learn this lesson. Will you feature some contents like this?

    • @evanreese8592
      @evanreese8592 3 ปีที่แล้ว

      @@jimmulawin6124 I'd like to make some content about my favorite custom features from myself and others at some point. Not sure when though.