Uploaded by Mahdi Abbasi

PML Basics

advertisement
PML Basics
The AVEVA Programmable Macro Language
Author: Romel E. Daguplo
Email: romeldhagz@gmail.com
Introduction



PML stands for Programmable Macro Language
It is a programming language for AVEVA PDMS
Customization
Difference between Macros and Programmable
Macros:
 Macros
are ASCII files containing PDMS command in
sequence
 Programmable Macros are macros containing
program constructs such as IF statements and loops
Note: Solutions of the exercises are provided which are attached
together with this guide.
PML File Hierarchy
des
gen
PML 1
PML 2
pdmsui
pmllib
dra
admin
pipe
adm
equi
clib
functions
forms
objects
xxx.pmlfnc
xxx.pmlfrm
xxx.pmlobj
PML 2




Designed to be easier to read and easier to write
Most PML1 macros will still run under PML2
Contains many new features not in PML1
Is more like other modern object-oriented languages
(C++, Java, Smalltalk, etc.)
 Provides
classes of built-in, system-defined and userdefined object types
 Main deficiency is that it lacks inheritance


Objects have members (their own variables) and
methods (their own functions)
Operators and methods are polymorphic
 What
they do (their behavior) depends on the type of
the variable
PML 2 Variables


Used to store values
Are objects
 Has



May be up to 16 characters long and can contain alpha
and numeric characters
NEVER start a variable name with a number
NEVER use dot (.) in variable names
 Has

unique name
set of functions called methods
Has an Object type when created
PML 2 Variables

PML 2 is supplied with

Built-In Object Types in PML
 built-in
object types
 system-defined object types
 and you can also define your own user-defined
object types
 String
– holds any text
 Real – used for numeric values including do loop
counters
 Boolean – for results of logical expressions and holds
the value TRUE or FALSE
 Array – holds many values of any type
PML 2 Variables

System-defined Object Types
 Position
 Orientation
 Direction
 Bore
 Dbref
 Pointvector
 etc…
PML 2 Variables

User-defined Object Types
Examples:
define object COMPANY
member .Name
is STRING
member .Address
is STRING
member .Employees is REAL
endobject
define object SERVICES
member .Description is STRING
member .Cost
is REAL
member .Owner
is COMPANY
endobject
PML 2 Variables

User-defined object type definitions should normally
stored in a file with a lowercase name matching the
name of the object type and a .pmlobj suffix in
PMLLIB search-path directory
Example: For object COMPANY, it should be stored in a
file named company.pmlobj
PML 2 Variables
 Types of Variables
 GLOBAL


Lasts for a whole session (or until you delete them)
Represented by two exclamation points (!!)
 LOCAL


Variables
Variables
Within one PML function or macro
Represented by one exclamation point (!)
Examples:
!!var
!var
 Global
 Local
PML 2 Variables

Other examples:
 creates !!number as a
GLOBAL REAL variable
!name = ’Romel’  creates !name as a
!!number = 42
!grid = TRUE
LOCAL STRING variable
 creates !grid as a LOCAL
BOOLEAN variable
PML 2 Variables

Giving variables a type:
!!number
!name
!grid
!dimensions

=
=
=
=
REAL()
STRING()
BOOLEAN()
ARRAY()
Creating Other Types of Variable
!owner = object COMPANY()
Text Delimiters

Text strings must be enclosed in either single
quotes or vertical bars
Example:
’This is a text string.’
|This is a text string.|
!str = ’text’
!str = |text|
!str = |’text’|
PML 2 Expressions

Introduces enhanced facilities for expressions
if and do commands
 When giving a PML variable a value using = sign
 In
Example:



!value = !number
May be of any complexity
May contain calls to PML Functions and Methods
May include form gadget values
Example:
!value = !!MyFunction(!arg) * !!Form.Gadget.Val / !MyArray.Method()
PML 1 Expressions



