VBA – 是否可以声明一个变体数组,其中的元素具有不同类的数据类型

huangapple go评论61阅读模式
英文:

VBA - Is it possible to declare a Variant array whose elements have the data types of different classes

问题

I would like to declare a variant array that contains different data types such as objects.

Dim faceObj As SldWorks.Face2

Dim selObject() As Variant
ReDim Preserve selObject(4)
' code that initializes faceObj is not included here

Now, selObject has 5 elements, that are of Empty data type.

I would like to change a first element datatype to be of a Face2 class.

Set selObject(0) = faceObj

While this works, array elements still have the Object datatype and therefore members of the objects are not offered by lint.

Apologies if I used wrong nomenclature as I don't understand properly OOP terminology.

I tried ReDim selObject(i - 1) As SldWorks.Face2 but it says that can't change the datatype of array elements. I think I saw that in Variant it's possible to have the different datatypes but maybe my syntax is wrong.

Edit: Would it be better to rather use a structure array for this case?

英文:

I would like to declare a variant array that contains different data types such as objects.

Dim faceObj As SldWorks.Face2

Dim selObject() As Variant
ReDim Preserve selObject(4)
' code that initializes faceObj is not included here

Now, selObject has 5 elements, that are of Empty data type.

I would like to change a first element datatype to be of a Face2 class.

Set selObject(0) = faceObj

While this works, array elements still have the Object datatype and therefore members of the objects are not offered by lint.

Apologies if I used wrong nomenclature as I don't understand properly OOP terminology.

I tried ReDim selObject(i - 1) As SldWorks.Face2 but it says that can't change the datatype of array elements. I think I saw that in Variant it's possible to have the different datatypes but maybe my syntax is wrong.

Edit: Would it be better to rather use a structure array for this case?

答案1

得分: 0

根据评论部分,不可能重新声明元素为不同的对象类。它仍然是相同的对象数据类型。

英文:

Based on the comment section, it is not possible to redeclare elements to be of the different object class. It will still be the same Object data type.

答案2

得分: 0

以下是翻译好的部分:

这已经在Solidworks API中实现。

ISelectionManager 接口将您当前的选择存储为对象数组。您可以使用内置的 GetSelectedObjectType(index) 来确定可以将对象强制转换为什么类型。

使用 ISelectionManager.GetSelectedObject6(index, mark) 返回给定索引处的选定对象。

这个示例检查选择是否为面,并成功将其强制转换为 Face2 对象。

Option Explicit

Sub main()

    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swSelectionManager As SelectionMgr
    Dim swFace As Face2
    Dim swBody As Body2

    Dim selectionType As Integer
    Dim selection As Variant
    Dim selCount As Integer

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelectionManager = swModel.SelectionManager

    selCount = swSelectionManager.GetSelectedObjectCount2(-1)

    If selCount > 0 Then
        selectionType = swSelectionManager.GetSelectedObjectType(1)

        If selectionType = swSelectType_e.swSelFACES Then
            Set swFace = swSelectionManager.GetSelectedObject6(1, -1)
            '现在可以按照Face2对象处理所选内容
        End If
    End If

End Sub

然而,如果您想使用自己的数组,您可以将项目存储为类似下面示例中的 Entity 对象。请注意,这不适用于与上述方法一样多的对象类型,因此它的行为可能不会像上述方法那样好。

Dim myArray(0) As Variant
Set myArray(0) = swSelectionManager.GetSelectedObject6(1, -1) '我只是用这个来填充我的数组

Dim swEntity As Entity

Set swEntity = myArray(0)
selectionType = swEntity.GetType '在此之后进行必要的强制转换
英文:

This is already implemented in the Solidworks API.

The ISelectionManager interface stores your current selections as an array of objects. You can use the built-in GetSelectedObjectType(index) to determine what you can cast the object as.

Using the ISelectionManager.GetSelectedObject6(index, mark) returns the selected object at the given index.

This example checks if the selection is a face and then casts it as the Face2 object successfully.

Option Explicit

Sub main()

    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swSelectionManager As SelectionMgr
    Dim swFace As Face2
    Dim swBody As Body2
    
    Dim selectionType As Integer
    Dim selection As Variant
    Dim selCount As Integer
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelectionManager = swModel.SelectionManager
    
    
    selCount = swSelectionManager.GetSelectedObjectCount2(-1)
    
    If selCount > 0 Then
        selectionType = swSelectionManager.GetSelectedObjectType(1)
        
        If selectionType = swSelectType_e.swSelFACES Then
            Set swFace = swSelectionManager.GetSelectedObject6(1, -1)
            'Now do what you want with the selection as Face2 object
        End If
    End If
    
End Sub

However, if you're focused on using your own array, you can store items as Entity objects similar to the below example. Please note that this doesn't work with as many object types so it won't behave as nicely as the above method.

Dim myArray(0) As Variant
Set myArray(0) = swSelectionManager.GetSelectedObject6(1, -1) ' I'm just using this to fill my array

Dim swEntity As Entity

Set swEntity = myArray(0)
selectionType = swEntity.GetType ' cast as necessary after this

huangapple
  • 本文由 发表于 2023年4月10日 23:54:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定