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: 

Occurrence Instance Property Error

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
gustavo.cassel
365 Views, 6 Replies

Occurrence Instance Property Error

Hi, my code is getting an error in a method to call the instance property of an occurrence.

On the assembly i make a recursive routine that loop through all occurrences and set a property inside each one.

This part of the code is working. As you can see in the image below. (its the browsertree of the drawing)

nodeswithproperties.PNG

But then i need to go to the drawing of this assembly, and on the views, acess each componentoccurrence and get the value that i set earlier.

Here is the code:

Private Sub Main()
    Dim oAsm As AssemblyDocument
    Set oAsm = ThisApplication.ActiveDocument
    Dim oDrawDoc As DrawingDocument
    'Set oDrawDoc = ThisApplication.Documents.Open("MY DRAWING FILE FOLDER", True)
    
    Dim oview As DrawingView
    For Each oview In oDrawDoc.ActiveSheet.DrawingViews
        Call ProcessAssemblyColor(oview, oview.ReferencedDocumentDescriptor.ReferencedDocument.ComponentDefinition.Occurrences)
    Next oview
    
End Sub

Private Sub ProcessAssemblyColor(drawview As DrawingView, Occurrences As ComponentOccurrences)

    Dim occ As ComponentOccurrence

    For Each occ In Occurrences
        If occ.Suppressed Then GoTo NextOcc 'If its supressed continue
        If TypeOf occ.Definition Is VirtualComponentDefinition Then GoTo NextOcc 'If its a virtual continue
        
        Debug.Print occ.Name
        Debug.Print occ.OccurrencePropertySetsEnabled
        Debug.Print occ.OccurrencePropertySets(1).Item(1).Value
        
SubOcc:
        If Not occ.SubOccurrences.Count = 0 Then Call ProcessAssemblyColor(drawview, occ.SubOccurrences)
NextOcc:
    Next occ
End Sub

On the row 23 and 24 its when it gives me an error.

For the first layer of the assembly it gives me the info, but when it goes more deeper it throws an error.

Can someone help me?

6 REPLIES 6
Message 2 of 7
WCrihfield
in reply to: gustavo.cassel

Hi @gustavo.cassel.  I think I see the problem.  I do not see any reason to pass the drawing view object down into the Sub routine, because it is not being used for anything, and prevents the 'recursive' routine from being able to process correctly.  I just eliminated that first input variable of your Sub routine to avoid this problem.  Also, I noticed that your line of code for setting the Value of the 'oDrawDoc' variable is currently commented out, so I don't see where it is getting its value.  See if this helps the situation.

Private Sub Main()
    Dim oAsm As AssemblyDocument
    Set oAsm = ThisApplication.ActiveDocument
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.Documents.Open("MY DRAWING FILE FOLDER", True)
    
    Dim oview As DrawingView
    For Each oview In oDrawDoc.ActiveSheet.DrawingViews
            Call ProcessAssemblyColor(oview.ReferencedDocumentDescriptor.ReferencedDocument.ComponentDefinition.Occurrences)
    Next oview
    
End Sub

Private Sub ProcessAssemblyColor(Occurrences As ComponentOccurrences)

    Dim occ As ComponentOccurrence

    For Each occ In Occurrences
        If occ.Suppressed Then GoTo NextOcc 'If its supressed continue
        If TypeOf occ.Definition Is VirtualComponentDefinition Then GoTo NextOcc 'If its a virtual continue
        
        Debug.Print occ.Name
        Debug.Print occ.OccurrencePropertySetsEnabled
        Debug.Print occ.OccurrencePropertySets(1).Item(1).Value
        
SubOcc:
        If Not occ.SubOccurrences.Count = 0 Then Call ProcessAssemblyColor(occ.SubOccurrences)
NextOcc:
    Next occ
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7
gustavo.cassel
in reply to: WCrihfield

When i only loop thorugh the first layer of assembly it works. It retrieve me the property value. As you can see in this image.

onlyfirstlayer.PNG

But when i go under that assembly called "CoresAplicações:1" it cannot retrieve me the property value again.

deeperlayers.PNG

This is the error that pop-up.

propertierror.PNG

It seems that i can only get the property in the firt layer.

Message 4 of 7
WCrihfield
in reply to: gustavo.cassel

Hi @gustavo.cassel.  I did some testing, and also ran into problems in the same two places when it came to the components within sub-assemblies.  It would through an error when you try to check the 'OccurrencePropertySetsEnabled' property, and at the line where it checks for SubOccurrences, to run it recursively.  To help work around the problems I was encoutering, I added another check at the start of the loop, to avoid trying to process a WeldsComponentDefinition, which you may encounter if any of you sub-assemblies are weldments.  And I had to change my SubOccurrences checking line from this:

If Not oComp.SubOccurrences.Count = 0 Then RecurseComponents(oComp.SubOccurrences)

...to this:

If Not oComp.SubOccurrences.Count = 0 Then RecurseComponents(oComp.Definition.Occurrences)

...which got rid of he second error message I was encountering.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 7
WCrihfield
in reply to: gustavo.cassel

Another observation I had was that when manually selecting a component within a sub-assembly, in the model browser, then right-clicking, the 'Instance Properties...' option (within the right-click menu) was greyed out, meaning you can't access it directly from within the top level assembly.  So, just for the sake of testing, I manually opened the sub-assembly, then used that manual process to add an 'instance' property to the part within that sub-assembly, saved, then closed the sub-assembly.  Then my code, ran from the top level assembly, was able to read the instance property of that part within the sub-assembly just fine.  (And I was able to edit its value by code from top level assembly rule too.)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 7

hi @gustavo.cassel ,

it's a very tricky matter, and trickier if you want to set the value and the parent occurrence has model states.

Anyway if you just want to read the OccurrencePropertySetsEnabled value, you can get the value from the native object of the occurrence proxy. (the occurrence proxy is a suboccurrence in the context of the main assembly, the native object is the occurrence proxy in the context of its parent i.e. just the occurrence. Sources: adnDevBlog, forum). 

Try this snippet, and add the occurrence proxy native object logic inside your code.

Dim oSS As SelectSet
oSS = ThisApplication.ActiveDocument.SelectSet
' Select an occurrence proxy (i.e. a sub occurrence)
Dim occ As ComponentOccurrenceProxy = oSS.Item(1)
Dim nativeOcc As ComponentOccurrence = occ.NativeObject
MsgBox(nativeOcc.OccurrencePropertySetsEnabled)

 

 

 

Message 7 of 7
gustavo.cassel
in reply to: WCrihfield

Thanks for the help!! I didn't know about this diference between oOcc.Definition.Occurrences and oOcc.SubOccurrences. With this my code was to run correctly. Thanks for the help!

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report