Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/geometry/altium-geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export function normalizeAltiumAngle(angle: number): number {
return Object.is(normalized, -0) ? 0 : normalized
}

export function getCcwSweepDegrees(
startAngleDegrees: number,
endAngleDegrees: number,
): number {
return normalizeAltiumAngle(endAngleDegrees - startAngleDegrees) || 360
}

export function altiumPointsEqual(
left: AltiumPoint,
right: AltiumPoint,
Expand Down
33 changes: 33 additions & 0 deletions lib/svg-serialization/approximate-altium-arc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getCcwSweepDegrees } from "../geometry/altium-geometry"
import type { SvgPoint } from "./svg-types"

interface ApproximateAltiumArcOptions {
center: SvgPoint
radius: number
startAngleDegrees: number
endAngleDegrees: number
}

/**
* Samples an Altium arc in its native counterclockwise direction. Altium
* angles wrap through zero, so an arc from 360° to 90° sweeps 90°, not -270°.
*/
export function approximateAltiumArc({
center,
radius,
startAngleDegrees,
endAngleDegrees,
}: ApproximateAltiumArcOptions): SvgPoint[] {
const ccwSweepDegrees = getCcwSweepDegrees(startAngleDegrees, endAngleDegrees)
const segments = Math.max(8, Math.ceil(ccwSweepDegrees / 7.5))

return Array.from({ length: segments + 1 }, (_, index) => {
const angleDegrees =
startAngleDegrees + (ccwSweepDegrees * index) / segments
const radians = (angleDegrees * Math.PI) / 180
return {
x: center.x + Math.cos(radians) * radius,
y: center.y + Math.sin(radians) * radius,
}
})
}
33 changes: 11 additions & 22 deletions lib/svg-serialization/serialize-altium-sheet-to-svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getSchematicCoordinate,
getSchematicIndexedPoints,
} from "./altium-values"
import { approximateAltiumArc } from "./approximate-altium-arc"
import { getSchematicFont } from "./get-schematic-font"
import { getSchematicSheetSize } from "./get-schematic-sheet-size"
import { renderAltiumNegatedText } from "./render-altium-negated-text"
Expand Down Expand Up @@ -44,7 +45,6 @@ interface SchematicPinRenderContext {
sheetRecord: AltiumSchSheetRecord | undefined
viewport: SvgViewport
}

export function serializeAltiumSheetToSvg(
source: AltiumPcbDoc | AltiumSchDoc | AltiumLine[],
options: AltiumSheetSvgOptions = {},
Expand Down Expand Up @@ -217,9 +217,16 @@ function renderSchematicRecord(
if (kind === "11" || kind === "12") {
const center = getSchematicLocation(record)
const radius = getSchematicCoordinate(record, "RADIUS", 1)
const startAngle = Number(record.getCaseInsensitive("STARTANGLE") ?? 0)
const endAngle = Number(record.getCaseInsensitive("ENDANGLE") ?? 360)
const points = approximateSchematicArc(center, radius, startAngle, endAngle)
const startAngleDegrees = Number(
record.getCaseInsensitive("STARTANGLE") ?? 0,
)
const endAngleDegrees = Number(record.getCaseInsensitive("ENDANGLE") ?? 360)
const points = approximateAltiumArc({
center,
radius,
startAngleDegrees,
endAngleDegrees,
})
return `<polyline ${metadata} points="${pointsToSvg(points, viewport)}" fill="none" stroke="${color}" stroke-width="${formatSvgNumber(lineWidth)}"/>`
}

Expand Down Expand Up @@ -1002,21 +1009,3 @@ function getSchematicCornerIfPresent(
y: getSchematicCoordinate(record, "CORNER.Y"),
}
}

function approximateSchematicArc(
center: SvgPoint,
radius: number,
startAngle: number,
endAngle: number,
): SvgPoint[] {
const sweep = endAngle - startAngle || 360
const segments = Math.max(8, Math.ceil(Math.abs(sweep) / 7.5))
return Array.from({ length: segments + 1 }, (_, index) => {
const angle = startAngle + (sweep * index) / segments
const radians = (angle * Math.PI) / 180
return {
x: center.x + Math.cos(radians) * radius,
y: center.y + Math.sin(radians) * radius,
}
})
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading