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.

Global Variables - Feature Script

3D3D OS Professional Posts: 52 ✭✭
Can anyone offer advise on the possibility / components for / method of making a global variable using FeatureScript?

Answers

  • lemon1324lemon1324 Member, Developers Posts: 225 EDU
    What scope are you actually thinking of? The two things that pop to mind are getting/setting a variable in the context and using a box to pass by reference.

    In my thoughts on programming, there's very very few cases in which the best way to implement something is with an actual global variable; usually it's cleaner to pass by reference or use getters/setters.
    Arul Suresh
    PhD, Mechanical Engineering, Stanford University
  • 3D3D OS Professional Posts: 52 ✭✭
    edited August 2016
    How do you make a variable in a Part Studio available / reference-able in an Assembly?
  • lemon1324lemon1324 Member, Developers Posts: 225 EDU
    Not with FeatureScript; for security (and probably other) reasons, FS scripts are sandboxed to only affect the Part Studio they're used in.  It will probably be possible to use the full API to access variables in Assembly, but I haven't had time to look at what's available there.
    Arul Suresh
    PhD, Mechanical Engineering, Stanford University
  • lougallolougallo Member, Moderator, Onshape Employees, Developers, csevp Posts: 2,005
    A workaround would be to create variables that drive a sketch and then snap a version and derive it in other documents. Many docs can use that sketch and by changing it and snapping a new version you can give the feeling of a global variable. 

    FS could be done in a similar way but it would be a workaround leveraging linked documents. 
    Lou Gallo / PD/UX - Support - Community / Onshape, Inc.
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,210
    There is currently no way to reference variables in assemblies (FS or otherwise) -- this is on our todo list.  For variables global to multiple part studios, like Arul suggests, having a single custom feature set all your variables and then using that feature at the beginning of every part studio is the workaround I would recommend.  We do plan on allowing true global variables (within a document) in the future.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • axel_svenssonaxel_svensson Member Posts: 2
    There is currently no way to reference variables in assemblies (FS or otherwise) -- this is on our todo list.  For variables global to multiple part studios, like Arul suggests, having a single custom feature set all your variables and then using that feature at the beginning of every part studio is the workaround I would recommend.  We do plan on allowing true global variables (within a document) in the future.
    It will require some thought to make this play nicely with assemblies that use parts with configurations.
    With part studio configurations, part variables become scoped to the part instance in the assembly. If other parts in the assembly depend on such a variable, I don't see how simple global variables would be a solution. (For example, with two instances of the same part with different configurations in the same assembly, two values for the same variable can be at play in the same assembly context.)

    Maybe the assembly "Insert" functionality could somehow be extended to import variables, as well as sketches and parts?

  • shawn_updegraffshawn_updegraff Member Posts: 3 ✭✭✭
    Any progress on this? It looks like closing in on 3 years on the last comment, 4-5 years on the original question.
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,935 PRO
    I believe they are working on a solution to this, at least many Onshape staff have agreed they want it too, but they can't tell us where it is on the priority list or give any indication on when new features will be available, as is the norm. They would hate to promise a  feature on a date, then find there are crippling bugs associated with it. It's better to just be surprised one day with a well thought out plan, rather than a band-aid feature that barely works. I mean look at configurations, that took a while, once it was released for assemblies also, it became the best configurator in the business.

    I'd love to have variables in asm, mostly for defining patterns. But today there enough work arounds to get 80% there.
    If you have a particular need, you should post it. We'll see if there is an elegant work around available.
  • Alex_KempenAlex_Kempen Member Posts: 248 EDU
    edited December 2020
    As I've stated in a recent thread, I actually have an advanced FeatureScript for this currently in the works, which uses setAttributes to read off attributes set with a custom built FS in another part studio. I plan on releasing it sometime in the next week or so. You can find more info about it in this forum thread.
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • jeffrey_hawkinsjeffrey_hawkins Member Posts: 9 EDU
    Unsure where to report this, but there is an odd bug in using a FeatureScript as a method of having globals... global variables (even if names are unique) cannot be combined with local variables.

    I have a FeatureScript with parameters for the tolerance needed for a tight fit vs. a loose fit for example. If I put all variables in (i.e. #diameter here), I can have one tube outside diameter specified as something like "#diameter - #gapLoose". But if I make a one-off tube where the variable is declared locally as #mountTube, I cannot set anything to be (#mountTube - #gapLoose). No error is displayed, but it turns red and fails.. 
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,210
    @jeffrey_hawkins I'd start by checking that the units on mountTube and gapLoose match.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • wout_theelen541wout_theelen541 Member, csevp Posts: 198 PRO
    I believe they are working on a solution to this, at least many Onshape staff have agreed they want it too, but they can't tell us where it is on the priority list or give any indication on when new features will be available, as is the norm. They would hate to promise a  feature on a date, then find there are crippling bugs associated with it. It's better to just be surprised one day with a well thought out plan, rather than a band-aid feature that barely works. I mean look at configurations, that took a while, once it was released for assemblies also, it became the best configurator in the business.

    I'd love to have variables in asm, mostly for defining patterns. But today there enough work arounds to get 80% there.
    If you have a particular need, you should post it. We'll see if there is an elegant work around available.
    I would really love this feature in 2023!
    I have some configurable plates that connect two parts which themselves are configurable. My approach for now is just going to be to have a limited selection of configurations and I'll manually measure the distance to connect the two parts to connect for each configuration. Having variables would unlock a lot of other possibilities too, instead of defining distances for mates offset or sliding mates you could use the variable and it would just be such a cleaner workflow.
  • MichaelPascoeMichaelPascoe Member Posts: 1,979 PRO
    edited December 2022

    @wout_theelen541 They implemented Global Variables in June 2022. This is not with FeatureScript, but it is global variables.
    https://forum.onshape.com/discussion/18569/improvements-to-onshape-june-27th-2022/p1


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • wout_theelen541wout_theelen541 Member, csevp Posts: 198 PRO
    I guess I was a bit vague, I was meaning to measure geometry in an assembly.
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,935 PRO
    The variable feature can save a measurement to a variable too. 

    You can use super derive to import that variable into other part studios. 

    You just can't import that variable into an assembly. 
Sign In or Register to comment.