Skip to content

Commit 7da3fef

Browse files
authored
add: parse the uuid property (#140)
* add: parse the uuid property * fix: use 'uuid_text'instead 'uuid' in find_footprint function * fix: use 'uuid_text' instead 'uuid' in footprints-panel * style: avoid property name confusion * fix: return uuid or tstamp property in all nodes
1 parent f5ad028 commit 7da3fef

3 files changed

Lines changed: 88 additions & 22 deletions

File tree

‎src/kicad/board.ts‎

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
Stroke,
1414
TitleBlock,
1515
expand_text_vars,
16+
type HasUniqueID,
1617
} from "./common";
1718
import { P, T, parse_expr, type Parseable } from "./parser";
1819
import type { List } from "./tokenizer";
@@ -119,9 +120,9 @@ export class KicadPCB {
119120
return bbox;
120121
}
121122

122-
find_footprint(uuid_or_ref: string) {
123+
find_footprint(uuid_or_ref: string): Footprint | null {
123124
for (const fp of this.footprints) {
124-
if (fp.uuid == uuid_or_ref || fp.reference == uuid_or_ref) {
125+
if (fp.unique_id == uuid_or_ref || fp.reference == uuid_or_ref) {
125126
return fp;
126127
}
127128
}
@@ -146,14 +147,15 @@ export class Property {
146147
}
147148
}
148149

149-
export class LineSegment {
150+
export class LineSegment implements HasUniqueID {
150151
start: Vec2;
151152
end: Vec2;
152153
width: number;
153154
layer: string;
154155
net: number;
155156
locked = false;
156-
tstamp: string;
157+
uuid?: string;
158+
tstamp?: string;
157159

158160
constructor(expr: Parseable) {
159161
/*
@@ -176,10 +178,15 @@ export class LineSegment {
176178
P.pair("layer", T.string),
177179
P.pair("net", T.number),
178180
P.atom("locked"),
181+
P.pair("uuid", T.string),
179182
P.pair("tstamp", T.string),
180183
),
181184
);
182185
}
186+
187+
get unique_id(): string | undefined {
188+
return this.uuid ?? this.tstamp;
189+
}
183190
}
184191

185192
export class ArcSegment {
@@ -221,7 +228,7 @@ export class ArcSegment {
221228
}
222229
}
223230

224-
export class Via {
231+
export class Via implements HasUniqueID {
225232
type: "blind" | "micro" | "through-hole" = "through-hole";
226233
at: At;
227234
size: number;
@@ -232,7 +239,8 @@ export class Via {
232239
locked = false;
233240
free = false;
234241
net: number;
235-
tstamp: string;
242+
tstamp?: string;
243+
uuid?: string;
236244

237245
constructor(expr: Parseable) {
238246
Object.assign(
@@ -251,12 +259,17 @@ export class Via {
251259
P.atom("remove_unused_layers"),
252260
P.atom("keep_end_layers"),
253261
P.pair("tstamp", T.string),
262+
P.pair("uuid", T.string),
254263
),
255264
);
256265
}
266+
267+
get unique_id(): string | undefined {
268+
return this.uuid ?? this.tstamp;
269+
}
257270
}
258271

259-
export class Zone {
272+
export class Zone implements HasUniqueID {
260273
locked = false;
261274
net: number;
262275
net_name: string;
@@ -275,7 +288,8 @@ export class Zone {
275288
fill: ZoneFill;
276289
polygons: Poly[];
277290
filled_polygons: FilledPolygon[];
278-
tstamp: string;
291+
tstamp?: string;
292+
uuid?: string;
279293

280294
constructor(
281295
expr: Parseable,
@@ -317,9 +331,14 @@ export class Zone {
317331
T.item(FilledPolygon),
318332
),
319333
P.pair("tstamp", T.string),
334+
P.pair("uuid", T.string),
320335
),
321336
);
322337
}
338+
339+
get unique_id(): string | undefined {
340+
return this.uuid ?? this.tstamp;
341+
}
323342
}
324343

325344
export class ZoneKeepout {
@@ -601,11 +620,12 @@ export class Net {
601620
}
602621
}
603622

604-
export class Dimension {
623+
export class Dimension implements HasUniqueID {
605624
locked = false;
606625
type: "aligned" | "leader" | "center" | "orthogonal" | "radial";
607626
layer: string;
608-
tstamp: string;
627+
tstamp?: string;
628+
uuid?: string;
609629
pts: Vec2[];
610630
height: number;
611631
orientation: number;
@@ -627,6 +647,7 @@ export class Dimension {
627647
P.pair("type", T.string),
628648
P.pair("layer", T.string),
629649
P.pair("tstamp", T.string),
650+
P.pair("uuid", T.string),
630651
P.list("pts", T.vec2),
631652
P.pair("height", T.number),
632653
P.pair("orientation", T.number),
@@ -649,6 +670,10 @@ export class Dimension {
649670
get end(): Vec2 {
650671
return this.pts.at(-1) ?? new Vec2(0, 0);
651672
}
673+
674+
get unique_id(): string | undefined {
675+
return this.uuid ?? this.tstamp;
676+
}
652677
}
653678

654679
export enum DimensionFormatUnits {
@@ -740,7 +765,7 @@ type FootprintDrawings =
740765
| FpText
741766
| SymbolProperty;
742767

743-
export class Footprint {
768+
export class Footprint implements HasUniqueID {
744769
at: At;
745770
reference: string;
746771
value: string;
@@ -751,12 +776,15 @@ export class Footprint {
751776
placed = false;
752777
layer: string;
753778
tedit: string;
754-
tstamp: string;
779+
tstamp?: string;
780+
uuid?: string;
755781
descr: string;
756782
tags: string;
757783
path: string;
758784
autoplace_cost90: number;
759785
autoplace_cost180: number;
786+
sheetname?: string;
787+
sheetfile?: string;
760788
solder_mask_margin: number;
761789
solder_paste_margin: number;
762790
solder_paste_ratio: number;
@@ -808,6 +836,9 @@ export class Footprint {
808836
P.pair("layer", T.string),
809837
P.pair("tedit", T.string),
810838
P.pair("tstamp", T.string),
839+
P.pair("uuid", T.string),
840+
P.pair("sheetname", T.string),
841+
P.pair("sheetfile", T.string),
811842
P.item("at", At),
812843
P.pair("descr", T.string),
813844
P.pair("tags", T.string),
@@ -878,8 +909,8 @@ export class Footprint {
878909
}
879910
}
880911

881-
get uuid() {
882-
return this.tstamp;
912+
get unique_id(): string | undefined {
913+
return this.uuid ?? this.tstamp;
883914
}
884915

885916
*items(): Generator<FootprintDrawings | Pad | Zone, void, undefined> {
@@ -974,7 +1005,7 @@ export class Footprint {
9741005
}
9751006
}
9761007

977-
export class SymbolProperty {
1008+
export class SymbolProperty implements HasUniqueID {
9781009
has_symbol_prop: boolean;
9791010

9801011
// generic properties
@@ -1029,14 +1060,23 @@ export class SymbolProperty {
10291060
get shown_text() {
10301061
return expand_text_vars(this.value, this.parent);
10311062
}
1063+
1064+
get unique_id(): string | undefined {
1065+
return this.uuid;
1066+
}
10321067
}
10331068

1034-
class GraphicItem {
1069+
class GraphicItem implements HasUniqueID {
10351070
parent?: Footprint;
10361071
layer: string;
1037-
tstamp: string;
1072+
uuid?: string;
1073+
tstamp?: string;
10381074
locked = false;
10391075

1076+
get unique_id(): string | undefined {
1077+
return this.uuid ?? this.tstamp;
1078+
}
1079+
10401080
/**
10411081
* Get the nominal bounding box for the item. This does not include any
10421082
* stroke or other expansion.
@@ -1072,6 +1112,7 @@ export class Line extends GraphicItem {
10721112
P.vec2("start"),
10731113
P.vec2("end"),
10741114
P.pair("width", T.number),
1115+
P.pair("uuid", T.string),
10751116
P.pair("tstamp", T.string),
10761117
P.item("stroke", Stroke),
10771118
),
@@ -1120,6 +1161,7 @@ export class Circle extends GraphicItem {
11201161
P.pair("width", T.number),
11211162
P.pair("fill", T.string),
11221163
P.pair("layer", T.string),
1164+
P.pair("uuid", T.string),
11231165
P.pair("tstamp", T.string),
11241166
P.item("stroke", Stroke),
11251167
),
@@ -1173,6 +1215,7 @@ export class Arc extends GraphicItem {
11731215
P.vec2("end"),
11741216
P.pair("angle", T.number),
11751217
P.pair("width", T.number),
1218+
P.pair("uuid", T.string),
11761219
P.pair("tstamp", T.string),
11771220
P.item("stroke", Stroke),
11781221
);
@@ -1262,6 +1305,7 @@ export class Poly extends GraphicItem {
12621305
P.list("pts", T.vec2),
12631306
P.pair("width", T.number),
12641307
P.pair("fill", T.string),
1308+
P.pair("uuid", T.string),
12651309
P.pair("tstamp", T.string),
12661310
P.item("stroke", Stroke),
12671311
),
@@ -1315,6 +1359,7 @@ export class Rect extends GraphicItem {
13151359
P.pair("layer", T.string),
13161360
P.pair("width", T.number),
13171361
P.pair("fill", T.string),
1362+
P.pair("uuid", T.string),
13181363
P.pair("tstamp", T.string),
13191364
P.item("stroke", Stroke),
13201365
),
@@ -1359,15 +1404,16 @@ export class TextRenderCache {
13591404
}
13601405
}
13611406

1362-
export class Text {
1407+
export class Text implements HasUniqueID {
13631408
parent?: Footprint | Dimension | KicadPCB;
13641409
text: string;
13651410
at: At;
13661411
layer: { name: string; knockout: boolean };
13671412
unlocked = false;
13681413
hide = false;
13691414
effects = new Effects();
1370-
tstamp: string;
1415+
tstamp?: string;
1416+
uuid?: string;
13711417
render_cache: TextRenderCache;
13721418

13731419
static common_expr_defs = [
@@ -1381,13 +1427,18 @@ export class Text {
13811427
P.atom("knockout"),
13821428
),
13831429
P.pair("tstamp", T.string),
1430+
P.pair("uuid", T.string),
13841431
P.item("effects", Effects),
13851432
P.item("render_cache", TextRenderCache),
13861433
];
13871434

13881435
get shown_text() {
13891436
return expand_text_vars(this.text, this.parent);
13901437
}
1438+
1439+
get unique_id(): string | undefined {
1440+
return this.uuid ?? this.tstamp;
1441+
}
13911442
}
13921443

13931444
export class FpText extends Text {
@@ -1436,7 +1487,7 @@ export class GrText extends Text {
14361487
}
14371488
}
14381489

1439-
export class Pad {
1490+
export class Pad implements HasUniqueID {
14401491
number: string; // I hate this
14411492
type: "thru_hole" | "smd" | "connect" | "np_thru_hole" = "thru_hole";
14421493
shape: "circle" | "rect" | "oval" | "trapezoid" | "roundrect" | "custom";
@@ -1467,6 +1518,9 @@ export class Pad {
14671518
net: Net;
14681519
options: PadOptions;
14691520
primitives: (GrLine | GrCircle | GrArc | GrRect | GrPoly)[];
1521+
uuid?: string;
1522+
tstamp?: string;
1523+
remove_unused_layers: boolean = false;
14701524

14711525
constructor(
14721526
expr: Parseable,
@@ -1506,6 +1560,8 @@ export class Pad {
15061560
P.pair("thermal_bridge_angle", T.number),
15071561
P.pair("zone_connect", T.number),
15081562
P.pair("tstamp", T.string),
1563+
P.pair("uuid", T.string),
1564+
P.pair("remove_unused_layers", T.boolean),
15091565
P.item("drill", PadDrill),
15101566
P.item("net", Net),
15111567
P.item("options", PadOptions),
@@ -1525,6 +1581,10 @@ export class Pad {
15251581

15261582
Object.assign(this, parsed);
15271583
}
1584+
1585+
get unique_id(): string | undefined {
1586+
return this.uuid ?? this.tstamp;
1587+
}
15281588
}
15291589

15301590
export class PadDrill {

‎src/kicad/common.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export function unescape_string(str: string): string {
3333
return str;
3434
}
3535

36+
/** Object with a unique ID (UUID or tstamp). */
37+
export interface HasUniqueID {
38+
get unique_id(): string | undefined;
39+
}
40+
3641
export type HasResolveTextVars = {
3742
resolve_text_var: (name: string) => string | undefined;
3843
};

‎src/kicanvas/elements/kc-board/footprints-panel.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export class KCBoardFootprintsPanelElement extends KCUIElement {
5555
// selection changes.
5656
this.addDisposable(
5757
this.viewer.addEventListener(KiCanvasSelectEvent.type, () => {
58-
this.menu.selected = this.viewer.selected?.context.uuid ?? null;
58+
this.menu.selected =
59+
this.viewer.selected?.context.uuid_text ?? null;
5960
}),
6061
);
6162

@@ -98,7 +99,7 @@ export class KCBoardFootprintsPanelElement extends KCUIElement {
9899
const match_text = `${fp.library_link} ${fp.descr} ${fp.layer} ${ref} ${val} ${fp.tags}`;
99100

100101
const entry = html`<kc-ui-menu-item
101-
name="${fp.uuid}"
102+
name="${fp.unique_id}"
102103
data-match-text="${match_text}">
103104
<span class="narrow">${ref}</span><span>${val}</span>
104105
</kc-ui-menu-item>`;

0 commit comments

Comments
 (0)