Changing from 2D plot to 3D plot and to STL file

7 visualizaciones (últimos 30 días)
Anh Phan Viet
Anh Phan Viet el 14 de Mzo. de 2020
Respondida: Deepak el 2 de Dic. de 2024
Hello everyone, I have these following codes: All of them creates a 2D plot, now I really want to create a 3D plot with Z: the thickness of plot XY. Then I want to convert it to STL file.
clc;
clear all;
close all;
% Input
R0 = 0.15;
a = 25*pi/180;
theta = 0:pi/1000:3*pi;
theta2 = theta + pi;
delta = pi/2;
%% Curl 1
R1 = R0*exp(a*(theta-delta));
X1 = R1.*cos(theta-delta);
Y1 = R1.*sin(theta-delta);
%% Curl 2
R2 = R0*exp(a*(theta-delta));
X2 = R2.*cos(theta);
Y2 = R2.*sin(theta);
%% Curl 3
R3 = R0*exp(a*(theta2-delta));
X3 = R1.*cos(theta2-delta);
Y3 = R1.*sin(theta2-delta);
%% Curl 4
R4 = R0*exp(a*(theta2 - delta));
X4 = R2.*cos(theta2);
Y4 = R2.*sin(theta2);
%% Extra plot (arc)
radius = R0*exp(a*(3*pi-pi/2));
a1 = pi/2;
b1 = pi;
h = 0;
k = 0;
t1 = linspace(a1,b1,3001);
X_arc1 = radius*cos(t1) + h;
Y_arc1 = radius*sin(t1) + k;
a2 = -pi/2;
b2 = 0;
t2 = linspace(a2,b2,3001);
X_arc2 = radius*cos(t2) + h;
Y_arc2 = radius*sin(t2) + k;
R5 = 0.05;
X5 = R5.*cos(theta);
Y5 = R5.*sin(theta);
%% Plot
figure(1);
plot(X1,Y1, 'k', 'LineWidth', 3); hold on
plot(X2,Y2, 'k', 'LineWidth', 3); hold on
P1 = plot(X_arc1, Y_arc1, 'k');
set(P1,'linewidth',3);
axis([h-radius-1 h+radius+1 k-radius-1 k+radius+1])
axis square; hold on;
plot(X3,Y3, 'k', 'LineWidth', 3);hold on
plot(X4,Y4, 'k', 'LineWidth', 3);hold on
plot(X5,Y5, 'k', 'LineWidth', 3); hold on
P2 = plot(X_arc2, Y_arc2, 'k');
set(P2,'linewidth',3);
axis('equal');
title('Two-arm equiangular spiral antenna', 'fontsize',14, 'fontweight','bold');
hold on
%% Filling with black color
patch([X1; flipud(X2)], [Y1; flipud(Y2)],'k');
patch([X3; flipud(X4)], [Y3; flipud(Y4)],'k');
X_arc1_bonus = horzcat(X_arc1, -4, -2.2, -2, -1, -0.3);
Y_arc1_bonus = horzcat(Y_arc1, 0.8, 2.2, 2.6, 3, 3.2);
fill(X_arc1_bonus, Y_arc1_bonus,'k');
X_arc2_bonus = horzcat(X_arc2, 4, 2.2, 2, 1, 0.3);
Y_arc2_bonus = horzcat(Y_arc2, -0.8, -2.2, -2.6, -3, -3.2);
fill(X_arc2_bonus, Y_arc2_bonus,'k');

Respuestas (1)

Deepak
Deepak el 2 de Dic. de 2024
To create a 3D plot with thickness by extruding 2D shapes into the third dimension, we can use "fill3" function in MATLAB.
Initially, the 2D shapes are defined by their X and Y coordinates. These shapes are then duplicated along the Z-axis to form both the bottom (Z = 0) and top (Z = thickness) surfaces, effectively giving them volume. The "fill3" function is used to plot these surfaces by specifying the X, Y, and Z coordinates for each shape. To complete the 3D model, side walls are created by connecting corresponding points on the top and bottom surfaces, forming vertical quadrilateral patches. This process results in a solid 3D object with defined thickness, visualized in a 3D space using plotting capabilities of MATLAB.
Below is a sample MATLAB to achieve the same:
% Input parameters
R0 = 0.15;
a = 25*pi/180;
theta = 0:pi/1000:3*pi;
theta2 = theta + pi;
delta = pi/2;
thickness = 0.05; % Thickness for the 3D extrusion
% Curl 1
R1 = R0*exp(a*(theta-delta));
X1 = R1.*cos(theta-delta);
Y1 = R1.*sin(theta-delta);
% Curl 2
R2 = R0*exp(a*(theta-delta));
X2 = R2.*cos(theta);
Y2 = R2.*sin(theta);
% Curl 3
R3 = R0*exp(a*(theta2-delta));
X3 = R1.*cos(theta2-delta);
Y3 = R1.*sin(theta2-delta);
% Curl 4
R4 = R0*exp(a*(theta2 - delta));
X4 = R2.*cos(theta2);
Y4 = R2.*sin(theta2);
% Extra plot (arc)
radius = R0*exp(a*(3*pi-pi/2));
a1 = pi/2;
b1 = pi;
h = 0;
k = 0;
t1 = linspace(a1,b1,3001);
X_arc1 = radius*cos(t1) + h;
Y_arc1 = radius*sin(t1) + k;
a2 = -pi/2;
b2 = 0;
t2 = linspace(a2,b2,3001);
X_arc2 = radius*cos(t2) + h;
Y_arc2 = radius*sin(t2) + k;
% Create a 3D plot with thickness
figure;
hold on;
% Extrude each 2D shape into 3D
fill3([X1; flipud(X2)], [Y1; flipud(Y2)], zeros(size([X1; flipud(X2)])), 'k');
fill3([X1; flipud(X2)], [Y1; flipud(Y2)], thickness * ones(size([X1; flipud(X2)])), 'k');
fill3([X3; flipud(X4)], [Y3; flipud(Y4)], zeros(size([X3; flipud(X4)])), 'k');
fill3([X3; flipud(X4)], [Y3; flipud(Y4)], thickness * ones(size([X3; flipud(X4)])), 'k');
% Extrude arcs
fill3(X_arc1, Y_arc1, zeros(size(X_arc1)), 'k');
fill3(X_arc1, Y_arc1, thickness * ones(size(X_arc1)), 'k');
fill3(X_arc2, Y_arc2, zeros(size(X_arc2)), 'k');
fill3(X_arc2, Y_arc2, thickness * ones(size(X_arc2)), 'k');
% Connect top and bottom faces to create side walls
for i = 1:length(X1)-1
fill3([X1(i), X1(i+1), X1(i+1), X1(i)], [Y1(i), Y1(i+1), Y1(i+1), Y1(i)], [0, 0, thickness, thickness], 'k');
end
% Repeat similarly for other parts...
axis equal;
view(3);
title('3D Model of Two-arm Equiangular Spiral Antenna');
Please find attached documentation of funcitons used for reference:
I hope this helps in resolving the issue.

Categorías

Más información sobre Lighting, Transparency, and Shading en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by