Inventor.ComponentDefinition fails sometimes

Inventor.ComponentDefinition fails sometimes

Anonymous
Not applicable
2,046 Views
3 Replies
Message 1 of 4

Inventor.ComponentDefinition fails sometimes

Anonymous
Not applicable

Hello,

 

Since we have Inventor 2011 I have a strange error in my add-in. Sometimes when I open a file and run my add-in I get this error:

"Object reference not set to an instance of an object"

 

When I close the file and try it again, the same error is there. When I restart Inventor and open the same file then the problem is gone. In Inventor 2009 I have never had this error.

 

The add-in gives me the BOM structure of the active document. See the code below and the line that gives the problem.

 

        Dim oDef As Inventor.ComponentDefinition
        Dim oDoc As Inventor.Document = IV.ActiveDocument
 
            'next line gives the error:
            oDef = oDoc.ComponentDefinition

        Dim BS As Integer
        If oDef.BOMStructure = 51970 Then
            BS = 0
        ElseIf oDef.BOMStructure = 51974 Then
            BS = 1
        ElseIf oDef.BOMStructure = 51973 Then
            BS = 2
        ElseIf oDef.BOMStructure = 51971 Then
            BS = 3
        ElseIf oDef.BOMStructure = 51972 Then
            BS = 4
        End If

 I hope someone can tell me how to solve this.

 

Wim

0 Likes
Accepted solutions (1)
2,047 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

I have some more details about the problem.

 

************** Exception Text **************
System.Runtime.InteropServices.COMException (0x80020003): Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))

 

When I run the add-in to get the bom structure for an actieve assembly there is no problem. Then I open a part from that assy and run the add-in again: no problems.

But it can happen that I run the add-in again for the same assy or another assy and then the error is there.

It happens only when I make a switch from part to assy or assy to part.

It looks like that the componentdefinition is set to a part in this fall because if the first failure is on an assy, it will never work again for an assy. For parts it still works fine then.

I need a restart for Inventor to reset.

 

In IV 2009 I never had this problem. Can it be that I need to check the active document is an assy or part and the set the assembly- or partcomponentdefinition?

 

Wim

0 Likes
Message 3 of 4

Anonymous
Not applicable
Accepted solution

Just some random thoughts.  Are you calling this in an event?  Do you use some sort of global storage on this object?  I'm not really sure what the exact problem might be, but since you're getting a MEMBERNOTFOUND exception, likely it's that the active document at the time this call is made may not be an assembly or part file.

 

First thing to do is check that the ActiveDocument is actually an assembly or part.  You can check something like:

 

IV.ActiveDocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject OrElse IV.ActiveDocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject

 

Or, even better, something like this to make absolutely sure you have a component definition in hand.

 

Dim oDef as Inventor.ComponentDefinition

If IV.ActiveDocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then

   oDef = CType(IV.ActiveDocument, Inventor.AssemblyDocument).ComponentDefinition

ElseIf IV.ActiveDocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then

   oDef = CType(IV.ActiveDocument, Inventor.PartDocument).ComponentDefinition

End If


If oDef IsNot Nothing Then

'Continue your stuff

End If

 

0 Likes
Message 4 of 4

Anonymous
Not applicable

Thanks a lot Peter! This has solved my problem.

0 Likes