-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpcad_lib.lua
1221 lines (1158 loc) · 34.8 KB
/
pcad_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
---------------------------------------------------------------------
---- PCAD ascii format library parser
---- Read data from the S-Expression file, then convert them
---- into kicad format
---- Can convert both the symbol library and footprint library
----------------------------------------------------------------------
require("s_format")
BBOX = require("bbox")
local LogI = function() end
local progress_hook = function() end
-- convert pcad shape to kicad shape
local shapes_t = {
Rect = "rect",
Oval = "oval",
}
-- convert pcad layer to kicad layer
local layers_t = {
["6"] = "F.SilkS",
["21"] = "F.Fab",
["22"] = "F.Fab",
["8"] = "F.Paste",
["symbol"] = "symbol"
}
-- covert pcad graph to kicad graph
local graph_t = {
arc = "fp_arc",
line = "fp_line",
pcbPoly = "fp_poly",
}
local global_fonts = {}
local global_units = "mil"
-- covert pcad number to kicad, mm/mil to mm, remove the unit string
local global_symbol_mode = true
function tokicad(value, quit)
if global_symbol_mode then
local p = string.find(value, "mm")
if p then
local t = string.sub(value,1, p-1)
return string.format( "%.3f", tonumber(t)/ 0.0254)
end
p = string.find(value, "deg")
if p then
return string.sub(value,1, p-1)
end
p = string.find(value, "mil")
if p then
return math.floor(tonumber(string.sub(value,1, p-1)))
end
if quit then return tonumber(value) end
return tokicad(value .. global_units, true)
end
local p = string.find(value, "mm")
if p then
return string.sub(value,1, p-1)
end
p = string.find(value, "deg")
if p then
return string.sub(value,1, p-1)
end
p = string.find(value, "mil")
if p then
local t = string.sub(value,1, p-1)
return string.format( "%.3f", tonumber(t)* 0.0254)
end
if quit then return string.format( "%.3f", tonumber(value)* 0.0254) end
-- append global units and parse again
return tokicad(value .. global_units, true)
--return string.format( "%.3f", tonumber(value)* 0.0254)
end
local function ele_is(v,n)
return (type(v) == "table") and (v[1] == n)
end
-- translate pcad coordinate to kicad
local function KiCoord(x,y)
if global_symbol_mode then
return x,y
end
return x,-y
end
-- print pad in kicad format
local function posToString(x,y,rotate)
if tonumber(rotate) == 0 then
rotate = nil
end
return "at " .. x .. " " .. y .. (rotate and (" " .. rotate) or "")
end
local function print_pad(pad, num, x,y, rotate)
local at = " ("..posToString(x,y,rotate) .. ")"
local r = "(pad " ..num .." " .. pad.type .. " " .. pad.shape ..
at ..
" (size " .. pad.size .. ")" ..
" (drill " .. pad.drill .. ")" ..
" (layers " .. pad.layers .. ")" ..
")"
return r
end
local function pad_to_string(pad)
return print_pad(pad.data, pad.num, pad.x, pad.y, pad.rotate)
end
-- create part's pad
local function create_part_pad(pads, name, num, x, y, rotate)
if pads[name] then
local bbox = pads[name].bbox
return {
data = pads[name],
num = num,
x = x,
y = y,
rotate = rotate,
tostring = pad_to_string,
bbox = BBOX.rotate(BBOX.moveTo(bbox,x,y), rotate),
}
else
LogI(name, " pad style not found")
end
end
-- create a pad style
local function build_pad(t)
local n = t[2]
local v = {}
v.drill = tokicad(t[3][2])
local w1 = t[6][4][2]
local h1 = t[6][5][2]
local w2 = t[7][4][2]
local h2 = t[7][5][2]
v.type = "smd"
if w2 ~= "0.0" or h2 ~= "0.0" then
v.type = "thru_hole"
if w1 ~= w2 or h1~=h2 then
LogI("top/bottom size missmatch in ", n)
end
end
w1, h1 = tokicad(w1), tokicad(h1)
v.size = w1 .. " " .. h1
v.bbox = BBOX.create(w1,h1)
local s = t[6][3][2]
if s and shapes_t[s] then
v.shape = shapes_t[s]
else
LogI("Unknown shape in ", n)
v.shape = "unknown"
end
if (tokicad(w1) == "0") or (tokicad(h1) == "0") then
LogI("Pad dimension may error in ", n)
end
if v.type == "smd" then
v.layers = "F.Cu F.Paste F.Mask"
else
v.layers = "*.Cu *.Mask"
end
return n,v
end
local function fontToString(font)
return string.gsub("font (size $h $w) (thickness $thickness)", "%$(%w+)", font)
end
local function build_font(t)
local n = t[2]
local v = {w=1,h=1,thickness=0.15}
for j=3, #t do
if ele_is(t[j], "font") then
local r = s_expand(t[3], {fontType = 1, fontHeight = tokicad, strokeWidth = tokicad})
if r.fontType == "Stroke" then
v = {w=r.fontHeight,h=r.fontHeight,thickness = r.strokeWidth}
end
end
end
v.tostring = fontToString
return n,v
end
local function progress(total, duration)
duration = duration or 0
local i = 1
return function(cur)
if i>duration then
i = 0
progress_hook(cur, total)
end
i = i + 1
end
end
-- collect all pad and font style in the library file
local function collect_pad_font(lib_table)
local pads = {}
local fonts = {}
pads.count = 0
fonts.count = 0
local hk = 1
local prog = progress(#lib_table, 20)
for i,e in s_elements(lib_table, "padStyleDef") do
local k,v = build_pad(e)
pads[k] = v
pads.count = pads.count + 1
prog(i)
end
for i,e in s_elements(lib_table, "textStyleDef") do
local k,v = build_font(e)
fonts[k] = v
fonts.count = pads.count + 1
prog(i)
end
pads.create_pad = create_part_pad
return pads, fonts
end
local function ptParser(x,y)
x,y = KiCoord(tokicad(x), tokicad(y))
return {x=x, y=y}
end
local function angleParser(rot)
return tokicad(rot.."deg")
end
local function boolParser(bool)
return bool ~= "False"
end
local function fontParser(f)
local font = global_fonts[f]
if not font then
LogI("Font (" .. f ..") not found")
end
return font
end
local function displayParser(p1, p2)
return {[p1[1]] = p1[2], [p2[1]]=p2[2]}
end
local function parse_pads(t, pads)
local r = {}
for i,e in s_elements(t, "pad") do
local pad = s_expand(
e,
{ -- required field
padNum=1,
padStyleRef=1,
pt = ptParser,
},{ -- optinaol field
rotation = angleParser,
defaultPinDes = function(num)
num = string.sub(num,2,-2)
if #num == 0 then return [[""]] end
return num
end })
r[#r+1] = pads:create_pad(pad.padStyleRef,pad.defaultPinDes or pad.padNum,pad.pt.x,pad.pt.y,pad.rotation)
end
return r
end
local function arcToString(arc)
if math.abs(arc.angle) == 360 then
return string.gsub("(fp_circle (center $sx $sy) (end $ex $ey) (layer $layer) (width $width))", "%$(%w+)", arc)
else
return string.gsub("(fp_arc (center $sx $sy) (end $ex $ey) (angle $angle) (layer $layer) (width $width))", "%$(%w+)", arc)
end
end
local function arcToSymbol(arc, part)
local tt = {}
for k,v in pairs(arc) do
tt[k] = v
end
tt.part = part or arc.part or 0
local sa = tt.startAngle
tt.startAngle = math.floor(tt.endAngle)
tt.endAngle = math.floor(sa)
tt.fill = arc.fill or 'N'
if math.abs(tt.startAngle - tt.endAngle) == 3600 then
return string.gsub("C $x $y $radius $part 1 $width $fill", "%$(%w+)", tt)
else
tt.sx = math.floor(arc.sx + math.cos(toArc(tt.startAngle/10))*arc.radius)
tt.sy = math.floor(arc.sy + math.sin(toArc(tt.startAngle/10))*arc.radius)
tt.ex = math.floor(arc.sx + math.cos(toArc(tt.endAngle/10))*arc.radius)
tt.ey = math.floor(arc.sy + math.sin(toArc(tt.endAngle/10))*arc.radius)
return string.gsub("A $x $y $radius $startAngle $endAngle $part 1 $width $fill $sx $sy $ex $ey", "%$(%w+)", tt)
end
end
function toKicadArc(x,y,r,startAngle, sweepAngle, width, layer)
local res = {}
sweepAngle = tonumber(sweepAngle)
res.sx,res.sy,res.ex,res.ey,res.angle,res.width = x,y, x+math.cos(toArc(startAngle))*r, y-math.sin(toArc(startAngle))*r, -sweepAngle, width
res.radius = r
res.x,res.y = x,y
res.startAngle = startAngle*10
res.endAngle = (startAngle + sweepAngle)*10
res.layer = layer
res.tostring = arcToString
res.toSymbol = arcToSymbol
res.bbox = { {res.sx-r, res.sy-r}, {res.sx+r, res.sy+r} }
return res
end
local function lineToString(line)
return string.gsub("(fp_line (start $sx $sy) (end $ex $ey) (layer $layer) (width $width))", "%$(%w+)", line)
end
local function lineToSymbol(line, part)
local tt = {}
tt.part = part
tt.width = line.width
tt.sx = line.sx
tt.ex = line.ex
tt.sy = line.sy
tt.ey = line.ey
return string.gsub("P 2 $part 1 $width $sx $sy $ex $ey N", "%$(%w+)", tt)
end
local function toKicadLine(pts, width, layer)
local r = {}
r.sx = pts[1][1]
r.sy = pts[1][2]
r.ex = pts[2][1]
r.ey = pts[2][2]
r.layer = layer
r.tostring = lineToString
r.toSymbol = lineToSymbol
r.width = width
local sx = tonumber(r.sx)
local ex = tonumber(r.ex)
local sy = tonumber(r.sy)
local ey = tonumber(r.ey)
local x1 = sx<ex and sx or ex
local x2 = sx<ex and ex or sx
local y1 = sy<ey and sy or ey
local y2 = sy<ey and ey or sy
r.bbox = { {x1,y1}, {x2,y2}}
r.isLine = true
return r
end
local function polyToString(poly)
local r = "(fp_poly (pts "
for i=1,#poly.pts do
local pt = poly.pts[i]
r = r .. "(xy " .. pt[1] .. " " .. pt[2] .. ")"
if i~= #poly.pts then r = r .. " " end
end
r = r .. ") (layer " .. poly.layer .. ") (width 0.15))"
return r
end
local function polyToSymbol(poly, part)
local tt = {}
tt.part = poly.part or part
tt.width = poly.width or 15
local p1,p2 = poly.pts[1], poly.pts[#poly.pts]
if poly.directOutput then
else
if p1[1] ~= p2[1] or p1[2] ~= p2[2] then
poly.pts[#poly.pts+1] = p1
end
end
tt.ptCnt = #poly.pts
local r = string.gsub("P $ptCnt $part 1 $width ", "%$(%w+)", tt)
for i=1,#poly.pts do
local x,y = poly.pts[i][1], poly.pts[i][2]
r = r .. x .. " ".. y .. " "
end
if poly.fill then
return r .. poly.fill
end
return r .. (tt.ptCnt > 4 and "f" or "N")
end
function toKicadPoly(pts, layer)
local r = {}
r.pts = pts
r.layer = layer
r.tostring = polyToString
r.toSymbol = polyToSymbol
local x1,x2,y1,y2
for i=1,#pts do
local x,y = tonumber(pts[i][1]), tonumber(pts[i][2])
x1 = x1 or x
x2 = x2 or x
y1 = y1 or y
y2 = y2 or y
x1 = x<x1 and x or x1
x2 = x2<x and x or x2
y1 = y<y1 and y or y1
y2 = y2<y and y or y2
end
r.bbox = {{x1+0,y1+0}, {x2+0,y2+0}}
return r
end
local function textToString(text)
text.fontstr = text.font:tostring()
text.pos = posToString(text.x,text.y,text.rotate)
text.hidestr = text.hide and " hide" or ""
if text.t == '"RefDes"' then
text.fpType = "reference"
text.fpLayer = "F.SilkS"
elseif text.t == '"Type"' then
text.fpType = "value"
text.fpLayer = "F.Fab"
else
text.fpType = text.t
text.fpLayer = text.layer
end
return string.gsub([[
(fp_text $fpType $value ($pos) (layer $fpLayer)$hidestr
(effects ($fontstr))
)]], "%$(%w+)", text)
end
function textToSymbol(text, index, needType)
local tt = {}
tt.value = text.value
tt.index = index
tt.size = text.font.w
tt.x = text.x
tt.y = text.y
local rot = tonumber(text.rotate or 0)
if rot == 0 or rot == 180 then
tt.dir = "H"
else
tt.dir = "V"
end
tt.visible = text.hide and "I" or "V"
tt.hJust = text.hJust or 'C'
tt.vJust = text.vJust or 'C'
tt.vJustItalicBold = tt.vJust.."NN"
local r = string.gsub([[
F$index $value $x $y $size $dir $visible $hJust $vJustItalicBold]], "%$(%w+)", tt)
if needType then
r = r .. " " .. text.t
end
return r
end
local function toKicadText(x, y, rotate, text_type, value, font, hide, layer)
local r = {}
local str = value
if string.find(value, '"') then
str = string.sub(str, 2,-2)
end
if not global_symbol_mode then
local l = #str
local rot = rotate and toArc(rotate) or toArc(0)
local lx = font.w*l*(1/1.69333)
local ly = font.h*(1/1.69333)
local lr = math.sqrt(lx*lx + ly*ly)
-- get the orignal position in normal coord
local ox, oy = KiCoord(x, y)
-- get the text mid point
local mx, my = ox + lx/ 2, oy + ly/ 2
-- rotate the mid point by org point
local nx = (mx - ox)*math.cos(rot) - (my - oy)*math.sin(rot) + ox
local ny = (mx - ox)*math.sin(rot) + (my - oy)*math.cos(rot) + oy
-- covert back to kicad coord
r.x,r.y = KiCoord(nx,ny)
else
r.x,r.y = x,y
end
r.rotate = rotate
r.value = value
r.t = text_type
r.font = font
r.hide = hide
r.layer = layer
r.tostring = textToString
r.toSymbol = textToSymbol
r.isText = true
return r
end
local function parse_graph(gv, layer, parent_name)
if gv[1] == "arc" then
local arc = s_expand(gv, {
pt=ptParser,
radius=tokicad,
startAngle=angleParser,
sweepAngle=angleParser,
width=tokicad})
return toKicadArc(arc.pt.x,arc.pt.y,arc.radius,arc.startAngle,arc.sweepAngle,arc.width,layer)
elseif gv[1] == "line" then
local pts = {}
local width
for i=2,#gv do
if ele_is(gv[i], "pt") then
local x,y = tokicad(gv[i][2]), tokicad(gv[i][3])
x,y = KiCoord(x,y)
pts[#pts+1] = {x,y}
elseif ele_is(gv[i], "width") then
width = tokicad(gv[i][2])
elseif ele_is(gv[i], "style") then
-- kicad not support line style
else
LogI("Un-processed element of line graph in ", parent_name)
end
end
return toKicadLine(pts,width,layer)
elseif gv[1] == "pcbPoly" or gv[1] == "poly" then
local pts = {}
for i=2,#gv do
if ele_is(gv[i], "pt") then
local x,y = tokicad(gv[i][2]), tokicad(gv[i][3])
x,y = KiCoord(x,y)
pts[#pts+1] = {x,y}
else
LogI("Un-processed element of poly graph in ", parent_name)
end
end
return toKicadPoly(pts,layer)
elseif gv[1] == "attr" then
local r = s_expand(gv, {[2] = "attr_t", [3] = "value", pt = ptParser, textStyleRef = fontParser},
{rotation = angleParser, isVisible = boolParser, justify = 1, extent = 1}, { isVisible = true}, true)
if r.attr_t == [["RefDes"]] then
r.value = "REF**"
elseif r.attr_t == [["Type"]] then
if global_symbol_mode then
r.value = removeSpaces(parent_name)
else
r.value = removeParentheses(parent_name)
end
end
return toKicadText(r.pt.x,r.pt.y,r.rotation, r.attr_t, r.value, r.textStyleRef, not r.isVisible, layer)
elseif gv[1] == "text" then
local r = s_expand(gv, {[3] = "value", pt = ptParser, textStyleRef = fontParser},
{rotation = angleParser, isVisible = boolParser, justify = 1, extent = 1}, {isVisible = true}, true)
return toKicadText(r.pt.x,r.pt.y,r.rotation, "user", r.value, r.textStyleRef, not r.isVisible, layer)
else
LogI("Unknown graph type (" .. gv[1] .. ") in ", parent_name)
end
return nil
end
local function parse_part_graph(t, part)
part.graphs = part.graphs or {}
local graphs = part.graphs
local l = t[2][2]
local layer = layers_t[l]
if layer then
for j=3, #t do
local r = parse_graph(t[j], layer, part.name)
if r then graphs[#graphs+1] = r end
end
else
LogI("Unknown layer(" .. l .. ") in ", part.name)
end
end
local function add_bbox_line(dest, bbox, layer, width)
layer = layer or "F.CrtYd"
width = width or 0.05
local l,t,r,b = BBOX.splitBB(bbox)
l = l - width
t = t - width
r = r + width
b = b + width
dest[#dest+1] = toKicadLine({{l,t},{r,t}},width,layer)
dest[#dest+1] = toKicadLine({{r,t},{r,b}},width,layer)
dest[#dest+1] = toKicadLine({{r,b},{l,b}},width,layer)
dest[#dest+1] = toKicadLine({{l,b},{l,t}},width,layer)
end
local function create_part_bbox(part)
local bbox = BBOX.create(1,1)
local graphs = part.graphs
for i=1,#part.pads do
local r = BBOX.merge(bbox, part.pads[i].bbox)
--add_bbox_line(graphs, part.pads[i].bbox, "B.CrtYd")
bbox = r or bbox
if not r then
LogI("merge pads bbox fail")
end
end
for i=1,#graphs do
local bb = graphs[i].bbox
if bb then
--add_bbox_line(graphs, bb, "B.SilkS")
local r = BBOX.merge(bbox, bb)
bbox = r or bbox
if not r then
LogI("merge graph bbox fail ... ", graphs[i]:tostring())
end
end
end
add_bbox_line(graphs, bbox)
end
function make_name(n)
if string.find(n,'"') == 1 then
n = string.sub(n,2,-2)
end
n = string.gsub(n, "([%(%) ])", {["("] = "_", [")"] = "", [" "] = "_"})
return n
end
local function build_part(t, pads)
local r = {}
r.pads = {}
r.graphs = {}
local n = t[3][2]
local graph = t[5]
if string.find(n,'"') == 1 then
n = string.sub(n,2,-2)
end
--n = string.gsub(n, "([%(%) ])", {["("] = "_", [")"] = "", [" "] = "_"})
n = string.gsub(n, " ", " ")
n = string.gsub(n, " ", "_")
r.name = n
for i,v in s_elements(graph, "multiLayer") do
r.pads = parse_pads(v, pads)
end
for i,v in s_elements(graph, "layerContents") do
parse_part_graph(v, r)
end
create_part_bbox(r)
return r
end
local function pinToKiCadSymbol(pin)
local r = ""
for k,v in pairs(pin) do
r = r .. k .. ":" .. tostring(v) .. ", "
end
return r
end
local function build_pin(t, parent_name)
local pin = {}
local r = s_expand(t, {pinNum=1,pt=ptParser, pinLength=tokicad, pinDisplay=displayParser,
pinDes = function(e) return parse_graph(e, "symbol", parent_name) end,
pinName = function(e) return parse_graph(e, "symbol", parent_name) end,
},{insideEdgeStyle=1, outsideEdgeStyle=1,isFlipped=boolParser,rotation=angleParser },{},true)
r.toSymbol = pinToKiCadSymbol
return r.pinNum, r
end
local function compPinToKiCadSymbol(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"
end
return r
end
local eleType_t = {
Passive = "U",
Bidirectional = "B",
Input = "I",
Output = "O",
Power = "W",
PowerIn = "W",
PowerOut = "w",
ThreeState = "T",
OpenH = "C",
OpenL = "E",
}
local function toKicadPinEleType(pcadType)
if eleType_t[pcadType] then
return eleType_t[pcadType]
else
LogI("Unkonwn pin type " .. pcadType)
return "U"
end
end
function toKicadPinDir(angle)
local a = tonumber(angle)
if a == 0 then return "L"
elseif a == 90 then return "D"
elseif a == 180 then return "R"
elseif a == 270 then return "U"
else
LogI("Unkonwn pin direction " .. angle)
return "R"
end
end
local function symbolCreatePin(sym, compPin)
local symPin = sym.pins[compPin.symPinNum]
local r = {}
if symPin then
for k,v in pairs(symPin) do
r[k] = v
end
r.x = r.pt.x
r.y = r.pt.y
r.nameSize = r.pinDes.font.w
r.numSize = r.pinName.font.w
r.name = make_name(compPin.pinName)
r.num = make_name(compPin.pinNum)
r.disNum = r.pinDisplay.dispPinDes ~= "False"
r.disName = r.pinDisplay.dispPinName ~= "False"
r.partNum = compPin.partNum
r.dir = toKicadPinDir(r.rotation)
if r.dir == "R" then
r.x = r.x - r.pinLength
elseif r.dir == "L" then
r.x = r.x + r.pinLength
elseif r.dir == "U" then
r.y = r.y - r.pinLength
elseif r.dir == "D" then
r.y = r.y + r.pinLength
end
r.eleType = toKicadPinEleType(compPin.pinType)
r.toSymbol = compPinToKiCadSymbol
return r
else
LogI("Fail to find pin ("..compPin.symPinNum..") in symbol " .. sym.name)
return nil
end
end
local function build_symbol(t)
local n = t[2]
local symbol = {}
symbol.name = n
symbol.pins = {}
symbol.graphs = {}
for i=4,#t do
local e = t[i]
if ele_is(e, "pin") then
local pinNum, pin = build_pin(e, n)
if symbol.pins[pinNum] then
LogI("Pin number (" .. pinNum .. ") re-defined in " .. n)
end
symbol.pins[pinNum] = pin
else
local graph = parse_graph(e, "symbol", n)
symbol.graphs[#symbol.graphs+1] = graph
end
end
symbol.createPin = symbolCreatePin
return n,symbol
end
local function take(t, pos)
local r = {}
for i=1,#t do
if i~= pos then
r[#r+1] = t[i]
end
end
return r
end
local function pt_equal(p1,p2)
return p1[1] == p2[1] and p1[2] == p2[2]
end
local function pt_s(l)
return {l.sx, l.sy}
end
local function pt_e(l)
return {l.ex, l.ey}
end
local function find_pt(pt, ls)
local find = false
local pos = nil
local s = false
for i=1,#ls do
if pt_equal(pt, pt_s(ls[i])) then
if not find then
find = true
pos = i
s = true
else
--LogI("Point already get")
end
end
if pt_equal(pt, pt_e(ls[i])) then
if not find then
find = true
pos = i
s = false
else
--LogI("Point already get")
end
end
end
return pos,s
end
local function mergeCompLine(ls, part)
if #ls < 1 then return end
local ps = {ls[1].sx, ls[1].sy}
local np = {ls[1].ex, ls[1].ey}
local rls = take(ls,1)
local pts = {ps,np}
--LogI("---", ps[1],ps[2], " -> ", np[1],np[2])
while not pt_equal(ps, np) do
local cp = {np[1],np[2]}
local pos,s = find_pt(np, rls)
if pos then
np = s and pt_e(rls[pos]) or pt_s(rls[pos])
rls = take(rls, pos)
pts[#pts+1] = np
else
--LogI("No loop find, maybe not poly")
break
end
--LogI("---", cp[1],cp[2], " -> ", np[1],np[2])
end
local r = ""
if pt_equal(ps, np) then
local poly = toKicadPoly(pts, "symbol")
r = r .. poly:toSymbol(part) .. "\n"
else
rls = ls
end
for i=1,#rls do
r = r .. ls[i]:toSymbol(part) .. "\n"
end
return r
end
local function compToSymbol(comp, libname4symbol)
local r = "#\n# " .. comp.name .. "\n#\n"
r = r .. string.gsub([[DEF $name $ref 0 40 $drawPinNo $drawPinName $partNum F N]], "%$(%w+)", comp)
r = r .. "\n"
local sym = comp.symbols['1']
local texts = {1,2,3,4}
for i=1,#sym.graphs do
local g = sym.graphs[i]
if g.isText then
if g.t == '"RefDes"' then
g.value = '"'..comp.ref..'"'
texts[1] = g
elseif g.t == '"Type"' then
texts[2] = g
elseif g.t == '"Package"' then
g.value = comp.footPrint
if libname4symbol then
g.value = '"' .. libname4symbol .. ":" .. make_name(comp.footPrint) .. '"'
end
texts[3] = g
elseif g.t == '"ComponentLink1URL"' then
texts[4] = g
else
if g.t == 'user' then
g.t = '"user"'
end
texts[#texts + 1] = g
end
end
end
for i=1,#texts do
r = r .. texts[i]:toSymbol(i-1, i>4) .. "\n"
end
r = r .. "DRAW\n"
--for k,v in pairs(comp.symbols) do
for i = 1, 100 do
local k = tostring(i)
local v = comp.symbols[k]
if not v then break end
local compLines = {}
for i=1,#v.graphs do
local g = v.graphs[i]
if g.isLine then
compLines[#compLines+1] = g
elseif not g.isText then
r = r .. g:toSymbol(k) .. "\n"
end
end
r = r .. mergeCompLine(compLines, k)
end
for i=1,#comp.pins do
r = r .. comp.pins[i]:toSymbol() .. "\n"
end
r = r .. "ENDDRAW\nENDDEF\n"
return r
end
local up_pin_name = {VCC=1, VDD=1,["V+"]=1 }
local function buildCompHidePins(comp, hide_pins)
if #hide_pins < 1 then return end
local sym = comp.symbols['1']
if not sym then
LogI("Part 1 symbol not found in ".. comp.name)
return
end
if not sym.bbox then
sym.bbox = BBOX.create(1,1)
for i=1, #sym.graphs do
local bb = sym.graphs[i].bbox
if bb then
local r = BBOX.merge(sym.bbox, bb)
sym.bbox = r or sym.bbox
end
end
end
local tempPin = comp.pins[1]
local upY = sym.bbox[2][2]
local dwonY = sym.bbox[1][2]
local mid = (sym.bbox[2][1] + sym.bbox[1][1]) / 2
local dist = 100
local upPins = {}
local downPins = {}
for i=1, #hide_pins do
local cp = hide_pins[i]
if up_pin_name[make_name(cp.pinName)] then
upPins[#upPins+1] = cp
cp.isUp = true
else
downPins[#downPins+1] = cp
end
end
local upX = math.floor(mid - ((#upPins-1)*dist/2))
local downX = math.floor(mid - ((#downPins-1)*dist/2))
for i=1, #hide_pins do
local cp = hide_pins[i]
local p = {}
for k,v in pairs(tempPin) do
p[k] = v
end
p.name = make_name(cp.pinName)
p.num = make_name(cp.pinNum)
p.partNum = cp.partNum
if cp.isUp then
p.dir = "D"
p.x = upX
p.y = upY + p.pinLength
upX = upX + dist
else
p.dir = "U"
p.x = downX
p.y = dwonY - p.pinLength
downX = downX + dist
end
-- kicad only auto connect PowerIn with power symbol
p.eleType = toKicadPinEleType(cp.pinType)
p.hide = true
comp.pins[#comp.pins+1] = p
end
end
local function parse_comp(t, symbols)
local comp = {}
comp.name = make_name(t[2])
local headers = s_expand(t[4], { numParts=1, refDesPrefix=make_name})
comp.ref = headers.refDesPrefix
comp.partNum = headers.numParts
comp.pins = {}
local compPins = {}
local compSymbols = {}
for i=5, #t do
local e = t[i]
if ele_is(e, "compPin") then
local compPin = s_expand(e, { [2]="pinNum", pinName = 1, partNum=1,symPinNum=1,pinType=1 })
compPins[#compPins+1] = compPin
elseif ele_is(e, "attachedSymbol") then
local symbol = s_expand(e, {partNum=1,symbolName=1 })
local sym = symbols[symbol.symbolName]
if sym then
if compSymbols[symbol.partNum] then
LogI("Part("..symbol.partNum..") redefined in ".. comp.name)
end
compSymbols[symbol.partNum] = sym
end
elseif ele_is(e, "attachedPattern") then
local fp = s_expand(e, {patternName=1})
comp.footPrint = fp.patternName
end
end
local disName = false
local disNum = false
local hide_pins = {}
for i=1,#compPins do
local cp = compPins[i]
local sym = compSymbols[cp.partNum]
if sym then
local p = sym:createPin(cp)
if p then
if p.disName then disName = true end
if p.disNum then disNum = true end
comp.pins[#comp.pins+1] = p
end
else
if cp.partNum == '0' then
hide_pins[#hide_pins+1] = cp
else
LogI("Pin " .. cp.pinNum .. " fail to get part " .. cp.partNum)
end
end
end
comp.drawPinNo = disNum and "Y" or "N"
comp.drawPinName = disName and "Y" or "N"
comp.symbols = compSymbols
buildCompHidePins(comp, hide_pins)
comp.toSymbol = compToSymbol
return comp
end