Skip to content

Commit

Permalink
Merge pull request #64 from tscircuit/repro-pt-issue
Browse files Browse the repository at this point in the history
Fix PT issue
  • Loading branch information
seveibar authored Oct 14, 2024
2 parents 89d2707 + 0e5d0a3 commit 37f48a0
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/schemas/single-letter-shape-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,33 @@ export const PolygonShapeSchema = z
.transform(parsePolygon)
.pipe(PolygonShapeOutputSchema)

const PathShapeOutputSchema = z.object({
type: z.literal("PATH"),
pathData: z.string(),
fillColor: z.string(),
strokeWidth: z.number(),
strokeColor: z.string(),
id: z.string(),
})

const parsePath = (str: string): z.infer<typeof PathShapeOutputSchema> => {
const [, pathData, fillColor, strokeWidth, strokeColor, , id] = str.split("~")
return {
type: "PATH",
pathData,
fillColor,
strokeWidth: Number(strokeWidth),
strokeColor,
id,
}
}

export const PathShapeSchema = z
.string()
.startsWith("PT~")
.transform(parsePath)
.pipe(PathShapeOutputSchema)

export const SingleLetterShapeSchema = z
.string()
.transform((x) => {
Expand All @@ -266,6 +293,7 @@ export const SingleLetterShapeSchema = z
if (x.startsWith("P~")) return PinShapeSchema.parse(x)
if (x.startsWith("PL~")) return PolylineShapeSchema.parse(x)
if (x.startsWith("PG~")) return PolygonShapeSchema.parse(x)
if (x.startsWith("PT~")) return PathShapeSchema.parse(x)
throw new Error(`Invalid shape type: ${x}`)
})
.pipe(
Expand All @@ -275,6 +303,7 @@ export const SingleLetterShapeSchema = z
PinShapeOutputSchema,
PolylineShapeOutputSchema,
PolygonShapeOutputSchema,
PathShapeOutputSchema,
]),
)