Must still be used when an expression is used as an argument
to a command
Must be enclosed in () brackets
Must be preceded with $ to obtain their value:
var !value ($!number)

The result of a PML 1 expression, even if it is a
number, is of type STRING

The VAR assign a (STRING) value to a PML variable
var !value 99
var !newvalue ($!value + 1)
 !newvalue is now
the STRING ‘100’
PML 2 Expressions


Consists of operators and operands.
Format:
 There
must be a space before and after an operator.
!a + !b


+ is the operator
Operator Precedence
 Operators








are evaluated in the following order:
()
Functions
*/
+–
NE NEQ GT LT GE GEQ LE LEQ
NOT
AND
OR
PML 2 Expressions
 Expression operators
+
-/*
 LT GT EQ NE GT GE NOT AND OR
 SIN COS TAN SQR POW NEGATE ASIN ACOS ATAN LOG
ALOG ABS INT NINT
Examples:
!a = 30 * sin(45)
!b = pow(20,2)
 raises 20 to the power 2 (=400)
!c = (match(name of owner,’LPX’) gt 0)
PML 2 Expressions
 Operator Precedence Example:
!a = 2
!b = 2
!result = (!a + !b) * 2 EQ 8
Value of !result would be TRUE.
PML 2 Expressions

Boolean Operators – returns TRUE or FALSE
 EQ
- equal
 NE - not equal
 LT - lesser than
 GT - greater than
 LE or LEQ - lesser/lesser or equal
 GE or GEQ - greater/greater or equal
 NOT - TRUE if the expression is FALSE
 AND - TRUE if both expressions are TRUE
 OR - TRUE if either or both the expressions are TRUE
PML 2 Expressions

Concatenation Operator &
 Concatenates

STRING values (joins them end-to-end)
Result would be a STRING also
 Values
of any type are automatically converted to
STRING first before concatenation
Example:
!a = 64
!b = ’romel’
!c = !a & !b
Value of !c is a STRING ’64romel’.
PML 2 Expressions

Nesting Expressions
 Expressions
can be nested using brackets
Example:
((SIN(!angleA) * 2) / SIN(!angleB))
Control Logic

IF Construct
Example:
If (!x EQ ’romel’ OR !x EQ ’ella’) then
!result = 2
Elseif (!x EQ ’bruno’) then
!result = 1
Else
!result = 0
Endif
 Simplest
form:
If (!x EQ ’romel’) then
!result = TRUE
Endif
Control Logic

Expressions based on BOOLEAN operators that give
also a BOOLEAN result.
Example (associated with IF statement):
!pass = !value GT 0
If (!pass) then
…………
 PML
functions can also be placed in the expression:
If (!!MyFunction()) then
…………
If (!!MyFunction() GT 0) then
…………
 !!MyFunction() returns
BOOLEAN
 !!MyFunction() returns
REAL
Control Logic

DO loops
 Enables
a series of commands to be repeated more
than once
 Optionally controlled by a counter
Example:
do !x from 10 to 100
!total = !total + !x
enddo

Simplest form – will loop forever unless something in
the commands block stops the loop
do
commands block
enddo
Control Logic

Other DO loop example:
 Loop a specified value
Example:
do !x from 10 to 100 by 2
!total = !total + !x
enddo

Decrementing:
do !x from 10 to 1 by -1
……
enddo
Control Logic

Stopping a DO loop: break or break if
Example:
do !number
if (!number GT 100) then
break
endif
…………
enddo
do !number
break if (!number GT 100)
…………
enddo
Control Logic

Skipping commands in a DO loop: skip or skip if
Example:
do !number
if (!number EQ 4) then
skip
endif
…………
enddo
do !number
skip if (!number EQ 4)
…………
enddo
Control Logic

Jumping to a labelled line: golabel
Example:
if (!ok) then
golabel /MyLabel
else
……
endif
label /MyLabel
-- do something
Control Logic

Conditional Jumping to a Labeled Line
Example:
do !x
do !y to 3
!z = !x * !y
golabel /Finished if (!z gt 100)
!total = !total + !z
enddo
enddo
label /Finished
$P Total is $!total
Control Logic

Illegal Jumping
Example:
golabel /MyLabel
do !y to 3
!total = !total + !y
label /MyLabel
$P here
enddo
The following is an illegal jump into a nested do block.
Jumping to a block expression such as if and do is illegal.
Comments in PML Lines

One-line comment:
 Begins
with -- or $*
-- This is a new style PML comment
$* This is a comment

Inline comment:
!c = !a + !b

$* This is a comment
Several line comment:
 Enclosed
$(
in the escape sequence $( and $)
A comment containing more
than one line $)
Comments in PML Lines

Comment-out lines
 Prevent
a code to be executed w/out deleting from
the file
 One-line Comment and Several line Comment is
applicable
Example:
$(
skip if (!x eq !y)
$)
or
-- !skip if (!x eq !y)
The Special Character $

It is an escape character
 that
together with the character which follows it are
treated as a special instruction to PML
!strx = ’!!ce.type’
!dbrefx = $!strx

$P – used to output message to the screen
$P This text will be output to the screen
!num = 24
$P My age is $!num

As the last character on a line, $ means that the
next line is a continuation line
$P This is an example of a long $
message to output on screen
PML 2 Functions

Can optionally have ARGUMENTS
 May


be built-in object, user-defined object or ANY
Can optionally return values as their results
Functions that don’t return values are known as PML
Procedures
Example:
define function !!Area( !length is REAL, !width is REAL ) is REAL
!result = !length * !width
return !result
endfunction
PML 2 Functions

PML Procedure
A
PML function that does not return a result
Example:
define function !!Area( !length is REAL, !width is REAL,
!result is REAL )
!result = !length * !width
endfunction
!area = REAL()
!partlength = 7
!partwidth = 6
call !!Area(!partlength, !partwidth, !area)
!area now is equal to 42.
PML 2 Functions

Arguments of type ANY
Example:
define function !!AnyType( !input is ANY )
$P $!input
endfunction
Finding its actual type using ObjectType() method:
define function !!AnyType( !input is ANY )
!type = !input.ObjectType()
if (!type eq ’STRING’) then
…
elseif (!type eq ‘REAL’) then
…
else
…
endif
endfunction
RETURN Command

At any point within a PML File a return command
will stop further execution
Example:
if (!count EQ 0) then
return
endif
Storing PML 2 Functions






Loaded automatically when called via PMLLIB
Filename must have suffix .pmlfnc
!!CALCULATE or !!Calculate or !!calculate all
corresponds to calculate.pmlfnc
!! Signifies that the function is user-defined and
that it is global
All user-defined functions are global and only one
may be defined per file
define function must be the first line in the file and
that its name and the file name must correspond
Loading PML 2 Files


Must be stored in directories pointed to by the
PMLLIB environment variable
At start up:
scans all files in the PMLLIB
 pml.index is created
 All PML files listed in the pml.index file are loaded
 PML
Filename Extensions

The naming conventions are as follows:
 .pmlfnc
 for PML function definition files
 .pmlobj  for PML object type definition files
 .pmlfrm  for PML form definition files
Note: Filename extensions must be entered in lower case.
PML 2 Directives






pml rehash all - rebuild file index
pml index - updates pml.index
pml reload form !!formname – reload the form
definition file
kill !!formname – use if you experienced problems of
an edited form definition not being re-loaded
pml reload object ObjectName – reload an object
definition
pmlscan DirectoryName - updates pml.index without
running an AVEVA products
Exercises

Exercise 1:
Create a function that will convert a value from MM to INCH.
Function details:
!!convertUnit(!value is REAL, !mode is STRING) is REAL
Sample Usage:
!value = 20
!inch = !!convertUnit(!value, 'INCH')
!mm
= !!convertUnit(!value, 'MM')

Exercise 2:
Implement Exercise 1 as PML Procedure.
Sample Usage:
!value = 20
call !!convertUnitProcedure(!value, ’INCH')
$P !value 20 converted to MM equal $!value
Exercises

Exercise 3:
Implement Fibonacci number using PML function.
Fibonacci numbers are the numbers in the ff. sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
By definition, the first two Fibonacci numbers are 0 and 1, and each
remaining number is the sum of the previous two. Some sources omit the initial
0, instead beginning the sequence with two 1s.
Function details:
!!generateFibonacciSequence(!numshow is REAL) is STRING
Sample usage:
!numsequence = 7
!sequence = !!generateFibonacciSequence(!numsequence)
$P $!sequence
Output:
0, 1, 1, 2, 3, 5, 8
Exercises

Exercise 4:
Implement Hailstone number sequence using PML function.
The German mathematician, Lothar Collatz, proposed that for any number it's possible to make a sequence
of numbers that will eventually end in one by following a simple rule; if the number is even halve it by two, if
it's odd, times it by three and add one (e.g., starting with the number 5 the sequence would be 5 16 8 4 2 1).
The name hailstone comes from the way the pattern of numbers rise and fall, like a hailstone in a weather
cloud before it drops to the ground.
Function details:
!!generateHailstoneSequence(!value is REAL) is STRING
Sample usage:
!value = 17
!sequence = !!generateHailstoneSequence(!value)
$P $!sequence
Output:
17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
PML 2 Methods




Each object types has several methods
May optionally have arguments
Can optionally return values as their results
Reference:
 Vantage
Plant Design Software Customization Reference
Manual contains a list of all PML Methods
PML 2 Methods

STRING Object example using Length() method:
!name = ’ROMEL’
!numname = !name.Length()
!numname is equal to 5.
Note: Notice the dot separator between the name
of the variable and the name of the method.
PML 2 Methods

Converting a STRING to BOOLEAN
!myString = ’TRUE’
!myBoolean = !myString.Boolean()
if (!myBoolean) then
……………
PML 2 Methods

Method on User-Defined Object Types
Example:
define object MAN
member .Age is REAL
endobject
define method .Man()
!this.Age = 42
endmethod
define method .Answer() is REAL
return !this.Age
endmethod
define method .Answer(!value is REAL)
!this.Age = !value
endmethod
PML 2 Methods

These methods might be used in the following way:
 The method .Man() was
called automatically
!number = !manObj.Answer()  !number is set to value 42
!manObj = object MAN()
!manObj.Answer(40)
!number = !man.Answer()
 !number now is set to value 40
Note: When creating new object type, or change an existing
definition, it must be loaded by using the command;
pml reload object [NAME OF OBJECT]
PML 2 Method Overloading

Two or more methods on an object may share the
same name providing they have different arguments
will invoke the method with the arguments which
match the method call
 PML


To use a method with the same name as a member and
one argument of the same type to set the member’s
value.
!manObj.Answer(65)
To use a method of the same name as a member
returning a value of the same type but with no
arguments to get the member’s value.
!num = !manObj.Answer()
PML 2 Methods

Constructor Methods with Arguments
Example Object:
define object MAN
member .num is REAL
endobject
define method .Man(!value is REAL)
!this.num = !value + 5
endmethod
Main function:
!manObj = object MAN(40)
!value = !manObj.num
 !value now is 45
PML 2 Methods

Invoking a Method from Another Method
Example:
define object MAN
member .num is REAL
endobject
define method .Man(!value is REAL)
!this.num = !value + !this.addFive()
endmethod
define method .addFive() is REAL
return 5
endmethod
Main function:
!manObj = object MAN(40)
!value = !manObj.num

 !value now is 45
Invoking a method from another method can be attained using
!This.Methodname()
PML 2 Method Concatenation

Any number of methods can be combined
providing the passed data is valid at each stage
!line = ’how are you’
!newline = !line.UpCase().Split().Sort()
q var !newline
Output:
<ARRAY>
[1] <STRING> ’ARE’
[2] <STRING> ’HOW’
[3] <STRING> ’YOU’
Deleting PML 2 Variables

A variable that exists can be explicitly made
UNDEFINED with the Delete() method
Example:
!!x.Delete()
!y.Delete()
UNSET Values and
UNDEFINED Variables

UNSET indicates that a variable does not have a
value
if (Unset(!x)) then ……
if (Set(!x)) then ……
Using Method:
if (!x.Unset()) then ……
if (!x.Set()) then ……

UNDEFINED variable is one that does not exist
if (Undefined(!x)) then ……
if (Defined(!x)) then ……
Exercises

Exercise 5:
Create a function that will check if a word is a palindrome.
A palindrome is a word, phrase, number or other sequence of units that
can be read the same way in either direction (the adjustment of
punctuation and spaces between words is generally permitted).
Punctuation, capitalization, and spacing are usually ignored.
Examples: ’level’ ’rotator’ ’civic’ ’radar’ ’Step on no
pets’ ’Dammit, ’I’m mad!’ ’Go hand a salami I’m a lasagna
hog.’
Use STRING methods in implementing.
(Using ARRAY methods is not acceptable.)
Function details:
!!isStringPalindrome(!word is STRING) is BOOLEAN
Exercises

Exercise 6:
Create an Object that will calculate the product, quotient,
sum, and difference of two specified values.
Object name:
object CALC()
Sample Usage:
!num1 = 15
!num2 = 3
!calculate = object CALC(!num1, !num2)
!prod = !calculate.product()
!quot = !calculate.quotient()
!sum = !calculate.sum()
!diff = !calculate.difference()
Default values if values not specified are 4, 2 respectively.
Exercises

Exercise 7:
Create an Object that will count the number of vowels
and consonants in a specified string.
Object name:
object COUNTVC
Sample Usage:
!str
!cObj
!numvowels
!numconso
=
=
=
=
’Remember the one I told you?’
object COUNTVC(!str)
!cObj.countVowels()
!cObj.countConsonants()
Exercise 7 Solution

An Object that has a count vowels and count consonants
method.
define object COUNTVC
member .text
member .vowels
member .consonants
member .textlen
endobject
is
is
is
is
STRING
STRING
STRING
REAL
-- Constructor method
define method .countvc(!str is STRING)
!this.text
= !str.Lowcase()
!this.textlen
= !str.Length()
!this.vowels
= ’aeiou’
!this.consonants = ’bcdfghjklmnpqrstvwxyz’
endmethod
-- countVowels() Method
define method .countVowels() is REAL
return !this.count(!this.vowels)
endmethod
Solution Continuation…
-- countConsonants() Method
define method .countConsonants() is REAL
return !this.count(!this.consonants)
endmethod
-- Method to do the counting process either counting the vowels or the consonants
define method .count(!reference is STRING) is REAL
!cntr = 0
do !itr1 from 1 to !this.textlen
do !itr2 from 1 to !reference.Length()
if (!this.text.Substring(!itr1, 1) eq !reference.Substring(!itr2, 1)) then
!cntr = !cntr + 1
break
endif
enddo
enddo
return !cntr
endmethod
Arrays

A variable that can contain many values,
each of which is called an array element
Example:
!arr[1] = ’ROMEL’
!arr[12] = 50
An empty ARRAY:
!arr = ARRAY()
Arrays of Arrays
(Multi-dimensional Arrays)

An ARRAY may itself be an ARRAY
Example:
!forname = 1
!employee[1][!forname] = ’Romel’
!employee[1][2] = ’Daguplo’
!fullname = !employee[1][!forname] & ’ ’ & !employee[1][2]

Assigning an entire array to an array element:
Example:
!tempname[1] = ’Romel’
!tempname[2] = ’Daguplo’
!employee[3] = !tempname
!fullname = !employee[3][1] & ’ ’ & !employee[3][2]
Array Methods
Vantage Plant Design Software Customization Reference
Manual contains a list of all PML Methods

Are built-in functions for performing a variety of
operations on the array
Example:
!myArray[1]
!myArray[2]
!myArray[3]
!myArray[4]
=
=
=
=
’romel’
3
’gadz’
’gadz’
!nelements = !myArray.Size()
!felements = !myArray.Find(’gadz’)
!myArray.Clear()
Using VAR Command
for Arrays

Example:
 Sorting an ARRAY in alphabetical order:
!myArray[1]
!myArray[2]
!myArray[3]
!myArray[4]
=
=
=
=
’abcdefg’
1
’efg’
’bcd’
var !result SORT !myArray CIASCII
!result is now equal to:
!result[1] = ’2’
!result[2] = ’1’
!result[3] = ’4’
!result[4] = ’3’
DO VALUES and DO INDICES
with Arrays

With do values, the counter takes the value of
each array element in return
Example:
!myArray[1] = ’romel’
!myArray[8] = 3
!myArray[15] = ’gadz’
do !result values !myArray
$P Array element is $!result
enddo
Output:
Array element is romel
Array element is 3
Array element is gadz
DO VALUES and DO INDICES
with Arrays

With do indices, the counter takes the value of each
array subscript at which an array element is stored
Example:
!myArray[1] = ’romel’
!myArray[8] = 3
!myArray[15] = ’gadz’
do !n indices !myArray
!stored = !myArray[!n]
$P Array element $!n is $!stored
enddo
Output:
Array element 1 is romel
Array element 8 is 3
Array element 15 is gadz
Exercises

Exercise 8:
Create a recursive function that will search all specified string value
in an array element with any size. Return value is the count value.
Don’t use .Find() method.
Function name:
!!searchValueRecursive()
Sample Usage:
!search
!arr[1]
!arr[2][1]
!arr[2][2]
!arr[2][2][1]
!arr[2][2][2]
=
=
=
=
=
=
’5’
’xx’
’5’
’10’
’yz’
’5’
Upon calling the function, result should be equal to 3
Exercises

Exercise 9:
Create an Object similar to the built-in ARRAY object with the
following limited methods only: FindFirst(), First(), Last(),
Indices() and an additional method FindLast()
Object name:
object RAWARRAY
Don’t use the built-in methods.
PML Macros





Are command sequences that are stored in text files
May include synonyms and user-defined variables
May also act on data which you define when you
give the command to run the macro –
parameterised macros
A Macro file can be given any name, .mac is often
used but it is optional
To run a macro, enter:
$M filename
 Where

filename is the pathname of the macro file
May optionally be preceded by a slash (/) character
PML Macros

Example: A macro that will create a box in file box.mac.
NEW BOX /MyBox
LEVE 2 10
XLEN 150
YLEN 150
ZLEN 150

Using PML variables:
!name = ’/MyBox’
!dim = 150
NEW BOX $!name
LEVE 2 10
XLEN $!dim
YLEN $!dim
ZLEN $!dim
PML Macros

Macros with Arguments
 Escape
code for macro arguments:
$n

Where n is an integer in the range 1 to 9
Example:
If a macro named rod.mac includes the ff. line;
NEW BOX XLEN $1 YLEN $2 ZLEN $3
then the macro call will be,
$M/rod.mac 3500 100 200
Will run the macro and will set the lengths defined as $1, $2,
$3 to 3500, 100, 200.
PML Macros

Macros with Arguments

For arguments in text, note that a space will be interpreted
as separator
Example:
argument.mac has the ff. line;
$P first argument is $1
$P second argument is $2
$P third argument is $3
and is called by a command,
$M argument.mac $<my life$> ella romel
or using a another separator $, and $.,
$M argument.mac $,my life$,ella$,gadz$.
then the output will be,
$P first argument is my life
$P second argument is ella
$P third argument is romel
Exercises

Exercise 10:
Create a parameterised macro that will automatically
create the object shown below:
Macro file: varobject.mac
The “var” specifies that the
dimension is a variable and
consider as an argument of
the macro.
Accessing PDMS DB Elements as
Objects

!!CE – a special global variable provided by PDMS
 Refers
to current element
 Its object type is DBREF
!bore = !!ce.bore
!owner = !!ce.owner
!rating = !!ce.cref.pspec.rating
 To
view all pseudo-attributes of !!CE
q var !!ce
Assigning Values to PDMS Element
Attributes

Ensure that the type of new value matches the
type of attribute
Example:
!!ce.built = TRUE
!a = !!ce
!a.desparam[1] = 250
!posce = !!ce.position
!posce.up = 2000
!!ce.position = !posce
Accessing Information About a
PDMS Session

A number of special commands have been
provided to set a PML Variable with the information
about the current PDMS session
Current Session
 Sessions
 Projects
 Teams
 Users
 MDBs
 DBs
 etc…

Example:
!sess = current session
!mdbcurrent = !sess.mdb()
!users = !sess.user()
Collections

Creating an array which includes all elements
which satisfy a selection criteria
Example:
var !arr1 collect all elbo
With expression:
!qitem = ’all elbo’
var !arr1 collect all elbo for CE
var !arr2 collect $!qitem with(name eq ’/ELBO1’)
Assigning Values to a Variable
with PDMS Commands
Examples:
var !owner OWNER OF CE
var !spco COLLECT ALL SPCO
var !pick IDP@
Copies and References

Assignment

Assignment always makes a copy of the right-hand-side to replace
what is on the left-hand-side
!a = !b

Form and Gadget References

!!Form and !!Form.Gadget are both PML references
!a = !!Form.Gadget
!a.Val = ’new value’
!!Form.Gadget.Val = ’new value’



!a is now a new reference, but the gadget itself has not been copied
both have the same effect and will assign a new value to the original gadget

!!CE is a DB reference and is a PML reference
DB References
!a = !!CE

!a is now a new reference to the same DB element, but the element itself has
not been copied
Errors and Error Handling

An error arising during the processing of a PML macro or function does not
immediately give rise to an error message – these depends on the next line of
input

To handle an error, the matching handle and endhandle block should be
put on the next line in which the error occurs upon processing
Example:
$* a command causes error(46,28) here
handle (46,27)
$* not processed this time
elsehandle (46,28)
$* The commands on this block is processed
elsehandle ANY
$* an ANY handle block is processed for any error
elsehandle NONE
$* a NONE handle block is processed only if $
there were no errors
endhandle
Errors and Error Handling

Responses to an Error

Output the detail of the error message:
$P $!!Error.Text
$P $!!Error.Command
$P $!!Error.Line
do !line values !!Error.Callstack
$P $!Line
enddo

To abandon a running PML macro or function:
return error

Re-instate the error but suppress the alert:
return error noalert

Generate a new error (or replace a user-defined error) plus an optional message:
return error 1
return error 1 ’Your error message’
return error 1 noalert
To handle this, a special form of handle command is used:
handle 1
…
endhandle
Handling Files and Directories

Creating a File Object
Example:
!MyFile = object File (’C:\mydir\list.txt’)
!MyDir = object File (’C:\mydir’)
With Methods:
!access = !MyFile.AccessMode()
!files = !MyDir.Files()
Handling Files and Directories

Example Code:

This example reads pairs of number from file C:\data.txt, adds them
together and writes the answers in file C:\result.txt
!Input = object File (’C:\data.txt’)
!Input.Open(’READ’)
!Output = object File (’C:\result.txt’)
!Output.Open(’WRITE’)
do
!Line = !Input.ReadRecord()
if (!Line.Set()) then
!arr = !Line.Split()
!Total = !arr[1].Real() + !arr[2].Real()
!Output.WriteRecord(!Total.String())
else
break
endif
enddo
!Output.Close()
!Input.Close()
Handling Files and Directories

Reading from Files
 When
reading a file one line at a time using the
ReadRecord() method you must open the file first
with the Open(‘READ’) method and close it
afterwards with the Close() method

Writing to Files
Open(‘WRITE’) method for files that don’t exist
yet or Open(‘OVERWRITE’) if overwriting an existing
one, use WriteRecord() method to write to data to
the file and close it also with the Close() method
 Use
Handling Files and Directories

Reading and Writing ARRAYS
Example:
!Input = object File (’C:\data.txt’)
!Output = object File (’C:\result.txt’)
!Lines = !Input.ReadFile()
!ResultArray = ARRAY()
do !Line values !Lines
!arr = !Line.Split()
!Total = !arr[1].Real() + !arr[2].Real()
!ResultArray.Append(!Total.String())
enddo
!Output.WriteFile(’WRITE’, !ResultArray)
The Alpha Log and PML Tracing

One way of tracing PML:
 Type


Alpha log /C:\trace.txt OVERWRITE
$R102
 Run


in the following in sequence:
any macro (example: showing any form in the GUI)
The tracing code is shown in the alpha window
 On this state, the output is recorded in the specified
file above C:\trace.txt
When it is done, type in the following:
 $R0
 Alpha log end
Querying Values of PML Variables

Queries:
Q var !LocalName – value of a specific local variable
 Q var LOCAL – values of all local variables
 Q var !!GlobalName – value of a specific global variable
 Q var GLOBAL – values of all global variables
 Q var !MyArray[1] – value of a specific element of an
array
 Q var !MyArray – values of all elements of an array
 Q var !MyArray.Size() – number of elements currently in
an array

Exercises

Exercise 11:
Create a function that will query all TEE in DESIGN with run size bore, branch size bore, and
hierarchy being specified. Handle invalid hierarchy input. Returns an array of names.
Function usage:
!tees = !!queryTee(50, 25, ’/SITE’)

Exercise 12:
Create an object that will query the available component types and descriptions in a specified
piping spec.
Sample usage:
!spec = object SPEC(’/1P1’)
!types = !spec.Types
!desc = !spec.Descriptions
!types value:
ARRAY
[1]
[2]
[3]
[4]
[5]
[n]
=
=
=
=
=
=
’TEE’
’PCOM’
’FLAN’
’ATTA’
’FBLI’
…
!desc value:
ARRAY
[1]
[2]
[3]
[4]
[5]
[n]
=
=
=
=
=
=
’Tee’
’Piping Component’
’Flange’
’Attachment Point’
’Blind Flange’
…
Getting noun definition of type: var !def nounDef ATTA rptx
Exercises

Exercise 13:
Create a PICK function that will automatically create a report file
based on an option file. Handle invalid data specified.
Example, file.opt contains the ff. lines:
TEE: P1BORE # P2BORE # P3BORE # ORI # ANG
ELBO: P1BORE # ANG
# LSTUBE
ATTA: ATTYPE # STEXT # LSTUBE
Upon picking an element with type found in file.opt, it will
automatically generate a report with data specified in the option
file (i.e. P1BORE, ORI, etc.).
Output report file name: pick-<TYPE>.rpt
Report delimiter is based on the option file.
Function name:
Syntax for picking:
!!pickAndGenerate()
var !pick PICK
That’s all folks!
DISCLAIMER:
This guide is for information purpose only. It is
recommended that users following this guide have
undergone training first.
You may use this manual at your own risk. In no
event shall the writer be liable for any direct,
indirect, incidental, exemplary or consequential
damages.
All examples in this guide are tested in PDMS 11.6
SP3 and SP5.
Author: Romel E. Daguplo
Email: romeldhagz@gmail.com
Download