Plot a Cuboid with a cylindrical shaped hole in 3D

Hey community,
I would like to plot a shape based on the picture shown below. Most important part is the cuboid in the center with a cylinder hole. It works for the cuboid with 'patch', but i stuck with the cylinder-shaped cutout. Any function recommendation to plot such shape?

 Accepted Answer

I'm just going to put this here.
% needed to zip attachments for the forum
unzip things.zip
% read the file
fname = 'things/thing.stl';
T = stlread(fname);
% plot it
patch('faces',T.ConnectivityList,'vertices',T.Points, ...
'facecolor',[1 1 1], ...
'edgecolor','none');
% set up lighting, etc
az = 30;
l1 = lightangle(az,45);
l2 = lightangle(az+180,-45);
set(l1,'color',[1 0.9 0.3]);
set(l2,'color',[0.9 0.3 1]);
material dull
axis image
view(3)
grid on
Okay fine fine. Here's something simple just using patch()
% some dimensions
holed = 45;
boxw = 50;
boxd = 18;
% generate the vertex list
n = 100; % number of points for the hole
P = zeros(n + 4,3);
P(1:4,:) = [0 0 0; boxw 0 0; boxw boxw 0; 0 boxw 0];
th = linspace(0,360,n+1);
th = mod(th-135,360);
P(5:end,:) = [boxw/2 + holed/2*cosd(th(1:end-1).'), ...
boxw/2 + holed/2*sind(th(1:end-1).'), ...
zeros(n,1)];
P = repmat(P,[2,1]); % duplicate
P(n+5:end,3) = boxd; % offset z
% set some common patch parameters
patchargs = {'facecolor',[1 1 1],'edgecolor','none'};
% plot all the quadrilateral faces
% the four outer box faces
F = zeros(n+4,4);
for k = 1:4
os = mod([k k+1]-1,4)+1;
F(k,:) = [os n+4+flip(os)];
end
% the interior cylinder faces
for k = 1:n
os = mod([k k+1]-1,n)+1;
F(4+k,:) = [4+os n+8+flip(os)];
end
patch('faces',F,'vertices',P,patchargs{:})
% the two end faces aren't quads
F = [1:4 1 n+4:-1:5 n+4] + [0; n+4];
patch('faces',F,'vertices',P,patchargs{:})
% set up lighting, etc
az = 30;
l1 = lightangle(az,45);
l2 = lightangle(az+180,-45);
set(l1,'color',[1 0.9 0.3]);
set(l2,'color',[0.9 0.3 1]);
material dull
axis image
view(3)
grid on

2 Comments

Hi, I solved a simular approach with your provided solution via the multiple patch objects. After ploting and using this shapes in a MATLAB Tool I also would like to export a *.3mf-File of the constructed shape. By converting the rectangular defined shapes of the patch objects to triangular shaped ones by simply using:
[faces_object(:,[1,2,3]); faces_object(:,[3,4,1])];
I can use the write3mf-Function from the MATLAB File-Exhange (Link). Sadly the cylindrical cutouts are lost in this way and just a rectangular shape is shown. Is there a way to define this shape based on triangular faces or convert it into a correct triangular based definition of the shape?
The polygon created for the end faces is neither a set of triangles or quads, it's not even a square with a polygonal hole. It's a non-convex shape with a seam formed by two coincident edges. It would actually need to be triangulated, and I'm not sure if the seam will cause issues.
I suppose that for a simple case like this, you could do it manually by splitting up the circle into quadrants and creating triangles between those circle segments and the nearest box corner.
Otherwise you might use delaunayTriangulation() or something. I'm not really familiar with the triangulation tools, and I find the documentation sparse, but you might need to set up some edge constraints for the hole to work.

Sign in to comment.

More Answers (1)

[X,Y,Z]=meshgrid(-150:150);
V=(X.^2+Y.^2>=80^2 & max(abs(X),abs(Y))<=120);
V(:,:,[1,end])=0;
V=V+0.5*imerode(V,ones(3,3,3));
isosurface(X,Y,Z,double(V),1);

2 Comments

Just a note - imerode requires Image Processing Toolbox.
If needed, imerode can be avoided by doing instead,
V=V+0.5*( convn(V, ones(3,3,3),'same') == 27 ) ;

Sign in to comment.

Tags

Asked:

on 29 Oct 2023

Edited:

DGM
on 2 Jul 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!