Interfacing MATLAB with CAD - EM Lab

5/12/2015
ECE 5390 Special Topics:
21st Century Electromagnetics
Instructor:
Office:
Phone:
E‐Mail:
Dr. Raymond C. Rumpf
A‐337
(915) 747‐6958
[email protected]
Spring 2014
Lecture #19
Synthesizing Geometries for
21st Century Electromagnetics
Interfacing MATLAB with CAD
Lecture 19
1
Lecture Outline
•
•
•
•
•
•
•
Text files in MATLAB
String manipulation in MATLAB
STL files
STL files in MATLAB
Visualizing surface meshes in MATLAB
Generating faces and vertices using MATLAB
Converting surfaces meshes from objects on a 3D
grid
• Point clouds
• Importing custom polygons into SolidWorks
Lecture 19
Slide 2
1
5/12/2015
Reading and
Writing Text Files
in MATLAB
Opening and Closing Files
A file is opened in MATLAB as read‐only with the following command.
% OPEN ASCII STL FILE
fid = fopen(‘pyramid.STL’,'r');
if fid==-1
error('Error opening file.');
end
%’r’ is read-only for safety
Use ‘w’ to write files.
A file is closed in MATLAB with the following command.
% CLOSE FILE
fclose(fid);
Open and closing the file is always the first and last thing you do.
WARNING! Always close open files!
Lecture 19
4
2
5/12/2015
Reading a Line from the Text
File
A line of text is read from the text file using the following command.
% READ A LINE OF TEXT
L = fgetl(fid);
You can read and display an entire text file with the following code.
% READ AND DISPLAY AN ENTIRE TEXT FILE
while feof(fid)==0
% Get Next Line from File
L = fgetl(fid);
% Display the Line of Text
disp(L);
end
Lecture 19
5
Writing a Line to the Text File
A line of text is written to the text file using fprintf().
% WRITE A LINE OF TEXT
fprintf(fid,’solid pyramid\r\n’);
This writes “solid pyramid” to the file followed by carriage line return.
Numbers can also be written to the file.
% Write Facet Normal to File
N = [ 1.234567 68.76543 1.592745 ];
L = '
facet normal %8.6f %8.6f %8.6f\r\n';
fprintf(fid,L,N);
This writes “
facet normal 1.234567e0 6.876543e1 1.592745e0” to the file followed by carriage line return.
Lecture 19
6
3
5/12/2015
ANSI Formatting -- Summary
ESCAPE CHARACTERS
CONVERSION CHARACTERS
Value Type
For most cases, \n is sufficient for a single line break.
However, if you are creating a file for use with Microsoft
Notepad, specify a combination of \r\n to move to a new line.
Conversion
Details
Integer, signed
%d or %i
Base 10 values
Integer, unsigned
%u
Base 10
Floating‐point number
%f
Fixed‐point notation
%e
Exponential notation, such as 3.141593e+00
%E
Same as %e, but uppercase, such as 3.141593E+00
%g The more compact of %e or %f, with no trailing zeros
%G
The more compact of %E or %f, with no trailing zeros
%c
Single character
%s
String of characters
Characters
Lecture 19
7
ANSI Formatting – Conversion
Characters
Lecture 19
8
4
5/12/2015
ANSI Formatting – Flags
Action
Flag
Example
Left‐justify.
'–'
%-5.2f
Print sign character (+ or –).
'+'
%+5.2f
Insert a space before the value.
' '
% 5.2f
Pad with zeros.
'0'
%05.2f
Modify selected numeric conversions: '#'
• For %o, %x, or %X, print 0, 0x, or 0X prefix.
• For %f, %e, or %E, print decimal point even when precision is 0.
• For %g or %G, do not remove trailing zeros or decimal point.
%#5.0f
Lecture 19
9
String
Manipulation in
MATLAB
5
5/12/2015
Parsing Strings
A line of text can be parsed into separate words using sscanf().
S = 'Hello Class';
T = sscanf(S,'%s %s');
T =
HelloClass
S = 'Hello Class';
T = sscanf(S,'%*s %s');
T =
Class
skip string
You can also extract numbers from a string using sscanf().
S = 'I am 25 years old and 6 feet tall.';
N = sscanf(S,'%*s %*s %f %*s %*s %*s %f %*s %*s');
N =
Lecture 19
25
6
11
Checking for Specific Words in
Strings (1 of 3)
You can perform an exact comparison between two strings using strcmp().
s1 = 'cat';
s2 = 'dog';
c = strcmp(s1,s2);
s1 = 'cat';
s2 = 'Cat';
c = strcmp(s1,s2);
s1 = 'cat';
s2 = 'cat';
c = strcmp(s1,s2);
Lecture 19
c =
0
c =
0
c =
1
12
6
5/12/2015
Checking for Specific Words in
Strings (2 of 3)
You can do the same camparison, but case insensitive using strcmpi().
s1 = 'cat';
s2 = 'dog';
c = strcmpi(s1,s2);
s1 = 'cat';
s2 = 'Cat';
c = strcmpi(s1,s2);
s1 = 'cat';
s2 = 'cat';
c = strcmpi(s1,s2);
c =
0
c =
1
c =
1
Lecture 19
13
Checking for Specific Words in
Strings (3 of 3)
You can find the occurrence of one string inside another using strfind().
s1 = 'University of Texas at El Paso';
s2 = 'Hawaii';
ind = strfind(s1,s2);
s1 = 'University of Texas at El Paso';
s2 = 'Texas';
ind = strfind(s1,s2);
Lecture 19
ind =
[]
ind =
15
14
7
5/12/2015
Converting Between Strings and
Numbers
You can convert a string to a number using str2num().
S = '534';
N = str2num(S);
N =
534
S = '2.74E-10';
N = str2num(S);
N =
2.74E-10
Similarly, you can convert numbers to strings using num2str().
N = 1234;
S = num2str(N);
S =
1234
N = 1.23456789;
S = num2str(N);
S =
1.2346
N = 1.23456789;
S = num2str(N,'%1.6f');
S =
1.234568
Lecture 19
15
STL Files
8
5/12/2015
What is an STL File?
STL – Standard Tessellation Language
This file format is widely used in rapid prototyping. It
contains only a triangular mesh of an objects surface.
Color, texture, materials, and other attributes are not
represented.
They can be text files or binary. Binary is more common
because they are more compact. We will look at text files
because that is more easily interfaced with MATLAB.
Lecture 19
Slide 17
Surface Mesh
Despite this sphere really being a solid object, it is represented in an STL file by just its surface.
Solid Object
STL Representation
Lecture 19
18
9
5/12/2015
STL File Format
solid name
facet normal nx ny nz
outer loop
vertex vx,1 vy,1 vz,1
vertex vx,2 vy,2 vz,2
vertex vx,3 vy,3 vz,3
endloop
endfacet
This set of text is repeated for every triangle on the surface of the object.
 
 
 v  v    v3  v1 
