Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Select iPart in Assembly and change

1 REPLY 1
SOLVED
Reply
Message 1 of 2
MCADAEPFW
769 Views, 1 Reply

Select iPart in Assembly and change

I'm not sure what I am doing wrong.  I'm trying to create an iLogic rule that will enable me to select an iPart in an assembly then have it pull out all the member names to an input list box from where you can select the member name and then change the iPart to the selected member.   Here is the code I got through some trial an error.   I can get it to run up to the input box and then it goes to the 'Catch'.

You should be able to run this in any assembly that contains an iPart.   I'm trying to make it easier to change an iPart by selecting it in the graphics window without having to go to it in the Browser and expanding then going down to the Table, right-click, and select Change Component from the context menu. 

 

Any help would be great.   Sorry for the cluttered code it took a bit of work just to get the selected component to pull out the member names.  

' Start of iLogic Rule ===============================================================================
' This rule by Luke Davenport @ Excitech

'Check whether open document is an assembly and exit rule if not
oDoc = ThisDoc.ModelDocument
If oDoc.DocumentType = kPartDocumentObject Then
	MessageBox.Show("This rule can only be run in an assembly file - exiting rule", "Excitech iLogic")
	Return
End If

Dim targetOcc As ComponentOccurrence = Nothing
Do While True
	' Note the below line will prompt the user to pick an assembly occurrence (this can be a single part file or a sub-assembly of the active document...kAssemblyOccurrenceFilter
	targetOcc = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select Occurrence to Modify...(Press ESC to cancel)")
	If Not targetOcc Is Nothing Then
		Exit Do
	Else
		Dim Res As MsgBoxResult = MsgBox("No model selected - exit?", 36, "Excitech iLogic")
		If Res = vbYes Then
			Return
		Else
			' Do nothing - keep on looping...
		End If
	End If
Loop
'targetOcc.IsiPartFactory Or 
			If targetOcc.IsiPartMember Then
				MessageBox.Show("This IS an iPart", "Title")
'				Return
			Else
				MessageBox.Show("This is NOT an iPart", "Title")
				Return 'do nothing
			End If

Dim compDef As PartComponentDefinition = targetOcc.Definition
Dim compiPartMem As iPartMember = compDef.iPartMember
Dim oFactory As iPartFactory = compiPartMem.ParentFactory

Try
	Dim ChangePart As New ArrayList

	' Iterate through the rows
'	Dim oRow As iPartTableRow
	For Each oRow As iPartTableRow In oFactory.TableRows
		'rowNumber 
		i = oRow.Index
		' Make this the active row so the model will recompute.
'		If i > 0 Then
'			oFactory.DefaultRow = oRow
			xVal = oRow.MemberName
			ChangePart.Add(xVal)
'		End If
	Next
	Dim NewRow As VariantType
	NewRow = InputListBox("Prompt", ChangePart, NewRow, Title := "Title", ListName := "Change Row")
	
'	comiPartMem.ChangeRowOfiPartMember(NewRow)
	

Catch

	targetName = iPart.RowName(comiPartMem) '   Name()
	MessageBox.Show(targetName)

End Try

' End of iLogic Rule ===============================================================================

 

1 REPLY 1
Message 2 of 2
JhoelForshav
in reply to: MCADAEPFW

Hi @MCADAEPFW 

Try changing variable NewRow to a string and then use function ChangeRowOfiPartMember on the occurrence object.

I also changed the code so that the active row will be pre-selected in the listbox 🙂

 

' Start of iLogic Rule ===============================================================================
' This rule by Luke Davenport @ Excitech

'Check whether open document is an assembly and exit rule if not
oDoc = ThisDoc.ModelDocument
If oDoc.DocumentType = kPartDocumentObject Then
	MessageBox.Show("This rule can only be run in an assembly file - exiting rule", "Excitech iLogic")
	Return
End If

Dim targetOcc As ComponentOccurrence = Nothing
Do While True
	' Note the below line will prompt the user to pick an assembly occurrence (this can be a single part file or a sub-assembly of the active document...kAssemblyOccurrenceFilter
	targetOcc = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select Occurrence to Modify...(Press ESC to cancel)")
	If Not targetOcc Is Nothing Then
		Exit Do
	Else
		Dim Res As MsgBoxResult = MsgBox("No model selected - exit?", 36, "Excitech iLogic")
		If Res = vbYes Then
			Return
		Else
			' Do nothing - keep on looping...
		End If
	End If
Loop
'targetOcc.IsiPartFactory Or 
			If targetOcc.IsiPartMember Then
				MessageBox.Show("This IS an iPart", "Title")
'				Return
			Else
				MessageBox.Show("This is NOT an iPart", "Title")
				Return 'do nothing
			End If

Dim compDef As PartComponentDefinition = targetOcc.Definition
Dim compiPartMem As iPartMember = compDef.iPartMember
Dim oFactory As iPartFactory = compiPartMem.ParentFactory

Try
	Dim ChangePart As New ArrayList

	' Iterate through the rows
'	Dim oRow As iPartTableRow
	For Each oRow As iPartTableRow In oFactory.TableRows
		'rowNumber 
		i = oRow.Index
		' Make this the active row so the model will recompute.
'		If i > 0 Then
'			oFactory.DefaultRow = oRow
			xVal = oRow.MemberName
			ChangePart.Add(xVal)
'		End If
	Next
	Dim NewRow As String
	NewRow = InputListBox("Prompt", ChangePart, compiPartMem.Row.MemberName, Title := "Title", ListName := "Change Row")
	targetOcc.ChangeRowOfiPartMember(NewRow)
	

Catch

	targetName = iPart.RowName(comiPartMem) '   Name()
	MessageBox.Show(targetName)

End Try

' End of iLogic Rule ===============================================================================

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report