Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Having trouble reading an array into a sub in order to swap visibility

2 REPLIES 2
Reply
Message 1 of 3
jpblower
225 Views, 2 Replies

Having trouble reading an array into a sub in order to swap visibility

Dim oAssyDoc As AssemblyDocument
Dim oAssyCompDef As AssemblyComponentDefinition
Dim oPartDoc As PartDocument
Dim oPartCompDef As PartComponentDefinition

Public Sub Hide()
    Call Initialize
    
    Dim oVisSurfs() As WorkSurface
    Dim tempWkSurf As WorkSurface
    Dim lngNumSurf As Long
    Dim lngTempCount  As Long
    
    lngTempCount = 1
    lngNumSurf = oPartCompDef.WorkSurfaces.Count
    ReDim oVisSurfs(lngNumSurf)
    
    For Each tempWkSurf In oPartCompDef.WorkSurfaces
        If tempWkSurf.Visible = True Then
            Set oVisSurfs(lngTempCount) = tempWkSurf
            'oVisSurfs(lngTempCount).Visible = False
            lngTempCount = lngTempCount + 1
        End If
    Next
    
    Call SwapVis(oVisSurfs())
End Sub
Public Sub SwapVis(visSurfs As Variant)
    Dim tempSurf As WorkSurface
    For Each tempSurf In visSurfs
        If tempSurf.Visible = True Then
            tempSurf.Visible = False
        Else: tempSurf.Visible = True
        End If
    Next
End Sub
Public Sub Initialize()
    Set oPartDoc = ThisApplication.ActiveDocument
    Set oPartCompDef = oPartDoc.ComponentDefinition
End Sub

 

I have this code to swap the visibility of surfaces in the model.  Essentially shutting all off and on based on their current status.  However I only want to swap the surfaces that are currently visible, not any in hiding. 

 

I get a Run-Time error '424':  Object required error when the program reaches

 

    For Each tempSurf In visSurfs

 

 I'm not sure what's wrong.

 

2 REPLIES 2
Message 2 of 3
ekinsb
in reply to: jpblower

There are a few of things not quite right because of VBA behavior.  The first is your resizing of the array.  Let's say there are 5 work surfaces.  The Redim statement below will create an array for 6 items.  In VBA (and VB) when you declare the size of the array you're not specifying the number of elements but instead the index of the last element.  Because by default the first item is 0 you end up with one more than you think you should.

 

   ReDim oVisSurfs(lngNumSurf)

 

So instead of the line above you can just do this:

 

   ReDim oVisSurfs(lngNumSurf -1)

 

and you'll need to initialize lngTempCount to 0 instead of 1.

 

    lngTempCount = 0

 

The next issue is that VBA doesn't support using a For Each statement to iterate over the contents of an array, (VB does).  So instead of For Each you'll need to use a For To as shown below.  Notice that I also changed the declaration of the Sub.

 

Public Sub SwapVis(visSurfs() As WorkSurface)
    Dim tempSurf As WorkSurface

    Dim i As Integer
    For i = 0 To UBound(visSurfs)

        Set tempSurf = visSurfs(i)
        If tempSurf.Visible = True Then
            tempSurf.Visible = False
        Else: tempSurf.Visible = True
        End If
    Next
End Sub

 

When calling SwapVis it should be like below:

 

Call SwapVis(oVisSurfs)


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3
jpblower
in reply to: ekinsb

Thanks.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report