1 ## Copyright (C) 2004 Patrick Labbe
2 ## Copyright (C) 2004 Laurent Mazet <mazet@crm.mot.com>
3 ## Copyright (C) 2005 Larry Doolittle <ldoolitt@recycle.lbl.gov>
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 2 of the License, or
8 ## (at your option) any later version.
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
19 ## @deftypefn {Function File} {@var{nb} =} dxfwrite (@var{filename}, @var{pl}, @dots{})
21 ## Write @var{filename} as a DXF file. Polyline @var{pl} must be defined as
22 ## matrix of 1, 2 or 3 columns respectively for x, y and z coordinates. The
23 ## number of polyline (@var{nb}) or 0 is returned.
26 function [nb] = dxfwrite (filename, varargin)
29 sn = split(filename, ".");
30 if !strcmp(tolower(deblank(sn(end,:))), "dxf")
31 filename = [filename, ".dxf"];
37 usage("dxfwrite = (filename, pl, ...)");
42 fid = fopen (filename, "wt");
44 error("error opening file \"%s\"\n", filename);
48 FMT = sprintf("%%.%dg", save_precision);
50 ## Header declarations
51 fprintf (fid, ["0\nSECTION\n", "2\nHEADER\n"]);
53 fprintf (fid, ["9\n$ACADVER\n", "1\nAC1009\n"]); ## AutoCAD R11
55 fprintf (fid, "0\nENDSEC\n");
58 fprintf (fid, ["0\nSECTION\n", "2\nTABLES\n"]);
60 ## Line type declarations
61 fprintf (fid, ["0\nTABLE\n", "2\nLTYPE\n"]);
62 ## Number of line types
63 fprintf (fid, "70\n1\n");
65 fprintf (fid, "0\nLTYPE\n");
67 fprintf (fid, "2\nCONTINUOUS\n");
69 fprintf (fid, "70\n0\n"); ## Optimal for AutoCAD
70 ## Descriptive text for linetype
71 fprintf (fid, "3\nContinuous line\n");
73 fprintf (fid, "72\n65\n"); ## the ASCII code for A
74 ## Number of linetype elements
75 fprintf (fid, "73\n0\n");
76 ## Total pattern length
77 fprintf (fid, "40\n0\n");
81 fprintf (fid, "0\nENDTAB\n");
83 ## Layers declarations
84 fprintf (fid, ["0\nTABLE\n", "2\nLAYER\n"]);
86 fprintf (fid, "70\n%d\n", nargin-1);
92 fprintf (fid, "0\nLAYER\n");
94 fprintf (fid, "2\nCurve%d\n", nb);
96 fprintf (fid, "70\n0\n"); ## Optimal for AutoCAD
98 fprintf (fid, "6\nCONTINUOUS\n");
100 fprintf (fid, "62\n%d\n", nb);
103 fprintf (fid, "0\nENDTAB\n");
106 fprintf (fid, "0\nENDSEC\n");
108 ## Entity declarations
109 fprintf (fid, ["0\nSECTION\n", "2\nENTITIES\n"]);
113 tmp_pl = varargin{1+nb++};
115 ## Check curve dimension (1, 2 or 3)
116 if columns(tmp_pl) <= 3
117 pl = zeros(rows(tmp_pl), 3);
118 pl(:, 1:columns(tmp_pl)) = tmp_pl;
120 warning ("%dth entry skipped (more than 3 dimensions)", nb);
124 ## Check if the curve is closed
126 if pl(1, :) == pl(rows(pl), :)
128 pl = pl([1:rows(pl)-1], :);
132 fprintf (fid, "0\nPOLYLINE\n");
135 fprintf (fid, "8\nCurve%d\n", nb);
137 fprintf (fid, "6\nCONTINUOUS\n");
139 fprintf (fid, "66\n%d\n", nb);
141 fprintf (fid, "70\n%d\n", closed);
143 ## Layer specification
144 layspec = sprintf("8\nCurve%d\n", nb);
147 fprintf(fid, ["0\nVERTEX\n", layspec, \
148 "10\n",FMT,"\n", "20\n",FMT,"\n", "30\n",FMT,"\n"], pl.');
151 fprintf(fid, "0\nSEQEND\n");
156 fprintf(fid, "0\nENDSEC\n");
159 fprintf(fid, "0\nEOF\n");