关键词

概览

了解 Altium Designer 脚本系统支持的 DelphiScript 关键字,通过声明、说明和示例掌握其用法。本参考文档涵盖运算符、条件语句、循环、函数和异常处理,并指出 DelphiScript 无类型语言的相关限制。继续阅读,加深理解,查找编写脚本所需的关键字使用指南。

- 学习常用 DelphiScript 关键字的语法和行为。
- 查看控制流、声明和错误处理的示例。
- 了解语言限制,包括不支持开放数组声明和函数指针。
使用 AI 深入了解:
 

本参考资料介绍 Altium Designer 脚本系统中使用的 DelphiScript 关键字。该脚本系统支持 DelphiScript 语言,它与 Embarcadero Delphi™ 编程语言非常相似。主要区别在于 DelphiScript 是一种无类型脚本语言。

有关 DelphiScript 与 Delphi 编程语言之间差异的详细信息,请参阅DelphiScript 与 Delphi 的区别页面。

本节概述常用的 DelphiScript 关键字,并提供详细信息和示例。有关更多关键字信息,例如 Delphi 文件、数学和字符串处理例程,请参阅 函数和 Embarcadero Delphi 参考资料。

And

Declaration
And 运算符执行逻辑与或按位与运算。

Description
如果操作数为布尔类型,And 运算符执行逻辑与运算;如果操作数为整数,则执行按位与运算。

Example of a boolean And evaluation:

Var
  I, J : Integer
Begin
  I := $F0;
  J := $8F;
  ShowMessage(IntToStr(I and J));
End;

Example of a logical And evaluation:

Var
  S : String;
Begin
  S := '';
  If (Length(S) > 0) and (S[1\] = 'X') Then Delete(S,1,1);
End;

See also
Or 关键字
Xor 关键字

Array

Declaration
Array [index range];

Description
DelphiScript 是一种无类型语言,因此需要指定数组的大小或范围。仍然可以定义数组可容纳的元素类型,但这并非必需。不支持 Open array 声明。

Example:

var x : array [1..2]; 

Begin

Declaration
Begin
  statement
End;

Description
begin 关键字用于开始脚本中的一个语句块。语句块是脚本的主体,可以包含任意数量的语句,并且可以用于任何需要单条语句的位置,例如条件语句或循环语句的主体。

Example:

Var
  Test : Integer;
Begin
  Test := Test + 4;
  ShowMessage(IntToStr(Test));
End;

See also
End 关键字

Break

Declaration
Break;

Description
Break 用于跳出循环,类似于 Goto 语句。

Example:

While Condition Do
Begin
  DoSomething;
  Begin
    If AnotherCondition Then
      Break;
  End;
End;

See also
While 关键字
Continue 关键字
Do 关键字
Repeat 关键字

Case

Declaration
Case expression Of
  Value range : Expression;
Else Expression;
End;

Description
Case 语句根据表达式的值,从多个可能的分支中选择一个分支。

在许多情况下,可以用 Case 语句替换一组复杂的 If 语句,从而简化代码。表达式中的 case 语句用于选择某个值、一组可能的值或一个值范围。由于 DelphiScript 是一种无类型语言,因此可以在 case 语句中使用任何类型。Case 语句可以包含一个 else 语句,当没有任何标签与选择器(Case Of 条件中)的值匹配时,就会执行该语句。

Example 1:

Case Char Of
  '+'     : Text := 'Plus sign';
  '-'     : Text := 'Minus sign';
  '*', '/': Text := 'Multiplication or division';
  '0'..'9': Text := 'Number';
  'a'..'z': Text := 'Lowercase character';
  'A'..'Z': Text := 'Uppercase character';
Else
  Text := 'Unknown character';
End;

Example 2:

Case UserName Of
  Jack', 'Joe' : IsAdministrator := true;
  'Fred' : IsAdministrator := false;
Else
  Raise('Unknown User');
End;

See also
Of 关键字

Continue

Declaration
Continue

Description
Continue 语句用于跳过循环体,类似于 Goto 语句;

Example:

Var
  I := 0; s:= 1;
Begin
  While True Do
  Begin
  S := S \* 2;
  I := I \\+ 1;
  If I <> 4 then continue;
  Break;
