PML Basics PDF
PML Basics PDF
Examples:
!!var Global
!var Local
PML 2 Variables
Other examples:
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
In
When giving a PML variable a value using = sign
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
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 !!MyFunction() returns
………… BOOLEAN
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
or
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)
Example:
define function !!AnyType( !input is ANY )
$P $!input
endfunction
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:
PML scans all files in the PMLLIB
pml.index is created
All PML files listed in the pml.index file are loaded
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
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.
!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
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
Main function:
!manObj = object MAN(40)
!value = !manObj.num !value now is 45
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 ……
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.
Sample Usage:
!num1 = 15
!num2 = 3
!calculate = object CALC(!num1, !num2)
!prod = !calculate.product()
!quot = !calculate.quotient()
!sum = !calculate.sum()
!diff = !calculate.difference()
Sample Usage:
!str = ’Remember the one I told you?’
!cObj = object COUNTVC(!str)
!numvowels = !cObj.countVowels()
!numconso = !cObj.countConsonants()
Exercise 7 Solution
An Object that has a count vowels and count consonants
method.
define object COUNTVC
member .text is STRING
member .vowels is STRING
member .consonants is STRING
member .textlen is REAL
endobject
-- 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
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]
Example:
!myArray[1] = ’romel’
!myArray[2] = 3
!myArray[3] = ’gadz’
!myArray[4] = ’gadz’
!nelements = !myArray.Size()
!felements = !myArray.Find(’gadz’)
!myArray.Clear()
Using VAR Command
for Arrays
Example:
Sorting an ARRAY in alphabetical order:
!myArray[1] = ’abcdefg’
!myArray[2] = 1
!myArray[3] = ’efg’
!myArray[4] = ’bcd’
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 = ’5’
!arr[1] = ’xx’
!arr[2][1] = ’5’
!arr[2][2] = ’10’
!arr[2][2][1] = ’yz’
!arr[2][2][2] = ’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()
!bore = !!ce.bore
!owner = !!ce.owner
!rating = !!ce.cref.pspec.rating
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 Example:
Sessions
!sess = current session
Projects !mdbcurrent = !sess.mdb()
Teams !users = !sess.user()
Users
MDBs
DBs
etc…
Collections
Creating an array which includes all elements
which satisfy a selection criteria
Example:
With expression:
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
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
Use 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
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 in the following in sequence:
Alpha log /C:\trace.txt OVERWRITE
$R102
Run 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
DISCLAIMER:
All examples in this guide are tested in PDMS 11.6 Author: Romel E. Daguplo
SP3 and SP5.
Email: [email protected]