CATIA VBA 测量用户选择的线/样条
CATIA VBA Measure a user selected line(s)/spline
我正在尝试获取用户 selected lines/splines
的长度
这是我用来让用户select他们的台词的代码:
Dim USel As Selection
Dim USelLB
Dim InputObject(0)
InputObject(0) = "AnyObject"
Set USel = CATIA.ActiveDocument.Selection
Set USelLB = USel
USel.Clear
USelLB.Clear
Linestomeasure = USelLB.SelectElement3(InputObject, "Select objects to list names", True, CATMultiSelTriggWhenUserValidatesSelection, False)
Linestomeasure
是一个 public
变量,在 mainsub 中我一直在尝试使用以下代码测量 Linestomeasure
:
Dim pd1 As PartDocument
Dim a As Object
Dim c As Reference
a = TrimLines.Item(1)
c = pd1.Part.CreateReferenceFromObject(a)
Dim Mea1 As Measurable
Dim TheSPAWorkbench As SPAWorkbench
Set TheSPAWorkbench = pd1.GetWorkbench("SPAWorkbench")
Set Mea1 = TheSPAWorkbench.GetMeasurable(c)
但是当我 运行 时,代码 a = trimLines.Item(1)
在调试器中突出显示并显示错误消息 "Object Required"。
有没有人知道如何更改我的代码,以便我可以将行的长度作为我可以使用的变量?或者只是我想做的事情的另一种方式?
编辑答案以反映下面的评论
看起来您为 USelLB.SelectElement3
分配了错误类型的变量,并且还误解了它的实际工作原理。
反映select离子是否成功的Selection.SelectElement3
returns一个String
从 Selection
中检索到的 Object
在 Selection.Item(Index)
中
你的代码应该是这样的:
Dim PD1 as PartDocument
Dim Sel 'as Selection 'Sometimes it is needed to comment the selection to use the .SelectElement3 method
Dim InputObjType(0)
Dim SelectionResult as string
Dim LineToMeasure as AnyObject
Dim I as Integer
Dim SpaWorkbench as SPAWorkbench
Dim Measurable as Measurable
InputObjType(0) = "AnyObject"
'set PD1 = Catia.ActiveDocument
set Sel = PD1.Selection
Set TheSPAWorkbench = pd1.GetWorkbench("SPAWorkbench")
Sel.Clear
SelectionResult= Sel.SelectElement3(InputObject, "Select objects to list names", True, CATMultiSelTriggWhenUserValidatesSelection, False)
If SelectionResult = "Ok" or SelectionResult = "Normal" then 'Check if user did not cancel the Selection
For i = 1 to Selection.Count
Set LineToMeasure = Sel.Item(i).Value
set Measurable = SpaWorkbench.GetMeasurable(LineToMeasure)
'Measure whatever you need here.
Next
End If
请记住,使用 AnyObject
类型过滤器可能会导致用户 select 不需要的对象。您应该使用更具体的过滤器。
我正在尝试获取用户 selected lines/splines
的长度这是我用来让用户select他们的台词的代码:
Dim USel As Selection
Dim USelLB
Dim InputObject(0)
InputObject(0) = "AnyObject"
Set USel = CATIA.ActiveDocument.Selection
Set USelLB = USel
USel.Clear
USelLB.Clear
Linestomeasure = USelLB.SelectElement3(InputObject, "Select objects to list names", True, CATMultiSelTriggWhenUserValidatesSelection, False)
Linestomeasure
是一个 public
变量,在 mainsub 中我一直在尝试使用以下代码测量 Linestomeasure
:
Dim pd1 As PartDocument
Dim a As Object
Dim c As Reference
a = TrimLines.Item(1)
c = pd1.Part.CreateReferenceFromObject(a)
Dim Mea1 As Measurable
Dim TheSPAWorkbench As SPAWorkbench
Set TheSPAWorkbench = pd1.GetWorkbench("SPAWorkbench")
Set Mea1 = TheSPAWorkbench.GetMeasurable(c)
但是当我 运行 时,代码 a = trimLines.Item(1)
在调试器中突出显示并显示错误消息 "Object Required"。
有没有人知道如何更改我的代码,以便我可以将行的长度作为我可以使用的变量?或者只是我想做的事情的另一种方式?
编辑答案以反映下面的评论
看起来您为 USelLB.SelectElement3
分配了错误类型的变量,并且还误解了它的实际工作原理。
反映select离子是否成功的Selection.SelectElement3
returns一个String
从 Selection
中检索到的 Object
在 Selection.Item(Index)
你的代码应该是这样的:
Dim PD1 as PartDocument
Dim Sel 'as Selection 'Sometimes it is needed to comment the selection to use the .SelectElement3 method
Dim InputObjType(0)
Dim SelectionResult as string
Dim LineToMeasure as AnyObject
Dim I as Integer
Dim SpaWorkbench as SPAWorkbench
Dim Measurable as Measurable
InputObjType(0) = "AnyObject"
'set PD1 = Catia.ActiveDocument
set Sel = PD1.Selection
Set TheSPAWorkbench = pd1.GetWorkbench("SPAWorkbench")
Sel.Clear
SelectionResult= Sel.SelectElement3(InputObject, "Select objects to list names", True, CATMultiSelTriggWhenUserValidatesSelection, False)
If SelectionResult = "Ok" or SelectionResult = "Normal" then 'Check if user did not cancel the Selection
For i = 1 to Selection.Count
Set LineToMeasure = Sel.Item(i).Value
set Measurable = SpaWorkbench.GetMeasurable(LineToMeasure)
'Measure whatever you need here.
Next
End If
请记住,使用 AnyObject
类型过滤器可能会导致用户 select 不需要的对象。您应该使用更具体的过滤器。