End;

See also
Break 关键字
While 关键字
Continue 关键字
Do 关键字
Repeat 关键字

Const

Declaration
Const
Name = Expression;

Description
Const 关键字将任意常量值表达式指定为常量的值。如果尝试在脚本中修改 const 类型的表达式,脚本系统将抛出“未声明的标识符”错误。

Example:

Const
  b = 30;
Begin
  ShowMessage(IntTtStr(b));
End;

尝试更改 b const 参数的值会产生错误,例如:

Const
  b = 30;
Begin
  b := 40;
  ShowMessage(IntToStr(b));
End;

Div

Declaration
dividend div divisor

Description
Div 运算符执行整数除法,直接舍弃小数部分而不进行四舍五入。如果除数为零,DelphiScript 会报告错误。

See also
Mod 运算符
Div 运算符

Do

Declaration

  • For variable := expression1 to expression2 do 语句
  • While expression do 语句
  • With expression do 语句。

Description
Do 关键字是 DelphiScript 的 For, While and With 语句的一部分。

Example:

For i := 0 To AnIndex - 1 Do
  S := S + #13 + AString;

See also
For 关键字
To 关键字
While 关键字
With 关键字
DownTo 关键字

DownTo

Declaration
For variable := expression1 DownTo expression2 Do statement.

Description
在 For 循环中使用 DownTo 进行递减计数。

See also
For 关键字
To 关键字
Do 关键字

Else

Declaration

  • If condition then statement Else statement
  • Try statement except exception Else statement end
  • Case expression of Else end;

Description
Else 关键字用于引出多种语句中处理所有其他情况的部分。请注意,if 语句的 else 部分后面只能跟一条语句,而 try-except 和 case 语句的 else 部分可以包含多条语句。

See also
If 关键字
Then 关键字
Try 关键字
Case 关键字

End

Declaration

  • Begin statements End;
  • Try statements Except Exception clauses... else Statements... End;
  • Try statements Finally statements End;
  • Case Expression of clauses Else statements... End;

Description
End 关键字用于结束语句块或由多个部分组成的结构,例如声明、Case 语句等。

See also
Begin 关键字
Case 关键字
Try 关键字

Except

Declaration
Try statements Except statements End;

Description
使用 Try-Except 块处理异常情况,例如捕获特定异常并对其进行有效处理,如将其添加到错误日志或创建友好的对话框。由于 DelphiScript 不支持 On 关键字,请在 Except 块中使用 Raise 语句,并仅报告文本消息。

Example:

Try
  X := Y/Z;
Except
  Raise('A divide by zero error!');
End;

See also
End 关键字
Finally 关键字
Try 关键字

Finally

Declaration
Try statements... Finally statements... End;

Description
finally 关键字用于开始 try-finally 块的 finally 部分。无论控制流程以何种方式离开 try 块(异常、exit 或 break),finally 块中的语句始终都会执行。处理对象的创建/销毁和文件 I/O 时,建议使用 try-finally 块。

See also
End 关键字
Raise 关键字
Try 关键字

For

Declaration

  • for variable := expression1 to expression2 do statement
  • for variable := expression1 downto expression2 do statement

Description
for 循环先对指定循环边界的表达式求值,然后通过循环控制变量重复执行循环体,该变量在每次迭代后都会更新。

Example:

For i := 0 to AnIndex - 1 Do
Begin
  S := S + #13 + AString;
End;
ShowMessage(S);

See also
Do 关键字
DownTo 关键字
Repeat 关键字
To 关键字
While 关键字
With 关键字

Forward

Declaration
subroutine header; forward;

Description
Forward 指令允许通过带有 forward 指令的头部声明(名称、参数和返回类型),在调用函数或过程之前对其进行声明。

Function

Declaration
Function name (parameters) : return type;

Description
Function 是返回值的子程序。请注意,脚本中不允许使用函数指针,也就是说,不能定义函数类型。在函数内部声明的变量无法在该过程外部访问。

Example

Function TestFunc(Min, Max : integer) : integer;
Begin
  Result := Random(Max - Min +1);
End;

Goto

Declaration
goto label

