You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Altium Designer Pascal脚本调用内置方法报‘未声明标识符’错误求助

问题:Altium Designer Pascal脚本中BoardOutline、RemoveAllVertices等方法报“未声明标识符”错误

尝试用ChatGPT提供的Pascal脚本在Altium Designer Professional中绘制圆形板框,但脚本里的BoardOutline、RemoveAllVertices、AddVertex等方法全部提示“未声明标识符”,这些明明是Altium的内置方法,为什么会出现这个错误?

ChatGPT提供的原代码如下:

var
  PCBBoard: IPCB_Board;
  BoardShape: IPCB_BoardShape;
  Vertices: T2DVertexes;
  CenterX, CenterY, Radius: Double;
  Angle: Double;
  NumSegments: Integer;
  i: Integer;
begin
  // Get the current PCB board
  PCBBoard := PCBServer.GetCurrentPCBBoard;

  // Access the board shape
  BoardShape := PCBBoard.BoardOutline;

  // Clear existing vertices
  BoardShape.RemoveAllVertices;

  // Define the center coordinates and radius of the circle
  CenterX := MilsToCoord(5000); // Center X coordinate in mils
  CenterY := MilsToCoord(5000); // Center Y coordinate in mils
  Radius := MilsToCoord(5000); // Radius in mils

  // Define the number of segments to approximate the circle
  NumSegments := 100;

  // Calculate and add vertices to approximate the circular shape
  for i := 0 to NumSegments - 1 do
  begin
    Angle := i * 2 * Pi / NumSegments;
    Vertices.AddVertex(CenterX + Radius * Cos(Angle), CenterY + Radius * Sin(Angle));
  end;

  // Update the board shape
  BoardShape.UpdateVertices;
end;

错误原因分析

出现“未声明标识符”错误核心有3点:

  • 未初始化T2DVertexes实例:原代码仅声明Vertices变量但未创建实例,直接调用AddVertex会导致方法无法识别。
  • 接口方法调用逻辑错误:IPCB_BoardShape的顶点操作需通过其内部的Vertices集合执行,原代码直接调用BoardShape.RemoveAllVertices不符合Altium官方接口规范;且板框更新的方式也不符合要求。
  • 缺少核心单元引用:Altium脚本必须引用PCBBase、PCBServer等基础单元,否则无法识别内置接口与方法。

修正后的脚本代码

以下是符合Altium Designer脚本规范的圆形板框实现代码,已修复所有标识符错误:

// 必须引用的核心单元
uses
  PCBBase, PCBServer;

procedure CreateCircularBoardOutline;
var
  PCBBoard: IPCB_Board;
  BoardShape: IPCB_BoardShape;
  VertexList: T2DVertexes;
  CenterX, CenterY, Radius: TCoord;
  Angle: Double;
  NumSegments: Integer;
  i: Integer;
begin
  // 获取当前PCB文档
  PCBBoard := PCBServer.GetCurrentPCBBoard;
  if PCBBoard = nil then Exit;

  // 获取板框对象
  BoardShape := PCBBoard.BoardOutline;
  if BoardShape = nil then Exit;

  // 创建顶点集合实例
  VertexList := T2DVertexes.Create;
  try
    // 清除板框原有顶点
    BoardShape.Vertices.Clear;

    // 定义圆心坐标和半径(转换为Altium内部坐标单位)
    CenterX := MilsToCoord(5000);
    CenterY := MilsToCoord(5000);
    Radius := MilsToCoord(5000);

    // 圆形分段数(数量越多越接近完美圆形)
    NumSegments := 100;

    // 计算并添加圆形顶点
    for i := 0 to NumSegments - 1 do
    begin
      Angle := i * 2 * Pi / NumSegments;
      VertexList.AddVertex(CenterX + Radius * Cos(Angle), CenterY + Radius * Sin(Angle));
    end;

    // 将计算好的顶点赋值给板框,并更新
    BoardShape.Vertices.Assign(VertexList);
    PCBBoard.BoardOutline := BoardShape;

    // 刷新PCB文档,使修改生效
    PCBBoard.ViewManager.FullRefresh;
  finally
    // 释放顶点集合资源
    VertexList.Free;
  end;
end;

// 执行脚本
begin
  CreateCircularBoardOutline;
end.

关键修正点说明

  • 添加单元引用:在脚本开头添加uses PCBBase, PCBServer;,确保Altium能识别所有内置接口和方法。
  • 初始化并释放顶点集合:通过VertexList := T2DVertexes.Create;创建实例,使用完毕后在finally块中释放资源,避免内存泄漏。
  • 修正板框顶点操作逻辑:使用BoardShape.Vertices.Clear清除原有顶点,通过BoardShape.Vertices.Assign(VertexList)将新顶点集合赋值给板框,符合Altium接口规范。
  • 添加视图刷新:调用PCBBoard.ViewManager.FullRefresh强制刷新PCB视图,确保修改即时显示。

内容的提问来源于stack exchange,提问作者user22049962

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 08:38:22