nˆ  2 1
 
 v2  v1    v3  v1 
endsolid name

v2

v1

v3
Bold face indicates a keyword; these must appear in lower case. Note that there is a space in “facet normal” and in
“outer loop,” while there is no space in any of the keywords beginning with “end.” Indentation must be with spaces;
tabs are not allowed. The notation, “{…}+,” means that the contents of the brace brackets can be repeated one or
more times. Symbols in italics are variables which are to be replaced with user-specified values. The numerical data
in the facet normal and vertex lines are single precision floats, for example, 1.23456E+789. A facet normal
coordinate may have a leading minus sign; a vertex coordinate may not.
Lecture 19
19
Example STL File
solid pyramid
facet normal -8.281842e-001 2.923717e-001 -4.781524e-001
outer loop
vertex 4.323172e-018 1.632799e-017 6.495190e-001
vertex 3.750000e-001 7.081604e-001 4.330127e-001
vertex 3.750000e-001 0.000000e+000 0.000000e+000
endloop
endfacet
facet normal 0.000000e+000 2.923717e-001 9.563048e-001
outer loop
vertex 7.500000e-001 0.000000e+000 6.495190e-001
vertex 3.750000e-001 7.081604e-001 4.330127e-001
vertex 0.000000e+000 0.000000e+000 6.495190e-001
endloop
endfacet
facet normal 8.281842e-001 2.923717e-001 -4.781524e-001
outer loop
vertex 3.750000e-001 -1.632799e-017 0.000000e+000
vertex 3.750000e-001 7.081604e-001 4.330127e-001
vertex 7.500000e-001 0.000000e+000 6.495190e-001
endloop
endfacet
facet normal 0.000000e+000 -1.000000e+000 0.000000e+000
outer loop
vertex 3.750000e-001 0.000000e+000 0.000000e+000
vertex 7.500000e-001 0.000000e+000 6.495190e-001
vertex 0.000000e+000 0.000000e+000 6.495190e-001
endloop
endfacet
endsolid pyramid
Lecture 19
An STL file is essentially just a list of all the triangles on the surface of an object.
Each triangle is defined with a surface normal and the position of the three vertices.
Slide 20
10
5/12/2015
A Single Triangle
facet normal -8.281842e-001 2.923717e-001 -4.781524e-001
outer loop
vertex 4.323172e-018 1.632799e-017 6.495190e-001
vertex 3.750000e-001 7.081604e-001 4.330127e-001
vertex 3.750000e-001 0.000000e+000 0.000000e+000
endloop
endfacet
1. Facet normal must follow right‐hand rule and point outward from object.
a) Some programs set this to [0;0;0] or convey shading information.
b) Don’t depend on it!
2. Adjacent triangles must have two common vertices.
3. STL files appear to be setup to handle arbitrary polygons. Don’t do this.
Vertex 3
Facet Normal
Vertex 1
Lecture 19
Vertex 2
Slide 21
Warnings About Facet Normals
• Since the facet normal can be calculated from the
vertices using the right-hand rule, sometimes the
facet normal in the STL file contains other
information like shading, color, etc.
• Don’t depend on the right-hand rule being
followed.
• Basically, don’t depend on anything!
Lecture 19
22
11
5/12/2015
Reading/Writing
STL Files in
MATLAB
How to Store the Data
We have N facets and ≤ 3N vertices to store in arrays.
F(N,3) Array of triangle facets
V(?,3) Array of triangle vertices
Many times, the number of vertices is 3N. Observing that many of the triangle facets share vertices, there will be redundant vertices Lecture 19
24
12
5/12/2015
Lazy Array of Vertices (1 of 2)
 vx ,1
v
x ,2
V 


vx , M
v y ,1
v y ,2

vy ,M
vz ,1 
vz ,2 


vz , M 
V is an array containing the position of all the vertices in Cartesian coordinates.
5
8
2
4
6
9
M is the total number of 1
vertices.
7
11
3
12
10
Lecture 19
25
Lazy Array of Vertices (2 of 2)
There is redundancy here. Twelve vertices are stored, but the device is really only composed of four.
While an inefficient representation, this is probably not worth your time fixing.
2,5,8
SolidWorks exports a lazy array of vertices.
 vx ,1
v
x ,2
V 


vx , M
Lecture 19
v y ,1
v y ,2

vy ,M
vz ,1 
vz ,2 


vz , M 
4,9,11
1,6,12
3,7,10
26
13
5/12/2015
Compact Array of Vertices
 vx ,1
v
x ,2
V 
 vx ,3

vx ,4
vz ,1 
vz ,2 
vz ,3 

vz ,4 
v y ,1
v y ,2
v y ,3
v y ,4
2
2
2
2
4
1
Array of Vertices, V
4
4
3
1
1
3
4
3
1
3
Lecture 19
27
Array of Faces
 n1,1
n
2,1
F 


 nN ,1
n1,2
n2,2

nN ,2
n1,3 
n2,3 


nN ,3 
F is an array indicating the array indices of the vertices defining the facet.
5
all integers
8
2
2
4
6
9
4
N is the total number of faces.
Lecture 19
7
1
1
3
11
3
12
10
28
14
5/12/2015
Example of Lazy Arrays
2,5,8
4,9,11
1,6,12
3,7,10
Lecture 19
29
Example of Compact Arrays
2
4
1
3
This can make a very large difference for large and complex objects.
Lecture 19
30
15
5/12/2015
How to Read an STL File Into
MATLAB
1. Open file as read-only.
2. Read first line and store object name if needed.
3. Read facet data
a. Read the facet normal. You probably don’t need this, and you can ignore it.
b. Skip outer loop line
c. Read vertex 1
d. Read vertex 2
e. Read vertex 3
f. Skip endloop line
g. Skip endfacet line
h. Add vertices to V array.
i. Add facet to F array.
4. If another facet exists, go back to Step 3.
5. Skip endsolid line
6. Close the file.
7. If desired, eliminate redundant vertices.
Lecture 19
31
How to Write an STL File From
MATLAB
1. Open a new text file.
2. Add first line, solid name.
See next slide.
3. For each triangle facet, repeat the following steps
  
