Source code for routing.core.model
"""Temporary module for implementing base class Object3D of objects with 3D representations."""
from typing import Optional
from dessia_common.core import DessiaObject
from dessia_common.decorators import cad_view
from volmdlr.core import Primitive3D
from volmdlr.model import VolumeModel
from volmdlr.ocaf import OCAF
[docs]
class Object3D(DessiaObject):
"""
A base class for objects with 3D representations.
This class inherits from `DessiaObject`,
and provides a common interface for 3D objects in the routing system,
with methods for visualization and CAD export. Subclasses must implement
volmdlr_primitives() to provide their 3D geometry.
:param name: The name to identify this 3D object. Defaults to ''.
"""
_standalone_in_db = True
def __init__(self, name: str = ""):
"""Initialize Object3D with name."""
super().__init__(name=name)
[docs]
def volmdlr_primitives(self) -> list[Primitive3D]:
"""
Get Primitive3D objects representing the 3D geometry.
This method must be implemented by subclasses to provide their 3D
geometric representation.
:return: List of Primitive3D objects
:raises NotImplementedError: If not implemented by subclass
"""
raise NotImplementedError("volmdlr_primitives method must be implemented for current class.")
[docs]
def volume_model(self) -> VolumeModel:
"""
Get volume model.
Creates a VolumeModel from the object's primitives for visualization
and CAD operations.
:return: VolumeModel containing the object's primitives
"""
return VolumeModel(self.volmdlr_primitives())
[docs]
def babylon_data(self, merge_meshes: bool = True, show_curves: bool = False, ocaf: OCAF = None) -> dict:
"""Get babylonjs data."""
return self.volume_model().babylon_data(merge_meshes=merge_meshes, show_curves=show_curves, ocaf=ocaf)
@cad_view("CAD View")
def cad_view(self, *args, **kwargs) -> dict:
"""Cad view for platform usage."""
return self.babylon_data()
[docs]
def babylonjs( # noqa: PLR0913, PLR0917
self,
page_name: Optional[str] = None,
use_cdn: bool = True,
debug: bool = False,
merge_meshes: bool = True,
show_curves: bool = False,
dark_mode: bool = False,
ocaf: Optional[OCAF] = None,
) -> str:
"""
Generate and display an HTML file to visualize the 3D model using Babylon.js in a web browser.
This method creates a 3D representation of the volume model using the Babylon.js framework.
The method allows options for debugging, merging meshes, and toggling dark mode for the visualization.
The resulting HTML file can either be a temporary file or a user-specified file.
:param page_name: The name of the HTML file to be generated. If None, a temporary file is created.
:param use_cdn: Flag to use CDN for loading Babylon.js resources. Defaults to True.
:param debug: Enable debugging mode for more detailed console output in the browser. Defaults to False.
:param merge_meshes: Flag to chose to merge all the faces of each shell into a single mesh. Defaults to True.
If False, shell are decomposed according to their faces in the Babylon.js scene nodes tree.
:param show_curves: Whether to show shape curves or not.
:param dark_mode: Enable dark mode for the HTML visualization. Defaults to False.
:param ocaf: OCAF document representing current Object3D
:return: The file path of the generated HTML file.
"""
return self.volume_model().babylonjs(
page_name=page_name,
use_cdn=use_cdn,
debug=debug,
merge_meshes=merge_meshes,
show_curves=show_curves,
dark_mode=dark_mode,
ocaf=ocaf,
)