Here is my 128B with the check character.
;|
Specification
A Code 128 barcode will have six sections:
* Quiet Zone
* Start Character
* Encoded Data
* Check Character
* Stop Character
* Quiet Zone
The check character is calculated from a weighted sum (modulo 103) of all the characters.
|;
;; CAB 10.08.09 Code 128B
(defun c:BarCode (/
str
;BarWidthUnits
;BarHeight
QuietZoneWidth
pt
)
(and
(setq str (getstring t "\nEnter text for Barcode: "))
;; may need to trim leading & trailing spaces or tabs?
(or BarWidthUnits
(setq BarWidthUnits (getint "\nEnter Unit width for each one Bar: "))
)
(or BarHeight (setq BarHeight (getdist "\nEnter Height of Barcode: ")))
(setq QuietZoneWidth 0.25)
(setq pt (getpoint "\nPick insert point, lower left."))
(AddBarCode str BarWidthUnits BarHeight QuietZoneWidth pt)
)
(princ)
)
(defun AddBarCode (str BarWidthUnits BarHeight QuietZoneWidth pt /
code letter codelist CheckDigitW CheckDigit BCdata idx )
(setq BCdata
'((" " 212222)("!" 222122)("\"" 222221)("#" 121223)("$" 121322)("%" 131222)("&" 122213)
("\'" 122312)("(" 132212)(")" 221213)("*" 221312)("+" 231212)("," 112232)("-" 122132)
("\." 122231)("/" 113222)("0" 123122)("1" 123221)("2" 223211)("3" 221132)("4" 221231)
("5" 213212)("6" 223112)("7" 312131)("8" 311222)("9" 321122)(":" 321221)(";" 312212)
("<" 322112)("=" 322211)(">" 212123)("?" 212321)("@" 232121)("A" 111323)("B" 131123)
("C" 131321)("D" 112313)("E" 132113)("F" 132311)("G" 211313)("H" 231113)("I" 231311)
("J" 112133)("K" 112331)("L" 132131)("M" 113123)("N" 113321)("O" 133121)("P" 313121)
("Q" 211331)("R" 231131)("S" 213113)("T" 213311)("U" 213131)("V" 311123)("W" 311321)
("X" 331121)("Y" 312113)("Z" 312311)("[" 332111)("\\" 314111)("]" 221411)("^" 431111)
("_" 111224)("`" 111422)("a" 121124)("b" 121421)("c" 141122)("d" 141221)("e" 112214)
("f" 112412)("g" 122114)("h" 122411)("i" 142112)("j" 142211)("k" 241211)("l" 221114)
("m" 413111)("n" 241112)("o" 134111)("p" 111242)("q" 121142)("r" 121241)("s" 114212)
("t" 124112)("u" 124211)("v" 411212)("w" 421112)("x" 421211)("y" 212141)("z" 214121)
("{" 412121)("|" 111143)("}" 111341)("~" 131141) (95 114113) (96 114311) (97 411113)
(98 411311)(99 113141) (100 114131) (101 311141)(102 411131) ))
(setq CodeList (mapcar (function(lambda(x)
(setq CheckDigitW (cons (vl-position (assoc (chr x) BCdata) BCData) CheckDigitW))
(cadr(assoc (chr x) BCdata)) )) (vl-string->list str)))
(defun MakeBar (letter / width code BarFlag)
(setq BarFlag t)
(foreach code (vl-string->list (itoa Letter))
(setq code (atoi (chr code))
width (* code BarWidthUnits))
(if BarFlag
(entmakex
(list '(0 . "LWPOLYLINE")
'(100 . "AcDbEntity")
'(100 . "AcDbPolyline")
(cons 43 width)
'(90 . 2)
(cons 10 (polar pt 0 (/ width 2.)))
(cons 10 (polar (polar pt 0 (/ width 2.)) (/ pi 2) BarHeight))
))
)
(setq pt (polar pt 0 width)
BarFlag (not BarFlag)
)
)
)
(setq CheckDigitW (reverse CheckDigitW))
(makebar 211214) ; Start 128B
(setq CheckDigit 104) ; Start 128B
(setq idx 1)
(foreach letter CodeList
(makebar letter)
(setq CheckDigit (+ CheckDigit (* idx (nth (1- idx) CheckDigitW))))
(setq idx (1+ idx))
)
(setq CheckDigit (rem CheckDigit 103))
(makebar (cadr (nth CheckDigit BCdata))) ; add the Check Character
(makebar 2331112) ; Stop
(princ)
)
<edit: bug in check character fixed, I think>