@tscircuit/circuit-json-util 0.0.132 → 0.0.134

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -192,7 +192,7 @@ var cju = ((circuitJsonInput, options = {}) => {
192
192
  return {
193
193
  get: (id) => circuitJson.find(
194
194
  (e) => e.type === component_type && e[`${component_type}_id`] === id
195
- ),
195
+ ) ?? null,
196
196
  getUsing: (using) => {
197
197
  const keys = Object.keys(using);
198
198
  if (keys.length !== 1) {
@@ -208,13 +208,13 @@ var cju = ((circuitJsonInput, options = {}) => {
208
208
  if (!joiner) return null;
209
209
  return circuitJson.find(
210
210
  (e) => e.type === component_type && e[`${component_type}_id`] === joiner[`${component_type}_id`]
211
- );
211
+ ) ?? null;
212
212
  },
213
213
  getWhere: (where) => {
214
214
  const keys = Object.keys(where);
215
215
  return circuitJson.find(
216
216
  (e) => e.type === component_type && keys.every((key) => e[key] === where[key])
217
- );
217
+ ) ?? null;
218
218
  },
219
219
  list: (where) => {
220
220
  const keys = !where ? [] : Object.keys(where);
@@ -260,7 +260,7 @@ var cju = ((circuitJsonInput, options = {}) => {
260
260
  if (component_type === "source_component") {
261
261
  return circuitJson.find(
262
262
  (e) => e.type === "source_component" && e.name === selector.replace(/\./g, "")
263
- );
263
+ ) ?? null;
264
264
  } else if (component_type === "pcb_port" || component_type === "source_port" || component_type === "schematic_port") {
265
265
  const [component_name, port_selector] = selector.replace(/\./g, "").split(/[\s\>]+/);
266
266
  const source_component = circuitJson.find(
@@ -275,13 +275,14 @@ var cju = ((circuitJsonInput, options = {}) => {
275
275
  if (component_type === "pcb_port") {
276
276
  return circuitJson.find(
277
277
  (e) => e.type === "pcb_port" && e.source_port_id === source_port.source_port_id
278
- );
278
+ ) ?? null;
279
279
  } else if (component_type === "schematic_port") {
280
280
  return circuitJson.find(
281
281
  (e) => e.type === "schematic_port" && e.source_port_id === source_port.source_port_id
282
- );
282
+ ) ?? null;
283
283
  }
284
284
  }
285
+ return null;
285
286
  }
286
287
  };
287
288
  }
@@ -3289,6 +3290,206 @@ var getTraceSegmentPolygon = (start, end, startWidth, endWidth = startWidth) =>
3289
3290
  );
3290
3291
  };
3291
3292
  var copperPolygonsTouch = (a, b, tolerance = 1e-7) => a.vertices.some((p) => b.contains(p)) || b.vertices.some((p) => a.contains(p)) || a.distanceTo(b)[0] <= tolerance;
3293
+
3294
+ // lib/convert-schematic-length-unit.ts
3295
+ import {
3296
+ LEGACY_SCHEMATIC_LENGTH_UNIT,
3297
+ MM_PER_SCHEMATIC_UNIT
3298
+ } from "circuit-json";
3299
+ var millimetersPerUnit = {
3300
+ mm: 1,
3301
+ in: 25.4,
3302
+ [LEGACY_SCHEMATIC_LENGTH_UNIT]: MM_PER_SCHEMATIC_UNIT
3303
+ };
3304
+ function getSchematicLengthUnitScale(from, to) {
3305
+ return millimetersPerUnit[from] / millimetersPerUnit[to];
3306
+ }
3307
+ function convertSchematicLengthUnit(element, unit) {
3308
+ const from = "schematic_length_unit" in element ? element.schematic_length_unit : void 0;
3309
+ if (unit === from) return element;
3310
+ const factor = getSchematicLengthUnitScale(
3311
+ from ?? LEGACY_SCHEMATIC_LENGTH_UNIT,
3312
+ unit
3313
+ );
3314
+ const point = (p) => ({
3315
+ ...p,
3316
+ x: p.x * factor,
3317
+ y: p.y * factor
3318
+ });
3319
+ const lengths = (obj, ...keys) => {
3320
+ const result = { ...obj };
3321
+ for (const key of keys) {
3322
+ const value = obj[key];
3323
+ if (typeof value === "number")
3324
+ Object.assign(result, { [key]: value * factor });
3325
+ }
3326
+ return result;
3327
+ };
3328
+ const tag = { schematic_length_unit: unit };
3329
+ switch (element.type) {
3330
+ case "schematic_sheet":
3331
+ return {
3332
+ ...element,
3333
+ ...tag,
3334
+ ...element.center && { center: point(element.center) }
3335
+ };
3336
+ case "schematic_net_label":
3337
+ return {
3338
+ ...element,
3339
+ ...tag,
3340
+ center: point(element.center),
3341
+ ...element.anchor_position && {
3342
+ anchor_position: point(element.anchor_position)
3343
+ }
3344
+ };
3345
+ case "schematic_port":
3346
+ return {
3347
+ ...lengths(
3348
+ element,
3349
+ "display_pin_label_font_size",
3350
+ "distance_from_component_edge"
3351
+ ),
3352
+ ...tag,
3353
+ center: point(element.center),
3354
+ ...element.boundary_position && {
3355
+ boundary_position: point(element.boundary_position)
3356
+ }
3357
+ };
3358
+ case "schematic_component":
3359
+ return {
3360
+ ...lengths(element, "pin_spacing", "box_width"),
3361
+ ...tag,
3362
+ center: point(element.center),
3363
+ size: lengths(element.size, "width", "height"),
3364
+ ...element.pin_styles && {
3365
+ pin_styles: Object.fromEntries(
3366
+ Object.entries(element.pin_styles).map(([pin, style]) => [
3367
+ pin,
3368
+ lengths(
3369
+ style,
3370
+ "left_margin",
3371
+ "right_margin",
3372
+ "top_margin",
3373
+ "bottom_margin"
3374
+ )
3375
+ ])
3376
+ )
3377
+ }
3378
+ };
3379
+ case "schematic_text":
3380
+ return {
3381
+ ...lengths(element, "font_size"),
3382
+ ...tag,
3383
+ position: point(element.position)
3384
+ };
3385
+ case "schematic_voltage_probe":
3386
+ return { ...element, ...tag, position: point(element.position) };
3387
+ case "schematic_debug_object":
3388
+ if (element.shape === "line")
3389
+ return {
3390
+ ...element,
3391
+ ...tag,
3392
+ start: point(element.start),
3393
+ end: point(element.end)
3394
+ };
3395
+ if (element.shape === "rect")
3396
+ return {
3397
+ ...element,
3398
+ ...tag,
3399
+ center: point(element.center),
3400
+ size: lengths(element.size, "height", "width")
3401
+ };
3402
+ return { ...element, ...tag, center: point(element.center) };
3403
+ case "schematic_trace":
3404
+ return {
3405
+ ...element,
3406
+ ...tag,
3407
+ edges: element.edges.map((edge) => ({
3408
+ ...edge,
3409
+ from: point(edge.from),
3410
+ to: point(edge.to)
3411
+ })),
3412
+ junctions: element.junctions.map(point)
3413
+ };
3414
+ case "schematic_table":
3415
+ return {
3416
+ ...lengths(element, "cell_padding", "border_width"),
3417
+ ...tag,
3418
+ anchor_position: point(element.anchor_position),
3419
+ column_widths: element.column_widths.map((width) => width * factor),
3420
+ row_heights: element.row_heights.map((height) => height * factor)
3421
+ };
3422
+ case "schematic_table_cell":
3423
+ return {
3424
+ ...lengths(element, "width", "height", "font_size"),
3425
+ ...tag,
3426
+ center: point(element.center)
3427
+ };
3428
+ case "schematic_rect":
3429
+ return {
3430
+ ...lengths(element, "width", "height", "stroke_width"),
3431
+ ...tag,
3432
+ center: point(element.center)
3433
+ };
3434
+ case "schematic_group":
3435
+ return {
3436
+ ...lengths(element, "width", "height"),
3437
+ ...tag,
3438
+ center: point(element.center)
3439
+ };
3440
+ case "schematic_arc":
3441
+ case "schematic_circle":
3442
+ return {
3443
+ ...lengths(element, "radius", "stroke_width"),
3444
+ ...tag,
3445
+ center: point(element.center)
3446
+ };
3447
+ case "schematic_box":
3448
+ return { ...lengths(element, "x", "y", "width", "height"), ...tag };
3449
+ case "schematic_graphic":
3450
+ return { ...lengths(element, "width", "height"), ...tag };
3451
+ case "schematic_line":
3452
+ return {
3453
+ ...lengths(
3454
+ element,
3455
+ "x1",
3456
+ "x2",
3457
+ "y1",
3458
+ "y2",
3459
+ "stroke_width",
3460
+ "dash_length",
3461
+ "dash_gap"
3462
+ ),
3463
+ ...tag
3464
+ };
3465
+ case "schematic_path":
3466
+ return {
3467
+ ...lengths(element, "stroke_width", "dash_length", "dash_gap"),
3468
+ ...tag,
3469
+ points: element.points.map(point)
3470
+ };
3471
+ case "source_failed_to_create_component_error":
3472
+ return {
3473
+ ...element,
3474
+ ...tag,
3475
+ ...element.schematic_center && {
3476
+ schematic_center: lengths(element.schematic_center, "x", "y")
3477
+ }
3478
+ };
3479
+ default:
3480
+ return element;
3481
+ }
3482
+ }
3483
+ function convertSchematicLengthUnits(circuit, unit) {
3484
+ if (unit === LEGACY_SCHEMATIC_LENGTH_UNIT) {
3485
+ const needsConversion = (element) => "schematic_length_unit" in element && element.schematic_length_unit !== void 0 && element.schematic_length_unit !== unit;
3486
+ if (!circuit.some(needsConversion)) return circuit;
3487
+ return circuit.map(
3488
+ (element) => needsConversion(element) ? convertSchematicLengthUnit(element, unit) : element
3489
+ );
3490
+ }
3491
+ return circuit.map((element) => convertSchematicLengthUnit(element, unit));
3492
+ }
3292
3493
  export {
3293
3494
  analyzePcbPin1Location,
3294
3495
  applySelector,
@@ -3300,6 +3501,8 @@ export {
3300
3501
  cju_indexed_default as cjuIndexed,
3301
3502
  computeClearanceBetweenElements,
3302
3503
  computeGapBetweenCopper,
3504
+ convertSchematicLengthUnit,
3505
+ convertSchematicLengthUnits,
3303
3506
  copperPolygonsTouch,
3304
3507
  createBoardOwnerMap,
3305
3508
  directionToVec,
@@ -3325,6 +3528,7 @@ export {
3325
3528
  getReadableNameForPcbSmtpad,
3326
3529
  getReadableNameForPcbTrace,
3327
3530
  getSchematicElementBounds,
3531
+ getSchematicLengthUnitScale,
3328
3532
  getSmtPadPolygon,
3329
3533
  getStringFromCircuitJsonTree,
3330
3534
  getTraceSegmentPolygon,