Skip to content

Variable

This module defines data models for variables used in Onshape documents retrieved from Onshape REST API responses.

The data models are implemented as Pydantic BaseModel classes, which are used to

1. Parse JSON responses from the API into Python objects.
2. Validate the structure and types of the JSON responses.
3. Provide type hints for better code clarity and autocompletion.

These models ensure that the data received from the API adheres to the expected format and types, facilitating easier and safer manipulation of the data within the application.

Models
  • Variable: Represents a variable used in Onshape's Variable Studio.
Enum
  • VARIABLE_TYPE: Enumerates the possible variable types in Onshape (LENGTH, ANGLE, NUMBER, ANY).

VARIABLE_TYPE

Bases: str, Enum

Enumerates the possible variable types in Onshape

Attributes:

Name Type Description
LENGTH str

Length variable type

ANGLE str

Angle variable type

NUMBER str

Number variable type

ANY str

Any variable type

Examples:

>>> VARIABLE_TYPE.LENGTH
'LENGTH'
>>> VARIABLE_TYPE.ANGLE
'ANGLE'
Source code in onshape_robotics_toolkit\models\variable.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class VARIABLE_TYPE(str, Enum):
    """
    Enumerates the possible variable types in Onshape

    Attributes:
        LENGTH (str): Length variable type
        ANGLE (str): Angle variable type
        NUMBER (str): Number variable type
        ANY (str): Any variable type

    Examples:
        >>> VARIABLE_TYPE.LENGTH
        'LENGTH'
        >>> VARIABLE_TYPE.ANGLE
        'ANGLE'
    """

    LENGTH = "LENGTH"
    ANGLE = "ANGLE"
    NUMBER = "NUMBER"
    ANY = "ANY"

Variable

Bases: BaseModel

Represents a variable used in Onshape's Variable Studio.

JSON
    {
        "type": "ANGLE",
        "name": "forkAngle",
        "value": null,
        "description": "Fork angle for front wheel assembly in deg",
        "expression": "15 deg"
    }

Attributes:

Name Type Description
type str

The type of the variable (LENGTH, ANGLE, NUMBER, ANY).

name str

The name of the variable.

value str

The value of the variable.

description str

The description of the variable.

expression str

The expression of the variable.

Examples:

>>> variable = Variable(
...     type="ANGLE",
...     name="forkAngle",
...     value=None,
...     description="Fork angle for front wheel assembly in deg",
...     expression="15 deg"
... )
>>> variable
Variable(
    type='ANGLE',
    name='forkAngle',
    value=None,
    description='Fork angle for front wheel assembly in deg',
    expression='15 deg'
)
Source code in onshape_robotics_toolkit\models\variable.py
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class Variable(BaseModel):
    """
    Represents a variable used in Onshape's Variable Studio.

    JSON:
        ```json
            {
                "type": "ANGLE",
                "name": "forkAngle",
                "value": null,
                "description": "Fork angle for front wheel assembly in deg",
                "expression": "15 deg"
            }
        ```

    Attributes:
        type (str): The type of the variable (LENGTH, ANGLE, NUMBER, ANY).
        name (str): The name of the variable.
        value (str, optional): The value of the variable.
        description (str, optional): The description of the variable.
        expression (str, optional): The expression of the variable.

    Examples:
        >>> variable = Variable(
        ...     type="ANGLE",
        ...     name="forkAngle",
        ...     value=None,
        ...     description="Fork angle for front wheel assembly in deg",
        ...     expression="15 deg"
        ... )
        >>> variable
        Variable(
            type='ANGLE',
            name='forkAngle',
            value=None,
            description='Fork angle for front wheel assembly in deg',
            expression='15 deg'
        )
    """

    type: str = Field(..., description="The type of the variable (LENGTH, ANGLE, NUMBER, ANY)")
    name: str = Field(..., description="The name of the variable")
    value: Union[str, None] = Field(None, description="The value of the variable")
    description: str = Field(None, description="The description of the variable")
    expression: str = Field(None, description="The expression of the variable")

    @field_validator("name")
    def validate_name(cls, value: str) -> str:
        """
        Validate the variable name to ensure it is not empty.

        Args:
            value (str): The variable name to validate.

        Returns:
            str: The validated variable name.

        Raises:
            ValueError: If the variable name is empty.
        """
        if not value:
            raise ValueError("Variable name cannot be empty")

        return value

    @field_validator("type")
    def validate_type(cls, value: str) -> str:
        """
        Validate the variable type to ensure it is one of the valid types.

        Args:
            value (str): The variable type to validate.

        Returns:
            str: The validated variable type.

        Raises:
            ValueError: If the variable type is not one of the valid types.
        """
        if value not in VARIABLE_TYPE.__members__.values():
            raise ValueError(f"Invalid variable type: {value}")

        return value

validate_name(value)

Validate the variable name to ensure it is not empty.

Parameters:

Name Type Description Default
value str

The variable name to validate.

required

Returns:

Name Type Description
str str

The validated variable name.

Raises:

Type Description
ValueError

If the variable name is empty.

Source code in onshape_robotics_toolkit\models\variable.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
@field_validator("name")
def validate_name(cls, value: str) -> str:
    """
    Validate the variable name to ensure it is not empty.

    Args:
        value (str): The variable name to validate.

    Returns:
        str: The validated variable name.

    Raises:
        ValueError: If the variable name is empty.
    """
    if not value:
        raise ValueError("Variable name cannot be empty")

    return value

validate_type(value)

Validate the variable type to ensure it is one of the valid types.

Parameters:

Name Type Description Default
value str

The variable type to validate.

required

Returns:

Name Type Description
str str

The validated variable type.

Raises:

Type Description
ValueError

If the variable type is not one of the valid types.

Source code in onshape_robotics_toolkit\models\variable.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@field_validator("type")
def validate_type(cls, value: str) -> str:
    """
    Validate the variable type to ensure it is one of the valid types.

    Args:
        value (str): The variable type to validate.

    Returns:
        str: The validated variable type.

    Raises:
        ValueError: If the variable type is not one of the valid types.
    """
    if value not in VARIABLE_TYPE.__members__.values():
        raise ValueError(f"Invalid variable type: {value}")

    return value