Sub Main
Dim strBarcode As String = String.Empty
Dim invApp As Inventor.Application = ThisApplication
Dim invDoc As Inventor.Document = invApp.ActiveDocument
Dim txtBox As Inventor.TextBox = Nothing
If invDoc IsNot Nothing Then
If invDoc.SelectSet.Count > 0 Then
For Each ent As Object In invDoc.SelectSet
If TypeOf ent Is TextBox Then
txtBox = ent
strBarcode = txtBox.text
Dim strEAN13 As String = ean13(strBarcode)
If strEAN13 <> String.Empty Then
txtBox.FormattedText = "<StyleOverride Font='Code EAN13' FontSize='1' Bold='False' Underline='False'>" & strEAN13 & "</StyleOverride>"
End If
End If
Next
End If
End If
End Sub
Public Function ean13(chain As String)
'This function is governed by the GNU Lesser General Public License (GNU LGPL)
'V 1.1.1
'Parameters : a 12 digits length string
'Return : * a string which give the bar code when it is dispayed with EAN13.TTF font
' * an empty string if the supplied parameter is no good
Dim tableA As Boolean
Dim checksum As Integer
Dim first As Integer
Dim BarCode As String = String.Empty
Dim i As Integer
'Check for 12 characters
If Len(chain) = 12 Then
'And they are really digits
For i = 1 To 12
If Asc(Mid(chain, i, 1)) < 48 Or Asc(Mid(chain, i, 1)) > 57 Then
i = 0
Exit For
End If
Next
If i = 13 Then
'Calculation of the checksum
For i = 12 To 1 Step -2
checksum = checksum + Val(Mid(chain, i, 1))
Next
checksum = checksum * 3
For i = 11 To 1 Step -2
checksum = checksum + Val(Mid(chain, i, 1))
Next
chain = chain & (10 - checksum Mod 10) Mod 10
'The first digit is taken just as it is, the second one come from table A
BarCode = Left(chain, 1) & Chr(65 + Val(Mid(chain, 2, 1)))
first = Val(Left(chain, 1))
For i = 3 To 7
tableA = False
Select Case i
Case 3
Select Case first
Case 0 To 3
tableA = True
End Select
Case 4
Select Case first
Case 0, 4, 7, 8
tableA = True
End Select
Case 5
Select Case first
Case 0, 1, 4, 5, 9
tableA = True
End Select
Case 6
Select Case first
Case 0, 2, 5, 6, 7
tableA = True
End Select
Case 7
Select Case first
Case 0, 3, 6, 8, 9
tableA = True
End Select
End Select
If tableA Then
BarCode = BarCode & Chr(65 + Val(Mid(chain, i, 1)))
Else
BarCode = BarCode & Chr(75 + Val(Mid(chain, i, 1)))
End If
Next
BarCode = BarCode & "*" 'Add middle separator
For i = 8 To 13
BarCode = BarCode & Chr(97 + Val(Mid(chain, i, 1)))
Next
BarCode = BarCode & "+" 'Add end mark
End If
End If
Return BarCode
End Function
How to use: Make a text box, place into it the value to be converted such as 012345678901, then select the text box while not in text edit mode (one or many), then run the rule.
Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/