summaryrefslogtreecommitdiff
path: root/CSXCAD/matlab/export_excellon.m
blob: 8877fcd37a3e9e4ce4694642b28dd6d7e725234f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function export_excellon( CSX, filename, options )
% export_excellon( CSX, filename, options )
%
% Exports the geometry defined in CSX to filename as an excellon drill file.
% Only cylinders will be considered for export.
% The xy-plane is exported.
%
% CSX: CSX-object created by InitCSX()
% filename: export filename (e.g. '/tmp/export.gbr')
% options (optional): struct
%   .header: (string) add this to the header of the file
%      comment line must have ';' as the very first character
%   .exclude: (cell array) ignore these CSX-properties
%   .include: (cell array) include only these CSX-properties
%      if neither .exclude or .include is specified, process all properties
%
% See also InitCSX 
% CSXCAD matlab interface
% -----------------------
% author: Sebastian Held <sebastian.held@gmx.de>
% 26. Aug 2010: initial version


if nargin < 3
	options = [];
end

fid = fopen( filename, 'w' );

% create header
header = [];
header = [header 'M48\n'];
header = [header '; EXCELLON file written by CSXCAD\n' ];
header = [header '; ' datestr(now) '\n' ];
header = [header 'METRIC,LZ\n'];
header = [header 'VER,1\n'];
header = [header 'FMAT,1\n'];

if isfield(options,'header')
	header = [header options.header];
end

% determine the drawing unit
options.drawingunit = CSX.RectilinearGrid.ATTRIBUTE.DeltaUnit;

if isfield(CSX.Properties,'Material')
	% process material
	Material = CSX.Properties.Material;
	options.Property = 'Material';
	[tools,drill] = process_primitives( Material, options );
end
if isfield(CSX.Properties,'Metal')
	% process PEC
	Metal = CSX.Properties.Metal;
	options.Property = 'Metal';
	[tools_,drill_] = process_primitives( Metal, options );
    tools = unique( [tools tools_] );
    drill = [drill drill_];
end

% check the generated tools
[~,m] = unique( cellfun( @(x) x(1:3), tools, 'UniformOutput', 0 ) );
if length(m) ~= numel(tools)
    disp( 'the tool list is not unique:' );
    disp( tools );
    error( 'the tool list is not unique!' );
end
a = unique( cellfun( @(x) x(4), tools, 'UniformOutput', 0 ) );
if (length(a) > 1) || (a ~= 'C')
    disp( 'the tool list has errors:' );
    disp( tools );
    error( 'the tool list has errors!' );
end
    
% convert cell array of strings to string
tools = char(cellfun( @(x)[x '\n'], tools ));
drill = char(cellfun( @(x)[x '\n'], drill ));

% save the file
fprintf( fid, header );
fprintf( fid, tools );
fprintf( fid, '%s\n', 'M95' ); % end of program header
fprintf( fid, drill );
fprintf( fid, '%s\n', 'M00' ); % end
fclose( fid );


% -----------------------------------------------------------------------------
function str = coord(v)
% str = coord(v)
% 2D-vector v takes coordinates in unit m
x = sprintf( '%.3f', v(1)*1e3 ); % mm
y = sprintf( '%.3f', v(2)*1e3 ); % mm
str = ['X' x 'Y' y];

% -----------------------------------------------------------------------------
function [tool,drill] = primitive_cylinder( CSX_cylinder, drawingunit )
start  = [CSX_cylinder.P1.ATTRIBUTE.X CSX_cylinder.P1.ATTRIBUTE.Y] * drawingunit;
stop   = [CSX_cylinder.P2.ATTRIBUTE.X CSX_cylinder.P2.ATTRIBUTE.Y] * drawingunit;
radius = CSX_cylinder.ATTRIBUTE.Radius * drawingunit;
if start(1:2) == stop(1:2)
	% via in z-plane
    dia_mm = radius * 2 * 1e3; % mm
    tool = sprintf( 'T%02iC1.3f', round(dia_mm*10), dia_mm );
    drill = sprintf( 'T%02i\n%s', round(dia_mm*10), coord(start(1:2)) );
else
	disp( 'omitting   primitive cylinder, because the projection onto the z-plane is not a circle' );	
end


% -----------------------------------------------------------------------------
function [tools,drill] = process_primitives( prop, options )
exclude = {};
if isfield(options,'exclude'), exclude = options.exclude; end
for num=1:numel(prop)
	Name = prop{num}.ATTRIBUTE.Name;
    if any( strcmp(Name,exclude) )
		disp( ['omitting   ' Name '...'] );
		continue
    end
    if isfield(options,'include')
        include = options.include;
        if ~any( strcmp(Name,include) )
            disp( ['omitting   ' Name '...'] );
            continue
        end
    end
    
    tools = {};
    drill = {};
    
    disp( ['processing ' prop{num}.ATTRIBUTE.Name '...'] );
	fprintf( fid, '%s\n', ['%LN' Name '*%'] );
	if isfield(prop{num}.Primitives,'Cylinder')
		for a=1:numel(prop{num}.Primitives.Cylinder)
			% iterate over all cylinders
			Cylinder = prop{num}.Primitives.Cylinder{a};
			[tool,drill_] = primitive_cylinder( fid, Cylinder );
            tools = [tools tool];
            drill = [drill drill_];
		end
	end
end