Tutorial 02: Using the VLFL-Toolbox for STL-File Export and Import

2014-11-18: Tim C. Lueth, MIMED - Technische Universität München, Germany (URL: http://www.sg-lib.org) - Last Change: 2019-06-11

Complete List of all Tutorials with Publishable MATLAB Files of this Solid-Geoemtries Toolbox

The following topics are covered an explained in the specific tutorials:

Motivation for this tutorial: (Originally SolidGeometry 1.1 required)

2. Import and export of STL-files in ASCII format and binary format

Often it is useful to import surface data fo solid volumes from STL-Files generated by other programs such as CATIA, ProEngineer, Solidworks etc. On the other hand we want to export our data for documentation, 3D-printing or the exchange with other users. The STL-File format is the most common file format for surface models. It supports ascii-text-format and binary formatted files. For export and import we need a couple of functions:
close all;
PL=PLcircle(10);
[VL,FL]=VLFLofPLz (PL,30);
VLFLplot(VL,FL); view (-30,30); grid on;
VLFLwriteSTL(VL,FL,'STL-ASCII','by My Name');
WRITING STL FILE /Users/timlueth/Desktop/Toolbox_test/STL-ASCII.STL in ASCII MODE completed.
VLFLwriteSTLb(VL,FL,'STL-BINAR','by My Name');
WRITING STL (90 vertices, 176 facets) FILE /Users/timlueth/Desktop/Toolbox_test/STL-BINAR.STL in BINARY MODE completed.
Similar it is possible to read the files in again
close all;
[VL,FL]=VLFLreadSTL ('STL-ASCII');
LOADING ASCII STL-File: /Users/timlueth/Desktop/Toolbox_test/STL-ASCII.STL scaling factor: 1 Processing 1234 lines: Finishing solid AOI-LIB:"STL-ASCII by My Name" 11-Jun-2019 13:49:41 11-Jun-2019 13:49:41
figure(1); VLFLplot(VL,FL,'b'); view (-30,30); grid on;
[VL,FL]=VLFLreadSTLb ('STL-BINAR');
LOADING BINARY STL-File: /Users/timlueth/Desktop/Toolbox_test/STL-BINAR.STL Binary Header: COLOR=RGBA,MATERIAL=AAAABBBBCCCCDDDD;SOLID "STL-BINAR by My Name" 11-Jun-2019 13 Color of solid defined as: "k" Alpha of solid defined as: 65.00 Number of facets: 176 0..
figure(2); VLFLplot(VL,FL,'g'); view (-30,30); grid on;

3. Checking surface volume data and STL-files

Especially, when reading in STL-Files that are generated by other programs and libraries it makes sense to check the data quality. For that purpose there is a function that will be explained later in more detail. This function is called at the end of each STL import.
VLFLchecker(VL,FL); % Check the data structure
VLFLchecker: 90 vertices and 176 facets. 0 FACET PROBLEMS DETECTED (ERRORS) 0 VERTEX PROBLEMS DETECTED (OBSOLETE WARNING) 0 EDGE PROBLEMS DETECTED (NON MANIFOLD WARNING) 0 SOLID/EDGE PROBLEMS DETECTED (OPEN SOLID WARNING)
% There are some more procedures to view and analyze solid volumee data
BBofVL(VL)
ans = 1×6
-10.0000 9.9756 -9.9939 9.9939 0 30.0000
close all; VLFLplots4 (VL,FL,'g');
close all; VLFLseparate(VL,FL);
Analyzing 90 facets for separation z=[0.0mm|30.0mm] Object TEST-1 with 176 facets
MVL = 1×3
0 -10 0

4. Generation of text, numbers, characters and formulas as solid volume

Often you want to write some numbers or code on top of a solid object. For that purpose there is a currently slow function that is able to convert a Matlab-string (even with LaTex-code) into a solid object.
close all;
[VL,FL]=VLFLtextimage('The lazy dog!');
VLFLplot (VL,FL,'g'); view (-30,30);
[VL,FL,d]=VLFLtext('TL-MMXI-XII-XVII');
VLFLwriteSTL (VL,FL,'exp_2011_12_17', 'by Tim C Lueth');
WRITING STL FILE /Users/timlueth/Desktop/Toolbox_test/exp_2011_12_17.STL in ASCII MODE completed.

5. Turning and mirroring of solids by manipulating the vertex lists (VL)

Turning an object and mirroring is quite simple by exchanging a column of the vertex list to change the sign of a column. To show the use of the functions we generate first a simple roman date string as solid volume.
close all;
[VL,FL,d]=VLFLtext('TL-MMXI-XII-XVII'); VLFLplot(VL,FL,'r'); view(-30,30);
The functions for mirroring solid objects by manipulating the vertex list are the following:
close all, view (-30,30); grid on;
VLFLplot(VLswapX(VL),FL,'b'); % mirror at x-axis
VLFLplot(VLswapY(VL),FL,'y'); % mirror at y-axis
VLFLplot(VLswapZ(VL),FL,'m'); % mirror at z-axis
The functions for turning solid objects by manipulating the vertex list are the following:
close all, view (-30,30); grid on
VLFLplot(VL,FL,'r'); % original solid
VLFLplot(VLswapXY(VL),FL,'b'); % turn the x-axis to the y-axis
VLFLplot(VLswapXZ(VL),FL,'m'); % turn the x-axis to the z-axis
VLFLplot(VLswapYZ(VL),FL,'y'); % turn the y-axis to the z-axis
VLFLplot(VLswapZY(VL),FL,'c'); % turn the z-axis to the y-axis
VLFLplot(VLswapZX(VL),FL,'g'); % turn the z-axis to the x-axis
VLFLplot(VLswapYX(VL),FL,'w'); % turn the y-axis to the x-axis

6. Spatial transformation of solids by manipulating the vertex lists (VL)

All solid objects consisting of vertices and facets can be moved and rotated by only manipulating the vertex list (VL). Since the facet list is an index list, the facet list (FL) is not affected by a transformation of the vertex list. The following example generates a cylinder and perform different postion and orientation transformations.
closeall;
VLFLviewer([]);
PL=PLcircle(10); % define a base-contour
[VL,FL]=VLFLofPLz (PL,30); % extrude to a solid volume
VL=VLswapZX (VL); % swap X and X axis
VLFLplot(VL,FL); view (-30,30); grid on; % plot as red cylinder
In detail, there are five basic transformation functions for manipulation a vertex list (VL)
In addition to the already existing matlab functions rotx, roty, and rotz, two new functions are useful.
VL=VLtrans0 (VL); % Transformation into the origin (blue)
VLFLplot(VL,FL,'b'); view (-30,30);
VL=VLtrans1 (VL); % Transformation into quadrant 1 (black)
VLFLplot(VL,FL,'k'); view (-30,30);
VL=VLtransP (VL,[0 ;0; 30]); % Transformation upwards 30 mm (yellow)
VLFLplot(VL,FL,'y'); view (-30,30);
VL=VLtransR (VL,rotdeg(0,30,15)); % Rotate 30 degree around y and 15 around z (magenta)
VLFLplot(VL,FL,'m'); view (-30,30);
T=[rotdeg(0,30,15), [20;0;0];[0 0 0 1]] % define a homogenous transformation matrix (green)
T = 4×4
0.8365 -0.2241 0.5000 20.0000 0.2588 0.9659 0 0 -0.4830 0.1294 0.8660 0 0 0 0 1.0000
VL=VLtransT (VL,T); % Transformation using an HT matrix
VLFLplot(VL,FL,'g'); view (-30,30); grid on;
VLFLplotlight (1,0.9); grid on;

Final remarks on toolbox version and execution date

VLFLlicense
This VLFL-Lib, Rel. (2019-Jun-11), is for limited non commercial educational use only! Licensee: Tim Lueth (Development Version)! Please contact Tim Lueth, Professor at TU Munich, Germany! WARNING: This VLFL-Lib (Rel. ) license will exceed at 14-Mar-2074 13:49:48! Executed 11-Jun-2019 13:49:50 by 'timlueth' using Matlab 9.6.0.1114505 (R2019a) Update 2 on a MACI64 ======================================== Used Matlab products: ====================================== database_toolbox image_toolbox map_toolbox matlab robotics_system_toolbox simmechanics simscape simulink =====================================================================================================