forked from tpaviot/pythonocc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_simple_mesh.py
78 lines (72 loc) · 2.82 KB
/
core_simple_mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
##Copyright 2009-2014 Thomas Paviot (tpaviot@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU Lesser General Public License for more details.
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
from OCC.BRep import BRep_Builder, BRep_Tool_Triangulation
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere
from OCC.BRepAlgoAPI import BRepAlgoAPI_Fuse
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.BRepMesh import brepmesh_Mesh
from OCC.TopExp import TopExp_Explorer
from OCC.TopoDS import TopoDS_Compound, topods_Face, topods_Edge
from OCC.TopAbs import TopAbs_FACE
from OCC.TopLoc import TopLoc_Location
from OCC.gp import gp_Pnt
from OCC.Display.SimpleGui import init_display
display, start_display, add_menu, add_function_to_menu = init_display()
display.SetSelectionModeVertex()
def simple_mesh():
#
# Create the shape
#
shape = BRepPrimAPI_MakeBox(200, 200, 200).Shape()
theBox = BRepPrimAPI_MakeBox(200, 60, 60).Shape()
theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape()
shape = BRepAlgoAPI_Fuse(theSphere, theBox).Shape()
#
# Mesh the shape
#
brepmesh_Mesh(shape, 0.8)
builder = BRep_Builder()
comp = TopoDS_Compound()
builder.MakeCompound(comp)
ex = TopExp_Explorer(shape, TopAbs_FACE)
while ex.More():
face = topods_Face(ex.Current())
location = TopLoc_Location()
facing = (BRep_Tool_Triangulation(face, location)).GetObject()
tab = facing.Nodes()
tri = facing.Triangles()
for i in range(1, facing.NbTriangles()+1):
trian = tri.Value(i)
index1, index2, index3 = trian.Get()
for j in range(1, 4):
if j == 1:
m = index1
n = index2
elif j == 2:
n = index3
elif j == 3:
m = index2
me = BRepBuilderAPI_MakeEdge(tab.Value(m), tab.Value(n))
if me.IsDone():
builder.Add(comp, me.Edge())
ex.Next()
display.EraseAll()
display.DisplayShape(shape)
display.DisplayShape(comp, update=True)
if __name__ == '__main__':
simple_mesh()
start_display()