Setting UIHint parameters from feature edit function

graham_lock
graham_lock Member Posts: 277 PRO
edited October 2024 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_Armstrong
    Caden_Armstrong Member Posts: 472 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 --- Renaissance --- Kestrel -- SmartLink
    Experts in Onshape Automation - Custom Features and Integrated Applications

Answers

  • Caden_Armstrong
    Caden_Armstrong Member Posts: 472 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 --- Renaissance --- Kestrel -- SmartLink
    Experts in Onshape Automation - Custom Features and Integrated Applications
  • graham_lock
    graham_lock Member Posts: 277 PRO

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