Description
goto 语句将控制权转移到指定标签。标签可以是任意标识符,也可以是最多包含四位数字的数字字符串。

Example

Label StartHere;
// code
 
StartHere: //do anything;
 
Goto StartHere;

See also
Label 关键字

If

Declaration

  • if condition then statement;
  • if condition then statement1 else statement2;

Description
If 关键字的条件必须是布尔表达式。Else 关键字是可选的。

Example

If A > B Then
    ShowMessage('X>Y and A > B');
Else
    ShowMessage('X>Y and A <=B');
End;

See also
And 关键字
Begin 关键字
Or 关键字
Then 关键字
Else 关键字

Interface

Declaration
Interface
// Globally unique identifier string.
Methods
Properties
End;

Description
interface 关键字用于访问内存中的现有对象并调用该对象的方法。接口只能包含属性和方法,不能包含数据。由于接口不能包含数据,其属性必须通过方法进行读写。最重要的是,接口不包含实现,它仅定义与内存中现有对象交互的契约。

接口可以看作与计算机内存中现有对象的连接点,并提供通过接口属性读写数据的能力。接口向其关联的对象请求数据。

DelphiScript 是一种无类型语言,因此无法定义新类型,例如新的记录、数组或类,以及相关接口。

请注意 Interface 关键字的另一种用法:它用于 Embarcadero Delphi 单元的 Interface/Implementation 部分。这些 Interface/Implementation 关键字可以在脚本中使用,但在 Altium Designer 中执行脚本时,它们实际上会被忽略。

Label

Declaration
label digits, identifier, ...;

Description
label 关键字用于声明一个或多个标签。标签可以是最多包含四位数字的数字字符串,也可以是标识符。标签可在同一程序块中用于将某条语句标识为 goto 语句的跳转目标。

Example:

Label StartHere;
// code
StartHere: //do anything;
Goto StartHere;

See also
Goto 关键字

Mod

Declaration
Integer expression mod integer expression

Description
mod 运算符用于执行整数取模或求余运算。A mod B 的结果为 A - (A div B) * B。

See also
Div 函数

Nil

Declaration
const nil = pointer(0);

Description
nil 关键字是一个特殊的指针值,保证具有唯一性且不指向任何对象。

Not

Declaration

  • not boolean expression
  • not integer expression

Description
not 运算符用于执行取反运算。如果操作数为布尔类型,则执行逻辑取反。Not False = True 和 not true = false。如果操作数为整数,not 运算符会对该整数值的每一位执行按位取反,即求反码运算。

Of

Declaration
case expression of
  selector: expression1
  ...
end

Description
Of 关键字用于 case 语句。

See also
Case 语句

Or

Declaration

  • boolean expression or boolean expression
  • integer expression or integer expression

Description
如果操作数为布尔类型,or 运算符执行逻辑或运算;如果操作数为整数,则执行按位或运算。只有当两个操作数均为 false 时,逻辑或的结果才为 false;否则,只要至少有一个操作数为 true,结果就为 true。

See also
And 关键字
Not 关键字
Shl 关键字
Shr 关键字
Xor 关键字

Procedure

Declaration

  • Procedure name;
  • Procedure Name (Parameter, ...);

Description
procedure 关键字用于声明没有返回类型的子程序。在过程内部声明的变量无法从该过程外部访问。请注意,虽然可以使用此关键字,但脚本系统会将其忽略。

Example:

Procedure TestRand(Var Rand: Integer; Max : Integer);
Begin
  Rand := Random(Max);
End;

See also
Function 关键字

Program

Declaration
Program Name;
declarations...
Block

Description
program 关键字用于标记脚本的开始。脚本的文件扩展名为 *.pas。请注意,虽然可以使用此关键字,但脚本系统会将其忽略。

See also
Function 关键字

Raise

Declaration
Raise statement;

Description
raise 关键字与 Try 关键字相关。Raise 关键字不带参数使用时,可重新引发上一次异常。也可以为其提供字符串参数,以使用指定消息引发异常。

Example:

Raise(Format('Invalid Value Entered : %d', \[Height])); 

请注意,DelphiScript 不支持 On 关键字,因此无法在脚本中使用 Exception 对象。

Repeat

Declaration
repeat
statements;
until boolean expression

