const modelNode = flux.getNodeById("4b32447c-e4b3-468b-8c7d-fb9753d46a91") as PcbLayoutNode;
const stepAssetNames = flux.assets
.filter((asset) => asset.fileType === "step")
.map((asset) => asset.name)
.sort();
const DEFAULT_MPN = "97171A310";
const MPN_PROPERTY = "Manufacturer Part Number";
if (modelNode) {
const assetModelRule = modelNode.rules.find((rule) => rule.key === "asset");
flux.on("setup", () => {
const mpnProp = flux.properties.find((p) => p.name === MPN_PROPERTY);
setModel(mpnProp ? String(mpnProp.rawValue) : DEFAULT_MPN);
});
flux.on("propertyChange", (event: PropertyChangeEvent) => {
if (event.property_name === MPN_PROPERTY) {
setModel(String(event.rawValue));
}
});
// Picks the first STEP asset whose name starts with the MPN. Falls back to the default MPN.
function setModel(mpn: string) {
if (!assetModelRule) return;
const match =
stepAssetNames.find((name) => name.startsWith(mpn)) ??
stepAssetNames.find((name) => name.startsWith(DEFAULT_MPN));