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: 2,012 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;
}



Learn more about the Gospel of Christ  ( Here )

CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴

Best Answer

Answers

Sign In or Register to comment.