Pre selection logic?

MichaelPascoe
MichaelPascoe Member Posts: 2,965 PRO
edited March 2021 in FeatureScript
I'm having trouble understanding how to export a function. Can someone give me some pointers?

Here is what I'm trying to do:
If you have pre selected a plane and a vertex, then click the feature, the feature will detect that you are wanting to use the "Up to vertex" method. I'm trying export these definitions so that the interface will show you what has been selected. When I'm not trying to export the definitions, the code works great, but I wan't the user to see which method is selected.

A summary of my attempt:
https://cad.onshape.com/documents/bdffe7bf48f3b4155e85d073/w/4aa864c3858500b61d8570f5/e/433e168b78be95d2d6e248ec 



annotation { "Feature Type Name" : "Section", "Auto Section Function" : "autoSection"}
export const section = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation { "Name" : "Plane or face", "Filter" : GeometryType.PLANE || EntityType.VERTEX, "MaxNumberOfPicks" : 2 }
        definition.rootPlane is Query;

        //Required to create a drop down menu.
        annotation { "Name" : "Section method" }
        definition.sectionMethod is sectionMethod;

        if (definition.sectionMethod == sectionMethod.UPTOVERTEX)
        {
            annotation { "Name" : "Up to vertex", "Filter" : (EntityType.VERTEX), "MaxNumberOfPicks" : 1 }
            definition.limitEntity is Query;
        }
    }

export function autoSection(context is Context, id is Id, oldDefinition is map, definition is map) returns map
{
    if (oldDefinition != {}) // Only do anything on preselection
        return definition;

    //When I'm not trying to export, the following logic works great.
    if (size(evaluateQuery(context, definition.rootPlane)) == 2)
        {

            const autoVertex = qEntityFilter(definition.rootPlane, EntityType.VERTEX);
            const autoPlaneOrFace = qSubtraction(definition.rootPlane, autoVertex);

            if (size(evaluateQuery(context, autoVertex)) == 1 && size(evaluateQuery(context, autoPlaneOrFace)) == 1)
            {
                definition.sectionMethod = sectionMethod.UPTOVERTEX;
                definition.limitEntity = autoVertex;
                definition.rootPlane = autoPlaneOrFace;
            }

        }
    
    return definition;
}



RENDERCAD - Photorealistic product rendering.
CADSharp - Professional CAD + software consulting.

▚▞▚▞▚▞▚▞▚

Best Answer

  • Alex_Kempen
    Alex_Kempen Member Posts: 260 EDU
    Answer ✓
    Wait, really? IIRC, there should be an option to mark a post as an answer if you've created the question. Maybe I'm wrong though?
    Software Developer at Epic Systems
    FRC Design Mentor - Team 10553 Orange Overdrive


Answers

  • TimRice
    TimRice Member, Moderator, Onshape Employees Posts: 315 image
    I dont know the answer here but it may be worth looking at the bottom of the Plane source code to see how we use editing logic to change the feature based on preselections: 
    https://cad.onshape.com/documents/12312312345abcabcabcdeff/w/a855e4161c814f2e9ab3698a/e/577e2aaf45a542f98d9343c7 
    Tim Rice | User Experience | Support 
    Onshape, Inc.
  • MichaelPascoe
    MichaelPascoe Member Posts: 2,965 PRO
    That is what I have been referencing, but I still don't seem to have it down.

    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • Alex_Kempen
    Alex_Kempen Member Posts: 260 EDU
    edited March 2021
    From a quick glance at your code, it seems you're not actually defining your editing logic function properly in your feature definition.
    annotation { "Feature Type Name" : "Section", "Auto Section Function" : "autoSection"}
    export const section = defineFeature(function(context is Context, id is Id, definition is map)
    It seems you've accidentally written "Auto Section Function" instead of "Editing Logic Function", so your editing logic function isn't actually running.

    As a side note, one common technique I like to use when troubleshooting code is to insert print statements into sections of the code. I find this very helpful for troubleshooting since it can quickly tell you if a part of your code is actually running (but not behaving properly), which can help rule out possible issues. You can also print out values to see what information your FeatureScript is actually processing.

    Edit: I had a brainfart and forgot how isCreating works. If you don't include it, the editing logic function only runs when a feature is created (i.e. before you click the checkmark once). Otherwise, the editing logic function will always run, regardless of whether the feature has been saved or not.


    Software Developer at Epic Systems
    FRC Design Mentor - Team 10553 Orange Overdrive


  • MichaelPascoe
    MichaelPascoe Member Posts: 2,965 PRO
    @Alex_Kempen That did it. Thank you!

    I can't go back and update "yes" this answered my question. If you post it again, I will click accept the answer, for the record.

    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • Alex_Kempen
    Alex_Kempen Member Posts: 260 EDU
    Answer ✓
    Wait, really? IIRC, there should be an option to mark a post as an answer if you've created the question. Maybe I'm wrong though?
    Software Developer at Epic Systems
    FRC Design Mentor - Team 10553 Orange Overdrive


  • MichaelPascoe
    MichaelPascoe Member Posts: 2,965 PRO
    edited March 2021
    There is, but I clicked no too soon... I made a typo when trying what you said, your answer worked perfectly.

    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • MichaelPascoe
    MichaelPascoe Member Posts: 2,965 PRO
    Also, @TimRice, your answer was good too, I appreciate the pointers!

    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