Skip to content

Commit 7857b0b

Browse files
jf---tpaviot
authored andcommitted
example boolean operations
1 parent 2143c6f commit 7857b0b

1 file changed

Lines changed: 151 additions & 0 deletions

File tree

examples/core_topology_boolean.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
##Copyright 2009-2016 Thomas Paviot (tpaviot@gmail.com)
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+
import sys
18+
import time
19+
20+
from OCC.BRepAlgoAPI import BRepAlgoAPI_Fuse, BRepAlgoAPI_Common, BRepAlgoAPI_Section, BRepAlgoAPI_Cut
21+
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeFace, BRepBuilderAPI_Transform
22+
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeWedge, BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeTorus
23+
from OCC.Display.SimpleGui import init_display
24+
from OCC.gp import gp_Vec, gp_Ax2, gp_Pnt, gp_Dir, gp_Pln, gp_Trsf
25+
26+
display, start_display, add_menu, add_function_to_menu = init_display()
27+
28+
29+
def translate_topods_from_vector(brep_or_iterable, vec, copy=False):
30+
'''
31+
translate a brep over a vector
32+
@param brep: the Topo_DS to translate
33+
@param vec: the vector defining the translation
34+
@param copy: copies to brep if True
35+
'''
36+
trns = gp_Trsf()
37+
trns.SetTranslation(vec)
38+
brep_trns = BRepBuilderAPI_Transform(brep_or_iterable, trns, copy)
39+
brep_trns.Build()
40+
return brep_trns.Shape()
41+
42+
43+
def fuse(event=None):
44+
display.EraseAll()
45+
box1 = BRepPrimAPI_MakeBox(2, 1, 1).Shape()
46+
box2 = BRepPrimAPI_MakeBox(2, 1, 1).Shape()
47+
box1 = translate_topods_from_vector(box1, gp_Vec(.5, .5, 0))
48+
fuse = BRepAlgoAPI_Fuse(box1, box2).Shape()
49+
display.DisplayShape(fuse)
50+
display.FitAll()
51+
52+
53+
def common(event=None):
54+
# Create Box
55+
axe = gp_Ax2(gp_Pnt(10, 10, 10), gp_Dir(1, 2, 1))
56+
Box = BRepPrimAPI_MakeBox(axe, 60, 80, 100).Shape()
57+
# Create wedge
58+
Wedge = BRepPrimAPI_MakeWedge(60., 100., 80., 20.).Shape()
59+
# Common surface
60+
CommonSurface = BRepAlgoAPI_Common(Box, Wedge).Shape()
61+
62+
display.EraseAll()
63+
ais_box = display.DisplayShape(Box)
64+
ais_wedge = display.DisplayShape(Wedge)
65+
display.Context.SetTransparency(ais_box, 0.8)
66+
display.Context.SetTransparency(ais_wedge, 0.8)
67+
display.DisplayShape(CommonSurface)
68+
display.FitAll()
69+
70+
71+
def slicer(event=None):
72+
# Param
73+
Zmin, Zmax, deltaZ = -100, 100, 5
74+
# Note: the shape can also come from a shape selected from InteractiveViewer
75+
if 'display' in dir():
76+
shape = display.GetSelectedShape()
77+
else:
78+
# Create the shape to slice
79+
shape = BRepPrimAPI_MakeSphere(60.).Shape()
80+
# Define the direction
81+
D = gp_Dir(0., 0., 1.) # the z direction
82+
# Perform slice
83+
sections = []
84+
init_time = time.time() # for total time computation
85+
for z in range(Zmin, Zmax, deltaZ):
86+
# Create Plane defined by a point and the perpendicular direction
87+
P = gp_Pnt(0, 0, z)
88+
Pln = gp_Pln(P, D)
89+
face = BRepBuilderAPI_MakeFace(Pln).Shape()
90+
# Computes Shape/Plane intersection
91+
section = BRepAlgoAPI_Section(shape, face)
92+
if section.IsDone():
93+
sections.append(section)
94+
total_time = time.time() - init_time
95+
print("%.3fs necessary to perform slice." % total_time)
96+
97+
display.EraseAll()
98+
display.DisplayShape(shape)
99+
for section in sections:
100+
display.DisplayShape(section.Shape())
101+
display.FitAll()
102+
103+
104+
def section(event=None):
105+
Torus = BRepPrimAPI_MakeTorus(120, 20).Shape()
106+
radius = 120.0
107+
sections = []
108+
for i in range(-3, 4):
109+
# Create Sphere
110+
Sphere = BRepPrimAPI_MakeSphere(gp_Pnt(26 * 3 * i, 0, 0), radius).Shape()
111+
# Computes Torus/Sphere section
112+
section = BRepAlgoAPI_Section(Torus, Sphere, False)
113+
section.ComputePCurveOn1(True)
114+
section.Approximation(True)
115+
section.Build()
116+
sections.append(section)
117+
118+
display.EraseAll()
119+
display.DisplayShape(Torus)
120+
for section in sections:
121+
display.DisplayShape(section.Shape())
122+
display.FitAll()
123+
124+
125+
def cut(event=None):
126+
# Create Box
127+
Box = BRepPrimAPI_MakeBox(200, 60, 60).Shape()
128+
# Create Sphere
129+
Sphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape()
130+
# Cut: the shere is cut 'by' the box
131+
Cut = BRepAlgoAPI_Cut(Sphere, Box).Shape()
132+
display.EraseAll()
133+
ais_box = display.DisplayShape(Box)
134+
display.Context.SetTransparency(ais_box, 0.8)
135+
display.DisplayShape(Cut)
136+
display.FitAll()
137+
138+
139+
def exit(event=None):
140+
sys.exit()
141+
142+
143+
if __name__ == '__main__':
144+
add_menu('topology boolean operations')
145+
add_function_to_menu('topology boolean operations', fuse)
146+
add_function_to_menu('topology boolean operations', common)
147+
add_function_to_menu('topology boolean operations', cut)
148+
add_function_to_menu('topology boolean operations', section)
149+
add_function_to_menu('topology boolean operations', slicer)
150+
add_function_to_menu('topology boolean operations', exit)
151+
start_display()

0 commit comments

Comments
 (0)