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

246 lines
7.3 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.
/**
* NETINFO_ITEM: a net entry.
*
* Counterpart: `pcbnew/netinfo_item.cpp`.
*/
import { unescapeString } from '@ziroeda/common/string_utils.js';
import type { EDA_SEARCH_DATA } from '@ziroeda/common/eda_search_data.js';
import type { EDA_DRAW_FRAME_LIKE } from '@ziroeda/common/eda_item.js';
import { MSG_PANEL_ITEM } from '@ziroeda/common/widgets/msgpanel.js';
import { NETCLASS } from '@ziroeda/common/netclass.js';
import { KICAD_T } from '@ziroeda/core/typeinfo.js';
import { BOX2I } from '@ziroeda/kimath/src/math/box2.js';
import type { VECTOR2I } from '@ziroeda/kimath/src/math/vector2.js';
import type { BOARD } from './board.js';
import { BOARD_ITEM } from './board_item.js';
/** `wxString::AfterLast`: the part after the last `ch`, or the whole string when absent. */
function wxAfterLast(aStr: string, ch: string): string {
const i = aStr.lastIndexOf(ch);
return i < 0 ? aStr : aStr.slice(i + 1);
}
/**
* Handle the data for a net.
*/
const netItemTypes: readonly KICAD_T[] = [
KICAD_T.PCB_TRACE_T,
KICAD_T.PCB_ARC_T,
KICAD_T.PCB_VIA_T,
KICAD_T.PCB_ZONE_T,
KICAD_T.PCB_PAD_T,
KICAD_T.PCB_SHAPE_T,
];
/**
* Handle the data for a net.
*/
export class NETINFO_ITEM extends BOARD_ITEM {
private m_netCode: number; ///< A number equivalent to the net name.
private m_netname: string; ///< Full net name like /sheet/subsheet/vout used by Eeschema.
private m_shortNetname: string; ///< Short net name, like vout from /sheet/subsheet/vout.
private m_displayNetname: string; ///< Unescaped netname for display. Usually the short netname,
///< but will be the full netname if disambiguation required.
///< The NETINFO_LIST is repsonsible for the management of when
///< these need to be updated/disambiguated.
private m_netClass: NETCLASS;
private m_isCurrent: boolean; ///< Indicates the net is currently in use. We still store
///< those that are not during a session for undo/redo and to
///< keep netclass membership information.
constructor(aParent: BOARD | null, aNetName = '', aNetCode = -1) {
super(aParent as unknown as BOARD_ITEM | null, KICAD_T.PCB_NETINFO_T);
this.m_netCode = aNetCode;
this.m_netname = aNetName;
this.m_shortNetname = wxAfterLast(this.m_netname, '/');
this.m_displayNetname = unescapeString(this.m_shortNetname);
this.m_isCurrent = true;
if (aParent) this.m_netClass = aParent.GetDesignSettings().m_NetSettings.GetDefaultNetclass();
else this.m_netClass = new NETCLASS('Default');
}
static ClassOf(aItem: { Type(): KICAD_T } | null): boolean {
return !!aItem && KICAD_T.PCB_NETINFO_T === aItem.Type();
}
GetClass(): string {
return 'NETINFO_ITEM';
}
override GetBoundingBox(): BOX2I {
const bbox = new BOX2I();
const conn = this.GetBoard()!.GetConnectivity();
for (const item of conn.GetNetItems(this.m_netCode, netItemTypes))
bbox.Merge(item.GetBoundingBox());
return bbox;
}
override GetPosition(): VECTOR2I {
return { x: 0, y: 0 };
}
override SetPosition(aPos: VECTOR2I): void {}
override Clone(): NETINFO_ITEM {
const copy = new NETINFO_ITEM(this.GetParent(), this.m_netname, this.m_netCode);
BOARD_ITEM.copyBase(copy, this);
copy.m_shortNetname = this.m_shortNetname;
copy.m_displayNetname = this.m_displayNetname;
copy.m_netClass = this.m_netClass;
copy.m_isCurrent = this.m_isCurrent;
return copy;
}
SetNetClass(aNetClass: NETCLASS | null): void {
const parent = this.GetParent();
if (!parent) return; // wxCHECK( m_parent, /* void */ )
if (aNetClass) this.m_netClass = aNetClass;
else this.m_netClass = parent.GetDesignSettings().m_NetSettings.GetDefaultNetclass();
}
/**
* @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.
*/
GetNetClass(): NETCLASS {
return this.m_netClass;
}
/**
* @note Slow version for swig access
*/
GetNetClassSlow(): NETCLASS {
return this.m_netClass;
}
GetNetCode(): number {
return this.m_netCode;
}
SetNetCode(aNetCode: number): void {
this.m_netCode = aNetCode;
}
/**
* @return the full netname.
*/
GetNetname(): string {
return this.m_netname;
}
/**
* @return the short netname.
*/
GetShortNetname(): string {
return this.m_shortNetname;
}
/**
* @return the unescaped short netname.
*/
GetDisplayNetname(): string {
return this.m_displayNetname;
}
/**
* @return true if the net was not labelled by the user.
*/
HasAutoGeneratedNetname(): boolean {
return (
this.m_shortNetname.startsWith('Net-(') || this.m_shortNetname.startsWith('unconnected-(')
);
}
/**
* Set the long netname to \a aNetName, the short netname to the last token in
* the long netname's path, and the unescaped short netname.
*/
SetNetname(aNewName: string): void {
this.m_netname = aNewName;
if (aNewName.includes('/')) this.m_shortNetname = wxAfterLast(aNewName, '/');
else this.m_shortNetname = aNewName;
this.m_displayNetname = unescapeString(this.m_shortNetname);
}
IsCurrent(): boolean {
return this.m_isCurrent;
}
SetIsCurrent(isCurrent: boolean): void {
this.m_isCurrent = isCurrent;
}
/**
* Return the information about the #NETINFO_ITEM in \a aList to display in the
* message panel.
*
* @param aList is the list in which to place the status information.
*/
override GetMsgPanelInfo(aFrame: EDA_DRAW_FRAME_LIKE, aList: MSG_PANEL_ITEM[]): void {
aList.push(new MSG_PANEL_ITEM('Net Name', unescapeString(this.GetNetname())));
aList.push(new MSG_PANEL_ITEM('Net Code', `${this.GetNetCode()}`));
// The pad, via and net-length rows walk Footprints()/Pads() and Tracks() and call
// BOARD::GetTrackLength: they come with FOOTPRINT, PAD and PCB_TRACK (#636 stage 1).
}
/**
* Set all fields to their default values.
*/
Clear(): void {
const parent = this.GetParent();
if (!parent) return; // wxCHECK( m_parent, /* void */ )
this.m_netClass = parent.GetDesignSettings().m_NetSettings.GetDefaultNetclass();
}
override SetParent(aParent: BOARD | null): void {
this.m_parent = aParent as unknown as BOARD_ITEM | null;
}
// Replace EDA_ITEM::GetParent() with a more useful return-type
override GetParent(): BOARD | null {
return this.m_parent as unknown as BOARD | null;
}
override Matches(aSearchData: EDA_SEARCH_DATA, aAuxData: unknown): boolean {
return this.matchesText(this.GetNetname(), aSearchData);
}
Similarity(aBoardItem: BOARD_ITEM): number {
return 0.0;
}
equals(aBoardItem: BOARD_ITEM): boolean {
return false; // `return 0.0;` in the C++
}
// NETINFO_LIST reaches these directly (it is a friend).
/** @internal */
set_netCode(aCode: number): void {
this.m_netCode = aCode;
}
/** @internal */
set_displayNetname(aName: string): void {
this.m_displayNetname = aName;
}
}
/** `NETNAMES_MAP` / `NETCODES_MAP`: `std::map`s, so their iteration is key order. */
export type NETNAMES_MAP = Map<string, NETINFO_ITEM>;
export type NETCODES_MAP = Map<number, NETINFO_ITEM>;