a. Determine the vertices of the facet v1 , v2 , v3
b. Calculate the facet normal (nx,ny,nz) using the right-hand rule.
c. Write line facet normal nx ny nz
d. Write line outer loop
e. Write vertex vx,1 vy,1 vz,1
Be sure to put these in the order that follows the f. Write vertex vx,2 vy,2 vz,2
right‐hand rule!
g. Write vertex v v v
x,3
y,3
z,3
h. Write line endloop
i. Write line endfacet
4. If another facet exists, go back to Step 3.
5. Write line endsolid name
6. Close the file.
Lecture 19
32
16
5/12/2015
Reconciling the Surface Normal
for Objects on a Grid
Construct an object on a grid.
Generate a surface mesh of the object.
gradient
surface normal
Blur the object on the grid.
The surface normal should point in the direction opposite to the gradient of the blur.
Lecture 19
33
STL Files Generated by
SolidWorks
For some reason, Solidworks does not use the z‐axis as the vertical axis. For convenience, STL files can be easily reoriented.
% REORIENT SOLIDWORKS AXES TO MATLAB AXES
Y
= V(:,2);
V(:,2) = V(:,3);
V(:,3) = Y;
Orientation in SolidWorks
Lecture 19
Imported Orientation
Adjusted Orientation
34
17
5/12/2015
Visualizing
Surface Meshes
in MATLAB
How to Draw the Object
Given the faces and vertices, the object can be drawn in MATLAB using the patch() command.
% DRAW STRUCTURE USING PATCHES
P = patch('faces',F,'vertices',V);
set(P,'FaceColor','b','FaceAlpha',0.5);
set(P,'EdgeColor','k','LineWidth',2);
Lecture 19
36
18
5/12/2015
Generating
Faces and
Vertices Using
MATLAB
MATLAB Surfaces
Surfaces composed of square facets are stored in X, Y, and Z arrays.
The surface shown is constructed of arrays that are all 5×5.
Lecture 19
38
19
5/12/2015
Direct Construction of the
Surface Mesh
MATLAB has a number of built‐in commands for generating surfaces. Some of these are cylinder(), sphere() and ellipsoid().
% CREATE A UNIT SPHERE
[X,Y,Z] = sphere(41);
Surfaces can be converted to triangular patches (facets and vertices) using the surf2patch() function.
% CONVERT TO PATCH
[F,V] = surf2patch(X,Y,Z,’triangles’);
The faces and vertices can be directly visualized using the patch()
function.
% VISUALIZE FACES AND VERTICES
h = patch('faces',F,'vertices',V);
set(h,'FaceColor',[0.5 0.5 0.8],'EdgeColor','k');
Lecture 19
39
Grid  Surface Mesh
3D objects on a grid can be converted to a surface mesh using the command isosurface().
% CREATE ELLIPTICAL OBJECT
OBJ = (X/rx).^2 + (Y/ry).^2 + (Z/rz).^2;
OBJ = (OBJ < 1);
% CREATE SURFACE MESH
[F,V] = isosurface(X,Y,Z,OBJ,0.5);
Object on 3D Grid
Lecture 19
Surface Mesh
40
20
5/12/2015
Combining Faces and Vertices
from Two Objects
There are no functions in MATLAB to perform Boolean operations on multiple meshes. We can, however, combine the faces and vertices from two objects. Be careful this does not result in overlaps or gaps between objects.
F1 and V1
F2 and V2
Correctly
Combined
% COMBINE FACES AND VERTICES
F3 = [ F1 ; F2+length(V1(:,1)) ]
V3 = [ V1 ; V2 ]
Incorrectly
Combined
% WRONG WAY TO
% COMBINE FACES AND VERTICES
F3 = [ F1 ; F2 ]
V3 = [ V1 ; V2 ]
Lecture 19
41
Converting
Surface Meshes
to Objects on a
3D Grid
21
5/12/2015
Math Relating to Planes

