Rule searches for virtual parts.

Rule searches for virtual parts.

tecnico
Contributor Contributor
253 Views
3 Replies
Message 1 of 4

Rule searches for virtual parts.

tecnico
Contributor
Contributor

Hello everyone, I've tried searching but I can't find a solution to my problem. I need to create a rule that checks if, in my assembly (only first level), there is a Virtual Part with the same name as the assembly.

 

thank 

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

WCrihfield
Mentor
Mentor

Hi @tecnico.  Here is something you can try for that task.  First, it makes sure it is working with an assembly (not a part or drawing).  Then it gets the components collection.  Then iterates through them, skipping any that are suppressed, and if a 'virtual' one is found, it compares the first part of that components name to the DisplayName of the main assembly.  If a match is found, a message will pop-up and let you know.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	For Each oOcc As ComponentOccurrence In oOccs
		If oOcc.Suppressed Then Continue For 'cant access Definition if Suppressed
		If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
			If oOcc.Name.StartsWith(oADoc.DisplayName) Then
				MsgBox("Found a Virtual component with same name as main assembly.", vbInformation, "iLogic")
			End If
		End If
	Next 'oOcc
End Sub

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
Message 3 of 4

tecnico
Contributor
Contributor

Hello, thank you for the response. I tried the rule you wrote, but I don't get any effect. I created a virtual part with the same name as the assembly from which I launch it, the rule runs but nothing happens. Rather than the assembly name, I would need it to look at the part number though.

Thank you.

0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Hi @tecnico.  OK.  Here is a slightly different version of the code, but this version compares the Part Number of the main assembly to the Part Number of the virtual component.  And also uses a Boolean to let you know if no match was found after the loop.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim sAssemblyPN As String = oADoc.PropertySets.Item(3).Item(2).Value
	Dim bFound As Boolean = False
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	For Each oOcc As ComponentOccurrence In oOccs
		If oOcc.Suppressed Then Continue For 'cant access Definition if Suppressed
		If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
			Dim oVCD As VirtualComponentDefinition = oOcc.Definition
			Dim sVOccPN As String = oVCD.PropertySets.Item(3).Item(2).Value
			If sVOccPN.Equals(sAssemblyPN, StringComparison.CurrentCultureIgnoreCase) Then
				bFound = True
				MsgBox("Found a Virtual component with same Part Number as main assembly.", vbInformation, "iLogic")
			End If
		End If
	Next 'oOcc
	If bFound = False Then
		MsgBox("Did not find a Virtual component with same Part Number as main assembly.", vbInformation, "iLogic")
	End If
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes