how to suppress multiple part using vba

how to suppress multiple part using vba

Anonymous
Not applicable
1,272 Views
6 Replies
Message 1 of 7

how to suppress multiple part using vba

Anonymous
Not applicable

The code below is working fine for selecting 3 parts 

 

 

' To suppress a part

Public Sub AssemblyCount()

    ' Set reference to active document.

    ' This assumes the active document is an assembly

    Dim oDoc As Inventor.AssemblyDocument

    Set oDoc = ThisApplication.ActiveDocument

    Dim selset As SelectSet

    Dim compOcc As ComponentOccurrence

    oDoc.SelectSet.Clear

    MsgBox ("Select a Part or a Subassembly")

    Do Until oDoc.SelectSet.Count = 3

        DoEvents    ' Yield to other processes.

    Loop

    Set selset = oDoc.SelectSet

    Dim obj As Object

    For Each obj In selset

        If TypeOf obj Is ComponentOccurrence Then

               Set compOcc = obj

               compOcc.Suppress

        End If

    Next

    oDoc.SelectSet.Clear

End Sub

 

 

But i want to select multiple parts until i hit enter button after hitting enter button all the selected parts must be suppressed

 

I tried the Application.onkey 

 

Public enterWasPressed As Boolean

' To suppress a part

Public Sub AssemblyCount()

    ' Set reference to active document.

    ' This assumes the active document is an assembly

    Dim oDoc As Inventor.AssemblyDocument

    Set oDoc = ThisApplication.ActiveDocument

    Dim selset As SelectSet

    Dim compOcc As ComponentOccurrence

   

    oDoc.SelectSet.Clear

    MsgBox ("Select a Part or a Subassembly")

    enterWasPressed = False

    Do Until enterWasPressed = True

        DoEvents    ' Yield to other processes.

        Application.OnKey "{ENTER}", "recordEnterKeypress"

    Loop

 

 

    Set selset = oDoc.SelectSet

    Dim obj As Object

    For Each obj In selset

        If TypeOf obj Is ComponentOccurrence Then

               Set compOcc = obj

               compOcc.Suppress

        End If

    Next

    oDoc.SelectSet.Clear

End Sub

 

   

 

Sub recordEnterKeypress()

    enterWasPressed = True

End Sub

 

 

but i am getting runtime error '424' object required can anyone please help me to fix my issue

0 Likes
1,273 Views
6 Replies
Replies (6)
Message 2 of 7

CCarreiras
Mentor
Mentor

Hi!

 

Why don't you use iLogic.... a lot more simple to achieve that...

CCarreiras

EESignature

0 Likes
Message 3 of 7

Anonymous
Not applicable

I need to use VBA and learn it because it has lot more capabilites.

0 Likes
Message 4 of 7

rjay75
Collaborator
Collaborator

Just an FYI everything you can do in VBA you can do in iLogic.

 

Second your VBA code. The Application object will use the Inventor Application. In which there is no OnKey event to hook to through VBA. 

 

An alternate work flow is to run your VBA code after the objects have been selected.

 

VBA Code

 

' To suppress a part

Public Sub AssemblyCount()

    ' Set reference to active document.

    ' This assumes the active document is an assembly

    Dim oDoc As Inventor.AssemblyDocument

    Set oDoc = ThisApplication.ActiveDocument

    Dim selset As SelectSet

    Dim compOcc As ComponentOccurrence

   


    Set selset = oDoc.SelectSet
    
    If selset.Count > 0 Then

    Dim obj As Object

    For Each obj In selset

        If TypeOf obj Is ComponentOccurrence Then

               Set compOcc = obj

               compOcc.Suppress

        End If

    Next
    End If
    oDoc.SelectSet.Clear

End Sub

 

 

Then if you want to do the same with an iLogic rule.

 

 

' To suppress a part

' Set reference to active document.

' This assumes the active document is an assembly

Dim oDoc As Inventor.AssemblyDocument

oDoc = ThisApplication.ActiveDocument

Dim selset As SelectSet

Dim compOcc As ComponentOccurrence




selset = oDoc.SelectSet

If selset.Count > 0 Then

Dim obj As Object

For Each obj In selset

    If TypeOf obj Is ComponentOccurrence Then

           compOcc = obj

           compOcc.Suppress

    End If

Next
End If
oDoc.SelectSet.Clear

 

 

The only change was to take it out of a Sub routine and remove the keyword set.

 

 

 

 

0 Likes
Message 5 of 7

Anonymous
Not applicable

Thanks for your reply.But my requirement is slightly different. First i need to press a command button in the user form and then go to inventor to select the part or assemblies so that it can be suppressed. Actually the code is to be written in the command button.

0 Likes
Message 6 of 7

rjay75
Collaborator
Collaborator

I understand now. You can still do it through the command button. I recommend that you use SelectEvents for object selection. A plus of this is you can set a filter to only allow selection of components. You can still use the same button to start and stop object selection. Just set a variable to state you're in selection mode and start the selection event. Then when the button is pressed again stop the selection event and process the selection. I use this method alot. It's also similar to existing Inventor behavior when you select the arrow buttons in certain command dialogs.

 

Look at the SelectEvents sample in the api.

0 Likes
Message 7 of 7

Anonymous
Not applicable

If the only reason you want to use VBA instead of iLogics is to add the button in the ribbon, here's a small macro that will launch an iLogic rule:

 

Sub StartRule()
Dim iLogicAddinGuid As String
iLogicAddinGuid = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"

Dim addin As Inventor.ApplicationAddIn
Dim tempaddin As Inventor.ApplicationAddIn
For Each addin In ThisApplication.ApplicationAddIns
If addin.ClassIdString = iLogicAddinGuid Then
Exit For
End If
Next

If (addin.Activated = False) Then
addin.Activate
End If

addin.Automation.RunRule ThisApplication.ActiveDocument, "MyRule"

End Sub

 

Naturally, you will need to change the "MyRule" for your rule name, but it should work.  

0 Likes