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

562 lines
17 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/board_connected_item.h` / `.cpp`: `BOARD_CONNECTED_ITEM`, a base
* class derived from #BOARD_ITEM for items that can be connected and have a
* net, a netname, a clearance ...
*
* Not here: `UnpackNet`/`PackNet` (the protobuf API) and the
* `BOARD_CONNECTED_ITEM_DESC` property registration. `GetOwnClearance` asks
* the DRC engine, which is not on the design settings yet; it returns 0 as
* the C++ does without one.
*/
import { ROUTER_TRANSIENT } from '@ziroeda/common/eda_item_flags.js';
import { PCB_LAYER_ID } from '@ziroeda/common/layer_id.js';
import { LSET } from '@ziroeda/common/lset.js';
import { NETCLASS } from '@ziroeda/common/netclass.js';
import {
ENUM_MAP,
type INSPECTABLE_ITEM,
NO_SETTER,
PROPERTY,
PROPERTY_DISPLAY,
PROPERTY_ENUM,
TYPE_BOOL,
TYPE_DOUBLE,
TYPE_INT,
TYPE_STRING,
} from '@ziroeda/common/properties/property.js';
import { PROPERTY_MANAGER, REGISTER_TYPE } from '@ziroeda/common/properties/property_mgr.js';
import { PROPERTY_VALIDATORS } from '@ziroeda/common/properties/property_validators.js';
import type { OutStr } from '@ziroeda/common/font/font.js';
import { unescapeString } from '@ziroeda/common/string_utils.js';
import { KICAD_T } from '@ziroeda/core/typeinfo.js';
import { BOARD_ITEM } from './board_item.js';
import { type NETINFO_ITEM, NETINFO_LIST } from './netinfo.js';
import { TEARDROP_PARAMETERS } from './teardrop/teardrop_parameters.js';
// Static fallback netclass for items without a valid board (e.g., during DRC evaluation
// of dummy items, or items not yet added to a board).
const fallbackNetclass = new NETCLASS(NETCLASS.Default);
/**
* A base class derived from #BOARD_ITEM for items that can be connected and have a net, a
* netname, a clearance ...
*/
export abstract class BOARD_CONNECTED_ITEM extends BOARD_ITEM {
/// Store all information about the net that item belongs to.
protected m_netinfo: NETINFO_ITEM | null;
/// Not all BOARD_CONNECTED_ITEMs support teardrops, but we want those that do to share a
/// single section in the property inspector.
protected m_teardropParams = new TEARDROP_PARAMETERS();
private m_localRatsnestVisible: boolean;
constructor(aParent: BOARD_ITEM | null, idtype: KICAD_T) {
super(aParent, idtype);
this.m_netinfo = NETINFO_LIST.OrphanedItem();
this.m_localRatsnestVisible = true;
}
/** The compiler-generated copy of this class's members, for a derived copy constructor. */
protected static copyConnected<T extends BOARD_CONNECTED_ITEM>(
aInto: T,
aOther: BOARD_CONNECTED_ITEM,
): T {
BOARD_ITEM.copyBase(aInto, aOther);
aInto.m_netinfo = aOther.m_netinfo;
aInto.m_teardropParams = aOther.m_teardropParams.clone();
aInto.m_localRatsnestVisible = aOther.m_localRatsnestVisible;
return aInto;
}
/** `operator=`: the compiler-generated one over BOARD_ITEM and this class's members. */
assignConnected(aOther: BOARD_CONNECTED_ITEM): this {
this.assignBoardItem(aOther);
this.m_netinfo = aOther.m_netinfo;
this.m_teardropParams = aOther.m_teardropParams.clone();
this.m_localRatsnestVisible = aOther.m_localRatsnestVisible;
return this;
}
override SetLayer(aLayer: PCB_LAYER_ID): void {
super.SetLayer(aLayer);
if (!(this.GetFlags() & ROUTER_TRANSIENT)) {
// if( BOARD* board = GetBoard() ) board->InvalidateClearanceCache( m_Uuid );
// -- BOARD's DRC caches pending (#636)
}
}
override GetLayer(): PCB_LAYER_ID {
return super.GetLayer();
}
// Do not create a copy constructor & operator=.
// The ones generated by the compiler are adequate.
static ClassOf(aItem: { Type(): KICAD_T } | null): boolean {
if (aItem === null) return false;
switch (aItem.Type()) {
case KICAD_T.PCB_PAD_T:
case KICAD_T.PCB_TRACE_T:
case KICAD_T.PCB_ARC_T:
case KICAD_T.PCB_VIA_T:
case KICAD_T.PCB_ZONE_T:
case KICAD_T.PCB_SHAPE_T:
return true;
default:
return false;
}
}
///< @copydoc BOARD_ITEM::IsConnected()
override IsConnected(): boolean {
return true;
}
/**
* Return #NET_INFO object for a given item.
*/
GetNet(): NETINFO_ITEM | null {
return this.m_netinfo;
}
/**
* Set a NET_INFO object for the item.
*/
SetNet(aNetInfo: NETINFO_ITEM | null): void {
this.m_netinfo = aNetInfo;
}
/**
* @return the net code.
*/
GetNetCode(): number {
return this.m_netinfo ? this.m_netinfo.GetNetCode() : -1;
}
/**
* Set net using a net code.
*
* @note Pads not on copper layers will have their net code always set to 0 (not connected).
*
* @param aNetCode is a net code for the new net. It has to exist in #NETINFO_LIST held
* by #BOARD.
* @param aNoAssert if true, do not assert that the net exists. Otherwise, item is assigned
* to the unconnected net.
* @return true on success, false if the net did not exist
*/
SetNetCode(aNetCode: number, aNoAssert = false): boolean {
if (!this.IsOnCopperLayer()) aNetCode = 0;
// if aNetCode < 0 (typically NETINFO_LIST::FORCE_ORPHANED) or no parent board,
// set the m_netinfo to the dummy NETINFO_LIST::ORPHANED
const board = this.GetBoard();
if (aNetCode >= 0 && board) this.m_netinfo = board.FindNet(aNetCode);
else this.m_netinfo = NETINFO_LIST.OrphanedItem();
if (!aNoAssert) console.assert(!!this.m_netinfo);
if (board) {
// if( !( GetFlags() & ROUTER_TRANSIENT ) ) board->InvalidateClearanceCache( m_Uuid );
// board->m_ItemNetclassCache.erase( this ); -- BOARD's DRC caches pending (#636)
}
return this.m_netinfo !== null;
}
/**
* @return the full netname.
*/
GetNetname(): string {
return this.m_netinfo ? this.m_netinfo.GetNetname() : '';
}
/**
* @return the full netname or "<no net>" in square braces, followed by "(Not Found)" if the
* netcode is undefined.
*/
GetNetnameMsg(): string {
if (!this.GetBoard()) return '[** NO BOARD DEFINED **]';
const netname = this.GetNetname();
if (!netname.length) return '[<no net>]';
else if (this.GetNetCode() < 0) return `[${unescapeString(netname)}](Not Found)`;
else return `[${unescapeString(netname)}]`;
}
/**
* @return the short netname.
*/
GetShortNetname(): string {
return this.m_netinfo ? this.m_netinfo.GetShortNetname() : '';
}
/**
* @return the unescaped short netname.
*/
GetDisplayNetname(): string {
if (!this.m_netinfo) return '';
const board = this.GetBoard();
if (board) {
if (board.GetNetInfo().m_DisplayNetnamesDirty) board.GetNetInfo().RebuildDisplayNetnames();
}
return this.m_netinfo.GetDisplayNetname();
}
/**
* Return an item's "own" clearance in internal units.
*
* @param aLayer the layer in question.
* @param aSource [out] optionally reports the source as a user-readable string.
* @return the clearance in internal units.
*/
GetOwnClearance(aLayer: PCB_LAYER_ID, aSource: OutStr | null = null): number {
if (this.GetBoard() && this.GetBoard()!.GetDesignSettings().m_DRCEngine) {
const bds = this.GetBoard()!.GetDesignSettings();
return bds.m_DRCEngine!.GetCachedOwnClearance(this, aLayer, aSource);
}
return 0;
}
/**
* Return any clearance overrides set in the "classic" (ie: pre-rule) system.
*
* @param aSource [out] optionally reports the source as a user-readable string.
* @return the clearance in internal units.
*/
GetClearanceOverrides(aSource: OutStr | null): number | undefined {
return undefined;
}
/**
* Return any local clearances set in the "classic" (ie: pre-rule) system.
*
* @param aSource [out] optionally reports the source as a user-readable string.
* @return the clearance (if any is specified) in internal units.
*/
GetLocalClearance(aSource?: OutStr | null): number | undefined {
return undefined;
}
/**
* Return the #NETCLASS for this item.
*
* @note Do **not** return a std::shared_ptr from this. It is used heavily in DRC, and the
* std::shared_ptr stuff shows up large in performance profiling.
*/
GetEffectiveNetClass(): NETCLASS {
if (this.m_netinfo && this.m_netinfo.GetNetClass()) return this.m_netinfo.GetNetClass();
const board = this.GetBoard();
if (board) {
if (board.GetDesignSettings().m_NetSettings)
return board.GetDesignSettings().m_NetSettings.GetDefaultNetclass();
}
return fallbackNetclass;
}
/**
* Returns the name of the effective netclass. Primarily for the use of the property system.
*/
GetNetClassName(): string {
return this.GetEffectiveNetClass().GetName();
}
SetLocalRatsnestVisible(aVisible: boolean): void {
this.m_localRatsnestVisible = aVisible;
}
GetLocalRatsnestVisible(): boolean {
return this.m_localRatsnestVisible;
}
GetTeardropParams(): TEARDROP_PARAMETERS {
return this.m_teardropParams;
}
SetTeardropsEnabled(aEnable: boolean): void {
this.m_teardropParams.m_Enabled = aEnable;
}
GetTeardropsEnabled(): boolean {
return this.m_teardropParams.m_Enabled;
}
SetTeardropBestLengthRatio(aRatio: number): void {
this.m_teardropParams.m_BestLengthRatio = aRatio;
}
GetTeardropBestLengthRatio(): number {
return this.m_teardropParams.m_BestLengthRatio;
}
SetTeardropMaxLength(aMaxLength: number): void {
this.m_teardropParams.m_TdMaxLen = aMaxLength;
}
GetTeardropMaxLength(): number {
return this.m_teardropParams.m_TdMaxLen;
}
SetTeardropBestWidthRatio(aRatio: number): void {
this.m_teardropParams.m_BestWidthRatio = aRatio;
}
GetTeardropBestWidthRatio(): number {
return this.m_teardropParams.m_BestWidthRatio;
}
SetTeardropMaxWidth(aMaxWidth: number): void {
this.m_teardropParams.m_TdMaxWidth = aMaxWidth;
}
GetTeardropMaxWidth(): number {
return this.m_teardropParams.m_TdMaxWidth;
}
SetTeardropCurved(aCurve: boolean): void {
this.m_teardropParams.m_CurvedEdges = aCurve;
}
GetTeardropCurved(): boolean {
return this.m_teardropParams.m_CurvedEdges;
}
SetTeardropPreferZoneConnections(aPrefer: boolean): void {
this.m_teardropParams.m_TdOnPadsInZones = !aPrefer;
}
GetTeardropPreferZoneConnections(): boolean {
return !this.m_teardropParams.m_TdOnPadsInZones;
}
SetTeardropAllowSpanTwoTracks(aAllow: boolean): void {
this.m_teardropParams.m_AllowUseTwoTracks = aAllow;
}
GetTeardropAllowSpanTwoTracks(): boolean {
return this.m_teardropParams.m_AllowUseTwoTracks;
}
SetTeardropMaxTrackWidth(aRatio: number): void {
this.m_teardropParams.m_WidthtoSizeFilterRatio = aRatio;
}
GetTeardropMaxTrackWidth(): number {
return this.m_teardropParams.m_WidthtoSizeFilterRatio;
}
}
/**
* `static struct BOARD_CONNECTED_ITEM_DESC` (pcbnew/board_connected_item.cpp).
*/
(() => {
const layerEnum = ENUM_MAP.Instance<PCB_LAYER_ID>('PCB_LAYER_ID');
if (layerEnum.Choices().GetCount() === 0) {
layerEnum.Undefined(PCB_LAYER_ID.UNDEFINED_LAYER);
for (const layer of LSET.AllLayersMask().Seq()) layerEnum.Map(layer, LSET.Name(layer));
}
const propMgr = PROPERTY_MANAGER.Instance();
REGISTER_TYPE(BOARD_CONNECTED_ITEM);
propMgr.InheritsAfter(BOARD_CONNECTED_ITEM, BOARD_ITEM);
// Replace layer property as the properties panel will set a restriction for copper layers
// only for BOARD_CONNECTED_ITEM that we don't want to apply to BOARD_ITEM
const layer = new PROPERTY_ENUM<BOARD_CONNECTED_ITEM, PCB_LAYER_ID>(
BOARD_CONNECTED_ITEM,
'Layer',
'SetLayer',
'GetLayer',
layerEnum,
);
layer.SetChoices(layerEnum.Choices());
propMgr.ReplaceProperty(BOARD_ITEM, 'Layer', layer);
propMgr
.AddProperty(
new PROPERTY_ENUM<BOARD_CONNECTED_ITEM, number>(
BOARD_CONNECTED_ITEM,
'Net',
'SetNetCode',
'GetNetCode',
TYPE_INT,
PROPERTY_DISPLAY.PT_NET,
),
)
.SetIsHiddenFromRulesEditor()
.SetIsHiddenFromLibraryEditors();
/**
* This property should just be an alias for the one below, it only exists so that we
* maintain compatibility with both `NetClass` and `Net_Class` in custom rules.
* It has the name we would show in the GUI if we wanted to show this in the GUI, but we
* don't at the moment because there is no way to edit the netclass of a net from a selected
* connected item, and showing it makes users think they can change it.
*/
propMgr
.AddProperty(
new PROPERTY<BOARD_CONNECTED_ITEM, string>(
BOARD_CONNECTED_ITEM,
'Net Class',
NO_SETTER,
'GetNetClassName',
TYPE_STRING,
),
)
.SetIsHiddenFromRulesEditor()
.SetIsHiddenFromPropertiesManager()
.SetIsHiddenFromLibraryEditors();
// Compatibility alias for DRC engine
propMgr
.AddProperty(
new PROPERTY<BOARD_CONNECTED_ITEM, string>(
BOARD_CONNECTED_ITEM,
'NetClass',
NO_SETTER,
'GetNetClassName',
TYPE_STRING,
),
)
.SetIsHiddenFromPropertiesManager()
.SetIsHiddenFromLibraryEditors();
// Used only in DRC engine
propMgr
.AddProperty(
new PROPERTY<BOARD_CONNECTED_ITEM, string>(
BOARD_CONNECTED_ITEM,
'NetName',
NO_SETTER,
'GetNetname',
TYPE_STRING,
),
)
.SetIsHiddenFromPropertiesManager()
.SetIsHiddenFromLibraryEditors();
const supportsTeardrops = (aItem: INSPECTABLE_ITEM): boolean => {
if (aItem instanceof BOARD_CONNECTED_ITEM) {
if (!aItem.GetBoard() || aItem.GetBoard()!.LegacyTeardrops()) return false;
return aItem.Type() === KICAD_T.PCB_PAD_T || aItem.Type() === KICAD_T.PCB_VIA_T;
}
return false;
};
const supportsTeardropPreferZoneSetting = (aItem: INSPECTABLE_ITEM): boolean => {
if (aItem instanceof BOARD_CONNECTED_ITEM) {
if (!aItem.GetBoard() || aItem.GetBoard()!.LegacyTeardrops()) return false;
return aItem.Type() === KICAD_T.PCB_PAD_T;
}
return false;
};
const groupTeardrops = 'Teardrops';
const enableTeardrops = new PROPERTY<BOARD_CONNECTED_ITEM, boolean>(
BOARD_CONNECTED_ITEM,
'Enable Teardrops',
'SetTeardropsEnabled',
'GetTeardropsEnabled',
TYPE_BOOL,
);
enableTeardrops.SetAvailableFunc(supportsTeardrops);
propMgr.AddProperty(enableTeardrops, groupTeardrops);
const bestLength = new PROPERTY<BOARD_CONNECTED_ITEM, number>(
BOARD_CONNECTED_ITEM,
'Best Length Ratio',
'SetTeardropBestLengthRatio',
'GetTeardropBestLengthRatio',
TYPE_DOUBLE,
);
bestLength.SetAvailableFunc(supportsTeardrops);
bestLength.SetValidator(PROPERTY_VALIDATORS.PositiveRatioValidator);
propMgr.AddProperty(bestLength, groupTeardrops);
const maxLength = new PROPERTY<BOARD_CONNECTED_ITEM, number>(
BOARD_CONNECTED_ITEM,
'Max Length',
'SetTeardropMaxLength',
'GetTeardropMaxLength',
TYPE_INT,
PROPERTY_DISPLAY.PT_SIZE,
);
maxLength.SetAvailableFunc(supportsTeardrops);
propMgr.AddProperty(maxLength, groupTeardrops);
const bestWidth = new PROPERTY<BOARD_CONNECTED_ITEM, number>(
BOARD_CONNECTED_ITEM,
'Best Width Ratio',
'SetTeardropBestWidthRatio',
'GetTeardropBestWidthRatio',
TYPE_DOUBLE,
);
bestWidth.SetAvailableFunc(supportsTeardrops);
bestWidth.SetValidator(PROPERTY_VALIDATORS.PositiveRatioValidator);
propMgr.AddProperty(bestWidth, groupTeardrops);
const maxWidth = new PROPERTY<BOARD_CONNECTED_ITEM, number>(
BOARD_CONNECTED_ITEM,
'Max Width',
'SetTeardropMaxWidth',
'GetTeardropMaxWidth',
TYPE_INT,
PROPERTY_DISPLAY.PT_SIZE,
);
maxWidth.SetAvailableFunc(supportsTeardrops);
propMgr.AddProperty(maxWidth, groupTeardrops);
const curvePts = new PROPERTY<BOARD_CONNECTED_ITEM, boolean>(
BOARD_CONNECTED_ITEM,
'Curved Teardrops',
'SetTeardropCurved',
'GetTeardropCurved',
TYPE_BOOL,
);
curvePts.SetAvailableFunc(supportsTeardrops);
propMgr.AddProperty(curvePts, groupTeardrops);
const preferZones = new PROPERTY<BOARD_CONNECTED_ITEM, boolean>(
BOARD_CONNECTED_ITEM,
'Prefer Zone Connections',
'SetTeardropPreferZoneConnections',
'GetTeardropPreferZoneConnections',
TYPE_BOOL,
);
preferZones.SetAvailableFunc(supportsTeardropPreferZoneSetting);
propMgr.AddProperty(preferZones, groupTeardrops);
const twoTracks = new PROPERTY<BOARD_CONNECTED_ITEM, boolean>(
BOARD_CONNECTED_ITEM,
'Allow Teardrops To Span Two Tracks',
'SetTeardropAllowSpanTwoTracks',
'GetTeardropAllowSpanTwoTracks',
TYPE_BOOL,
);
twoTracks.SetAvailableFunc(supportsTeardrops);
propMgr.AddProperty(twoTracks, groupTeardrops);
const maxTrackWidth = new PROPERTY<BOARD_CONNECTED_ITEM, number>(
BOARD_CONNECTED_ITEM,
'Max Width Ratio',
'SetTeardropMaxTrackWidth',
'GetTeardropMaxTrackWidth',
TYPE_DOUBLE,
);
maxTrackWidth.SetAvailableFunc(supportsTeardrops);
propMgr.AddProperty(maxTrackWidth, groupTeardrops);
})();