Model#

Defines 3D models.

class volmdlr.model.Model(name: str = '')#

Bases: DessiaObject, Displayable3D

Abstract class to define common methods to models.

babylon_data(merge_meshes: bool = True, show_curves: bool = False, ocaf: OCAF | None = None) dict#

Get babylonjs data.

Returns:

Dictionary with babylon data.

babylonjs(page_name: str | None = None, use_cdn: bool = True, debug: bool = False, merge_meshes: bool = True, show_curves: bool = False, dark_mode: bool = False, 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.

Parameters:
  • page_name (str, optional) – The name of the HTML file to be generated. If None, a temporary file is created.

  • use_cdn (bool) – Flag to use CDN for loading Babylon.js resources. Defaults to True.

  • debug (bool) – Enable debugging mode for more detailed console output in the browser. Defaults to False.

  • merge_meshes (bool) – 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.

  • show_curves – Whether to show shape curves or not.

  • dark_mode (bool) – Enable dark mode for the HTML visualization. Defaults to False.

Returns:

The file path of the generated HTML file.

Return type:

str

Deprecated since version Use: display_3d() instead. Will be removed in v1.0.0.

classmethod babylonjs_script(babylon_data, use_cdn=True, **kwargs)#

Run babylonjs script.

cad_view#

Implementation of a tag for displays.

save_babylonjs_to_file(filename: str | None = None, use_cdn=True, debug=False, dark_mode=False)#

Export a html file of the model.

Deprecated since version Use: save_display_3d_to_file() instead. Will be removed in v1.0.0.

class volmdlr.model.MovingVolumeModel(primitives: list[Primitive3D], step_frames: list[list[Frame3D]], name: str = '')#

Bases: VolumeModel

A volume model with possibility to declare time steps at which the primitives are positioned with frames.

babylon_data(merge_meshes=True, show_curves: bool = False)#

Get babylonjs data.

Returns:

Dictionary with babylonjs data.

is_consistent()#

Check if the number of frames for each step corresponds to the number of primitives of the model.

step_volume_model(istep: int)#

Move the volume model with a list of local frames.

class volmdlr.model.VolumeModel(primitives: list[Primitive3D], name: str = '')#

Bases: Model

A class containing one or several volmdlr.core.Primitive3D.

Parameters:
babylon_data(merge_meshes=True, show_curves: bool = False, ocaf=None)#

Get babylonjs data.

Returns:

Dictionary with babylon data.

property bounding_box#

Return the bounding box.

copy(deep=True, memo=None)#

Specific copy.

frame_mapping(frame: Frame3D, side: str)#

Change frame_mapping and return a new VolumeModel.

side = ‘old’ or ‘new’

classmethod from_ocaf(ocaf_object, name: str = '')#

Translate an OCAF object into a volume model using Open CASCADE Technology (OCCT).

classmethod from_ocp(ocp_shapes: list[TopoDS_Shape], name: str = '')#

Instantiate a volume model from a list of OCCT shapes.

classmethod from_step(step_file: str, name: str = '', reading_unit: Literal['M', 'MM'] = 'M')#

Translate a STEP file into a volume model using Open CASCADE Technology (OCCT).

Parameters:
  • step_file – The path to the STEP file.

  • name – A name for the volume model.

  • reading_unit – The unit of measurement used to read the STEP file. Default is “M” (meters).

classmethod from_step_stream(stream: BinaryFile, name: str = '', reading_unit: Literal['M', 'MM'] = 'M')#

Translate a STEP stream into a volume model using Open CASCADE Technology (OCCT).

Parameters:
  • stream – The stream of the STEP file.

  • name – A name for the volume model.

  • reading_unit – The unit of measurement used to read the STEP file. Default is “M” (meters).

static get_elements_lines(gmsh_model)#

Export the volume model into gmsh format.

get_geo_lines()#

Get the lines that define a VolumeModel geometry in a .geo file.

Returns:

A list of lines that describe the geometry

Return type:

list[str]

get_mesh_lines(factor: float, **kwargs)#

Get the lines that define mesh parameters for a VolumeModel, to be added to a .geo file.

Parameters:

factor – A float, between 0 and 1, that describes the mesh quality

(1 for coarse mesh - 0 for fine mesh) :type factor: float :param curvature_mesh_size: Activate the calculation of mesh element sizes based on curvature (with curvature_mesh_size elements per 2*Pi radians), defaults to 0 :type curvature_mesh_size: int, optional :param min_points: Check if there are enough points on small edges (if it is not, we force to have min_points on that edge), defaults to None :type min_points: int, optional :param initial_mesh_size: If factor=1, it will be initial_mesh_size elements per dimension, defaults to 5 :type initial_mesh_size: float, optional

Returns:

A list of lines that describe mesh parameters

Return type:

list[str]

static get_nodes_lines(gmsh_model)#

Get nodes lines.

get_shape_metadata(shape) ShapeMetadata#

Get all metadata related to a given shape.

Parameters:

shape – A volmdlr.shapes.Shape instance.

Returns:

A ShapeMetadata object.

get_shapes()#

Dissociates all the assemblies to get a list of shapes.

Returns:

A list of shapes

Return type:

list[Shapes]

heal() None#

Perform a shape healing fixing general issues on the model.

mesh(relative_linear_deflection: float = 0.005, angular_deflection: float = 0.5, linear_deflection: float | None = None, **kwargs) None#

Mesh the OCP shapes, save the meshing parameters, and save the mesh in cache of the OCP objects.

Parameters:
  • relative_linear_deflection – Relative linear deflection for mesh generation, used to compute linear deflection based on bounding box size. Smaller values produce finer meshes.

  • angular_deflection – Angular deflection in radians. Smaller values, finer meshes.

  • linear_deflection – Linear deflection for mesh generation. If provided, it overrides the automatic calculation based on bounding box size.

property ocaf#

Get OCAF.

plot(ax=None, equal_aspect=True)#

Matplotlib plot of model.

To use for debug.

rotation(center: Point3D, axis: Vector3D, angle: float)#

Rotate the VolumeModel.

Parameters:
  • center – rotation center

  • axis – rotation axis

  • angle – angle rotation

Returns:

a new rotated VolumeModel

search_by_name(name: str) Generator[Primitive3D | Shape, None, None]#

Search a primitive inside the volume model.

set_face_metadata(parent, face_id: int, metadata: ShapeMetadata)#

Set a color for a specific face of a primitive inside the volume model.

Parameters:
  • parent – The parent shape of the face.

  • face_id – The face index with respect to its parent.

  • metadata – metadata object containing the shape style.

set_shape_metadata(shape: Shape | TopoDS_Shape, metadata: ShapeMetadata)#

Set a color for a specific face of a primitive inside the volume model.

Parameters:
  • shape – The shape to set the metadata.

  • metadata – metadata object containing the shape style.

to_compound()#

Return a Compound containing the same primitives of the VolumeModel.

to_geo(file_name: str = '', factor: float = 0.5, **kwargs)#

Get the .geo file for the VolumeModel.

Parameters:
  • file_name (str) – The geo. file name

  • factor – A float, between 0 and 1, that describes the mesh quality

(1 for coarse mesh - 0 for fine mesh) :type factor: float :param curvature_mesh_size: Activate the calculation of mesh element sizes based on curvature (with curvature_mesh_size elements per 2*Pi radians), defaults to 0 :type curvature_mesh_size: int, optional :param min_points: Check if there are enough points on small edges (if it is not, we force to have min_points on that edge), defaults to None :type min_points: int, optional :param initial_mesh_size: If factor=1, it will be initial_mesh_size elements per dimension, defaults to 5 :type initial_mesh_size: float, optional

Returns:

A txt file

Return type:

.txt

to_geo_stream(stream: StringFile, factor: float, **kwargs)#

Get the .geo file for the VolumeModel.

Parameters:
  • file_name (str) – The geo. file name

  • factor – A float, between 0 and 1, that describes the mesh quality

(1 for coarse mesh - 0 for fine mesh) :type factor: float :param curvature_mesh_size: Activate the calculation of mesh element sizes based on curvature (with curvature_mesh_size elements per 2*Pi radians), defaults to 0 :type curvature_mesh_size: int, optional :param min_points: Check if there are enough points on small edges (if it is not, we force to have min_points on that edge), defaults to None :type min_points: int, optional :param initial_mesh_size: If factor=1, it will be initial_mesh_size elements per dimension, defaults to 5 :type initial_mesh_size: float, optional

Returns:

A txt file

Return type:

.txt

to_geo_with_stl(file_name: str, factor: float, **kwargs)#

Get the .geo file for the VolumeModel, with saving each closed shell in a stl file.

Parameters:
  • file_name (str) – The geo. file name

  • factor – A float, between 0 and 1, that describes the mesh quality

(1 for coarse mesh - 0 for fine mesh) :type factor: float :param curvature_mesh_size: Activate the calculation of mesh element sizes based on curvature (with curvature_mesh_size elements per 2*Pi radians), defaults to 0 :type curvature_mesh_size: int, optional :param min_points: Check if there are enough points on small edges (if it is not, we force to have min_points on that edge), defaults to None :type min_points: int, optional :param initial_mesh_size: If factor=1, it will be initial_mesh_size elements per dimension, defaults to 5 :type initial_mesh_size: float, optional

Returns:

A txt file

Return type:

.txt

to_mesh(merge_vertices: bool = False, merge_triangles: bool = False, relative_linear_deflection: float = 0.005, angular_deflection: float = 0.5, linear_deflection: float | None = None, **kwargs) Mesh3D#

Convert the volume model to a Mesh3D object.

Parameters:
  • merge_vertices – Flag to indicate whether to merge vertices of the shells meshes.

  • merge_triangles – Flag to indicate whether to merge triangles of the shells meshes.

  • relative_linear_deflection – Relative linear deflection for mesh generation, used to compute linear deflection based on bounding box size. Smaller values produce finer meshes.

  • angular_deflection – Angular deflection in radians. Smaller values, finer meshes.

  • linear_deflection – Linear deflection for mesh generation. If provided, it overrides the automatic calculation based on bounding box size.

Returns:

A Mesh3D of the VolumeModel

to_mesh_list(relative_linear_deflection: float = 0.004, angular_deflection: float = 0.5, linear_deflection: float | None = None, **kwargs)#

Convert the volume model to a list Mesh3D object.

Parameters:
  • relative_linear_deflection – Relative linear deflection for mesh generation, used to compute linear deflection based on bounding box size. Smaller values produce finer meshes.

  • angular_deflection – Angular deflection in radians. Smaller values, finer meshes.

  • linear_deflection – Linear deflection for mesh generation. If provided, it overrides the automatic calculation based on bounding box size.

Returns:

A list of Mesh3D objects representing the VolumeModel shells.

to_msh(mesh_dimension: int, factor: float, mesh_order: int = 1, file_name: str = '', **kwargs)#

Get .msh file for the VolumeModel generated by gmsh.

Parameters:
  • file_name (str) – The msh. file name

  • mesh_dimension (int) – The mesh dimension (1: 1D-Edge, 2: 2D-Triangle, 3D-Tetrahedra)

  • mesh_order (int) – The msh order (1: linear, 2: 2nd order)

  • factor – A float, between 0 and 1, that describes the mesh quality

(1 for coarse mesh - 0 for fine mesh) :type factor: float :param curvature_mesh_size: Activate the calculation of mesh element sizes based on curvature (with curvature_mesh_size elements per 2*Pi radians), defaults to 0 :type curvature_mesh_size: int, optional :param min_points: Check if there are enough points on small edges (if it is not, we force to have min_points on that edge), defaults to None :type min_points: int, optional :param initial_mesh_size: If factor=1, it will be initial_mesh_size elements per dimension, defaults to 5 :type initial_mesh_size: float, optional

Returns:

A txt file

Return type:

.txt

to_msh_file(mesh_dimension: int, factor: float, stream: StringFile, mesh_order: int = 1, file_name: str = '', **kwargs)#

Convert and write model to a .msh file.

to_step(filepath: str, preferences=None)#

Export a step file of the model.

to_step_stream(stream: StringFile, preferences=StepWriterPreferences(precision_mode=1, assembly=2, schema='AP214IS', surfacecurve_mode=0, unit='M'))#

Export object CAD to given stream in STEP format using the to_step method.

to_step_stream_volmdlr(stream: StringFile)#

Export object CAD to given stream in STEP format.

to_step_volmdlr(filepath: str)#

Export a step file of the model.

to_stl(filepath: str)#

Export a stl file of the model.

to_stl_model()#

Convert the model into a stl object.

to_stl_stream(stream: BinaryFile)#

Convert the model into a stl stream file.

translation(offset: Vector3D)#

Translate the VolumeModel.

Parameters:

offset – translation vector

Returns:

A new translated VolumeModel

static update_surfaces_list(face_contours, surfaces, contours, i)#

Update surfaces list.

volmdlr_volume_model()#

Get volume model.

volume() float#

Return the sum of volumes of the primitives.

It does not make any Boolean operation in case of overlapping.

volmdlr.model.get_ocaf_color_type(shape: Shape | TopoDS_Shape) XCAFDoc_ColorType#

Get the color type according to the shape type.

volmdlr.model.write_ocaf_doc_from_list_of_shapes(primitives: list[Primitive3D]) OCAF#

Create a OCAF document from a list o shapes.

Parameters:

primitives (list[volmdlr.core.Primitive3D]) – A list of 3D shapes.

Returns:

A OCAF object

Return type:

OCAF