Code covered by the BSD License  

Highlights from
Generate U3D files from STL models for making multilayer 3D PDF figures

image thumbnail

Generate U3D files from STL models for making multilayer 3D PDF figures

by

 

11 May 2011 (Updated )

Conversion of meshes into U3D files for making multilayer 3D PDF figures with transparency.

installGoogleArchive(downloadLocation, zipFile)
function installGoogleArchive(downloadLocation, zipFile)
% INSTALLGOOGLEARCHIVE Install files from a zip file stored on Google Sites.
% 
% The Mathworks file exchange no longer allows executables to be
% distributed in packages. This function allows the executables to be
% stored on Google sites then downloaded automatically when they're needed.
%
% This is just to make use of executable code seamless for people
% downloading packages. Google Sites file attachments have URL redirection.
% This looks for the file, then uses the redirected URL to download and
% unpack the zip to the current folder. Then the intermediate html and zip
% files are removed. If you don't want to host the unallowed files through
% Google Sites, you don't need this, but it still suggests a way for
% installing unallowed dependencies seamlessly.
%
% Usage:
%   installGoogleArchive(downloadLocation, zipFile);
%
% The downloadLocation should be a fully qualified URL, and the zipFile
% should be the name of the temporary archive (it will be removed if the
% installation is successful). The program will return an error if the
% download fails.
%
% Example:
%   Create a file test.m, with a single line containing "installGoogleArchive();"
%   Run test.m at the command line. If you are connected to the internet, a
%   folder titled "win32" should appear with an executable
%   "IDTFConverter.exe" inside.
%
% For more information, see the initial usage in:
%   http://www.mathworks.com/matlabcentral/fileexchange/31413
% 
% Francis Esmonde-White, francis@esmonde-white.com, May 2011

if nargin<3
    downloadLocation = 'http://www.esmonde-white.com/home/diversions/matlab-program-for-making-3d-pdf-figures/win32_3dpdf.zip?attredirects=0';
%     redirectFile = 'win32_3dpdf_tmp.html';
    zipFile = 'win32_3dpdf.zip';
end

st=dbstack('-completenames'); % this is just some text to make sure the user knows what is happening.
st = st(2);
warning(['The function installGoogleArchive() has just been executed.\n',...
            'This function has downloaded a file named ',zipFile,' to your computer from:\n', downloadLocation, ' \n',...
            'This is probably due to an installation process for a package required for a Matlab tool.\n',...
            'The download is happening as part of the matlab file:\n \t', st.name],1);
disp(st.file);


% Read the file into a string directly. Improvement suggested by Jan Simon.
[str,status] = urlread(downloadLocation); % grab the redirect file from Google

if (status == 0)
    error('The installation failed. The redirect file was not read. There may be a problem with the internet connection or with the download location.');
end

% % Don't actually download the file. Improvement suggested by Jan Simon.
% [filestr,status] = urlwrite(downloadLocation,redirectFile); % grab the redirect file from Google
% fid = fopen(filestr); % open the redirect file.
% C = textscan(fid, '%s'); % read the redirect file into C.
% fclose(fid);
% 
% delete(redirectFile); % remove the redirect file
% 
% C = C{1}; % convert C into a cell array
% 
% str = '';
% for i=1:numel(C);
%     str = strcat(str,' ',C{i}); % create a single string from the downloaded redirect file
% end

indexA = strfind(str,'<A HREF="') + 9;
indexB = strfind(str,';attredirects=0">here</A>');

downloadURL = str(indexA:indexB); % 

disp('Please wait, the archive file is downloading.');

[filestr,status] = urlwrite(downloadURL,zipFile); % download the zip file

if (status == 0)
    error('The installation failed. The file was not downloaded. There may be a problem with the internet connection.');
end

disp('Please wait, the archive file is being unpacked.');

unzip(zipFile); % extract the zip file contents.

delete(zipFile); % remove the zip file

disp(['The file ',zipFile,' has been installed.']);

Contact us