### ===================================================================================================================
### Functions to get the reference object models from MYVIIA
### ===================================================================================================================
# Copyright ©VIIA 2024
### ===================================================================================================================
### 1. Import modules
### ===================================================================================================================
# General imports
from __future__ import annotations
from typing import TYPE_CHECKING, Dict, Any
import json
# References for functions and classes in the viiaPackage
if TYPE_CHECKING:
from viiapackage.viiaStatus import ViiaProject
from viiapackage.database import myviia_get_object_deel, myviia_get_eng_a1_with_model, myviia_get_eng_measure
from viiapackage.database.viia_myviia_get_info import myviia_get_deliverables
### ===================================================================================================================
### 2. Collect the object references from MYVIIA
### ===================================================================================================================
[docs]def viia_get_ref_models_from_myviia(project: ViiaProject) -> Dict[int, Dict[str, Any]]:
"""
Function to collect the selected reference objects from MYVIIA.
Input:
- project (obj): VIIA project object containing collections of fem objects and project variables.
Output:
- Returns dictionary with the IDs of the referenced objects and the general data of these objects.
"""
# Get the data for reference objects in MYVIIA
data = myviia_get_object_deel(
object_deel_id=project.get_myviia_object_deel_id(), token=project.token, ref_objects=True)
data = data['object_deel_ref_objects']
# Convert to dictionary for postprocessing
return {ref_data['ref_object_deel_id']: ref_data['ref_object_deel'] for ref_data in data}
### ===================================================================================================================
### 3. Collect the object reference models from MYVIIA
### ===================================================================================================================
[docs]def viia_collect_ref_models(project: ViiaProject, objectparts: Dict[int, Dict[str, Any]]) -> Dict[int, Dict[str, Any]]:
"""
Function to collect the models from the reference objects from MYVIIA.
Input:
- objectparts (dict): Dictionary with the IDs of the referenced objectparts and the general data of these
objectparts.
Output:
- Creates subprojects for the reference objects selected by user in MYVIIA, which have a model stored in the
database of MYVIIA.
- Returns dictionary with the reference objectparts ID (key) with information in dictionary with the subproject.
This dictionary is also stored in the project variable.
"""
from viiapackage.viiaStatus import viia_create_project
project.ref_models = {}
for objectpart in objectparts:
obj_deliverable = myviia_get_deliverables(object_deel_id=objectpart, token=project.token)
tva_link = None
for item in obj_deliverable['deliverables']:
if item['deliverable_id']['deliverable'] == 'TVA':
if item['link_document'] is None:
pass
else:
tva_link = item['link_document']
# Collect necessary data from the object part on MYVIIA
object_deel_dict = myviia_get_object_deel(object_deel_id=objectpart, token=project.token)
# Check if object part has A1 results on MYVIIA
if not object_deel_dict['object_deel_eng_a1']:
project.write_log(
f"{object_deel_dict['viia_object_id']['objectnummer_viia']} ({object_deel_dict['naam']}) does not "
f"have A1 results stored on MYVIIA. Reference object is skipped.")
continue
# Check if object part has FEM model json on MYVIIA
# Filter list for model present and by date
eng_a1s = sorted([
eng_a1 for eng_a1 in object_deel_dict['object_deel_eng_a1']
if 'eng_a1_model_id' in eng_a1 and eng_a1['eng_a1_model_id']], key=lambda d: d['date'])
eng_a1 = None
if eng_a1s:
eng_a1 = myviia_get_eng_a1_with_model(eng_a1_id=eng_a1s[-1]['id'])
if not eng_a1:
project.write_log(
f"{object_deel_dict['viia_object_id']['objectnummer_viia']} ({object_deel_dict['naam']}) does not have "
f"a model json stored in A1 results on MYVIIA. Reference object is skipped.")
continue
# Create easily readable data structure for referencing the ref models
project.ref_models[objectpart] = {}
project.ref_models[objectpart]['object'] = object_deel_dict['viia_object_id']['objectnummer_viia']
project.ref_models[objectpart]['object_part'] = object_deel_dict['naam']
project.ref_models[objectpart]['measures'] = \
myviia_get_eng_measure(object_deel_id=object_deel_dict['id'], token=project.token)
project.ref_models[objectpart]['Box_Link'] = object_deel_dict['viia_object_id']['boxmap']
project.ref_models[objectpart]['tva_link'] = tva_link
project.ref_models[objectpart]['dat_link'] = obj_deliverable.get('link_rekenmodel', None)
# Create subproject
project.write_log(
f"Start loading model for {object_deel_dict['viia_object_id']['objectnummer_viia']} "
f"({object_deel_dict['naam']}).")
project.ref_models[objectpart]['model'] = viia_create_project(
project_name=project.ref_models[objectpart]['object'],
object_part=project.ref_models[objectpart]['object_part'])
# Read the reference object model json-file
try:
object_deel_json = json.dumps(eng_a1['eng_a1_model']['model'])
project.ref_models[objectpart]['model'].read_dump(json_string=object_deel_json, loading_model='minimum')
except Exception as e:
project.write_log(
f"WARNING: An unknown error occurred during reading of json of "
f"{object_deel_dict['viia_object_id']['objectnummer_viia']} - {object_deel_dict['naam']}. "
f"Error: {e}. Object part is skipped.")
project.ref_models[objectpart]['model'].logfile.unlink()
project.ref_models.pop(objectpart)
continue
project.write_log(
f"Loaded model for {object_deel_dict['viia_object_id']['objectnummer_viia']} ({object_deel_dict['naam']}).")
# Return dictionary with all reference models available
return project.ref_models
### ===================================================================================================================
### 4. Collect measures of the reference models
### ===================================================================================================================
def viia_collect_measures_of_reference_models(ref_models: Dict) -> Dict[str, str]:
""" Collects the measures of all reference models """
obj_vs_measures = {}
for ref_model in ref_models.values():
if ref_model['measures']:
measures_name_list = []
for measure in ref_model['measures']:
measures_name_list.append(measure['name'])
if len(ref_model['measures']) > 1:
measures_name = ', '.join(measures_name_list)
else:
measures_name = measures_name_list[0]
else:
measures_name = '-'
obj_vs_measures[f"{ref_model['object']} - {ref_model['object_part']}"] = measures_name
return obj_vs_measures
### ===================================================================================================================
### 5. End of script
### ===================================================================================================================