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.

Setting UIHint parameters from feature edit function

graham_lockgraham_lock Member Posts: 142 PRO
edited October 27 in FeatureScript

Hi,

Is there a way to apply UIHints from code - for example make a field read only within the feature edit function if certain other criterion are met?

Thank you.

Best Answer

  • Caden_ArmstrongCaden_Armstrong Member Posts: 177 PRO
    Answer ✓

    No, you cannot set UIHints dynamically.

    But…there is a work around with editing logic.

    Make two identical fields, one that is read only another that is not read only.

    Make another field that has UIHint.Always_hidden, is a boolean, and controls the visibility of the other two fields.

    When the read-only condition is met, set that field with editing logic so that the editable field is hidden and the read only is visible.

    Example:
    https://cad.onshape.com/documents/8477b927bcadbb9f95b06c09/w/7544e4a05c45a8f6578681b0/e/852cedb2769a0877d1e4dafe

    export function EditingLogicFunction(context is Context, id is Id, olddefinition is map, definition is map, isEditing is boolean) returns map
    {
    // condition for visibility
    definition.isVisible = definition.myLengthControl > 50*millimeter;
    // Update the value
    definition.myLengthReadOnly = definition.myLength;
    return definition;
    
    }
    annotation { "Feature Type Name" : "My Feature", "Feature Type Description" : "", "Editing Logic Function":"EditingLogicFunction" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation { "Name" : "Controls other field readonly" }
        isLength(definition.myLengthControl, LENGTH_BOUNDS);
    
        annotation { "Name" : "control Visiblity", "UIHint" : UIHint.ALWAYS_HIDDEN }
        definition.isVisible is boolean;
    
    
        if (definition.isVisible)
        {
            annotation { "Name" : "My field" }
            isLength(definition.myLength, LENGTH_BOUNDS);
        }
        if (!definition.isVisible)
        {
            annotation { "Name" : "My field", "UIHint" : UIHint.READ_ONLY }
            isLength(definition.myLengthReadOnly, LENGTH_BOUNDS);
        }
    
    
    }
    {
        // Define the function's action
    });
    
    
    

    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications

Answers

  • Caden_ArmstrongCaden_Armstrong Member Posts: 177 PRO
    Answer ✓

    No, you cannot set UIHints dynamically.

    But…there is a work around with editing logic.

    Make two identical fields, one that is read only another that is not read only.

    Make another field that has UIHint.Always_hidden, is a boolean, and controls the visibility of the other two fields.

    When the read-only condition is met, set that field with editing logic so that the editable field is hidden and the read only is visible.

    Example:
    https://cad.onshape.com/documents/8477b927bcadbb9f95b06c09/w/7544e4a05c45a8f6578681b0/e/852cedb2769a0877d1e4dafe

    export function EditingLogicFunction(context is Context, id is Id, olddefinition is map, definition is map, isEditing is boolean) returns map
    {
    // condition for visibility
    definition.isVisible = definition.myLengthControl > 50*millimeter;
    // Update the value
    definition.myLengthReadOnly = definition.myLength;
    return definition;
    
    }
    annotation { "Feature Type Name" : "My Feature", "Feature Type Description" : "", "Editing Logic Function":"EditingLogicFunction" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation { "Name" : "Controls other field readonly" }
        isLength(definition.myLengthControl, LENGTH_BOUNDS);
    
        annotation { "Name" : "control Visiblity", "UIHint" : UIHint.ALWAYS_HIDDEN }
        definition.isVisible is boolean;
    
    
        if (definition.isVisible)
        {
            annotation { "Name" : "My field" }
            isLength(definition.myLength, LENGTH_BOUNDS);
        }
        if (!definition.isVisible)
        {
            annotation { "Name" : "My field", "UIHint" : UIHint.READ_ONLY }
            isLength(definition.myLengthReadOnly, LENGTH_BOUNDS);
        }
    
    
    }
    {
        // Define the function's action
    });
    
    
    

    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
  • graham_lockgraham_lock Member Posts: 142 PRO

    Thank you for answering and taking the time to generate an example, much appreciated.

Sign In or Register to comment.