Source code for viiapackage.pictures.psse_nsce_plots.viia_plot_psse_nsce

### ===================================================================================================================
###  FUNCTION: Create pictures for PSSE and NSCE elements in the model
### ===================================================================================================================
# Copyright ©VIIA 2024

### ===================================================================================================================
###   1. Import modules
### ===================================================================================================================

# General imports
from __future__ import annotations
from pathlib import Path
from typing import List
from dataclasses import dataclass
from typing import Tuple, Optional, TYPE_CHECKING
import matplotlib.pyplot as plt
import warnings

# References for functions and classes in the rhdhv_fem package
from rhdhv_fem.shapes import Shapes
from rhdhv_fem.fem_tools import fem_create_folder
from rhdhv_fem.grid import Grid
from rhdhv_fem.shapes import Wall, Column

# References for functions and classes in the viiaPackage
if TYPE_CHECKING:
    from viiapackage.viiaStatus import ViiaProject
from viiapackage.geometry import StructuralType


### ===================================================================================================================
###    2. Helper functions
### ===================================================================================================================

def _plot_shape(shape: Shapes, color: str, legend_text: str, linewidth: float) -> None:
    """ Function checks if the plot functionality is available for the shape."""
    if not isinstance(shape, (Column, Wall)):
        warnings.warn(f"WARNING: Plotting is not implemented for shape '{shape.name}', it is skipped.")
        return
    shape._plot(color=color, legend_text=legend_text, extra_kwargs={"linewidth": linewidth})


### ===================================================================================================================
###    3. Helper class PSSENSCEPlotSettings
### ===================================================================================================================

[docs]@dataclass class PSSENSCEPlotSettings: """ Helper class for storing settings for PSSE and NSCE plots.""" psse_color: str = '#C00040' nsce_color: str = '#4040C0' psse_linewidth: float = 3 nsce_linewidth: float = 3 figsize: Tuple = (10, 10) # (width, height) subfolder: str = 'PSSE NSCE' file_base_name: str = 'Element Classification'
### =================================================================================================================== ### 4. Function to generate PSSE-NSCE plot ### ===================================================================================================================
[docs]def viia_plot_psse_nsce( project: ViiaProject, settings: PSSENSCEPlotSettings = PSSENSCEPlotSettings(), show: bool = False, grid: Optional[Grid] = None) -> List[Path]: """ Function to create the psse and nsce plots. By default, psse's are plotted in red and nsce's in blue. All shapes that have either structural type PSSE or NSCE defined in their metadata are included in the plots. A separate image is created for each layer. Input: - project (obj): VIIA project object containing collections of fem objects and project variables. - settings (obj): Settings object for the plot, including styling and save location. - show (bool): Toggle to show the plot. Default is False. - grid (Grid): Optional grid object to plot behind the shapes. Default is the first grid in the collections. Output: - Plots are created in the specified folder. Default location is workfolder/Appendix Pictures/PSSE NSCE. """ # Get the folder for the pictures path_plot_folder = project.appendix_pictures_folder / settings.subfolder if not path_plot_folder.exists(): fem_create_folder(path_plot_folder) files = [] for layer in project.collections.layers: # Assemble lists of psse and nsce shapes from layer psse_shapes = [] nsce_shapes = [] for shape in layer.shapes: if not shape.meta_data: continue elif shape.meta_data.get('structural_type') == StructuralType.PSSE: psse_shapes.append(shape) elif shape.meta_data.get('structural_type') == StructuralType.NSCE: nsce_shapes.append(shape) # Continue if no shapes need to be plotted on this layer if not psse_shapes and not nsce_shapes: continue # Initiate the plot fig, ax = plt.subplots() project.plot_building_area() # Plot the grid in the background and fetch axes and figures grid_obj = grid if grid else project.find_grid() grid_obj.plot(tags=None, show=False, keep_plot=True) # Plot the psse or nsce element for psse_shape in psse_shapes: _plot_shape(psse_shape, color=settings.psse_color, legend_text='PSSE', linewidth=settings.psse_linewidth) for nsce_shape in nsce_shapes: _plot_shape(nsce_shape, color=settings.nsce_color, legend_text='NSCE', linewidth=settings.nsce_linewidth) # Make legend handles, labels = ax.get_legend_handles_labels() by_label = dict(zip(labels, handles)) plt.legend(by_label.values(), by_label.keys()) # Set title and save the figure # Do not change the name as it is related to reporting functionality name = f'{settings.file_base_name} {layer.name}' filename = name + '.png' plt.title(name) fig.set_figwidth(settings.figsize[0]) fig.set_figheight(settings.figsize[1]) plotfile = path_plot_folder / filename plt.savefig(plotfile) if plotfile.exists(): files.append(plotfile) # Show the figure if desired. if show: plt.show() # Return files of all created pictures return files
### =================================================================================================================== ### 5. End of script ### ===================================================================================================================