Files
likang 7e01147566
CI / typecheck + test + build (push) Has been cancelled
Initial commit: import VibeMake PCB Studio
2026-11-03 11:20:08 +08:00

369 lines
12 KiB
TypeScript

// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 ZiroEDA and contributors.
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
/**
* `pcbnew/pcb_target.h` / `pcb_target.cpp`: `PCB_TARGET`, a mire (a + or X
* inside a circle) used as a photo-plot alignment mark.
*
* Not here: `PCB_TARGET_DESC`, the `PROPERTY_MANAGER` registration.
*/
import type { EDA_DRAW_FRAME_LIKE } from '@ziroeda/common/eda_item.js';
import { SHAPE_T } from '@ziroeda/common/eda_shape.js';
import { pcbIUScale } from '@ziroeda/common/eda_units.js';
import { FLASHING, PCB_LAYER_ID } from '@ziroeda/common/layer_id.js';
import type { UNITS_PROVIDER } from '@ziroeda/common/units_provider.js';
import { MSG_PANEL_ITEM } from '@ziroeda/common/widgets/msgpanel.js';
import { FLIP_DIRECTION } from '@ziroeda/core/mirror.js';
import { KICAD_T } from '@ziroeda/core/typeinfo.js';
import type { ERROR_LOC } from '@ziroeda/kimath/src/convert_basic_shapes_to_polygon.js';
import { EDA_ANGLE, EDA_ANGLE_T } from '@ziroeda/kimath/src/geometry/eda_angle.js';
import type { SHAPE } from '@ziroeda/kimath/src/geometry/shape.js';
import { SHAPE_CIRCLE } from '@ziroeda/kimath/src/geometry/shape_circle.js';
import type { SHAPE_LINE_CHAIN } from '@ziroeda/kimath/src/geometry/shape_line_chain.js';
import type { SHAPE_POLY_SET } from '@ziroeda/kimath/src/geometry/shape_poly_set.js';
import { BOX2I } from '@ziroeda/kimath/src/math/box2.js';
import { KiROUND } from '@ziroeda/kimath/src/math/util.js';
import { type VECTOR2I, equal } from '@ziroeda/kimath/src/math/vector2.js';
import { RotatePoint } from '@ziroeda/kimath/src/trigo.js';
import { DEFAULT_COPPER_LINE_WIDTH } from './board_design_settings_defaults.js';
import { BOARD_ITEM } from './board_item.js';
import {
ENUM_MAP,
type INSPECTABLE_ITEM,
NO_SETTER,
PG_CHOICES,
PROPERTY,
PROPERTY_DISPLAY,
PROPERTY_ENUM,
TYPE_BOOL,
TYPE_CAST,
TYPE_COLOR4D,
TYPE_DOUBLE,
TYPE_INT,
TYPE_OPT_INT,
TYPE_STRING,
} from '@ziroeda/common/properties/property.js';
import { PROPERTY_MANAGER, REGISTER_TYPE } from '@ziroeda/common/properties/property_mgr.js';
import { PCB_SHAPE } from './pcb_shape.js';
export class PCB_TARGET extends BOARD_ITEM {
private m_shape: number; // bit 0 : 0 = draw +, 1 = draw X
private m_size: number;
private m_lineWidth: number;
private m_pos: VECTOR2I;
constructor(aParent: BOARD_ITEM | null);
constructor(
aParent: BOARD_ITEM | null,
aShape: number,
aLayer: PCB_LAYER_ID,
aPos: VECTOR2I,
aSize: number,
aWidth: number,
);
constructor(
aParent: BOARD_ITEM | null,
aShape?: number,
aLayer?: PCB_LAYER_ID,
aPos?: VECTOR2I,
aSize?: number,
aWidth?: number,
) {
super(aParent, KICAD_T.PCB_TARGET_T);
if (aShape === undefined) {
this.m_shape = 0;
this.m_size = pcbIUScale.mmToIU(5); // Gives a decent size
this.m_lineWidth = pcbIUScale.mmToIU(DEFAULT_COPPER_LINE_WIDTH);
this.m_layer = PCB_LAYER_ID.Edge_Cuts; // a target is on all layers
this.m_pos = { x: 0, y: 0 };
} else {
this.m_shape = aShape;
this.m_layer = aLayer!;
this.m_pos = { x: aPos!.x, y: aPos!.y };
this.m_size = aSize!;
this.m_lineWidth = aWidth!;
}
}
// Do not create a copy constructor & operator=.
// The ones generated by the compiler are adequate.
/** `PCB_TARGET( const PCB_TARGET& )`: the compiler-generated copy, as a static. */
static copyOf(aOther: PCB_TARGET): PCB_TARGET {
const copy = new PCB_TARGET(null);
copy.assignTarget(aOther);
(copy as { m_Uuid: string }).m_Uuid = aOther.m_Uuid;
return copy;
}
/** The compiler-generated `operator=`. */
assignTarget(aOther: PCB_TARGET): this {
this.assignBoardItem(aOther);
this.m_shape = aOther.m_shape;
this.m_size = aOther.m_size;
this.m_lineWidth = aOther.m_lineWidth;
this.m_pos = { ...aOther.m_pos };
return this;
}
static ClassOf(aItem: { Type(): KICAD_T } | null): boolean {
return aItem !== null && KICAD_T.PCB_TARGET_T === aItem.Type();
}
override SetPosition(aPos: VECTOR2I): void {
this.m_pos = { x: aPos.x, y: aPos.y };
}
override GetPosition(): VECTOR2I {
return this.m_pos;
}
SetShape(aShape: number): void {
this.m_shape = aShape;
}
GetShape(): number {
return this.m_shape;
}
SetSize(aSize: number): void {
this.m_size = aSize;
}
GetSize(): number {
return this.m_size;
}
SetWidth(aWidth: number): void {
this.m_lineWidth = aWidth;
}
GetWidth(): number {
return this.m_lineWidth;
}
override Move(aMoveVector: VECTOR2I): void {
this.m_pos = { x: this.m_pos.x + aMoveVector.x, y: this.m_pos.y + aMoveVector.y };
}
override Rotate(aRotCentre: VECTOR2I, aAngle: EDA_ANGLE): void {
this.m_pos = RotatePoint(this.m_pos, aRotCentre, aAngle);
}
override Flip(aCentre: VECTOR2I, aFlipDirection: FLIP_DIRECTION): void {
if (aFlipDirection === FLIP_DIRECTION.LEFT_RIGHT)
this.m_pos = { x: aCentre.x - (this.m_pos.x - aCentre.x), y: this.m_pos.y };
else this.m_pos = { x: this.m_pos.x, y: aCentre.y - (this.m_pos.y - aCentre.y) };
this.SetLayer(this.GetBoard()!.FlipLayer(this.GetLayer()));
}
GetClass(): string {
return 'PCB_TARGET';
}
override HitTest(aPosition: VECTOR2I, aAccuracy?: number): boolean;
override HitTest(aRect: BOX2I, aContained: boolean, aAccuracy?: number): boolean;
override HitTest(aPoly: SHAPE_LINE_CHAIN, aContained: boolean): boolean;
override HitTest(
a: VECTOR2I | BOX2I | SHAPE_LINE_CHAIN,
b?: number | boolean,
c?: number,
): boolean {
if (a instanceof BOX2I) {
const arect = a.Clone();
arect.Inflate(c ?? 0);
if (b as boolean) return arect.Contains(this.GetBoundingBox());
else return this.GetBoundingBox().Intersects(arect);
}
if (typeof (a as VECTOR2I).x === 'number') {
const aPosition = a as VECTOR2I;
const dX = aPosition.x - this.m_pos.x;
const dY = aPosition.y - this.m_pos.y;
const radius = ((b as number | undefined) ?? 0) + Math.trunc(this.m_size / 2);
return Math.abs(dX) <= radius && Math.abs(dY) <= radius;
}
// Not overridden for a chain in C++: BOARD_ITEM's default.
return super.HitTest(a as SHAPE_LINE_CHAIN, b as boolean);
}
// Virtual function
override GetBoundingBox(): BOX2I {
const bBox = new BOX2I();
bBox.SetX(this.m_pos.x - Math.trunc(this.m_size / 2));
bBox.SetY(this.m_pos.y - Math.trunc(this.m_size / 2));
bBox.SetWidth(this.m_size);
bBox.SetHeight(this.m_size);
return bBox;
}
override GetEffectiveShape(
aLayer: PCB_LAYER_ID = PCB_LAYER_ID.UNDEFINED_LAYER,
aFlash: FLASHING = FLASHING.DEFAULT,
): SHAPE {
return new SHAPE_CIRCLE(this.m_pos, Math.trunc(this.m_size / 2));
}
override GetItemDescription(aUnitsProvider: UNITS_PROVIDER | null, aFull: boolean): string {
// Targets are on *every* layer by definition
return 'Target';
}
override GetMenuImage(): string {
return 'add_pcb_target'; // BITMAPS::add_pcb_target
}
override Clone(): PCB_TARGET {
return PCB_TARGET.copyOf(this);
}
protected override swapData(aImage: BOARD_ITEM): void {
console.assert(aImage.Type() === KICAD_T.PCB_TARGET_T);
// std::swap( *this, *aImage ): every member of both classes.
const image = aImage as PCB_TARGET;
const mine = PCB_TARGET.copyOf(this);
this.assignTarget(image);
(this as { m_Uuid: string }).m_Uuid = image.m_Uuid;
image.assignTarget(mine);
(image as { m_Uuid: string }).m_Uuid = mine.m_Uuid;
}
override GetMsgPanelInfo(aFrame: EDA_DRAW_FRAME_LIKE, aList: MSG_PANEL_ITEM[]): void {
aList.push(new MSG_PANEL_ITEM('PCB Target', ''));
aList.push(new MSG_PANEL_ITEM('Layer', this.GetLayerName()));
aList.push(new MSG_PANEL_ITEM('Size', aFrame.MessageTextFromValue(this.GetSize())));
aList.push(new MSG_PANEL_ITEM('Width', aFrame.MessageTextFromValue(this.GetWidth())));
aList.push(new MSG_PANEL_ITEM('Shape', this.GetShape() === 0 ? '+' : 'X'));
}
/**
* Convert the shape to a closed polygon.
*
* Used in filling zones calculations. Circles and arcs are approximated by segments.
*
* @param aBuffer is a buffer to store the polygon.
* @param aClearance is the clearance around the pad.
* @param aError is the maximum deviation from a true arc.
* @param aErrorLoc whether any approximation error should be placed inside or outside
* @param ignoreLineWidth is used for edge cut items where the line width is only for
* visualization
*/
override TransformShapeToPolygon(
aBuffer: SHAPE_POLY_SET,
aLayer: PCB_LAYER_ID,
aClearance: number,
aError: number,
aErrorLoc: ERROR_LOC,
ignoreLineWidth = false,
): void {
const size = KiROUND(this.GetShape() ? this.GetSize() / 1.5 : this.GetSize() / 2.0);
const radius = KiROUND(this.GetShape() ? this.GetSize() / 2.0 : this.GetSize() / 3.0);
const line1 = new PCB_SHAPE();
const line2 = new PCB_SHAPE();
const circle = new PCB_SHAPE(null, SHAPE_T.CIRCLE);
line1.SetStart({ x: -size, y: 0 });
line1.SetEnd({ x: size, y: 0 });
line2.SetStart({ x: 0, y: -size });
line2.SetEnd({ x: 0, y: size });
circle.SetEndX(radius);
if (this.GetShape()) {
// shape x
line1.Rotate({ x: 0, y: 0 }, new EDA_ANGLE(45.0, EDA_ANGLE_T.DEGREES_T));
line2.Rotate({ x: 0, y: 0 }, new EDA_ANGLE(45.0, EDA_ANGLE_T.DEGREES_T));
}
for (const item of [line1, line2, circle]) {
item.SetWidth(this.GetWidth());
item.Move(this.GetPosition());
item.TransformShapeToPolygon(aBuffer, aLayer, aClearance, aError, aErrorLoc, ignoreLineWidth);
}
}
/** `operator==( const BOARD_ITEM& )`. */
equals(aBoardItem: BOARD_ITEM): boolean {
if (aBoardItem.Type() !== this.Type()) return false;
const other = aBoardItem as PCB_TARGET;
return this.equalsTarget(other);
}
/** `operator==( const PCB_TARGET& )`. */
equalsTarget(aOther: PCB_TARGET): boolean {
return (
this.m_shape === aOther.m_shape &&
this.m_size === aOther.m_size &&
this.m_lineWidth === aOther.m_lineWidth &&
this.m_layer === aOther.m_layer &&
equal(this.m_pos, aOther.m_pos)
);
}
Similarity(aOther: BOARD_ITEM): number {
if (aOther.Type() !== this.Type()) return 0.0;
const other = aOther as PCB_TARGET;
let similarity = 1.0;
if (this.GetShape() !== other.GetShape()) similarity *= 0.9;
if (this.GetSize() !== other.GetSize()) similarity *= 0.9;
if (this.GetWidth() !== other.GetWidth()) similarity *= 0.9;
if (this.GetLayer() !== other.GetLayer()) similarity *= 0.9;
if (!equal(this.GetPosition(), other.GetPosition())) similarity *= 0.9;
return similarity;
}
}
/**
* `static struct PCB_TARGET_DESC` (pcbnew/pcb_target.cpp).
*/
(() => {
const propMgr = PROPERTY_MANAGER.Instance();
REGISTER_TYPE(PCB_TARGET);
propMgr.InheritsAfter(PCB_TARGET, BOARD_ITEM);
propMgr.AddProperty(
new PROPERTY<PCB_TARGET, number>(
PCB_TARGET,
'Size',
'SetSize',
'GetSize',
TYPE_INT,
PROPERTY_DISPLAY.PT_SIZE,
),
);
propMgr.AddProperty(
new PROPERTY<PCB_TARGET, number>(
PCB_TARGET,
'Width',
'SetWidth',
'GetWidth',
TYPE_INT,
PROPERTY_DISPLAY.PT_SIZE,
),
);
const shape = new PROPERTY<PCB_TARGET, number>(
PCB_TARGET,
'Shape',
'SetShape',
'GetShape',
TYPE_INT,
);
// TODO change the integer to an enum?
//shape->SetValues( { { 0, _HKI( "Cross" ) }, { 1, ( "Plus" ) } } );
propMgr.AddProperty(shape);
})();