-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathad_lib.lua
1025 lines (952 loc) · 29.9 KB
/
ad_lib.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---------------------------------------------------------------------
---- Convert AD binary schlib to kicad symbol lib
---- Use 7z to unpack the compound format binary library
---- Get the headerfile of the library, then parse the element in each
---- componment
---------------------------------------------------------------------
require("util")
require("pcad_lib")
--- Creates a case table with case insensitive string lookup.
-- Table can be used to store and lookup non-string keys as well.
-- Keys are case preserved while iterating (calling pairs).
-- The case will be updated after a set operation.
--
-- E.g:
-- * keys "ABC", "abc" and "aBc" are all considered the same key.
-- * Setting "ABC", "abc" then "aBc" the case returned when iterating will be "aBc".
--
-- @return A table with case insensitive lookup.
-- https://nachtimwald.com/2019/02/12/lua-case-insenstive-table/
function create_case_table()
-- For case preservation.
local lookup = {}
-- Stores the values so we can properly update. We need the main table to always return nil on lookup
-- so __newindex is called when setting a value. If this doesn't happen then a case change (FOO to foo)
-- won't happen because __newindex is only called when the lookup fails (there isn't a metamethod for
-- lookup we can use).
local values = {}
local mt = {
__index=function(t, k)
local v = nil
if type(k) == "string" then
-- Try to get the value for the key normalized.
v = values[k:lower()]
end
if v == nil then
v = values[k]
end
return v
end,
__newindex=function(t, k, v)
-- Store all strings normalized as lowercase.
if type(k) == "string" then
lookup[k:lower()] = v ~= nil and k or nil -- Clear the lookup value if we're setting to nil.
k = k:lower()
end
values[k] = v
end,
__pairs=function(t)
local function n(t, i)
if i ~= nil then
-- Check that strings that have been normalized exist in the table.
if type(i) == "string" and values[i:lower()] ~= nil then
i = i:lower()
end
-- Ensure the value exists in the table.
if values[i] == nil then
return nil
end
end
local k,v = next(values, i)
return lookup[k] or k, v
end
return n, t, nil
end
}
return setmetatable({}, mt)
end
-- UTF8 encoding
-- 0xxx xxxx
-- 110x xxxx 10xxxxxx
-- 1110 xxxx 10xxxxxx 10xxxxxx
-- 1111 0xxx 10xxxxxx 10xxxxxx 10xxxxxx
-- 1111 10xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
-- 1111 110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
local function ensureUTF8(str)
local lead = 0
if not string.find(str, "[\x80-\xff]") then
return str
end
local r = true
for i=1,#str do
local t = str:byte(i)
if lead > 0 then
if t > 0xC0 then
r = false
break
end
lead = lead - 1
else
if t < 0x80 then
lead = 0
elseif t >= 0xc0 then
if t < 0xe0 then
lead = 1
elseif t < 0xf0 then
lead = 2
elseif t < 0xf8 then
lead = 3
elseif t < 0xfC then
lead = 4
elseif t < 0xfE then
lead = 5
else
r = false
break
end
else
r = false
break
end
end
end
if not r then
local s = string.gsub(str, "[\x80-\xff].", "??")
if _G.logE then
_G.logE("String Not ASCII or UTF8 Convert", str, "to", s)
end
print("Warning: String Not ASCII or UTF8 Convert", str, "to", s)
return s
end
return str
end
local function get_str(object, field)
return ensureUTF8(object[ [[%UTF8%]] .. field] or object[field] or "")
end
local delimer = "|+"
local logData = print
local function parseADBlock(data, pos)
local r = create_case_table()
local length, t = string.unpack("I2I2", data, pos)
local lastPos = pos+4+length
pos = pos + 4
if t ~= 0 then
r.bin = string.sub(data, pos, lastPos-1)
return r,lastPos
end
while pos < lastPos do
local p1,p2 = string.find(data, delimer, pos)
if not p1 then
if pos < lastPos-1 then
p1=lastPos+1
p2 = p1
else
return r,lastPos
end
end
--logData(p1,p2, pos)
if p1>pos then
if p1>lastPos then p1 = lastPos - 1 end
local record = string.sub(data, pos, p1-1)
--logData(record)
string.gsub(record, "([^=]+)=([^=]+)", function(k,v)
r[k] = v
end)
end
pos = p2 + 1
end
return r,lastPos
end
local function parseADBlockFile(fileName)
local f,e = io.open(fileName, "rb")
local blocks = {}
if f then
local r = f:read("*a")
--logData("Get ",#r, "bytes from ", fileName)
local pos = 1
while pos < #r do
data, pos = parseADBlock(r, pos)
blocks[#blocks+1] = data
end
f:close()
return blocks
else
logData(e)
end
end
local function buildFont(fontID, fonts)
local font = {
w = 50,
h = 50,
thickness= 10,
}
if fontID and fonts[fontID] then
font.w = math.floor(fonts[fontID].size * 5)
font.h = font.w
font.thickness = 10
end
return font
end
local function parse_Header(headerName)
local blocks = parseADBlockFile(headerName)
local compList = {}
local fonts = {}
if #blocks > 0 then
local block = blocks[1]
local cnt = tonumber(block.COMPCOUNT)
local compCnt = cnt
if not cnt then
logData("No comp in file " .. headerName)
return
end
for i=1,cnt do
local nRef = 'LIBREF' .. tostring(i-1)
local n = 'COMPDESCR'..tostring(i-1)
local nUtf = '%UTF8%COMPDESCR'..tostring(i-1)
compList[#compList+1] = {
name = block[nRef],
desc = get_str(block,n),
desc8 = block[nUtf],
}
--logData(block[nRef])
end
cnt = tonumber(block.FONTIDCOUNT)
if not cnt then
logData("No font in file " .. headerName)
return
end
for i=1,cnt do
local n = tostring(i)
fonts[n] = {
size = block["SIZE"..n],
name = block["FONTNAME"..n],
unerline = block["UNDERLINE"..n],
}
end
if #compList ~= compCnt then
logData("Comp count parse error", #compList, compCnt)
end
return compList, fonts
else
logData("Fail to parse block in header")
end
return {},{}
end
local ad2kicad_line_width_t = {
['0'] = 1,
['1'] = 10,
['2'] = 30,
['3'] = 60,
}
local function getWidth(width)
if width and ad2kicad_line_width_t[width] then
return ad2kicad_line_width_t[width]
end
return 0
end
local function ad2kicad_CoordValue(data)
if data then
if type(data) == 'string' then
string.gsub(data, "([%+%-%d%.]+)", function(x)
data = x
end)
end
return math.floor(tonumber(data) * 10)
end
return 0
end
local ad2kicad_textJustify = {
[0] = {'B', 'L'},
[1] = {'B', 'C'},
[2] = {'B', 'R'},
[3] = {'C', 'L'},
[4] = {'C', 'C'},
[5] = {'C', 'R'},
[6] = {'T', 'L'},
[7] = {'T', 'C'},
[8] = {'T', 'R'},
}
local ad2kicad_pinType = {
[0] = "I_Input",
[1] = "B_I/O",
[2] = "O_Output",
[3] = "C_Open Collector",
[4] = "P_Passive",
[5] = "T_HiZ",
[6] = "E_Open Emitter",
[7] = "W_Power",
}
local function split_name(n)
local p = string.find(n,"_")
if p then
return string.sub(n,1,p-1),n
end
return "",n
end
local function get_pin_type(t)
if ad2kicad_pinType[t] then return split_name(ad2kicad_pinType[t]) end
logData("Pin type not found for " .. tostring(t) .. ", convert to passive")
return split_name(ad2kicad_pinType[4])
end
local kicad_pinShape_t = {
[0] = " line",
[1] = "I_Invert",
[2] = "C_Clock",
[3] = "IC_InvClock",
[4] = "L_LowInput",
[5] = "CL_LowClock",
[6] = "V_LowOutput",
[7] = "F_ClockNegEdge",
[8] = "X_LogicNot",
}
local function get_pin_shage(shape)
local t = kicad_pinShape_t[shape]
if t then
return split_name(t)
end
logData("Pin shape not found for " .. tostring(shape) .. ", convert to default")
return "","default"
end
local function BF(d, From, To)
To = To or From
if _G.bit32 then
d = bit32.rshift(d, From)
return bit32.band(d, (2^(To-From+1)) - 1)
end
local t1 = math.floor(d/ (2^From))
local div = 2^(To-From+1)
local t2 = math.modf(t1/div)
return t1 - (t2 * div)
end
local InEdgeClock = 3
local OutEdgeDot = 1
local OutEdgeLowIn = 4
local OutEdgeLowOut = 17
local OutNot = 6
local function get_shape(inEdge, outEdge, inShape, outShape)
if outShape == OutNot then
return get_pin_shage(8)
end
if inEdge == InEdgeClock then
if outEdge == OutEdgeLowIn then
return get_pin_shage(5)
end
if outEdge == OutEdgeDot then
return get_pin_shage(3)
end
return get_pin_shage(2)
end
if outEdge == OutEdgeDot then
return get_pin_shage(1)
end
if outEdge == OutEdgeLowIn then
return get_pin_shage(4)
end
if outEdge == OutEdgeLowOut then
return get_pin_shage(6)
end
return get_pin_shage(0)
end
local function adPinToSymbol(pin)
local r = string.gsub("X $name $num $x $y $pinLength $dir $nameSize $numSize $partNum 1 $eleType", "%$(%w+)", pin)
if pin.hide then
r = r .. " N" .. pin.shape
end
if pin.shape ~= "" then
return r .. " " .. pin.shape
end
return r
end
local function next_is_bar(n,i)
if i+1<=#n then
local t = string.sub(n,i+1,i+1)
return t == "\\"
end
return false
end
local function make_symbol_pin_name(n)
local r = string.gsub(n, " ", " ")
r = string.gsub(r, " ", "_")
r = string.gsub(r, "[%s]+", "")
r = string.gsub(r, "^[\\]+", "")
local res = ""
local isBar = false
local i = 1
while i <= #r do
local t = string.sub(r,i,i)
if next_is_bar(r, i) then
if not isBar then res = res .. "~" end
isBar = true
i = i + 1
else
if isBar then res = res .. "~" end
isBar = false
end
res = res .. t
i = i + 1
end
return res
end
local function parsePin(data,fonts, comp)
local pin = {}
pin.partNum = string.unpack("I1", data, 6)
local inEdge,outEdge,inShape, outShape = string.unpack("I1I1I1I1", data, 9)
pin.shape, pin.lShape = get_shape(inEdge, outEdge, inShape, outShape)
local pos = 13
local strLen = string.unpack("I1", data, pos)
local str = string.unpack("c" .. strLen, data, pos+1)
pin.desc = str
pos = pos + 1 + strLen
pos = pos + 1
pin.t,pin.dir,pin.len, pin.x, pin.y = string.unpack("i1i1i2i2i2", data, pos)
pin.disName = BF(pin.dir, 3) == 1 --and "show" or "hide"
pin.disNum = BF(pin.dir, 4) == 1 --and "show" or "hide"
pin.disNameT = tostring(pin.disName)
pin.disNumT = tostring(pin.disNum)
pin.hide = BF(pin.dir,2) == 1 --and "hide" or "show"
pin.hideT = tostring(pin.hide)
pin.dir = toKicadPinDir(math.floor(BF(pin.dir,0,1) * 90))
pin.pinLength = ad2kicad_CoordValue(pin.len)
pin.x = ad2kicad_CoordValue(pin.x)
pin.y = ad2kicad_CoordValue(pin.y)
if pin.dir == "R" then
pin.x = pin.x - pin.pinLength
pin.bbox = BBOX.create(pin.pinLength, 10, pin.x + pin.pinLength/2, pin.y)
elseif pin.dir == "L" then
pin.x = pin.x + pin.pinLength
pin.bbox = BBOX.create(pin.pinLength, 10, pin.x - pin.pinLength/2, pin.y)
elseif pin.dir == "U" then
pin.y = pin.y - pin.pinLength
pin.bbox = BBOX.create(10, pin.pinLength, pin.x , pin.y + pin.pinLength/2)
elseif pin.dir == "D" then
pin.y = pin.y + pin.pinLength
pin.bbox = BBOX.create(10, pin.pinLength, pin.x , pin.y - pin.pinLength/2)
end
-- TODO need parse the PinTextData to get the pin text size
pin.nameSize = 50
pin.numSize = 50
pos = pos + 8
pos = pos + 4
local l1 = string.unpack("I1", data, pos)
local n1 = string.unpack("c"..l1, data, pos+1)
pos = pos + 1 + l1
local l2 = string.unpack("I1", data, pos)
local n2 = string.unpack("c"..l2, data, pos + 1)
pin.name = make_symbol_pin_name(n1)
pin.name = ensureUTF8(pin.name)
pin.num = n2
pin.eleType,pin.tl = get_pin_type(pin.t)
pin.toSymbol = adPinToSymbol
comp.pins[#comp.pins+1] = pin
end
local function parseText(block,fonts,comp)
local t = block.NAME or 'user'
local value = get_str(block, "TEXT")
local x = ad2kicad_CoordValue(block['LOCATION.X'])
local y = ad2kicad_CoordValue(block['LOCATION.Y'])
local rotate = block['ORIENTATION'] or 0
local hide = false
local just = tonumber(block.JUSTIFICATION) or 0
if block.ISHIDDEN and block.ISHIDDEN=='T' then
hide = true
end
rotate = tonumber(rotate) * 90
local font = buildFont(block.FONTID, fonts)
local hJust = ad2kicad_textJustify[just] and ad2kicad_textJustify[just][2] or 'C'
local vJust = ad2kicad_textJustify[just] and ad2kicad_textJustify[just][1] or 'C'
if t == 'Designator' then
value = string.sub(value,1,-2)
comp.ref = value == "" and 'U' or value
end
--x = math.floor(tonumber(x))
--x = math.floor(tonumber(y))
local v = {
x = x,
y = y,
value = value,
t = t,
rotate = rotate,
font = font,
hide = hide,
layer = 'symbol',
toSymbol = textToSymbol,
isText = true,
hJust = hJust,
vJust = vJust,
}
comp.texts = comp.texts or {}
comp.texts[#comp.texts+1] = v
end
local function list_block(block)
for k,v in pairs(block) do
logData(" "..k.."="..v)
end
end
local function parseCompHeader(block,fonts,comp)
--list_block(block)
comp.name = get_str(block, "LIBREFERENCE")
comp.drawPinNo = 'Y'
comp.drawPinName = 'Y'
comp.ref = '***'
comp.partNum = block.PARTCOUNT
comp.description = get_str(block, "COMPONENTDESCRIPTION") --[ [[%UTF8%COMPONENTDESCRIPTION]] ] or block[ [[COMPONENTDESCRIPTION]] ] or ""
end
local function parseDummy(block,fonts,comp)
return {}
end
local function element(array)
local i = 0
return function()
i = i + 1
return array[i]
end
end
local function parseFP(block,fonts,comp)
comp.FPName = block.MODELNAME
end
local function rectToSymbol(rect)
return string.gsub("S $x1 $y1 $x2 $y2 $part 1 $width $fill", "%$(%w+)", rect)
end
local function parseRectangle(block, fonts, comp)
--logData("Rectangle")
--list_block(block)
local f = block.ISSOLID and block.ISSOLID == 'T'
local r = {
x1 = ad2kicad_CoordValue(block['LOCATION.X']),
y1 = ad2kicad_CoordValue(block['LOCATION.Y']),
x2 = ad2kicad_CoordValue(block['CORNER.X']),
y2 = ad2kicad_CoordValue(block['CORNER.Y']),
part = block.OWNERPARTID or 0,
width = getWidth(block.LINEWIDTH),
fill = f and 'f' or 'N',
toSymbol = rectToSymbol,
}
r.bbox = BBOX.create2P(r.x1,r.y1,r.x2,r.y2)
comp.graphs[#comp.graphs+1] = r
end
local function parseArc(block, fonts, comp)
local x = ad2kicad_CoordValue(block['LOCATION.X'])
local y = ad2kicad_CoordValue(block['LOCATION.Y'])
local r = ad2kicad_CoordValue(block.RADIUS)
local startAngle = block.STARTANGLE or 0
local sweepAngle = block.ENDANGLE or 0
sweepAngle = sweepAngle - startAngle
local width = getWidth(block.LINEWIDTH)
local v = toKicadArc(x,y,r,startAngle,sweepAngle,width,"symbol")
v.part = block.OWNERPARTID or 0
local f = block.ISSOLID and block.ISSOLID == 'T'
v.fill = f and 'f' or 'N'
v.bbox = BBOX.create(r*2,r*2)
v.bbox = BBOX.moveTo(v.bbox, x, y)
comp.graphs[#comp.graphs+1] = v
end
local function parseElliArc(block, fonts, comp)
-- just ignore the second radius
return parseArc(block, fonts, comp)
end
local function parseElliCircle(block, fonts, comp)
block.STARTANGLE = 0
block.ENDANGLE = 360
return parseArc(block, fonts, comp)
end
local function buildShape(shape, x1,y1, x2,y2, size)
-- TODO support for the line shape
end
local function parseLine(block, fonts, comp)
--list_block(block)
local pts = {}
pts[1] = {ad2kicad_CoordValue(block['LOCATION.X']), ad2kicad_CoordValue(block['LOCATION.Y'])}
pts[2] = {ad2kicad_CoordValue(block['CORNER.X']), ad2kicad_CoordValue(block['CORNER.Y'])}
local v = toKicadPoly(pts, "symbol")
v.fill = 'N'
v.part = block.OWNERPARTID or 0
v.width = getWidth(block.LINEWIDTH)
v.directOutput = true
comp.graphs[#comp.graphs+1] = v
end
local function parsePolyLine(block, fonts, comp)
local cnt = tonumber(block.LOCATIONCOUNT)
--list_block(block)
local pts = {}
for i=1,cnt do
pts[#pts+1] = {ad2kicad_CoordValue(block['X'..i]), ad2kicad_CoordValue(block['Y'..i])}
end
local v = toKicadPoly(pts, "symbol")
v.fill = 'N'
v.part = block.OWNERPARTID or 0
v.width = getWidth(block.LINEWIDTH)
v.directOutput = true
comp.graphs[#comp.graphs+1] = v
end
local function parsePoly(block, fonts, comp)
local cnt = tonumber(block.LOCATIONCOUNT)
local pts = {}
for i=1,cnt do
pts[#pts+1] = {ad2kicad_CoordValue(block['X'..i]), ad2kicad_CoordValue(block['Y'..i])}
end
pts[#pts+1] = pts[1]
local v = toKicadPoly(pts, "symbol")
local f = block.ISSOLID and block.ISSOLID == 'T'
v.fill = f and 'f' or 'N'
v.part = block.OWNERPARTID or 0
v.width = getWidth(block.LINEWIDTH)
v.directOutput = true
comp.graphs[#comp.graphs+1] = v
end
local function bLineToSymbol(bline)
local r = string.gsub("B $count $part 1 $width ", "%$(%w+)", bline)
for i=1,#bline.pts do
local x,y = bline.pts[i][1], bline.pts[i][2]
r = r .. x .. " " .. y .. " "
end
return r .. bline.fill
end
local function parseBLine(block, fonts, comp)
logData("Not support bline right now")
--[[
local cnt = tonumber(block.LOCATIONCOUNT)
local pts = {}
for i=1,cnt do
pts[#pts+1] = {ad2kicad_CoordValue(block['X'..i]), ad2kicad_CoordValue(block['Y'..i])}
end
local v = {}
v.pts = pts
v.count = #pts
v.part = block.OWNERPARTID or 0
v.width = getWidth(block.LINEWIDTH)
local f = block.ISSOLID and block.ISSOLID == 'T'
v.fill = f and 'f' or 'N'
v.toSymbol = bLineToSymbol
comp.graphs[#comp.graphs+1] = v
--]]
end
------------------------------------------------------------------------------------
-------------Convert IEEE symbol to kicad symbol begin -----------------------------
------------------------------------------------------------------------------------
local function IEEE_polyLine_method(param, block, fonts, comp)
local pts = {}
local x = ad2kicad_CoordValue(block['LOCATION.X'])
local y = ad2kicad_CoordValue(block['LOCATION.Y'])
local rotate = nil
if block.ORIENTATION then
rotate = tonumber(block.ORIENTATION) * 90
end
local scale = ad2kicad_CoordValue(block.SCALEFACTOR)
scale = scale / 100
for pt in element(param) do
local tx,ty = pt[1]*scale + x, pt[2]*scale + y
if rotate then
tx,ty = rotate_p2p(x,y,tx,ty,rotate)
end
pts[#pts+1] = {math.floor(tx), math.floor(ty)}
end
local v = toKicadPoly(pts, "symbol")
local f = block.ISSOLID and block.ISSOLID == 'T'
v.fill = f and 'f' or 'N'
v.part = block.OWNERPARTID or 0
v.width = getWidth(block.LINEWIDTH)
v.directOutput = true
comp.graphs[#comp.graphs+1] = v
end
local function IEEE_circle_method(param, block, fonts, comp)
block.RADIUS = tostring(tonumber(block.SCALEFACTOR)/2)
parseElliCircle(block, fonts, comp)
end
local function IEEE_arc_method(param, block, fonts, comp)
factor = tonumber(block.SCALEFACTOR)
local scale = factor / 100
local rotate = 0
if block.ORIENTATION then
rotate = tonumber(block.ORIENTATION) * 90
end
local x = block['LOCATION.X']
local y = block['LOCATION.Y']
local mx,my = param.x*scale + x, param.y*scale + y
mx,my = rotate_p2p(x,y,mx,my, rotate)
block['LOCATION.X'] = mx
block['LOCATION.Y'] = my
block.RADIUS = param.r*scale
block.STARTANGLE = param.startAngle + rotate
block.ENDANGLE = param.endAngle + rotate
parseArc(block, fonts, comp)
end
local function IEEE_mixed_method(param, block, fonts, comp)
for action in element(param) do
action.method(action.param, block, fonts, comp)
end
end
-----------------------------------------------------------------------------------
---- How to add new IEEE symbol
---- 1. Create a IEEE symbol in AD, with x,y = 0,0, rotate = 0, scale = 100
---- 2. For poly lines, record the points coordinate in AD
---- 3. For circle witch center is at 0,0, use IEEE_circle_method without param
---- 4. For circle center not at 0,0, record the center and radius,
---- set startAngle = 0, endAngle = 360, use IEEE_arc_method
---- 5. For arc, record the center point, radius and start/end angle
---- 6. For poly line with arc, combine them with IEEE_mixed_method
local IEEE_sym = {
['1'] = { -- dot
method = IEEE_circle_method,
param = {}
},
['2'] = { -- right left signal flow
method = IEEE_polyLine_method,
param = {{0,40},{120,0},{120, 80},{0,0}}
},
['3'] = { -- clock
method = IEEE_polyLine_method,
param = {{0,0},{0,80},{120, 40},{0,0}}
},
['4'] = { -- active low input
method = IEEE_polyLine_method,
param = {{0,0},{120,0},{0, 60},{0,0}}
},
['5'] = { -- analog in
method = IEEE_mixed_method,
param = {
{ method = IEEE_polyLine_method,
param = {{0,30},{0,0},{60,0},{60,30}}
},
{ method = IEEE_arc_method,
param = {x=30,y=30,r=30,startAngle = 0, endAngle = 180}
},
}
},
['6'] = { -- not logic connection
method = IEEE_mixed_method,
param = {
{ method = IEEE_polyLine_method,
param = {{0,0},{80,80}}
},
{ method = IEEE_polyLine_method,
param = {{0,80},{80,0}}
},
}
},
['7'] = { -- shift right
method = IEEE_polyLine_method,
param = {{0,0}, {60,-30}, {60,30}, {0,0}}
},
['8'] = { -- postpond out
method = IEEE_polyLine_method,
param = {{80,0}, {80,80}, {0,80}}
},
['9'] = { -- open collector
method = IEEE_mixed_method,
param = {
{ method = IEEE_polyLine_method,
param = {{0,0}, {80,0}},
},
{ method = IEEE_polyLine_method,
param = {{0,40}, {40,0}, {80,40}, {40, 80}, {0, 40}},
},
}
},
['10'] = { -- HiZ
method = IEEE_polyLine_method,
param = {{0,80}, {40,0}, {80,80}, {0,80}}
},
['11'] = { -- high current
method = IEEE_polyLine_method,
param = {{0,0},{0,80},{80, 40},{0,0}}
},
['12'] = { -- pulse
method = IEEE_polyLine_method,
param = {{0,0},{80,0},{80, 80},{160,80}, {160, 0}, {240,0}}
},
['13'] = { -- schmitt
method = IEEE_polyLine_method,
param = {{0,0},{120,10},{120, 70},{160,80}, {40,70}, {40,10},{0,0}}
},
['14'] = { -- delay
method = IEEE_polyLine_method,
param = {{0,-20},{0,20},{0, 0},{200,0}, {200, -20} , {200, 20}}
},
}
local function drawIEEESymbol(symbol, block, fonts, comp)
local is = IEEE_sym[symbol]
if is then
is.method(is.param, block, fonts, comp)
else
logData("IEEE symbol " .. symbol .. " not support")
end
end
local function parseIEEESymbol(block, fonts, comp)
local sym = block.SYMBOL
drawIEEESymbol(sym, block, fonts, comp)
--logData("IEEE")
--list_block(block)
end
------------------------------------------------------------------------------------
-------------Convert IEEE symbol to kicad symbol end -------------------------------
------------------------------------------------------------------------------------
local record_t = {
['1'] = parseCompHeader,
['41'] = parseText,
['34'] = parseText,
['4'] = parseText,
['44'] = parseDummy,
['45'] = parseFP,
['46'] = parseDummy,
['48'] = parseDummy,
['14'] = parseRectangle,
['12'] = parseArc,
['6'] = parsePolyLine,
['13'] = parseLine,
['7'] = parsePoly,
['5'] = parseBLine,
['11'] = parseElliArc,
['8'] = parseElliCircle,
['3'] = parseIEEESymbol,
}
local function mkDummy(text, t)
text = text and '"' .. text .. '"' or '"Dummy"'
t = t and '"' .. t .. '"' or '"Dummy"'
return {
x = 0, y = 0,
value = text,
t = t,
rotate = 0,
font = {w=50,h=50,thickness=10},
hide = true,
layer = 'symbol',
toSymbol = textToSymbol,
isText = true,
}
end
local function adCompToSymbol(comp, libname4symbol)
local r = "#\n# " .. comp.name .. "\n#\n"
local pin_str = ""
local graph_str = ""
comp.drawPinNo = 'N'
comp.drawPinName = 'N'
local partUsed = {}
local bbox = BBOX.create(1,1)
for pin in element(comp.pins) do
pin_str = pin_str .. pin:toSymbol() .. "\n"
if pin.disName then comp.drawPinName = 'Y' end
if pin.disNum then comp.drawPinNo = 'Y' end
partUsed[tonumber(pin.partNum)] = 1
bbox = BBOX.merge(bbox, pin.bbox)
end
for graph in element(comp.graphs) do
graph_str = graph_str .. graph:toSymbol() .. "\n"
partUsed[tonumber(graph.part)] = 1
bbox = BBOX.merge(bbox, graph.bbox)
end
comp.partNum = #partUsed
comp.defName = make_name(comp.name)
r = r .. string.gsub([[DEF $defName $ref 0 40 $drawPinNo $drawPinName $partNum F N]], "%$(%w+)", comp)
r = r .. "\n"
local fp = comp.FPName
if libname4symbol and libname4symbol ~= "" then
fp = libname4symbol .. ":" .. string.upper(fp)
end
local bbl,bbt,bbr,bbb = BBOX.splitBB(bbox)
local texts = {mkDummy(),mkDummy(comp.name),mkDummy(fp),mkDummy(""), mkDummy(comp.description, 'description')}
texts[3].x = math.floor(bbl)
texts[3].y = math.floor(bbt - 200)
texts[3].hJust = 'L'
texts[3].vJust = 'B'
texts[2].x = math.floor(bbl + 50)
texts[2].y = math.floor(bbb + 150)
texts[2].hJust = 'L'
texts[2].vJust = 'B'
local vPos = math.floor(bbt - 300)
for text in element(comp.texts) do
if text.t == 'Designator' then
text.x = math.floor(bbl)
text.y = math.floor(bbb + 50)
texts[1] = text
elseif text.t == 'Comment' then
text.x = math.floor(bbl + 150)
text.y = math.floor(bbb + 50)
texts[#texts+1] = text
elseif text.t == 'ComponentLink1URL' then
text.x = math.floor(bbl)
text.y = math.floor(bbt - 100)
texts[4] = text
else
if text.t ~= "user" then
text.x = math.floor(bbl)
text.y = math.floor(vPos)
end
texts[#texts+1] = text
vPos = vPos - 100
end
text.value = text.value or ""
text.value = '"'..text.value..'"'
text.t = '"'..text.t..'"'
end
for i=1,#texts do
r = r .. texts[i]:toSymbol(i-1, i>4) .. "\n"
end
r = r .. "DRAW\n"
r = r .. graph_str .. pin_str
r = r .. "ENDDRAW\nENDDEF\n"
return r
end
local function parseComp(compPath, fonts)
local blocks = parseADBlockFile(compPath.."/data")
local r = {}
r.texts = {}
r.graphs = {}
r.pins = {}
for i=1,#blocks do
local bk = blocks[i]
if bk.bin then
parsePin(bk.bin,fonts,r)
else
local rec = tostring(bk.RECORD,fonts)
if record_t[rec] then
local g = record_t[rec](bk,fonts,r)
else
logData("Unknown record type " .. rec .. " in " .. compPath)
list_block(bk)
end
end
end
r.toSymbol = adCompToSymbol
return r
end
function parse_schlib(libpath, logFunc)
logData = logFunc or logData
local compList, fonts = parse_Header(libpath.."/FileHeader")
local comps = {}
for i=1,#compList do
local n = string.gsub(compList[i].name, "([/\\%*])", "_")
if #n > 31 then
n = string.sub(n,1,31)
end
if string.sub(n,#n,#n) == '.' then
n = string.sub(n,1,#n-1) .. '_'
end
local comp = parseComp(libpath.."/"..n, fonts)
comps[#comps+1] = comp
end
return comps
end