关键词

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

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

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

还有

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 关键字

阵列

Declaration
Array [index range];

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

Example:

var x : array [1..2]; 

开始

Declaration
Begin
statement
End;

Description
begin 关键字在脚本中开始一个代码块。代码块是脚本的主体,可以包含任意数量的语句,也可用于任何需要单条语句的地方,如条件语句或循环语句的主体。

Example:

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

See also
结束关键字

休息

Declaration
Break;

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

Example:

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

See also
虽然关键词
继续关键字
执行关键字
重复关键字

案例

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
关键词

继续

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
断开关键字
同时关键字
继续关键字
Do 关键字
重复关键字

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;

分部

Declaration
dividend div divisor

Description
Div 运算符执行整数除法,其结果将舍弃小数而不四舍五入。如果除数为零,DelphiScript 将报错。

See also
Mod 运算符
除法运算符

Declaration

  • For variable := expression1 to expression2 do声明
  • While expression do声明
  • With expression do声明。

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

Example:

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

See also
用于关键词
至关键字
While 关键字
有关键字
DownTo 关键字

向下

Declaration
For variable := expression1 DownTo expression2 Do statement.

Description
For 循环中使用 DownTo 进行倒计时。

See also
对于关键字
至关键字
执行关键字

其他

Declaration

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

Description
Else 关键字引入了多个语句的 catch all 部分。请注意,if 语句的else 部分后面只有一条语句,但try-exceptcase 语句的else 部分可以有多条语句。

See also
如果关键字
则关键字
尝试关键字
以关键字为例

结束

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
开始关键字
Case 关键字
尝试关键字

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
结束关键字
最后关键字
尝试关键字

最后

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

Description
finally 关键字启动try-finally 程序块的 finally 部分。无论控制如何离开try 块异常、退出或中断,finally 块中的语句始终运行。在处理对象的创建/销毁和文件 IO 时,建议使用try-finally 块。

See also
结束关键字
Raise 关键字
尝试关键字

对于

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
做关键字
DownTo 关键字
重复关键字
To 关键字
While 关键字
使用关键字

转发

Declaration
subroutine header; forward;

Description
Forward 指令允许在调用函数或存储过程之前,通过forward 指令声明头(名称、参数和返回类型)来声明该函数或存储过程。

功能

Declaration
Function name (parameters) : return type;

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

Example

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

后藤

Declaration
goto label

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

Example

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

See also
标签关键字

如果

Declaration

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

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

Example

如果 A > B 那么
    ShowMessage('X>Y and A > B');
否则
    ShowMessage('X>Y and A =B');
End;

另请参阅

And 关键字

开始关键字
Or 关键字
然后关键字
Else 关键字

界面

界面

声明
接口

Interface

描述

接口关键字允许访问内存中的现有对象。 interface 关键字允许访问内存中的现有对象并调用该对象的方法。接口只能包含属性和方法,不能包含数据。由于接口不能包含数据,其属性必须写入方法或从方法中读取。最重要的是,接口没有实现,因为它们只定义了与内存中现有对象的契约。

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

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

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

标签

Declaration
label digits, identifier, ...;

Description
label 关键字声明一个或多个标签。标签可以是最多四位数的数字字符串,也可以是一个标识符。标签可在同一程序块中使用,以标识作为goto 语句目标的语句。

Example:

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

See also
转到关键字

模式

Declaration
Integer expression mod integer expression

Description
mod 运算符执行整数调制或余数运算。A mod B 的结果是A - (A div B) * B

See also
除法函数

Declaration
const nil = pointer(0);

Description
nil 关键字是一个特殊的指针值,它保证是独特的,并且不指向任何东西。

没有

Declaration

  • not boolean expression
  • not integer expression

Description
not 运算符执行否定。如果操作数是布尔类型,否定就是逻辑否定。Not False = Truenot true = false 。如果操作数是整数,not 运算符对整数值中的每一位进行按位否定,即补码运算。

Declaration
case expression of
selector: expression1
...
end

Description
Of 关键字用于case 语句。

See also
案例声明

或者

Declaration

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

Description
如果操作数是布尔类型,or 运算符执行逻辑 Or;如果操作数是整数,则执行位 Or。逻辑 Or 只有在两个操作数都为假的情况下才为假,否则,当至少有一个操作数为真时才为真。

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

程序

Declaration

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

Description
procedure 关键字声明了一个没有返回类型的子程序。在存储过程中声明的变量在该存储过程之外是不可访问的。请注意,该关键字可以使用,但会被脚本系统忽略。

