-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLcscComponent.js
More file actions
174 lines (146 loc) · 5.58 KB
/
LcscComponent.js
File metadata and controls
174 lines (146 loc) · 5.58 KB
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
const {EeSymbol, EeSymbolBbox} = require("./easyeda/parametersEasyeda");
const easyeda_handlers = require("./helpers/easyeda_handlers")
const {ExporterSymbolKicad} = require("./kicad/exportKicadSymbol");
const {EasyedaFootprintImporter, Easyeda3dModelImporter} = require("./easyeda/easyedaImporter");
const {ExporterFootprintKicad} = require("./kicad/exportKicadFootprint");
const {Exporter3dModelKicad} = require("./kicad/exportKicad3dmodel");
const { Ki3dModelBase } = require("./kicad/parametersKicadFootprint");
class LcscComponent {
constructor(lcscId, kicadVersion, footprintLibName) {
if (!lcscId || typeof lcscId !== 'string') {
throw new Error('LCSC ID is required and must be a string');
}
this.lcscId = lcscId;
this.kicadVersion = kicadVersion;
this.footprintLibName = footprintLibName;
this.source = {
cadData: null,
'3dRawObj': null,
'3dStep': null,
};
this.translation = new Ki3dModelBase({
x: 0,
y: 0,
z: 0,
});
}
getId() {
return this.lcscId;
}
getKicadVersion() {
return this.kicadVersion;
}
getFootprintLibName() {
return this.footprintLibName;
}
set3dRawObj(data) {
this.source["3dRawObj"] = data;
}
get3dRawObj(data) {
return this.source["3dRawObj"];
}
set3dStep(data) {
this.source["3dStep"] = data;
}
get3dStep(data) {
return this.source["3dStep"];
}
setCadData(data) {
if (!data || typeof data !== 'object') {
throw new Error('CAD data must be a non-null object');
}
this.source.cadData = data;
return this;
}
getCadData() {
if (!this.source.cadData) {
throw new Error(`No CAD data available for ${this.lcscId}`);
}
return this.source.cadData;
}
get3DModelInfo() {
for (const line of this.getCadData().packageDetail.dataStr.shape) {
const ee_designator = line.split("~")[0];
if (ee_designator === "SVGNODE") {
const raw_json = line.split("~").slice(1)[0];
return JSON.parse(raw_json).attrs;
}
}
return {};
}
async create3dModel() {
const modelImporter = new Easyeda3dModelImporter(this);
const model3d = await modelImporter.create_3d_model();
return model3d;
}
async createEasyedaFootprint() {
return await this.createEasyedaFootprintImporter().extract_easyeda_data(
this.getCadData().packageDetail.dataStr,
this.getCadData().packageDetail.dataStr.head.c_para,
this.getCadData().SMT && !this.getCadData().packageDetail.title.includes("-TH_")
);
}
createEasyedaFootprintImporter() {
return new EasyedaFootprintImporter(this.getCadData());
}
async createExporterFootprintKicad() {
const f = await this.createEasyedaFootprint();
return new ExporterFootprintKicad(f, await this.create3dModel(), this.translation);
}
async createFootprintResult() {
const easyedaFootprint = await this.createEasyedaFootprint();
const footprintExporter = await this.createExporterFootprintKicad();
return {
name: easyedaFootprint.info.name,
content: footprintExporter.getContent() // This should return the content without writing to file
};
}
async create3dModelResult() {
const model3d = await this.create3dModel();
if (model3d) {
const exporter3d = new Exporter3dModelKicad(model3d);
return {
name: exporter3d.output ? exporter3d.output.name : null,
wrlContent: exporter3d.getWrlContent(), // This should return the content without writing to file
stepContent: exporter3d.getStepContent() // This should return the content without writing to file
};
}
}
createSymbolResult() {
return {
name: this.createEeSymbol().info.name,
content: this.createExporterSymbolKicad().export(this.footprintLibName).replace(/[\s\n]+$/, '')
};
}
createExporterSymbolKicad() {
return new ExporterSymbolKicad(this.createEeSymbol(), this.kicadVersion);
}
createEeSymbol() {
const componentParameters = this.getCadData().dataStr.head.c_para;
const new_ee_symbol = new EeSymbol({
info: {
name: componentParameters["name"],
prefix: componentParameters["pre"],
package: componentParameters["package"] || null,
manufacturer: componentParameters["BOM_Manufacturer"] || null,
datasheet: this.getCadData().lcsc ? this.getCadData().lcsc.url : null,
lcsc_id: this.getCadData().lcsc ? this.getCadData().lcsc.number : null,
jlc_id: componentParameters["BOM_JLCPCB Part Class"] || null,
},
bbox: new EeSymbolBbox({
x: parseFloat(this.getCadData().dataStr.head.x),
y: parseFloat(this.getCadData().dataStr.head.y),
}),
});
this.getCadData().dataStr.shape.forEach((line) => {
const designator = line.split("~")[0];
if (easyeda_handlers.hasOwnProperty(designator)) {
easyeda_handlers[designator](line, new_ee_symbol);
} else {
console.warn(`Unknown symbol designator: ${designator}`);
}
});
return new_ee_symbol;
}
}
module.exports = LcscComponent;