Description
Repeat Until 块内的语句会反复执行,直到布尔表达式为 true。

Example:

Repeat
  Write('Enter a value (0..9): ');
  ShowMessage(IntToStr(I));
Until (I >= 0) and (I <= 9);

See also
Until 关键字

Result

Declaration
Var result : Function return type;

Description
脚本中的每个函数都必须使用 Result 关键字返回结果值。该变量的类型就是函数的返回类型。

See also
Function 关键字

Shl

Declaration
value shl bits

Description
shl 运算符将整数值向左移动 Bits 位。右侧空出的位用零填充。

See also
And 关键字
Not 关键字
Or 关键字
Shr 关键字
Xor 关键字

Shr

Declaration
value shr bits

Description
shr 运算符将整数值向右移动 Bits 位。左侧空出的位用零填充。

See also
And 关键字
Not 关键字
Or 关键字
Shl 关键字
Xor 关键字

String

Declaration

  • type string;
  • type Name = string[Constant]

Description
string 关键字表示字符串类型。

Then

Declaration
If expression then statement

Description
Then 关键字是 If 语句的一部分。
See also
If 关键字

To

Declaration
For variable := expression1 to expression2 do statement

Description
to 关键字是递增计数的 for 循环的一部分。

Example

For i := 0 to AnIndex - 1 do
  S := S + #13 + AString;

See also
Downto 关键字
For 关键字

Try

Declaration

  • Try statements finally statements end;
  • Try statements except statements end;

Description
try 关键字用于引入 try-except 语句或 try-finally 语句。这两种语句彼此相关,但用途不同。

Try Finally
无论控制流程以何种方式离开 try 块(异常、Exit 或 Break),finally 块中的语句都会执行。使用 try-finally 块可以释放临时对象及其他资源,并执行清理操作。通常,一个子程序中不需要使用多个 try-finally 语句。

Example:

Reset(F);
Try
  ...  // process file F
Finally
  CloseFile(F);
End;

Try Except
使用 try-except 处理异常情况,例如捕获特定异常并进行适当处理,如将其添加到错误日志中或创建友好的对话框。由于 DelphiScript 不支持 On 关键字,请在 Except 块内使用 Raise 语句。

Example:

Try
  X := Y/Z;
Except
  Raise('A divide by zero error!');
End;

See also
Raise 关键字

Type

Declaration
Type Name = type declaration ...

Description
Type 关键字用于声明变量的类型。由于 DelphiScript 是一种无类型语言,因此无需将变量声明为特定类型,但可以这样做以提高脚本的可读性。脚本中的所有变量始终都是 Variant 类型。编写脚本时的主要限制是无法声明记录或类。

脚本会忽略类型转换,因此变量声明中的类型也会被忽略,可以省略。所以,以下声明是正确的:

Example:
var a : integer;
var b : integer;
var c, d;

过程/函数声明中的参数类型会被忽略,可以省略。例如,以下代码是正确的:

Function sum(a, b) : integer;
Begin
  Result := a + b;
End;

通常,Variant 变量可用于存储任何数据类型,并执行多种运算和类型转换。Variant 变量的类型检查和计算在运行时进行。编译器不会对代码中可能存在的错误发出警告,这些错误只能通过充分测试才能发现。总体而言,使用 Variant 变量的代码部分可以视为解释执行的代码,因为许多运算只有在运行时才能确定。这可能影响代码的执行速度。

Example:

Var
V
Begin
  // you can assign to it values of several different types:
  V := 10;
  V := 'Hello, World';
  V := 45.55;
End;

See also
Var 关键字

Unit

Declaration

  • Unit Name;
    interface
       declarations
    implementation
       declarations
       statements
    Initialization
       statements
    finalization
       statements
    end.

     
  • Unit Name;
    interface
       declarations
    implementation
       declarations
       statements
    begin
       statements
    end.

unit 关键字用于引入单元,单元是脚本的基本模块。请注意,虽然可以使用此关键字,但脚本系统会将其忽略。

See also
Function 关键字
Program 关键字

Until

Declaration
Repeat
Statements;
Until boolean expression

Description
until 关键字标记 Repeat-Until 块的结束。Repeat-Until 块内的语句会反复执行,直到布尔表达式为 true。

