Create STL from patch object or simlar

Hello,
Currently, I have several matrices that represent the (x,y) coordinates for contours. In the image shown, I am just plotting those contours using the 'patch' command. What I need to do, however, is export these contours (either individually, or all at once) as an STL file to be opened in a CAD software.
Matlab's 'stlwrite' command only accepts a triangulation object as an argument. However, currently, when I try to create a triangulation of one of the contours (in the image shown it is of the outermost contour), I get a distorted and incorrect representation.
It seems like this should be a simple task to do, so any help would be greatly appreciated.

3 commentaires

KSSV
KSSV le 3 Mar 2022
Attach your data.
Here are the contours. Thanks for taking a look
So it looks like you do the delaunayTriangulation w/ constraints is so that the the edges are known to Matlab for when you do 'isInterior'?

Connectez-vous pour commenter.

Réponses (1)

KSSV
KSSV le 3 Mar 2022
load('Contours.mat') ;
p = [perimeter1 ; perimeter2 ; perimeter3 ; perimeter4 ; perimeter5 ; perimeter6 ; perimeter7 ; perimeter8 ;
perimeter9] ;
id = knnsearch(p,perimeter1) ;
C = [id(1:end-1) id(2:end)];
% Make delaunay triangulation with constriants
dt = delaunayTriangulation(perimeter1,C) ;
% Get the interior triangular elements
tf = isInterior(dt) ;
% get points and nodes
p = dt.Points ;
t = dt.ConnectivityList ;
% Pick the required
t(~tf,:) = [] ;
% Make deluanay again
dt = triangulation(t,p);
triplot(dt)

5 commentaires

Thanks man, this worked great! You figured it out fast!
By the way, when I try to do it with 'perimeter 2' instead of 'perimeter 1', my Matlab keeps crashing
Holden Tranquillo
Holden Tranquillo le 5 Mar 2022
Modifié(e) : KSSV le 7 Mar 2022
The issue was in the way the variable 'id' was found. I have done it a different way. Please load the perimeters and then run the attached code if you can. It seems like the triangulations get shifted a little bit even though they seem accurate (if you compare the two plots you can see the difference)
%% Colorful Contours
col_list='ymcrgbk';
figure();
for i = 1:9
col=randi([1,7],1);
random_color=col_list(col);
final_color=['-' random_color];
var = eval(append('perimeter',num2str(i)));
contour = patch(gca,var(:,1),var(:,2),final_color);
hold on;
rotate(contour,[0 0 1],270);
end
%% STL Write
p = [perimeter1 ; perimeter2 ; perimeter3 ; perimeter4 ; perimeter5 ; perimeter6 ; perimeter7 ; perimeter8 ;
perimeter9] ;
col_list='ymcrgbk';
% Create STL's of Each Contour
for i = 1:9
filename = convertCharsToStrings(append('perimeter',num2str(i)));
temp = eval(filename);
% temp = perimeter2;
%id = knnsearch(p,temp) ;
id = linspace(1,length(temp),length(temp))';
C = [id(1:end-1) id(2:end)];
% Make delaunay triangulation with constraints
dt = delaunayTriangulation(temp,C) ;
% Get the interior triangular elements
tf = isInterior(dt) ;
% get points and nodes
points = dt.Points ;
t = dt.ConnectivityList ;
% Pick the required
t(~tf,:) = [] ; % only save elements of interior triangles
% Make deluanay again
dt = triangulation(t,points);
% Plot
col=randi([1,7],1);
random_color=col_list(col);
final_color=['-' random_color];
tri = triplot(dt,final_color);
rotate(tri,[0 0 1],270);
axis equal
hold on;
end
Using perimeter2 doesnot crash my MATLAB. You may consider using like shown below:
id = knnsearch(perimeter2,perimeter2) ;
C = [id(1:end-1) id(2:end)];
% Make delaunay triangulation with constriants
dt = delaunayTriangulation(perimeter2,C) ;
% Get the interior triangular elements
tf = isInterior(dt) ;
% get points and nodes
p = dt.Points ;
t = dt.ConnectivityList ;
% Pick the required
t(~tf,:) = [] ;
% Make deluanay again
dt = triangulation(t,p);
triplot(dt)
Hi KSSV, sorry to bother you again, but there is on emore thing I cannot seem to figure out. I am now using a filter on the boundary traces in order to smooth them out, and now during the STL creation, the 'isInterior' command returns all true, and thus the correct STLs are not being created. If you can, can you glance at this attached code and see if you can figure out why it returns all true? I cannot figure it out

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by