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

Struggling with partstudio import "namespace"... (plus importing variable values from partstudio)

eric_pestyeric_pesty Member Posts: 1,515 PRO
I'm working on some "conceptual" featurescript that would involve importing sketches from different part studios based on user input (but the user doesn't directly select the part studio).
Basically the user would input a string and the featurescript would grab a different sketch for each character in the string and apply some transforms (with some base values corresponding to each "sketch").

I'm ignoring the whole string and selection for now and just jumping ahead to importing the sketch I understand I need to name an import and use that namespace later:

Import1::import(path etc...);
Import2::import(path2 etc...);
The part where I am getting stuck is how to "select" the correct import to instantiate. Basically how do I "set" which one to use when I do something like:
addInstance(instantiator, Importx::build,{etc...}
I can't seem to use a variable instead of the "Importx" part above.

It seems like if I got the user to select a part studio I could use something like "definition.mypartstudio::build" but I'm unclear on how to create a variable that would reference the imported studio in the same way...

The second part I was hoping to do was to import the variables contained within the partstudio and do some math on that but I don't see a way to achieve that...


An alternate option I am considering is creating a structure (maybe an enum or table of some sort, I have a CSV with the values to match with each character so I would just need to add the reference part studio) that would store the values I need (instead of variables) as well as the corresponding "import" but I am not sure if/how that could be done...
If I had that I would just have to retrieve the correct sketch and associated values for each character in the string...


Best Answer

  • Options
    ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,178
    Answer ✓
    There's no way to use a variable as a namespace.  You could create a map with the various build functions, e.g.
    const nameToBuildFunction = { 1 : Import1::build, 2 : Import2::build, ... };
    and then look up the correct function in the map.  To get the variables in the part studio, you need to get the context it produces (run the build function) and call getVariable on it.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc

Answers

  • Options
    ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,178
    Answer ✓
    There's no way to use a variable as a namespace.  You could create a map with the various build functions, e.g.
    const nameToBuildFunction = { 1 : Import1::build, 2 : Import2::build, ... };
    and then look up the correct function in the map.  To get the variables in the part studio, you need to get the context it produces (run the build function) and call getVariable on it.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Options
    eric_pestyeric_pesty Member Posts: 1,515 PRO
    edited May 2023
    @ilya_baran
    Thanks that helps...
    I am able to create a variable "importedPS=import1::build" and then use that in the instantiator so that makes sense. The map idea above could work, I'll think about it, I'm guessing I should be able to add the values in there too if that made sense...

    Can you elaborate on calling getVariable on "it"? How would I "getVariable" from the "importedPS" above? My fuzzy understanding makes me think the "context" part is where I have to point to importedPS but haven't had any luck yet...

    Any thoughts on how I could put the import reference in a table or is that a bad idea?
  • Options
    eric_pestyeric_pesty Member Posts: 1,515 PRO
    Update:
    I figured out my issue with the getVariable!

    I had tried:
    getVariable(importedPS, "variablename");

    But I finally figured out I needed to add the "()" to get the actual context:
    getVariable(importedPS(), "variablename");

    The map looks like it's going to work fine, I moved it to a separate featurestudio to clean things up and this would make it reasonably easy to "swap out" different "sets" in the future (if that came up...).
Sign In or Register to comment.