from dlubal.api import rfem, common

def main():
    with rfem.Application() as rfem_app:
        # Retrieve the Design ULS Configuration TreeTable for Alu add-on
        alu_uls_config: rfem.aluminum_design_objects.AluminumDesignUlsConfiguration = rfem_app.get_object(
            obj=rfem.aluminum_design_objects.AluminumDesignUlsConfiguration(no=1)
        )
        print(f'alu_uls_config.name={alu_uls_config.name}')
        alu_uls_config.assigned_to_members.clear()
        # Assign to members with numbers 1512 and 1513
        alu_uls_config.assigned_to_members.extend([1512,1513])
        print(f'alu_uls_config.no={alu_uls_config.assigned_to_members}')
        settings_ec9 = alu_uls_config.settings_ec9
        print(f"settings_ec9:\n{settings_ec9}")

        # Retrieve a specific value from the configuration
        # Path to the specific value

        chapter = 'general_options'
        key = 'perform_stability_analysis'
        design_path = [
            chapter,
            key
        ]

        # Get and print specific value (not necessary just for demonstration)
        print(f"design_path: {design_path}")
        design_val = common.get_tree_value(
            tree=settings_ec9,
            path=design_path
        )
        print(f"design_val: {design_val}")

        # Modify the value
        common.set_tree_value(
            tree=settings_ec9,
            path=design_path,
            value=True
        )

        # Apply the updated configuration to the model
        # ToDo: Temporarily clear the 'standard_parameters' & 'standard_parameters_tree' field - Currently required before updating the object
        alu_uls_config.ClearField("standard_parameters")
        alu_uls_config.ClearField("standard_parameters_tree")
        rfem_app.update_object(
            obj=alu_uls_config
        )

if __name__ == "__main__":
    main()