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

Finding feature ID from Featurelist?

Evan_ReeseEvan_Reese Member Posts: 2,064 PRO
I'm working on a feature with a feature query field that gives me a FeatureList. From that, how do I find a feature ID that I can use in qCapEntity, for example?
Evan Reese / Principal and Industrial Designer with Ovyl
Website: ovyl.io

Answers

  • Options
    konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    featurelist is map with structure {id: function, ...}. so to get the id of the first entry you can like this: id0 = keys(featureList)[0];
  • Options
    MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,034 EDU
    edited November 2020
    @Evan_Reese

    If you use valuesSortedById [1], and then take the first entry of that, that will give a correct first id (actually, value. See below). If you use keys(featureList), that will give you the first id alphabetically, not actually the first feature.

    [1]: valuesSortedById(context is Context, idToValue is map) returns array

    E.g: const first = valuesSortedById(context, featureList)[0];<br>
    EDIT: I just realised that this is values, not keys. See my comment below :/
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • Options
    konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    edited November 2020
    @MBartlett21 Doesn't valuesSortedById() return the array of values of featurelist itself, meaning it is not an array of separate maps [{id_i: function_i},...], but an array of functions sorted in historical order [function_i, ...]?
    According to it's usage in applyPattern() it only returns array of feature functions:

    So if Evan is going to deal with featurelist of several instances I would reccomend to try this:

    id_i = featurelist[i].key;

  • Options
    MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,034 EDU
    Ok. Just realised that :/

    If it is something other than qCapEntity that takes a Query, qCreatedBy(FeatureList) [1] could be used to turn it into a Query then.

    [1]: qCreatedBy(features is FeatureList) returns Query
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,064 PRO
    Thanks to both of you. keys(featurelist)[0] worked for me. Feature order doesn't matter for what I'm doing. I was headed in the right direction by checking out the featurelist content with println, and trying to get the keys of the map, but hadn't used the keys function before and wasn't aware of it.
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • Options
    Alex_KempenAlex_Kempen Member Posts: 244 EDU
    You can use an overload of qCreatedBy to get a query for all of the things created by your selected features. Depending on the complexity of the features, you might then be able to use lastModifyingOperationId to get the id of the feature which last operated on your query. This code worked for me:
            var solids = qCreatedBy(definition.featureList, EntityType.BODY);
            var solidId = lastModifyingOperationId(context, qNthElement(solids, 0));
            
            debug(context, qNonCapEntity(solidId, EntityType.FACE));
    You might also be able to use query modifiers like qDependency() to get more complex subFeatureIds, although I can't speculate to much since I haven't personally used FeatureLists for any projects (yet).
    Lastly, if you're using the FeatureList to choose functions which you've made through FeatureScript, then you can use setAttributes and getAttributes to retrieve the information attached to them, including things like specific feature ids. That's probably the most robust way to handle this, but it obviously requires you to be selecting non-std FeatureScripts.
    CS Student at UT Dallas
    Alex.Kempen@utdallas.edu
    Check out my FeatureScripts here:



Sign In or Register to comment.