Scripting

Within the Autodesk Fabrication products, you can use scripting features to access and modifying object data programmatically . Scripting is similar in syntax to VB.NET (Visual Basic.NET).

Scripts allow you to read and write object data on multiple objects, run tests, and add features that allow users to perform certain functions.

What Can Be Scripted?

Within the Autodesk Fabrication products, many types of object data can be accessed for read and write operations. These include:

Additionally, some methods are available, such as:

Read and write access for binary and text files is also supported. To read data from an object, access the data using the ITEM object, followed by the required DATATYPE. 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

Writng or storing data to the object is very similar 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 quote ("), you must do the following:

Dim quote = ASCII(34)

Then concatenate the string using something like this: 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 it is important 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). You can also work on the selection in a loop which is described below.

The simplest way to check is to use the SELECT command and test the items CID number. However, 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 function called DEBUG which is used to display information in a dialog.

Any data can be output to the debug window. This should be used 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 comment. This is useful if anyone needs to edit the script at a later date. The REM comments are ignored by the script, but allow you to add notes to allow people to easily understand 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

You can open 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

You 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

You 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. You 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 CAMduct or ESTmep by cllicking Window Scripting. A script file can be opened for editing using File Open Script. The File extension is .COD.

Execute the Script from CAMduct or ESTmep

To execute a script from CAMduct, either open the script by clicking File Open Script, then click . This runs 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, select the script file (if already run, or select Browse.).

The script is run on the selected objects.

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 by how much the flex is too long.

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