Debian Codegraph @

examples/01-filter/extrude_trim.py

raw
1 """
2 .. _extrude_trim_example:
3
4 Extrude Trim
5 ~~~~~~~~~~~~
6 Extrude a :class:`pyvista.PolyData` with a :func:`pyvista.Plane` using
7 :func:`extrude_trim() <pyvista.PolyDataFilters.extrude_trim>`.
8
9 """
10
11 import pyvista as pv
12
13 # %%
14 # Generate an Extruded Surface
15 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16
17 # Create surface and plane
18 mesh = pv.ParametricRandomHills(random_seed=2)
19 plane = pv.Plane(
20 center=(mesh.center[0], mesh.center[1], -5),
21 direction=(0, 0, -1),
22 i_size=30,
23 j_size=30,
24 )
25
26 # Perform the extrude trim
27 extruded_hills = mesh.extrude_trim((0, 0, -1.0), plane)
28 extruded_hills
29
30
31 # %%
32 # Plot the Extruded Surface
33 # ~~~~~~~~~~~~~~~~~~~~~~~~~
34 # Plot the resulting :class:`pyvista.PolyData`.
35
36 # sphinx_gallery_start_ignore
37 # add_text does not show up in interactive
38 PYVISTA_GALLERY_FORCE_STATIC = True
39 # sphinx_gallery_end_ignore
40
41 pl = pv.Plotter(shape=(1, 2))
42 pl.add_mesh(mesh)
43 pl.add_text('Original Mesh')
44
45 pl.subplot(0, 1)
46 pl.add_mesh(plane, style='wireframe', color='black')
47 pl.add_mesh(extruded_hills)
48 pl.add_text('Extruded Mesh')
49
50 pl.link_views()
51 pl.camera_position = 'iso'
52 pl.camera.zoom(1.5)
53 pl.show()
54
55
56 # %%
57 # Extruding All Edges
58 # ~~~~~~~~~~~~~~~~~~~
59 # The previous example used the default ``extrusion='boundary_edges'``, which
60 # only generates faces on the boundary. When using ``extrusion='all_edges'``,
61 # interior edges are also created.
62
63 # Create a triangle.
64 disc = pv.Disc(c_res=3, r_res=4, inner=0)
65 plane = pv.Plane(
66 center=(disc.center[0], disc.center[1], -1),
67 direction=(0, 0, -1),
68 i_size=1,
69 j_size=1,
70 )
71
72 # extrude with and without the all_edges option
73 extruded_disc = disc.extrude_trim((0, 0, -1.0), plane)
74 extruded_disc_all = disc.extrude_trim((0, 0, -1.0), plane, extrusion='all_edges')
75 print(f'Extrusion has {extruded_disc.n_faces} faces with default boundary_edges')
76 print(f'Extrusion has {extruded_disc_all.n_faces} faces with all_edges')
77
78
79 # %%
80 # Plot
81 # ~~~~
82 # Show the additional interior faces by plotting with ``style='wireframe'``.
83
84 # sphinx_gallery_start_ignore
85 # add_text does not show up in interactive
86 PYVISTA_GALLERY_FORCE_STATIC = True
87 # sphinx_gallery_end_ignore
88 pl = pv.Plotter(shape=(1, 2))
89 pl.add_mesh(extruded_disc, style='wireframe', line_width=5)
90 pl.add_text('Extrude with boundary_edges')
91
92 pl.subplot(0, 1)
93 pl.add_mesh(extruded_disc_all, style='wireframe', line_width=5)
94 pl.add_text('Extrude with all_edges')
95
96 pl.link_views()
97 pl.camera_position = 'iso'
98 pl.camera.zoom(1.3)
99 pl.show()
100
101
102 # %%
103 # Extrude a Line
104 # ~~~~~~~~~~~~~~
105 # You can also extrude lines. Observe that the output from extruded lines is
106 # still a :class:`pyvista.PolyData`.
107
108 plane = pv.Plane(
109 center=(0, 0, 1), i_size=2, j_size=0.2, direction=[1, 1, 1], j_resolution=1
110 )
111 line = pv.Line()
112 extruded_line = line.extrude_trim((0, 0, 1), plane)
113 extruded_line
114
115
116 # %%
117 # Plot the Extruded Line
118 # ~~~~~~~~~~~~~~~~~~~~~~
119 # Note how the scalars are copied to the extruded line.
120
121 pl = pv.Plotter()
122 pl.add_mesh(line, style='wireframe', line_width=20, show_scalar_bar=False, color='r')
123 pl.add_mesh(plane, style='wireframe', color='black', show_scalar_bar=False)
124 pl.add_mesh(extruded_line, show_scalar_bar=False, lighting=False)
125 pl.show()
126 # %%
127 # .. tags:: filter