Version: 11.0

    Show / Hide Table of Contents

    Mastapy Examples

    These examples were written using mastapy 11.0. To use these examples, update both MASTA and mastapy to version 11.0. You can update the mastapy package to the correct version by entering python -m pip install mastapy==11.0 --upgrade into the command line.

    The following are a number of simple examples written using mastapy 11.0. They are designed to show you an array of possible use-cases for mastapy and to get an idea of some of the best practices when writing Python scripts for MASTA. There are much more examples available on the SMT Store.

    Scripts do not need to call mastapy.init if they are launched from MASTA as a masta_property.

    Basic Scripted Property

    This script is also available on the SMT Store.

    '''Creates a property which returns the bearing radius.
    Use any model with defined rolling bearings for this task.
    
    Note:
        For more information on Python coding standards (as used by this module):
        https://www.python.org/dev/peps/pep-0008/
    '''
    
    
    from mastapy import masta_property, MeasurementType
    from mastapy.system_model.part_model import Bearing
    
    
    ###############################################################################
    # Constants
    
    
    # It is a good idea to write all of your constants at the top of the module.
    # This makes finding and changing them a lot easier.
    NAME = 'EX1 - Bearing Radius Getter'
    DESCRIPTION = 'Displays radius of a selected bearing'
    SYMBOL = 'D/2'
    MEASUREMENT = MeasurementType.SHORT_LENGTH
    
    
    ###############################################################################
    # MASTA Properties
    
    
    @masta_property(
        name=NAME, description=DESCRIPTION, symbol=SYMBOL, measurement=MEASUREMENT)
    def element_radius(bearing: Bearing) -> float:
        ''' A Masta Property for display on a bearing component, it calculates
        a bearing's radius and returns it.
    
        Args:
            bearing (Bearing): A bearing component
    
        Returns:
            float: The bearing radius
        '''
    
        bearing_diameter = bearing.detail.outer_diameter
        bearing_radius = bearing_diameter / 2.0
        return bearing_radius
    
    

    Creating a Model Using a Script

    '''Constructs a very simple example gearbox.
    
    Note:
        For more information on Python coding standards (as used by this module):
        https://www.python.org/dev/peps/pep-0008/
    '''
    
    
    from mastapy import masta_property, MeasurementType
    from mastapy.system_model import Design
    from mastapy.bearings import BearingCatalog
    from mastapy.system_model.analyses_and_results.static_loads import AnalysisType
    
    
    ###############################################################################
    # Constants
    
    
    # It is a good idea to write all of your constants at the top of the module.
    # This makes finding and changing them a lot easier.
    NAME = 'Simple Example'
    MEASUREMENT = MeasurementType.UNMEASURABLE
    
    
    ###############################################################################
    # MASTA Properties
    
    
    @masta_property(name=NAME, measurement=MEASUREMENT)
    def simple_example(design: Design):
        ''' A Masta Property for creating a simple gearbox.
    
        Args:
            design (Design): A design component. Should be empty.
        '''
    
        assembly = design.all_parts_of_type_root_assembly()[0]
        shaft1 = assembly.add_shaft()
        shaft2 = assembly.add_shaft()
        bearing1 = assembly.add_bearing('Bearing')
        bearing2 = assembly.add_rolling_bearing_from_catalogue(
            BearingCatalog.SKF, 'N 209 ECP', 'Bearing')
        bearing3 = assembly.add_bearing('Bearing')
        bearing4 = assembly.add_rolling_bearing_from_catalogue(
            BearingCatalog.SKF, 'N 209 ECP', 'Bearing')
        shaft1.mount_component(bearing1, bearing1.length / 2.0)
        shaft1.mount_component(bearing2, shaft1.length - bearing2.length / 2.0)
        shaft2.mount_component(bearing3, bearing3.length / 2.0)
        shaft2.mount_component(bearing4, shaft2.length - bearing4.length / 2.0)
    
        for point in shaft1.active_definition.outer_profile.points:
            point.diameter = bearing2.detail.bore
    
        for point in shaft2.active_definition.outer_profile.points:
            point.diameter = bearing2.detail.bore
    
        gear_set = assembly.add_cylindrical_gear_pair_with_options()
        shaft1.mount_component(gear_set.cylindrical_gears[0], shaft1.length / 2.0)
        shaft2.mount_component(gear_set.cylindrical_gears[1], shaft2.length / 2.0)
        gear_set = assembly.add_cylindrical_gear_pair_with_options()
        shaft1.mount_component(gear_set.cylindrical_gears[1], shaft1.length / 4.0)
        shaft2.mount_component(gear_set.cylindrical_gears[0], shaft2.length / 4.0)
    
        input_power_load = assembly.add_power_load('Input Power Load')
        output_power_load = assembly.add_power_load('Output Power Load')
        shaft1.mount_component(input_power_load, bearing1.length)
        shaft2.mount_component(output_power_load, bearing3.length)
    
        assembly.design_properties.input_power_load = input_power_load
    
        design_state = assembly.design_properties.add_design_state()
        load_case = design_state.create_load_case()
        load_case.input_power_load.torque = 100
        load_case.input_power_load.speed = 100
        duty_cycle = assembly.design_properties.add_duty_cycle()
        duty_cycle.add_static_load(load_case)
    
        duty_cycle_system_deflection = duty_cycle.analysis_of(
            AnalysisType.SYSTEM_DEFLECTION)
        load_case_system_deflection = load_case.analysis_of(
            AnalysisType.SYSTEM_DEFLECTION)
        load_case_system_deflection.perform_analysis()
        duty_cycle_system_deflection.perform_analysis()
    
        load_case_system_deflection.results_for(bearing2)
        if load_case_system_deflection.results_ready:
            print('Finished!')
    
    
    Back to top