from dlubal.api import rfem, common

# Close existing model named 'new_test_model' if it exists, then create a new one
def main():
    with rfem.Application() as rfem_app:
        model_name = 'new_test_model'
        model_list = rfem_app.get_model_list().model_info
        for model in model_list:
            if model.name == model_name:
                model_guid = model.guid
                rfem_app.close_model(model_id=common.ModelId(guid=model_guid), save_changes=False)
                break

        rfem_app.create_model(name=model_name)

        # Clean up the model
        rfem_app.delete_all_objects()

if __name__ == '__main__':
    main()