Example:

Repeat
  Write('Enter a value (0..9): ');
  ShowMessage(IntToStr(I));
Until (I >= 0) and (I <= 9);

See also
Repeat 关键字

Uses

Declaration
Uses Unit Name, ...;

Description
uses 关键字用于列出导入到当前单元中的单元名称。uses 声明是可选的,因为脚本系统已经支持 Altium Designer 中导入的单元。可以包含 uses 声明,以提高可读性。

同一项目中存储的所有单元都可以访问其中任意单元的全局变量。在同一项目的各个单元中声明变量时,请牢记这一点。

Altium Designer 的 Client、PCB、Schematic 和 WorkSpace Manager API,以及 Delphi 的 SysUtils、Classes 和其他单元均已导入,可在脚本中使用,因此无需在脚本中专门声明这些单元。

See also

Var

Declaration
Name : Type
Name : Type = Expression;

DelphiScript Variables
脚本中的所有变量始终为 Variant 类型。强制类型转换会被忽略。变量声明中的类型会被忽略,也可以省略,因此以下声明均正确:
Var a : integer;
Var b : integer;
Var c, d;

过程/函数声明中的参数类型会被忽略,也可以省略。例如,以下代码是正确的:

Function sum(a, b) : integer;
Begin
  Result := a + b;
End;

一般而言,Variant 可用于存储任何数据类型,并执行多种运算和类型转换。Variant 的类型检查和计算在运行时进行。编译器不会对代码中可能存在的错误发出警告,这些错误只能通过充分测试来发现。总体而言,使用 Variant 的代码部分可以视为解释执行的代码,因为许多运算只有在运行时才能确定。这可能会影响代码的执行速度。

声明 Variant 变量:

Var
  V;
Begin
  // you can assign to it values of several different types:
  V := 10;
  V := 'Hello, World';
  V := 45.55;
End;

Array elements
数组元素的类型会被忽略,也可以省略,因此以下声明是等效的:
Var x : array [1..2] of double;
Var x : array [1..2];

Illegal array example:
Type
  TVertices = Array [1..50] Of TLocation;
Var
  NewVertices : TVertices;

Legal array example:
Var
  NewVertices : Array [1..50] of TLocation;

While

Declaration
while expression do statement

Description
只要表达式为真,while 语句就会反复执行指定语句。

See also
Break 关键字
Continue 关键字
Do 关键字
DownTo 关键字
For 关键字
Repeat 关键字
To 关键字
With 关键字

With

Declaration
with expression do statement

Description
With 语句将记录、对象、类或接口引用添加到用于解析符号名称的作用域中。

Normal version example:

Form.Canvas.Pen.Width := 2;
Form.Canvas.Pen.Color := clSilver;

With version example:

With Form.Canvas.Pen do
Begin
  Width := 2;
  Color := clSilver;
End;

See also
Do 关键字

Xor

Declaration

  • 布尔表达式 Xor 布尔表达式
  • integer expression Xor integer expression

Description
xor 运算符对其操作数执行异或运算。如果操作数为布尔类型,则返回布尔结果:操作数不同时为 true,相同时为 false。

整数 xor 对操作数的每一位执行运算:如果两个操作数的对应位不同,则将结果位设置为 1;如果对应位相同,则将结果位设置为 0。如果一个操作数的位数少于另一个,则在位数较少的操作数左侧用 0 补齐。

See also
And 关键字
Not 关键字
Or 关键字
Shl 关键字
Shr 关键字

AI-LocalizedAI 翻译
如您发现任何问题,请选中相关文本/图片,并按 Ctrl + Enter 键向我们提交反馈。
Nav
Contents
功能可用性

您可使用的功能取决于您所采用的 Altium 解决方案——Altium Develop、Altium Agile Teams、Altium Agile Enterprise,或 Altium Designer(在有效订阅期内)。

如果您在实际软件中未看到文档中介绍的某项功能, 请联系 Altium Sales 了解更多信息。

旧版文档

Altium Designer 文档不再提供版本区分。如果您需要访问 Altium Designer 旧版本的文档,请前往其他安装程序页面的旧版文档部分。

Contents