Grouping Enum

Dylan_Stewart
Dylan_Stewart Member, Developers Posts: 107 PRO
Okay so it's been a little while since I've deep in to FS and I have another hurdle to jump. 

I am trying to group my enums instead of having a huge list. 

I have a lot of different sketch profiles that will always be together when imported in. I have to separate them due to the complexity of the profile so each part will extrude correctly. 


annotation {"Feature Type Name": "Group Enum"}
export const myGroupEnum = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
annotation {"Name":"Profile1"}
definition.profile_1 is 1profile;

annotation {"Name":"Profile2"}
definition.profile_2 is 2profile;

annotation {"Name":"Profile3"}
definition.profile_3 is 3profile;
}PROFILES::import(importedDoc/123456.....)

export enum 1profile
{
   annotation {"Name":"1"}
   123_0
}
<pre class="CodeBlock"><code>export enum 2profile
{
   annotation {"Name":"2"}
   456_0
}
<pre class="CodeBlock"><code>export enum 3profile
{
   annotation {"Name":"3"}
   789_0
}
This example only uses 3 profiles but I imagine I will have somewhere in the neighborhood of around 20-30...
Is there a way to 'bypass' the need for this in the precondition?

What would be better is to be able to group all of the profiles that will be used in to 1 enumerator.

As always, your help is greatly appreciated.  
Digital Engineering

Best Answer

  • mahir
    mahir Member, Developers Posts: 1,324 ✭✭✭✭✭
    Answer ✓
    @Dylan_Stewart, I'm not sure what the desired end result is, but I don't see why you can't just code the desired behavior into a branched if/else based on an initial selection.

    If (definition.profile =="123")
    ...import profiles 1, 2, 3
    else if (definition.profile == "456")
    ...import profiles 4, 5, 6
    else if (definition.profile == "789")
    ...import profiles 7, 8, 9

    Another option is to group your profiles into an array of sub arrays that separates and organizes all the various profiles.

    var profiles as array = [ [1a, 1b, 1c, 1d], [2a, 2b], [3a, 3b, 3c] ]

    Then, depending on which enum is selected, you can load all the profiles in either profiles[1], profiles[2], or profiles,[3].

Answers

  • kevin_o_toole_1
    kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565 image
    edited October 2016
    It sounds like you want a single enum defined with several options. With n profiles, you'll need n imports into n namespaces, but only one parameter and one list of options. Here' an example:
    
    FeatureScript 422;
    import(path : "onshape/std/geometry.fs", version : "422.0");
    
    // Created using import button above
    PROFILE_1::import(path : "b43fcf52027e641d12c6e75b", version : "b9ff861b36c4801a846c3058");
    PROFILE_2::import(path : "fad6bab7c868aa7808a486c2", version : "545623d9717aa6a186745075");
    PROFILE_3::import(path : "e81df29897b702fb3c66444f", version : "b889912552d569e0c391aa74");
    
    
    export enum Profiles
    {
        annotation {"Name":"1"}
        p123_0,
        annotation {"Name":"2"}
        p456_0,
        annotation {"Name":"3"}
        p789_0
        // etc.
    }
    
    annotation {"Feature Type Name": "Profile chooser"}
    export const profileChooser = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation {"Name" : "Profile"}
        definition.profile is Profiles;
        
        annotation { "Name" : "Size" }
        isLength(definition.size, LENGTH_BOUNDS);    
    }
    {
        println("Profile chosen is " ~ definition.profile);
        
        var profileContext;
        
        if (definition.profile == Profiles.p123_0)
            profileContext = PROFILE_1::build();
        if (definition.profile == Profiles.p456_0)
            profileContext = PROFILE_2::build();
        if (definition.profile == Profiles.p789_0)
            profileContext = PROFILE_3::build();
        // etc.
    
        // Delete construction planes and lines, leaving just a sketch region
        const profileRegion is Query = qConstructionFilter(
            qEverything(EntityType.FACE),
            ConstructionObject.NO
        );
        const profileBody is Query = qOwnerBody(profileRegion);
        opDeleteBodies(profileContext, id + "deleteBodies1", {
                "entities" : qSubtraction(qEverything(EntityType.BODY), profileBody)
        });
    
        opMergeContexts(context, id + "mergeContexts1", {
                "contextFrom" : profileContext
        });
    
        opTransform(context, id + "transform1", {
                "bodies" : qCreatedBy(id + "mergeContexts1", EntityType.BODY),
                "transform" : scaleUniformly(definition.size / (1 * inch))
        });
    
        opExtrude(context, id + "extrude1", {
                "entities" : qCreatedBy(id + "mergeContexts1", EntityType.FACE),
                "direction" : vector(0, 0, 1),
                "endBound" : BoundingType.BLIND,
                "endDepth" : 0.125 * inch
        });
    });
    
    And here's a document using this code: https://cad.onshape.com/documents/d7defad80f5b58678d7702f7/w/629ed705a4e5996689b1d2b9/e/335044dc6622d08bb86f8e9a


    Let us know if you have more questions!
  • Dylan_Stewart
    Dylan_Stewart Member, Developers Posts: 107 PRO
    Yes, that would allow you to make a choice between all of the imported profiles, but I need something that will build all of the profiles without having a huge list of 20 or so options.

    Grouping all profiles would be ideal, as there may be some options later on down the line and that's when I would like to introduce the idea of the drop-down menu. 

    Basically, I need to build a group of profiles automatically while only using 1 option from the menu. 
    Digital Engineering
  • mahir
    mahir Member, Developers Posts: 1,324 ✭✭✭✭✭
    Answer ✓
    @Dylan_Stewart, I'm not sure what the desired end result is, but I don't see why you can't just code the desired behavior into a branched if/else based on an initial selection.

    If (definition.profile =="123")
    ...import profiles 1, 2, 3
    else if (definition.profile == "456")
    ...import profiles 4, 5, 6
    else if (definition.profile == "789")
    ...import profiles 7, 8, 9

    Another option is to group your profiles into an array of sub arrays that separates and organizes all the various profiles.

    var profiles as array = [ [1a, 1b, 1c, 1d], [2a, 2b], [3a, 3b, 3c] ]

    Then, depending on which enum is selected, you can load all the profiles in either profiles[1], profiles[2], or profiles,[3].
  • Dylan_Stewart
    Dylan_Stewart Member, Developers Posts: 107 PRO
    @mahir

    I believe this is going to be my solution. Thank you!!
    Digital Engineering
  • mahir
    mahir Member, Developers Posts: 1,324 ✭✭✭✭✭
    No problem :smile:

  • lemon1324
    lemon1324 Member, Developers Posts: 230 EDU
    The other option may be to use a lookup table, as the Onshape hole feature does, but I'm not sure if you can use that to set non-numeric values.
    Arul Suresh
    PhD, Mechanical Engineering, Stanford University
  • kevin_o_toole_1
    kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565 image
    @lemon1324
    You can use a lookup table to look up any FeatureScript value, not just numbers. You could even have that value be a FeatureScript map with many values inside.