70. plugins.dxf — Read/write geometry in DXF format.

This module allows to import and export some simple geometrical items in DXF format.

70.1. Classes defined in module plugins.dxf

class plugins.dxf.DxfExporter(filename, terminator='\n')[source]

Export geometry in DXF format.

While we certainly do not want to promote proprietary software, some of our users occasionally needed to export some model in DXF format. This class provides a minimum of functionality.

write(s)[source]

Write a string to the dxf file.

The string does not include the line terminator.

out(code, data)[source]

Output a string data item to the dxf file.

code is the group code, data holds the data

close()[source]

Finalize and close the DXF file

section(name)[source]

Start a new section

endSection()[source]

End the current section

entities()[source]

Start the ENTITIES section

layer(layer)[source]

Export the layer

vertex(x, layer=0)[source]

Export a vertex.

x is a (3,) shaped array

line(x, layer=0)[source]

Export a line.

x is a (2,3) shaped array

polyline(x, layer=0)[source]

Export a polyline.

x is a (nvertices,3) shaped array

arc(C, R, a, layer=0)[source]

Export an arc.

70.2. Functions defined in module plugins.dxf

plugins.dxf.importDXF(filename)[source]

Import (parts of) a DXF file into pyFormex.

This function scans a DXF file for recognized entities and imports those entities as pyFormex objects. It is only a very partial importer, but has proven to be already very valuable for many users.

filename: name of a DXF file. The return value is a list of pyFormex objects.

Importing a DXF file is done in two steps:

  • First the DXF file is scanned and the recognized entities are formatted into a text with standard function calling syntax. See readDXF().

  • Then the created text is executed as a Python script, producing equivalent pyFormex objects. See convertDXF().

plugins.dxf.readDXF(filename)[source]

Read a DXF file and extract the recognized entities.

filename: name of a .DXF file.

Returns a multiline string with one line for each recognized entity, in a format that can directly be used by convertDXF().

This function requires the external program dxfparser which comes with the pyFormex distribution. It currently recognizes entities of type ‘Arc’, ‘Line’, ‘Polyline’, ‘Vertex’.

plugins.dxf.convertDXF(text)[source]

Convert a textual representation of a DXF format to pyFormex objects.

texta multiline text representation of the contents of a DXF file.

This text representation can e.g. be obtained by the function readDXF(). It contains lines defining DXF entities. A small example:

Arc(0.0,0.0,0.0,1.0,-90.,90.)
Arc(0.0,0.0,0.0,3.0,-90.,90.)
Line(0.0,-1.0,0.0,0.0,1.0,0.0)
Polyline(0)
Vertex(0.0,3.0,0.0)
Vertex(-2.0,3.0,0.0)
Vertex(-2.0,-7.0,0.0)
Vertex(0.0,-7.0,0.0)
Vertex(0.0,-3.0,0.0)

Each line of the text defines a single entity or starts a multiple component entity. The text should be well aligned to constitute a proper Python script. Currently, the only defined entities are ‘Arc’, ‘Line’, ‘Polyline’, ‘Vertex’.

Returns a list of pyFormex objects corresponding to the text. The returned objects are of the following type:

function name

object

Arc

curve.Arc

Line

plugins.dxf.Line

Polyline

curve.PolyLine

No object is returned for the Vertex function: they define the vertices of a PolyLine.

plugins.dxf.collectByType(entities)[source]

Collect the dxf entities by type.

plugins.dxf.toLines(coll, chordal=0.01, arcdiv=None)[source]

Convert the dxf entities in a dxf collection to a plex-2 Formex

This converts Lines, Arcs and PolyLines to plex-2 elements and collects them in a single Formex. The chordal and arcdiv parameters are passed to Arc.approx() to set the accuracy for the approximation of the Arc by line segments.

plugins.dxf.exportDXF(filename, F)[source]

Export a Formex to a DXF file

Currently, only plex-2 Formices can be exported to DXF.

plugins.dxf.exportDxf(filename, coll)[source]

Export a collection of dxf parts a DXF file

coll is a list of dxf objects

Currently, only dxf objects of type ‘Line’ and ‘Arc’ can be exported.

plugins.dxf.dxftext(obj)[source]

Examples

>>> L = curve.Line([[0.,0.,0.], [0.,1.,0.]])
>>> print(dxftext(L))
Line(0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
>>> A = curve.Arc(center=[0.,0.,0.], radius=1., angles=(-90., 90.))
>>> print(dxftext(A))
Arc(0.0, 0.0, 0.0, 1.0, 270.0, 450.0)
plugins.dxf.exportDxfText(filename, parts)[source]

Export a set of dxf entities to a .dxftext file.