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.

Pull selections from a previous feature into new feature

shawn_crockershawn_crocker Member, OS Professional Posts: 798 PRO
I have modified the sheet metal model feature and adding an enum selection for selecting a material which sets the bend parameters to predetermined values. I now want to modify the finish sheet metal feature to set some custom properties of the finished parts. Does anyone know how I can pull in the previous enum material selection from the parent sheet metal model feature?
Tagged:

Best Answer

  • Alex_KempenAlex_Kempen Member Posts: 244 EDU
    Answer ✓
    Both my (Updated) Plate FeatureScript and my Variable Library FeatureScript use setAttribute and getAttribute to pull in information from other versions of said feature. Once a feature has set attributes on itself, the question of when you pull in said attributes is up to you; my plate FeatureScript does it in the editing logic, so that it can fill in the feature definition with an exact copy of the other plate's UI (so you don't have to manually recreate the entire plate just to make one or two changes), while the Variable Library FeatureScript does it in the feature body since only its output depends on what it reads off. 
    // in the sheet metal feature
    setAttribute(context, {
                    "entities" : qCreatedBy(id + "sheetMetal", EntityType.BODY),
                    "name" : "sheetMetalModel",
                    "attribute" : definition
                });
    
    // in the finish sheet metal model feature
    // use getAttributes to get attributes from multiple queries, otherwise use getAttribute for only one at a time
    const sheetMetalDef = getAttribute(context, {
                                "entity" : definition.myQuery,
                                "name" : "sheetMetalModel"
                            });
    if (sheetMetalDef != undefined)
    {
        // successful read; use sheetMetalDef to figure out output
    }
    else
    {
        // failed to read; finish sheet metal model as normal and optionally display error message
    }

    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



Answers

  • Alex_KempenAlex_Kempen Member Posts: 244 EDU
    If it’s your own sheet metal feature, you can set an attribute with either the entire feature definition or just the value of the enum (if selected). You can then check for that attribute with the finish sheet metal feature and adjust behavior accordingly.
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • shawn_crockershawn_crocker Member, OS Professional Posts: 798 PRO
    Thanks again @Alex_Kempen.  I really wasn't sure what to search for.  Ended up seeing how the syntax is used in one of your documents.  Would I be right in thinking, on the other end, I would be using getAttributes to pull in the values to another custom feature down the line after collecting a query of the bodies that have the attributes in them?  I haven't seen any code yet of people retrieving the attributes.
  • Alex_KempenAlex_Kempen Member Posts: 244 EDU
    Answer ✓
    Both my (Updated) Plate FeatureScript and my Variable Library FeatureScript use setAttribute and getAttribute to pull in information from other versions of said feature. Once a feature has set attributes on itself, the question of when you pull in said attributes is up to you; my plate FeatureScript does it in the editing logic, so that it can fill in the feature definition with an exact copy of the other plate's UI (so you don't have to manually recreate the entire plate just to make one or two changes), while the Variable Library FeatureScript does it in the feature body since only its output depends on what it reads off. 
    // in the sheet metal feature
    setAttribute(context, {
                    "entities" : qCreatedBy(id + "sheetMetal", EntityType.BODY),
                    "name" : "sheetMetalModel",
                    "attribute" : definition
                });
    
    // in the finish sheet metal model feature
    // use getAttributes to get attributes from multiple queries, otherwise use getAttribute for only one at a time
    const sheetMetalDef = getAttribute(context, {
                                "entity" : definition.myQuery,
                                "name" : "sheetMetalModel"
                            });
    if (sheetMetalDef != undefined)
    {
        // successful read; use sheetMetalDef to figure out output
    }
    else
    {
        // failed to read; finish sheet metal model as normal and optionally display error message
    }

    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • shawn_crockershawn_crocker Member, OS Professional Posts: 798 PRO
    @Alex_Kempen
    Thanks for that clarity.  I have been able to set and get attributes.  I have been able to get an attribute and assign it to a variable (definition.thickness).  I have been able to prinln the variable and see it printed but I seem to be unable to use the variable for calculation.  It seems like getAttributes is pulling in the value as a string.  When I try to use "as valueWithUnits" I get a message in the console "Array index must be number, value is string".  I'm not sure how to bring in the attribute so that it is a number value.  If I just bring it in without trying to assign it as a type, The whole feature fails as I'm guessing the functions using "definition.thickness" cannot use the type of value that is being assigned to it.
  • shawn_crockershawn_crocker Member, OS Professional Posts: 798 PRO
    @Alex_Kempen
    I think I found my problem.  I need to drop the getAttribute into a variable.  Then assign definition.thickness with the variable and the array reference "[0]" or "[0].somthing if the setAttribute was assigned a map.  I didn't realize it was putting the map I was setting into the first location of an array.
  • Alex_KempenAlex_Kempen Member Posts: 244 EDU
    edited June 2021
    getAttribute retrieves an attribute from a single entity, while getAttributes retrieves attributes from multiple entities. Because getAttributes retrieves multiple attributes, it returns an array of attributes, even if only a single entity is passed in. This gives it flexibility. That's why you're getting an array index error; FeatureScript expects you to provide an index into the array returned by getAttributes, but your code is instead passing in a string because you're trying to get into the inner definition map first. Loop through the array returned by getAttributes or switched to getAttribute to get your feature to behave as expected.
    const myAttributes = getAttributes(context, ...);
    // myAttributes = [attribute1, attribute2...]
    println(myAttributes[0].thickness); // prints a value with units as expected
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



  • shawn_crockershawn_crocker Member, OS Professional Posts: 798 PRO
    I see.  I didn't notice that other singular attribute function.  Thanks @Alex_Kempen

Sign In or Register to comment.