Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Patch 2 STL

Subject: Patch 2 STL

From: Luciano Teresi

Date: 8 Jun, 2006 09:49:06

Message: 1 of 3

I would like to generate a STL file from a Matlab patch object (I
know there exist script for surf 2 STL, and STL 2 patch)

Is there anybody that knows how to do it?

Thanks,
L.T.

Subject: Patch 2 STL

From: Eric Carlson

Date: 8 Jun, 2006 09:55:10

Message: 2 of 3

Luciano Teresi wrote:
> I would like to generate a STL file from a Matlab patch object (I
> know there exist script for surf 2 STL, and STL 2 patch)
>
> Is there anybody that knows how to do it?
>
> Thanks,
> L.T.

if text output okay, try:

function stl_write(fv, name)
%routine to creat stl file from FV structure array
% fv is face-vertex structure array
% name is the file name (character string)
% either just give the 'root' file name or give 'root.stl'
lnm = length(name);
if name((lnm-3):(lnm-1))=='stl'
     label = name(1:lnm-3);
else
     label = name;
     name = sprintf('%s.stl', name);
end
v1=fv.vertices(fv.faces(:,2),:)-fv.vertices(fv.faces(:,1),:);
v2=fv.vertices(fv.faces(:,3),:)-fv.vertices(fv.faces(:,2),:);
Norms=cross3(v1,v2);
clear v1 v2
v1(:,1:3)=fv.vertices(fv.faces(:,1),1:3);
v2(:,1:3)=fv.vertices(fv.faces(:,2),1:3);
v3(:,1:3)=fv.vertices(fv.faces(:,3),1:3);
fid = fopen(name,'w');
fprintf(fid,'solid %s\n',label);
nf = length(fv.faces); %k = (1:nf)';
for k = 1:nf
fprintf(fid,'facet normal %5.5f %5.5f %5.5f\n outer loop\n vertex %5.5f
%5.5f %5.5f\n vertex %5.5f %5.5f %5.5f\n vertex %5.5f %5.5f %5.5f\n
endloop\n endfacet\n',...
     Norms(k,1),Norms(k,2),Norms(k,3), v1(k,1), v1(k,2),
v1(k,3),v2(k,1), v2(k,2), v2(k,3),v3(k,1), v3(k,2), v3(k,3) );
end
fprintf(fid,'endsolid %s\n',label);
fclose(fid);

function M=cross3(r,F)
% function to calculate normalized cross product rxF/|rxF|
% handles (same-size) arrays (n by 3) for r and F
%
       M = [(r(:,2).*F(:,3) - r(:,3).*F(:,2)) ...
            (r(:,3).*F(:,1) - r(:,1).*F(:,3)) ...
            (r(:,1).*F(:,2) - r(:,2).*F(:,1))];
       M_mag = sqrt(sum((M.*M)')');
       M(:,1) = M(:,1)./M_mag;
       M(:,2) = M(:,2)./M_mag;
       M(:,3) = M(:,3)./M_mag;

Subject: Patch 2 STL

From: Luciano Teresi

Date: 9 Jun, 2006 10:26:26

Message: 3 of 3

Dear Eric,

thanks a lot. It works and it is what I was looking for.

L.T.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us