|  | 
|  | 1 | +import { SxClass } from "../base-classes/SxClass" | 
|  | 2 | +import type { PrimitiveSExpr } from "../parseToPrimitiveSExpr" | 
|  | 3 | +import { Pts } from "./Pts" | 
|  | 4 | +import { Stroke } from "./Stroke" | 
|  | 5 | +import { Uuid } from "./Uuid" | 
|  | 6 | + | 
|  | 7 | +const SUPPORTED_TOKENS_KICAD_SCH = new Set(["pts", "stroke", "uuid"]) | 
|  | 8 | +const SUPPORTED_TOKENS_SYMBOL = new Set(["pts", "stroke", "fill"]) | 
|  | 9 | + | 
|  | 10 | +// Forward declaration for SymbolPolylineFill to avoid circular dependency | 
|  | 11 | +export interface SymbolPolylineFillLike extends SxClass { | 
|  | 12 | +  type?: string | 
|  | 13 | +} | 
|  | 14 | + | 
|  | 15 | +export interface PolylineConstructorParams { | 
|  | 16 | +  points?: Pts | 
|  | 17 | +  stroke?: Stroke | 
|  | 18 | +  uuid?: string | Uuid | 
|  | 19 | +  fill?: SymbolPolylineFillLike | 
|  | 20 | +} | 
|  | 21 | + | 
|  | 22 | +/** | 
|  | 23 | + * Base Polyline class that can be used in both kicad_sch and symbol contexts. | 
|  | 24 | + * - When parent is "kicad_sch": supports pts, stroke, uuid | 
|  | 25 | + * - When parent is "symbol": supports pts, stroke, fill | 
|  | 26 | + */ | 
|  | 27 | +export class Polyline extends SxClass { | 
|  | 28 | +  static override token = "polyline" | 
|  | 29 | +  override token = "polyline" | 
|  | 30 | + | 
|  | 31 | +  private _sxPts?: Pts | 
|  | 32 | +  private _sxStroke?: Stroke | 
|  | 33 | +  private _sxUuid?: Uuid | 
|  | 34 | +  private _sxFill?: SymbolPolylineFillLike | 
|  | 35 | + | 
|  | 36 | +  constructor(params: PolylineConstructorParams = {}) { | 
|  | 37 | +    super() | 
|  | 38 | + | 
|  | 39 | +    if (params.points !== undefined) { | 
|  | 40 | +      this.points = params.points | 
|  | 41 | +    } | 
|  | 42 | + | 
|  | 43 | +    if (params.stroke !== undefined) { | 
|  | 44 | +      this.stroke = params.stroke | 
|  | 45 | +    } | 
|  | 46 | + | 
|  | 47 | +    if (params.uuid !== undefined) { | 
|  | 48 | +      this.uuid = params.uuid | 
|  | 49 | +    } | 
|  | 50 | + | 
|  | 51 | +    if (params.fill !== undefined) { | 
|  | 52 | +      this.fill = params.fill | 
|  | 53 | +    } | 
|  | 54 | +  } | 
|  | 55 | + | 
|  | 56 | +  static override fromSexprPrimitives( | 
|  | 57 | +    primitiveSexprs: PrimitiveSExpr[], | 
|  | 58 | +  ): Polyline { | 
|  | 59 | +    const polyline = new Polyline() | 
|  | 60 | + | 
|  | 61 | +    const { propertyMap, arrayPropertyMap } = | 
|  | 62 | +      SxClass.parsePrimitivesToClassProperties(primitiveSexprs, this.token) | 
|  | 63 | + | 
|  | 64 | +    // Determine which tokens are supported based on parent context | 
|  | 65 | +    // Check the static parentToken property of the class being instantiated | 
|  | 66 | +    const supportedTokens = | 
|  | 67 | +      this.parentToken === "symbol" | 
|  | 68 | +        ? SUPPORTED_TOKENS_SYMBOL | 
|  | 69 | +        : SUPPORTED_TOKENS_KICAD_SCH | 
|  | 70 | + | 
|  | 71 | +    for (const [token, entries] of Object.entries(arrayPropertyMap)) { | 
|  | 72 | +      if (!supportedTokens.has(token)) { | 
|  | 73 | +        throw new Error( | 
|  | 74 | +          `Unsupported child tokens inside polyline expression: ${token}`, | 
|  | 75 | +        ) | 
|  | 76 | +      } | 
|  | 77 | +      if (entries.length > 1) { | 
|  | 78 | +        throw new Error( | 
|  | 79 | +          `polyline does not support repeated child tokens: ${token}`, | 
|  | 80 | +        ) | 
|  | 81 | +      } | 
|  | 82 | +    } | 
|  | 83 | + | 
|  | 84 | +    const unsupportedTokens = Object.keys(propertyMap).filter( | 
|  | 85 | +      (token) => !supportedTokens.has(token), | 
|  | 86 | +    ) | 
|  | 87 | +    if (unsupportedTokens.length > 0) { | 
|  | 88 | +      throw new Error( | 
|  | 89 | +        `Unsupported child tokens inside polyline expression: ${unsupportedTokens.join(", ")}`, | 
|  | 90 | +      ) | 
|  | 91 | +    } | 
|  | 92 | + | 
|  | 93 | +    polyline._sxPts = | 
|  | 94 | +      (arrayPropertyMap.pts?.[0] as Pts | undefined) ?? | 
|  | 95 | +      (propertyMap.pts as Pts | undefined) | 
|  | 96 | +    polyline._sxStroke = propertyMap.stroke as Stroke | undefined | 
|  | 97 | +    polyline._sxUuid = propertyMap.uuid as Uuid | undefined | 
|  | 98 | +    polyline._sxFill = propertyMap.fill | 
|  | 99 | + | 
|  | 100 | +    return polyline | 
|  | 101 | +  } | 
|  | 102 | + | 
|  | 103 | +  get points(): Pts | undefined { | 
|  | 104 | +    return this._sxPts | 
|  | 105 | +  } | 
|  | 106 | + | 
|  | 107 | +  set points(value: Pts | undefined) { | 
|  | 108 | +    this._sxPts = value | 
|  | 109 | +  } | 
|  | 110 | + | 
|  | 111 | +  get stroke(): Stroke | undefined { | 
|  | 112 | +    return this._sxStroke | 
|  | 113 | +  } | 
|  | 114 | + | 
|  | 115 | +  set stroke(value: Stroke | undefined) { | 
|  | 116 | +    this._sxStroke = value | 
|  | 117 | +  } | 
|  | 118 | + | 
|  | 119 | +  get uuid(): Uuid | undefined { | 
|  | 120 | +    return this._sxUuid | 
|  | 121 | +  } | 
|  | 122 | + | 
|  | 123 | +  set uuid(value: Uuid | string | undefined) { | 
|  | 124 | +    if (value === undefined) { | 
|  | 125 | +      this._sxUuid = undefined | 
|  | 126 | +      return | 
|  | 127 | +    } | 
|  | 128 | +    this._sxUuid = value instanceof Uuid ? value : new Uuid(value) | 
|  | 129 | +  } | 
|  | 130 | + | 
|  | 131 | +  get fill(): SymbolPolylineFillLike | undefined { | 
|  | 132 | +    return this._sxFill | 
|  | 133 | +  } | 
|  | 134 | + | 
|  | 135 | +  set fill(value: SymbolPolylineFillLike | undefined) { | 
|  | 136 | +    this._sxFill = value | 
|  | 137 | +  } | 
|  | 138 | + | 
|  | 139 | +  override getChildren(): SxClass[] { | 
|  | 140 | +    const children: SxClass[] = [] | 
|  | 141 | +    if (this._sxPts) children.push(this._sxPts) | 
|  | 142 | +    if (this._sxStroke) children.push(this._sxStroke) | 
|  | 143 | +    if (this._sxFill) children.push(this._sxFill) | 
|  | 144 | +    if (this._sxUuid) children.push(this._sxUuid) | 
|  | 145 | +    return children | 
|  | 146 | +  } | 
|  | 147 | +} | 
|  | 148 | + | 
|  | 149 | +// Register for both kicad_sch and symbol parents | 
|  | 150 | +SxClass.register(Polyline) | 
|  | 151 | + | 
|  | 152 | +// Create a class that explicitly sets parentToken for kicad_sch | 
|  | 153 | +export class SchematicPolyline extends Polyline { | 
|  | 154 | +  static override parentToken = "kicad_sch" | 
|  | 155 | +} | 
|  | 156 | +SxClass.register(SchematicPolyline) | 
|  | 157 | + | 
|  | 158 | +// Create a class that explicitly sets parentToken for symbol | 
|  | 159 | +export class SymbolPolyline extends Polyline { | 
|  | 160 | +  static override parentToken = "symbol" | 
|  | 161 | +} | 
|  | 162 | +SxClass.register(SymbolPolyline) | 
0 commit comments