A barcode in DXF format

Exporting Barcodes into DXF

The following example is a VB program that stores barcodes in DXF format that can be loaded into SolidWorks, AutoCAD or other CAD or PCB design program.

We also have a javascript-based version of the converter.

How to Proceed

A Simple DXF File Structure

A simple DXF file with one SOLID entity

Here we will show how to build a simple DXF file which contains a single square barcode module. We use a SOLID DXF entity for this purpose.

To draw a square SOLID entity, you need to specify (x,y,z) coordinates for its corners. If you'll look into this sample DXF file, you will see multiple sequences that look like: 10 X1 20 Y1 30 Z1 11 X2 21 Y2 31 Z2 12 X3 22 Y3 32 Z3 13 X4 23 Y4 33 Z4

Where the codes 10, 20, 30 are the group codes:

The listing shown here contains one SOLID entity at the following coordinates (X, Y, Z): (1, 2, 0), (2, 2, 0), (1, 1, 0), (2, 1, 0):

0 SECTION 2 HEADER 9 $ACADVER 1 AC1006 0 ENDSEC 0 SECTION 2 ENTITIES 0 SOLID 8 barcode 10 1 20 2 30 0 11 2 21 2 31 0 12 1 22 1 32 0 13 2 23 1 33 0 0 ENDSEC 0 EOF

If you want to play with this DXF sample, do not add unnecessary spaces or TAB characters or empty lines. AutoCAD is the most picky program - it will show the "Incomplete DXF file" message or will display an empty drawing sometimes without any warnings.

Also, SolidWorks will accept your file without the 9 $ACADVER 1 AC1006 sequence but AutoCAD will not (AC1006 is the drawing version number for AutoCAD R10).

To tell your CAD program that the DXF file is not unitless, place following sequence into the HEADER:

9 $INSUNITS 70 X

Where the X is:

More information is available here, look for the $INSUNITS.

DXF Barcode Export with StrokeScribe Class

A Data Matrix barcode loaded into AutoCAD 10101010101010 11001001000111 11001100000100 11000110100001 11101000011100 10001110101011 10110110001000 10000111101101 10111010111000 10011010000111 11010010111110 10010101001001 10011001111100 11111111111111

On the left is a sample output of the ZebraBits property provided by StrokeScribe barcode generator.

The ones in the bit string correspond to black modules, zeros - to white modules. We will draw only black modules using SOLID DXF entities.

DXF Converter - the Code Example

To create a DXF file, run the dxf_barcode(). In the write_barcode() call, specify a path to the DXF file, X/Y coordinates of the barcode and width/height of the barcode modules (the code works with millimeters). For 2D barcodes, you must use the same value both for width and height.

Sub write_header(file As Integer) Print #file, 0 Print #file, "SECTION" Print #file, 2 Print #file, "HEADER" Print #file, 9 Print #file, "$ACADVER" ' AutoCAD drawing version number Print #file, 1 Print #file, "AC1006" ' AC1006 = R10, AC1009 = R11/R12, AC1012 = R13, AC1014 = R14 Print #file, 9 Print #file, "$INSUNITS" ' Default drawing units. Print #file, 70 Print #file, 4 ' 1 = Inches; 2 = Feet; 3 = Miles; 4 = Millimeters; 5 = Centimeters; 6 = Meters Print #file, 0 Print #file, "ENDSEC" Print #file, 0 Print #file, "SECTION" Print #file, 2 Print #file, "ENTITIES" End Sub Sub write_endsec(file As Integer) Print #file, 0 Print #file, "ENDSEC" Print #file, 0 Print #file, "EOF" End Sub Sub writexyz(file As Integer, x As Integer, y As Integer, corner_number As Integer) Print #file, 10 + corner_number ' Group codes 10,11,12,13 are X1,X2,X3,X4 coordinates Print #file, x ' of the SOLID entity. Print #file, 20 + corner_number ' Y1, Y2, Y3, Y4 coordinates of the SOLID entity. Print #file, y Print #file, 30 + corner_number ' Z1, Z2, Z3, Z4 are always =0 Print #file, 0 End Sub Sub write_solid(file As Integer, x As Integer, y As Integer, w As Integer, h As Integer) Print #file, "0" Print #file, "SOLID" Print #file, "8" Print #file, "barcode" ' The layer name writexyz file, x, y, 0 ' Four corners of the SOLID entity. 0,1,2,3 are the corner numbers. writexyz file, x + w, y, 1 writexyz file, x, y - h, 2 writexyz file, x + w, y - h, 3 End Sub Sub write_barcode(dxf_file_name As String, zebra As String, _ left As Integer, top As Integer, _ modw As Integer, modh As Integer) Dim file As Integer file = FreeFile Open dxf_file_name For Output As file write_header file ' Writes the HEADER, $ACADVER, $INSUNITS, ENTITIES into the DXF file Dim x As Integer Dim y As Integer x = left y = top i = 1 Do z = Mid(zebra, i, 1) Select Case z Case "" Exit Do Case "0" x = x + modw ' We ignore white modules of the barcode Case "1" write_solid file, x, y, modw, modh ' Writes a SOLID entity for a black barcode module x = x + modw Case Chr(10) x = left y = y - modh End Select i = i + 1 Loop write_endsec file ' Writes ENDSEC and EOF Close #file End Sub ' We provide this DXF-to-VBA converter as open source. ' You may modify the code as you want and use it to draw anything else than barcodes. ' But please keep the link to our web site, https://strokescribe.com Private Sub dxf_barcode() Dim ss As StrokeScribeClass Set ss = CreateObject("STROKESCRIBE.StrokeScribeClass.1") ss.Alphabet = DATAMATRIX ss.Text = "123ABCD" Dim zebra As String zebra = ss.ZebraBits ' Free StrokeScribe Class version => see the notes below write_barcode "c:\poly.dxf", zebra, 0, 20, 1, 1 ' X, Y, width, height End Sub