v1
sˆ
The facet normal can be written as
sˆ  Axˆ  Byˆ  Czˆ

v3

v2
sˆ  1
The facet defines a plane. The standard equation of a plane in 3D space is
Ax  By  Cz  D  0
The equation for plane can be written in a vector notation as

sˆ  r  D  0

r  xxˆ  yyˆ  zzˆ

r0
We can determine D given any point on the plane .

sˆ  r0  D  0

 D   sˆ  r0
Lecture 19
43
Our Equation for a Plane
Using the new expression for D, the equation of the plane becomes

sˆ  r  D  0


sˆ  r  sˆ  r0  0
 
sˆ   r  r0   0

v1

v2
Lecture 19
sˆ

v3
44
22
5/12/2015
Which Side of a Plane Does a
Point Reside?

Given some point , which side of the plane does this point reside?
ri
 
sˆ   ri  r0   0 Opposite side as the normal
 
sˆ   ri  r0   0 On the plane
 
sˆ   ri  r0   0 Same side as the normal
Proof
Pick a point that is obviously on the same side as the normal.
 
ri  r0  sˆ

v1
sˆ
Substitute this into the equation of a plane and check the sign.
 
sˆ   ri  r0   ?


sˆ   r0  sˆ  r0   ?

v2
sˆ  sˆ  ?

v3
2
sˆ  sˆ  sˆ  0
Lecture 19
45
Initialize the Algorithm (1 of 2)
Step 1 – Determine the axis limits by looking at the vertices data.
% COMPUTE GRID LIMITS
xmin = min(V(:,1));
ymin = min(V(:,2));
zmin = min(V(:,3));
xmax = max(V(:,1));
ymax = max(V(:,2));
zmax = max(V(:,3));
Step 2 – Compute the 3D grid parameters.
% CALCULATE GRID
Sx = xmax - xmin;
Sy = ymax - ymin;
Sz = zmax - zmin;
Nx = 32;
Ny = round(Nx*Sy/Sx);
Nz = round(Nx*Sz/Sx);
xa = linspace(xmin,xmax,Nx);
ya = linspace(ymin,ymax,Ny);
za = linspace(zmin,zmax,Nz);
Lecture 19
46
23
5/12/2015
Initialize the Algorithm (2 of 2)
Step 3 – Compute the centers of the facets and the facet normals.
% COMPUTE FACET CENTERS AND NORMALS
FN = zeros(3,NF);
F0 = zeros(3,NF);
for nf = 1 : NF
% Calculate Vertices
v1 = [ V(F(nf,1),1) ; V(F(nf,1),2) ; V(F(nf,1),3) ];
v2 = [ V(F(nf,2),1) ; V(F(nf,2),2) ; V(F(nf,2),3) ];
v3 = [ V(F(nf,3),1) ; V(F(nf,3),2) ; V(F(nf,3),3) ];
% Calculate Facet Normal
p1 = v2 - v1;
Facet normal may already be p2 = v3 - v1;
known if the data was imported fn = cross(p2,p1);
fn = fn/norm(fn);
from an STL file.
FN(:,nf) = fn;
% Calculate Position
F0(:,nf) = (v1 + v2 + v3)/3;
end
Lecture 19
47
Fill the Grid
Step 4 – Loop through the grid, find the closest facet, determine which side of the facet the point is on, and fill in if inside object.
% FILL GRID
ER = zeros(Nx,Ny,Nz);
for nz = 1 : Nz
for ny = 1 : Ny
for nx = 1 : Nx
% Get Point
p = [ xa(nx) ; ya(ny) ; za(nz) ];
% Find Closest Facet
dV = [ F0(1,:)-p(1) ; F0(2,:)-p(2) ; F0(3,:)-p(3) ];
dV = sum(abs(dV).^2);
[v,ind] = min(dV);
ind = ind(1);
% Add Point if Inside Object
a = dot(FN(:,ind),p-F0(:,ind));
ER(nx,ny,nz) = (a<=0);
end
end
end
Lecture 19
48
24
5/12/2015
Example – Pyramid
ER(nx,ny,nz)
SolidWorks Model
Lecture 19
Import STL into MATLAB
Convert to Volume Object
49
Example – Dinosaur
ER(nx,ny,nz)
Import STL into MATLAB
Lecture 19
Convert to Volume Object
50
25
5/12/2015
Point Clouds
What is a Point Cloud?
Klein Bottle (see MATLAB demos)
Point Cloud Description of a Klein Bottle
Point clouds represent the outside surface of object as a set of vertices defined by X, Y, and Z coordinates. They are typically generated by 3D scanners, but can also be used to export 3D objects from MATLAB into SolidWorks or other CAD programs.
Lecture 19
52
26
5/12/2015
Other Examples of Point Clouds
Lecture 19
53
Point Cloud Data
The position of all the points in the point cloud can be stored in an array.
 x1
