How to make an duplicate array of the Selection Manager?

Programming and macros
User avatar
loeb
Posts: 70
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 39
x 10

How to make an duplicate array of the Selection Manager?

Unread post by loeb »

I need to make an array of everything that the user has selected in the feature tree of an assembly file. The selected items an be planes, sketches, mates, parts, assemblies, etc. I made a copy of the selection manager's array in the form of a variant array, but I can't figure out how to operate on the members of the variant array.

For example, I can't get the type of the elements in the array. In the following example, the last "Type" method doesn't work.

Code: Select all

    For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
        Debug.Print "i: " & i & "    Type:" & swSelMgr.GetSelectedObjectType3(i, -1)
        ReDim Preserve SelSetCopy(i)
        Set SelSetCopy(i) = swSelMgr.GetSelectedObject5(i)
    Next i
    
    For i = 1 To UBound(SelSetCopy)
       Debug.Print "i: " & i & "    Type:" & SelSetCopy(i).Type
    Next i
I think I'm heading down the wrong path. Do I instead need to make an array for each object type?
User avatar
AlexB
Posts: 508
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 29
x 274
x 462

Re: How to make an duplicate array of the Selection Manager?

Unread post by AlexB »

Typically, you'll either want to do one of two things.

1. Keep all of the types of objects in an array the same type
2. Use 2 separate arrays of the same length and have array1 hold the objects and have array2 hold the object types. This way, if you know what type to treat your object as from array2, then you can just cast the object from array1 to that object type.

I hope that makes sense. Without more of an example to go off of, this is the best I can recommend.
User avatar
loeb
Posts: 70
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 39
x 10

Re: How to make an duplicate array of the Selection Manager?

Unread post by loeb »

AlexB wrote: Thu Jun 02, 2022 8:21 am Typically, you'll either want to do one of two things.

1. Keep all of the types of objects in an array the same type
2. Use 2 separate arrays of the same length and have array1 hold the objects and have array2 hold the object types. This way, if you know what type to treat your object as from array2, then you can just cast the object from array1 to that object type.

I hope that makes sense. Without more of an example to go off of, this is the best I can recommend.
Alex, that makes perfect sense. I have been wondering if type casting is possible in VBA. I cannot find reference to it the Microsoft VBA Reference and I don't see any examples of explicit casting in the VBA code I have read. I have seen plenty of "type mismatch" errors in my own immediate window, though.

Is all type casting implicit? Does it always require one of the elements to be a variant?
User avatar
AlexB
Posts: 508
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 29
x 274
x 462

Re: How to make an duplicate array of the Selection Manager?

Unread post by AlexB »

Now that you mention it, I'd probably have to test it to see if it's possible but I feel like it should work. I would approach it similar to the below loop to check the type of the feature while iterating through the array. If it's what I expect, in this case a Sensor object (anything within the sensor folder is a sensor object. I just wanted the first one), I would then just use Set swSensor = arrayObject(i) that matches the type. If it doesn't work with a variant array, perhaps an array with type Object would work instead.

Best of luck!

Code: Select all

    Dim swApp               As SldWorks.SldWorks
    Dim swDoc               As SldWorks.ModelDoc2
    Dim swFeat              As SldWorks.Feature
    Dim swFeatureMgr        As SldWorks.FeatureManager
    Dim swSensorFeat        As SldWorks.Feature
    Dim swSensor            As SldWorks.Sensor
    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
    Set swFeatureMgr = swDoc.FeatureManager
    Set swEquationMgr = swDoc.GetEquationMgr
    
    featVar = swFeatureMgr.GetFeatures(True)                    'get all features into featVar variant array
    
    For i = 0 To UBound(featVar)                                'loop though features
        Set swFeat = featVar(i)                                 'set swFeat object to current feature in loop
        If swFeat.GetTypeName2 = "SensorFolder" Then            'if that feature is a sensor folder continue into if

            Set swSensorFeat = swFeat.GetFirstSubFeature        'set swSensorFeat object to first subfeature in sensor folder
            Debug.Print swSensorFeat.Name                       'check it's the right feature
            Set swSensor = swSensorFeat.GetSpecificFeature2     'set swSensor object
            
        End If
        Set swFeat = Nothing                                    'clear swFeat object to free memory
    Next i
Post Reply