Change member of Content Center part with iLogic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Good morning, good afternoon and good evening all,
I'm currently wanting to implement some code in iLogic to replace content center parts in an assembly with other members of themselves (i.e. trying to automate the "Change Size" command with iLogic).
The following code is trying to do this by tapping into the API, but I've gotten stuck with it. Would appreciate any help and expertise.
____________________________________________________________________________
Sub Main()
' Example usage - Call the function to update the part
UpdateContentCenterMember("Fixtures", "Legs", "Marine Leg 180", "SAA")
End Sub
Sub UpdateContentCenterMember(Category As String, Group As String, FamilyName As String, FinishType As String)
' Get application and active document
Dim oApp As Inventor.Application
oApp = ThisApplication
Dim oMainAsm As AssemblyDocument
oMainAsm = oApp.ActiveDocument
Dim oAsmDef As AssemblyComponentDefinition
oAsmDef = oMainAsm.ComponentDefinition
' Get all leaf occurrences (parts without sub-occurrences)
Dim leafOccurrences As List(Of ComponentOccurrence)
leafOccurrences = GetLeafOccurrences(oAsmDef.Occurrences)
Dim oContentCenter As ContentCenter
oContentCenter = oApp.ContentCenter
' Iterate over all leaf occurrences and check if they are Content Center parts
For Each oOcc As ComponentOccurrence In leafOccurrences
If oOcc.Definition.IsContentMember Then
Dim oDef As PartComponentDefinition
oDef = oOcc.Definition
Dim oDoc As PartDocument
oDoc = oDef.Document
' Reference to the Content Library Component Properties
Dim oPropSets As PropertySets
oPropSets = oDoc.PropertySets
Dim oProps As PropertySet
oProps = oPropSets.Item("Content Library Component Properties")
' Get FamilyId
Dim oProp As Inventor.Property
oProp = oProps.Item("FamilyId")
Dim FamilyId As String
FamilyId = oProp.Value
Debug.Print("FamilyId: " + FamilyId)
' Reference to the ContentFamily
Dim oFamily As ContentFamily
oFamily = oContentCenter.GetContentObject("v3#" + FamilyId + "#")
Debug.Print("Content Family DisplayName: " + oFamily.DisplayName)
' Get current MemberId (row) for the part
oProp = oProps.Item("MemberId")
Dim MemberId As String
MemberId = oProp.Value
Debug.Print("Current MemberId (row): " + MemberId)
' Find column for PARTNUMBER
Dim nCol As Integer
nCol = -1
For i = 1 To oFamily.TableColumns.Count
If oFamily.TableColumns.Item(i).InternalName = "PARTNUMBER" Then
nCol = i
Exit For
End If
Next
If nCol < 0 Then
MessageBox.Show("Error: PARTNUMBER column not found!", "Error")
Exit Sub
End If
' Get table row for the current member
Dim oRow As ContentTableRow
oRow = oFamily.TableRows.Item(MemberId)
Dim partNumber As String
partNumber = oRow.Item(nCol).Value
Debug.Print("Member PartNumber: " & partNumber)
' Select a new member row (this could be dynamically set based on a condition)
Dim NewRow As Integer
NewRow = 3 ' <--- Example: Set dynamically based on your logic
' Create a new member based on the selected row
Dim Error1 As MemberManagerErrorsEnum
Dim strErrorMessage As String
Dim strContentPartFileName As String
' Correct the method call
Dim RefreshEnum As ContentMemberRefreshEnum
RefreshEnum = ContentMemberRefreshEnum.kUseDefaultRefreshSetting ' Correct enum member
strContentPartFileName = oFamily.CreateMember(NewRow, Error1, strErrorMessage, RefreshEnum, False, "", Nothing, Nothing)
If strErrorMessage <> "" Then
MessageBox.Show("Error creating member: " & strErrorMessage, "Error")
Exit Sub
End If
' Replace occurrence with the new part
oOcc.Replace(strContentPartFileName, False)
Beep
' Optional: Set the finish type (if it exists as a parameter)
Dim oFinish As Inventor.Property
On Error Resume Next
oFinish = oDoc.PropertySets.Item("Design Tracking Properties").Item("Finish")
On Error GoTo 0
If Not oFinish Is Nothing Then
oFinish.Value = FinishType
Debug.Print("Finish updated to: " & FinishType)
End If
End If
Next
End Sub
' Function to retrieve all leaf occurrences in the assembly
Function GetLeafOccurrences(Occurrences As ComponentOccurrences) As List(Of ComponentOccurrence)
Dim leafOccurrences As New List(Of ComponentOccurrence)
For Each oOccurrence As ComponentOccurrence In Occurrences
If oOccurrence.SubOccurrences.Count = 0 Then ' If no children, it's a leaf
leafOccurrences.Add(oOccurrence)
Else
' Recursively get leaf occurrences
leafOccurrences.AddRange(GetLeafOccurrences(oOccurrence.SubOccurrences))
End If
Next
Return leafOccurrences
End Function