可以编辑涉及对象数组的操作参数的代码。
印能捷中的许多操作参数需要使用由对象组成的列表(或数组)。例如,精炼操作需要输入文件列表来进行精炼,执行单页输出操作需要页面列表。许多事件属性也是列表。例如,页面审核已更改事件提供审核状态已更改的页面的列表。
在代码中使用这些数组比使用简单赋值复杂得多。
简单数组赋值
要将事件属性数组的所有成员传递给操作参数数组时,代码看上去与简单赋值相同:
'This takes ALL the input files from the event and gives them to the action
action.InputFiles = triggerEvent.InputFiles
使用 ArrayList
ArrayList 非常有用,因为它们无类型且大小可变。向其中添加内容时,它们会增大,并且您可以向其中放入任何内容。
'Create an ArrayList and add items to it
Dim oArray As System.Collections.ArrayList = New System.Collections.ArrayList
oArray.AddRange(System.IO.Directory.GetFiles("c:\", "*.pdf"))
oArray.Add(someOtherObject)
将 ArrayList 转换为其他数组
在准备 ArrayList 之后,可能需要将它转换为要在规则内使用的其他数组 - 有类型且大小不变的数组。
ArrayList 具有 ToArray 方法,该方法将 ArrayList 的元素复制到给定类型的数组中。
' Converts the ArrayList into an Array of Automation Pages
Dim oAlist As System.Collections.ArrayList ' . . . fill with contents
action.Pages = oAList.ToArray(GetType(Creo.PWS.Automation.PrinergyDataModel.Page))
仅当 ArrayList 的内容可以通过强制类型转换来转换为指定类型时,ToArray 方法才起作用。如果无法转换,则会发生运行时错误。您必须手动将 ArrayList 中的每一项转换为数组。例如,无法将 String 通过强制类型转换转换为 FileSystemItem,但可以使用 CreateFrom 工厂方法来基于 String 创建 FileSystemItem。
' Converts the ArrayList into an Array of Automation FileSystemItems.
Dim oFSIArray() As Creo.PWS.Automation.PrinergyDataModel.FileSystemItem = _
New Creo.PWS.Automation.PrinergyDataModel.FileSystemItem(oAList.count - 1) {}
For i As Integer = 0 To oAList.Count - 1
oFSIArray = _
Creo.PWS.Automation.PrinergyDataModel.FileSystemItem.CreateFrom(oAList.ToString)
Next i
合并嵌套数组
有些时候,要为操作参数指定的数据分散在事件属性中某数组的多个成员中。例如,精炼操作的结果可从输入文件精炼成功事件获取。查看该事件时,可以看到它具有以下属性:
成功精炼的输入文件 |
已精炼成功的输入文件列表 |
处理 |
精炼处理 |
事件时间 |
事件的处理时间 |
根事件 |
序列中的第一事件 |
先前事件 |
序列中的前一个事件 |
页面位于何处?每个已精炼输入文件都具有自己的基于输入文件创建的页面列表。也就是说,InputFiles 数组的每个成员都包含一个页面数组。
要将所有已精炼页面的列表传递给操作(例如执行单页输出),需要一种方式来遍历每个输入文件,并构造所有页面的集合。
'First we need to create a holder for the new list we will be building
Dim newList As System.Collections.ArrayList = New System.Collections.ArrayList
'Now we create a counter variable and initialize to the value 0
Dim i As Integer = 0
'Next we create a loop that will go through all the input files (until
'the counter i reaches the end of the list)
Do While (i < triggerEvent.InputFiles.Length)
'We reach into the ith Input File and add its Pages array to our list
newList.AddRange(triggerEvent.InputFiles.Pages)
'Increment the value of i
i = (i + 1)
Loop
'Finally, we convert our list into an Array of Pages and assign it to the
'Pages parameter of the action
action.Pages = newList.ToArray(GetType(Creo.PWS.Automation.PrinergyDataModel.Page))
过滤数组
另一种常见需求是过滤列表中不感兴趣部分的能力。例如,您可能希望使用一种简单方法来创建仅由作业中的每个帖的正面组成的拼版打样。您希望向拼版打样操作传递过滤掉所有背面的印面列表。
'First we need to create a holder for the new list we will be building
Dim newList As System.Collections.ArrayList = New System.Collections.ArrayList
'Now we create a counter variable and initialize to the value 0
Dim i As Integer = 0
'Next we create a loop that will go through all the input files (until
'the counter i reaches the end of the list)
Do While (i < triggerEvent.Surfaces.Length)
'We look at the ith Surface and add it to our list if it is "Front"
If triggerEvent.Surfaces.Side = "Front" Then
newList.AddRange(triggerEvent.Surfaces)
End If
'Increment the value of i
i = (i + 1)
Loop
'Finally, we convert our list into an Array of Surfaces and assign it to the
'Surfaces parameter of the action
action.Surfaces =
newList.ToArray(GetType(Creo.PWS.Automation.PrinergyDataModel.Surface))
过滤和合并数组
结合过滤和合并两种模式可以实现最大的功能。在此示例中,我们希望得到尺寸大于 8.5 x 11 的所有已精炼页面的列表。因为需要检查每个页面来确定其尺寸,所以这需要引入第二个循环。外部循环遍历所有输入文件,而内部(嵌套)循环遍历每个输入文件的所有已精炼页面。
'First we need to create a holder for the new list we will be building
Dim newList As System.Collections.ArrayList = New System.Collections.ArrayList
'Now we create a counter variable and initialize to the value 0
Dim i As Integer = 0
'Next we create a loop that will go through all the input files (until
'the counter i reaches the end of the list)
Do While (i < triggerEvent.InputFiles.Length)
'Create a second counter and make a second loop to go through all
' the pages for input file i
Dim j As Integer = 0
Do While (j < triggerEvent.InputFiles.Pages.Length)
'Grab the jth Page from the ith Input File
Dim thePage As Page = triggerEvent.InputFiles.Pages(j)
'See if the page is larger than 8.5 x 11 (in points) – if so add it to our list
If (thePage.TrimSize.x > 8.5*72 AND thePage.TrimSize.y > 11*72) Then
newList.add(thePage
End If
'Increment the value of j
j = (j + 1)
Loop
'Increment the value of i
i = (i + 1)
Loop
'Finally, we convert our list into an Array of Pages and assign it to the
'Pages parameter of the action
action.Pages = newList.ToArray(GetType(Creo.PWS.Automation.PrinergyDataModel.Page))