| 1 |
# Bonsai - OpenBIM Blender Add-on |
| 2 |
# Copyright (C) 2022 Dion Moult <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of Bonsai. |
| 5 |
# |
| 6 |
# Bonsai is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Bonsai is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
import bcf.v2.visinfo |
| 21 |
import bonsai.core.tool |
| 22 |
import bonsai.tool as tool |
| 23 |
import bpy |
| 24 |
import bcf.v2.model |
| 25 |
import bcf.v2.model.visinfo |
| 26 |
import bcf.v2.topic |
| 27 |
|
| 28 |
import bcf.v3.model |
| 29 |
import bcf.v3.model.visinfo |
| 30 |
import bcf.v3.topic |
| 31 |
|
| 32 |
import bcf.agnostic.model |
| 33 |
import bcf.agnostic.topic |
| 34 |
import bcf.agnostic.visinfo |
| 35 |
|
| 36 |
from typing import Any, Union, TypeVar, TypeGuard, Optional, TYPE_CHECKING |
| 37 |
|
| 38 |
if TYPE_CHECKING: |
| 39 |
from bonsai.bim.module.bcf.prop import BCFProperties |
| 40 |
|
| 41 |
|
| 42 |
T = TypeVar("T") |
| 43 |
|
| 44 |
|
| 45 |
class Bcf(bonsai.core.tool.Bcf): |
| 46 |
@classmethod |
| 47 |
def get_bcf_props(cls) -> "BCFProperties": |
| 48 |
return bpy.context.scene.BCFProperties |
| 49 |
|
| 50 |
@classmethod |
| 51 |
def get_path(cls) -> str: |
| 52 |
props = cls.get_bcf_props() |
| 53 |
return props.bcf_file |
| 54 |
|
| 55 |
@classmethod |
| 56 |
def is_list_of(cls, a: list[Any], t: type[T]) -> TypeGuard[list[T]]: |
| 57 |
"""Narrow type for a list assuming list is not mixing types.""" |
| 58 |
if not a: |
| 59 |
return True |
| 60 |
return isinstance(a[0], t) |
| 61 |
|
| 62 |
# BCF version agnostic API. |
| 63 |
## topic |
| 64 |
@classmethod |
| 65 |
def set_topic_bim_snippet( |
| 66 |
cls, |
| 67 |
topic: bcf.agnostic.topic.TopicHandler, |
| 68 |
bim_snippet: Union[bcf.agnostic.model.BimSnippet, None], |
| 69 |
bim_snippet_bytes: Optional[bytes] = None, |
| 70 |
) -> None: |
| 71 |
if isinstance(bim_snippet, bcf.v2.model.BimSnippet): |
| 72 |
assert isinstance(topic, bcf.v2.topic.TopicHandler) |
| 73 |
topic.topic.bim_snippet = bim_snippet |
| 74 |
elif isinstance(bim_snippet, bcf.v3.model.BimSnippet): |
| 75 |
assert isinstance(topic, bcf.v3.topic.TopicHandler) |
| 76 |
topic.topic.bim_snippet = bim_snippet |
| 77 |
else: |
| 78 |
topic.topic.bim_snippet = bim_snippet |
| 79 |
|
| 80 |
if bim_snippet and not bim_snippet.is_external and bim_snippet_bytes is None: |
| 81 |
raise Exception("No bytes data provided for non-external bim snippet.") |
| 82 |
topic._bim_snippet = bim_snippet_bytes |
| 83 |
|
| 84 |
@classmethod |
| 85 |
def get_topic_document_references( |
| 86 |
cls, topic: bcf.agnostic.topic.TopicHandler |
| 87 |
) -> Union[list[bcf.v2.model.TopicDocumentReference], list[bcf.v3.model.DocumentReference]]: |
| 88 |
if isinstance(topic, bcf.v2.topic.TopicHandler): |
| 89 |
document_references = topic.topic.document_reference |
| 90 |
else: |
| 91 |
document_references = topic.topic.document_references |
| 92 |
document_references = document_references.document_reference if document_references else [] |
| 93 |
return document_references |
| 94 |
|
| 95 |
@classmethod |
| 96 |
def set_topic_document_references( |
| 97 |
cls, |
| 98 |
topic: bcf.agnostic.topic.TopicHandler, |
| 99 |
document_references: Union[list[bcf.v2.model.TopicDocumentReference], list[bcf.v3.model.DocumentReference]], |
| 100 |
) -> None: |
| 101 |
topic_ = topic.topic |
| 102 |
if isinstance(topic_, bcf.v2.model.Topic): |
| 103 |
assert cls.is_list_of(document_references, bcf.v2.model.TopicDocumentReference) |
| 104 |
topic_.document_reference = document_references |
| 105 |
else: |
| 106 |
assert cls.is_list_of(document_references, bcf.v3.model.DocumentReference) |
| 107 |
topic_document_references = topic_.document_references |
| 108 |
if topic_document_references is None: |
| 109 |
if not document_references: |
| 110 |
return |
| 111 |
topic_.document_references = (topic_document_references := bcf.v3.model.TopicDocumentReferences()) |
| 112 |
topic_document_references.document_reference = document_references |
| 113 |
|
| 114 |
@classmethod |
| 115 |
def get_topic_header_files( |
| 116 |
cls, topic: bcf.agnostic.topic.TopicHandler |
| 117 |
) -> Union[list[bcf.v2.model.HeaderFile], list[bcf.v3.model.File]]: |
| 118 |
header = topic.header |
| 119 |
if not header: |
| 120 |
return [] |
| 121 |
if isinstance(header, bcf.v2.model.Header): |
| 122 |
header_files = header.file |
| 123 |
else: |
| 124 |
header_files = header.files |
| 125 |
header_files = header_files.file if header_files else [] |
| 126 |
return header_files |
| 127 |
|
| 128 |
@classmethod |
| 129 |
def set_topic_header_files( |
| 130 |
cls, |
| 131 |
topic: bcf.agnostic.topic.TopicHandler, |
| 132 |
files: Union[list[bcf.v2.model.HeaderFile], list[bcf.v3.model.File]], |
| 133 |
) -> None: |
| 134 |
if isinstance(topic, bcf.v2.topic.TopicHandler): |
| 135 |
header = topic.header |
| 136 |
if not header: |
| 137 |
if not files: |
| 138 |
return |
| 139 |
topic.header = (header := bcf.v2.model.Header()) |
| 140 |
if not header.file: |
| 141 |
if not files: |
| 142 |
return |
| 143 |
assert cls.is_list_of(files, bcf.v2.model.HeaderFile) |
| 144 |
header.file = files |
| 145 |
else: |
| 146 |
header = topic.header |
| 147 |
if not header: |
| 148 |
if not files: |
| 149 |
return |
| 150 |
header = bcf.v3.model.Header() |
| 151 |
header_files = header.files |
| 152 |
if not header_files: |
| 153 |
if not files: |
| 154 |
return |
| 155 |
header.files = (header_files := bcf.v3.model.HeaderFiles()) |
| 156 |
assert cls.is_list_of(files, bcf.v3.model.File) |
| 157 |
header_files.file = files |
| 158 |
|
| 159 |
@classmethod |
| 160 |
def get_topic_labels(cls, topic: bcf.agnostic.topic.TopicHandler) -> list[str]: |
| 161 |
if isinstance(topic, bcf.v2.topic.TopicHandler): |
| 162 |
labels = topic.topic.labels |
| 163 |
else: |
| 164 |
labels = topic.topic.labels |
| 165 |
labels = labels.label if labels else [] |
| 166 |
return labels |
| 167 |
|
| 168 |
@classmethod |
| 169 |
def set_topic_labels(cls, topic: bcf.agnostic.topic.TopicHandler, labels: list[str]) -> None: |
| 170 |
topic_ = topic.topic |
| 171 |
if isinstance(topic_, bcf.v2.model.Topic): |
| 172 |
topic_.labels = labels |
| 173 |
else: |
| 174 |
topic_labels = topic_.labels |
| 175 |
if topic_labels is None: |
| 176 |
if not labels: |
| 177 |
return |
| 178 |
topic_.labels = (topic_labels := bcf.v3.model.TopicLabels()) |
| 179 |
topic_labels.label = labels |
| 180 |
|
| 181 |
@classmethod |
| 182 |
def get_topic_reference_links(cls, topic: bcf.agnostic.topic.TopicHandler) -> list[str]: |
| 183 |
if isinstance(topic, bcf.v2.topic.TopicHandler): |
| 184 |
reference_links = topic.topic.reference_link |
| 185 |
else: |
| 186 |
reference_links = topic.topic.reference_links |
| 187 |
reference_links = reference_links.reference_link if reference_links else [] |
| 188 |
return reference_links |
| 189 |
|
| 190 |
@classmethod |
| 191 |
def set_topic_reference_links(cls, topic: bcf.agnostic.topic.TopicHandler, reference_links: list[str]) -> None: |
| 192 |
topic_ = topic.topic |
| 193 |
if isinstance(topic_, bcf.v2.model.Topic): |
| 194 |
topic_.reference_link = reference_links |
| 195 |
else: |
| 196 |
topic_links = topic_.reference_links |
| 197 |
if topic_links is None: |
| 198 |
if not reference_links: |
| 199 |
return |
| 200 |
topic_.reference_links = (topic_links := bcf.v3.model.TopicReferenceLinks()) |
| 201 |
topic_links.reference_link = reference_links |
| 202 |
|
| 203 |
@classmethod |
| 204 |
def get_topic_related_topics( |
| 205 |
cls, topic: bcf.agnostic.topic.TopicHandler |
| 206 |
) -> Union[list[bcf.v2.model.TopicRelatedTopic], list[bcf.v3.model.TopicRelatedTopicsRelatedTopic]]: |
| 207 |
if isinstance(topic, bcf.v2.topic.TopicHandler): |
| 208 |
related_topics = topic.topic.related_topic |
| 209 |
else: |
| 210 |
related_topics = topic.topic.related_topics |
| 211 |
related_topics = related_topics.related_topic if related_topics else [] |
| 212 |
return related_topics |
| 213 |
|
| 214 |
@classmethod |
| 215 |
def set_topic_related_topics( |
| 216 |
cls, |
| 217 |
topic: bcf.agnostic.topic.TopicHandler, |
| 218 |
related_topics: Union[list[bcf.v2.model.TopicRelatedTopic], list[bcf.v3.model.TopicRelatedTopicsRelatedTopic]], |
| 219 |
) -> None: |
| 220 |
topic_ = topic.topic |
| 221 |
if isinstance(topic_, bcf.v2.model.Topic): |
| 222 |
assert cls.is_list_of(related_topics, bcf.v2.model.TopicRelatedTopic) |
| 223 |
topic_.related_topic = related_topics |
| 224 |
else: |
| 225 |
assert cls.is_list_of(related_topics, bcf.v3.model.TopicRelatedTopicsRelatedTopic) |
| 226 |
topic_related_topics = topic_.related_topics |
| 227 |
if topic_related_topics is None: |
| 228 |
if not related_topics: |
| 229 |
return |
| 230 |
topic_.related_topics = (topic_related_topics := bcf.v3.model.TopicRelatedTopics()) |
| 231 |
topic_related_topics.related_topic = related_topics |
| 232 |
|
| 233 |
@classmethod |
| 234 |
def get_topic_viewpoints( |
| 235 |
cls, topic: bcf.agnostic.topic.TopicHandler |
| 236 |
) -> Union[list[bcf.v2.model.ViewPoint], list[bcf.v3.model.ViewPoint]]: |
| 237 |
if isinstance(topic, bcf.v2.topic.TopicHandler): |
| 238 |
assert topic.markup |
| 239 |
viewpoints = topic.markup.viewpoints |
| 240 |
else: |
| 241 |
topic_viewpoints = topic.topic.viewpoints |
| 242 |
if not topic_viewpoints: |
| 243 |
return [] |
| 244 |
viewpoints = topic_viewpoints.view_point |
| 245 |
return viewpoints |
| 246 |
|
| 247 |
@classmethod |
| 248 |
def set_topic_viewpoints( |
| 249 |
cls, |
| 250 |
topic: bcf.agnostic.topic.TopicHandler, |
| 251 |
viewpoints: Union[list[bcf.v2.model.ViewPoint], list[bcf.v3.model.ViewPoint]], |
| 252 |
) -> None: |
| 253 |
if isinstance(topic, bcf.v2.topic.TopicHandler): |
| 254 |
assert topic.markup |
| 255 |
assert cls.is_list_of(viewpoints, bcf.v2.model.ViewPoint) |
| 256 |
topic.markup.viewpoints = viewpoints |
| 257 |
else: |
| 258 |
topic_viewpoints = topic.topic.viewpoints |
| 259 |
if not topic_viewpoints: |
| 260 |
if not viewpoints: |
| 261 |
return |
| 262 |
topic.topic.viewpoints = (topic_viewpoints := bcf.v3.model.TopicViewpoints()) |
| 263 |
assert cls.is_list_of(viewpoints, bcf.v3.model.ViewPoint) |
| 264 |
topic_viewpoints.view_point = viewpoints |
| 265 |
|
| 266 |
## visinfo |
| 267 |
@classmethod |
| 268 |
def get_viewpoint_bitmaps( |
| 269 |
cls, viewpoint: bcf.agnostic.visinfo.VisualizationInfoHandler |
| 270 |
) -> Union[list[bcf.v2.model.VisualizationInfoBitmap], list[bcf.v3.model.Bitmap]]: |
| 271 |
if isinstance(viewpoint, bcf.v2.visinfo.VisualizationInfoHandler): |
| 272 |
bitmaps = viewpoint.visualization_info.bitmap |
| 273 |
else: |
| 274 |
bitmaps = viewpoint.visualization_info.bitmaps |
| 275 |
bitmaps = bitmaps.bitmap if bitmaps else [] |
| 276 |
return bitmaps |
| 277 |
|
| 278 |
@classmethod |
| 279 |
def get_viewpoint_view_setup_hints( |
| 280 |
cls, viewpoint: bcf.agnostic.visinfo.VisualizationInfoHandler |
| 281 |
) -> Union[bcf.v2.model.visinfo.ViewSetupHints, bcf.v3.model.visinfo.ViewSetupHints, None]: |
| 282 |
components = viewpoint.visualization_info.components |
| 283 |
if not components: |
| 284 |
return None |
| 285 |
|
| 286 |
if isinstance(components, bcf.v2.model.visinfo.Components): |
| 287 |
return components.view_setup_hints |
| 288 |
|
| 289 |
visibility = components.visibility |
| 290 |
if not visibility: |
| 291 |
return None |
| 292 |
return visibility.view_setup_hints |