x
 2
PC   x3


 xN
PC =
0.1200
0.1159
0.0311
0.0000
-0.0311
-0.0600
Lecture 19
y1
y2
y3

yN
0.0000
-0.0311
-0.1159
-0.1200
-0.1159
-0.1039
z1 
z2 
z3 


z N 
0.7071
0.7071
0.7071
0.7071
0.7071
0.7071
54
27
5/12/2015
Saving Point Cloud Data to File
(1 of 2)
Point cloud data files are comma separated text files with the extension “.xyz”
These can be easily generated using the built‐in MATLAB command csvwrite().
% SAVE POINT CLOUD AS A COMMA SEPERATED FILE
PC = [ X Z Y ];
dlmwrite('pointcloud.xyz',PC,'delimiter',',','newline','pc');
PC = [ X Y Z ];
PC = [ X Z Y ];
Lecture 19
55
Saving Point Cloud Data to File
(2 of 2)
SolidWorks wants to see an X Y Z at the start of the file.
You can add this manually using Notepad or write a more sophisticated MATLAB text file creator.
Lecture 19
56
28
5/12/2015
Activate ScanTo3D Add-In in
SolidWorks
First, you will need to activate the ScanTo3D in SolidWorks.
Click ToolsAdd‐Ins.
Check the Scanto3D check box.
Click OK.
Lecture 19
57
Open the Point Cloud File
Lecture 19
58
29
5/12/2015
Run the Mesh Prep Wizard
ToolsScanTo3DMesh Prep Wizard…
Lecture 19
1.
2.
3.
4.
Run the Mesh Prep Wizard.
Select the point cloud.
Click the right array button.
Orientation method, select None because we did this in MATLAB.
5. Noise removal, zero assuming geometry came from MATLAB.
6. Work through all options.
7. Click the green check mark to finish.
59
Point Cloud Density
Lecture 19
60
30
5/12/2015
Final Notes on Point Clouds
• Other CAD packages have better point cloud
import capabilities than SolidWorks. Rhino 3D is
said to be excellent.
• Generating a solid model from the data can be
done in SolidWorks. The meshed model is
essentially used as a template for creating the
solid model. This procedure is beyond the scope
of this lecture.
Lecture 19
61
Importing
Custom
Polygons into
SolidWorks
31
5/12/2015
The Problem
Suppose we calculate the vertices of a polygon from an optimization in MATLAB. How do we important the vertices so that the polygon can be imported exactly into Solidworks so that it can be extruded, cut, modified, etc.?
There is no feature in SolidWorks to do this!!
Lecture 19
63
Example Application
Placing a GMR filter onto a curved surface.
Grating period is spatially varied to compensate.


sin  x R 
x
 tan 1 

R
1  d R  cos  x R  
K  x   K 0  k0 ninc sin inc  x  
inc  x  
x
  x    K  x  dx
0
 1
