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.

set default value for length and angle in annotation in feature script precondition

daniel_cravensdaniel_cravens Member Posts: 29
How do you set a custom default value in a length and angle precondition? I'd like the default length to be 10 mm and the default angle to be 65 degrees.

Comments

  • daniel_cravensdaniel_cravens Member Posts: 29
    edited July 8
    After searching, I learned the following.

    1. You can set default boolean and default string values like this:  annotation { "Name" : "myBool", "Default" : false}
    2. However, if you want to set other numbers, degrees, etc you need to use editing logic as in the below. ***

    *** This statement is incorrect. Defaults for numbers and degrees are possible as explained by NeilCooke below. The editing logic method works but is overkill for basic default values.

    This code sets the default values upon UI open and for the first dovetail joint.  It then sets the default for each subsequent joint based on the settings for the first joint. Also this is an example of how to implement an array variable in the precondition.

    Caveat: this works but is certainly not the most efficient implementation as I am merely a hobbiest.

    ****

    annotation { "Feature Type Name" : "2. BeginDovetail", "Editing Logic Function" : "onFeatureChange" }
    export const CreateDoveTail = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "edge for split", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
            definition.edge is Query;     
            
            annotation { "Name" : "Base" }
            isLength(definition.tBase, LENGTH_BOUNDS);       
           
            annotation { "Name" : "Height"}
            isLength(definition.tHeight, LENGTH_BOUNDS);   

            annotation { "Name" : "Flair Angle"}
            isAngle(definition.tAngle, ANGLE_360_BOUNDS);
            
                       
            annotation { "Name" : "Joints", "Item name" : "Variable", "Item label template" : "###name : #value" }
            
            definition.joints is array;
            
            for (var variable in definition.joints)
            {
                
                annotation { "Name" : "Offset"}
                isLength(variable.vOffset, LENGTH_BOUNDS);    
                
                annotation { "Name" : "Depth" }
                isLength(variable.vDepth, LENGTH_BOUNDS); 
            }
            

            
             
            
            annotation { "Name" : "Flip" }
            definition.flipped is boolean;
            
            
           
       
        }
        {
      
            opDoveTailSketch(context, id, definition);
                
                
            
        });    


    export function onFeatureChange(context is Context, id is Id, oldDefinition is map, definition is map,
        isCreating is boolean, specifiedParameters is map, hiddenBodies is Query) returns map
    {  
        const default_tHeight = .010 * meter;
        const default_vOffset = 0 * meter;
        const default_tAngle = 65 * degree;
        
        if(size(definition.joints) == 0)                           // set default height and angle as soon as UI opens (i.e. joints == 0)
        {
            definition.tHeight = default_tHeight;
            definition.tAngle = default_tAngle;
        }
        else
        {
            if (size(definition.joints) == 1)
            {
                
                if(size(oldDefinition.joints) == 0)           // set default for first joint
                {
                    println("Undefined");
                   definition.joints[0].vDepth = definition.tHeight/2;
                   definition.joints[0].vOffset = 0 * meter;
                }
            }

            // set default for each new joint to the values set for the prior joint

            else if (size(oldDefinition.joints) < size(definition.joints) && size(definition.joints) > 1)
            {
                definition.joints[size(definition.joints)-1] = definition.joints[size(definition.joints)-2];

            }
        }
        

        

        return definition;
    }
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,680
    annotation { "Name" : "Length" }
    isLength(definition.length, { (millimeter) : [-1e5, 0, 1e5] } as LengthBoundSpec);
    
    annotation { "Name" : "Angle" }
    isAngle(definition.angle, { (degree) : [-360, 0, 360] } as AngleBoundSpec);
    = [min, default, max]
    Senior Director, Technical Services, EMEAI
  • daniel_cravensdaniel_cravens Member Posts: 29
    Thank you!
Sign In or Register to comment.