dxf.write
drafting: dxf.write (FILE, E)
Write drawing entities to an ASCII DXF file.
dxf.write (FILE, E) writes the entity struct array
E to FILE as an AutoCAD R12 (AC1009) ASCII DXF drawing,
in millimetres. E has the layout returned by dxf.read; only
the type and pts fields are required, and any of layer,
closed, radius, angles, text, height and
rotation that are absent take their defaults.
Supported entity types and what each needs in pts:
'LINE' | exactly two points |
'POLYLINE' | two or more vertices; closed honoured |
'LWPOLYLINE' | as 'POLYLINE'; see the note below |
'CIRCLE' | one centre, plus radius |
'ARC' | one centre, plus radius and angles |
'TEXT' | one insertion point, plus text and
height; an optional rotation in degrees, counter-clockwise,
is written only when it is non-zero |
'POINT' | one point |
'DIMENSION' | six definition points, plus block
naming the block that holds its picture, angles giving the DXF
dimension type (0 linear, 2 angular, 3 diameter, 4 radius) and text,
where '<>' leaves the reader to measure it |
Source Code: dxf.write
Layers are collected from the entities and declared in the layer table. An
entity naming no layer goes on layer '0'. A closed polyline must
not carry a repeated final vertex; the closed flag does that work,
matching the implicitly closed convention of the geom namespace.
R12 has no LWPOLYLINE, so an entity of that type is written
as a POLYLINE. The geometry, layer and closed flag survive exactly;
only the type name normalises, which reading the file back will show.
Coordinates are written in fixed-point notation to six decimals, so the file resolves to a nanometre — far finer than any drawing means, but worth knowing when comparing a round trip for exact equality.
R12 rather than R2000. R12 needs no entity handles, no
OBJECTS section and no BLOCK_RECORD table, which makes the
structural boilerplate small enough to audit by eye, and it is the most
widely accepted DXF flavour there is. The cost is no HATCH entity,
which will matter when wall sections want hatching and is the point at which
moving to R2000 becomes worthwhile. Nothing outside this file depends on
the choice.
See also: dxf.read
Source Code: dxf.write
Writing a drawing as DXF is one call on what entities produced. The file carries the layers, line types and colours, and a line-type table with the dash patterns.
D = draw.Drawing ('plate');
D.Layer = 'OUTLINE';
D = D.polyline ([0, 0; 60, 0; 60, 40; 0, 40], true);
D.Layer = 'AXES';
D.Linetype = 'CENTER';
D.Colour = 'red';
D = D.line ([-5, 20], [65, 20]);
fn = [tempname(), '.dxf'];
dxf.write (fn, entities (D));
printf ('%d bytes written\n', stat (fn).size);
1118 bytes written
R = dxf.read (fn);
for k = 1:numel (R)
printf (' %-9s on %-8s %-11s colour %d\n', R(k).type, R(k).layer, ...
R(k).linetype, R(k).colour);
endfor
POLYLINE on OUTLINE CONTINUOUS colour 256 LINE on AXES CENTER colour 1
unlink (fn);
A repeated feature is written once as a block and referred to, which is what a draughtsman expects to receive and a fraction of the file size.
bore = draw.Drawing ().circle ([0, 0], 4).line ([-6, 0], [6, 0]);
D = draw.Drawing ().block ('bore', bore);
for k = 0:24
D = D.insert ('bore', [12 * k, 0]);
endfor
f1 = [tempname(), '.dxf'];
f2 = [tempname(), '.dxf'];
[E, LOST, B] = entities (D, 'blocks', 'reference');
dxf.write (f1, E, 'blocks', B);
dxf.write (f2, entities (D));
printf ('25 instances: %d bytes referenced, %d bytes expanded\n', ...
stat (f1).size, stat (f2).size);
25 instances: 2174 bytes referenced, 4230 bytes expanded
unlink (f1); unlink (f2);