Referencing Occurrences in Weldment Doc Type

Referencing Occurrences in Weldment Doc Type

emanuel.c
Collaborator Collaborator
252 Views
2 Replies
Message 1 of 3

Referencing Occurrences in Weldment Doc Type

emanuel.c
Collaborator
Collaborator

I'm starting work on this piece of code. It works fine on Assembly documents but throws this error on Weldments. What am I doing wrong?

Thank you much!

 

Sub Main()
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		Messagebox.Show("Run this rule from an Assembly document!", "Error!")
		Exit Sub
	End If
	
	'Get the active assembly.	
	Dim doc As AssemblyDocument = ThisDoc.Document
	Dim refDocs = doc.ComponentDefinition.Occurrences.Cast(Of ComponentOccurrence).
	    Select(Of Document)(Function(occ) occ.ReferencedDocumentDescriptor.ReferencedDocument).
	    Distinct().ToList()

	For Each oRefDoc As Document In refDocs
	    If Not oRefDoc.IsModifiable Or _
			oRefDoc.ComponentDefinition.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Continue For
		If oRefDoc.DocumentType = kAssemblyDocumentObject Then	
			Dim oAsmPN As String = oRefDoc.PropertySets("Design Tracking Properties")("Part Number").Value
			If Left(oAsmPN, 1) = "5" Then '5000 part numbers define a Weldment
				MessageBox.Show(oAsmPN, "Assembly Part Number:")
				CompileWeldments(oRefDoc, oAsmPN)				
			ElseIf Left(oAsmPN, 1) = "2" Then	 '2000 part number define an Assembly			
				'CompileAssemblies
			End If		
		End If
	Next

End Sub

Sub CompileWeldments(oRefDoc As AssemblyDocument, oAsmPN As String)
	
	Dim refDocs = oRefDoc.ComponentDefinition.Occurrences.Cast(Of ComponentOccurrence).
	    Select(Of Document)(Function(occ)occ.ReferencedDocumentDescriptor.ReferencedDocument).
	    Distinct().ToList()
	
	For Each ooRefDoc As Document In refDocs
	    If Not ooRefDoc.IsModifiable Or _
			ooRefDoc.ComponentDefinition.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Continue For
			oPN = ooRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
		If Left(oPN, 1) = "7" Or Left (oPN, 1) = "8" Then 'get only part numbers 7000 and 8000
			MessageBox.Show("Assembly: " & oAsmPN & vbCrLf & "Part: " & oPN, "Weldment and SubPart:")
		End If
	Next
	
End Sub

 

emanuelc_0-1705930477290.png

 

0 Likes
Accepted solutions (1)
253 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @emanuel.c.  Not all components in a weldment type assembly will have a referenced document associated with them.  If there are any welds in that weldment, then there will be at least one component that just represents the welds in that assembly.  You can filter for components like that by using a line like:

If oOcc.Suppressed = False AndAlso _
(TypeOf oOcc.Definition Is WeldsComponentDefinition) Then

You first need to make sure that the component is not suppressed.  If it is suppressed, then accessing its oOcc.Definition property will throw an error.  Once you are sure it is not suppressed, then check its definition type.  There are many 'sub types' of a ComponentDefinition (see Derived Classes notes on that linked help web page).  One of those derived types is a WeldsComponentDefinition.  That will not be like other normal components, and you should not try to get what document it references, like you would with some other component types.  I usually also filter out the virtual type also (VirtualComponentDefinition), because they will not be referencing a document either.  They are just for adding another line item into your assembly's BOM.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

emanuel.c
Collaborator
Collaborator

Thank you Wesley! Also with some help from here I have it working now.

 

Sub Main()
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MessageBox.Show("Run this rule from an Assembly document!", "Error!")
		Exit Sub
	End If
	
	Dim oAsmCompDef As AssemblyComponentDefinition
	oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
	Dim oOccurrence As ComponentOccurrence
	
	For Each oOccurrence In oAsmCompDef.Occurrences
		If oOccurrence.Suppressed Then Continue For
		If TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then Continue For
		If oOccurrence.Definition.Document.IsModifiable = False Then Continue For
		If oOccurrence.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Continue For
		If oOccurrence.Definition.Type = ObjectTypeEnum.kWeldmentComponentDefinitionObject Or _
		oOccurrence.Definition.Type = ObjectTypeEnum.kAssemblyComponentDefinitionObject Then
			Dim oAsmPN As String = iProperties.Value(oOccurrence.Name, "Project", "Part Number")
			If Left(oAsmPN, 1) = "5" Then '5000 part numbers define a Weldment. SubType could be both Assm or Weldment
				'MessageBox.Show(oAsmPN, "Assembly Part Number:")
				CompileWeldments(oOccurrence, oAsmPN)				
			ElseIf Left(oAsmPN, 1) = "2" Then	 '2000 part number define an Assembly			
				'CompileAssemblies
			End If		
		End If
	Next

End Sub

Private Sub CompileWeldments(ByVal oOccurrence As ComponentOccurrence, oAsmPN As String)
	
	Dim oSubCompOcc As ComponentOccurrence
		
	For Each oSubCompOcc In oOccurrence.SubOccurrences
		If (oSubCompOcc.ReferencedDocumentDescriptor Is Nothing) Then
			'avoid weld beads in Weldments
            'MessageBox.Show(oSubCompOcc.Name & " has no reference document")
            Continue For
        End If		
		If oSubCompOcc.Suppressed Then Continue For
		If TypeOf oSubCompOcc.Definition Is VirtualComponentDefinition Then Continue For
		If oSubCompOcc.Definition.Document.IsModifiable = False Then Continue For
		If oSubCompOcc.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Continue For
			oPN = iProperties.Value(oSubCompOcc.Name, "Project", "Part Number")
			If Left(oPN, 1) = "7" Or Left (oPN, 1) = "8" Then 'get only part numbers 7000 and 8000
				MessageBox.Show("Assembly: " & oAsmPN & vbCrLf & "Part: " & oPN, "Weldment and SubPart:")
			End If
	Next
	
End Sub

 

0 Likes