Function Repository Resource:

ExtrudePolygon

Source Notebook

Create 3D shapes from 2D polygons via extrusion along the z axis, or create 3D extrusions of 3D polygons along a 3D vector

Contributed by: Sanay Nesargi

ResourceFunction["ExtrudePolygon"][polygon,{zmin,zmax}]

extrude 2D polygon from zmin to zmax.

ResourceFunction["ExtrudePolygon"][polygon,{vector},type]

extrude 3D polygon along 3D vector.

ResourceFunction["ExtrudePolygon"][polygon,{zmin,zmax},type]

close one or both ends with type.

Details

The argument polygon must have explicit head Polygon.
zmin and zmax are defined as relative coordinates, the length of extrusion will be the difference Abs[zmax-zmin]. As such, these coordinates are flexible enough to align in a scene with existing 3D geometry.
vector must be a list of 3 numeric values (e.g {0, 1, 1})
The argument type must be one of the following string values: "Open" (default), "Capped" or "Closed". "Open" is open on both ends. "Closed" is closed on one end. "Capped" is closed on both ends.

Examples

Basic Examples (4) 

Extrude a simple square and visualize with Graphics3D:

In[1]:=
extrusion = ResourceFunction["ExtrudePolygon"][
   Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}}], {0, 10}];
Graphics3D[{extrusion}]
Out[2]=

Specify a closed 3D shape (with closed bottom but open top):

In[3]:=
extrusion = ResourceFunction["ExtrudePolygon"][
   Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}}], {0, 10}, "Closed"];
Graphics3D[{extrusion}]
Out[4]=

Specify a capped 3D shape (with closed bottom and top):

In[5]:=
extrusion = ResourceFunction["ExtrudePolygon"][
   Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}}], {0, 10}, "Capped"];
Graphics3D[{extrusion}]
Out[6]=

Specify a 3D polygon and extrude it along a 3D vector:

In[7]:=
polygon3D = Polygon[{{0, 0, 0}, {1, 0, 0}, {0.5, 1, 0}, {0, 0, 0}}];
extrusion = ResourceFunction["ExtrudePolygon"][polygon3D, {0, 1, 1}, "Capped"];
Row[{Graphics3D[polygon3D, ImageSize -> Medium], Graphics3D[extrusion, ImageSize -> Medium]}]
Out[9]=

Scope (1) 

ExtrudePolygon can apply to arbitrary Cartesian plane polygons, even polygons representing other shapes such as a circle:

In[10]:=
circlePolygon[radius_, n_] := Polygon[Table[{radius Cos[2 Pi i/n], radius Sin[2 Pi i/n]}, {i, 0, n - 1}]];
circle = circlePolygon[1, 100]; 
circleExtruded = ResourceFunction["ExtrudePolygon"][circle, {0, 2}];
Graphics3D[circleExtruded]
Out[11]=

Neat Examples (2) 

It is possible to extrude a curve such as the one below:

In[12]:=
curvePolygon[radius_, n_] := Polygon[Table[{radius 2 Pi i/n, radius Sin[2 Pi i/n]}, {i, 0, n - 1}]];
curve = curvePolygon[1, 100]; 
curveExtruded = ResourceFunction["ExtrudePolygon"][curve, {0, 2}];
Graphics3D[curveExtruded]
Out[13]=

This function can be used to export 2D geometry into 3D object files:

In[14]:=

 fanBlades[mu_, n_, theta_, bladeLength_ : 1.5, bladeWidth_ : 0.03, bladeDepth_ : 5] := Module[{thetaVal, vertices1, vertices2, extrudedShape1, extrudedShape2, rotatedShapes}, thetaVal = Pi mu + theta;
  vertices1 = {{-bladeWidth*7.5, 0}, {bladeWidth*7.5, 0}, {bladeWidth*7.5, bladeDepth}, {-bladeWidth*7.5, bladeDepth}};
  extrudedShape1 = ResourceFunction["ExtrudePolygon"][
    Polygon[vertices1], {0, bladeLength*1.8*7.5}, "Capped"];
  rotatedShapes = Table[Rotate[{extrudedShape1}, (i 2) (Pi/n), {0, 1, 0}], {i, n}];
  {Gray, Thick, Rotate[rotatedShapes, thetaVal, {0, 1, 0}]}]
object = Translate[
   Rotate[fanBlades[0, 12, 0], Pi, Normalize[{1, 1, 0}]], {100, 0, 0}];

Visualize the 3D Fan Blades:

In[15]:=
Graphics3D[object]
Out[15]=

Export the now 3D object file to a standard format (e.g. STL):

In[16]:=
Export["threeDimensionalObject.stl", object];

Publisher

Sanay Nesargi

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 13 January 2025

Related Resources

License Information