]> Creatis software - CreaPhase.git/blob - octave_packages/plot-1.1.0/dxfwrite.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / plot-1.1.0 / dxfwrite.m
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>
4 ##
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.
9 ##
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.
14 ##
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/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {@var{nb} =} dxfwrite (@var{filename}, @var{pl}, @dots{})
20 ##
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.
24 ## @end deftypefn
25
26 function [nb] = dxfwrite (filename, varargin)
27   
28   ## Check file name
29   sn = split(filename, ".");
30   if !strcmp(tolower(deblank(sn(end,:))), "dxf")
31     filename = [filename, ".dxf"];
32   endif
33
34   ## Check arguments
35   nb = 0;
36   if nargin <= 1
37     usage("dxfwrite = (filename, pl, ...)");
38     return;
39   endif
40   
41   ## Open file
42   fid = fopen (filename, "wt");
43   if fid <= 0
44     error("error opening file \"%s\"\n", filename);
45   endif
46
47   ## Format string
48   FMT = sprintf("%%.%dg", save_precision);
49
50   ## Header declarations
51   fprintf (fid, ["0\nSECTION\n", "2\nHEADER\n"]);
52   ## DXF version
53   fprintf (fid, ["9\n$ACADVER\n", "1\nAC1009\n"]); ## AutoCAD R11
54   ## End of headers
55   fprintf (fid, "0\nENDSEC\n");
56
57   ## Table declarations
58   fprintf (fid, ["0\nSECTION\n", "2\nTABLES\n"]);
59
60   ## Line type declarations
61   fprintf (fid, ["0\nTABLE\n", "2\nLTYPE\n"]);
62   ## Number of line types
63   fprintf (fid, "70\n1\n");
64   ## New line type
65   fprintf (fid, "0\nLTYPE\n");
66   ## Line type name
67   fprintf (fid, "2\nCONTINUOUS\n");
68   ## Standard flags
69   fprintf (fid, "70\n0\n"); ## Optimal for AutoCAD
70   ## Descriptive text for linetype
71   fprintf (fid, "3\nContinuous line\n");
72   ## Alignment code
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");
78   ## Pattern definition
79   ## ???
80   ## End of line types
81   fprintf (fid, "0\nENDTAB\n");
82
83   ## Layers declarations
84   fprintf (fid, ["0\nTABLE\n", "2\nLAYER\n"]);
85   ## Number of layers
86   fprintf (fid, "70\n%d\n", nargin-1);
87
88   nb = 0;
89   for i=1:nargin-1
90     nb++;
91     ## New layer
92     fprintf (fid, "0\nLAYER\n");
93     ## Layer name
94     fprintf (fid, "2\nCurve%d\n", nb);
95     ## Standard flags
96     fprintf (fid, "70\n0\n"); ## Optimal for AutoCAD
97     ## Line type
98     fprintf (fid, "6\nCONTINUOUS\n");
99     ## Color number
100     fprintf (fid, "62\n%d\n", nb);
101   endfor
102   ## End of layers
103   fprintf (fid, "0\nENDTAB\n");
104
105   ## End of tables
106   fprintf (fid, "0\nENDSEC\n");
107
108   ## Entity declarations
109   fprintf (fid, ["0\nSECTION\n", "2\nENTITIES\n"]);
110   
111   nb = 0;
112   for i=1:nargin-1
113     tmp_pl = varargin{1+nb++};
114     
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;
119     else
120       warning ("%dth entry skipped (more than 3 dimensions)", nb);
121       continue;
122     endif
123
124     ## Check if the curve is closed
125     closed = false;
126     if pl(1, :) == pl(rows(pl), :)
127       closed = true;
128       pl = pl([1:rows(pl)-1], :);
129     endif
130
131     ## New polyline
132     fprintf (fid, "0\nPOLYLINE\n");
133
134     ## Layer name
135     fprintf (fid, "8\nCurve%d\n", nb);
136     ## Line type name
137     fprintf (fid, "6\nCONTINUOUS\n");
138     ## Color number???
139     fprintf (fid, "66\n%d\n", nb);
140     ## Standard flags
141     fprintf (fid, "70\n%d\n", closed);
142
143     ## Layer specification
144     layspec = sprintf("8\nCurve%d\n", nb);
145
146     ## List of vertex
147     fprintf(fid, ["0\nVERTEX\n", layspec, \
148                   "10\n",FMT,"\n", "20\n",FMT,"\n", "30\n",FMT,"\n"], pl.');
149
150     ## End of polyline
151     fprintf(fid, "0\nSEQEND\n");
152     
153   endfor
154   
155   ## End of entities
156   fprintf(fid, "0\nENDSEC\n");
157   
158   ## End of file
159   fprintf(fid, "0\nEOF\n");
160   
161   ## Close file
162   fclose(fid);
163   
164 endfunction