Scripting is a built in API for accessing and modifying Object data programmatically. Scripting is similar in syntax to VB.NET (Visual Basic.NET), any help reference material would be useful to understand some concepts in Scripting, like String and Math handling.
Scripts allow you to read and write object data on multiple objects, run tests, and interface with the user results of the script. You can create you own rules and tests for data that would normally be outside of the programs scope, and no code changes to the main application are required (unless a new script object is needed or requested).
What can we Script?
We can access for Read and Write many parts of the Objects Data these include:
Additionally some methods are available.
Read and Write access for binary and text files is also supported. Reading Data from an ObjectWe access the data using the built in " ITEM" object, followed by the DATATYPE we require, you can assign this to an automatically typed variable or use "as is". Note. variable names do not contain spaces, and are not case sensitive.
Examples:
Writing Data to the Object
We write/store data to the object is a very similar way to reading using the ITEM object followed by the .data name. String values are wrapped in quotation marks.
Because strings are "quoted", if you want to write a string value that contains a " we need to do the following
Dim quote = ASCII(34)
Then concatenate the string using something like Item.Specification = "2" + quote + " WG"
To write values to array data, use the .VALUE specifier
Flow Control "Select Case End Select"
The Script will run once for each object selected, so you would need to test some item data to check you are working on the right type of object (or pre-filter, by selecting only the required objects), we can also work on the selection in a loop which will be covered later in this topic.
The simplest way to check is to use the SELECT command and test the items CID number (but you can use any data).
Select ITEM.CID
case 873
do something here for Flex
case 866,35,36
do something here with straights
case 3,4
do something for Square and Radius Bends
End select
Flow Control "If Then Else", "Else If", and "End If"
An alternative would be to use an " If then Else" statement enabling you to test some data and act only if it matches your condition.
Dim cid = item.cid
If cid = 873 then
doflex()
Else if cid = 866 or cid = 35 then
dostraights()
Else if cid = 3 or cid = 4 then
dobends()
End if
Flow Control "While Loop - Do While Loop"
Dim count = 5
While count < 10
debug count
count = count + 1
End while
Dim count = 10
Do
debug count
count = count - 1
Loop until count = 0
Do while count < 10
debug count
count = count + 1
loop
Dim Found = FALSE
While not myfile.eof and not found
dim line = myfile.readline()
if line = "My Data" then
found = TRUE
end if
end while
There is a built in function called DEBUG which you use to display information in a dialogue.
Any data can be output to the debug window, this should be used widely until the script is complete and working.
DEBUG item.cid
DEBUG item.description
DEBUG item.connector[1].value
You can add data together and then debug as shown in the examples below:
Dim cr = ascii(10)
Dim output = "Item " + item.description + cr + " has " +
item.connectors " Connectors and " + cr +
item.seams " Seams"
Debug outpu
It is also advisable to use the REM keyword in your code to add comments, this will help you if you or a colleague has to ammend the script at a later date. The REM comments are ignored by the script but allow you to add notes to assist in seeing what the script is trying to do.
Advanced Scripting
The following section will demonstrate:
Progress bars are used to display to the user the progress of the script operation. Include " requires task" at the top of the file to use Progress Bars.
requires task
task.beginprogress(5000)
dim lp = 1
task.progress = lp
for lp = 1 to 5000
task.progress = task.progress + 1
task.message = "Count is now " + lp
if task.aborted then lp = 5000
next lp
task.endprogress()
Use Task.Selection to process multiple items within one call from the script, instead of the script being called for each item. We have to use " requires task.selection" at the start of the script.If you want to write information to a file you have to work with a selection, you would not want the script to run for each item, else you would keep overwriting the file you are trying to create.
requires task.selection
dim loop = 1
while loop <= task.selection.count
dim jobitem = task.selection[loop]
loop = loop + 1
task.message = "Working with " + jobitem.description
if task.aborted then loop = task.selection.count + 1
endwhile
Combining both Progress Bar and Selection
requires task.selection
dim loop = 1
task.beginprogress(task.selection.count)
while loop <= task.selection.count
dim jobitem = task.selection[loop]
loop = loop + 1
task.message = "Working with " + jobitem.description
if task.aborted then loop = task.selection.count + 1
task.progress = task.progress + 1
endwhile
task.endprogress()
Working with Text or Binary files for Read
We can open a Text or Binary files to read and use its contents from within a script.
object myfile = new file ("c:/Temp.dat" forread)
object myfile = new file ("c:/Temp.txt" forread+istext)
if myfile.isopen then
while not myfile.eof
dim str = myfile.readline()
rem do something with the string, check
rem documentation for the string functions
end while
endif
Rem Don't forget to close the file
myfile.close()
Working with Files for Write
We can open a Text or Binary files for Writing to from a script.
object outfile = new file ("c:/Temp.dat" forwrite)
object outfile = new file ("c:/Temp.txt" forwrite+istext)
rem if appending, move to the file end
object outfile = new file ("c:/Temp.txt" forread+forwrite+istext)
outfile.position = FILE_END
if outfile.isopen then
dim str = item.description
outfile.writeline(str + ",")
str = item.number
outfile.writeline(str)
endif
outfile.close()
Working with Files in Folders
We can scan through folders and work on every instance of the itm files.
rem scan for all items in the specified folder
dim filescan as filelocator
Rem just scan for files
filescan.scan(folderpath, "*.itm", true, false)
Rem scan for folders
dim folderscan as filelocator
folderscan.scan(path, "*.*", false, true)
for lp=1 to filescan.filecount
rem load the item
dim item as itemstruct
dim loadfile=filescan.file[lp]
if item.load(loadfile) then
Arrays
Data can be a single object, or can be an array of data, which requires indexing to access. We can use Arrays for collecting and storing any type of data or object.
Methods on Array include:
You access the element in the array using the [ ] operator.
requires task.selection
Dim myarray as array
Dim jobitem=task.selection[1]
myarray.add(jobitem)
or
or myarray.insert(jobitem,3)
myarray.delete(2)
Dim arraycount = myarray.count
Creating a Script
Create the script as a text file with Notepad or the Script Editor in CAM-Duct, EST-Duct, or Profilemaster , Window > Scripting. A script file can be opened for editing using File > Open Script. The File extension is .COD.
Execute the Script from CAM-Duct, EST-Duct, or Profilemaster
To execute a script from CAM-Duct, EST-Duct, or Profilemaster you can either open the script, File > Open Script, then hit the
Icon, this will run the script on all items in the current job, or select the items in Job Contents, right click, and select " Execute Script".
From the Menu, selecting the script file (if already run, or select Browse.).
The script is run on the selected objects.
(using task)
Examples
To Change the Arrow type on Diffusers looping through all types. 1,2,3, this is complicated by the fact that when you read out the options value it will return 1,2 or 3, but we need to set it with 0,1 or 2, or the string "1" "2" "3".
select item.cid
case 515
dim opt = item.Option [10].value
if (opt > 0) then opt = opt - 1
opt = opt + 1
if (opt > 2) then opt = 0
item.option[10].value = opt
end select
Example to change the length of flex
This example reads the Length (dim 3) from the dimensions, and the Max Length in the patterns options. If the Length is greater that the maximum length, it displays a message telling the user how much too long the flex is.
select item.cid
case 873
dim len = item.Dim[3].value
dim maxlen = Item.Option["Max Length"].value
if len > maxlen then
dim len2 = len - maxlen
debug "Flex, " + Item.Number + " is " + len2 + " too Long"
endif
end select
This example reads the Width and Depth, calculates the periphery, and sets the option for number of parts.
select item.cid
case 7
dim width = item.dim ["Top Width"].value
dim depth = item.dim ["Depth"].value
dim periphery = width+width+depth+depth
if (periphery>60) then
item.Option["2 Parts"].Value="No""
else
item.Option["2 Parts"].Value="Yes"
end if
end select
Getting user Input
There are 2 methods of getting information from the user. Use the " Query" object to get a Yes/No from the user or use the " InputBox" method to get specific values from the user.
Dim default = "123"
Dim newOrder = InputBox ("Enter Mark:" "Title" default)
Item.Order = newOrder t