Categories &

Functions List

Function Reference: dxf.read

drafting: E = dxf.read (FILE)
drafting: E = dxf.read (FILE, LAYER)
drafting: [E, UNITS, SKIPPED] = dxf.read (…)
drafting: [E, UNITS, SKIPPED, BLOCKS] = dxf.read (…)

Read geometry from an ASCII DXF file.

[E, UNITS, SKIPPED, BLOCKS] = dxf.read (…) additionally returns the block definitions, in the shape dxf.write takes back through its 'blocks' pair. Without it an INSERT is a reference to geometry the caller never receives.

A DIMENSION is returned with its six definition points in pts, its DXF type in angles — 0 linear, 2 angular, 3 diameter, 4 radius — the name of the block holding its picture in block, and its text in text, where '<>' means the dimension measures itself.

E = dxf.read (FILE) parses FILE and returns its drawing entities as a struct array with one element per entity and the fields below. Coordinates are converted to millimetres.

typeentity type: 'LINE', 'LWPOLYLINE', 'POLYLINE', 'CIRCLE', 'ARC', 'TEXT' or 'POINT'
layerlayer name; '0' when the entity names none
ptsN-by-2 coordinates in millimetres
closedtrue for a closed polyline, false otherwise
radiusradius of a circle or arc, else empty
angles[start, end] of an arc in degrees, else empty
textstring of a text entity, else empty
heightheight of a text entity, else empty
rotationrotation of a text entity in degrees, counter-clockwise; zero when the entity declares none, else empty

Source Code: dxf.read

For a polyline pts holds the vertices; for a line, its two endpoints; for a circle, arc, text or point, the single centre or insertion point. A closed polyline is not given a repeated final vertex, matching the implicitly closed convention of the geom namespace.

E = dxf.read (FILE, LAYER) returns only the entities on the named layer. Layer names are compared case-insensitively, as CAD applications treat them.

[E, UNITS, SKIPPED] = dxf.read (…) also reports the units the file declared and the entity types that were ignored.

UNITS is one of 'mm', 'cm', 'm', 'in', 'ft' or 'unitless', taken from the $INSUNITS header variable, and coordinates are scaled to millimetres accordingly. When the file declares no units, or declares itself unitless, coordinates are passed through unscaled — which amounts to assuming they were already millimetres. That assumption is right for most architectural DXF but it is an assumption, so check UNITS rather than trusting it silently.

SKIPPED is a cell array of the entity types present in the file but not understood, listed once each. This reader deliberately covers the geometry needed to get an outline in, and reports the rest rather than growing into a general DXF importer.

Only ASCII DXF is supported. Binary DXF and DWG must be converted first, for instance with the ODA File Converter.

See also: dxf.write, geom.isrectilinear

Source Code: dxf.read

Reading a DXF gives one struct per entity, with the layer, line type and colour it carried. Anything the reader does not handle is counted rather than silently dropped.

 D = draw.Drawing ().circle ([0, 0], 20).polyline ([0,0; 30,0; 30,20], true);
 D.Linetype = 'HIDDEN';
 D = D.line ([-25, 0], [25, 0]);
 fn = [tempname(), '.dxf'];
 dxf.write (fn, entities (D));
 [E, UNITS, SKIPPED] = dxf.read (fn);
 printf ('%d entities, units "%s", %d skipped\n', numel (E), UNITS, SKIPPED);
error: printf: wrong type argument 'cell'
 for k = 1:numel (E)
   printf ('  %-9s %d point(s), %s\n', E(k).type, ...
           rows (E(k).pts), E(k).linetype);
 endfor

What was read can be drawn again

 Q = draw.Drawing ();
 for k = 1:numel (E)
   if (strcmp (E(k).type, 'CIRCLE'))
     Q = Q.circle (E(k).pts, E(k).radius);
   elseif (strcmp (E(k).type, 'POLYLINE'))
     Q = Q.polyline (E(k).pts, E(k).closed);
   else
     Q = Q.line (E(k).pts(1,:), E(k).pts(2,:));
   endif
 endfor
 plot (Q);
 title ('read back from the file and drawn again');
 unlink (fn);