Expand Down
217 changes: 217 additions & 0 deletions tests/assets/C57759.raweasy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
{
"uuid": "adc27cf35c9bb9de5dceabaac876b68a",
"title": "1N4148WS-C57759",
"description": "1N4148WS",
"docType": 2,
"type": 3,
"thumb": "//image.easyeda.com/components/adc27cf35c9bb9de5dceabaac876b68a.png",
"szlcsc": {
"id": 58795,
"number": "C57759",
"step": 50,
"min": 50,
"price": 0.066682,
"stock": 2800,
"url": "http://www.szlcsc.com/product/details_58795.html",
"image": "//image.lceda.cn/szlcsc/C57759.jpg"
},
"lcsc": {
"id": 58795,
"number": "C57759",
"step": 50,
"min": 50,
"price": 0.0122,
"stock": 475700,
"url": "https://lcsc.com/product-detail/Switching-Diode_1N4148WS_C57759.html"
},
"owner": {
"uuid": "0819f05c4eef4c71ace90d822a990e87",
"username": "LCSC",
"nickname": "LCSC",
"avatar": "//image.lceda.cn/avatars/2018/6/kFlrasi7W06gTdBLAqW3fkrqbDhbowynuSzkjqso.png"
},
"tags": ["Switching Diode"],
"updateTime": 1702434011,
"updated_at": "2024-10-12 13:06:43",
"dataStr": {
"head": {
"docType": "2",
"editorVersion": "6.5.9",
"c_para": {
"pre": "D?",
"name": "1N4148WS-C57759",
"package": "SOD-323_L1.8-W1.3-LS2.5-RD-1",
"Supplier": "LCSC",
"Manufacturer": "ST(先科)",
"Manufacturer Part": "1N4148WS",
"Supplier Part": "C57759",
"JLCPCB Part Class": "Extended Part"
},
"x": 0,
"y": 0,
"importFlag": 0,
"c_spiceCmd": null,
"puuid": "86deff21ccc74acc9fb12d14cadbb741",
"uuid": "adc27cf35c9bb9de5dceabaac876b68a",
"utime": 1660018609,
"hasIdFlag": true
},
"canvas": "CA~1000~1000~#FFFFFF~yes~#CCCCCC~5~1000~1000~line~5~pixel~5~0~0",
"shape": [
"PL~-5 6 -5 -6~#880000~1~0~none~gge13~0",
"P~show~0~2~20~0~0~gge14~0^^20~0^^M 20 0 h -15~#880000^^0~2~3~0~A~end~~~#0000FF^^0~11~-1~0~2~start~~~#0000FF^^0~8~0^^0~M 5 -3 L 2 0 L 5 3",
"P~show~0~1~-20~0~180~gge22~0^^-20~0^^M -20 0 h 15~#880000^^0~-2~3~0~C~start~~~#0000FF^^0~-11~-1~0~1~end~~~#0000FF^^0~-8~0^^0~M -5 3 L -2 0 L -5 -3",
"PT~M 5 -6 L -5 0 L 5 6 Z~#880000~1~0~#880000~gge29~0~"
],
"BBox": {
"x": -21,
"y": -6,
"width": 42,
"height": 12
},
"colors": []
},
"verify": true,
"SMT": true,
"datastrid": "d58d9d386bdd488caee1dab3f50168f5",
"jlcOnSale": 1,
"writable": false,
"isFavorite": false,
"packageDetail": {
"uuid": "86deff21ccc74acc9fb12d14cadbb741",
"title": "SOD-323_L1.8-W1.3-LS2.5-RD-1",
"docType": 4,
"updateTime": 1659187526,
"owner": {
"uuid": "0819f05c4eef4c71ace90d822a990e87",
"username": "lcsc",
"nickname": "LCSC",
"avatar": "//image.lceda.cn/avatars/2018/6/kFlrasi7W06gTdBLAqW3fkrqbDhbowynuSzkjqso.png"
},
"datastrid": "9919fb845a1e48bba09e426940af8e0d",
"writable": false,
"dataStr": {
"head": {
"docType": "4",
"editorVersion": "6.4.25",
"newgId": true,
"c_para": {
"package": "SOD-323_L1.8-W1.3-LS2.5-RD-1",
"pre": "D?",
"Contributor": "lcsc",
"link": "https://atta.szlcsc.com/upload/public/pdf/source/20190515/C391443_7283481C4C5430844F618E58AEAF13DC.pdf",
"3DModel": "SOD-323_L1.8-W1.3-LS2.5-RD-1"
},
"x": 3999.8835,
"y": 3000,
"hasIdFlag": true,
"utime": 1632810502,
"uuid": "86deff21ccc74acc9fb12d14cadbb741",
"importFlag": 0,
"transformList": "",
"uuid_3d": "8c233131b2ee4434a1a6c8954cf67cd3"
},
"canvas": "CA~1000~1000~#000000~yes~#FFFFFF~10~1000~1000~line~0.5~mm~1~45~visible~0.5~3999.8835~3000~0~none",
"shape": [
"CIRCLE~3995.061~3002.559~0.118~0.2362~101~gge1116~0~~circle_gge1117,circle_gge1118",
"SOLIDREGION~12~~M 4004.1474 2997.9134 L 4004.1474 3002.0078 L 4003.5332 3002.0078 L 4003.5332 2997.9134 Z ~solid~gge1113~~~~0",
"SOLIDREGION~12~~M 4005.0607 2999.6457 L 4005.0607 3000.2599 L 4002.604 3000.2599 L 4002.604 2999.6457 Z ~solid~gge1114~~~~0",
"SOLIDREGION~12~~M 3995.0608 2999.7244 L 3995.0608 3000.3386 L 3997.5175 3000.3386 L 3997.5175 2999.7244 Z ~solid~gge1115~~~~0",
"SOLIDREGION~100~~M 4004.9033 2999.3701 L 4004.9033 3000.6299 L 4003.0331 3000.6299 L 4003.0331 2999.3701 Z ~solid~gge1099~~~~0",
"SOLIDREGION~100~~M 3994.8637 3000.6299 L 3994.8637 2999.3701 L 3996.7339 2999.3701 L 3996.7339 3000.6299 Z ~solid~gge1120~~~~0",
"SOLIDREGION~99~~M 4003.2299 2997.441 L 4003.2299 3002.5591 L 3996.537 3002.5591 L 3996.537 2997.441 Z ~solid~gge1112~~~~0",
"PAD~RECT~3995.267~3000~3.937~2.9528~1~~1~0~3993.2988 2998.5236 3997.2359 2998.5236 3997.2359 3001.4764 3993.2988 3001.4764~0~gge1103~0~~Y~0~0.0000~0.2000~3995.2672,3000",
"PAD~RECT~4004.5~3000~3.937~2.9528~1~~2~0~4002.5313 2998.5236 4006.4684 2998.5236 4006.4684 3001.4764 4002.5313 3001.4764~0~gge1104~0~~Y~0~0.0000~0.2000~4004.5,3000",
"TRACK~0.5984~3~~4003.4317 3002.8591 4003.4317 3002.0472~gge1111~0",
"TRACK~0.5984~3~~4003.4317 2997.1409 4003.4317 2997.9134~gge1110~0",
"TRACK~0.6~3~~3996.5323 2997.1409 4003.4317 2997.1409~gge1109~0",
"TRACK~0.6~3~~3996.5323 3002.8591 4003.4317 3002.8591~gge1108~0",
"TRACK~0.6~3~~3998.1246 2997.1409 3998.1246 3002.8591~gge1107~0",
"SOLIDREGION~3~~M 3996.8324 3003.1591 L 3996.8324 3001.7717 L 3995.6324 3001.7717 L 3995.6324 3003.1591 Z ~solid~gge1105~~~~0",
"SOLIDREGION~3~~M 3996.8324 2996.8409 L 3996.8324 2998.2283 L 3995.6324 2998.2283 L 3995.6324 2996.8409 Z ~solid~gge1106~~~~0",
"SVGNODE~{\"gId\":\"g1_outline\",\"nodeName\":\"g\",\"nodeType\":1,\"layerid\":\"19\",\"attrs\":{\"c_width\":\"9.8425\",\"c_height\":\"5.080651256\",\"c_rotation\":\"0,0,0\",\"z\":\"0\",\"c_origin\":\"3999.8835,3000\",\"uuid\":\"973acf8a660c48b1975f1ba1c890421a\",\"c_etype\":\"outline3D\",\"id\":\"g1_outline\",\"title\":\"SOD-323_L1.8-W1.3-LS2.5-RD-1\",\"layerid\":\"19\",\"transform\":\"scale(1) translate(0, 0)\"},\"childNodes\":[{\"gId\":\"g1_outline_line0\",\"nodeName\":\"polyline\",\"nodeType\":1,\"attrs\":{\"fill\":\"none\",\"id\":\"g1_outline_line0\",\"c_shapetype\":\"line\",\"points\":\"3994.9622 2999.4095 3994.9622 3000.5906 3996.3402 3000.5906 3996.3402 3002.3623 3996.3796 3002.4017 3996.3796 3002.441 3996.4189 3002.4804 3996.4583 3002.5198 3996.4977 3002.5198 3996.537 3002.5591 4003.2299 3002.5591 4003.2693 3002.5198 4003.3087 3002.5198 4003.3481 3002.4804 4003.3874 3002.441 4003.3874 3002.4017 4003.4268 3002.3623 4003.4268 3000.5906 4004.8047 3000.5906 4004.8047 2999.4095 4003.4268 2999.4095 4003.4268 2998.504 4003.4268 2997.6379 4003.3874 2997.5985 4003.3874 2997.5592 4003.3481 2997.5198 4003.3087 2997.4804 4003.2693 2997.4804 4003.2299 2997.441 3996.537 2997.441 3996.4977 2997.4804 3996.4583 2997.4804 3996.4189 2997.5198 3996.3796 2997.5592 3996.3796 2997.5985 3996.3402 2997.6379 3996.3402 2999.4095 3994.9622 2999.4095 3994.9622 2999.4095\"}}]}"
],
"layers": [
"1~TopLayer~#FF0000~false~false~true~",
"2~BottomLayer~#0000FF~false~false~true~",
"3~TopSilkLayer~#FFCC00~false~true~true~",
"4~BottomSilkLayer~#66CC33~false~false~true~",
"5~TopPasteMaskLayer~#808080~false~false~true~",
"6~BottomPasteMaskLayer~#800000~false~false~true~",
"7~TopSolderMaskLayer~#800080~false~false~true~0.3",
"8~BottomSolderMaskLayer~#AA00FF~false~false~true~0.3",
"9~Ratlines~#6464FF~false~false~true~",
"10~BoardOutLine~#FF00FF~false~false~true~",
"11~Multi-Layer~#C0C0C0~true~false~true~",
"12~Document~#FFFFFF~false~false~true~",
"13~TopAssembly~#33CC99~false~false~true~",
"14~BottomAssembly~#5555FF~false~false~true~",
"15~Mechanical~#F022F0~false~false~true~",
"19~3DModel~#66CCFF~false~false~true~",
"21~Inner1~#999966~false~false~false~~",
"22~Inner2~#008000~false~false~false~~",
"23~Inner3~#00FF00~false~false~false~~",
"24~Inner4~#BC8E00~false~false~false~~",
"25~Inner5~#70DBFA~false~false~false~~",
"26~Inner6~#00CC66~false~false~false~~",
"27~Inner7~#9966FF~false~false~false~~",
"28~Inner8~#800080~false~false~false~~",
"29~Inner9~#008080~false~false~false~~",
"30~Inner10~#15935F~false~false~false~~",
"31~Inner11~#000080~false~false~false~~",
"32~Inner12~#00B400~false~false~false~~",
"33~Inner13~#2E4756~false~false~false~~",
"34~Inner14~#99842F~false~false~false~~",
"35~Inner15~#FFFFAA~false~false~false~~",
"36~Inner16~#99842F~false~false~false~~",
"37~Inner17~#2E4756~false~false~false~~",
"38~Inner18~#3535FF~false~false~false~~",
"39~Inner19~#8000BC~false~false~false~~",
"40~Inner20~#43AE5F~false~false~false~~",
"41~Inner21~#C3ECCE~false~false~false~~",
"42~Inner22~#728978~false~false~false~~",
"43~Inner23~#39503F~false~false~false~~",
"44~Inner24~#0C715D~false~false~false~~",
"45~Inner25~#5A8A80~false~false~false~~",
"46~Inner26~#2B937E~false~false~false~~",
"47~Inner27~#23999D~false~false~false~~",
"48~Inner28~#45B4E3~false~false~false~~",
"49~Inner29~#215DA1~false~false~false~~",
"50~Inner30~#4564D7~false~false~false~~",
"51~Inner31~#6969E9~false~false~false~~",
"52~Inner32~#9069E9~false~false~false~~",
"99~ComponentShapeLayer~#00CCCC~true~false~true~0.4",
"100~LeadShapeLayer~#CC9999~true~false~true~",
"101~ComponentMarkingLayer~#66FFCC~false~false~true~",
"Hole~Hole~#222222~false~false~true~",
"DRCError~DRCError~#FAD609~false~false~true~"
],
"objects": [
"All~true~false",
"Component~true~true",
"Prefix~true~true",
"Name~true~false",
"Track~true~true",
"Pad~true~true",
"Via~true~true",
"Hole~true~true",
"Copper_Area~true~true",
"Circle~true~true",
"Arc~true~true",
"Solid_Region~true~true",
"Text~true~true",
"Image~true~true",
"Rect~true~true",
"Dimension~true~true",
"Protractor~true~true"
],
"BBox": {
"x": 3994.9,
"y": 2997.4,
"width": 10,
"height": 5.1
},
"netColors": []
}
}
}
13 changes: 13 additions & 0 deletions tests/convert-to-soup-tests/__snapshots__/c57759.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions tests/convert-to-soup-tests/c57759.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import c57759 from "tests/assets/C57759.raweasy.json"
import { expect, test } from "bun:test"
import { convertEasyEdaJsonToCircuitJson, EasyEdaJsonSchema } from "lib/index"
import { convertCircuitJsonToPcbSvg } from "circuit-to-svg"

test("C57759 should generate Circuit Json without errors", () => {
const bettereasy = EasyEdaJsonSchema.parse(c57759)
const circuitJson = convertEasyEdaJsonToCircuitJson(bettereasy)

expect(convertCircuitJsonToPcbSvg(circuitJson)).toMatchSvgSnapshot(
import.meta.path,
)
})

// Add a new test to specifically check the parsing of the PT shape
test("C57759 should parse PT shape correctly", () => {
const bettereasy = EasyEdaJsonSchema.parse(c57759)
const ptShape = bettereasy.dataStr.shape.find(
(shape) => shape.type === "PATH",
)
expect(ptShape).toBeDefined()
expect(ptShape?.type).toBe("PATH")
expect(ptShape?.pathData).toBe("M 5 -6 L -5 0 L 5 6 Z")
})

0 comments on commit 37f48a0

Please sign in to comment.