Example:

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

See also
功能关键字

计划

Declaration
Program Name;
declarations...
Block

Description
program 关键字是脚本的开头。脚本的文件扩展名为*.pas 。请注意,该关键字可以使用,但会被脚本系统忽略。

See also
功能关键字

提高

Declaration
Raise statement;

Description
raise 关键字与Try 关键字相关。Raise 关键字可在不带参数的情况下使用,以重新引发上一个异常。该关键字还可与字符串参数一起使用,通过特定信息引发异常。

Example:

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

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

重复

Declaration
repeat
statements;
until boolean expression

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

Example:

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

See also
直到关键字

结果

Declaration
Var result : Function return type;

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

See also
函数关键字

Shl

Declaration
value shl bits

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

See also
和关键字
非关键字
或关键字
Shr 关键字
Xor 关键字

嘘声

Declaration
value shr bits

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

See also
和关键字
非关键字
或关键字
Shl 关键字
Xor 关键字

字符串

Declaration

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

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

那么

Declaration
If expression then statement

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

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
向下关键字
关键词

尝试

Declaration

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

Description
try 关键字引入try-except 语句或try-finally 语句。这两个语句相互关联,但目的不同。

Try Finally
无论控制如何离开 try 块异常、退出或断开,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
提高关键词

类型

Declaration
Type Name = type declaration ...

Description
Type 关键字声明了变量的类型。由于 DelphiScript 是一种无类型语言,因此没有必要声明变量的特定类型,但为了脚本的可读性,可以这样做。脚本中的所有变量都是变量类型。编写脚本的主要限制是不能声明记录或类。

脚本中忽略了类型转换,因此变量声明中的类型会被忽略,可以跳过。因此,这些声明是正确的:

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

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

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

一般来说,变量可用于存储任何数据类型,并执行大量操作和类型转换。变体在运行时进行类型检查和计算。编译器不会对代码中可能出现的错误发出警告,这些错误只能通过大量测试才能发现。总的来说,使用变体的代码部分可被视为解释型代码,因为许多操作要到运行时才能解决。这会影响代码的运行速度。

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
变量关键字

单位

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
功能关键字
程序关键字

直到

Declaration
Repeat
Statements;
Until boolean expression

Description
until 关键字标志着Repeat-Until 程序块的结束。Repeat-Until 程序块内的语句会重复执行,直到布尔表达式为真。

Example:

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

See also
重复关键字

用途

Declaration
Uses Unit Name, ...;

Description
uses 关键字列出了导入周围单元的单元名称。uses 声明是可选的,因为脚本系统支持在 Altium Designer 中导入的单元。为了便于阅读,可以包含 uses 声明。

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

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

See also

变量

Declaration
Name : Type
Name : Type = Expression;

DelphiScript Variables
脚本中的所有变量都是变量类型。类型转换被忽略。变量声明中的类型会被忽略,可以跳过,因此这些声明是正确的:
Var a : integer;
Var b : integer;
Var c, d;

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

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

一般来说,变量可用于存储任何数据类型,并执行大量操作和类型转换。变体在运行时进行类型检查和计算。编译器不会对代码中可能出现的错误发出警告,这些错误只能通过大量测试才能发现。总的来说,使用变体的代码部分可被视为解释型代码,因为许多操作要到运行时才能解决。这会影响代码的运行速度。

声明变量:

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;

虽然

Declaration
while expression do statement

Description
while 语句在表达式为真时重复执行语句。

See also
Break 关键字
继续关键字
Do 关键字
DownTo 关键字
For 关键字
重复关键字
至关键字
使用关键字

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
关键字

Xor

Declaration

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

Description
xor 运算符对其操作数执行排他或运算。如果操作数是布尔类型,则返回布尔结果--如果操作数不同,则返回 true;如果操作数相同,则返回 false。

整型运算符xor 对操作数的每个位进行运算,如果两个操作数的相应位不同,则将结果位设置为1 ;如果两个操作数的位相同,则将结果位设置为 0。如果一个操作数小于另一个操作数,那么较小的操作数的最左边位将扩展为0

See also
和关键字
非关键字
或关键字
Shl 关键词
Shr 关键字

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

您可使用的功能取决于您所选择的 Altium 解决方案 —— Altium DevelopAltium Agile(Agile Teams 或 Agile Enterprise 版本),或仍在有效订阅期内的 Altium Designer。

如果您在软件中未找到文中提及的功能,请联系 Altium 销售团队了解更多信息。

旧版文档

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

Content