Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Hello

 

I think the code is almost correct. I've added an additional check. If the name of the model state is not "master" and also the name of the model state does not contain "In-Process", it will place the component with this model state.

 

Option Explicit On
If ThisDoc.Document.DocumentType = kAssemblyDocumentObject
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oDef As AssemblyComponentDefinition = oADoc.ComponentDefinition

	Dim oPDoc As PartDocument 
	Dim oDoc As Document
	For Each oDoc In ThisApplication.Documents
		If oDoc.FullFileName = "C:\Work\Inventor\TEMP\assembly test\part test.ipt" Then
			oPDoc = DirectCast(oDoc, Inventor.PartDocument)
			Exit For
		End If
	Next
	
	Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
	Dim sActiveModelState As String= oPDef.ModelStates.ActiveModelState.Name 
	Dim ModelStateComponent(0 To 4)
	Dim addCompB = True
	Dim j As Integer = 1
	
	If oPDef.ModelStates.Count > 1
		For i = 1 To oPDef.ModelStates.Count
			If oPDef.ModelStates.Item(i).Name = sActiveModelState  Then
				' Do Nothing because the in-process part doesn't need to be added to the assembly
			Else
				If Not oPDef.ModelStates.Item(i).Name = "Master" And Not oPDef.ModelStates.Item(i).Name.Contains("In-Process") Then
					'ModelStateComponent(i-1) = Components.AddWithModelState("Part:" & i, "part test.ipt", "Section " & i, , True)
					ModelStateComponent(i - 1) = Components.AddWithModelState("Part:" & j, oPDoc.FullFileName, oPDef.ModelStates.Item(i).Name, , True)
					j += 1
				End If
			End If
		Next
	Else
		Dim componentA = Components.Add("Part:1", "part test.ipt", , True)
	End If
Else
	MessageBox.Show("This rule should only be run in an assembly document.")
End If

 


R. Krieg
RKW Solutions
www.rkw-solutions.com