Barcode in SolidWorks
Some examples how to create barcodes in SolidWorks with our barcode generator.
The first and the easiest method is to put a barcode as an Active Document object onto your drawing. This barcode type is less customizable and cannot be CNC'ed.
The second method is to create a barcode as a vector picture in a temporary file (we use EMF) and then load it into a SolidWorks drawing. This method requires some VBA programming.
The third method is to draw a barcode in sketching mode with polygons.
How to Proceed
- Download and install the barcode generator;
- Create an empty part file in SolidWorks;
- Use one of the following examples to insert a barcode.
Method #1: StrokeScribe Active Document
1. While running SolidWorks, open the main menu:

2. Click on Object at the bottom of the Insert menu.

3. Select 'StrokeScribe document' from this list:

4. Drag the barcode picture into a place you need and resize the barcode using the thick border around it.

5. Double-click on the barcode picture to open barcode properties dialog.

For more information about barcode types and settings, please visit the Documentation section.
Method #2: VBA and Vector Pictures
This example shows how to insert a barcode into SolidWorks drawing using VBA and StrokeScribe barcode generator.
The main idea is to create a barcode image with SavePicture() method of the StrokeScribeClass object in the system's TEMP folder and then load it back into the drawing with SketchManager.InsertSketchPicture().
The Code Example
Execute Tools > Macro > New from the SolidWorks menu and paste the following code into the VBA editor.
Add a reference to StrokeScribe Class into the VBA project.
Declare PtrSafe Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Function TempPath() As String ' This function is a helper that calls the WinAPI's GetTempPath().
Dim path As String
path = String(256, 0)
rc = GetTempPath(256, path)
If rc <> 0 Then
path = Left(path, rc)
Else
path = vbNullString
End If
TempPath = path
End Function
Sub main()
pic_name = "barcode1" ' The name of a sketch picture on the drawing.
pic_path = TempPath() & "barcode.emf" ' The path to store the temporary barcode picture on a disk.
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
Set doc = swApp.ActiveDoc ' We will place the barcode into the currently active drawing.
doc.SelectByName 0, pic_name ' If we already have a barcode picture on the drawing, let's delete it.
doc.EditDelete
Dim ss As StrokeScribeClass ' The barcode generator object.
Set ss = CreateObject("STROKESCRIBE.StrokeScribeClass.1")
ss.Alphabet = DATAMATRIX ' or =QRCODE
ss.Text = "123ABC" ' A text to be encoded in the barcode.
rc = ss.SavePicture(pic_path, EMF, 50, 50) ' This will store a 50x50mm barcode picture in a temporary file.
If rc > 0 Then ' In a case of error, this displays the error description message.
MsgBox ss.ErrorDescription
Exit Sub
End If
doc.EditSketch
Dim SkPicture As SketchPicture ' This loads the picture from the temporary file.
Set SkPicture = doc.SketchManager.InsertSketchPicture(pic_path)
SkPicture.SetSize 0.05, 0.05, True ' Specifying barcode's position and size.
SkPicture.SetOrigin 0.1, 0.1
Dim feat As Feature ' Let's change the name of the barcode object. This allows fo find and delete an old picture if we
Set feat = SkPicture.GetFeature() ' need to update the barcode.
feat.Name = pic_name
doc.EditSheet
Kill pic_path ' We don't need the temporary picture file anymore.
End Sub
Method #3: VBA and Sketch Entities
This example shows how to draw a barcode in sketching mode. The sketch may be optionally extruded if you need a barcode made of solids.
The code is based on the ZebraBits property of the barcode generator that generates black/white (1/0) barcode patterns.
Here is a typical Data Matrix barcode:

And here is the zebra pattern created by ZebraBits:
1010101010
1011001101
1000111100
1011001011
1010011000
1011101011
1101011000
1110101101
1110001010
1111111111
The Code Example
Execute Tools > Macro > New from the SolidWorks menu and paste the following code into the VBA editor.
Add a reference to StrokeScribe Class into the VBA project.
Private Sub draw_barcode(ByRef sm As SldWorks.SketchManager, zebra As String, r As Double)
Dim x As Double
Dim y As Double
x = 0
y = 0
i = 1
Do
Z = Mid(zebra, i, 1)
Select Case Z
Case ""
Exit Do
Case "0"
x = x + r 'We ignore white modules of the barcode
Case "1"
sm.CreateCircleByRadius x, y, 0, r / 2 - 0.0000001 ' Subtracting a very small value from the circle radius to avoid the "intersecting contours" error
x = x + r
Case Chr(10)
x = 0
y = y - r
End Select
i = i + 1
Loop
End Sub
Sub make_barcode()
Dim app As SldWorks.SldWorks
Set app = Application.SldWorks
Dim doc As SldWorks.ModelDoc2
Set doc = app.ActiveDoc
Dim skmgr As SldWorks.SketchManager
Set skmgr = doc.SketchManager
' Deletes the previously extruded barcode
If doc.Extension.SelectByID2("BARCODE3D", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0) Then
doc.EditDelete
End If
' Deletes the previously created barcode sketch
If doc.Extension.SelectByID2("BARCODE", "SKETCH", 0, 0, 0, False, 0, Nothing, 0) Then
doc.EditDelete
End If
Dim ss As StrokeScribeClass
Set ss = CreateObject("STROKESCRIBE.StrokeScribeClass.1")
ss.Alphabet = DATAMATRIX 'or =QRCODE or =AZTEC
ss.Text = "ABC"
If ss.Error Then
MsgBox ss.ErrorDescription
Exit Sub
End If
Dim zebra As String
zebra = ss.ZebraBits
skmgr.AddToDB = True
draw_barcode skmgr, zebra, 0.005 ' barcode module size = 5mm
skmgr.AddToDB = False ' Speeds up insertion of multiple sketch entities
Dim feat As SldWorks.Feature
Set feat = skmgr.ActiveSketch
feat.Name = "BARCODE"
skmgr.InsertSketch True
feat.Select False
If False Then ' Set this to True if you want to extrude the barcode
Dim h As Double
h = 0.001 ' Extrusion height in meters
Set feat = doc.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, h, 0, False, False, False, False, 0, 0, _
False, False, False, False, False, True, False, 0, 0, False)
feat.Name = "BARCODE3D"
End If
End Sub
A barcode sketch:

An extruded barcode:
