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.

Pre selection logic?

MichaelPascoeMichaelPascoe Member Posts: 599 PRO
edited March 12 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;
}


Best Answer

Answers

  • TimRiceTimRice Member, Moderator, Onshape Employees Posts: 312
    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.
  • MichaelPascoeMichaelPascoe Member Posts: 599 PRO
    That is what I have been referencing, but I still don't seem to have it down.
  • Alex_KempenAlex_Kempen Member Posts: 203 EDU
    edited March 12
    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.


  • MichaelPascoeMichaelPascoe Member Posts: 599 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.
  • Alex_KempenAlex_Kempen Member Posts: 203 EDU
    Accepted 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?
  • MichaelPascoeMichaelPascoe Member Posts: 599 PRO
    edited March 12
    There is, but I clicked no too soon... I made a typo when trying what you said, your answer worked perfectly.
  • MichaelPascoeMichaelPascoe Member Posts: 599 PRO
    Also, @TimRice, your answer was good too, I appreciate the pointers!
Sign In or Register to comment.