You need to add a reference to the "StrokeScribe Class" into your VBA project to run this code.

A ready-to-use DXF file sample can be downloaded here.

Free version of the barcode generator provides limited functionality for zebra pattern generation. You can test the above code only on small barcodes or use the pre-built zebra patterns of large barcodes shown below.

Exporting Linear Barcodes

A CODE 128 barcode loaded into AutoCAD

To produce linear barcodes (CODE 128, EAN codes, CODE 11), modify the above code in the following way:

ss.Alphabet = CODE128 ss.Text = "A" Dim zebra As String zebra = ss.ZebraBits write_barcode "c:\poly.dxf", zebra, 5, 15, 1, 10 ' The width/height ratio of this barcode is 1:10.

The ready-to-use DXF file is available here.

Exporting 2D Barcodes

Some examples of 2D barcode customization:

Data Matrix

One more example of Data Matrix barcode

To create a Data Matrix barcode, make the following modifications in the VBA code shown above.

ss.Alphabet = DATAMATRIX ss.Text = "ABCDEF" Dim zebra As String zebra = ss.ZebraBits write_barcode "c:\poly.dxf", zebra, 0, 0, 1, 1

QR Code

A DXF file with QR Code loaded into AutoCAD

To create a QR Code, make the following modifications in the DXF converter's VBA code.

ss.Alphabet = QRCODE ss.Text = "ABCDEFGH" Dim zebra As String zebra = ss.ZebraBits write_barcode "c:\poly.dxf", zebra, 0, 0, 1, 1

Here you can download a ready-to-use DXF file.

Qr Codes That Use UTF-8

To create a barcode that contains national characters and needs to be scanned by mobile devices, modify the CodePage property as shown below.

ss.Alphabet = QRCODE ss.CodePage = 65001 ' 65001 = UTF-8 ss.Text = "ΞΟΠΡΣΤΥΦ" Dim zebra As String zebra = ss.ZebraBits write_barcode "c:\poly.dxf", zebra, 0, 0, 1, 1

PDF417

A DXF file with PDF417

To produce a PDF417 barcode, make the following changes in the DXF converter's code:

ss.Alphabet = PDF417 ss.Text = "ABCDE" Dim zebra As String zebra = ss.ZebraBits write_barcode "c:\poly.dxf", zebra, 0, 0, 1, 3 ' The typical module width/height ratio for PDF417 1:3.

Here you can download a pre-built DXF file.

Testing the Converter with ArchiCAD

Now we can report our DXF barcode converter works with ArchiCad. You can download the pre-built test drawings (PDF417, QR Code, Data Matrix, CODE 128) or make a VB6/VBA-based converter yourself if you have commercial version of StrokeScribe software.

Execute the File->External Content->Place External Drawing command to load a DXF file:

Loading an external drawing in ArchiCAD 19

A screenshot of ArchiCAD 19 with some barcodes loaded from DXF drawings:

A test set of DXF-formatted barcodes loaded into ArchiCAD

Testing the Converter with Revit

Our barcode DXF converter has been tested with Revit. Before trying the code, you can download and import the pre-built test barcode drawings (PDF417, QR Code, Data Matrix, CODE 128).

To insert a barcode into an existing drawing, go to the Insert tab, and click Import CAD (the picture below shows the Revit Architecture 2015):

DXF-based barcode samples in Revit

In the Import CAD Formats dialog, select a previously downloaded barcode file. All barcode samples are designed in millimeters so you may need to adjust the Import units to not to loose the small barcode image on a big drawing.

Loading an external drawing in Revit

Testing the Converter with Siemens NX

Our barcode DXF converter has been tested with NX. Before trying the code, you can download and import the pre-built test barcode drawings (PDF417, QR Code, Data Matrix, CODE 128).

To import a barcode drawing, go to File>Import>AutoCAD DXF/DWG:

DXF-based barcode in Siemens NX