GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add 5 new jupyter notebooks · hovedguy/pythonocc-demos@688e195

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • jupyter_notebooks
    • core_topology_boolean.ipynb
    • core_topology_boolean_general_fuse_algorithm.ipynb
    • core_topology_boolean_shape_upgrade.ipynb
    • core_topology_draft_angle.ipynb
    • core_topology_prism.ipynb
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎jupyter_notebooks/core_topology_boolean.ipynb‎

Copy file name to clipboard
+266Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# PythonOCC - Example - Topology - Boolean"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"Example of the ***BRepAlgoAPI*** functions performing boolean operations: Fuse, Common, Section and Cut.<br>"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Fuse, BRepAlgoAPI_Common, BRepAlgoAPI_Section, BRepAlgoAPI_Cut\n",
24+
"from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeFace, BRepBuilderAPI_Transform\n",
25+
"from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeWedge, BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeTorus\n",
26+
"from OCC.Core.gp import gp_Vec, gp_Ax2, gp_Pnt, gp_Dir, gp_Pln, gp_Trsf"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 2,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer\n",
36+
"from OCC.Extend.TopologyUtils import shape_type_string, dump_topology_to_string"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 3,
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"from ipywidgets import interact, interactive, fixed, interact_manual\n",
46+
"import ipywidgets as widgets"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 4,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"def translate_topods_from_vector(brep_or_iterable, vec, copy=False):\n",
56+
" '''\n",
57+
" translate a brep over a vector\n",
58+
" @param brep: the Topo_DS to translate\n",
59+
" @param vec: the vector defining the translation\n",
60+
" @param copy: copies to brep if True\n",
61+
" '''\n",
62+
" trns = gp_Trsf()\n",
63+
" trns.SetTranslation(vec)\n",
64+
" brep_trns = BRepBuilderAPI_Transform(brep_or_iterable, trns, copy)\n",
65+
" brep_trns.Build()\n",
66+
" return brep_trns.Shape()"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 5,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"def fuse(event=None):\n",
76+
" box1 = BRepPrimAPI_MakeBox(2, 1, 1).Shape()\n",
77+
" box2 = BRepPrimAPI_MakeBox(2, 1, 1).Shape()\n",
78+
" box1 = translate_topods_from_vector(box1, gp_Vec(.5, .5, 0))\n",
79+
" fusedshp = BRepAlgoAPI_Fuse(box1, box2).Shape()\n",
80+
" \n",
81+
" rnd = JupyterRenderer()\n",
82+
" rnd.DisplayShape(fusedshp, render_edges=True)\n",
83+
" rnd.Display()"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 6,
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"def common(event=None):\n",
93+
" # Create Box\n",
94+
" axe = gp_Ax2(gp_Pnt(10, 10, 10), gp_Dir(1, 2, 1))\n",
95+
" Box = BRepPrimAPI_MakeBox(axe, 60, 80, 100).Shape()\n",
96+
" # Create wedge\n",
97+
" Wedge = BRepPrimAPI_MakeWedge(60., 100., 80., 20.).Shape()\n",
98+
" # Common surface\n",
99+
" CommonSurface = BRepAlgoAPI_Common(Box, Wedge).Shape()\n",
100+
"\n",
101+
" rnd = JupyterRenderer()\n",
102+
" rnd.DisplayShape(Box, transparency = True, opacity =0.2)\n",
103+
" rnd.DisplayShape(Wedge, transparency = True, opacity =0.2)\n",
104+
" rnd.DisplayShape(CommonSurface, render_edges=True)\n",
105+
" rnd.Display()"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 7,
111+
"metadata": {},
112+
"outputs": [],
113+
"source": [
114+
"def slicer(event=None):\n",
115+
" # Param\n",
116+
" Zmin, Zmax, deltaZ = -100, 100, 5\n",
117+
" # Note: the shape can also come from a shape selected from InteractiveViewer\n",
118+
" #if 'display' in dir():\n",
119+
" # shape = display.GetSelectedShape()\n",
120+
" #else:\n",
121+
" # Create the shape to slice\n",
122+
" shape = BRepPrimAPI_MakeSphere(60.).Shape()\n",
123+
" # Define the direction\n",
124+
" D = gp_Dir(0., 0., 1.) # the z direction\n",
125+
" # Perform slice\n",
126+
" sections = []\n",
127+
" #init_time = time.time() # for total time computation\n",
128+
" for z in range(Zmin, Zmax, deltaZ):\n",
129+
" # Create Plane defined by a point and the perpendicular direction\n",
130+
" P = gp_Pnt(0, 0, z)\n",
131+
" Pln = gp_Pln(P, D)\n",
132+
" face = BRepBuilderAPI_MakeFace(Pln).Shape()\n",
133+
" # Computes Shape/Plane intersection\n",
134+
" section_shp = BRepAlgoAPI_Section(shape, face)\n",
135+
" if section_shp.IsDone():\n",
136+
" sections.append(section_shp)\n",
137+
" #total_time = time.time() - init_time\n",
138+
" #print(\"%.3fs necessary to perform slice.\" % total_time)\n",
139+
" \n",
140+
" rnd = JupyterRenderer()\n",
141+
" rnd.DisplayShape(shape)\n",
142+
" rnd.Display()\n",
143+
" #for section_ in sections:\n",
144+
" # rnd.DisplayShape(section_.Shape())"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 8,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"def section(event=None):\n",
154+
" torus = BRepPrimAPI_MakeTorus(120, 20).Shape()\n",
155+
" radius = 120.0\n",
156+
" sections = []\n",
157+
" for i in range(-3, 4):\n",
158+
" # Create Sphere\n",
159+
" sphere = BRepPrimAPI_MakeSphere(gp_Pnt(26 * 3 * i, 0, 0), radius).Shape()\n",
160+
" # Computes Torus/Sphere section\n",
161+
" section_shp = BRepAlgoAPI_Section(torus, sphere, False)\n",
162+
" section_shp.ComputePCurveOn1(True)\n",
163+
" section_shp.Approximation(True)\n",
164+
" section_shp.Build()\n",
165+
" sections.append(section_shp)\n",
166+
"\n",
167+
" rnd = JupyterRenderer()\n",
168+
" rnd.DisplayShape(torus)\n",
169+
" rnd.Display()\n",
170+
" #for section_ in sections:\n",
171+
" # display.DisplayShape(section_.Shape())"
172+
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": 9,
177+
"metadata": {},
178+
"outputs": [],
179+
"source": [
180+
"def cut(event=None):\n",
181+
" # Create Box\n",
182+
" Box = BRepPrimAPI_MakeBox(200, 60, 60).Shape()\n",
183+
" # Create Sphere\n",
184+
" Sphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape()\n",
185+
" # Cut: the shere is cut 'by' the box\n",
186+
" Cut = BRepAlgoAPI_Cut(Sphere, Box).Shape()\n",
187+
" \n",
188+
" rnd = JupyterRenderer()\n",
189+
" rnd.DisplayShape(Box, transparency = True, opacity =0.2)\n",
190+
" rnd.DisplayShape(Cut, render_edges=True)\n",
191+
" rnd.Display()"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": 10,
197+
"metadata": {},
198+
"outputs": [],
199+
"source": [
200+
"def f(boolop):\n",
201+
" if boolop == 'fuse':\n",
202+
" fuse()\n",
203+
" elif boolop == 'common':\n",
204+
" common()\n",
205+
" elif boolop == 'slicer':\n",
206+
" slicer()\n",
207+
" elif boolop == 'section':\n",
208+
" section()\n",
209+
" elif boolop == 'cut':\n",
210+
" cut()"
211+
]
212+
},
213+
{
214+
"cell_type": "code",
215+
"execution_count": 11,
216+
"metadata": {},
217+
"outputs": [
218+
{
219+
"data": {
220+
"application/vnd.jupyter.widget-view+json": {
221+
"model_id": "8b526801764f4263b6dda7374db0f072",
222+
"version_major": 2,
223+
"version_minor": 0
224+
},
225+
"text/plain": [
226+
"interactive(children=(Dropdown(description='boolop', options=('fuse', 'common', 'slicer', 'section', 'cut'), v…"
227+
]
228+
},
229+
"metadata": {},
230+
"output_type": "display_data"
231+
}
232+
],
233+
"source": [
234+
"interact(f, boolop = ['fuse','common', 'slicer', 'section', 'cut']);"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": null,
240+
"metadata": {},
241+
"outputs": [],
242+
"source": []
243+
}
244+
],
245+
"metadata": {
246+
"kernelspec": {
247+
"display_name": "Python 3",
248+
"language": "python",
249+
"name": "python3"
250+
},
251+
"language_info": {
252+
"codemirror_mode": {
253+
"name": "ipython",
254+
"version": 3
255+
},
256+
"file_extension": ".py",
257+
"mimetype": "text/x-python",
258+
"name": "python",
259+
"nbconvert_exporter": "python",
260+
"pygments_lexer": "ipython3",
261+
"version": "3.6.6"
262+
}
263+
},
264+
"nbformat": 4,
265+
"nbformat_minor": 2
266+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.