PartsListRow null ReferenceDocument

etckerry
Enthusiast
Enthusiast

PartsListRow null ReferenceDocument

etckerry
Enthusiast
Enthusiast

Hello all,

 

I am using VB.net to iterate a parts list and check some information for each row. Some of the information requires accessing the part document itself, so I do something like this:

 

Private Function CheckPartRow(ByRef row As PartsListRow) As Boolean
        If row.ReferencedFiles.Count = 0 Then Return True
        If Not row.ReferencedFiles(1).DocumentType = DocumentTypeEnum.kPartDocumentObject Then Return False
        If CType(row.ReferencedFiles(1).ReferencedDocument, PartDocument).ComponentDefinition.Features.Count = 0 Then Return True

...

 

This works fine for nearly all of drawings, but occasionally this routine fails when we attempt to cast the referenced document to a PartDocument, and I've determined that in such cases row.ReferencedFiles(1).ReferencedDocument is null. Also in these cases row.ReferencedFiles(1).ReferenceStatus = ReferenceStatusEnum.kUnknownReference where typically the value is ReferenceStatusEnum.kUpToDateReference.

 

Can anyone shed light on why the ReferencedDocument may be null? The file exists and row.ReferencedFiles(1).DocumentFound is True, it appears on the drawing properly, it can even be open in my workspace and I get the same results. It is unclear if this is an issue with the part file, the drawing file, or if this is expected behavior that I need to handle in my code.

 

Thanks,

 

Kerry

0 Likes
Reply
Accepted solutions (1)
139 Views
2 Replies
Replies (2)

WCrihfield
Mentor
Mentor
Accepted solution

Hi @etckerry.  I am not that familiar with the property of the PartsListRow object that you are using a lot in that code example (PartsListRow.ReferencedFiles).  It is apparently a 'hidden' property, which Autodesk warns us against using, so I have never relied on it.  But if you insist on using it, maybe switch from using 'CType()' to using 'TryCast()' to capture the immediate object to a variable, then check if it obtained anything, before moving on in your code process.

 

Below is a custom function which obtains a reference to the Document object that a PartsListRow is ultimately referencing, the traditional way.  As you can see, there are multiple opportunities for it to return Nothing, including one that I left commented out.  If the row is custom, or for a virtual component, or if there are zero occurrences left unsuppressed in the assembly, then it might not be able to get a document from that row.

Function GetPartsListRowDoc(oPLRow As PartsListRow) As Inventor.Document
	If oPLRow Is Nothing OrElse oPLRow.Custom Then Return Nothing
	Dim oRefDBOMRow As DrawingBOMRow = oPLRow.ReferencedRows.Item(1)
	If oRefDBOMRow.Custom OrElse oRefDBOMRow.Virtual Then Return Nothing
	Dim oRefBOMRow As BOMRow = oRefDBOMRow.BOMRow
	'If oRefBOMRow.ItemQuantity = 0 Then Return Nothing
	Dim oCDs As ComponentDefinitionsEnumerator = oRefBOMRow.ComponentDefinitions
	If oCDs Is Nothing OrElse oCDs.Count = 0 Then Return Nothing
	Dim oCD As ComponentDefinition = oCDs.Item(1)
	If oCD.Document IsNot Nothing Then
		Return TryCast(oCD.Document, Inventor.Document)
	End If
	Return Nothing
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

etckerry
Enthusiast
Enthusiast

Hello Wesley,

 

Thank you for your reply. Using the method you provided successfully retrieves a non-null document object, even for the row that was creating an issues with my approach. Problem solved!

 

-Kerry

0 Likes