r  x  
r
cos   x    cos  f 
cos   x    cos  f 
R. C. Rumpf, M. Gates, C. L. Kozikowski, W. A. Davis, "Guided‐Mode Resonance Filter Compensated to Operate on a Curved Surface," PIER C, Vol. 40, pp. 93‐103, 2013.
Lecture 19
64
32
5/12/2015
Be Careful About XYZ Curves
There is a feature in SolidWorks “Curve Through XYZ Points” that appears to import discrete points, but it fits the points to a spline. This effectively rounds any corners and may produce intersections that cannot be rendered.
This should be a square!
The four points are fit to a spline!
Lecture 19
65
Step 1 – Define the Polygon
Create an N×3 array in memory containing the all the points in the polygon. N is the number of points.
P =
0.6448
1.0941
1.3427
0.3642
0.4753
-0.0651
-0.5258
-0.4824
-0.8912
-0.9966
-0.9087
-1.0666
-1.1762
-0.5403
-0.1403
0.3818
0.7795
0.8293
1.0972
0.6448
Lecture 19
0
0.3758
1.0452
0.5573
1.8765
0.7853
1.1984
0.5240
0.4822
0.1664
-0.1517
-0.5774
-1.2777
-1.2314
-1.6931
-1.5074
-1.1927
-0.6455
-0.3766
-0.0000
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
66
33
5/12/2015
Step 2 – Save as an IGES from
MATLAB
There is a function called igesout() available for free download from the Mathworks website. This will save your polygon to an IGES file.
% SAVE IGES FILE
igesout(P,'poly');
This creates a file called “poly.igs” in your working directory.
Lecture 19
67
Step 3 – Open the IGES in
SolidWorks (1 of 2)
Select the IGES file type in the [Open] file dialog. Then click on the [Options…] button. Lecture 19
68
34
5/12/2015
Step 3 – Open the IGES in
SolidWorks (2 of 2)
1. Make sure that “Surface/solid entities” is unchecked 2. Ensure that “Free point/curve entities” is checked. 3. Click on the “Import as sketch(es) radio button.
Open the file.
Lecture 19
69
Step 4 – Convert to a Standard
2D Sketch
The polygon imports as a 3D sketch by default.
Edit the 3D sketch and copy.
Open a new 2D sketch and paste.
Now you can do whatever you wish with the sketch! Extrude, revolve, cut extrude, etc.
Lecture 19
70
35
5/12/2015
Converting
Images and 2D
Objects to STL
Load and Resize the Image
% LOAD IMAGE
B = imread(‘letter.jpg');
% RESIZE IMAGE
B = imresize(B,0.2);
[Nx,Ny,Nc] = size(B);
This will give us a coarser mesh to be faster and more memory efficient.
Lecture 19
72
36
5/12/2015
Flatten the Image
Images loaded from file contain RGB information so they are 3D arrays. They must be converted to flat 2D arrays before meshing.
%
B
B
B
FLATTEN IMAGE
= double(B);
= B(:,:,1) + B(:,:,2) + B(:,:,3);
= 1 - B/765;
Lecture 19
73
Stack the Image
We will mesh the image using isocaps(), but that function requires a 3D array. So, we will stack this image to be two layers thick.
% STACK IMAGE
B(:,:,2) = B;
Lecture 19
74
37
5/12/2015
Mesh the Image Using
isocaps()
We only wish to mesh a single surface so we give isocaps(), the additional parameter ‘zmin’ to do this.
% CREATE 2D MESH
[F,V] = isocaps(ya,xa,[0 1],B,0.5,'zmin');
Lecture 19
75
Save the Mesh as an STL File
We can save this as an STL file.
% SAVE AS A STL FILE
stlwrite(‘letter.stl',F,V,'bin');
Lecture 19
76
38
5/12/2015
Extrude Using Blender (1 of 2)
1. Open Blender.exe.
2. File  Import  Stl (.stl)
3. Open the STL file you just created.
Lecture 19
77
Extrude Using Blender (2 of 2)
1. Select the object with right mouse click.
2. Press TAB to enter Edit mode.
3. Press ‘e’ to extrude mesh.
4. Press TAB again to exit Edit mode.
5. You can now edit your object
or export as a new 3D STL file.
Lecture 19
78
39
5/12/2015
STL to CAD
Conversion
Import STL Into MeshLab
1. Open meshlab.exe.
2. File  Import Mesh
3. Open the 3D STL file you just created.
Lecture 19
80
40
5/12/2015
Convert to SolidWorks Part
1.
2.
3.
4.
5.
6.
7.
8.
Open meshlab.exe.
File  Export Mesh As
Select DXF file type.
Save mesh.
Launch SolidWorks.
File  Open
Select DXF file.
Select ‘Import to a new part as:’ then ‘3D curves or model’ then ‘Next >.’
9. Select ‘All Layers’
Lecture 19
81
41