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: 51 PRO
Can anyone offer advise on the possibility / components for / method of making a global variable using FeatureScript?

Answers

  • lemon1324lemon1324 Member, Developers Posts: 160 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 Candidate at Stanford University
  • 3D3D OS Professional Posts: 51 PRO
    edited August 2016
    How do you make a variable in a Part Studio available / reference-able in an Assembly?
  • lemon1324lemon1324 Member, Developers Posts: 160 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 Candidate at Stanford University
  • lougallolougallo Member, Administrator, Moderator, Onshape Employees, Developers Posts: 1,631
    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: 912
    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 \ Director of 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?

Sign In or Register to comment.