Skip to content

Commit e6483c5

Browse files
committed
Add an example loading an stl file using numpy-stl
1 parent 4d6387e commit e6483c5

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
##Copyright 2024 Thomas Paviot ([email protected])
2+
##
3+
##This file is part of pythonOCC.
4+
##
5+
##pythonOCC is free software: you can redistribute it and/or modify
6+
##it under the terms of the GNU Lesser General Public License as published by
7+
##the Free Software Foundation, either version 3 of the License, or
8+
##(at your option) any later version.
9+
##
10+
##pythonOCC is distributed in the hope that it will be useful,
11+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
##GNU Lesser General Public License for more details.
14+
##
15+
##You should have received a copy of the GNU Lesser General Public License
16+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
17+
18+
import os
19+
20+
from OCC.Core.MeshDS import MeshDS_DataSource
21+
from OCC.Core.MeshVS import *
22+
23+
from OCC.Display.SimpleGui import init_display
24+
25+
import numpy as np
26+
import stl # numpy-stl
27+
28+
# load a stl file using numpy-stl
29+
stl_filename = os.path.join("..", "assets", "models", "fan.stl")
30+
stl_mesh = stl.mesh.Mesh.from_file(stl_filename)
31+
vertices = stl_mesh.vectors.reshape(-1, 3)
32+
faces = np.array(
33+
[[i, i + 1, i + 2] for i in range(0, len(vertices), 3)], dtype=np.int32
34+
)
35+
36+
# create data source
37+
a_data_source = MeshDS_DataSource(vertices, faces)
38+
a_mesh_prs = MeshVS_Mesh()
39+
a_mesh_prs.SetDataSource(a_data_source)
40+
a_builder = MeshVS_MeshPrsBuilder(a_mesh_prs)
41+
a_mesh_prs.AddBuilder(a_builder, True)
42+
43+
display, start_display, add_menu, add_function_to_menu = init_display()
44+
display.Context.Display(a_mesh_prs, True)
45+
display.FitAll()
46+
start_display()

0 commit comments

Comments
 (0)