Skip to content

Commit

Permalink
make sure silkscreentext flips properly
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Sep 22, 2024
1 parent 23f4106 commit b5f1a5a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
34 changes: 31 additions & 3 deletions lib/components/base-components/PrimitiveComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "transformation-matrix"
import { isMatchingSelector } from "lib/utils/selector-matching"
import type { LayoutBuilder } from "@tscircuit/layout"
import type { LayerRef } from "circuit-json"

export interface BaseComponentConfig {
schematicSymbolName?: BaseSymbolName | null
Expand Down Expand Up @@ -157,19 +158,22 @@ export abstract class PrimitiveComponent<

// If this is a primitive, and the parent primitive container is flipped,
// we flip it's position
if (false && this.isPcbPrimitive) {
if (this.isPcbPrimitive) {
const primitiveContainer = this.getPrimitiveContainer()
if (primitiveContainer) {
const isFlipped = primitiveContainer._parsedProps.layer === "bottom"
const containerCenter =
primitiveContainer._getGlobalPcbPositionBeforeLayout()

if (isFlipped) {
return compose(
this.parent?._computePcbGlobalTransformBeforeLayout() ?? identity(),
const flipOperation = compose(
translate(containerCenter.x, containerCenter.y),
flipY(),
translate(-containerCenter.x, -containerCenter.y),
)
return compose(
this.parent?._computePcbGlobalTransformBeforeLayout() ?? identity(),
flipY(),
this.computePcbPropsTransform(),
)
}
Expand Down Expand Up @@ -205,6 +209,30 @@ export abstract class PrimitiveComponent<
}
}

/**
* Determine if this pcb primitive should be flipped because the primitive
* container is flipped
*
* TODO use footprint.originalLayer instead of assuming everything is defined
* relative to the top layer
*/
_getPcbPrimitiveFlippedHelpers(): {
isFlipped: boolean
maybeFlipLayer: (layer: LayerRef) => LayerRef
} {
const container = this.getPrimitiveContainer()
const isFlipped = !container
? false
: container._parsedProps.layer === "bottom"
const maybeFlipLayer = (layer: LayerRef) => {
if (isFlipped) {
return layer === "top" ? "bottom" : "top"
}
return layer
}
return { isFlipped, maybeFlipLayer }
}

/**
* Set the position of this component from the layout solver. This method
* should operate using CircuitJson associated with this component, like
Expand Down
13 changes: 12 additions & 1 deletion lib/components/primitive-components/SilkscreenText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { silkscreenTextProps } from "@tscircuit/props"
export class SilkscreenText extends PrimitiveComponent<
typeof silkscreenTextProps
> {
isPcbPrimitive = true
doInitialPcbPrimitiveRender(): void {
const { db } = this.root!
const { _parsedProps: props } = this
const container = this.getPrimitiveContainer()!

const position = this._getGlobalPcbPositionBeforeLayout()
const { maybeFlipLayer } = this._getPcbPrimitiveFlippedHelpers()

// TODO handle layer flipping
db.pcb_silkscreen_text.insert({
Expand All @@ -20,9 +22,18 @@ export class SilkscreenText extends PrimitiveComponent<
},
font: props.font ?? "tscircuit2024",
font_size: props.fontSize ?? 1,
layer: "top",
layer: maybeFlipLayer(props.layer ?? "top") as "top" | "bottom",
text: props.text ?? "",
pcb_component_id: container.pcb_component_id!,
})
}

getPcbSize(): { width: number; height: number } {
const { _parsedProps: props } = this
const fontSize = props.fontSize ?? 1
const text = props.text ?? ""
const textWidth = text.length * fontSize
const textHeight = fontSize
return { width: textWidth * fontSize, height: textHeight * fontSize }
}
}
27 changes: 2 additions & 25 deletions lib/components/primitive-components/SmtPad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,15 @@ export class SmtPad extends PrimitiveComponent<typeof smtPadProps> {
const { _parsedProps: props } = this
if (!props.portHints) return
const container = this.getPrimitiveContainer()
let position = this._getGlobalPcbPositionBeforeLayout()
const position = this._getGlobalPcbPositionBeforeLayout()
const containerCenter = container?._getGlobalPcbPositionBeforeLayout()
const decomposedMat = decomposeTSR(
this._computePcbGlobalTransformBeforeLayout(),
)
const isRotated90 =
Math.abs(decomposedMat.rotation.angle * (180 / Math.PI) - 90) < 0.01

const isFlipped = !container
? false
: container._parsedProps.layer === "bottom"

const maybeFlipLayer = (layer: LayerRef) => {
if (isFlipped) {
return layer === "top" ? "bottom" : "top"
}
return layer
}

// When a component is flipped, we need to reflect it's position over the
// reference horizontal line formed at the container's center y
if (isFlipped && containerCenter) {
// TODO move this into _getGlobalPcbPositionBeforeLayout
position = applyToPoint(
compose(
translate(containerCenter.x, containerCenter.y),
flipY(),
translate(-containerCenter.x, -containerCenter.y),
),
position,
)
}
const { maybeFlipLayer } = this._getPcbPrimitiveFlippedHelpers()

let pcb_smtpad: PCBSMTPad | null = null
const pcb_component_id =
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b5f1a5a

Please sign in to comment.